From 85e93a88a697b8b287689721b3c5e48a8ea405c0 Mon Sep 17 00:00:00 2001 From: Konstantinos Sideris Date: Sun, 22 Jul 2018 19:48:58 +0300 Subject: [PATCH] Add option to specify the scale factor fixes #357 fixes #335 fixes #230 --- src/CommunitiesList.cpp | 2 +- src/LoginPage.cpp | 3 +- src/UserSettingsPage.cpp | 59 ++++++++++++++++++++++++++++++++++++++++ src/UserSettingsPage.h | 2 ++ src/Utils.cpp | 17 ++++++++++++ src/Utils.h | 6 ++++ src/main.cpp | 15 ++++++++-- 7 files changed, 99 insertions(+), 5 deletions(-) diff --git a/src/CommunitiesList.cpp b/src/CommunitiesList.cpp index af30050d..7054db9d 100644 --- a/src/CommunitiesList.cpp +++ b/src/CommunitiesList.cpp @@ -1,5 +1,5 @@ -#include "Cache.h" #include "CommunitiesList.h" +#include "Cache.h" #include "Logging.h" #include "MatrixClient.h" diff --git a/src/LoginPage.cpp b/src/LoginPage.cpp index c0880bdd..4ec9373e 100644 --- a/src/LoginPage.cpp +++ b/src/LoginPage.cpp @@ -277,7 +277,8 @@ LoginPage::onLoginButtonClicked() http::client()->login( user.localpart(), password_input_->text().toStdString(), - deviceName_->text().isEmpty() ? initialDeviceName() : deviceName_->text().toStdString(), + deviceName_->text().trimmed().isEmpty() ? initialDeviceName() + : deviceName_->text().toStdString(), [this](const mtx::responses::Login &res, mtx::http::RequestErr err) { if (err) { emit loginError(QString::fromStdString(err->matrix_error.error)); diff --git a/src/UserSettingsPage.cpp b/src/UserSettingsPage.cpp index 04ce5b5d..ad5d484f 100644 --- a/src/UserSettingsPage.cpp +++ b/src/UserSettingsPage.cpp @@ -25,6 +25,7 @@ #include "Config.h" #include "UserSettingsPage.h" +#include "Utils.h" #include "ui/FlatButton.h" #include "ui/ToggleButton.h" @@ -187,6 +188,24 @@ UserSettingsPage::UserSettingsPage(QSharedPointer settings, QWidge receiptsLayout->addWidget(receiptsLabel); receiptsLayout->addWidget(readReceipts_, 0, Qt::AlignBottom | Qt::AlignRight); + auto scaleFactorOptionLayout = new QHBoxLayout; + scaleFactorOptionLayout->setContentsMargins(0, OptionMargin, 0, OptionMargin); + auto scaleFactorLabel = new QLabel(tr("Scale factor (requires restart)"), this); + scaleFactorLabel->setFont(font); + scaleFactorCombo_ = new QComboBox(this); + scaleFactorCombo_->addItem("1"); + scaleFactorCombo_->addItem("1.25"); + scaleFactorCombo_->addItem("1.5"); + scaleFactorCombo_->addItem("1.75"); + scaleFactorCombo_->addItem("2"); + scaleFactorCombo_->addItem("2.25"); + scaleFactorCombo_->addItem("2.5"); + scaleFactorCombo_->addItem("2.75"); + scaleFactorCombo_->addItem("3"); + + scaleFactorOptionLayout->addWidget(scaleFactorLabel); + scaleFactorOptionLayout->addWidget(scaleFactorCombo_, 0, Qt::AlignBottom | Qt::AlignRight); + auto themeOptionLayout_ = new QHBoxLayout; themeOptionLayout_->setContentsMargins(0, OptionMargin, 0, OptionMargin); auto themeLabel_ = new QLabel(tr("Theme"), this); @@ -219,6 +238,15 @@ UserSettingsPage::UserSettingsPage(QSharedPointer settings, QWidge mainLayout_->addLayout(typingLayout); mainLayout_->addLayout(receiptsLayout); mainLayout_->addWidget(new HorizontalLine(this)); + +#if defined(Q_OS_MAC) + scaleFactorLabel->hide(); + scaleFactorCombo_->hide(); +#else + mainLayout_->addLayout(scaleFactorOptionLayout); + mainLayout_->addWidget(new HorizontalLine(this)); +#endif + mainLayout_->addLayout(themeOptionLayout_); mainLayout_->addWidget(new HorizontalLine(this)); @@ -241,6 +269,9 @@ UserSettingsPage::UserSettingsPage(QSharedPointer settings, QWidge connect(themeCombo_, static_cast(&QComboBox::activated), [this](const QString &text) { settings_->setTheme(text.toLower()); }); + connect(scaleFactorCombo_, + static_cast(&QComboBox::activated), + [this](const QString &factor) { utils::setScaleFactor(factor.toFloat()); }); connect(trayToggle_, &Toggle::toggled, this, [this](bool isDisabled) { settings_->setTray(!isDisabled); @@ -282,6 +313,7 @@ void UserSettingsPage::showEvent(QShowEvent *) { restoreThemeCombo(); + restoreScaleFactor(); // FIXME: Toggle treats true as "off" trayToggle_->setState(!settings_->isTrayEnabled()); @@ -311,6 +343,33 @@ 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 { diff --git a/src/UserSettingsPage.h b/src/UserSettingsPage.h index 177f1921..f6ecd9c7 100644 --- a/src/UserSettingsPage.h +++ b/src/UserSettingsPage.h @@ -126,6 +126,7 @@ signals: private: void restoreThemeCombo() const; + void restoreScaleFactor() const; // Layouts QVBoxLayout *topLayout_; @@ -143,6 +144,7 @@ private: Toggle *readReceipts_; QComboBox *themeCombo_; + QComboBox *scaleFactorCombo_; int sideMargin_ = 0; }; diff --git a/src/Utils.cpp b/src/Utils.cpp index e6b0bcce..809ea293 100644 --- a/src/Utils.cpp +++ b/src/Utils.cpp @@ -15,6 +15,23 @@ utils::localUser() return settings.value("auth/user_id").toString(); } +void +utils::setScaleFactor(float factor) +{ + if (factor < 1 || factor > 3) + return; + + QSettings settings; + settings.setValue("settings/scale_factor", factor); +} + +float +utils::scaleFactor() +{ + QSettings settings("nheko", "nheko"); + return settings.value("settings/scale_factor", -1).toFloat(); +} + bool utils::respondsToKeyRequests(const std::string &roomId) { diff --git a/src/Utils.h b/src/Utils.h index b8d675f3..7132f2ab 100644 --- a/src/Utils.h +++ b/src/Utils.h @@ -18,6 +18,12 @@ using TimelineEvent = mtx::events::collections::TimelineEvents; QString localUser(); +float +scaleFactor(); + +void +setScaleFactor(float factor); + //! Whether or not we should respond to key requests for the given room. bool respondsToKeyRequests(const QString &roomId); diff --git a/src/main.cpp b/src/main.cpp index d8bf30ce..694811a7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -36,6 +37,7 @@ #include "MainWindow.h" #include "MatrixClient.h" #include "RunGuard.h" +#include "Utils.h" #include "ui/RaisedButton.h" #include "version.h" @@ -98,7 +100,6 @@ main(int argc, char *argv[]) QApplication a(argc, argv); QFont font; - font.setPointSize(15); font.setWeight(60); QWidget widget; @@ -117,7 +118,6 @@ main(int argc, char *argv[]) RaisedButton submitBtn("OK"); submitBtn.setBackgroundColor(pal.color(QPalette::Button)); submitBtn.setForegroundColor(pal.color(QPalette::ButtonText)); - submitBtn.setMinimumSize(120, 35); submitBtn.setFontSize(conf::btn::fontSize); submitBtn.setCornerRadius(conf::btn::cornerRadius); @@ -127,7 +127,7 @@ main(int argc, char *argv[]) layout.addWidget(&msg); layout.addLayout(&btnLayout); - widget.setFixedSize(480, 180); + widget.setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum); widget.move(screenCenter(widget.width(), widget.height())); widget.show(); @@ -136,6 +136,15 @@ main(int argc, char *argv[]) return a.exec(); } +#if defined(Q_OS_LINUX) || defined(Q_OS_WIN) + if (qgetenv("QT_SCALE_FACTOR").size() == 0) { + float factor = utils::scaleFactor(); + + if (factor != -1) + qputenv("QT_SCALE_FACTOR", QString::number(factor).toUtf8()); + } +#endif + QApplication app(argc, argv); QCoreApplication::setApplicationName("nheko"); QCoreApplication::setApplicationVersion(nheko::version);