Debian Codegraph @

src/common/platform/HdpiScaler.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2020 Damir Porobic <[email protected]>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 #include "HdpiScaler.h"
21 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
22 #include <QDesktopWidget>
23 #endif
24
25 QRect HdpiScaler::unscale(const QRect &rect) const
26 {
27 auto factor = scaleFactor();
28 return {
29 static_cast<int>(rect.x() / factor),
30 static_cast<int>(rect.y() / factor),
31 static_cast<int>(rect.width() / factor),
32 static_cast<int>(rect.height() / factor)
33 };
34 }
35
36 QRect HdpiScaler::scale(const QRect &rect) const
37 {
38 auto factor = scaleFactor();
39 return {
40 static_cast<int>(rect.x() * factor),
41 static_cast<int>(rect.y() * factor),
42 static_cast<int>(rect.width() * factor),
43 static_cast<int>(rect.height() * factor)
44 };
45 }
46
47 qreal HdpiScaler::scaleFactor() const
48 {
49 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
50 return QApplication::primaryScreen()->devicePixelRatio();
51 #else
52 auto desktopWidget = QApplication::desktop();
53
54 #if defined(__APPLE__)
55 auto myWindow = QGuiApplication::topLevelWindows().first();
56
57 return myWindow->devicePixelRatio();
58 #endif
59
60 #if defined(UNIX_X11) || defined(_WIN32)
61 return desktopWidget->devicePixelRatioF();
62 #endif
63 #endif
64 }