Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
dpi_chooser.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3// Qt-Security score:significant reason:default
4
5#include "dpi_chooser.h"
6
7#include <deviceprofile_p.h>
8
9#include <QtWidgets/qcombobox.h>
10#include <QtWidgets/qspinbox.h>
11#include <QtWidgets/qlabel.h>
12#include <QtWidgets/qboxlayout.h>
13#include <QtWidgets/qpushbutton.h>
14#include <QtWidgets/qcheckbox.h>
15
17
18enum { minDPI = 50, maxDPI = 400 };
19
20namespace qdesigner_internal {
21
22// Entry struct for predefined values
23struct DPI_Entry {
24 int dpiX;
25 int dpiY;
26 const char *description;
27};
28
29const struct DPI_Entry dpiEntries[] = {
30 //: Embedded device standard screen resolution
31 { 96, 96, QT_TRANSLATE_NOOP("DPI_Chooser", "Standard (96 x 96)") },
32 //: Embedded device screen resolution
33 { 179, 185, QT_TRANSLATE_NOOP("DPI_Chooser", "Greenphone (179 x 185)") },
34 //: Embedded device high definition screen resolution
35 { 192, 192, QT_TRANSLATE_NOOP("DPI_Chooser", "High (192 x 192)") }
36};
37
38} // namespace qdesigner_internal
39
41
43
44QT_BEGIN_NAMESPACE
45
46namespace qdesigner_internal {
47
48// ------------- DPI_Chooser
49
50DPI_Chooser::DPI_Chooser(QWidget *parent) :
51 QWidget(parent),
52 m_systemEntry(new DPI_Entry),
53 m_predefinedCombo(new QComboBox),
54 m_dpiXSpinBox(new QSpinBox),
55 m_dpiYSpinBox(new QSpinBox)
56{
57 // Predefined settings: System
58 DeviceProfile::systemResolution(&(m_systemEntry->dpiX), &(m_systemEntry->dpiY));
59 m_systemEntry->description = nullptr;
60 const struct DPI_Entry *systemEntry = m_systemEntry;
61 //: System resolution
62 m_predefinedCombo->addItem(tr("System (%1 x %2)").arg(m_systemEntry->dpiX).arg(m_systemEntry->dpiY), QVariant::fromValue(systemEntry));
63 // Devices. Exclude the system values as not to duplicate the entries
64 for (const DPI_Entry &e : dpiEntries) {
65 if (e.dpiX != m_systemEntry->dpiX || e.dpiY != m_systemEntry->dpiY)
66 m_predefinedCombo->addItem(tr(e.description), QVariant::fromValue(&e));
67 }
68 m_predefinedCombo->addItem(tr("User defined"));
69
70 setFocusProxy(m_predefinedCombo);
71 m_predefinedCombo->setEditable(false);
72 m_predefinedCombo->setCurrentIndex(0);
73 connect(m_predefinedCombo, &QComboBox::currentIndexChanged,
74 this, &DPI_Chooser::syncSpinBoxes);
75 // top row with predefined settings
76 QVBoxLayout *vBoxLayout = new QVBoxLayout;
77 vBoxLayout->setContentsMargins(QMargins());
78 vBoxLayout->addWidget(m_predefinedCombo);
79 // Spin box row
80 QHBoxLayout *hBoxLayout = new QHBoxLayout;
81 hBoxLayout->setContentsMargins(QMargins());
82
83 m_dpiXSpinBox->setMinimum(minDPI);
84 m_dpiXSpinBox->setMaximum(maxDPI);
85 hBoxLayout->addWidget(m_dpiXSpinBox);
86 //: DPI X/Y separator
87 hBoxLayout->addWidget(new QLabel(tr(" x ")));
88
89 m_dpiYSpinBox->setMinimum(minDPI);
90 m_dpiYSpinBox->setMaximum(maxDPI);
91 hBoxLayout->addWidget(m_dpiYSpinBox);
92
93 hBoxLayout->addStretch();
94 vBoxLayout->addLayout(hBoxLayout);
95 setLayout(vBoxLayout);
96
97 syncSpinBoxes();
98}
99
101{
102 delete m_systemEntry;
103}
104
105void DPI_Chooser::getDPI(int *dpiX, int *dpiY) const
106{
107 *dpiX = m_dpiXSpinBox->value();
108 *dpiY = m_dpiYSpinBox->value();
109}
110
111void DPI_Chooser::setDPI(int dpiX, int dpiY)
112{
113 // Default to system if it is something weird
114 const bool valid = dpiX >= minDPI && dpiX <= maxDPI && dpiY >= minDPI && dpiY <= maxDPI;
115 if (!valid) {
116 m_predefinedCombo->setCurrentIndex(0);
117 return;
118 }
119 // Try to find the values among the predefined settings
120 const int count = m_predefinedCombo->count();
121 int predefinedIndex = -1;
122 for (int i = 0; i < count; i++) {
123 const QVariant data = m_predefinedCombo->itemData(i);
124 if (data.metaType().id() != QMetaType::UnknownType) {
125 const struct DPI_Entry *entry = qvariant_cast<const struct DPI_Entry *>(data);
126 if (entry->dpiX == dpiX && entry->dpiY == dpiY) {
127 predefinedIndex = i;
128 break;
129 }
130 }
131 }
132 if (predefinedIndex != -1) {
133 m_predefinedCombo->setCurrentIndex(predefinedIndex); // triggers syncSpinBoxes()
134 } else {
135 setUserDefinedValues(dpiX, dpiY);
136 }
137}
138
139void DPI_Chooser::setUserDefinedValues(int dpiX, int dpiY)
140{
141 const bool blocked = m_predefinedCombo->blockSignals(true);
142 m_predefinedCombo->setCurrentIndex(m_predefinedCombo->count() - 1);
143 m_predefinedCombo->blockSignals(blocked);
144
145 m_dpiXSpinBox->setEnabled(true);
146 m_dpiYSpinBox->setEnabled(true);
147 m_dpiXSpinBox->setValue(dpiX);
148 m_dpiYSpinBox->setValue(dpiY);
149}
150
151void DPI_Chooser::syncSpinBoxes()
152{
153 const int predefIdx = m_predefinedCombo->currentIndex();
154 const QVariant data = m_predefinedCombo->itemData(predefIdx);
155
156 // Predefined mode in which spin boxes are disabled or user defined?
157 const bool userSetting = data.metaType().id() == QMetaType::UnknownType;
158 m_dpiXSpinBox->setEnabled(userSetting);
159 m_dpiYSpinBox->setEnabled(userSetting);
160
161 if (!userSetting) {
162 const struct DPI_Entry *entry = qvariant_cast<const struct DPI_Entry *>(data);
163 m_dpiXSpinBox->setValue(entry->dpiX);
164 m_dpiYSpinBox->setValue(entry->dpiY);
165 }
166}
167}
168
169QT_END_NAMESPACE
friend class QWidget
Definition qpainter.h:432
void getDPI(int *dpiX, int *dpiY) const
void setDPI(int dpiX, int dpiY)
@ minDPI
@ maxDPI
QT_END_NAMESPACE Q_DECLARE_METATYPE(const struct qdesigner_internal::DPI_Entry *)
Combined button and popup list for selecting options.
Auxiliary methods to store/retrieve settings.
const struct DPI_Entry dpiEntries[]
int dpiY
const char * description
int dpiX