Add option to configure the font size

fixes #161
This commit is contained in:
Konstantinos Sideris 2018-10-01 17:56:46 +03:00
parent 4b1b062388
commit 3a57d1018e
6 changed files with 68 additions and 56 deletions

View file

@ -17,6 +17,8 @@
#include <QTimer> #include <QTimer>
#include <iostream>
#include "Config.h" #include "Config.h"
#include "MainWindow.h" #include "MainWindow.h"
#include "UserInfoWidget.h" #include "UserInfoWidget.h"
@ -28,26 +30,31 @@ UserInfoWidget::UserInfoWidget(QWidget *parent)
: QWidget(parent) : QWidget(parent)
, display_name_("User") , display_name_("User")
, user_id_("@user:homeserver.org") , 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_ = new QHBoxLayout(this);
topLayout_->setSpacing(0); topLayout_->setSpacing(0);
topLayout_->setMargin(5); topLayout_->setMargin(widgetMargin);
avatarLayout_ = new QHBoxLayout(); avatarLayout_ = new QHBoxLayout();
textLayout_ = new QVBoxLayout(); textLayout_ = new QVBoxLayout();
textLayout_->setSpacing(2); textLayout_->setSpacing(0);
textLayout_->setContentsMargins(10, 5, 10, 5); textLayout_->setContentsMargins(
widgetMargin * 2, widgetMargin, widgetMargin * 2, widgetMargin);
userAvatar_ = new Avatar(this); userAvatar_ = new Avatar(this);
userAvatar_->setObjectName("userAvatar"); userAvatar_->setObjectName("userAvatar");
userAvatar_->setLetter(QChar('?')); userAvatar_->setLetter(QChar('?'));
userAvatar_->setSize(45); userAvatar_->setSize(fontHeight * 2.5);
QFont nameFont; QFont nameFont;
nameFont.setPointSizeF(nameFont.pointSizeF() * 1.2);
nameFont.setWeight(QFont::Medium); nameFont.setWeight(QFont::Medium);
displayNameLabel_ = new QLabel(this); displayNameLabel_ = new QLabel(this);

View file

