Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
Loading...
Searching...
No Matches
embeddedoptionspage.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
"embeddedoptionspage.h"
6
#
include
"deviceprofiledialog.h"
7
#
include
"widgetfactory_p.h"
8
#
include
"formwindowmanager.h"
9
10
#
include
<
deviceprofile_p
.
h
>
11
#
include
<
iconloader_p
.
h
>
12
#
include
<
shared_settings_p
.
h
>
13
#
include
<
abstractdialoggui_p
.
h
>
14
#
include
<
formwindowbase_p
.
h
>
15
16
17
// SDK
18
#
include
<
QtDesigner
/
abstractformeditor
.
h
>
19
#
include
<
QtDesigner
/
abstractformwindowmanager
.
h
>
20
21
#
include
<
QtWidgets
/
qlabel
.
h
>
22
#
include
<
QtWidgets
/
qboxlayout
.
h
>
23
#
include
<
QtWidgets
/
qapplication
.
h
>
24
#
include
<
QtWidgets
/
qcombobox
.
h
>
25
#
include
<
QtWidgets
/
qtoolbutton
.
h
>
26
#
include
<
QtWidgets
/
qmessagebox
.
h
>
27
#
include
<
QtWidgets
/
qlabel
.
h
>
28
#
include
<
QtWidgets
/
qgroupbox
.
h
>
29
30
#
include
<
QtCore
/
qset
.
h
>
31
#
include
<
QtCore
/
qlist
.
h
>
32
33
#
include
<
algorithm
>
34
35
QT_BEGIN_NAMESPACE
36
37
using
namespace
Qt::StringLiterals;
38
39
namespace
qdesigner_internal
{
40
41
using
DeviceProfileList
=
QList
<
DeviceProfile
>;
42
43
enum
{
profileComboIndexOffset
= 1 };
44
45
// Sort by name. Used by template, do not make it static!
46
bool
deviceProfileLessThan
(
const
DeviceProfile &d1,
const
DeviceProfile &d2)
47
{
48
return
d1.name().toLower() < d2.name().toLower();
49
}
50
51
static
bool
ask
(
QWidget
*parent,
52
QDesignerDialogGuiInterface *dlgui,
53
const
QString &title,
54
const
QString &what)
55
{
56
return
dlgui->message(parent, QDesignerDialogGuiInterface::OtherMessage,
57
QMessageBox::Question, title, what,
58
QMessageBox::Yes|QMessageBox::No, QMessageBox::No) == QMessageBox::Yes;
59
}
60
61
// ------------ EmbeddedOptionsControlPrivate
62
class
EmbeddedOptionsControlPrivate
{
63
Q_DISABLE_COPY_MOVE
(
EmbeddedOptionsControlPrivate
)
64
public
:
65
EmbeddedOptionsControlPrivate
(
QDesignerFormEditorInterface
*
core
);
66
void
init
(
EmbeddedOptionsControl
*q);
67
68
bool
isDirty
()
const
{
return
m_dirty; }
69
70
void
loadSettings
();
71
void
saveSettings
();
72
void
slotAdd
();
73
void
slotEdit
();
74
void
slotDelete
();
75
void
slotProfileIndexChanged
(
int
);
76
77
private
:
78
QStringList existingProfileNames()
const
;
79
void
sortAndPopulateProfileCombo();
80
void
updateState();
81
void
updateDescriptionLabel();
82
83
QDesignerFormEditorInterface *m_core;
84
QComboBox *m_profileCombo;
85
QToolButton *m_addButton;
86
QToolButton *m_editButton;
87
QToolButton *m_deleteButton;
88
QLabel *m_descriptionLabel;
89
90
DeviceProfileList m_sortedProfiles;
91
EmbeddedOptionsControl
*m_q =
nullptr
;
92
QSet<QString> m_usedProfiles;
93
bool
m_dirty =
false
;
94
};
95
96
EmbeddedOptionsControlPrivate
::EmbeddedOptionsControlPrivate(QDesignerFormEditorInterface *core) :
97
m_core
(
core
),
98
m_profileCombo
(
new
QComboBox
),
99
m_addButton
(
new
QToolButton
),
100
m_editButton
(
new
QToolButton
),
101
m_deleteButton
(
new
QToolButton
),
102
m_descriptionLabel
(
new
QLabel
)
103
{
104
m_descriptionLabel
->
setMinimumHeight
(80);
105
// Determine used profiles to lock them
106
const
QDesignerFormWindowManagerInterface
*
fwm
=
core
->
formWindowManager
();
107
if
(
const
int
fwCount
=
fwm
->
formWindowCount
()) {
108
for
(
int
i
= 0;
i
<
fwCount
;
i
++)
109
if
(
const
FormWindowBase
*
fwb
=
qobject_cast
<
const
FormWindowBase
*>(
fwm
->
formWindow
(
i
))) {
110
const
QString
deviceProfileName
=
fwb
->
deviceProfileName
();
111
if
(!
deviceProfileName
.
isEmpty
())
112
m_usedProfiles
.
insert
(
deviceProfileName
);
113
}
114
}
115
}
116
117
void
EmbeddedOptionsControlPrivate
::
init
(
EmbeddedOptionsControl
*
q
)
118
{
119
m_q
=
q
;
120
QVBoxLayout
*
vLayout
=
new
QVBoxLayout
;
121
QHBoxLayout
*
hLayout
=
new
QHBoxLayout
;
122
m_profileCombo
->
setMinimumWidth
(200);
123
m_profileCombo
->
setEditable
(
false
);
124
hLayout
->
addWidget
(
m_profileCombo
);
125
m_profileCombo
->
addItem
(
EmbeddedOptionsControl
::
tr
(
"None"
));
126
EmbeddedOptionsControl
::
connect
(
m_profileCombo
, &
QComboBox
::
currentIndexChanged
,
127
m_q
, &
EmbeddedOptionsControl
::
slotProfileIndexChanged
);
128
129
m_addButton
->
setIcon
(
createIconSet
(
"plus.png"_L1
));
130
m_addButton
->
setToolTip
(
EmbeddedOptionsControl
::
tr
(
"Add a profile"
));
131
EmbeddedOptionsControl
::
connect
(
m_addButton
, &
QAbstractButton
::
clicked
,
132
m_q
, &
EmbeddedOptionsControl
::
slotAdd
);
133
hLayout
->
addWidget
(
m_addButton
);
134
135
EmbeddedOptionsControl
::
connect
(
m_editButton
, &
QAbstractButton
::
clicked
,
136
m_q
, &
EmbeddedOptionsControl
::
slotEdit
);
137
m_editButton
->
setIcon
(
createIconSet
(
"edit.png"_L1
));
138
m_editButton
->
setToolTip
(
EmbeddedOptionsControl
::
tr
(
"Edit the selected profile"
));
139
hLayout
->
addWidget
(
m_editButton
);
140
141
m_deleteButton
->
setIcon
(
createIconSet
(
"minus.png"_L1
));
142
m_deleteButton
->
setToolTip
(
EmbeddedOptionsControl
::
tr
(
"Delete the selected profile"
));
143
EmbeddedOptionsControl
::
connect
(
m_deleteButton
, &
QAbstractButton
::
clicked
,
144
m_q
, &
EmbeddedOptionsControl
::
slotDelete
);
145
hLayout
->
addWidget
(
m_deleteButton
);
146
147
hLayout
->
addStretch
();
148
vLayout
->
addLayout
(
hLayout
);
149
vLayout
->
addWidget
(
m_descriptionLabel
);
150
m_q
->
setLayout
(
vLayout
);
151
}
152
153
QStringList
EmbeddedOptionsControlPrivate
::
existingProfileNames
()
const
154
{
155
QStringList
rc
;
156
for
(
const
auto
&
dp
:
m_sortedProfiles
)
157
rc
.
append
(
dp
.
name
());
158
return
rc
;
159
}
160
161
void
EmbeddedOptionsControlPrivate
::
slotAdd
()
162
{
163
DeviceProfileDialog
dlg
(
m_core
->
dialogGui
(),
m_q
);
164
dlg
.
setWindowTitle
(
EmbeddedOptionsControl
::
tr
(
"Add Profile"
));
165
// Create a new profile with a new, unique name
166
DeviceProfile
settings
;
167
settings
.
fromSystem
();
168
dlg
.
setDeviceProfile
(
settings
);
169
170
const
QStringList
names
=
existingProfileNames
();
171
const
QString
newNamePrefix
=
EmbeddedOptionsControl
::
tr
(
"New profile"
);
172
QString
newName
=
newNamePrefix
;
173
for
(
int
i
= 2;
names
.
contains
(
newName
);
i
++) {
174
newName
=
newNamePrefix
;
175
newName
+=
QString
::
number
(
i
);
176
}
177
178
settings
.
setName
(
newName
);
179
dlg
.
setDeviceProfile
(
settings
);
180
if
(
dlg
.
showDialog
(
names
)) {
181
const
DeviceProfile
newProfile
=
dlg
.
deviceProfile
();
182
m_sortedProfiles
.
push_back
(
newProfile
);
183
// Maintain sorted order
184
sortAndPopulateProfileCombo
();
185
const
int
index
=
m_profileCombo
->
findText
(
newProfile
.
name
());
186
m_profileCombo
->
setCurrentIndex
(
index
);
187
m_dirty
=
true
;
188
}
189
}
190
191
void
EmbeddedOptionsControlPrivate
::
slotEdit
()
192
{
193
const
int
index
=
m_profileCombo
->
currentIndex
() -
profileComboIndexOffset
;
194
if
(
index
< 0)
195
return
;
196
197
// Edit the profile, compile a list of existing names
198
// excluding current one. re-insert if changed,
199
// re-sort if name changed.
200
const
DeviceProfile
oldProfile
=
m_sortedProfiles
.
at
(
index
);
201
const
QString
oldName
=
oldProfile
.
name
();
202
QStringList
names
=
existingProfileNames
();
203
names
.
removeAll
(
oldName
);
204
205
DeviceProfileDialog
dlg
(
m_core
->
dialogGui
(),
m_q
);
206
dlg
.
setWindowTitle
(
EmbeddedOptionsControl
::
tr
(
"Edit Profile"
));
207
dlg
.
setDeviceProfile
(
oldProfile
);
208
if
(
dlg
.
showDialog
(
names
)) {
209
const
DeviceProfile
newProfile
=
dlg
.
deviceProfile
();
210
if
(
newProfile
!=
oldProfile
) {
211
m_dirty
=
true
;
212
m_sortedProfiles
[
index
] =
newProfile
;
213
if
(
newProfile
.
name
() !=
oldName
) {
214
sortAndPopulateProfileCombo
();
215
const
int
index
=
m_profileCombo
->
findText
(
newProfile
.
name
());
216
m_profileCombo
->
setCurrentIndex
(
index
);
217
}
else
{
218
updateDescriptionLabel
();
219
}
220
221
}
222
}
223
}
224
225
void
EmbeddedOptionsControlPrivate
::
slotDelete
()
226
{
227
const
int
index
=
m_profileCombo
->
currentIndex
() -
profileComboIndexOffset
;
228
if
(
index
< 0)
229
return
;
230
const
QString
name
=
m_sortedProfiles
.
at
(
index
).
name
();
231
if
(
ask
(
m_q
,
m_core
->
dialogGui
(),
232
EmbeddedOptionsControl
::
tr
(
"Delete Profile"
),
233
EmbeddedOptionsControl
::
tr
(
"Would you like to delete the profile '%1'?"
).
arg
(
name
))) {
234
m_profileCombo
->
setCurrentIndex
(0);
235
m_sortedProfiles
.
removeAt
(
index
);
236
m_profileCombo
->
removeItem
(
index
+
profileComboIndexOffset
);
237
m_dirty
=
true
;
238
}
239
}
240
241
void
EmbeddedOptionsControlPrivate
::
sortAndPopulateProfileCombo
()
242
{
243
// Clear items until only "None" is left
244
for
(
int
i
=
m_profileCombo
->
count
() - 1;
i
> 0;
i
--)
245
m_profileCombo
->
removeItem
(
i
);
246
if
(!
m_sortedProfiles
.
isEmpty
()) {
247
std
::
sort
(
m_sortedProfiles
.
begin
(),
m_sortedProfiles
.
end
(),
deviceProfileLessThan
);
248
m_profileCombo
->
addItems
(
existingProfileNames
());
249
}
250
}
251
252
void
EmbeddedOptionsControlPrivate
::
loadSettings
()
253
{
254
const
QDesignerSharedSettings
settings
(
m_core
);
255
m_sortedProfiles
=
settings
.
deviceProfiles
();
256
sortAndPopulateProfileCombo
();
257
// Index: 0 is "None"
258
const
int
settingsIndex
=
settings
.
currentDeviceProfileIndex
();
259
const
int
profileIndex
=
settingsIndex
>= 0 &&
settingsIndex
<
m_sortedProfiles
.
size
() ?
settingsIndex
+
profileComboIndexOffset
: 0;
260
m_profileCombo
->
setCurrentIndex
(
profileIndex
);
261
updateState
();
262
m_dirty
=
false
;
263
}
264
265
void
EmbeddedOptionsControlPrivate
::
saveSettings
()
266
{
267
QDesignerSharedSettings
settings
(
m_core
);
268
settings
.
setDeviceProfiles
(
m_sortedProfiles
);
269
// Index: 0 is "None"
270
settings
.
setCurrentDeviceProfileIndex
(
m_profileCombo
->
currentIndex
() -
profileComboIndexOffset
);
271
m_dirty
=
false
;
272
}
273
274
//: Format embedded device profile description
275
static
const
char
*
descriptionFormat
=
QT_TRANSLATE_NOOP
(
"EmbeddedOptionsControl"
,
276
"<html>"
277
"<table>"
278
"<tr><td><b>Font</b></td><td>%1, %2</td></tr>"
279
"<tr><td><b>Style</b></td><td>%3</td></tr>"
280
"<tr><td><b>Resolution</b></td><td>%4 x %5</td></tr>"
281
"</table>"
282
"</html>"
);
283
284
static
inline
QString
description
(
const
DeviceProfile& p)
285
{
286
QString styleName = p.style();
287
if
(styleName.isEmpty())
288
styleName = EmbeddedOptionsControl::tr(
"Default"
);
289
return
EmbeddedOptionsControl::tr(descriptionFormat).
290
arg(p.fontFamily()).arg(p.fontPointSize()).arg(styleName).arg(p.dpiX()).arg(p.dpiY());
291
}
292
293
void
EmbeddedOptionsControlPrivate
::updateDescriptionLabel()
294
{
295
const
int
profileIndex = m_profileCombo->currentIndex() - profileComboIndexOffset;
296
if
(profileIndex >= 0) {
297
m_descriptionLabel->setText(description(m_sortedProfiles.at(profileIndex)));
298
}
else
{
299
m_descriptionLabel->clear();
300
}
301
}
302
303
void
EmbeddedOptionsControlPrivate
::updateState()
304
{
305
const
int
profileIndex = m_profileCombo->currentIndex() - profileComboIndexOffset;
306
// Allow for changing/deleting only if it is not in use
307
bool
modifyEnabled =
false
;
308
if
(profileIndex >= 0)
309
modifyEnabled = !m_usedProfiles.contains(m_sortedProfiles.at(profileIndex).name());
310
m_editButton->setEnabled(modifyEnabled);
311
m_deleteButton->setEnabled(modifyEnabled);
312
updateDescriptionLabel();
313
}
314
315
void
EmbeddedOptionsControlPrivate
::
slotProfileIndexChanged
(
int
)
316
{
317
updateState();
318
m_dirty =
true
;
319
}
320
321
// ------------- EmbeddedOptionsControl
322
EmbeddedOptionsControl
::EmbeddedOptionsControl(QDesignerFormEditorInterface *core,
QWidget
*parent) :
323
QWidget(parent),
324
m_d(
new
EmbeddedOptionsControlPrivate(core))
325
{
326
m_d
->
init
(
this
)
;
327
}
328
329
EmbeddedOptionsControl
::~
EmbeddedOptionsControl
()
330
{
331
delete
m_d;
332
}
333
334
void
EmbeddedOptionsControl
::slotAdd()
335
{
336
m_d
->
slotAdd
(
)
;
337
}
338
339
void
EmbeddedOptionsControl
::slotEdit()
340
{
341
m_d
->
slotEdit
(
)
;
342
}
343
344
void
EmbeddedOptionsControl
::slotDelete()
345
{
346
m_d
->
slotDelete
(
)
;
347
}
348
349
void
EmbeddedOptionsControl
::loadSettings()
350
{
351
m_d
->
loadSettings
(
)
;
352
}
353
354
void
EmbeddedOptionsControl
::
saveSettings
()
355
{
356
m_d
->
saveSettings
(
)
;
357
}
358
359
void
EmbeddedOptionsControl
::slotProfileIndexChanged(
int
i)
360
{
361
m_d
->
slotProfileIndexChanged
(
i
)
;
362
}
363
364
bool
EmbeddedOptionsControl
::
isDirty
()
const
365
{
366
return
m_d
->
isDirty
(
)
;
367
}
368
369
// EmbeddedOptionsPage:
370
EmbeddedOptionsPage
::EmbeddedOptionsPage(QDesignerFormEditorInterface *core) :
371
m_core(core)
372
{
373
}
374
375
QString
EmbeddedOptionsPage
::
name
()
const
376
{
377
//: Tab in preferences dialog
378
return
QCoreApplication::translate(
"EmbeddedOptionsPage"
,
"Embedded Design"
);
379
}
380
381
QWidget
*
EmbeddedOptionsPage
::createPage(
QWidget
*parent)
382
{
383
QWidget
*optionsWidget =
new
QWidget(parent);
384
385
QVBoxLayout *optionsVLayout =
new
QVBoxLayout();
386
387
//: EmbeddedOptionsControl group box"
388
QGroupBox *gb =
new
QGroupBox(QCoreApplication::translate(
"EmbeddedOptionsPage"
,
"Device Profiles"
));
389
QVBoxLayout *gbVLayout =
new
QVBoxLayout();
390
m_embeddedOptionsControl =
new
EmbeddedOptionsControl(m_core);
391
m_embeddedOptionsControl->loadSettings();
392
gbVLayout->addWidget(m_embeddedOptionsControl);
393
gb->setLayout(gbVLayout);
394
optionsVLayout->addWidget(gb);
395
396
optionsVLayout->addStretch(1);
397
398
// Outer layout to give it horizontal stretch
399
QHBoxLayout *optionsHLayout =
new
QHBoxLayout();
400
optionsHLayout->addLayout(optionsVLayout);
401
optionsHLayout->addStretch(1);
402
optionsWidget->setLayout(optionsHLayout);
403
return
optionsWidget;
404
}
405
406
void
EmbeddedOptionsPage
::
apply
()
407
{
408
if
(!m_embeddedOptionsControl || !m_embeddedOptionsControl->isDirty())
409
return
;
410
411
m_embeddedOptionsControl->saveSettings();
412
if
(
FormWindowManager
*fw = qobject_cast<
qdesigner_internal
::
FormWindowManager
*>(m_core->formWindowManager()))
413
fw
->
deviceProfilesChanged
(
)
;
414
}
415
416
void
EmbeddedOptionsPage
::
finish
()
417
{
418
}
419
}
420
421
QT_END_NAMESPACE
QPainter::QWidget
friend class QWidget
Definition
qpainter.h:432
qdesigner_internal::EmbeddedOptionsControlPrivate
Definition
embeddedoptionspage.cpp:62
qdesigner_internal::EmbeddedOptionsControlPrivate::isDirty
bool isDirty() const
Definition
embeddedoptionspage.cpp:68
qdesigner_internal::EmbeddedOptionsControlPrivate::init
void init(EmbeddedOptionsControl *q)
Definition
embeddedoptionspage.cpp:117
qdesigner_internal::EmbeddedOptionsControlPrivate::slotProfileIndexChanged
void slotProfileIndexChanged(int)
Definition
embeddedoptionspage.cpp:315
qdesigner_internal::EmbeddedOptionsControlPrivate::slotEdit
void slotEdit()
Definition
embeddedoptionspage.cpp:191
qdesigner_internal::EmbeddedOptionsControlPrivate::saveSettings
void saveSettings()
Definition
embeddedoptionspage.cpp:265
qdesigner_internal::EmbeddedOptionsControlPrivate::slotAdd
void slotAdd()
Definition
embeddedoptionspage.cpp:161
qdesigner_internal::EmbeddedOptionsControlPrivate::loadSettings
void loadSettings()
Definition
embeddedoptionspage.cpp:252
qdesigner_internal::EmbeddedOptionsControlPrivate::slotDelete
void slotDelete()
Definition
embeddedoptionspage.cpp:225
qdesigner_internal::EmbeddedOptionsControl
Definition
embeddedoptionspage.h:22
qdesigner_internal::EmbeddedOptionsControl::~EmbeddedOptionsControl
~EmbeddedOptionsControl() override
Definition
embeddedoptionspage.cpp:329
qdesigner_internal::EmbeddedOptionsControl::saveSettings
void saveSettings()
Definition
embeddedoptionspage.cpp:354
qdesigner_internal::EmbeddedOptionsControl::isDirty
bool isDirty() const
Definition
embeddedoptionspage.cpp:364
qdesigner_internal::EmbeddedOptionsPage
Definition
embeddedoptionspage.h:49
qdesigner_internal::EmbeddedOptionsPage::finish
void finish() override
Definition
embeddedoptionspage.cpp:416
qdesigner_internal::EmbeddedOptionsPage::apply
void apply() override
Definition
embeddedoptionspage.cpp:406
qdesigner_internal::EmbeddedOptionsPage::name
QString name() const override
Definition
embeddedoptionspage.cpp:375
qdesigner_internal::FormWindowManager
Definition
formwindowmanager.h:34
qdesigner_internal::FormWindowManager::deviceProfilesChanged
void deviceProfilesChanged()
Definition
formwindowmanager.cpp:934
qdesigner_internal
Auxiliary methods to store/retrieve settings.
Definition
buddyeditor.cpp:67
qdesigner_internal::deviceProfileLessThan
bool deviceProfileLessThan(const DeviceProfile &d1, const DeviceProfile &d2)
Definition
embeddedoptionspage.cpp:46
qdesigner_internal::ask
static bool ask(QWidget *parent, QDesignerDialogGuiInterface *dlgui, const QString &title, const QString &what)
Definition
embeddedoptionspage.cpp:51
qdesigner_internal::description
static QString description(const DeviceProfile &p)
Definition
embeddedoptionspage.cpp:284
qdesigner_internal::profileComboIndexOffset
@ profileComboIndexOffset
Definition
embeddedoptionspage.cpp:43
qttools
src
designer
src
components
formeditor
embeddedoptionspage.cpp
Generated on
for Qt by
1.18.0