From 654b652db4493b4f27da3009307b832dc5e90396 Mon Sep 17 00:00:00 2001 From: redsky17 Date: Sat, 19 Jan 2019 16:20:41 +0000 Subject: Add User Font Setting User can now select a font from the installed fonts on their system This font currently will only be applied when nheko is restarted (similar to how font size and scaling currently work). This will be addressed in a future commit. Additionally, the dropdown does not correctly select the previously-chosen user font, and instead defaults to the first font available on the system (alphabetically). This is similar to the issue with the 'Theme' combo defaulting to 'Light' even when another theme is selected. --- src/UserSettingsPage.cpp | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) (limited to 'src/UserSettingsPage.cpp') diff --git a/src/UserSettingsPage.cpp b/src/UserSettingsPage.cpp index 15ad72e1..194a32e1 100644 --- a/src/UserSettingsPage.cpp +++ b/src/UserSettingsPage.cpp @@ -49,7 +49,7 @@ UserSettings::load() isTypingNotificationsEnabled_ = settings.value("user/typing_notifications", true).toBool(); isReadReceiptsEnabled_ = settings.value("user/read_receipts", true).toBool(); theme_ = settings.value("user/theme", "light").toString(); - + font_ = settings.value("user/font_family", "default").toString(); baseFontSize_ = settings.value("user/font_size", QFont().pointSizeF()).toDouble(); applyTheme(); @@ -62,6 +62,13 @@ UserSettings::setFontSize(double size) save(); } +void +UserSettings::setFontFamily(QString family) +{ + font_ = family; + save(); +} + void UserSettings::setTheme(QString theme) { @@ -106,6 +113,7 @@ UserSettings::save() settings.setValue("group_view", isGroupViewEnabled_); settings.setValue("desktop_notifications", hasDesktopNotifications_); settings.setValue("theme", theme()); + settings.setValue("font_family", font_); settings.endGroup(); } @@ -220,6 +228,20 @@ UserSettingsPage::UserSettingsPage(QSharedPointer settings, QWidge fontSizeOptionLayout->addWidget(fontSizeLabel); fontSizeOptionLayout->addWidget(fontSizeCombo_, 0, Qt::AlignRight); + auto fontFamilyOptionLayout = new QHBoxLayout; + fontFamilyOptionLayout->setContentsMargins(0, OptionMargin, 0, OptionMargin); + auto fontFamilyLabel = new QLabel(tr("Font Family"), this); + fontFamilyLabel->setFont(font); + fontSelectionCombo_ = new QComboBox(this); + QFontDatabase fontDb; + auto fontFamilies = fontDb.families(); + for (const auto &family : fontFamilies) { + fontSelectionCombo_->addItem(family); + } + + fontFamilyOptionLayout->addWidget(fontFamilyLabel); + fontFamilyOptionLayout->addWidget(fontSelectionCombo_, 0, Qt::AlignRight); + auto themeOptionLayout_ = new QHBoxLayout; themeOptionLayout_->setContentsMargins(0, OptionMargin, 0, OptionMargin); auto themeLabel_ = new QLabel(tr("Theme"), this); @@ -319,6 +341,7 @@ UserSettingsPage::UserSettingsPage(QSharedPointer settings, QWidge mainLayout_->addLayout(scaleFactorOptionLayout); mainLayout_->addLayout(fontSizeOptionLayout); + mainLayout_->addLayout(fontFamilyOptionLayout); mainLayout_->addWidget(new HorizontalLine(this)); mainLayout_->addLayout(themeOptionLayout_); mainLayout_->addWidget(new HorizontalLine(this)); @@ -355,7 +378,9 @@ UserSettingsPage::UserSettingsPage(QSharedPointer settings, QWidge connect(fontSizeCombo_, static_cast(&QComboBox::activated), [this](const QString &size) { settings_->setFontSize(size.trimmed().toDouble()); }); - + connect(fontSelectionCombo_, + static_cast(&QComboBox::activated), + [this](const QString &family) { settings_->setFontFamily(family.trimmed()); }); connect(trayToggle_, &Toggle::toggled, this, [this](bool isDisabled) { settings_->setTray(!isDisabled); if (isDisabled) { -- cgit 1.5.1 From 98102f4f092d3e47f98dd81d978932a3838e19fe Mon Sep 17 00:00:00 2001 From: redsky17 Date: Sat, 19 Jan 2019 17:31:17 +0000 Subject: Fix UserSettings UI not showing saved prefs Theme and Font Family settings will now correctly display the stored settings when nheko is re-launched. Previously, these combo boxes would default to the first thing in the combo box, even if that wasn't what the user selected. --- src/UserSettingsPage.cpp | 8 ++++++++ src/UserSettingsPage.h | 1 + 2 files changed, 9 insertions(+) (limited to 'src/UserSettingsPage.cpp') diff --git a/src/UserSettingsPage.cpp b/src/UserSettingsPage.cpp index 194a32e1..fdb67080 100644 --- a/src/UserSettingsPage.cpp +++ b/src/UserSettingsPage.cpp @@ -239,6 +239,9 @@ UserSettingsPage::UserSettingsPage(QSharedPointer settings, QWidge fontSelectionCombo_->addItem(family); } + int fontIndex = fontSelectionCombo_->findText(settings_->font()); + fontSelectionCombo_->setCurrentIndex(fontIndex); + fontFamilyOptionLayout->addWidget(fontFamilyLabel); fontFamilyOptionLayout->addWidget(fontSelectionCombo_, 0, Qt::AlignRight); @@ -251,6 +254,11 @@ UserSettingsPage::UserSettingsPage(QSharedPointer settings, QWidge themeCombo_->addItem("Dark"); themeCombo_->addItem("System"); + QString themeStr = settings_->theme(); + themeStr.replace(0, 1, themeStr[0].toUpper()); + int themeIndex = themeCombo_->findText(themeStr); + themeCombo_->setCurrentIndex(themeIndex); + themeOptionLayout_->addWidget(themeLabel_); themeOptionLayout_->addWidget(themeCombo_, 0, Qt::AlignRight); diff --git a/src/UserSettingsPage.h b/src/UserSettingsPage.h index 196ba844..94fb816f 100644 --- a/src/UserSettingsPage.h +++ b/src/UserSettingsPage.h @@ -92,6 +92,7 @@ public: bool isReadReceiptsEnabled() const { return isReadReceiptsEnabled_; } bool hasDesktopNotifications() const { return hasDesktopNotifications_; } double fontSize() const { return baseFontSize_; } + QString font() const { return font_; } signals: void groupViewStateChanged(bool state); -- cgit 1.5.1 From 237c7ad114b7c3d6960fdaf712b600e715eff082 Mon Sep 17 00:00:00 2001 From: redsky17 Date: Sun, 20 Jan 2019 04:43:48 +0000 Subject: Author Color Fixes Author color is now cached so that it will not be re-calculated each time a new message is posted. This cache gets cleared when the theme is changed. Additionally, the author color is now automatically refreshed when the theme is changed, fixing the issue where you had to change rooms before the colors would switch. --- src/ChatPage.h | 1 + src/MainWindow.cpp | 6 +++++- src/UserSettingsPage.cpp | 5 ++++- src/UserSettingsPage.h | 1 + src/Utils.cpp | 24 ++++++++++++++++++++++++ src/Utils.h | 12 ++++++++++++ src/timeline/TimelineItem.cpp | 30 +++++++++++++++++++++++++----- src/timeline/TimelineItem.h | 3 +++ 8 files changed, 75 insertions(+), 7 deletions(-) (limited to 'src/UserSettingsPage.cpp') diff --git a/src/ChatPage.h b/src/ChatPage.h index 2c728c17..492613ec 100644 --- a/src/ChatPage.h +++ b/src/ChatPage.h @@ -148,6 +148,7 @@ signals: const QImage &icon); void updateGroupsInfo(const mtx::responses::JoinedGroups &groups); + void themeChanged(); private slots: void showUnreadMessageNotification(int count); diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index 55dbba34..450eb71a 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -112,7 +112,11 @@ MainWindow::MainWindow(QWidget *parent) connect( userSettingsPage_, SIGNAL(trayOptionChanged(bool)), trayIcon_, SLOT(setVisible(bool))); - + connect(userSettingsPage_, &UserSettingsPage::themeChanged, this, [this]() { + utils::clearAuthorColors(); + }); + connect( + userSettingsPage_, &UserSettingsPage::themeChanged, chat_page_, &ChatPage::themeChanged); connect(trayIcon_, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, diff --git a/src/UserSettingsPage.cpp b/src/UserSettingsPage.cpp index fdb67080..e3c0d190 100644 --- a/src/UserSettingsPage.cpp +++ b/src/UserSettingsPage.cpp @@ -379,7 +379,10 @@ UserSettingsPage::UserSettingsPage(QSharedPointer settings, QWidge connect(themeCombo_, static_cast(&QComboBox::activated), - [this](const QString &text) { settings_->setTheme(text.toLower()); }); + [this](const QString &text) { + settings_->setTheme(text.toLower()); + emit themeChanged(); + }); connect(scaleFactorCombo_, static_cast(&QComboBox::activated), [](const QString &factor) { utils::setScaleFactor(factor.toFloat()); }); diff --git a/src/UserSettingsPage.h b/src/UserSettingsPage.h index 94fb816f..900f57e4 100644 --- a/src/UserSettingsPage.h +++ b/src/UserSettingsPage.h @@ -132,6 +132,7 @@ protected: signals: void moveBack(); void trayOptionChanged(bool value); + void themeChanged(); private slots: void importSessionKeys(); diff --git a/src/Utils.cpp b/src/Utils.cpp index b7980fc3..66bc86f6 100644 --- a/src/Utils.cpp +++ b/src/Utils.cpp @@ -15,6 +15,8 @@ using TimelineEvent = mtx::events::collections::TimelineEvents; +QHash authorColors_; + QString utils::localUser() { @@ -511,6 +513,28 @@ utils::luminance(const QColor &col) return lum; } +void +utils::clearAuthorColors() +{ + authorColors_.clear(); +} + +QString +utils::getAuthorColor(const QString &author) +{ + if (authorColors_.contains(author)) { + return authorColors_[author]; + } else { + return ""; + } +} + +void +utils::addAuthorColor(const QString &author, const QString &color) +{ + authorColors_[author] = color; +} + void utils::centerWidget(QWidget *widget, QWidget *parent) { diff --git a/src/Utils.h b/src/Utils.h index 8672e7d4..b6f89f73 100644 --- a/src/Utils.h +++ b/src/Utils.h @@ -246,6 +246,18 @@ computeContrast(const qreal &one, const qreal &two); qreal luminance(const QColor &col); +//! Clear the author color hashmap +void +clearAuthorColors(); + +//! Get the given QString from the authorColors hash +QString +getAuthorColor(const QString &author); + +//! Put the given QString into the authorColor hash +void +addAuthorColor(const QString &author, const QString &color); + //! Center a widget in relation to another widget. void centerWidget(QWidget *widget, QWidget *parent); diff --git a/src/timeline/TimelineItem.cpp b/src/timeline/TimelineItem.cpp index bd3d73bc..103bd6d6 100644 --- a/src/timeline/TimelineItem.cpp +++ b/src/timeline/TimelineItem.cpp @@ -192,7 +192,8 @@ TimelineItem::init() emit eventRedacted(event_id_); }); }); - + connect( + ChatPage::instance(), &ChatPage::themeChanged, this, &TimelineItem::refreshAuthorColor); connect(markAsRead_, &QAction::triggered, this, &TimelineItem::sendReadReceipt); connect(viewRawMessage_, &QAction::triggered, this, &TimelineItem::openRawMessageViewer); @@ -603,6 +604,21 @@ TimelineItem::generateBody(const QString &body) }); } +void +TimelineItem::refreshAuthorColor() +{ + if (userName_) { + QString userColor = utils::getAuthorColor(userName_->text()); + if (userColor.isEmpty()) { + qApp->style()->polish(this); + // generate user's unique color. + auto backCol = backgroundColor().name(); + userColor = utils::generateContrastingHexColor(userName_->text(), backCol); + utils::addAuthorColor(userName_->text(), userColor); + } + userName_->setStyleSheet("QLabel { color : " + userColor + "; }"); + } +} // The username/timestamp is displayed along with the message body. void TimelineItem::generateBody(const QString &user_id, const QString &displayname, const QString &body) @@ -639,10 +655,14 @@ TimelineItem::generateUserName(const QString &user_id, const QString &displaynam // TimelineItem isn't displayed. This forces the QSS to get // loaded. - qApp->style()->polish(this); - // generate user's unique color. - auto backCol = backgroundColor().name(); - auto userColor = utils::generateContrastingHexColor(user_id, backCol); + QString userColor = utils::getAuthorColor(user_id); + if (userColor.isEmpty()) { + qApp->style()->polish(this); + // generate user's unique color. + auto backCol = backgroundColor().name(); + userColor = utils::generateContrastingHexColor(user_id, backCol); + utils::addAuthorColor(user_id, userColor); + } userName_->setStyleSheet("QLabel { color : " + userColor + "; }"); auto filter = new UserProfileFilter(user_id, userName_); diff --git a/src/timeline/TimelineItem.h b/src/timeline/TimelineItem.h index c7f320d5..6ed3325f 100644 --- a/src/timeline/TimelineItem.h +++ b/src/timeline/TimelineItem.h @@ -227,6 +227,9 @@ signals: void eventRedacted(const QString &event_id); void redactionFailed(const QString &msg); +public slots: + void refreshAuthorColor(); + protected: void paintEvent(QPaintEvent *event) override; void contextMenuEvent(QContextMenuEvent *event) override; -- cgit 1.5.1