-
Notifications
You must be signed in to change notification settings - Fork 476
/
units.cpp
135 lines (111 loc) · 3.69 KB
/
units.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
* QML Material - An application framework implementing Material Design.
*
* Copyright (C) 2016 Michael Spencer <[email protected]>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include "units.h"
#include <QGuiApplication>
#include <QQuickItem>
#if defined(Q_OS_ANDROID)
#include <QtAndroidExtras>
#endif
#define DEFAULT_DPI 72
UnitsAttached::UnitsAttached(QObject *attachee)
: QObject(attachee), m_screen(nullptr), m_window(nullptr), m_dpi(0), m_multiplier(1)
{
m_attachee = qobject_cast<QQuickItem *>(attachee);
if (m_attachee) {
if (m_attachee->window()) // It might not be assigned to a window yet
windowChanged(m_attachee->window());
} else {
QQuickWindow *window = qobject_cast<QQuickWindow *>(attachee);
if (window)
windowChanged(window);
}
if (!m_screen)
screenChanged(QGuiApplication::primaryScreen());
}
void UnitsAttached::windowChanged(QQuickWindow *window)
{
if (m_window)
disconnect(m_window, &QQuickWindow::screenChanged, this, &UnitsAttached::screenChanged);
m_window = window;
screenChanged(window ? window->screen() : nullptr);
if (window)
connect(window, &QQuickWindow::screenChanged, this, &UnitsAttached::screenChanged);
}
void UnitsAttached::screenChanged(QScreen *screen)
{
if (screen != m_screen) {
QScreen *oldScreen = m_screen;
m_screen = screen;
if (oldScreen)
oldScreen->disconnect(this);
if (oldScreen == nullptr || screen == nullptr ||
screen->physicalDotsPerInch() != oldScreen->physicalDotsPerInch() ||
screen->logicalDotsPerInch() != oldScreen->logicalDotsPerInch() ||
screen->devicePixelRatio() != oldScreen->devicePixelRatio()) {
updateDPI();
emit dpChanged();
}
}
}
int UnitsAttached::dp() const
{
#if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)
return m_multiplier;
#else
auto dp = dpi() / 160;
return dp > 0 ? dp * m_multiplier : m_multiplier;
#endif
}
int UnitsAttached::dpi() const { return m_dpi; }
qreal UnitsAttached::multiplier() const { return m_multiplier; }
void UnitsAttached::setMultiplier(qreal multiplier)
{
if (m_multiplier != multiplier) {
m_multiplier = multiplier;
emit multiplierChanged();
}
}
void UnitsAttached::updateDPI()
{
if (m_screen == nullptr) {
m_dpi = DEFAULT_DPI;
return;
}
#if defined(Q_OS_IOS)
// iOS integration of scaling (retina, non-retina, 4K) does itself.
m_dpi = m_screen->physicalDotsPerInch();
#elif defined(Q_OS_ANDROID)
// https://bugreports.qt-project.org/browse/QTBUG-35701
// recalculate dpi for Android
QAndroidJniEnvironment env;
QAndroidJniObject activity = QtAndroid::androidActivity();
QAndroidJniObject resources =
activity.callObjectMethod("getResources", "()Landroid/content/res/Resources;");
if (env->ExceptionCheck()) {
env->ExceptionDescribe();
env->ExceptionClear();
m_dpi = DEFAULT_DPI;
return;
}
QAndroidJniObject displayMetrics =
resources.callObjectMethod("getDisplayMetrics", "()Landroid/util/DisplayMetrics;");
if (env->ExceptionCheck()) {
env->ExceptionDescribe();
env->ExceptionClear();
m_dpi = DEFAULT_DPI;
return;
}
m_dpi = displayMetrics.getField<int>("densityDpi");
m_multiplier = displayMetrics.getField<float>("density");
#else
// standard dpi
m_dpi = m_screen->logicalDotsPerInch() * m_screen->devicePixelRatio();
#endif
}