better way to solve yesterday font problem, small visual avatar rendering fix

This commit is contained in:
Blue 2023-01-12 20:56:01 +03:00
parent 15fb4bbd62
commit d4bf7e599a
Signed by: blue
GPG Key ID: 9B203B252A63EE38
9 changed files with 62 additions and 56 deletions

View File

@ -17,6 +17,7 @@
*/ */
#include "global.h" #include "global.h"
#include <QFontDatabase>
#include "enums.h" #include "enums.h"
#include "ui/models/roster.h" #include "ui/models/roster.h"
@ -26,6 +27,25 @@ constexpr bool OMEMO_SUPPORT = true;
constexpr bool OMEMO_SUPPORT = false constexpr bool OMEMO_SUPPORT = false
#endif #endif
QFont getFont (QFontDatabase::SystemFont type, bool bold = false, bool italic = false, qreal factor = 1.0) {
QFont font = QFontDatabase::systemFont(type);
if (bold)
font.setBold(true);
if (italic)
font.setItalic(true);
if (factor != 1.0) {
float ps = font.pointSizeF();
if (ps != -1) {
font.setPointSizeF(ps * factor);
} else {
font.setPointSize(font.pointSize() * factor);
}
}
return font;
}
Shared::Global* Shared::Global::instance = 0; Shared::Global* Shared::Global::instance = 0;
const std::set<QString> Shared::Global::supportedImagesExts = {"png", "jpg", "webp", "jpeg", "gif", "svg"}; const std::set<QString> Shared::Global::supportedImagesExts = {"png", "jpg", "webp", "jpeg", "gif", "svg"};
@ -102,6 +122,14 @@ Shared::Global::Global():
defaultSystemStyle(QApplication::style()->objectName()), defaultSystemStyle(QApplication::style()->objectName()),
defaultSystemPalette(QApplication::palette()), defaultSystemPalette(QApplication::palette()),
omemoSupport(OMEMO_SUPPORT), omemoSupport(OMEMO_SUPPORT),
defaultFont(QFontDatabase::systemFont(QFontDatabase::GeneralFont)),
smallFont(getFont(QFontDatabase::SmallestReadableFont, false, true)),
headerFont(getFont(QFontDatabase::TitleFont, true, false, 1.1)),
titleFont(getFont(QFontDatabase::TitleFont, true, false, 1.3)),
defaultFontMetrics(defaultFont),
smallFontMetrics(smallFont),
headerFontMetrics(headerFont),
titleFontMetrics(titleFont),
pluginSupport({ pluginSupport({
{"KWallet", false}, {"KWallet", false},
{"openFileManagerWindowJob", false}, {"openFileManagerWindowJob", false},

View File

@ -42,6 +42,8 @@
#include <QProcess> #include <QProcess>
#include <QDesktopServices> #include <QDesktopServices>
#include <QRegularExpression> #include <QRegularExpression>
#include <QFont>
#include <QFontMetrics>
namespace Shared { namespace Shared {
@ -100,6 +102,14 @@ namespace Shared {
static void setTheme(const QString& path); static void setTheme(const QString& path);
static void setStyle(const QString& style); static void setStyle(const QString& style);
const bool omemoSupport; const bool omemoSupport;
QFont defaultFont;
QFont smallFont;
QFont headerFont;
QFont titleFont;
QFontMetrics defaultFontMetrics;
QFontMetrics smallFontMetrics;
QFontMetrics headerFontMetrics;
QFontMetrics titleFontMetrics;
template<typename T> template<typename T>
static T fromInt(int src); static T fromInt(int src);

View File

@ -371,7 +371,7 @@ void Conversation::setAvatar(const QString& path)
if (path.size() == 0) { if (path.size() == 0) {
pixmap = Shared::icon("user", true).pixmap(avatarSize); pixmap = Shared::icon("user", true).pixmap(avatarSize);
} else { } else {
pixmap = QPixmap(path).scaled(avatarSize); pixmap = QPixmap(path).scaled(avatarSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
} }

View File

@ -24,7 +24,6 @@
#include <QApplication> #include <QApplication>
#include <QClipboard> #include <QClipboard>
#include <QDebug> #include <QDebug>
#include <QFontDatabase>
#include "messagedelegate.h" #include "messagedelegate.h"
#include "messagefeed.h" #include "messagefeed.h"
@ -41,8 +40,6 @@ const std::set<int> FeedView::geometryChangingRoles = {
Models::MessageFeed::Error, Models::MessageFeed::Error,
Models::MessageFeed::Date Models::MessageFeed::Date
}; };
QFont FeedView::dividerFont = QFontDatabase::systemFont(QFontDatabase::TitleFont);
QFontMetrics FeedView::dividerMetrics = QFontMetrics(dividerFont);
FeedView::FeedView(QWidget* parent): FeedView::FeedView(QWidget* parent):
QAbstractItemView(parent), QAbstractItemView(parent),
@ -54,6 +51,8 @@ FeedView::FeedView(QWidget* parent):
clearWidgetsMode(false), clearWidgetsMode(false),
modelState(Models::MessageFeed::complete), modelState(Models::MessageFeed::complete),
progress(), progress(),
dividerFont(Shared::Global::getInstance()->titleFont),
dividerMetrics(Shared::Global::getInstance()->titleFontMetrics),
mousePressed(false), mousePressed(false),
dragging(false), dragging(false),
hovered(Shared::Hover::nothing), hovered(Shared::Hover::nothing),

View File

@ -99,8 +99,8 @@ private:
bool clearWidgetsMode; bool clearWidgetsMode;
Models::MessageFeed::SyncState modelState; Models::MessageFeed::SyncState modelState;
Progress progress; Progress progress;
static QFont dividerFont; const QFont& dividerFont;
static QFontMetrics dividerMetrics; const QFontMetrics& dividerMetrics;
bool mousePressed; bool mousePressed;
bool dragging; bool dragging;
Shared::Hover hovered; Shared::Hover hovered;

View File

@ -24,21 +24,11 @@
#include <QAbstractItemView> #include <QAbstractItemView>
#include <QAbstractTextDocumentLayout> #include <QAbstractTextDocumentLayout>
#include <QTextBlock> #include <QTextBlock>
#include <QFontDatabase>
#include <cmath> #include <cmath>
#include "messagedelegate.h" #include "messagedelegate.h"
#include "messagefeed.h" #include "messagefeed.h"
QFont getFont (QFontDatabase::SystemFont type, bool bold, bool italic) {
QFont font = QFontDatabase::systemFont(type);
if (bold)
font.setBold(true);
if (italic)
font.setItalic(true);
return font;
}
constexpr int textMargin = 2; constexpr int textMargin = 2;
constexpr int statusIconSize = 16; constexpr int statusIconSize = 16;
@ -48,15 +38,13 @@ constexpr int bubbleBorderRadius = 3;
int MessageDelegate::avatarHeight(50); int MessageDelegate::avatarHeight(50);
int MessageDelegate::margin(6); int MessageDelegate::margin(6);
bool MessageDelegate::fontsInitialized(false);
QFont MessageDelegate::bodyFont;
QFont MessageDelegate::nickFont;
QFont MessageDelegate::dateFont;
QFontMetrics MessageDelegate::nickMetrics(nickFont);
QFontMetrics MessageDelegate::dateMetrics(dateFont);
MessageDelegate::MessageDelegate(QObject* parent): MessageDelegate::MessageDelegate(QObject* parent):
QStyledItemDelegate(parent), QStyledItemDelegate(parent),
bodyFont(Shared::Global::getInstance()->defaultFont),
nickFont(Shared::Global::getInstance()->headerFont),
dateFont(Shared::Global::getInstance()->smallFont),
nickMetrics(Shared::Global::getInstance()->headerFontMetrics),
dateMetrics(Shared::Global::getInstance()->smallFontMetrics),
bodyRenderer(new QTextDocument()), bodyRenderer(new QTextDocument()),
buttonHeight(0), buttonHeight(0),
buttonWidth(0), buttonWidth(0),
@ -71,11 +59,6 @@ MessageDelegate::MessageDelegate(QObject* parent):
currentId(""), currentId(""),
selection(0, 0) selection(0, 0)
{ {
if (!fontsInitialized) {
fontsInitialized = true;
initializeFonts();
}
bodyRenderer->setDocumentMargin(0); bodyRenderer->setDocumentMargin(0);
bodyRenderer->setDefaultFont(bodyFont); bodyRenderer->setDefaultFont(bodyFont);
@ -812,11 +795,3 @@ void MessageDelegate::clearHelperWidget(const Models::FeedItem& data) const
} }
} }
} }
void MessageDelegate::initializeFonts () {
bodyFont = getFont(QFontDatabase::GeneralFont, false, false);
nickFont = getFont(QFontDatabase::TitleFont, true, false);
dateFont = getFont(QFontDatabase::SmallestReadableFont, false, true);
nickMetrics = QFontMetrics(nickFont);
dateMetrics = QFontMetrics(dateFont);
}

View File

@ -93,9 +93,6 @@ protected:
protected slots: protected slots:
void onButtonPushed() const; void onButtonPushed() const;
private:
static void initializeFonts();
private: private:
class FeedButton : public QPushButton { class FeedButton : public QPushButton {
@ -103,13 +100,12 @@ private:
QString messageId; QString messageId;
}; };
static bool fontsInitialized; const QFont& bodyFont;
static QFont bodyFont; const QFont& nickFont;
static QFont nickFont; const QFont& dateFont;
static QFont dateFont; const QFontMetrics& nickMetrics;
const QFontMetrics& dateMetrics;
QTextDocument* bodyRenderer; QTextDocument* bodyRenderer;
static QFontMetrics nickMetrics;
static QFontMetrics dateMetrics;
int buttonHeight; int buttonHeight;
int buttonWidth; int buttonWidth;

View File

@ -16,22 +16,20 @@
#include "keydelegate.h" #include "keydelegate.h"
#include <QPainter> #include <QPainter>
#include <QFontDatabase>
#include "keysmodel.h" #include "keysmodel.h"
#include <shared/global.h>
constexpr int minHeight = 50;
constexpr int minWidth = 400;
constexpr uint8_t margin = 10; constexpr uint8_t margin = 10;
constexpr uint8_t partSize = 8; constexpr uint8_t partSize = 8;
constexpr uint8_t maxSingleLineParts = 3; constexpr uint8_t maxSingleLineParts = 3;
UI::KeyDelegate::KeyDelegate(QObject* parent): UI::KeyDelegate::KeyDelegate(QObject* parent):
QStyledItemDelegate(parent), QStyledItemDelegate(parent),
fingerPrintFont(QFontDatabase::systemFont(QFontDatabase::FixedFont)), fingerPrintFont(Shared::Global::getInstance()->defaultFont),
labelFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont)), labelFont(Shared::Global::getInstance()->smallFont),
fingerPrintMetrics(fingerPrintFont), fingerPrintMetrics(Shared::Global::getInstance()->defaultFontMetrics),
labelFontMetrics(labelFont), labelFontMetrics(Shared::Global::getInstance()->smallFontMetrics),
spaceWidth(fingerPrintMetrics.horizontalAdvance(" ")) spaceWidth(fingerPrintMetrics.horizontalAdvance(" "))
{} {}

View File

@ -33,10 +33,10 @@ 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:
QFont fingerPrintFont; const QFont& fingerPrintFont;
QFont labelFont; const QFont& labelFont;
QFontMetrics fingerPrintMetrics; const QFontMetrics& fingerPrintMetrics;
QFontMetrics labelFontMetrics; const QFontMetrics& labelFontMetrics;
int spaceWidth; int spaceWidth;
private: private: