Merge pull request #4311 from huffman/application-dpi-scale

Application dpi scale
This commit is contained in:
Andrew Meadows 2015-02-23 09:43:12 -08:00
commit 2519f66037
7 changed files with 2445 additions and 2302 deletions

View file

@ -12,6 +12,9 @@
#include <QStyle>
#include <QStyleOptionTitleBar>
#include "DependencyManager.h"
#include "GLCanvas.h"
#include "UIUtil.h"
int UIUtil::getWindowTitleBarHeight(const QWidget* window) {
@ -27,3 +30,49 @@ int UIUtil::getWindowTitleBarHeight(const QWidget* window) {
return titleBarHeight;
}
// When setting the font size of a widget in points, the rendered text will be larger
// on Windows and Linux than on Mac OSX. This is because Windows and Linux use a DPI
// of 96, while OSX uses 72. In order to get consistent results across platforms, the
// font sizes need to be scaled based on the system DPI.
// This function will scale the font size of widget and all
// of its children recursively.
//
// When creating widgets, if a font size isn't explicitly set Qt will choose a
// reasonable, but often different font size across platforms. This means
// YOU SHOUD EXPLICITLY SET ALL FONT SIZES and call this function OR not use
// this function at all. If you mix both you will end up with inconsistent results
// across platforms.
void UIUtil::scaleWidgetFontSizes(QWidget* widget) {
auto glCanvas = DependencyManager::get<GLCanvas>();
// This is the base dpi that we are targetting. This is based on Mac OSXs default DPI,
// and is the basis for all font sizes.
const float BASE_DPI = 72.0f;
#ifdef Q_OS_MAC
const float NATIVE_DPI = 72.0f;
#else
const float NATIVE_DPI = 96.0f;
#endif
// Scale fonts based on the native dpi. On Windows, where the native DPI is 96,
// the scale will be: 72.0 / 96.0 = 0.75
float fontScale = BASE_DPI / NATIVE_DPI;
internalScaleWidgetFontSizes(widget, fontScale);
}
// Depth-first traversal through widget hierarchy. It is important to do a depth-first
// traversal because modifying the font size of a parent node can affect children.
void UIUtil::internalScaleWidgetFontSizes(QWidget* widget, float scale) {
for (auto child : widget->findChildren<QWidget*>()) {
if (child->parent() == widget) {
internalScaleWidgetFontSizes(child, scale);
}
}
QFont font = widget->font();
font.setPointSizeF(font.pointSizeF() * scale);
widget->setFont(font);
}

View file

@ -18,7 +18,10 @@
class UIUtil {
public:
static int getWindowTitleBarHeight(const QWidget* window);
static void scaleWidgetFontSizes(QWidget* widget);
private:
static void internalScaleWidgetFontSizes(QWidget* widget, float scale);
};
#endif // hifi_UIUtil_h

View file

@ -21,6 +21,7 @@
#include "AccountManager.h"
#include "ui_loginDialog.h"
#include "LoginDialog.h"
#include "UIUtil.h"
const QString FORGOT_PASSWORD_URL = "https://metaverse.highfidelity.io/users/password/new";
@ -42,6 +43,8 @@ LoginDialog::LoginDialog(QWidget* parent) :
connect(_ui->closeButton, &QPushButton::clicked,
this, &LoginDialog::close);
UIUtil::scaleWidgetFontSizes(this);
// Initialize toggle connection
toggleQAction();
};

View file

