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
newformwidget.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
"newformwidget_p.h"
6
#
include
"ui_newformwidget.h"
7
#
include
"qdesigner_formbuilder_p.h"
8
#
include
"sheet_delegate_p.h"
9
#
include
"widgetdatabase_p.h"
10
#
include
"shared_settings_p.h"
11
12
#
include
<
QtDesigner
/
abstractformeditor
.
h
>
13
#
include
<
QtDesigner
/
abstractformwindow
.
h
>
14
#
include
<
QtDesigner
/
qextensionmanager
.
h
>
15
#
include
<
QtDesigner
/
abstractlanguage
.
h
>
16
#
include
<
QtDesigner
/
abstractwidgetdatabase
.
h
>
17
18
#
include
<
QtCore
/
qdir
.
h
>
19
#
include
<
QtCore
/
qfile
.
h
>
20
#
include
<
QtCore
/
qfileinfo
.
h
>
21
#
include
<
QtCore
/
qdebug
.
h
>
22
#
include
<
QtCore
/
qbytearray
.
h
>
23
#
include
<
QtCore
/
qbuffer
.
h
>
24
#
include
<
QtCore
/
qdir
.
h
>
25
#
include
<
QtCore
/
qtextstream
.
h
>
26
27
#
include
<
QtWidgets
/
qapplication
.
h
>
28
#
include
<
QtWidgets
/
qheaderview
.
h
>
29
#
include
<
QtWidgets
/
qtreewidget
.
h
>
30
#
include
<
QtGui
/
qpainter
.
h
>
31
#
include
<
QtGui
/
qscreen
.
h
>
32
#
include
<
QtWidgets
/
qpushbutton
.
h
>
33
34
#
include
<
array
>
35
36
QT_BEGIN_NAMESPACE
37
38
using
namespace
Qt::StringLiterals;
39
40
enum
{
profileComboIndexOffset
= 1 };
41
enum
{
debugNewFormWidget
= 0 };
42
43
enum
NewForm_CustomRole
{
44
// File name (templates from resources, paths)
45
TemplateNameRole
=
Qt
::
UserRole
+ 100,
46
// Class name (widgets from Widget data base)
47
ClassNameRole
=
Qt
::
UserRole
+ 101
48
};
49
50
static
constexpr
auto
newFormObjectNameC
=
"Form"_L1
;
51
52
// Create a form name for an arbitrary class. If it is Qt, qtify it,
53
// else return "Form".
54
static
QString
formName
(
const
QString &className)
55
{
56
if
(!className.startsWith(u'Q'))
57
return
newFormObjectNameC;
58
QString rc = className;
59
rc.remove(0, 1);
60
return
rc;
61
}
62
63
namespace
qdesigner_internal
{
64
65
struct
StandardScreenSize
66
{
67
QLatin1StringView
name
;
68
QSize
size
;
69
};
70
71
static
constexpr
std
::array
standardScreenSizes
= {
72
StandardScreenSize
{
"QVGA"_L1
, {320, 240} },
73
StandardScreenSize
{
"MIMXRT1050-EVKB"_L1
, {480,272} },
74
StandardScreenSize
{
"VGA"_L1
, {640, 480} },
75
StandardScreenSize
{
"WVGA"_L1
, {800, 480} },
76
StandardScreenSize
{
"WSVGA"_L1
, {1024, 600} },
77
StandardScreenSize
{
"XGA"_L1
, {1024, 768} },
78
StandardScreenSize
{
"MIMXRT1170-EVKB"_L1
, {1280, 720} },
79
StandardScreenSize
{
"WXGA"_L1
, {1280, 800} }
80
};
81
82
static
void
populateSizeCombo
(QComboBox *cb)
83
{
84
cb->addItem(NewFormWidget::tr(
"Default size"
), QSize{ 0, 0 });
85
for
(
const
auto
&size : standardScreenSizes) {
86
//: Standard screen size: %1 Name, %2 screen width, %3 screen height
87
QString portraitName = NewFormWidget::tr(
"%1 portrait (%2x%3)"
).arg(size.name)
88
.arg(size.size.height()).arg(size.size.width());
89
cb->addItem(portraitName, size.size.transposed());
90
//: Standard screen size: %1 Name, %2 screen width, %3 screen height
91
QString landscapeName = NewFormWidget::tr(
"%1 landscape (%2x%3)"
).arg(size.name)
92
.arg(size.size.width()).arg(size.size.height());
93
cb->addItem(landscapeName, size.size);
94
}
95
}
96
97
/* -------------- NewForm dialog.
98
* Designer takes new form templates from:
99
* 1) Files located in directories specified in resources
100
* 2) Files located in directories specified as user templates
101
* 3) XML from container widgets deemed usable for form templates by the widget
102
* database
103
* 4) XML from custom container widgets deemed usable for form templates by the
104
* widget database
105
*
106
* The widget database provides helper functions to obtain lists of names
107
* and xml for 3,4.
108
*
109
* Fixed-size forms for embedded platforms are obtained as follows:
110
* 1) If the origin is a file:
111
* - Check if the file exists in the subdirectory "/<width>x<height>/" of
112
* the path (currently the case for the dialog box because the button box
113
* needs to be positioned)
114
* - Scale the form using the QWidgetDatabase::scaleFormTemplate routine.
115
* 2) If the origin is XML:
116
* - Scale the form using the QWidgetDatabase::scaleFormTemplate routine.
117
*
118
* The tree widget item roles indicate which type of entry it is
119
* (TemplateNameRole = file name 1,2, ClassNameRole = class name 3,4)
120
*/
121
122
NewFormWidget
::
NewFormWidget
(
QDesignerFormEditorInterface
*
core
,
QWidget
*
parentWidget
) :
123
QDesignerNewFormWidgetInterface
(
parentWidget
),
124
m_core
(
core
),
125
m_ui
(
new
QT_PREPEND_NAMESPACE
(
qdesigner_internal
)::
Ui
::
NewFormWidget
),
126
m_currentItem
(
nullptr
),
127
m_acceptedItem
(
nullptr
)
128
{
129
// ### FIXME Qt 8: Remove (QTBUG-96005)
130
#
if
QT_VERSION
>=
QT_VERSION_CHECK
(
7
,
0
,
0
)
131
QDesignerSharedSettings
::
migrateTemplates
();
132
#
endif
133
134
m_ui
->
setupUi
(
this
);
135
m_ui
->
treeWidget
->
setItemDelegate
(
new
qdesigner_internal
::
SheetDelegate
(
m_ui
->
treeWidget
,
this
));
136
m_ui
->
treeWidget
->
header
()->
hide
();
137
m_ui
->
treeWidget
->
header
()->
setStretchLastSection
(
true
);
138
m_ui
->
lblPreview
->
setBackgroundRole
(
QPalette
::
Base
);
139
140
connect
(
m_ui
->
treeWidget
, &
QTreeWidget
::
itemActivated
,
141
this
, &
NewFormWidget
::
treeWidgetItemActivated
);
142
connect
(
m_ui
->
treeWidget
, &
QTreeWidget
::
currentItemChanged
,
143
this
, &
NewFormWidget
::
treeWidgetCurrentItemChanged
);
144
connect
(
m_ui
->
treeWidget
, &
QTreeWidget
::
itemPressed
,
145
this
, &
NewFormWidget
::
treeWidgetItemPressed
);
146
147
QDesignerSharedSettings
settings
(
m_core
);
148
149
QString
uiExtension
= u"ui"_s;
150
QString
templatePath
= u":/qt-project.org/designer/templates/forms"_s;
151
152
QDesignerLanguageExtension
*
lang
=
qt_extension
<
QDesignerLanguageExtension
*>(
core
->
extensionManager
(),
core
);
153
if
(
lang
) {
154
templatePath
= u":/templates/forms"_s;
155
uiExtension
=
lang
->
uiExtension
();
156
}
157
158
// Resource templates
159
const
QString
formTemplate
=
settings
.
formTemplate
();
160
QTreeWidgetItem
*
selectedItem
=
nullptr
;
161
loadFrom
(
templatePath
,
true
,
uiExtension
,
formTemplate
,
selectedItem
);
162
// Additional template paths
163
const
QStringList
formTemplatePaths
=
settings
.
formTemplatePaths
();
164
for
(
const
auto
&
ftp
:
formTemplatePaths
)
165
loadFrom
(
ftp
,
false
,
uiExtension
,
formTemplate
,
selectedItem
);
166
167
// Widgets/custom widgets
168
if
(!
lang
) {
169
//: New Form Dialog Categories
170
loadFrom
(
tr
(
"Widgets"
),
qdesigner_internal
::
WidgetDataBase
::
formWidgetClasses
(
core
),
formTemplate
,
selectedItem
);
171
loadFrom
(
tr
(
"Custom Widgets"
),
qdesigner_internal
::
WidgetDataBase
::
customFormWidgetClasses
(
core
),
formTemplate
,
selectedItem
);
172
}
173
174
// Still no selection - default to first item
175
if
(
selectedItem
==
nullptr
&&
m_ui
->
treeWidget
->
topLevelItemCount
() != 0) {
176
QTreeWidgetItem
*
firstTopLevel
=
m_ui
->
treeWidget
->
topLevelItem
(0);
177
if
(
firstTopLevel
->
childCount
() > 0)
178
selectedItem
=
firstTopLevel
->
child
(0);
179
}
180
181
// Open parent, select and make visible
182
if
(
selectedItem
) {
183
m_ui
->
treeWidget
->
setCurrentItem
(
selectedItem
);
184
selectedItem
->
setSelected
(
true
);
185
m_ui
->
treeWidget
->
scrollToItem
(
selectedItem
->
parent
());
186
}
187
// Fill profile combo
188
m_deviceProfiles
=
settings
.
deviceProfiles
();
189
m_ui
->
profileComboBox
->
addItem
(
tr
(
"None"
));
190
connect
(
m_ui
->
profileComboBox
,
191
&
QComboBox
::
currentIndexChanged
,
192
this
, &
NewFormWidget
::
slotDeviceProfileIndexChanged
);
193
if
(
m_deviceProfiles
.
isEmpty
()) {
194
m_ui
->
profileComboBox
->
setEnabled
(
false
);
195
}
else
{
196
for
(
const
auto
&
deviceProfile
:
std
::
as_const
(
m_deviceProfiles
))
197
m_ui
->
profileComboBox
->
addItem
(
deviceProfile
.
name
());
198
const
int
ci
=
settings
.
currentDeviceProfileIndex
();
199
if
(
ci
>= 0)
200
m_ui
->
profileComboBox
->
setCurrentIndex
(
ci
+
profileComboIndexOffset
);
201
}
202
203
populateSizeCombo
(
m_ui
->
sizeComboBox
);
204
205
setTemplateSize
(
settings
.
newFormSize
());
206
207
if
(
debugNewFormWidget
)
208
qDebug
() <<
Q_FUNC_INFO
<<
"Leaving"
;
209
}
210
211
NewFormWidget
::~
NewFormWidget
()
212
{
213
QDesignerSharedSettings
settings
(
m_core
);
214
settings
.
setNewFormSize
(
templateSize
());
215
// Do not change previously stored item if dialog was rejected
216
if
(
m_acceptedItem
)
217
settings
.
setFormTemplate
(
m_acceptedItem
->
text
(0));
218
delete
m_ui
;
219
}
220
221
void
NewFormWidget
::
treeWidgetCurrentItemChanged
(
QTreeWidgetItem
*
current
)
222
{
223
if
(
debugNewFormWidget
)
224
qDebug
() <<
Q_FUNC_INFO
<<
current
;
225
if
(!
current
)
226
return
;
227
228
if
(!
current
->
parent
()) {
// Top level item: Ensure expanded when browsing down
229
return
;
230
}
231
232
m_currentItem
=
current
;
233
234
emit
currentTemplateChanged
(
showCurrentItemPixmap
());
235
}
236
237
bool
NewFormWidget
::
showCurrentItemPixmap
()
238
{
239
bool
rc
=
false
;
240
if
(
m_currentItem
) {
241
const
QPixmap
pixmap
=
formPreviewPixmap
(
m_currentItem
);
242
if
(
pixmap
.
isNull
()) {
243
m_ui
->
lblPreview
->
setText
(
tr
(
"Error loading form"
));
244
}
else
{
245
m_ui
->
lblPreview
->
setPixmap
(
pixmap
);
246
rc
=
true
;
247
}
248
}
249
return
rc
;
250
}
251
252
void
NewFormWidget
::
treeWidgetItemActivated
(
QTreeWidgetItem
*
item
)
253
{
254
if
(
debugNewFormWidget
)
255
qDebug
() <<
Q_FUNC_INFO
<<
item
;
256
257
if
(
item
->
data
(0,
TemplateNameRole
).
isValid
() ||
item
->
data
(0,
ClassNameRole
).
isValid
())
258
emit
templateActivated
();
259
}
260
261
QPixmap
NewFormWidget
::
formPreviewPixmap
(
const
QTreeWidgetItem
*
item
)
262
{
263
// Cache pixmaps per item/device profile
264
const
ItemPixmapCacheKey
cacheKey
(
item
,
profileComboIndex
());
265
auto
it
=
m_itemPixmapCache
.
find
(
cacheKey
);
266
if
(
it
==
m_itemPixmapCache
.
end
()) {
267
// file or string?
268
const
QVariant
fileName
=
item
->
data
(0,
TemplateNameRole
);
269
QPixmap
rc
;
270
if
(
fileName
.
metaType
().
id
() ==
QMetaType
::
QString
) {
271
rc
=
formPreviewPixmap
(
fileName
.
toString
());
272
}
else
{
273
const
QVariant
classNameV
=
item
->
data
(0,
ClassNameRole
);
274
Q_ASSERT
(
classNameV
.
metaType
().
id
() ==
QMetaType
::
QString
);
275
const
QString
className
=
classNameV
.
toString
();
276
QByteArray
data
=
qdesigner_internal
::
WidgetDataBase
::
formTemplate
(
m_core
,
className
,
formName
(
className
)).
toUtf8
();
277
QBuffer
buffer
(&
data
);
278
buffer
.
open
(
QIODevice
::
ReadOnly
);
279
rc
=
formPreviewPixmap
(
buffer
);
280
}
281
if
(
rc
.
isNull
())
// Retry invalid ones
282
return
rc
;
283
it
=
m_itemPixmapCache
.
insert
(
cacheKey
,
rc
);
284
}
285
return
it
.
value
();
286
}
287
288
QPixmap
NewFormWidget
::
formPreviewPixmap
(
const
QString
&
fileName
)
const
289
{
290
QFile
f
(
fileName
);
291
if
(
f
.
open
(
QFile
::
ReadOnly
)) {
292
QFileInfo
fi
(
fileName
);
293
const
QPixmap
rc
=
formPreviewPixmap
(
f
,
fi
.
absolutePath
());
294
f
.
close
();
295
return
rc
;
296
}
297
qWarning
() <<
"The file "
<<
fileName
<<
" could not be opened: "
<<
f
.
errorString
();
298
return
QPixmap
();
299
}
300
301
QImage
NewFormWidget
::
grabForm
(
QDesignerFormEditorInterface
*
core
,
302
QIODevice
&
file
,
303
const
QString
&
workingDir
,
304
const
qdesigner_internal
::
DeviceProfile
&
dp
)
305
{
306
qdesigner_internal
::
NewFormWidgetFormBuilder
307
formBuilder
(
core
,
dp
);
308
if
(!
workingDir
.
isEmpty
())
309
formBuilder
.
setWorkingDirectory
(
workingDir
);
310
311
QWidget
*
widget
=
formBuilder
.
load
(&
file
,
nullptr
);
312
if
(!
widget
)
313
return
QImage
();
314
315
const
QPixmap
pixmap
=
widget
->
grab
(
QRect
(0, 0, -1, -1));
316
widget
->
deleteLater
();
317
return
pixmap
.
toImage
();
318
}
319
320
QPixmap
NewFormWidget
::
formPreviewPixmap
(
QIODevice
&
file
,
const
QString
&
workingDir
)
const
321
{
322
const
QSizeF
screenSize
(
screen
()->
geometry
().
size
());
323
const
int
previewSize
=
qRound
(
screenSize
.
width
() / 7.5);
// 256 on 1920px screens.
324
const
int
margin
=
previewSize
/ 32 - 1;
// 7 on 1920px screens.
325
const
int
shadow
=
margin
;
326
327
const
QImage
wimage
=
grabForm
(
m_core
,
file
,
workingDir
,
currentDeviceProfile
());
328
if
(
wimage
.
isNull
())
329
return
QPixmap
();
330
const
qreal
devicePixelRatio
=
wimage
.
devicePixelRatioF
();
331
const
QSize
imageSize
(
previewSize
-
margin
* 2,
previewSize
-
margin
* 2);
332
QImage
image
=
wimage
.
scaled
((
QSizeF
(
imageSize
) *
devicePixelRatio
).
toSize
(),
333
Qt
::
KeepAspectRatio
,
Qt
::
SmoothTransformation
);
334
image
.
setDevicePixelRatio
(
devicePixelRatio
);
335
336
QImage
dest
((
QSizeF
(
previewSize
,
previewSize
) *
devicePixelRatio
).
toSize
(),
337
QImage
::
Format_ARGB32_Premultiplied
);
338
dest
.
setDevicePixelRatio
(
devicePixelRatio
);
339
dest
.
fill
(0);
340
341
QPainter
p
(&
dest
);
342
p
.
drawImage
(
margin
,
margin
,
image
);
343
344
p
.
setPen
(
QPen
(
palette
().
brush
(
QPalette
::
WindowText
), 0));
345
346
p
.
drawRect
(
QRectF
(
margin
- 1,
margin
- 1,
imageSize
.
width
() + 1.5,
imageSize
.
height
() + 1.5));
347
348
const
QColor
dark
(
Qt
::
darkGray
);
349
const
QColor
light
(
Qt
::
transparent
);
350
351
// right shadow
352
{
353
const
QRect
rect
(
margin
+
imageSize
.
width
() + 1,
margin
+
shadow
,
shadow
,
imageSize
.
height
() -
shadow
+ 1);
354
QLinearGradient
lg
(
rect
.
topLeft
(),
rect
.
topRight
());
355
lg
.
setColorAt
(0,
dark
);
356
lg
.
setColorAt
(1,
light
);
357
p
.
fillRect
(
rect
,
lg
);
358
}
359
360
// bottom shadow
361
{
362
const
QRect
rect
(
margin
+
shadow
,
margin
+
imageSize
.
height
() + 1,
imageSize
.
width
() -
shadow
+ 1,
shadow
);
363
QLinearGradient
lg
(
rect
.
topLeft
(),
rect
.
bottomLeft
());
364
lg
.
setColorAt
(0,
dark
);
365
lg
.
setColorAt
(1,
light
);
366
p
.
fillRect
(
rect
,
lg
);
367
}
368
369
// bottom/right corner shadow
370
{
371
const
QRect
rect
(
margin
+
imageSize
.
width
() + 1,
margin
+
imageSize
.
height
() + 1,
shadow
,
shadow
);
372
QRadialGradient
g
(
rect
.
topLeft
(),
shadow
- 1);
373
g
.
setColorAt
(0,
dark
);
374
g
.
setColorAt
(1,
light
);
375
p
.
fillRect
(
rect
,
g
);
376
}
377
378
// top/right corner
379
{
380
const
QRect
rect
(
margin
+
imageSize
.
width
() + 1,
margin
,
shadow
,
shadow
);
381
QRadialGradient
g
(
rect
.
bottomLeft
(),
shadow
- 1);
382
g
.
setColorAt
(0,
dark
);
383
g
.
setColorAt
(1,
light
);
384
p
.
fillRect
(
rect
,
g
);
385
}
386
387
// bottom/left corner
388
{
389
const
QRect
rect
(
margin
,
margin
+
imageSize
.
height
() + 1,
shadow
,
shadow
);
390
QRadialGradient
g
(
rect
.
topRight
(),
shadow
- 1);
391
g
.
setColorAt
(0,
dark
);
392
g
.
setColorAt
(1,
light
);
393
p
.
fillRect
(
rect
,
g
);
394
}
395
396
p
.
end
();
397
398
return
QPixmap
::
fromImage
(
dest
);
399
}
400
401
void
NewFormWidget
::
loadFrom
(
const
QString
&
path
,
bool
resourceFile
,
const
QString
&
uiExtension
,
402
const
QString
&
selectedItem
,
QTreeWidgetItem
*&
selectedItemFound
)
403
{
404
const
QDir
dir
(
path
);
405
406
if
(!
dir
.
exists
())
407
return
;
408
409
// Iterate through the directory and add the templates
410
const
QFileInfoList
list
=
dir
.
entryInfoList
(
QStringList
{
"*."_L1
+
uiExtension
},
411
QDir
::
Files
);
412
413
if
(
list
.
isEmpty
())
414
return
;
415
416
const
QChar
separator
=
resourceFile
?
QChar
(u'/')
417
:
QDir
::
separator
();
418
QTreeWidgetItem
*
root
=
new
QTreeWidgetItem
(
m_ui
->
treeWidget
);
419
root
->
setFlags
(
root
->
flags
() & ~
Qt
::
ItemIsSelectable
);
420
// Try to get something that is easy to read.
421
QString
visiblePath
=
path
;
422
int
index
=
visiblePath
.
lastIndexOf
(
separator
);
423
if
(
index
!= -1) {
424
// try to find a second slash, just to be a bit better.
425
const
int
index2
=
visiblePath
.
lastIndexOf
(
separator
,
index
- 1);
426
if
(
index2
!= -1)
427
index
=
index2
;
428
visiblePath
=
visiblePath
.
mid
(
index
+ 1);
429
visiblePath
=
QDir
::
toNativeSeparators
(
visiblePath
);
430
}
431
432
root
->
setText
(0,
visiblePath
.
replace
(u'_', u' '));
433
root
->
setToolTip
(0,
path
);
434
435
for
(
const
auto
&
fi
:
list
) {
436
if
(!
fi
.
isFile
())
437
continue
;
438
439
QTreeWidgetItem
*
item
=
new
QTreeWidgetItem
(
root
);
440
const
QString
text
=
fi
.
baseName
().
replace
(u'_', u' ');
441
if
(
selectedItemFound
==
nullptr
&&
text
==
selectedItem
)
442
selectedItemFound
=
item
;
443
item
->
setText
(0,
text
);
444
item
->
setData
(0,
TemplateNameRole
,
fi
.
absoluteFilePath
());
445
}
446
}
447
448
void
NewFormWidget
::
loadFrom
(
const
QString
&
title
,
const
QStringList
&
nameList
,
449
const
QString
&
selectedItem
,
QTreeWidgetItem
*&
selectedItemFound
)
450
{
451
if
(
nameList
.
isEmpty
())
452
return
;
453
QTreeWidgetItem
*
root
=
new
QTreeWidgetItem
(
m_ui
->
treeWidget
);
454
root
->
setFlags
(
root
->
flags
() & ~
Qt
::
ItemIsSelectable
);
455
root
->
setText
(0,
title
);
456
for
(
const
auto
&
text
:
nameList
) {
457
QTreeWidgetItem
*
item
=
new
QTreeWidgetItem
(
root
);
458
item
->
setText
(0,
text
);
459
if
(
selectedItemFound
==
nullptr
&&
text
==
selectedItem
)
460
selectedItemFound
=
item
;
461
item
->
setData
(0,
ClassNameRole
,
text
);
462
}
463
}
464
465
void
NewFormWidget
::
treeWidgetItemPressed
(
QTreeWidgetItem
*
item
)
466
{
467
if
(
item
&& !
item
->
parent
())
468
item
->
setExpanded
(!
item
->
isExpanded
());
469
}
470
471
QSize
NewFormWidget
::
templateSize
()
const
472
{
473
return
m_ui
->
sizeComboBox
->
itemData
(
m_ui
->
sizeComboBox
->
currentIndex
()).
toSize
();
474
}
475
476
void
NewFormWidget
::
setTemplateSize
(
const
QSize
&
s
)
477
{
478
const
int
index
=
s
.
isNull
() ? 0 :
m_ui
->
sizeComboBox
->
findData
(
s
);
479
if
(
index
!= -1)
480
m_ui
->
sizeComboBox
->
setCurrentIndex
(
index
);
481
}
482
483
static
QString
readAll
(
const
QString &fileName, QString *errorMessage)
484
{
485
QFile file(fileName);
486
if
(!file.open(QIODevice::ReadOnly|QIODevice::Text)) {
487
*errorMessage = NewFormWidget::tr(
"Unable to open the form template file '%1': %2"
).arg(fileName, file.errorString());
488
return
QString();
489
}
490
return
QString::fromUtf8(file.readAll());
491
}
492
493
QString
NewFormWidget
::
itemToTemplate
(
const
QTreeWidgetItem
*
item
,
QString
*
errorMessage
)
const
494
{
495
const
QSize
size
=
templateSize
();
496
// file name or string contents?
497
const
QVariant
templateFileName
=
item
->
data
(0,
TemplateNameRole
);
498
if
(
templateFileName
.
metaType
().
id
() ==
QMetaType
::
QString
) {
499
const
QString
fileName
=
templateFileName
.
toString
();
500
// No fixed size: just open.
501
if
(
size
.
isNull
())
502
return
readAll
(
fileName
,
errorMessage
);
503
// try to find a file matching the size, like "../640x480/xx.ui"
504
const
QFileInfo
fiBase
(
fileName
);
505
QString
sizeFileName
;
506
QTextStream
(&
sizeFileName
) <<
fiBase
.
path
() <<
QDir
::
separator
()
507
<<
size
.
width
() <<
'x'
<<
size
.
height
() <<
QDir
::
separator
()
508
<<
fiBase
.
fileName
();
509
if
(
QFileInfo
(
sizeFileName
).
isFile
())
510
return
readAll
(
sizeFileName
,
errorMessage
);
511
// Nothing found, scale via DOM/temporary file
512
QString
contents
=
readAll
(
fileName
,
errorMessage
);
513
if
(!
contents
.
isEmpty
())
514
contents
=
qdesigner_internal
::
WidgetDataBase
::
scaleFormTemplate
(
contents
,
size
,
false
);
515
return
contents
;
516
}
517
// Content.
518
const
QString
className
=
item
->
data
(0,
ClassNameRole
).
toString
();
519
QString
contents
=
qdesigner_internal
::
WidgetDataBase
::
formTemplate
(
m_core
,
className
,
formName
(
className
));
520
if
(!
size
.
isNull
())
521
contents
=
qdesigner_internal
::
WidgetDataBase
::
scaleFormTemplate
(
contents
,
size
,
false
);
522
return
contents
;
523
}
524
525
void
NewFormWidget
::
slotDeviceProfileIndexChanged
(
int
idx
)
526
{
527
// Store index for form windows to take effect and refresh pixmap
528
QDesignerSharedSettings
settings
(
m_core
);
529
settings
.
setCurrentDeviceProfileIndex
(
idx
-
profileComboIndexOffset
);
530
showCurrentItemPixmap
();
531
}
532
533
int
NewFormWidget
::
profileComboIndex
()
const
534
{
535
return
m_ui
->
profileComboBox
->
currentIndex
();
536
}
537
538
qdesigner_internal
::
DeviceProfile
NewFormWidget
::
currentDeviceProfile
()
const
539
{
540
const
int
ci
=
profileComboIndex
();
541
if
(
ci
> 0)
542
return
m_deviceProfiles
.
at
(
ci
-
profileComboIndexOffset
);
543
return
qdesigner_internal
::
DeviceProfile
();
544
}
545
546
bool
NewFormWidget
::
hasCurrentTemplate
()
const
547
{
548
return
m_currentItem
!=
nullptr
;
549
}
550
551
QString
NewFormWidget
::
currentTemplateI
(
QString
*
ptrToErrorMessage
)
552
{
553
if
(
m_currentItem
==
nullptr
) {
554
*
ptrToErrorMessage
=
tr
(
"Internal error: No template selected."
);
555
return
QString
();
556
}
557
const
QString
contents
=
itemToTemplate
(
m_currentItem
,
ptrToErrorMessage
);
558
if
(
contents
.
isEmpty
())
559
return
contents
;
560
561
m_acceptedItem
=
m_currentItem
;
562
return
contents
;
563
}
564
565
QString
NewFormWidget
::
currentTemplate
(
QString
*
ptrToErrorMessage
)
566
{
567
if
(
ptrToErrorMessage
)
568
return
currentTemplateI
(
ptrToErrorMessage
);
569
// Do not loose the error
570
QString
errorMessage
;
571
const
QString
contents
=
currentTemplateI
(&
errorMessage
);
572
if
(!
errorMessage
.
isEmpty
())
573
qWarning
(
"%s"
,
errorMessage
.
toUtf8
().
constData
());
574
return
contents
;
575
}
576
577
bool
NewFormWidget
::
isEmbeddedGroupBoxVisible
()
const
578
{
579
return
m_ui
->
embeddedGroup
->
isVisible
();
580
}
581
void
NewFormWidget
::
setEmbeddedGroupBoxVisible
(
bool
v
)
582
{
583
m_ui
->
embeddedGroup
->
setVisible
(
v
);
584
}
585
586
}
// namespace qdesigner_internal
587
588
QT_END_NAMESPACE
qdesigner_internal
Auxiliary methods to store/retrieve settings.
Definition
buddyeditor.cpp:67
qdesigner_internal::readAll
static QString readAll(const QString &fileName, QString *errorMessage)
Definition
newformwidget.cpp:483
qdesigner_internal::populateSizeCombo
static void populateSizeCombo(QComboBox *cb)
Definition
newformwidget.cpp:82
qdesigner_internal::standardScreenSizes
static constexpr std::array standardScreenSizes
Definition
newformwidget.cpp:71
std
[33]
Definition
src_corelib_tools_qhash.cpp:421
newFormObjectNameC
static constexpr auto newFormObjectNameC
Definition
newformwidget.cpp:50
profileComboIndexOffset
@ profileComboIndexOffset
Definition
newformwidget.cpp:40
debugNewFormWidget
@ debugNewFormWidget
Definition
newformwidget.cpp:41
formName
static QString formName(const QString &className)
Definition
newformwidget.cpp:54
NewForm_CustomRole
NewForm_CustomRole
Definition
newformwidget.cpp:43
TemplateNameRole
@ TemplateNameRole
Definition
newformwidget.cpp:45
ClassNameRole
@ ClassNameRole
Definition
newformwidget.cpp:47
qdesigner_internal::StandardScreenSize
Definition
newformwidget.cpp:66
qdesigner_internal::StandardScreenSize::size
QSize size
Definition
newformwidget.cpp:68
qdesigner_internal::StandardScreenSize::name
QLatin1StringView name
Definition
newformwidget.cpp:67
qttools
src
designer
src
lib
shared
newformwidget.cpp
Generated on
for Qt by
1.18.0