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