From 46508b52a2ab9c0cbabefff13c24224396a23ca8 Mon Sep 17 00:00:00 2001
From: Andreas Traczyk <andreas.traczyk@savoirfairelinux.com>
Date: Fri, 4 Jan 2019 11:49:24 -0500
Subject: [PATCH] ui: fixes to font sizes and update scaling at app start

Change-Id: Ie2b68929369d3d3f084025dd16a30aabdf43a9eb
---
 accountitemdelegate.cpp      | 35 +++++++++++++++++++++++------------
 accountitemdelegate.h        |  2 +-
 conversationitemdelegate.cpp | 14 +++++++++-----
 conversationitemdelegate.h   |  2 +-
 currentaccountcombobox.cpp   | 23 ++++++++++++++---------
 mainwindow.cpp               | 22 ++++++++++++----------
 6 files changed, 60 insertions(+), 38 deletions(-)

diff --git a/accountitemdelegate.cpp b/accountitemdelegate.cpp
index 351a230..015e35f 100644
--- a/accountitemdelegate.cpp
+++ b/accountitemdelegate.cpp
@@ -26,6 +26,7 @@
 #include "accountlistmodel.h"
 #include "ringthemeutils.h"
 #include "lrcinstance.h"
+#include "mainwindow.h"
 
 #undef REGISTERED
 
