From 3a57d1018ef284de7bb6ece2ac8dc799479c369c Mon Sep 17 00:00:00 2001 From: Konstantinos Sideris Date: Mon, 1 Oct 2018 17:56:46 +0300 Subject: [PATCH] Add option to configure the font size fixes #161 --- src/UserInfoWidget.cpp | 21 ++++++++----- src/UserSettingsPage.cpp | 65 ++++++++++++++-------------------------- src/UserSettingsPage.h | 12 ++++++-- src/Utils.cpp | 12 ++++++++ src/Utils.h | 5 ++++ src/main.cpp | 9 ++++-- 6 files changed, 68 insertions(+), 56 deletions(-) diff --git a/src/UserInfoWidget.cpp b/src/UserInfoWidget.cpp index d62cea22..0c8a3114 100644 --- a/src/UserInfoWidget.cpp +++ b/src/UserInfoWidget.cpp @@ -17,6 +17,8 @@ #include +#include + #include "Config.h" #include "MainWindow.h" #include "UserInfoWidget.h" @@ -28,26 +30,31 @@ UserInfoWidget::UserInfoWidget(QWidget *parent) : QWidget(parent) , display_name_("User") , user_id_("@user:homeserver.org") - , logoutButtonSize_{20} { - setFixedHeight(56); + const int fontHeight = QFontMetrics(font()).height(); + const int widgetMargin = fontHeight / 3; + const int contentHeight = fontHeight * 3; + + logoutButtonSize_ = fontHeight + (fontHeight / 4); + + setFixedHeight(contentHeight + widgetMargin * 2); topLayout_ = new QHBoxLayout(this); topLayout_->setSpacing(0); - topLayout_->setMargin(5); + topLayout_->setMargin(widgetMargin); avatarLayout_ = new QHBoxLayout(); textLayout_ = new QVBoxLayout(); - textLayout_->setSpacing(2); - textLayout_->setContentsMargins(10, 5, 10, 5); + textLayout_->setSpacing(0); + textLayout_->setContentsMargins( + widgetMargin * 2, widgetMargin, widgetMargin * 2, widgetMargin); userAvatar_ = new Avatar(this); userAvatar_->setObjectName("userAvatar"); userAvatar_->setLetter(QChar('?')); - userAvatar_->setSize(45); + userAvatar_->setSize(fontHeight * 2.5); QFont nameFont; - nameFont.setPointSizeF(nameFont.pointSizeF() * 1.2); nameFont.setWeight(QFont::Medium); displayNameLabel_ = new QLabel(this); diff --git a/src/UserSettingsPage.cpp b/src/UserSettingsPage.cpp index 9245702e..2481260e 100644 --- a/src/UserSettingsPage.cpp +++ b/src/UserSettingsPage.cpp @@ -51,6 +51,8 @@ UserSettings::load() isReadReceiptsEnabled_ = settings.value("user/read_receipts", true).toBool(); theme_ = settings.value("user/theme", "light").toString(); + baseFontSize_ = settings.value("user/font_size", QFont().pointSizeF()).toDouble(); + applyTheme(); } @@ -92,6 +94,7 @@ UserSettings::save() settings.setValue("start_in_tray", isStartInTrayEnabled_); settings.endGroup(); + settings.setValue("font_size", baseFontSize_); settings.setValue("room_ordering", isOrderingEnabled_); settings.setValue("typing_notifications", isTypingNotificationsEnabled_); settings.setValue("read_receipts", isReadReceiptsEnabled_); @@ -214,8 +217,16 @@ UserSettingsPage::UserSettingsPage(QSharedPointer settings, QWidge scaleFactorCombo_->addItem("2.75"); scaleFactorCombo_->addItem("3"); - scaleFactorOptionLayout->addWidget(scaleFactorLabel); - scaleFactorOptionLayout->addWidget(scaleFactorCombo_, 0, Qt::AlignRight); + auto fontSizeOptionLayout = new QHBoxLayout; + fontSizeOptionLayout->setContentsMargins(0, OptionMargin, 0, OptionMargin); + auto fontSizeLabel = new QLabel(tr("Font size"), this); + fontSizeLabel->setFont(font); + fontSizeCombo_ = new QComboBox(this); + for (double option = 10; option < 17; option += 0.5) + fontSizeCombo_->addItem(QString("%1 ").arg(QString::number(option))); + + fontSizeOptionLayout->addWidget(fontSizeLabel); + fontSizeOptionLayout->addWidget(fontSizeCombo_, 0, Qt::AlignRight); auto themeOptionLayout_ = new QHBoxLayout; themeOptionLayout_->setContentsMargins(0, OptionMargin, 0, OptionMargin); @@ -316,9 +327,10 @@ UserSettingsPage::UserSettingsPage(QSharedPointer settings, QWidge scaleFactorCombo_->hide(); #else mainLayout_->addLayout(scaleFactorOptionLayout); - mainLayout_->addWidget(new HorizontalLine(this)); #endif + mainLayout_->addLayout(fontSizeOptionLayout); + mainLayout_->addWidget(new HorizontalLine(this)); mainLayout_->addLayout(themeOptionLayout_); mainLayout_->addWidget(new HorizontalLine(this)); @@ -351,6 +363,9 @@ UserSettingsPage::UserSettingsPage(QSharedPointer settings, QWidge connect(scaleFactorCombo_, static_cast(&QComboBox::activated), [](const QString &factor) { utils::setScaleFactor(factor.toFloat()); }); + connect(fontSizeCombo_, + static_cast(&QComboBox::activated), + [this](const QString &size) { settings_->setFontSize(size.trimmed().toDouble()); }); connect(trayToggle_, &Toggle::toggled, this, [this](bool isDisabled) { settings_->setTray(!isDisabled); @@ -395,8 +410,10 @@ UserSettingsPage::UserSettingsPage(QSharedPointer settings, QWidge void UserSettingsPage::showEvent(QShowEvent *) { - restoreThemeCombo(); - restoreScaleFactor(); + // FIXME macOS doesn't show the full option unless a space is added. + utils::restoreCombobox(fontSizeCombo_, QString::number(settings_->fontSize()) + " "); + utils::restoreCombobox(scaleFactorCombo_, QString::number(utils::scaleFactor())); + utils::restoreCombobox(themeCombo_, settings_->theme()); // FIXME: Toggle treats true as "off" trayToggle_->setState(!settings_->isTrayEnabled()); @@ -431,44 +448,6 @@ UserSettingsPage::paintEvent(QPaintEvent *) style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this); } -void -UserSettingsPage::restoreScaleFactor() const -{ - auto factor = utils::scaleFactor(); - - if (factor == 1) - scaleFactorCombo_->setCurrentIndex(0); - else if (factor == 1.25) - scaleFactorCombo_->setCurrentIndex(1); - else if (factor == 1.5) - scaleFactorCombo_->setCurrentIndex(2); - else if (factor == 1.75) - scaleFactorCombo_->setCurrentIndex(3); - else if (factor == 2) - scaleFactorCombo_->setCurrentIndex(4); - else if (factor == 2.25) - scaleFactorCombo_->setCurrentIndex(5); - else if (factor == 2.5) - scaleFactorCombo_->setCurrentIndex(6); - else if (factor == 2.75) - scaleFactorCombo_->setCurrentIndex(7); - else if (factor == 3) - scaleFactorCombo_->setCurrentIndex(7); - else - scaleFactorCombo_->setCurrentIndex(0); -} - -void -UserSettingsPage::restoreThemeCombo() const -{ - if (settings_->theme() == "light") - themeCombo_->setCurrentIndex(0); - else if (settings_->theme() == "dark") - themeCombo_->setCurrentIndex(1); - else - themeCombo_->setCurrentIndex(2); -} - void UserSettingsPage::importSessionKeys() { diff --git a/src/UserSettingsPage.h b/src/UserSettingsPage.h index 501e76d9..59a6518c 100644 --- a/src/UserSettingsPage.h +++ b/src/UserSettingsPage.h @@ -53,6 +53,12 @@ public: save(); } + void setFontSize(double size) + { + baseFontSize_ = size; + save(); + } + void setRoomOrdering(bool state) { isOrderingEnabled_ = state; @@ -94,6 +100,7 @@ public: bool isTypingNotificationsEnabled() const { return isTypingNotificationsEnabled_; } bool isReadReceiptsEnabled() const { return isReadReceiptsEnabled_; } bool hasDesktopNotifications() const { return hasDesktopNotifications_; } + double fontSize() const { return baseFontSize_; } signals: void groupViewStateChanged(bool state); @@ -107,6 +114,7 @@ private: bool isTypingNotificationsEnabled_; bool isReadReceiptsEnabled_; bool hasDesktopNotifications_; + double baseFontSize_; }; class HorizontalLine : public QFrame @@ -138,9 +146,6 @@ private slots: void exportSessionKeys(); private: - void restoreThemeCombo() const; - void restoreScaleFactor() const; - // Layouts QVBoxLayout *topLayout_; QVBoxLayout *mainLayout_; @@ -161,6 +166,7 @@ private: QComboBox *themeCombo_; QComboBox *scaleFactorCombo_; + QComboBox *fontSizeCombo_; int sideMargin_ = 0; }; diff --git a/src/Utils.cpp b/src/Utils.cpp index 8ee6d5ae..567e7651 100644 --- a/src/Utils.cpp +++ b/src/Utils.cpp @@ -1,6 +1,7 @@ #include "Utils.h" #include +#include #include #include #include @@ -400,3 +401,14 @@ utils::centerWidget(QWidget *widget, QWidget *parent) widget->move(findCenter(QApplication::desktop()->screenGeometry())); } + +void +utils::restoreCombobox(QComboBox *combo, const QString &value) +{ + for (auto i = 0; i < combo->count(); ++i) { + if (value == combo->itemText(i)) { + combo->setCurrentIndex(i); + break; + } + } +} diff --git a/src/Utils.h b/src/Utils.h index bb032a1c..47754604 100644 --- a/src/Utils.h +++ b/src/Utils.h @@ -14,6 +14,8 @@ #include #include +class QComboBox; + namespace utils { using TimelineEvent = mtx::events::collections::TimelineEvents; @@ -228,4 +230,7 @@ linkColor(); //! Center a widget in relation to another widget. void centerWidget(QWidget *widget, QWidget *parent); + +void +restoreCombobox(QComboBox *combo, const QString &value); } diff --git a/src/main.cpp b/src/main.cpp index 3e33c198..8556f8cb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -150,7 +150,12 @@ main(int argc, char *argv[]) std::exit(1); } - app.setFont(QFont("Open Sans")); + QSettings settings; + + QFont font("Open Sans"); + font.setPointSizeF(settings.value("user/font_size", font.pointSizeF()).toDouble()); + + app.setFont(font); QString lang = QLocale::system().name(); @@ -167,8 +172,6 @@ main(int argc, char *argv[]) // Move the MainWindow to the center w.move(screenCenter(w.width(), w.height())); - QSettings settings; - if (!settings.value("user/window/start_in_tray", false).toBool() || !settings.value("user/window/tray", true).toBool()) w.show();