Skip to content

Commit

Permalink
gui / preferences: added experimental font scaling
Browse files Browse the repository at this point in the history
- other changes for a more seamless scaling

Signed-off-by: Andrei Popa <[email protected]>
  • Loading branch information
andrei47w committed Nov 20, 2024
1 parent 337cc75 commit 2ffc78e
Show file tree
Hide file tree
Showing 35 changed files with 167 additions and 214 deletions.
2 changes: 1 addition & 1 deletion core/src/emuwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ void EmuWidget::initEnBtn(QWidget *parent)
m_enDemoBtn = new AnimationPushButton(parent);
m_enDemoBtn->setText("Enable");
StyleHelper::BasicButton(m_enDemoBtn);
m_enDemoBtn->setFixedWidth(128);
m_enDemoBtn->setFixedWidth(Style::getDimension(json::global::unit_6));
QMovie *loadingIcon(new QMovie(this));
loadingIcon->setFileName(":/gui/loading.gif");
m_enDemoBtn->setAnimation(loadingIcon);
Expand Down
2 changes: 1 addition & 1 deletion core/src/iiotabwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ QWidget *IioTabWidget::createVerifyBtnWidget(QWidget *parent)
StyleHelper::BasicButton(m_btnVerify);
m_btnVerify->setText("Verify");
m_btnVerify->setIconSize(QSize(30, 30));
m_btnVerify->setFixedWidth(128);
m_btnVerify->setFixedWidth(Style::getDimension(json::global::unit_6));
m_btnVerify->setEnabled(false);
m_btnVerify->setAutoDefault(true);

Expand Down
4 changes: 2 additions & 2 deletions core/src/scopyhomeaddpage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,13 +239,13 @@ QWidget *ScopyHomeAddPage::createBtnsWidget(QWidget *parent)
m_backBtn = new QPushButton(btnsWidget);
m_backBtn->setText("BACK");
StyleHelper::BasicButton(m_backBtn);
m_backBtn->setFixedWidth(128);
m_backBtn->setFixedWidth(Style::getDimension(json::global::unit_6));

m_addBtn = new QPushButton(btnsWidget);
m_addBtn->setText("ADD DEVICE");
m_addBtn->setAutoDefault(true);
StyleHelper::BasicButton(m_addBtn);
m_addBtn->setFixedWidth(128);
m_addBtn->setFixedWidth(Style::getDimension(json::global::unit_6));

btnsLay->addWidget(m_backBtn);
btnsLay->addWidget(m_addBtn);
Expand Down
2 changes: 1 addition & 1 deletion core/src/scopyhomepage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ ScopyHomePage::ScopyHomePage(QWidget *parent, PluginManager *pm)
// addDevice("dev1","dev1","descr1",new QPushButton("abc"),new QLabel("page1"));

Style::setStyle(scanBtn(), style::properties::button::basicButton);
scanBtn()->setFixedWidth(80);
scanBtn()->setFixedWidth(Style::getDimension(json::global::unit_5));
connect(hc, SIGNAL(goLeft()), db, SLOT(prevDevice()));
connect(hc, SIGNAL(goRight()), db, SLOT(nextDevice()));
connect(db, SIGNAL(requestDevice(QString, int)), is, SLOT(slideInKey(QString, int)));
Expand Down
6 changes: 3 additions & 3 deletions core/src/scopymainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,7 @@ void ScopyMainWindow::initPreferences()
p->init("general_use_opengl", true);
#endif
p->init("general_use_animations", true);
p->init("font_scale", "1");
p->init("general_theme", "Scopy");
p->init("general_language", "en");
p->init("show_grid", true);
Expand Down Expand Up @@ -496,13 +497,12 @@ void ScopyMainWindow::handlePreferences(QString str, QVariant val)

