QStyleHelper: Base DPI-calculation on QScreen.

- Use qt_defaultDpiX() to obtain the resolution, which
  obtains it from QScreen. This implies that for X11,
  which previously used a hardcoded default of 96 DPI,
  the real resolution will be used (typically 75).

- Since many tests (layouts, graphicsview) contain
  test data for 96 DPI, add an attribute to
  QCoreApplication making it possible to set the
  resolution to 96 DPI for testing.

Change-Id: I77c8233a96b0d75de07406f58d48886a89c3de06
Reviewed-by: Samuel Rødal <samuel.rodal@nokia.com>
This commit is contained in:
Friedemann Kleint 2012-01-11 11:49:35 +01:00 committed by Qt by Nokia
parent f65a10b733
commit 1fbe8242ec
4 changed files with 22 additions and 40 deletions

View File

@ -479,8 +479,7 @@ public:
AA_MacPluginApplication = 5,
AA_DontUseNativeMenuBar = 6,
AA_MacDontSwapCtrlAndMeta = 7,
AA_S60DontConstructApplicationPanes = 8,
AA_S60DisablePartialScreenInputMode = 9,
AA_Use96Dpi = 8,
AA_X11InitThreads = 10,
// Add new attributes before this line

View File

@ -138,36 +138,32 @@ extern bool qt_is_gui_used;
Q_GUI_EXPORT int qt_defaultDpiX()
{
if (qApp->testAttribute(Qt::AA_Use96Dpi))
return 96;
if (!qt_is_gui_used)
return 75;
int dpi;
QScreen *screen = QGuiApplication::primaryScreen();
if (screen) {
dpi = qRound(screen->logicalDotsPerInchX());
} else {
//PI has not been initialised, or it is being initialised. Give a default dpi
dpi = 100;
}
if (const QScreen *screen = QGuiApplication::primaryScreen())
return qRound(screen->logicalDotsPerInchX());
return dpi;
//PI has not been initialised, or it is being initialised. Give a default dpi
return 100;
}
Q_GUI_EXPORT int qt_defaultDpiY()
{
if (qApp->testAttribute(Qt::AA_Use96Dpi))
return 96;
if (!qt_is_gui_used)
return 75;
int dpi;
QScreen *screen = QGuiApplication::primaryScreen();
if (screen) {
dpi = qRound(screen->logicalDotsPerInchY());
} else {
//PI has not been initialised, or it is being initialised. Give a default dpi
dpi = 100;
}
if (const QScreen *screen = QGuiApplication::primaryScreen())
return qRound(screen->logicalDotsPerInchY());
return dpi;
//PI has not been initialised, or it is being initialised. Give a default dpi
return 100;
}
Q_GUI_EXPORT int qt_defaultDpi()

View File

@ -254,6 +254,7 @@ int main(int argc, char *argv[]) \
int main(int argc, char *argv[]) \
{ \
QApplication app(argc, argv); \
app.setAttribute(Qt::AA_Use96Dpi, true); \
QTEST_DISABLE_KEYPAD_NAVIGATION \
TestObject tc; \
return QTest::qExec(&tc, argc, argv); \
@ -267,6 +268,7 @@ int main(int argc, char *argv[]) \
int main(int argc, char *argv[]) \
{ \
QGuiApplication app(argc, argv); \
app.setAttribute(Qt::AA_Use96Dpi, true); \
TestObject tc; \
return QTest::qExec(&tc, argc, argv); \
}
@ -277,6 +279,7 @@ int main(int argc, char *argv[]) \
int main(int argc, char *argv[]) \
{ \
QCoreApplication app(argc, argv); \
app.setAttribute(Qt::AA_Use96Dpi, true); \
TestObject tc; \
return QTest::qExec(&tc, argc, argv); \
}
@ -287,6 +290,7 @@ int main(int argc, char *argv[]) \
int main(int argc, char *argv[]) \
{ \
QCoreApplication app(argc, argv); \
app.setAttribute(Qt::AA_Use96Dpi, true); \
TestObject tc; \
return QTest::qExec(&tc, argc, argv); \
}

View File

@ -46,17 +46,13 @@
#include <private/qstyle_p.h>
#include <qmath.h>
#if defined(Q_WS_WIN)
#include "qt_windows.h"
#elif defined(Q_WS_MAC)
#include <private/qt_cocoa_helpers_mac_p.h>
#endif
#include "qstylehelper_p.h"
#include <qstringbuilder.h>
QT_BEGIN_NAMESPACE
Q_GUI_EXPORT int qt_defaultDpiX();
namespace QStyleHelper {
QString uniqueName(const QString &key, const QStyleOption *option, const QSize &size)
@ -81,20 +77,7 @@ QString uniqueName(const QString &key, const QStyleOption *option, const QSize &
qreal dpiScaled(qreal value)
{
static qreal scale = -1;
if (scale < 0) {
scale = 1.0;
#if defined(Q_WS_WIN)
{
HDC hdcScreen = GetDC(0);
int dpi = GetDeviceCaps(hdcScreen, LOGPIXELSX);
ReleaseDC(0, hdcScreen);
scale = dpi/96.0;
}
#elif defined(Q_WS_MAC)
scale = qt_mac_get_scalefactor();
#endif
}
static const qreal scale = qreal(qt_defaultDpiX()) / 96.0;
return value * scale;
}