@@ -63,8 +64,18 @@ AccountItemDelegate::paint(QPainter* painter,
 
     QRect &rect = opt.rect;
 
-    QFont font(painter->font());
-    font.setPointSize(fontSize_);
+    // define and set the two fonts
+    QFont fontPrimary = painter->font();
+    QFont fontSecondary = painter->font();
+    fontPrimary.setWeight(QFont::ExtraLight);
+    auto scalingRatio = MainWindow::instance().getCurrentScalingRatio();
+    if (scalingRatio > 1.0) {
+        fontPrimary.setPointSize(fontSize_ - 1);
+        fontSecondary.setPointSize(fontSize_ - 2);
+    } else {
+        fontPrimary.setPointSize(fontSize_);
+        fontSecondary.setPointSize(fontSize_ - 1);
+    }
 
     QPen pen(painter->pen());
 
@@ -72,8 +83,8 @@ AccountItemDelegate::paint(QPainter* painter,
     if (index.row() == LRCInstance::accountModel().getAccountList().size()) {
         pen.setColor(RingTheme::lightBlack_);
         painter->setPen(pen);
-        painter->setFont(font);
-        QFontMetrics fontMetrics(font);
+        painter->setFont(fontPrimary);
+        QFontMetrics fontMetrics(fontPrimary);
         painter->drawText(rect, Qt::AlignVCenter | Qt::AlignHCenter, tr("Add Account") + "+");
         return;
     }
@@ -117,12 +128,12 @@ AccountItemDelegate::paint(QPainter* painter,
     QVariant name = index.data(static_cast<int>(AccountListModel::Role::Alias));
     if (name.isValid())
     {
-        font.setItalic(false);
-        font.setBold(false);
+        fontPrimary.setItalic(false);
+        fontPrimary.setBold(false);
         pen.setColor(RingTheme::lightBlack_);
         painter->setPen(pen);
-        painter->setFont(font);
-        QFontMetrics fontMetrics(font);
+        painter->setFont(fontPrimary);
+        QFontMetrics fontMetrics(fontPrimary);
         QString nameStr = fontMetrics.elidedText(name.value<QString>(), Qt::ElideRight,
             rectTexts.width() - avatarSize_ - leftPadding_ - rightPadding_ * 2);
         painter->drawText(rectTexts, Qt::AlignVCenter | Qt::AlignLeft, nameStr);
@@ -132,12 +143,12 @@ AccountItemDelegate::paint(QPainter* painter,
 
     QString idStr = index.data(static_cast<int>(AccountListModel::Role::Username)).value<QString>();
     if (idStr != name.toString()) {
-        font.setItalic(false);
-        font.setBold(false);
+        fontSecondary.setItalic(false);
+        fontSecondary.setBold(false);
         pen.setColor(RingTheme::grey_);
         painter->setPen(pen);
-        painter->setFont(font);
-        QFontMetrics fontMetrics(font);
+        painter->setFont(fontSecondary);
+        QFontMetrics fontMetrics(fontSecondary);
         if (!idStr.isNull()) {
             idStr = fontMetrics.elidedText(idStr, Qt::ElideRight,
                 rectTexts.width() - avatarSize_ - leftPadding_ - rightPadding_ * 2);
diff --git a/accountitemdelegate.h b/accountitemdelegate.h
index f65052f..d30d198 100644
--- a/accountitemdelegate.h
+++ b/accountitemdelegate.h
@@ -31,7 +31,7 @@ protected:
     QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const;
 
 private:
-    constexpr static int fontSize_ = 10;
+    constexpr static int fontSize_ = 11;
     const QFont font_ = QFont("Arial", fontSize_);
     constexpr static int topPadding_ = 6;
     constexpr static int bottomPadding_ = 6;
diff --git a/conversationitemdelegate.cpp b/conversationitemdelegate.cpp
index 8763d10..30ce4cb 100644
--- a/conversationitemdelegate.cpp
+++ b/conversationitemdelegate.cpp
@@ -162,15 +162,18 @@ ConversationItemDelegate::paintRingConversationItem(QPainter* painter,
 {
     Q_UNUSED(option);
     QFont font(painter->font());
-    font.setPointSize(fontSize_);
     QPen pen(painter->pen());
     painter->setPen(pen);
 
     int infoTextWidthModifier = 0;
+    int infoText2HeightModifier = 0;
     auto scalingRatio = MainWindow::instance().getCurrentScalingRatio();
     if (scalingRatio > 1.0) {
-        font.setPointSize(fontSize_ - 1);
+        font.setPointSize(fontSize_ - 2);
         infoTextWidthModifier = 12;
+        infoText2HeightModifier = 2;
+    } else {
+        font.setPointSize(fontSize_);
     }
 
     auto leftMargin = dx_ + sizeImage_ + dx_;
@@ -194,9 +197,9 @@ ConversationItemDelegate::paintRingConversationItem(QPainter* painter,
                     rect.height() / 2 - 2);
 
     QRect rectInfo2(rectInfo1.left(),
-                    rectInfo1.top() + rectInfo1.height(),
+                    rectInfo1.top() + rectInfo1.height() - infoText2HeightModifier,
                     rectInfo1.width(),
-                    rectInfo1.height() - bottomMargin);
+                    rectInfo1.height() - bottomMargin + infoText2HeightModifier);
 
     QFontMetrics fontMetrics(font);
 
@@ -240,6 +243,7 @@ ConversationItemDelegate::paintRingConversationItem(QPainter* painter,
     QString interactionStr = index.data(static_cast<int>(SmartListModel::Role::LastInteraction)).value<QString>();
     if (!interactionStr.isNull()) {
         painter->save();
+        font.setWeight(QFont::ExtraLight);
         interactionStr = interactionStr.simplified();
         auto type = Utils::toEnum<lrc::api::interaction::Type>(index
             .data(static_cast<int>(SmartListModel::Role::LastInteractionType))
@@ -263,7 +267,7 @@ ConversationItemDelegate::paintRingConversationItem(QPainter* painter,
             QFont emojiMsgFont(QStringLiteral("Segoe UI Emoji"));
             emojiMsgFont.setItalic(false);
             emojiMsgFont.setBold(false);
-            emojiMsgFont.setPointSize(fontSize_);
+            emojiMsgFont.setPointSize(scalingRatio > 1.0 ? fontSize_ - 2 : fontSize_);
             painter->setOpacity(0.7);
             painter->setFont(emojiMsgFont);
         }
diff --git a/conversationitemdelegate.h b/conversationitemdelegate.h
index 249d77b..f877845 100644
--- a/conversationitemdelegate.h
+++ b/conversationitemdelegate.h
@@ -42,7 +42,7 @@ private:
     constexpr static int cellHeight_ = 60;
     constexpr static int dy_ = 6;
     constexpr static int dx_ = 12;
-    constexpr static int fontSize_ = 10;
+    constexpr static int fontSize_ = 11;
     constexpr static int infoTextWidth_ = 144;
 
     mutable std::map<int, bool> highlightMap_;
diff --git a/currentaccountcombobox.cpp b/currentaccountcombobox.cpp
index 2187aa5..feefccd 100644
--- a/currentaccountcombobox.cpp
+++ b/currentaccountcombobox.cpp
@@ -18,16 +18,16 @@
  **************************************************************************/
 
 #include "currentaccountcombobox.h"
-#include <accountitemdelegate.h>
-#include "pixbufmanipulator.h"
 
+#include <QPixmap>
+#include <QMouseEvent>
+
+#include "accountitemdelegate.h"
+#include "pixbufmanipulator.h"
 #include "utils.h"
 #include "ringthemeutils.h"
 #include "lrcinstance.h"
-#include <QPixmap>
-
-#include "callwidget.h"
-#include "ui_callwidget.h"
+#include "mainwindow.h"
 
 #undef REGISTERED
 
@@ -86,10 +86,15 @@ CurrentAccountComboBox::paintEvent(QPaintEvent* e)
     // define and set the two fonts
     QFont fontPrimary = painter.font();
     QFont fontSecondary = painter.font();
-
-    fontPrimary.setPointSize(11);
     fontPrimary.setWeight(QFont::ExtraLight);
-    fontSecondary.setPointSize(10);
+    auto scalingRatio = MainWindow::instance().getCurrentScalingRatio();
+    if (scalingRatio > 1.0) {
+        fontPrimary.setPointSize(10);
+        fontSecondary.setPointSize(9);
+    } else {
+        fontPrimary.setPointSize(11);
+        fontSecondary.setPointSize(10);
+    }
 
     QFontMetrics fontMetricPrimary(fontPrimary);
     QFontMetrics fontMetricSecondary(fontSecondary);
diff --git a/mainwindow.cpp b/mainwindow.cpp
index 369be0e..5b3a03f 100644
--- a/mainwindow.cpp
+++ b/mainwindow.cpp
@@ -149,15 +149,6 @@ MainWindow::MainWindow(QWidget* parent) :
     }
 
     lastScr_ = startScreen;
-
-    activeChangedConnection_ = connect(windowHandle(), &QWindow::activeChanged,
-        [this]() {
-            auto screenNumber = qApp->desktop()->screenNumber();
-            QScreen* screen = qApp->screens().at(screenNumber);
-            windowHandle()->setScreen(nullptr);
-            windowHandle()->setScreen(screen);
-        });
-
 }
 
 MainWindow::~MainWindow()
@@ -351,12 +342,23 @@ MainWindow::show()
     disconnect(screenChangedConnection_);
     screenChangedConnection_ = connect(windowHandle(), &QWindow::screenChanged,
                                        this, &MainWindow::slotScreenChanged);
+    disconnect(activeChangedConnection_);
+    activeChangedConnection_ = connect(windowHandle(), &QWindow::activeChanged,
+        [this]() {
+            auto screenNumber = qApp->desktop()->screenNumber();
+            windowHandle()->setScreen(nullptr);
+            QScreen* screen = qApp->screens().at(screenNumber);
+            windowHandle()->setScreen(screen);
+        });
+    auto screenNumber = qApp->desktop()->screenNumber();
+    QScreen* screen = qApp->screens().at(screenNumber);
+    currentScalingRatio_ = screen->logicalDotsPerInchX() / 96;
+    qobject_cast<NavWidget*>(ui->navStack->currentWidget())->updateCustomUI();
 }
 
 void
 MainWindow::slotScreenChanged(QScreen* screen)
 {
-    Q_UNUSED(screen);
     adjustSize();
     updateGeometry();
     update();
-- 
GitLab