if(str == "general_use_opengl") {
Q_EMIT p->restartRequired();

} else if(str == "general_use_animations") {
AnimationManager::getInstance().toggleAnimations(val.toBool());

} else if(str == "general_theme") {
Q_EMIT p->restartRequired();

} else if(str == "font_scale") {
Q_EMIT p->restartRequired();
} else if(str == "general_language") {
Q_EMIT p->restartRequired();
} else if(str == "general_show_status_bar") {
Expand Down
3 changes: 3 additions & 0 deletions core/src/scopymainwindow_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,9 @@ void ScopyMainWindow_API::setPreference(QString prefName, QVariant value)
} else if(prefName == "general_theme") {
qWarning(CAT_SCOPY_API) << "Restart is required for the change to take place";

} else if(prefName == "font_scale") {
qWarning(CAT_SCOPY_API) << "Restart is required for the change to take place";

} else if(prefName == "general_language") {
qWarning(CAT_SCOPY_API) << "Restart is required for the change to take place";
}
Expand Down
9 changes: 6 additions & 3 deletions core/src/scopypreferencespage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ QWidget *ScopyPreferencesPage::buildSaveSessionPreference()
lay->addWidget(new QLabel("Settings files location ", this));
QPushButton *navigateBtn = new QPushButton("Open", this);
Style::setStyle(navigateBtn, style::properties::button::borderButton);
navigateBtn->setMaximumWidth(80);
navigateBtn->setMaximumWidth(Style::getDimension(json::global::unit_5));
connect(navigateBtn, &QPushButton::clicked, this,
[=]() { QDesktopServices::openUrl(scopy::config::settingsFolderPath()); });
lay->addWidget(navigateBtn);
Expand Down Expand Up @@ -211,7 +211,7 @@ QWidget *ScopyPreferencesPage::buildResetScopyDefaultButton()

QPushButton *resetBtn = new QPushButton("Reset", this);
Style::setStyle(resetBtn, style::properties::button::borderButton);
resetBtn->setMaximumWidth(80);
resetBtn->setMaximumWidth(Style::getDimension(json::global::unit_5));
connect(resetBtn, &QPushButton::clicked, this, &ScopyPreferencesPage::resetScopyPreferences);
lay->addWidget(resetBtn);
lay->setMargin(0);
Expand Down Expand Up @@ -270,6 +270,9 @@ QWidget *ScopyPreferencesPage::buildGeneralPreferencesPage()
QWidget *autoConnectCb = PreferencesHelper::addPreferenceCheckBox(
p, "autoconnect_previous", "Auto-connect to previous session", generalSection);
generalSection->contentLayout()->addWidget(autoConnectCb);
generalSection->contentLayout()->addWidget(PreferencesHelper::addPreferenceCombo(
p, "font_scale", "Font scale (EXPERIMENTAL)", {"1", "1.15", "1.3", "1.45", "1.6", "1.75", "1.9"},
generalSection));
generalSection->contentLayout()->addWidget(PreferencesHelper::addPreferenceCombo(
p, "general_theme", "Theme", Style::GetInstance()->getThemeList(), generalSection));
generalSection->contentLayout()->addWidget(PreferencesHelper::addPreferenceCombo(
Expand All @@ -292,7 +295,7 @@ QWidget *ScopyPreferencesPage::buildGeneralPreferencesPage()
lay->addSpacerItem(new QSpacerItem(0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding));

m_devRefresh = new QPushButton("Refresh", m_autoConnectWidget);
m_devRefresh->setMaximumWidth(80);
m_devRefresh->setMaximumWidth(Style::getDimension(json::global::unit_5));
Style::setStyle(m_devRefresh, style::properties::button::basicButton);
m_autoConnectWidget->add(m_devRefresh);

Expand Down
2 changes: 2 additions & 0 deletions gui/include/gui/style.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class SCOPY_GUI_EXPORT Style : public QObject
QString getAllProperties();
static bool isProperty(QString style);
static const char *replaceProperty(const char *prop);
static QString scaleNumberInString(QString string, float factor);
static QString adjustForScaling(QString key, QString value);

public:
static Style *GetInstance();
Expand Down
12 changes: 8 additions & 4 deletions gui/src/basictracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,8 @@ QRect BasicTracker::trackerRect(QSizeF size) const
yOffset = m_canvas->height() - bottomRight.y();
}

// hackish - magic +-3 so the whole text is visible
topLeft.rx() += xOffset - 3;
bottomRight.rx() += xOffset + 3;
topLeft.rx() += xOffset;
bottomRight.rx() += xOffset;
topLeft.ry() += yOffset;
bottomRight.ry() += yOffset;

Expand All @@ -114,6 +113,7 @@ QRect BasicTracker::trackerRect(QSizeF size) const

QwtText *BasicTracker::trackerText(QPoint pos) const
{
QString padding = " ";
QString xText = "";
QString yText = "";
QString separator = "";
Expand All @@ -140,9 +140,13 @@ QwtText *BasicTracker::trackerText(QPoint pos) const
separator = ", ";
}

QwtText *text = new QwtText(xText + separator + yText);
QwtText *text = new QwtText(padding + xText + separator + yText + padding);
text->setColor(m_color);

QFont font = QFont();
font.setPixelSize(Style::getDimension(json::global::font_size));
text->setFont(font);

return text;
}