@ -10,6 +10,7 @@
//
#include <QFileDialog>
#include <QFont>
#include <AudioClient.h>
#include <avatar/AvatarManager.h>
@ -23,6 +24,7 @@
#include "PreferencesDialog.h"
#include "Snapshot.h"
#include "UserActivityLogger.h"
#include "UIUtil.h"
const int PREFERENCES_HEIGHT_PADDING = 20;
@ -46,6 +48,8 @@ PreferencesDialog::PreferencesDialog(QWidget* parent) :
// move dialog to left side
move(parentWidget()->geometry().topLeft());
setFixedHeight(parentWidget()->size().height() - PREFERENCES_HEIGHT_PADDING);
UIUtil::scaleWidgetFontSizes(this);
}
void PreferencesDialog::accept() {

View file

@ -64,6 +64,8 @@ RunningScriptsWidget::RunningScriptsWidget(QWidget* parent) :
connect(ui->loadScriptFromURLButton, &QPushButton::clicked,
Application::getInstance(), &Application::loadScriptURLDialog);
connect(&_signalMapper, SIGNAL(mapped(QString)), Application::getInstance(), SLOT(stopScript(const QString&)));
UIUtil::scaleWidgetFontSizes(this);
}
RunningScriptsWidget::~RunningScriptsWidget() {
@ -103,6 +105,7 @@ void RunningScriptsWidget::setRunningScripts(const QStringList& list) {
hash.insert(list.at(i), 1);
}
QWidget* row = new QWidget(ui->scriptListWidget);
row->setFont(ui->scriptListWidget->font());
row->setLayout(new QHBoxLayout(row));
QUrl url = QUrl(list.at(i));

File diff suppressed because it is too large Load diff

View file

@ -10,6 +10,12 @@
<height>728</height>
</rect>
</property>
<property name="font">
<font>
<family>Helvetica,Arial,sans-serif</family>
<pointsize>13</pointsize>
</font>
</property>
<property name="windowTitle">
<string>Running Scripts</string>
</property>
@ -31,6 +37,12 @@
<height>141</height>
</size>
</property>
<property name="font">
<font>
<family>Helvetica,Arial,sans-serif</family>
<pointsize>13</pointsize>
</font>
</property>
<layout class="QVBoxLayout" name="verticalLayout_4">
<property name="spacing">
<number>0</number>
@ -113,6 +125,12 @@ font: bold 16pt;
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<family>Helvetica,Arial,sans-serif</family>
<pointsize>13</pointsize>
</font>
</property>
<property name="styleSheet">
<string notr="true">reloadStopButtonArea { padding: 0 }</string>
</property>
@ -131,6 +149,12 @@ font: bold 16pt;
</property>
<item>
<widget class="QPushButton" name="reloadAllButton">
<property name="font">
<font>
<family>Helvetica,Arial,sans-serif</family>
<pointsize>13</pointsize>
</font>
</property>
<property name="text">
<string>Reload all</string>
</property>
@ -138,6 +162,12 @@ font: bold 16pt;
</item>
<item>
<widget class="QPushButton" name="stopAllButton">
<property name="font">
<font>
<family>Helvetica,Arial,sans-serif</family>
<pointsize>13</pointsize>
</font>
</property>
<property name="text">
<string>Stop all</string>
</property>
@ -167,6 +197,12 @@ font: bold 16pt;
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<family>Helvetica,Arial,sans-serif</family>
<pointsize>13</pointsize>
</font>
</property>
<layout class="QVBoxLayout" name="verticalLayout_8">
<property name="spacing">
<number>0</number>
@ -191,8 +227,14 @@ font: bold 16pt;
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<family>Helvetica,Arial,sans-serif</family>
<pointsize>14</pointsize>
</font>
</property>
<property name="styleSheet">
<string notr="true">font: 14pt; color: #5f5f5f; margin: 2px;</string>
<string notr="true">color: #5f5f5f; margin: 2px;</string>
</property>
<property name="text">
<string>There are no scripts running.</string>
@ -255,12 +297,15 @@ font: bold 16pt;
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<family>Helvetica,Arial,sans-serif</family>
<pointsize>14</pointsize>
</font>
</property>
<property name="layoutDirection">
<enum>Qt::LeftToRight</enum>
</property>
<property name="styleSheet">
<string notr="true">font-size: 14pt;</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_3">
<property name="spacing">
<number>0</number>
@ -279,6 +324,12 @@ font: bold 16pt;
</property>
<item>
<widget class="QWidget" name="scriptListWidget" native="true">
<property name="font">
<font>
<family>Helvetica,Arial,sans-serif</family>
<pointsize>14</pointsize>
</font>
</property>
<layout class="QVBoxLayout" name="verticalLayout_5">
<property name="spacing">
<number>0</number>
@ -300,8 +351,14 @@ font: bold 16pt;
</item>
<item>
<widget class="QLabel" name="tipLabel">
<property name="font">
<font>
<family>Helvetica,Arial,sans-serif</family>
<pointsize>14</pointsize>
</font>
</property>
<property name="styleSheet">
<string notr="true">font: 14pt; color: #5f5f5f; margin: 2px;</string>
<string notr="true">color: #5f5f5f; margin: 2px;</string>
</property>
<property name="text">
<string>Tip</string>