fixed hidpi scaling for the settings window

This commit is contained in:
fourtf 2017-12-18 00:54:17 +01:00
parent 87182d078c
commit 01e9c723fa
7 changed files with 67 additions and 14 deletions

View file

@ -3,7 +3,7 @@
}
* {
font-size: 14px;
font-size: <font-size>px;
}
SettingsDialogTab:hover {
@ -17,3 +17,8 @@ QLabel, QCheckBox, QGroupBox, SettingsDialogTab {
QGroupBox {
background-color: #444;
}
QCheckBox::indicator {
width: <checkbox-size>px;
height: <checkbox-size>px;
}

View file

@ -69,6 +69,8 @@ inline bool initSettings(bool portable)
int main(int argc, char *argv[])
{
// QApplication::setAttribute(Qt::AA_Use96Dpi, true);
QApplication::setAttribute(Qt::AA_UseSoftwareOpenGL, true);
QApplication a(argc, argv);
#ifdef USEWINSDK

View file

@ -37,6 +37,7 @@ BaseWidget::BaseWidget(QWidget *parent)
float BaseWidget::getDpiMultiplier()
{
// return 1.f;
BaseWidget *baseWidget = dynamic_cast<BaseWidget *>(this->window());
if (baseWidget == nullptr) {
@ -90,8 +91,7 @@ bool BaseWidget::nativeEvent(const QByteArray &eventType, void *message, long *r
this->dpiMultiplier = dpi / 96.f;
float scale = this->dpiMultiplier / oldDpiMultiplier;
qDebug() << scale;
qDebug() << dpi;
this->dpiMultiplyerChanged(oldDpiMultiplier, this->dpiMultiplier);
this->resize(static_cast<int>(this->width() * scale),
static_cast<int>(this->height() * scale));

View file

@ -34,6 +34,10 @@ protected:
#ifdef USEWINSDK
virtual bool nativeEvent(const QByteArray &eventType, void *message, long *result) override;
#endif
virtual void dpiMultiplyerChanged(float oldDpi, float newDpi)
{
}
void initAsWindow();
private:

View file

@ -14,9 +14,6 @@ SettingsDialogTab::SettingsDialogTab(SettingsDialog *_dialog, QString _labelText
this->ui.labelText = _labelText;
this->ui.image.load(imageFileName);
// XXX: DPI (not sure if this is auto-adjusted with custom DPI)
this->setFixedHeight(32);
this->setCursor(QCursor(Qt::PointingHandCursor));
this->setStyleSheet("color: #FFF");
@ -28,6 +25,8 @@ void SettingsDialogTab::setSelected(bool _selected)
return;
}
// height: <checkbox-size>px;
this->selected = _selected;
emit selectedChanged(selected);
}

View file

@ -10,6 +10,7 @@
#include <QDebug>
#include <QFile>
#include <QFileDialog>
#include <QFont>
#include <QFontDialog>
#include <QFormLayout>
#include <QGroupBox>
@ -31,11 +32,6 @@ SettingsDialog::SettingsDialog()
{
this->initAsWindow();
QFile file(":/qss/settings.qss");
file.open(QFile::ReadOnly);
QString styleSheet = QLatin1String(file.readAll());
this->setStyleSheet(styleSheet);
QPalette palette;
palette.setColor(QPalette::Background, QColor("#444"));
this->setPalette(palette);
@ -48,11 +44,10 @@ SettingsDialog::SettingsDialog()
this->ui.vbox.addWidget(&this->ui.buttonBox);
auto tabWidget = new QWidget();
auto tabWidget = &ui.tabWidget;
tabWidget->setObjectName("tabWidget");
tabWidget->setLayout(&this->ui.tabs);
tabWidget->setFixedWidth(200);
this->ui.hbox.addWidget(tabWidget);
this->ui.hbox.addLayout(&this->ui.pageStack);
@ -71,6 +66,8 @@ SettingsDialog::SettingsDialog()
this->resize(600, 500);
this->addTabs();
this->dpiMultiplyerChanged(this->getDpiMultiplier(), this->getDpiMultiplier());
}
void SettingsDialog::addTabs()
@ -578,6 +575,7 @@ void SettingsDialog::addTab(QBoxLayout *layout, QString title, QString imageRes)
tab->setWidget(widget);
this->ui.tabs.addWidget(tab, 0, Qt::AlignTop);
tabs.push_back(tab);
this->ui.pageStack.addWidget(widget);
@ -610,6 +608,44 @@ void SettingsDialog::showDialog()
instance->setFocus();
}
void SettingsDialog::dpiMultiplyerChanged(float oldDpi, float newDpi)
{
QFile file(":/qss/settings.qss");
file.open(QFile::ReadOnly);
QString styleSheet = QLatin1String(file.readAll());
styleSheet.replace("<font-size>", QString::number((int)(14 * newDpi)));
styleSheet.replace("<checkbox-size>", QString::number((int)(14 * newDpi)));
for (SettingsDialogTab *tab : this->tabs) {
tab->setFixedHeight((int)(30 * newDpi));
}
this->setStyleSheet(styleSheet);
this->ui.tabWidget.setFixedWidth((int)(200 * newDpi));
}
void SettingsDialog::setChildrensFont(QLayout *object, QFont &font, int indent)
{
// for (QWidget *widget : this->widgets) {
// widget->setFont(font);
// }
// for (int i = 0; i < object->count(); i++) {
// if (object->itemAt(i)->layout()) {
// setChildrensFont(object->layout()->itemAt(i)->layout(), font, indent + 2);
// }
// if (object->itemAt(i)->widget()) {
// object->itemAt(i)->widget()->setFont(font);
// if (object->itemAt(i)->widget()->layout() &&
// !object->itemAt(i)->widget()->layout()->isEmpty()) {
// setChildrensFont(object->itemAt(i)->widget()->layout(), font, indent + 2);
// }
// }
// }
}
/// Widget creation helpers
QVBoxLayout *SettingsDialog::createTabLayout()
{

View file

@ -33,8 +33,12 @@ public:
static void showDialog();
protected:
virtual void dpiMultiplyerChanged(float oldDpi, float newDpi) override;
private:
SettingsSnapshot snapshot;
std::vector<SettingsDialogTab *> tabs;
pajlada::Settings::Setting<int> usernameDisplayMode;
@ -42,6 +46,7 @@ private:
QVBoxLayout tabs;
QVBoxLayout vbox;
QHBoxLayout hbox;
QWidget tabWidget;
QStackedLayout pageStack;
QDialogButtonBox buttonBox;
QPushButton okButton;
@ -49,8 +54,8 @@ private:
} ui;
void addTab(QBoxLayout *layout, QString title, QString imageRes);
void addTabs();
QVBoxLayout *createAccountsTab();
QVBoxLayout *createAppearanceTab();
QVBoxLayout *createMessagesTab();
@ -84,6 +89,8 @@ private:
void cancelButtonClicked();
std::vector<pajlada::Signals::ScopedConnection> managedConnections;
static void setChildrensFont(QLayout *object, QFont &font, int indent = 0);
};
} // namespace widgets