Expand Down
59 changes: 53 additions & 6 deletions gui/src/style.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@

#include <common/scopyconfig.h>

#include <pluginbase/preferences.h>

using namespace scopy;

Style *Style::pinstance_{nullptr};
Expand Down Expand Up @@ -201,7 +203,7 @@ QString Style::getAttribute(const char *key)
attr = m_global_json->object().value(key).toString();
}

return replaceAttributes(attr);
return replaceAttributes(adjustForScaling(key, attr));
}

QColor Style::getChannelColor(int index) { return getChannelColorList()[index]; }
Expand Down Expand Up @@ -238,7 +240,24 @@ void Style::setBackgroundColor(QWidget *widget, QString color, bool extend_to_ch

QColor Style::getColor(const char *key) { return QColor(getAttribute(key)); }

int Style::getDimension(const char *key) { return getAttribute(key).toInt(); }
int Style::getDimension(const char *key)
{
QString attr = getAttribute(key);
int number = attr.toInt();

// this is for attributes with a string suffix. like "10px"
if(number == 0) {
QRegularExpression regex("(\\d+)");
QRegularExpressionMatch match = regex.match(attr);

if(match.hasMatch()) {
QString numberStr = match.captured(1);
number = numberStr.toInt();
}
}

return number;
}

const char *Style::replaceProperty(const char *prop)
{
Expand All @@ -257,12 +276,12 @@ QString Style::replaceAttributes(QString style, int calls_limit)
{
if(style.contains('&') && calls_limit > 0) {
for(const QString &key : m_theme_json->object().keys()) {
QJsonValue value = m_theme_json->object().value(key);
style.replace("&" + key + "&", value.toString());
QString value = m_theme_json->object().value(key).toString();
style.replace("&" + key + "&", adjustForScaling(QString(key), value));
}
for(const QString &key : m_global_json->object().keys()) {
QJsonValue value = m_global_json->object().value(key);
style.replace("&" + key + "&", value.toString());
QString value = m_global_json->object().value(key).toString();
style.replace("&" + key + "&", adjustForScaling(QString(key), value));
}

style = replaceAttributes(style, --calls_limit);
Expand Down Expand Up @@ -312,3 +331,31 @@ void Style::setGlobalStyle(QWidget *widget)
}

void Style::setM2KStylesheet(QWidget *widget) { widget->setStyleSheet(m_styleMap->value(m_m2kqssFile)); }

QString Style::scaleNumberInString(QString string, float factor)
{
QRegularExpression regex("(\\d+)");
QRegularExpressionMatch match = regex.match(string);

if(match.hasMatch()) {
QString numberStr = match.captured(1);
int number = numberStr.toInt();
int scaledNumber = number * factor;
return string.replace(regex, QString::number(scaledNumber));
}

return string;
}

QString Style::adjustForScaling(QString key, QString value)
{
if(QString(key).startsWith("font_size")) {
value = scaleNumberInString(value, Preferences::GetInstance()->get("font_scale").toFloat());
}
if(QString(key).startsWith("unit_")) {
value = scaleNumberInString(value,
(Preferences::GetInstance()->get("font_scale").toFloat() - 1) / 2 + 1);
}

return value;
}
Loading

0 comments on commit 2ffc78e

Please sign in to comment.