1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the Qt Designer of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
21 | ** included in the packaging of this file. Please review the following |
22 | ** information to ensure the GNU General Public License requirements will |
23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
24 | ** |
25 | ** $QT_END_LICENSE$ |
26 | ** |
27 | ****************************************************************************/ |
28 | |
29 | #include "dpi_chooser.h" |
30 | |
31 | #include <deviceprofile_p.h> |
32 | |
33 | #include <QtWidgets/qcombobox.h> |
34 | #include <QtWidgets/qspinbox.h> |
35 | #include <QtWidgets/qlabel.h> |
36 | #include <QtWidgets/qboxlayout.h> |
37 | #include <QtWidgets/qpushbutton.h> |
38 | #include <QtWidgets/qcheckbox.h> |
39 | |
40 | QT_BEGIN_NAMESPACE |
41 | |
42 | enum { minDPI = 50, maxDPI = 400 }; |
43 | |
44 | namespace qdesigner_internal { |
45 | |
46 | // Entry struct for predefined values |
47 | struct DPI_Entry { |
48 | int dpiX; |
49 | int dpiY; |
50 | const char *description; |
51 | }; |
52 | |
53 | const struct DPI_Entry dpiEntries[] = { |
54 | //: Embedded device standard screen resolution |
55 | { .dpiX: 96, .dpiY: 96, QT_TRANSLATE_NOOP("DPI_Chooser" , "Standard (96 x 96)" ) }, |
56 | //: Embedded device screen resolution |
57 | { .dpiX: 179, .dpiY: 185, QT_TRANSLATE_NOOP("DPI_Chooser" , "Greenphone (179 x 185)" ) }, |
58 | //: Embedded device high definition screen resolution |
59 | { .dpiX: 192, .dpiY: 192, QT_TRANSLATE_NOOP("DPI_Chooser" , "High (192 x 192)" ) } |
60 | }; |
61 | |
62 | } // namespace qdesigner_internal |
63 | |
64 | QT_END_NAMESPACE |
65 | |
66 | Q_DECLARE_METATYPE(const struct qdesigner_internal::DPI_Entry*); |
67 | |
68 | QT_BEGIN_NAMESPACE |
69 | |
70 | namespace qdesigner_internal { |
71 | |
72 | // ------------- DPI_Chooser |
73 | |
74 | DPI_Chooser::DPI_Chooser(QWidget *parent) : |
75 | QWidget(parent), |
76 | m_systemEntry(new DPI_Entry), |
77 | m_predefinedCombo(new QComboBox), |
78 | m_dpiXSpinBox(new QSpinBox), |
79 | m_dpiYSpinBox(new QSpinBox) |
80 | { |
81 | // Predefined settings: System |
82 | DeviceProfile::systemResolution(dpiX: &(m_systemEntry->dpiX), dpiY: &(m_systemEntry->dpiY)); |
83 | m_systemEntry->description = nullptr; |
84 | const struct DPI_Entry *systemEntry = m_systemEntry; |
85 | //: System resolution |
86 | m_predefinedCombo->addItem(atext: tr(s: "System (%1 x %2)" ).arg(a: m_systemEntry->dpiX).arg(a: m_systemEntry->dpiY), auserData: QVariant::fromValue(value: systemEntry)); |
87 | // Devices. Exclude the system values as not to duplicate the entries |
88 | for (const DPI_Entry &e : dpiEntries) { |
89 | if (e.dpiX != m_systemEntry->dpiX || e.dpiY != m_systemEntry->dpiY) |
90 | m_predefinedCombo->addItem(atext: tr(s: e.description), auserData: QVariant::fromValue(value: &e)); |
91 | } |
92 | m_predefinedCombo->addItem(atext: tr(s: "User defined" )); |
93 | |
94 | setFocusProxy(m_predefinedCombo); |
95 | m_predefinedCombo->setEditable(false); |
96 | m_predefinedCombo->setCurrentIndex(0); |
97 | connect(sender: m_predefinedCombo, signal: QOverload<int>::of(ptr: &QComboBox::currentIndexChanged), |
98 | receiver: this, slot: &DPI_Chooser::syncSpinBoxes); |
99 | // top row with predefined settings |
100 | QVBoxLayout *vBoxLayout = new QVBoxLayout; |
101 | vBoxLayout->setContentsMargins(QMargins()); |
102 | vBoxLayout->addWidget(m_predefinedCombo); |
103 | // Spin box row |
104 | QHBoxLayout *hBoxLayout = new QHBoxLayout; |
105 | hBoxLayout->setContentsMargins(QMargins()); |
106 | |
107 | m_dpiXSpinBox->setMinimum(minDPI); |
108 | m_dpiXSpinBox->setMaximum(maxDPI); |
109 | hBoxLayout->addWidget(m_dpiXSpinBox); |
110 | //: DPI X/Y separator |
111 | hBoxLayout->addWidget(new QLabel(tr(s: " x " ))); |
112 | |
113 | m_dpiYSpinBox->setMinimum(minDPI); |
114 | m_dpiYSpinBox->setMaximum(maxDPI); |
115 | hBoxLayout->addWidget(m_dpiYSpinBox); |
116 | |
117 | hBoxLayout->addStretch(); |
118 | vBoxLayout->addLayout(layout: hBoxLayout); |
119 | setLayout(vBoxLayout); |
120 | |
121 | syncSpinBoxes(); |
122 | } |
123 | |
124 | DPI_Chooser::~DPI_Chooser() |
125 | { |
126 | delete m_systemEntry; |
127 | } |
128 | |
129 | void DPI_Chooser::getDPI(int *dpiX, int *dpiY) const |
130 | { |
131 | *dpiX = m_dpiXSpinBox->value(); |
132 | *dpiY = m_dpiYSpinBox->value(); |
133 | } |
134 | |
135 | void DPI_Chooser::setDPI(int dpiX, int dpiY) |
136 | { |
137 | // Default to system if it is something weird |
138 | const bool valid = dpiX >= minDPI && dpiX <= maxDPI && dpiY >= minDPI && dpiY <= maxDPI; |
139 | if (!valid) { |
140 | m_predefinedCombo->setCurrentIndex(0); |
141 | return; |
142 | } |
143 | // Try to find the values among the predefined settings |
144 | const int count = m_predefinedCombo->count(); |
145 | int predefinedIndex = -1; |
146 | for (int i = 0; i < count; i++) { |
147 | const QVariant data = m_predefinedCombo->itemData(index: i); |
148 | if (data.type() != QVariant::Invalid) { |
149 | const struct DPI_Entry *entry = qvariant_cast<const struct DPI_Entry *>(v: data); |
150 | if (entry->dpiX == dpiX && entry->dpiY == dpiY) { |
151 | predefinedIndex = i; |
152 | break; |
153 | } |
154 | } |
155 | } |
156 | if (predefinedIndex != -1) { |
157 | m_predefinedCombo->setCurrentIndex(predefinedIndex); // triggers syncSpinBoxes() |
158 | } else { |
159 | setUserDefinedValues(dpiX, dpiY); |
160 | } |
161 | } |
162 | |
163 | void DPI_Chooser::setUserDefinedValues(int dpiX, int dpiY) |
164 | { |
165 | const bool blocked = m_predefinedCombo->blockSignals(b: true); |
166 | m_predefinedCombo->setCurrentIndex(m_predefinedCombo->count() - 1); |
167 | m_predefinedCombo->blockSignals(b: blocked); |
168 | |
169 | m_dpiXSpinBox->setEnabled(true); |
170 | m_dpiYSpinBox->setEnabled(true); |
171 | m_dpiXSpinBox->setValue(dpiX); |
172 | m_dpiYSpinBox->setValue(dpiY); |
173 | } |
174 | |
175 | void DPI_Chooser::syncSpinBoxes() |
176 | { |
177 | const int predefIdx = m_predefinedCombo->currentIndex(); |
178 | const QVariant data = m_predefinedCombo->itemData(index: predefIdx); |
179 | |
180 | // Predefined mode in which spin boxes are disabled or user defined? |
181 | const bool userSetting = data.type() == QVariant::Invalid; |
182 | m_dpiXSpinBox->setEnabled(userSetting); |
183 | m_dpiYSpinBox->setEnabled(userSetting); |
184 | |
185 | if (!userSetting) { |
186 | const struct DPI_Entry *entry = qvariant_cast<const struct DPI_Entry *>(v: data); |
187 | m_dpiXSpinBox->setValue(entry->dpiX); |
188 | m_dpiYSpinBox->setValue(entry->dpiY); |
189 | } |
190 | } |
191 | } |
192 | |
193 | QT_END_NAMESPACE |
194 | |