Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scale the pixel sizes in the stylesheet according to GUI font size #3490

Merged
merged 2 commits into from
Nov 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions data/gui/normalStyle.css
Original file line number Diff line number Diff line change
Expand Up @@ -409,15 +409,22 @@ QCheckBox:disabled {
}

QCheckBox::indicator,
QListWidget::indicator,
QListView::indicator,
QGroupBox::indicator {
width: 16px;
height: 16px;
margin: 0;
background: none;
}

/* FIXME: these should configured the same way as QCheckBox and QGroupBox
* indicators, but there's a problem with spacing discussed at
* https://stackoverflow.com/q/77422984 */
QListWidget::indicator,
QListView::indicator {
margin: 0;
background: none;
}

QCheckBox::indicator:checked,
QListWidget::indicator:checked,
QListView::indicator:checked,
Expand Down
32 changes: 31 additions & 1 deletion src/gui/StelGui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,28 @@
#include <QAction>
#include <QKeySequence>

static QString applyScaleToCSS(const QString& css, const double scale)
{
auto out = css;
const QRegularExpression pat("\\b([0-9.]+)px\\b");
QRegularExpressionMatch match;
int pos = 0;
while((pos = out.indexOf(pat, pos, &match)) >= 0)
{
const auto numStr = match.captured(1);
bool ok = false;
const auto num = numStr.toDouble(&ok);
assert(ok);
const auto scaled = num * scale;
const bool hasDot = numStr.contains(QLatin1Char('.'));
const auto newNumStr = hasDot ? QString::number(scaled,'f',3)
: QString::number(int(scaled));
out.replace(pos, numStr.size(), newNumStr);
pos += newNumStr.size() + 2; // skip the whole resulting pattern
}
return out;
}

StelGui::StelGui()
: topLevelGraphicsWidget(nullptr)
, skyGui(nullptr)
Expand Down Expand Up @@ -189,6 +211,11 @@ StelGui::~StelGui()
}
}

void StelGui::updateStelStyle()
{
setStelStyle(StelApp::getInstance().getCurrentStelStyle());
}

void StelGui::init(QGraphicsWidget *atopLevelGraphicsWidget)
{
qDebug() << "Creating GUI ...";
Expand Down Expand Up @@ -384,7 +411,8 @@ void StelGui::init(QGraphicsWidget *atopLevelGraphicsWidget)
l->addItem(skyGui, 0, 0);
atopLevelGraphicsWidget->setLayout(l);

setStelStyle(StelApp::getInstance().getCurrentStelStyle());
connect(&StelApp::getInstance(), &StelApp::guiFontSizeChanged, this, &StelGui::updateStelStyle);
updateStelStyle();

int margin = conf->value("gui/space_between_groups", 5).toInt();
skyGui->bottomBar->setGroupMargin("020-gridsGroup", margin, 0);
Expand Down Expand Up @@ -484,6 +512,8 @@ void StelGui::setStelStyle(const QString& style)
else
qDebug().noquote() << "Cannot find HTML style file:" << htmlStyleFileName;

const auto scale = double(StelApp::getInstance().getGuiFontSize()) / StelApp::getDefaultGuiFontSize();
currentStelStyle.qtStyleSheet = applyScaleToCSS(currentStelStyle.qtStyleSheet, scale);
emit guiStyleChanged(currentStelStyle.qtStyleSheet);
emit htmlStyleChanged(currentStelStyle.htmlStyleSheet);
}
Expand Down
1 change: 1 addition & 0 deletions src/gui/StelGui.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ private slots:
void copySelectedObjectInfo(void);

private:
void updateStelStyle();
//! convenience method to find an action in the StelActionMgr.
StelAction* getAction(const QString& actionName) const;

Expand Down
Loading