summaryrefslogtreecommitdiffstats
path: root/src/gui/kernel/qhighdpiscaling.cpp
diff options
context:
space:
mode:
authorMorten Johan Sørvig <morten.sorvig@digia.com>2014-05-21 13:52:53 +0200
committerMorten Johan Sørvig <morten.sorvig@digia.com>2015-03-26 11:00:14 +0100
commit70f565b6e43b6ab93b01112286a40869155207c8 (patch)
treeed4c7fee3fdc5a25bce1041e9aa4616155b8eabd /src/gui/kernel/qhighdpiscaling.cpp
parent3cede847c39269374c52dcf156dc982d7a51f29c (diff)
WIP: Add platform independent high-dpi support to QtGui
Add coordinate scaling support to the QWindow/ QWindowSystemInterface layer. The scale factor can be set with the QT_HIGHDPI_SCALE_FACTOR environment variable. Setting a scale factor different than the default (1) now has the following effects: QWindow::devicePixelRatio is set accordingly, enabling the high-dpi code paths. QWindow and related classes now return geometry in device independent pixels. This includes screen, desktop and window geometry as well as event coordinates. The platform plugins continue to operate in device pixels, unaware of the scaling. Task-number: QTBUG-38858 Change-Id: I85b0d1bc682b25196f6db286e672a64f8da0ae5c
Diffstat (limited to 'src/gui/kernel/qhighdpiscaling.cpp')
-rw-r--r--src/gui/kernel/qhighdpiscaling.cpp102
1 files changed, 102 insertions, 0 deletions
diff --git a/src/gui/kernel/qhighdpiscaling.cpp b/src/gui/kernel/qhighdpiscaling.cpp
new file mode 100644
index 0000000000..6cdfd93860
--- /dev/null
+++ b/src/gui/kernel/qhighdpiscaling.cpp
@@ -0,0 +1,102 @@
+/****************************************************************************
+**
+** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the QtGui module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia. For licensing terms and
+** conditions see http://qt.digia.com/licensing. For further information
+** use the contact form at http://qt.digia.com/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Digia gives you certain additional
+** rights. These rights are described in the Digia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3.0 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU General Public License version 3.0 requirements will be
+** met: http://www.gnu.org/copyleft/gpl.html.
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "qhighdpiscaling_p.h"
+#include "qwindow_p.h" // for QWINDOWSIZE_MAX
+#include "qguiapplication.h"
+#include "qscreen.h"
+#include "private/qscreen_p.h"
+
+#include <QtCore/qdebug.h>
+
+QT_BEGIN_NAMESPACE
+
+static inline qreal initialScaleFactor()
+{
+ static const char envVar[] = "QT_HIGHDPI_SCALE_FACTOR";
+ qreal result = 1;
+ if (qEnvironmentVariableIsSet(envVar)) {
+ bool ok;
+ const qreal f = qgetenv(envVar).toDouble(&ok);
+ if (ok && f > 0)
+ result = f;
+ }
+ return result;
+}
+
+/*!
+ \class QHighDpiScaling
+ \since 5.4
+ \internal
+ \preliminary
+ \ingroup qpa
+
+ \brief Collection of utility functions for UI scaling.
+*/
+
+qreal QHighDpiScaling::m_factor = initialScaleFactor();
+bool QHighDpiScaling::m_active = !qFuzzyCompare(QHighDpiScaling::m_factor, qreal(1));
+
+void QHighDpiScaling::setFactor(qreal factor)
+{
+ if (qFuzzyCompare(factor, QHighDpiScaling::m_factor))
+ return;
+ if (!QGuiApplication::allWindows().isEmpty()) {
+ qWarning() << Q_FUNC_INFO << "QHighDpiScaling::setFactor: Should only be called when no windows exist.";
+ }
+
+ QHighDpiScaling::m_active = !qFuzzyCompare(factor, qreal(1));
+ QHighDpiScaling::m_factor = QHighDpiScaling::m_active ? factor : qreal(1);
+ Q_FOREACH (QScreen *screen, QGuiApplication::screens())
+ screen->d_func()->updateHighDpi();
+}
+
+Q_GUI_EXPORT QSize qHighDpiToDevicePixelsConstrained(const QSize &size)
+{
+ const int width = size.width();
+ const int height = size.height();
+ return QSize(width > 0 && width < QWINDOWSIZE_MAX ?
+ qHighDpiToDevicePixels(width) : width,
+ height > 0 && height < QWINDOWSIZE_MAX ?
+ qHighDpiToDevicePixels(height) : height);
+}
+
+QT_END_NAMESPACE