Line data Source code
1 : /*
2 : * SPDX-License-Identifier: GPL-3.0-only
3 : * MuseScore-CLA-applies
4 : *
5 : * MuseScore Studio
6 : * Music Composition & Notation
7 : *
8 : * Copyright (C) 2026 MuseScore Limited and others
9 : *
10 : * This program is free software: you can redistribute it and/or modify
11 : * it under the terms of the GNU General Public License version 3 as
12 : * published by the Free Software Foundation.
13 : *
14 : * This program is distributed in the hope that it will be useful,
15 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : * GNU General Public License for more details.
18 : *
19 : * You should have received a copy of the GNU General Public License
20 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 : */
22 :
23 : #include "uicontextconfiguration.h"
24 :
25 : #include <QScreen>
26 : #include <QApplication>
27 : #include <QOperatingSystemVersion>
28 :
29 : using namespace muse::ui;
30 :
31 : constexpr double DEFAULT_DPI = 96;
32 :
33 0 : QScreen* UiContextConfiguration::screen() const
34 : {
35 0 : if (!mainWindow()) {
36 0 : return QApplication::primaryScreen();
37 : }
38 0 : return mainWindow()->screen();
39 : }
40 :
41 0 : double UiContextConfiguration::guiScaling() const
42 : {
43 0 : const QScreen* s = screen();
44 0 : return s ? s->devicePixelRatio() : 1;
45 : }
46 :
47 0 : double UiContextConfiguration::physicalDpi() const
48 : {
49 0 : if (configuration()->customPhysicalDotsPerInch()) {
50 0 : return configuration()->customPhysicalDotsPerInch().value();
51 : }
52 :
53 0 : const QScreen* screen = this->screen();
54 0 : if (!screen) {
55 0 : return DEFAULT_DPI;
56 : }
57 :
58 0 : auto physicalSize = screen->physicalSize();
59 : // Work around xrandr reporting a 1x1mm size if
60 : // the screen doesn't have a valid physical size
61 0 : if (physicalSize.height() <= 1 && physicalSize.width() <= 1) {
62 0 : return DEFAULT_DPI;
63 : }
64 :
65 : #ifdef Q_OS_WIN
66 : //! NOTE: copied from MU3, `MuseScore::MuseScore()`
67 : if (QOperatingSystemVersion::current() <= QOperatingSystemVersion(QOperatingSystemVersion::Windows, 7)) {
68 : return screen->logicalDotsPerInch() * screen->devicePixelRatio();
69 : }
70 : #endif
71 0 : return screen->physicalDotsPerInch();
72 : }
73 :
74 0 : double UiContextConfiguration::logicalDpi() const
75 : {
76 0 : const QScreen* screen = this->screen();
77 0 : if (!screen) {
78 0 : return DEFAULT_DPI;
79 : }
80 :
81 0 : return screen->logicalDotsPerInch();
82 : }
|