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