trust level display in delegate, list size tweaking

This commit is contained in:
Blue 2023-01-14 18:34:14 +03:00
parent d4bf7e599a
commit b72a837754
Signed by untrusted user: blue
GPG Key ID: 9B203B252A63EE38
10 changed files with 203 additions and 27 deletions

View File

@ -144,6 +144,8 @@ enum class TrustLevel {
authenticated authenticated
}; };
Q_ENUM_NS(TrustLevel) Q_ENUM_NS(TrustLevel)
static const TrustLevel TrustLevelHighest = TrustLevel::undecided;
static const TrustLevel TrustLevelLowest = TrustLevel::authenticated;
enum class EncryptionProtocol { enum class EncryptionProtocol {
omemo omemo

View File

@ -113,6 +113,14 @@ Shared::Global::Global():
tr("Always Ask", "AccountPassword"), tr("Always Ask", "AccountPassword"),
tr("KWallet", "AccountPassword") tr("KWallet", "AccountPassword")
}), }),
trustLevel({
tr("Undecided", "TrustLevel"),
tr("Automatically distrusted", "TrustLevel"),
tr("Manually distrusted", "TrustLevel"),
tr("Automatically trusted", "TrustLevel"),
tr("Manually trusted", "TrustLevel"),
tr("Authenticated", "TrustLevel")
}),
accountPasswordDescription({ accountPasswordDescription({
tr("Your password is going to be stored in config file in plain text", "AccountPasswordDescription"), tr("Your password is going to be stored in config file in plain text", "AccountPasswordDescription"),
tr("Your password is going to be stored in config file but jammed with constant encryption key you can find in program source code. It might look like encryption but it's not", "AccountPasswordDescription"), tr("Your password is going to be stored in config file but jammed with constant encryption key you can find in program source code. It might look like encryption but it's not", "AccountPasswordDescription"),
@ -123,10 +131,12 @@ Shared::Global::Global():
defaultSystemPalette(QApplication::palette()), defaultSystemPalette(QApplication::palette()),
omemoSupport(OMEMO_SUPPORT), omemoSupport(OMEMO_SUPPORT),
defaultFont(QFontDatabase::systemFont(QFontDatabase::GeneralFont)), defaultFont(QFontDatabase::systemFont(QFontDatabase::GeneralFont)),
monospaceFont(QFontDatabase::systemFont(QFontDatabase::FixedFont)),
smallFont(getFont(QFontDatabase::SmallestReadableFont, false, true)), smallFont(getFont(QFontDatabase::SmallestReadableFont, false, true)),
headerFont(getFont(QFontDatabase::TitleFont, true, false, 1.1)), headerFont(getFont(QFontDatabase::TitleFont, true, false, 1.1)),
titleFont(getFont(QFontDatabase::TitleFont, true, false, 1.3)), titleFont(getFont(QFontDatabase::TitleFont, true, false, 1.3)),
defaultFontMetrics(defaultFont), defaultFontMetrics(defaultFont),
monospaceMetrics(monospaceFont),
smallFontMetrics(smallFont), smallFontMetrics(smallFont),
headerFontMetrics(headerFont), headerFontMetrics(headerFont),
titleFontMetrics(titleFont), titleFontMetrics(titleFont),
@ -256,6 +266,11 @@ QString Shared::Global::getName(Shared::AccountPassword ap)
return instance->accountPassword[static_cast<int>(ap)]; return instance->accountPassword[static_cast<int>(ap)];
} }
QString Shared::Global::getName(Shared::TrustLevel tl)
{
return instance->trustLevel[static_cast<int>(tl)];
}
void Shared::Global::setSupported(const QString& pluginName, bool support) void Shared::Global::setSupported(const QString& pluginName, bool support)
{ {
std::map<QString, bool>::iterator itr = instance->pluginSupport.find(pluginName); std::map<QString, bool>::iterator itr = instance->pluginSupport.find(pluginName);
@ -405,3 +420,4 @@ FROM_INT_INPL(Shared::SubscriptionState)
FROM_INT_INPL(Shared::AccountPassword) FROM_INT_INPL(Shared::AccountPassword)
FROM_INT_INPL(Shared::Avatar) FROM_INT_INPL(Shared::Avatar)
FROM_INT_INPL(Shared::Availability) FROM_INT_INPL(Shared::Availability)
FROM_INT_INPL(Shared::TrustLevel)

View File

@ -74,6 +74,7 @@ namespace Shared {
static QString getName(Role rl); static QString getName(Role rl);
static QString getName(Message::State rl); static QString getName(Message::State rl);
static QString getName(AccountPassword ap); static QString getName(AccountPassword ap);
static QString getName(TrustLevel tl);
static QString getDescription(AccountPassword ap); static QString getDescription(AccountPassword ap);
@ -84,6 +85,7 @@ namespace Shared {
const std::deque<QString> role; const std::deque<QString> role;
const std::deque<QString> messageState; const std::deque<QString> messageState;
const std::deque<QString> accountPassword; const std::deque<QString> accountPassword;
const std::deque<QString> trustLevel;
const std::deque<QString> accountPasswordDescription; const std::deque<QString> accountPasswordDescription;
@ -103,10 +105,12 @@ namespace Shared {
static void setStyle(const QString& style); static void setStyle(const QString& style);
const bool omemoSupport; const bool omemoSupport;
QFont defaultFont; QFont defaultFont;
QFont monospaceFont;
QFont smallFont; QFont smallFont;
QFont headerFont; QFont headerFont;
QFont titleFont; QFont titleFont;
QFontMetrics defaultFontMetrics; QFontMetrics defaultFontMetrics;
QFontMetrics monospaceMetrics;
QFontMetrics smallFontMetrics; QFontMetrics smallFontMetrics;
QFontMetrics headerFontMetrics; QFontMetrics headerFontMetrics;
QFontMetrics titleFontMetrics; QFontMetrics titleFontMetrics;

View File

@ -15,4 +15,6 @@ target_sources(squawk PRIVATE
resizer.h resizer.h
shadowoverlay.cpp shadowoverlay.cpp
shadowoverlay.h shadowoverlay.h
expandinglist.cpp
expandinglist.h
) )

View File

@ -0,0 +1,43 @@
// 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 "expandinglist.h"
QSize ExpandingList::viewportSizeHint() const {
if (QAbstractItemView::sizeAdjustPolicy() != QAbstractScrollArea::AdjustToContents)
return QListView::viewportSizeHint();
if (model() == nullptr)
return QSize(0, 0);
if (model()->rowCount() == 0)
return QSize(0, 0);
#if (QT_VERSION < QT_VERSION_CHECK(6, 2, 0))
const int rowCount = model()->rowCount();
int height = 0;
for (int i = 0; i < rowCount; i++) {
height += QListView::sizeHintForRow(i);
}
return QSize(QListView::viewportSizeHint().width(), height);
#else
return QListView::viewportSizeHint();
#endif
}
QSize ExpandingList::minimumSizeHint() const {
return viewportSizeHint();
}

31
ui/utils/expandinglist.h Normal file
View File

@ -0,0 +1,31 @@
// 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 EXPANDINGLIST_H
#define EXPANDINGLIST_H
#include <QListView>
class ExpandingList : public QListView {
Q_OBJECT
public:
using QListView::QListView; //explicit constructor inheriatnce
QSize viewportSizeHint() const override;
QSize minimumSizeHint() const override;
};
#endif // EXPANDINGLIST_H

View File

@ -16,6 +16,7 @@
#include "keydelegate.h" #include "keydelegate.h"
#include <QPainter> #include <QPainter>
#include <QDebug>
#include "keysmodel.h" #include "keysmodel.h"
#include <shared/global.h> #include <shared/global.h>
@ -26,10 +27,12 @@ constexpr uint8_t maxSingleLineParts = 3;
UI::KeyDelegate::KeyDelegate(QObject* parent): UI::KeyDelegate::KeyDelegate(QObject* parent):
QStyledItemDelegate(parent), QStyledItemDelegate(parent),
fingerPrintFont(Shared::Global::getInstance()->defaultFont), defaultFont(Shared::Global::getInstance()->defaultFont),
fingerPrintFont(Shared::Global::getInstance()->monospaceFont),
labelFont(Shared::Global::getInstance()->smallFont), labelFont(Shared::Global::getInstance()->smallFont),
fingerPrintMetrics(Shared::Global::getInstance()->defaultFontMetrics), defaultMetrics(Shared::Global::getInstance()->defaultFontMetrics),
labelFontMetrics(Shared::Global::getInstance()->smallFontMetrics), fingerPrintMetrics(Shared::Global::getInstance()->monospaceMetrics),
labelMetrics(Shared::Global::getInstance()->smallFontMetrics),
spaceWidth(fingerPrintMetrics.horizontalAdvance(" ")) spaceWidth(fingerPrintMetrics.horizontalAdvance(" "))
{} {}
@ -47,11 +50,14 @@ void UI::KeyDelegate::paint(QPainter* painter, const QStyleOptionViewItem& optio
painter->restore(); painter->restore();
} }
QRect r;
int maxRight = 0;
int leftOrigin = option.rect.left() + margin;
QColor q = painter->pen().color(); QColor q = painter->pen().color();
q.setAlpha(180); q.setAlpha(180);
QRect rect = option.rect; QRect rect = option.rect;
rect.adjust(margin, margin, 0, 0); rect.adjust(margin, margin, -margin, -margin);
QVariant labelV = index.data(UI::KeysModel::Label); QVariant labelV = index.data(UI::KeysModel::Label);
if (labelV.isValid()) { if (labelV.isValid()) {
QString label = labelV.toString(); QString label = labelV.toString();
@ -59,8 +65,9 @@ void UI::KeyDelegate::paint(QPainter* painter, const QStyleOptionViewItem& optio
painter->save(); painter->save();
painter->setFont(labelFont); painter->setFont(labelFont);
painter->setPen(q); painter->setPen(q);
painter->drawText(rect, Qt::AlignLeft | Qt::AlignTop, label); painter->drawText(rect, Qt::AlignLeft | Qt::AlignTop, label, &r);
rect.adjust(0, labelFontMetrics.lineSpacing(), 0, 0); rect.adjust(0, labelMetrics.lineSpacing(), 0, 0);
maxRight = std::max(r.width(), maxRight);
painter->restore(); painter->restore();
} }
} }
@ -72,7 +79,6 @@ void UI::KeyDelegate::paint(QPainter* painter, const QStyleOptionViewItem& optio
QByteArray fingerPrint = fingerPrintV.toByteArray(); QByteArray fingerPrint = fingerPrintV.toByteArray();
std::vector<QString> parts = getParts(fingerPrint); std::vector<QString> parts = getParts(fingerPrint);
uint8_t partsLength = parts.size(); uint8_t partsLength = parts.size();
QRect r;
uint8_t firstLine; uint8_t firstLine;
if (partsLength > maxSingleLineParts) if (partsLength > maxSingleLineParts)
firstLine = partsLength / 2 + partsLength % 2; firstLine = partsLength / 2 + partsLength % 2;
@ -81,15 +87,17 @@ void UI::KeyDelegate::paint(QPainter* painter, const QStyleOptionViewItem& optio
for (uint8_t i = 0; i < partsLength; ++i) { for (uint8_t i = 0; i < partsLength; ++i) {
if (i == firstLine) { if (i == firstLine) {
rect.setLeft(option.rect.left() + margin); maxRight = std::max(rect.left() - leftOrigin - margin, maxRight);
rect.setLeft(leftOrigin);
rect.adjust(0, r.height() + fingerPrintMetrics.leading(), 0, 0); rect.adjust(0, r.height() + fingerPrintMetrics.leading(), 0, 0);
} }
painter->drawText(rect, Qt::AlignLeft | Qt::AlignTop, parts[i], &r); painter->drawText(rect, Qt::AlignLeft | Qt::AlignTop, parts[i], &r);
rect.adjust(r.width() + spaceWidth ,0, 0, 0); rect.adjust(r.width() + spaceWidth ,0, 0, 0);
} }
maxRight = std::max(rect.left() - leftOrigin - margin, maxRight);
rect.adjust(0, r.height() + fingerPrintMetrics.leading(), 0, 0); rect.adjust(0, r.height() + fingerPrintMetrics.leading(), 0, 0);
rect.setLeft(option.rect.left() + margin); rect.setLeft(leftOrigin);
painter->restore(); painter->restore();
} }
@ -102,12 +110,27 @@ void UI::KeyDelegate::paint(QPainter* painter, const QStyleOptionViewItem& optio
painter->save(); painter->save();
painter->setFont(labelFont); painter->setFont(labelFont);
painter->setPen(q); painter->setPen(q);
painter->drawText(rect, Qt::AlignLeft | Qt::AlignTop, last.toString()); painter->drawText(rect, Qt::AlignLeft | Qt::AlignTop, last.toString(), &r);
rect.adjust(0, labelFontMetrics.lineSpacing(), 0, 0); rect.adjust(0, labelMetrics.lineSpacing(), 0, 0);
maxRight = std::max(r.width(), maxRight);
painter->restore(); painter->restore();
} }
} }
//painter->drawText(option.rect, option.displayAlignment, index.data().toString());
QVariant levelV = index.data(UI::KeysModel::TrustLevel);
if (levelV.isValid()) {
Shared::TrustLevel level = static_cast<Shared::TrustLevel>(levelV.toUInt());
QString levelName = Shared::Global::getName(level);
if (maxRight > 0)
maxRight += margin;
rect.setLeft(leftOrigin + maxRight);
rect.setTop(option.rect.top() + maxRight);
rect.setBottom(option.rect.bottom() - maxRight);
painter->setFont(defaultFont);
painter->drawText(rect, Qt::AlignLeft | Qt::AlignVCenter, levelName, &r);
}
painter->restore(); painter->restore();
} }
@ -117,10 +140,15 @@ QSize UI::KeyDelegate::sizeHint(const QStyleOptionViewItem& option, const QModel
int mh = margin * 2; int mh = margin * 2;
int mw = margin * 2; int mw = margin * 2;
int width = 0;
QVariant labelV = index.data(UI::KeysModel::Label); QVariant labelV = index.data(UI::KeysModel::Label);
if (labelV.isValid() && labelV.toString().size() > 0) { if (labelV.isValid()) {
mh += labelFontMetrics.lineSpacing(); QString label = labelV.toString();
if (label.size() > 0) {
mh += labelMetrics.lineSpacing();
width = labelMetrics.horizontalAdvance(label);
}
} }
QVariant fingerPrintV = index.data(UI::KeysModel::FingerPrint); QVariant fingerPrintV = index.data(UI::KeysModel::FingerPrint);
@ -129,9 +157,8 @@ QSize UI::KeyDelegate::sizeHint(const QStyleOptionViewItem& option, const QModel
uint8_t parts = hex.size() / partSize; uint8_t parts = hex.size() / partSize;
mh += fingerPrintMetrics.height(); mh += fingerPrintMetrics.height();
if (parts > maxSingleLineParts) { if (parts > maxSingleLineParts)
mh += fingerPrintMetrics.height() + fingerPrintMetrics.leading(); mh += fingerPrintMetrics.height() + fingerPrintMetrics.leading();
}
uint8_t firstLine; uint8_t firstLine;
if (parts > maxSingleLineParts) if (parts > maxSingleLineParts)
@ -139,13 +166,29 @@ QSize UI::KeyDelegate::sizeHint(const QStyleOptionViewItem& option, const QModel
else else
firstLine = parts; firstLine = parts;
mw += firstLine * partSize * spaceWidth + (firstLine - 1) * spaceWidth; width = std::max(width, firstLine * fingerPrintMetrics.horizontalAdvance(hex, partSize) + (firstLine - 1) * spaceWidth);
} }
QVariant lastV = index.data(UI::KeysModel::LastInteraction); QVariant lastV = index.data(UI::KeysModel::LastInteraction);
if (lastV.isValid() && lastV.toDateTime().isValid()) { if (lastV.isValid()) {
mh += labelFontMetrics.lineSpacing(); QDateTime last = lastV.toDateTime();
if (last.isValid()) {
mh += labelMetrics.lineSpacing();
QString dt = last.toString();
width = std::max(labelMetrics.horizontalAdvance(dt), width);
}
} }
QVariant levelV = index.data(UI::KeysModel::TrustLevel);
if (levelV.isValid()) {
Shared::TrustLevel level = static_cast<Shared::TrustLevel>(levelV.toUInt());
QString levelName = Shared::Global::getName(level);
if (width > 0)
width += margin;
width += defaultMetrics.horizontalAdvance(levelName);
}
mw += width;
if (size.width() < mw) if (size.width() < mw)
size.setWidth(mw); size.setWidth(mw);

View File

@ -33,10 +33,12 @@ public:
void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const override; void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const override;
private: private:
const QFont& defaultFont;
const QFont& fingerPrintFont; const QFont& fingerPrintFont;
const QFont& labelFont; const QFont& labelFont;
const QFontMetrics& defaultMetrics;
const QFontMetrics& fingerPrintMetrics; const QFontMetrics& fingerPrintMetrics;
const QFontMetrics& labelFontMetrics; const QFontMetrics& labelMetrics;
int spaceWidth; int spaceWidth;
private: private:

View File

@ -18,7 +18,7 @@
#include "ui_omemo.h" #include "ui_omemo.h"
#include <random> #include <random>
constexpr uint8_t fingerprintLength = 24; constexpr uint8_t fingerprintLength = 32;
Omemo::Omemo(QWidget* parent): Omemo::Omemo(QWidget* parent):
QWidget(parent), QWidget(parent),

View File

@ -65,21 +65,24 @@
</property> </property>
<layout class="QGridLayout" name="gridLayout" columnstretch="1,3,1"> <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="ExpandingList" name="keysView">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="sizeAdjustPolicy"> <property name="sizeAdjustPolicy">
<enum>QAbstractScrollArea::AdjustToContents</enum> <enum>QAbstractScrollArea::AdjustToContents</enum>
</property> </property>
<property name="showDropIndicator" stdset="0"> <property name="showDropIndicator" stdset="0">
<bool>false</bool> <bool>false</bool>
</property> </property>
<property name="selectionBehavior">
<enum>QAbstractItemView::SelectColumns</enum>
</property>
<property name="verticalScrollMode"> <property name="verticalScrollMode">
<enum>QAbstractItemView::ScrollPerPixel</enum> <enum>QAbstractItemView::ScrollPerPixel</enum>
</property> </property>
<property name="horizontalScrollMode"> <property name="horizontalScrollMode">
<enum>QAbstractItemView::ScrollPerItem</enum> <enum>QAbstractItemView::ScrollPerPixel</enum>
</property> </property>
<property name="uniformItemSizes"> <property name="uniformItemSizes">
<bool>false</bool> <bool>false</bool>
@ -122,7 +125,29 @@
</widget> </widget>
</item> </item>
<item row="5" column="1"> <item row="5" column="1">
<widget class="QListView" name="unusedKeysView"/> <widget class="ExpandingList" name="unusedKeysView">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="sizeAdjustPolicy">
<enum>QAbstractScrollArea::AdjustToContents</enum>
</property>
<property name="showDropIndicator" stdset="0">
<bool>false</bool>
</property>
<property name="verticalScrollMode">
<enum>QAbstractItemView::ScrollPerPixel</enum>
</property>
<property name="horizontalScrollMode">
<enum>QAbstractItemView::ScrollPerPixel</enum>
</property>
<property name="uniformItemSizes">
<bool>false</bool>
</property>
</widget>
</item> </item>
<item row="0" column="0" rowspan="6"> <item row="0" column="0" rowspan="6">
<spacer name="spacerLeft"> <spacer name="spacerLeft">
@ -156,6 +181,14 @@
</item> </item>
</layout> </layout>
</widget> </widget>
<customwidgets>
<customwidget>
<class>ExpandingList</class>
<extends>QListView</extends>
<header location="global">ui/utils/expandinglist.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/> <resources/>
<connections/> <connections/>
</ui> </ui>