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
deviceprofiledialog.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
6#include "ui_deviceprofiledialog.h"
7
8#include <abstractdialoggui_p.h>
9#include <deviceprofile_p.h>
10
11#include <QtWidgets/qdialogbuttonbox.h>
12#include <QtWidgets/qboxlayout.h>
13#include <QtWidgets/qpushbutton.h>
14#include <QtWidgets/qstylefactory.h>
15#include <QtGui/qfontdatabase.h>
16#include <QtGui/qvalidator.h>
17
18#include <QtCore/qfileinfo.h>
19#include <QtCore/qfile.h>
20
22
23using namespace Qt::StringLiterals;
24
25static constexpr auto profileExtensionC = "qdp"_L1;
26
27static inline QString fileFilter()
28{
29 return qdesigner_internal::DeviceProfileDialog::tr("Device Profiles (*.%1)").arg(profileExtensionC);
30}
31
32// Populate a combo with a sequence of integers, also set them as data.
33template <class IntIterator>
34 static void populateNumericCombo(IntIterator i1, IntIterator i2, QComboBox *cb)
35{
36 QString s;
37 for ( ; i1 != i2 ; ++i1) {
38 const int n = *i1;
39 s.setNum(n);
40 cb->addItem(s, QVariant(n));
41 }
42}
43
44namespace qdesigner_internal {
45
46DeviceProfileDialog::DeviceProfileDialog(QDesignerDialogGuiInterface *dlgGui, QWidget *parent) :
47 QDialog(parent),
48 m_ui(new QT_PREPEND_NAMESPACE(Ui)::DeviceProfileDialog),
49 m_dlgGui(dlgGui)
50{
51 setModal(true);
52 m_ui->setupUi(this);
53
54 const auto standardFontSizes = QFontDatabase::standardSizes();
55 populateNumericCombo(standardFontSizes.constBegin(), standardFontSizes.constEnd(), m_ui->m_systemFontSizeCombo);
56
57 // 288pt observed on macOS.
58 const int maxPointSize = qMax(288, standardFontSizes.constLast());
59 m_ui->m_systemFontSizeCombo->setValidator(new QIntValidator(1, maxPointSize,
60 m_ui->m_systemFontSizeCombo));
61
62 // Styles
63 const QStringList styles = QStyleFactory::keys();
64 m_ui->m_styleCombo->addItem(tr("Default"), QVariant(QString()));
65 for (const auto &s : styles)
66 m_ui->m_styleCombo->addItem(s, s);
67
68 connect(m_ui->m_nameLineEdit, &QLineEdit::textChanged, this, &DeviceProfileDialog::nameChanged);
69 connect(m_ui->buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
70 connect(m_ui->buttonBox->button(QDialogButtonBox::Ok), &QAbstractButton::clicked,
71 this, &QDialog::accept);
72 // Note that Load/Save emit accepted() of the button box..
73 connect(m_ui->buttonBox->button(QDialogButtonBox::Save), &QAbstractButton::clicked,
74 this, &DeviceProfileDialog::save);
75 connect(m_ui->buttonBox->button(QDialogButtonBox::Open), &QAbstractButton::clicked,
77}
78
80{
81 delete m_ui;
82}
83
84DeviceProfile DeviceProfileDialog::deviceProfile() const
85{
86 DeviceProfile rc;
87 rc.setName(m_ui->m_nameLineEdit->text());
88 rc.setFontFamily(m_ui->m_systemFontComboBox->currentFont().family());
89 rc.setFontPointSize(m_ui->m_systemFontSizeCombo->itemData(m_ui->m_systemFontSizeCombo->currentIndex()).toInt());
90
91 int dpiX, dpiY;
92 m_ui->m_dpiChooser->getDPI(&dpiX, &dpiY);
93 rc.setDpiX(dpiX);
94 rc.setDpiY(dpiY);
95
96 rc.setStyle(m_ui->m_styleCombo->itemData(m_ui->m_styleCombo->currentIndex()).toString());
97
98 return rc;
99}
100
101void DeviceProfileDialog::setDeviceProfile(const DeviceProfile &s)
102{
103 m_ui->m_nameLineEdit->setText(s.name());
104 m_ui->m_systemFontComboBox->setCurrentFont(QFont(s.fontFamily()));
105 const int fontSizeIndex = m_ui->m_systemFontSizeCombo->findData(QVariant(s.fontPointSize()));
106 m_ui->m_systemFontSizeCombo->setCurrentIndex(fontSizeIndex != -1 ? fontSizeIndex : 0);
107 m_ui->m_dpiChooser->setDPI(s.dpiX(), s.dpiY());
108 const int styleIndex = m_ui->m_styleCombo->findData(s.style());
109 m_ui->m_styleCombo->setCurrentIndex(styleIndex != -1 ? styleIndex : 0);
110}
111
112void DeviceProfileDialog::setOkButtonEnabled(bool v)
113{
114 m_ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(v);
115}
116
117bool DeviceProfileDialog::showDialog(const QStringList &existingNames)
118{
119 m_existingNames = existingNames;
120 m_ui->m_nameLineEdit->setFocus(Qt::OtherFocusReason);
121 nameChanged(m_ui->m_nameLineEdit->text());
122 return exec() == Accepted;
123}
124
125void DeviceProfileDialog::nameChanged(const QString &name)
126{
127 const bool invalid = name.isEmpty() || m_existingNames.indexOf(name) != -1;
128 setOkButtonEnabled(!invalid);
129}
130
131void DeviceProfileDialog::save()
132{
133 QString fn = m_dlgGui->getSaveFileName(this, tr("Save Profile"), QString(), fileFilter());
134 if (fn.isEmpty())
135 return;
136 if (QFileInfo(fn).completeSuffix().isEmpty())
137 fn += u'.' + profileExtensionC;
138
139 QFile file(fn);
140 if (!file.open(QIODevice::WriteOnly|QIODevice::Text)) {
141 critical(tr("Save Profile - Error"), tr("Unable to open the file '%1' for writing: %2").arg(fn, file.errorString()));
142 return;
143 }
144 file.write(deviceProfile().toXml().toUtf8());
145}
146
148{
149 const QString fn = m_dlgGui->getOpenFileName(this, tr("Open profile"), QString(), fileFilter());
150 if (fn.isEmpty())
151 return;
152
153 QFile file(fn);
154 if (!file.open(QIODevice::ReadOnly|QIODevice::Text)) {
155 critical(tr("Open Profile - Error"), tr("Unable to open the file '%1' for reading: %2").arg(fn, file.errorString()));
156 return;
157 }
158 QString errorMessage;
159 DeviceProfile newSettings;
160 if (!newSettings.fromXml(QString::fromUtf8(file.readAll()), &errorMessage)) {
161 critical(tr("Open Profile - Error"), tr("'%1' is not a valid profile: %2").arg(fn, errorMessage));
162 return;
163 }
164 setDeviceProfile(newSettings);
165}
166
167void DeviceProfileDialog::critical(const QString &title, const QString &msg)
168{
169 m_dlgGui->message(this, QDesignerDialogGuiInterface::OtherMessage, QMessageBox::Critical, title, msg);
170}
171}
172
173QT_END_NAMESPACE
friend class QWidget
Definition qpainter.h:432
bool showDialog(const QStringList &existingNames)
void setDeviceProfile(const DeviceProfile &s)
static constexpr auto profileExtensionC
static QString fileFilter()
static void populateNumericCombo(IntIterator i1, IntIterator i2, QComboBox *cb)
Combined button and popup list for selecting options.
Auxiliary methods to store/retrieve settings.