From 78ef3664f76a98ba871d322468877b72499cbe52 Mon Sep 17 00:00:00 2001 From: blue Date: Sat, 7 Jan 2023 17:30:22 +0300 Subject: [PATCH] some initial delegate stuff --- ui/widgets/vcard/omemo/CMakeLists.txt | 2 + ui/widgets/vcard/omemo/keydelegate.cpp | 71 ++++++++++++++++++++++++++ ui/widgets/vcard/omemo/keydelegate.h | 39 ++++++++++++++ ui/widgets/vcard/omemo/keysmodel.cpp | 3 ++ ui/widgets/vcard/omemo/omemo.cpp | 14 +++++ ui/widgets/vcard/omemo/omemo.h | 3 ++ ui/widgets/vcard/omemo/omemo.ui | 23 ++++++++- ui/widgets/vcard/vcard.ui | 8 +-- 8 files changed, 157 insertions(+), 6 deletions(-) create mode 100644 ui/widgets/vcard/omemo/keydelegate.cpp create mode 100644 ui/widgets/vcard/omemo/keydelegate.h diff --git a/ui/widgets/vcard/omemo/CMakeLists.txt b/ui/widgets/vcard/omemo/CMakeLists.txt index 19a9fe9..e2ade51 100644 --- a/ui/widgets/vcard/omemo/CMakeLists.txt +++ b/ui/widgets/vcard/omemo/CMakeLists.txt @@ -4,4 +4,6 @@ target_sources(squawk PRIVATE omemo.ui keysmodel.cpp keysmodel.h + keydelegate.cpp + keydelegate.h ) diff --git a/ui/widgets/vcard/omemo/keydelegate.cpp b/ui/widgets/vcard/omemo/keydelegate.cpp new file mode 100644 index 0000000..1816ecd --- /dev/null +++ b/ui/widgets/vcard/omemo/keydelegate.cpp @@ -0,0 +1,71 @@ +// Squawk messenger. +// Copyright (C) 2019 Yury Gubich +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +#include "keydelegate.h" +#include +#include + +#include "keysmodel.h" + +constexpr int minHeight = 50; +constexpr int minWidth = 400; + +UI::KeyDelegate::KeyDelegate(QObject* parent): + QStyledItemDelegate(parent), + fingerPrintFont(QFontDatabase::systemFont(QFontDatabase::FixedFont)) +{ +} + +UI::KeyDelegate::~KeyDelegate() {} + + +void UI::KeyDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const { + painter->save(); + painter->setRenderHint(QPainter::Antialiasing, true); + + bool hover = option.state & QStyle::State_MouseOver; + if (hover) { + painter->save(); + painter->fillRect(option.rect, option.palette.brush(QPalette::Inactive, QPalette::Highlight)); + painter->restore(); + } + + QVariant fingerPrintV = index.data(UI::KeysModel::FingerPrint); + if (fingerPrintV.isValid()) { + painter->save(); + QByteArray fingerPrint = fingerPrintV.toByteArray(); + + painter->setFont(fingerPrintFont); + painter->drawText(option.rect, option.displayAlignment | Qt::AlignTop, fingerPrint.toHex()); + + painter->restore(); + } + painter->drawText(option.rect, option.displayAlignment, index.data().toString()); + + painter->restore(); +} + +QSize UI::KeyDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const { + QSize size = QStyledItemDelegate::sizeHint(option, index); + + if (size.width() < minWidth) + size.setWidth(minWidth); + + if (size.height() < minHeight) + size.setHeight(minHeight); + + return size; +} diff --git a/ui/widgets/vcard/omemo/keydelegate.h b/ui/widgets/vcard/omemo/keydelegate.h new file mode 100644 index 0000000..d1f768b --- /dev/null +++ b/ui/widgets/vcard/omemo/keydelegate.h @@ -0,0 +1,39 @@ +// Squawk messenger. +// Copyright (C) 2019 Yury Gubich +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +#ifndef UI_KEYDELEGATE_H +#define UI_KEYDELEGATE_H + +#include + +namespace UI { + +class KeyDelegate : public QStyledItemDelegate +{ +public: + KeyDelegate(QObject *parent = nullptr); + ~KeyDelegate(); + + QSize sizeHint(const QStyleOptionViewItem & option, const QModelIndex & index) const override; + void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const override; + +private: + QFont fingerPrintFont; +}; + +} + +#endif // UI_KEYDELEGATE_H diff --git a/ui/widgets/vcard/omemo/keysmodel.cpp b/ui/widgets/vcard/omemo/keysmodel.cpp index b70fefd..4184133 100644 --- a/ui/widgets/vcard/omemo/keysmodel.cpp +++ b/ui/widgets/vcard/omemo/keysmodel.cpp @@ -46,6 +46,9 @@ QVariant UI::KeysModel::data(const QModelIndex& index, int role) const { case Qt::DisplayRole: answer = keys[i]->label; break; + case FingerPrint: + answer = keys[i]->fingerPrint; + break; } return answer; diff --git a/ui/widgets/vcard/omemo/omemo.cpp b/ui/widgets/vcard/omemo/omemo.cpp index 8a1e1cc..b2db902 100644 --- a/ui/widgets/vcard/omemo/omemo.cpp +++ b/ui/widgets/vcard/omemo/omemo.cpp @@ -17,9 +17,14 @@ #include "omemo.h" #include "ui_omemo.h" +#include +constexpr uint8_t fingerprintLength = 24; + Omemo::Omemo(QWidget* parent): QWidget(parent), m_ui(new Ui::Omemo()), + keysDelegate(), + unusedKeysDelegate(), keysModel(), unusedKeysModel() { @@ -27,7 +32,9 @@ Omemo::Omemo(QWidget* parent): generateMockData(); + m_ui->keysView->setItemDelegate(&keysDelegate); m_ui->keysView->setModel(&keysModel); + m_ui->unusedKeysView->setItemDelegate(&unusedKeysDelegate); m_ui->unusedKeysView->setModel(&unusedKeysModel); } @@ -38,10 +45,17 @@ Omemo::~Omemo() void Omemo::generateMockData() { + std::random_device rd; + std::uniform_int_distribution dist(CHAR_MIN, CHAR_MAX); for (int i = 0; i < 5; ++i) { + QByteArray fp(fingerprintLength, 0); + for (int i = 0; i < fingerprintLength; ++i) { + fp[i] = dist(rd); + } Shared::KeyInfo info; info.id = i; info.label = QString("test_") + std::to_string(i).c_str(); + info.fingerPrint = fp; keysModel.addKey(info); } } diff --git a/ui/widgets/vcard/omemo/omemo.h b/ui/widgets/vcard/omemo/omemo.h index d01f4a8..2bc0433 100644 --- a/ui/widgets/vcard/omemo/omemo.h +++ b/ui/widgets/vcard/omemo/omemo.h @@ -21,6 +21,7 @@ #include #include "keysmodel.h" +#include "keydelegate.h" namespace Ui { @@ -38,6 +39,8 @@ private: private: QScopedPointer m_ui; + UI::KeyDelegate keysDelegate; + UI::KeyDelegate unusedKeysDelegate; UI::KeysModel keysModel; UI::KeysModel unusedKeysModel; }; diff --git a/ui/widgets/vcard/omemo/omemo.ui b/ui/widgets/vcard/omemo/omemo.ui index 7052486..cf72e0f 100644 --- a/ui/widgets/vcard/omemo/omemo.ui +++ b/ui/widgets/vcard/omemo/omemo.ui @@ -63,9 +63,28 @@ 592 - + - + + + QAbstractScrollArea::AdjustToContents + + + false + + + QAbstractItemView::SelectColumns + + + QAbstractItemView::ScrollPerPixel + + + QAbstractItemView::ScrollPerItem + + + true + + diff --git a/ui/widgets/vcard/vcard.ui b/ui/widgets/vcard/vcard.ui index b71d262..7e09257 100644 --- a/ui/widgets/vcard/vcard.ui +++ b/ui/widgets/vcard/vcard.ui @@ -10,7 +10,7 @@ 0 0 578 - 671 + 748 @@ -84,7 +84,7 @@ QTabWidget::Rounded - 0 + 1 Qt::ElideNone @@ -566,8 +566,8 @@ 0 0 - 566 - 498 + 545 + 544