1
0
forked from blue/squawk

some initial delegate stuff

This commit is contained in:
Blue 2023-01-07 17:30:22 +03:00
parent 5aa0f4bca9
commit 78ef3664f7
Signed by untrusted user: blue
GPG Key ID: 9B203B252A63EE38
8 changed files with 157 additions and 6 deletions

View File

@ -4,4 +4,6 @@ target_sources(squawk PRIVATE
omemo.ui omemo.ui
keysmodel.cpp keysmodel.cpp
keysmodel.h keysmodel.h
keydelegate.cpp
keydelegate.h
) )

View File

@ -0,0 +1,71 @@
// Squawk messenger.
// Copyright (C) 2019 Yury Gubich <blue@macaw.me>
//
// 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 <http://www.gnu.org/licenses/>.
#include "keydelegate.h"
#include <QPainter>
#include <QFontDatabase>
#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;
}

View File

@ -0,0 +1,39 @@
// Squawk messenger.
// Copyright (C) 2019 Yury Gubich <blue@macaw.me>
//
// 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 <http://www.gnu.org/licenses/>.
#ifndef UI_KEYDELEGATE_H
#define UI_KEYDELEGATE_H
#include <QStyledItemDelegate>
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

View File

@ -46,6 +46,9 @@ QVariant UI::KeysModel::data(const QModelIndex& index, int role) const {
case Qt::DisplayRole: case Qt::DisplayRole:
answer = keys[i]->label; answer = keys[i]->label;
break; break;
case FingerPrint:
answer = keys[i]->fingerPrint;
break;
} }
return answer; return answer;

View File

@ -17,9 +17,14 @@
#include "omemo.h" #include "omemo.h"
#include "ui_omemo.h" #include "ui_omemo.h"
#include <random>
constexpr uint8_t fingerprintLength = 24;
Omemo::Omemo(QWidget* parent): Omemo::Omemo(QWidget* parent):
QWidget(parent), QWidget(parent),
m_ui(new Ui::Omemo()), m_ui(new Ui::Omemo()),
keysDelegate(),
unusedKeysDelegate(),
keysModel(), keysModel(),
unusedKeysModel() unusedKeysModel()
{ {
@ -27,7 +32,9 @@ Omemo::Omemo(QWidget* parent):
generateMockData(); generateMockData();
m_ui->keysView->setItemDelegate(&keysDelegate);
m_ui->keysView->setModel(&keysModel); m_ui->keysView->setModel(&keysModel);
m_ui->unusedKeysView->setItemDelegate(&unusedKeysDelegate);
m_ui->unusedKeysView->setModel(&unusedKeysModel); m_ui->unusedKeysView->setModel(&unusedKeysModel);
} }
@ -38,10 +45,17 @@ Omemo::~Omemo()
void Omemo::generateMockData() void Omemo::generateMockData()
{ {
std::random_device rd;
std::uniform_int_distribution<char> dist(CHAR_MIN, CHAR_MAX);
for (int i = 0; i < 5; ++i) { 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; Shared::KeyInfo info;
info.id = i; info.id = i;
info.label = QString("test_") + std::to_string(i).c_str(); info.label = QString("test_") + std::to_string(i).c_str();
info.fingerPrint = fp;
keysModel.addKey(info); keysModel.addKey(info);
} }
} }

View File

@ -21,6 +21,7 @@
#include <QScopedPointer> #include <QScopedPointer>
#include "keysmodel.h" #include "keysmodel.h"
#include "keydelegate.h"
namespace Ui namespace Ui
{ {
@ -38,6 +39,8 @@ private:
private: private:
QScopedPointer<Ui::Omemo> m_ui; QScopedPointer<Ui::Omemo> m_ui;
UI::KeyDelegate keysDelegate;
UI::KeyDelegate unusedKeysDelegate;
UI::KeysModel keysModel; UI::KeysModel keysModel;
UI::KeysModel unusedKeysModel; UI::KeysModel unusedKeysModel;
}; };

View File

@ -63,9 +63,28 @@
<height>592</height> <height>592</height>
</rect> </rect>
</property> </property>
<layout class="QGridLayout" name="gridLayout"> <layout class="QGridLayout" name="gridLayout" columnstretch="1,3,1">
<item row="1" column="1"> <item row="1" column="1">
<widget class="QListView" name="keysView"/> <widget class="QListView" name="keysView">
<property name="sizeAdjustPolicy">
<enum>QAbstractScrollArea::AdjustToContents</enum>
</property>
<property name="showDropIndicator" stdset="0">
<bool>false</bool>
</property>
<property name="selectionBehavior">
<enum>QAbstractItemView::SelectColumns</enum>
</property>
<property name="verticalScrollMode">
<enum>QAbstractItemView::ScrollPerPixel</enum>
</property>
<property name="horizontalScrollMode">
<enum>QAbstractItemView::ScrollPerItem</enum>
</property>
<property name="uniformItemSizes">
<bool>true</bool>
</property>
</widget>
</item> </item>
<item row="3" column="1"> <item row="3" column="1">
<widget class="Line" name="line"> <widget class="Line" name="line">

View File

@ -10,7 +10,7 @@
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>578</width> <width>578</width>
<height>671</height> <height>748</height>
</rect> </rect>
</property> </property>
<layout class="QGridLayout" name="gridLayout_4"> <layout class="QGridLayout" name="gridLayout_4">
@ -84,7 +84,7 @@
<enum>QTabWidget::Rounded</enum> <enum>QTabWidget::Rounded</enum>
</property> </property>
<property name="currentIndex"> <property name="currentIndex">
<number>0</number> <number>1</number>
</property> </property>
<property name="elideMode"> <property name="elideMode">
<enum>Qt::ElideNone</enum> <enum>Qt::ElideNone</enum>
@ -566,8 +566,8 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>566</width> <width>545</width>
<height>498</height> <height>544</height>
</rect> </rect>
</property> </property>
<layout class="QGridLayout" name="gridLayout_3" columnstretch="1,3,1"> <layout class="QGridLayout" name="gridLayout_3" columnstretch="1,3,1">