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