@ -51,6 +51,8 @@ UserSettings::load()
isReadReceiptsEnabled_ = settings.value("user/read_receipts", true).toBool(); isReadReceiptsEnabled_ = settings.value("user/read_receipts", true).toBool();
theme_ = settings.value("user/theme", "light").toString(); theme_ = settings.value("user/theme", "light").toString();
baseFontSize_ = settings.value("user/font_size", QFont().pointSizeF()).toDouble();
applyTheme(); applyTheme();
} }
@ -92,6 +94,7 @@ UserSettings::save()
settings.setValue("start_in_tray", isStartInTrayEnabled_); settings.setValue("start_in_tray", isStartInTrayEnabled_);
settings.endGroup(); settings.endGroup();
settings.setValue("font_size", baseFontSize_);
settings.setValue("room_ordering", isOrderingEnabled_); settings.setValue("room_ordering", isOrderingEnabled_);
settings.setValue("typing_notifications", isTypingNotificationsEnabled_); settings.setValue("typing_notifications", isTypingNotificationsEnabled_);
settings.setValue("read_receipts", isReadReceiptsEnabled_); settings.setValue("read_receipts", isReadReceiptsEnabled_);
@ -214,8 +217,16 @@ UserSettingsPage::UserSettingsPage(QSharedPointer<UserSettings> settings, QWidge
scaleFactorCombo_->addItem("2.75"); scaleFactorCombo_->addItem("2.75");
scaleFactorCombo_->addItem("3"); scaleFactorCombo_->addItem("3");
scaleFactorOptionLayout->addWidget(scaleFactorLabel); auto fontSizeOptionLayout = new QHBoxLayout;
scaleFactorOptionLayout->addWidget(scaleFactorCombo_, 0, Qt::AlignRight); 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; auto themeOptionLayout_ = new QHBoxLayout;
themeOptionLayout_->setContentsMargins(0, OptionMargin, 0, OptionMargin); themeOptionLayout_->setContentsMargins(0, OptionMargin, 0, OptionMargin);
@ -316,9 +327,10 @@ UserSettingsPage::UserSettingsPage(QSharedPointer<UserSettings> settings, QWidge
scaleFactorCombo_->hide(); scaleFactorCombo_->hide();
#else #else
mainLayout_->addLayout(scaleFactorOptionLayout); mainLayout_->addLayout(scaleFactorOptionLayout);
mainLayout_->addWidget(new HorizontalLine(this));
#endif #endif
mainLayout_->addLayout(fontSizeOptionLayout);
mainLayout_->addWidget(new HorizontalLine(this));
mainLayout_->addLayout(themeOptionLayout_); mainLayout_->addLayout(themeOptionLayout_);
mainLayout_->addWidget(new HorizontalLine(this)); mainLayout_->addWidget(new HorizontalLine(this));
@ -351,6 +363,9 @@ UserSettingsPage::UserSettingsPage(QSharedPointer<UserSettings> settings, QWidge
connect(scaleFactorCombo_, connect(scaleFactorCombo_,
static_cast<void (QComboBox::*)(const QString &)>(&QComboBox::activated), static_cast<void (QComboBox::*)(const QString &)>(&QComboBox::activated),
[](const QString &factor) { utils::setScaleFactor(factor.toFloat()); }); [](const QString &factor) { utils::setScaleFactor(factor.toFloat()); });
connect(fontSizeCombo_,
static_cast<void (QComboBox::*)(const QString &)>(&QComboBox::activated),
[this](const QString &size) { settings_->setFontSize(size.trimmed().toDouble()); });
connect(trayToggle_, &Toggle::toggled, this, [this](bool isDisabled) { connect(trayToggle_, &Toggle::toggled, this, [this](bool isDisabled) {
settings_->setTray(!isDisabled); settings_->setTray(!isDisabled);
@ -395,8 +410,10 @@ UserSettingsPage::UserSettingsPage(QSharedPointer<UserSettings> settings, QWidge
void void
UserSettingsPage::showEvent(QShowEvent *) UserSettingsPage::showEvent(QShowEvent *)
{ {
restoreThemeCombo(); // FIXME macOS doesn't show the full option unless a space is added.
restoreScaleFactor(); 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" // FIXME: Toggle treats true as "off"
trayToggle_->setState(!settings_->isTrayEnabled()); trayToggle_->setState(!settings_->isTrayEnabled());
@ -431,44 +448,6 @@ UserSettingsPage::paintEvent(QPaintEvent *)
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this); 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 void
UserSettingsPage::importSessionKeys() UserSettingsPage::importSessionKeys()
{ {

View file

@ -53,6 +53,12 @@ public:
save(); save();
} }
void setFontSize(double size)
{
baseFontSize_ = size;
save();
}
void setRoomOrdering(bool state) void setRoomOrdering(bool state)
{ {
isOrderingEnabled_ = state; isOrderingEnabled_ = state;
@ -94,6 +100,7 @@ public:
bool isTypingNotificationsEnabled() const { return isTypingNotificationsEnabled_; } bool isTypingNotificationsEnabled() const { return isTypingNotificationsEnabled_; }
bool isReadReceiptsEnabled() const { return isReadReceiptsEnabled_; } bool isReadReceiptsEnabled() const { return isReadReceiptsEnabled_; }
bool hasDesktopNotifications() const { return hasDesktopNotifications_; } bool hasDesktopNotifications() const { return hasDesktopNotifications_; }
double fontSize() const { return baseFontSize_; }
signals: signals:
void groupViewStateChanged(bool state); void groupViewStateChanged(bool state);
@ -107,6 +114,7 @@ private:
bool isTypingNotificationsEnabled_; bool isTypingNotificationsEnabled_;
bool isReadReceiptsEnabled_; bool isReadReceiptsEnabled_;
bool hasDesktopNotifications_; bool hasDesktopNotifications_;
double baseFontSize_;
}; };
class HorizontalLine : public QFrame class HorizontalLine : public QFrame
@ -138,9 +146,6 @@ private slots:
void exportSessionKeys(); void exportSessionKeys();
private: private:
void restoreThemeCombo() const;
void restoreScaleFactor() const;
// Layouts // Layouts
QVBoxLayout *topLayout_; QVBoxLayout *topLayout_;
QVBoxLayout *mainLayout_; QVBoxLayout *mainLayout_;
@ -161,6 +166,7 @@ private:
QComboBox *themeCombo_; QComboBox *themeCombo_;
QComboBox *scaleFactorCombo_; QComboBox *scaleFactorCombo_;
QComboBox *fontSizeCombo_;
int sideMargin_ = 0; int sideMargin_ = 0;
}; };

View file

@ -1,6 +1,7 @@
#include "Utils.h" #include "Utils.h"
#include <QApplication> #include <QApplication>
#include <QComboBox>
#include <QDesktopWidget> #include <QDesktopWidget>
#include <QSettings> #include <QSettings>
#include <QTextDocument> #include <QTextDocument>
@ -400,3 +401,14 @@ utils::centerWidget(QWidget *widget, QWidget *parent)
widget->move(findCenter(QApplication::desktop()->screenGeometry())); 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;
}
}
}

View file

@ -14,6 +14,8 @@
#include <mtx/events/collections.hpp> #include <mtx/events/collections.hpp>
#include <mtx/events/common.hpp> #include <mtx/events/common.hpp>
class QComboBox;
namespace utils { namespace utils {
using TimelineEvent = mtx::events::collections::TimelineEvents; using TimelineEvent = mtx::events::collections::TimelineEvents;
@ -228,4 +230,7 @@ linkColor();
//! Center a widget in relation to another widget. //! Center a widget in relation to another widget.
void void
centerWidget(QWidget *widget, QWidget *parent); centerWidget(QWidget *widget, QWidget *parent);
void
restoreCombobox(QComboBox *combo, const QString &value);
} }

View file

@ -150,7 +150,12 @@ main(int argc, char *argv[])
std::exit(1); 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(); QString lang = QLocale::system().name();
@ -167,8 +172,6 @@ main(int argc, char *argv[])
// Move the MainWindow to the center // Move the MainWindow to the center
w.move(screenCenter(w.width(), w.height())); w.move(screenCenter(w.width(), w.height()));
QSettings settings;
if (!settings.value("user/window/start_in_tray", false).toBool() || if (!settings.value("user/window/start_in_tray", false).toBool() ||
!settings.value("user/window/tray", true).toBool()) !settings.value("user/window/tray", true).toBool())
w.show(); w.show();