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
qdesigner_resource.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 "formwindow.h"
8#include "iconloader_p.h"
20
21#include <QtDesigner/abstractformeditor.h>
22#include <QtDesigner/abstractintegration.h>
23#include <QtDesigner/private/ui4_p.h>
24#include <QtDesigner/private/formbuilderextra_p.h>
25#include <QtDesigner/private/resourcebuilder_p.h>
26#include <QtDesigner/private/textbuilder_p.h>
27#include <qdesigner_widgetitem_p.h>
28
29// shared
30#include <widgetdatabase_p.h>
31#include <metadatabase_p.h>
32#include <layout_p.h>
33#include <layoutinfo_p.h>
34#include <spacer_widget_p.h>
35#include <pluginmanager_p.h>
36#include <widgetfactory_p.h>
37#include <abstractlanguage.h>
38#include <abstractintrospection_p.h>
39
40#include <qlayout_widget_p.h>
41#include <qdesigner_utils_p.h>
42#include <QtDesigner/private/ui4_p.h>
43
44// sdk
45#include <QtDesigner/propertysheet.h>
46#include <QtDesigner/abstractformeditor.h>
47#include <QtDesigner/extrainfo.h>
48#include <QtDesigner/abstractformwindowtool.h>
49#include <QtDesigner/qextensionmanager.h>
50#include <QtDesigner/container.h>
51#include <abstractdialoggui_p.h>
52
53#include <QtWidgets/qmenu.h>
54#include <QtWidgets/qmessagebox.h>
55#include <QtWidgets/qlayout.h>
56#include <QtWidgets/qformlayout.h>
57#include <QtWidgets/qtabwidget.h>
58#include <QtWidgets/qtoolbox.h>
59#include <QtWidgets/qstackedwidget.h>
60#include <QtWidgets/qtoolbar.h>
61#include <QtWidgets/qtabbar.h>
62#include <QtWidgets/qbuttongroup.h>
63#include <QtWidgets/qapplication.h>
64#include <QtWidgets/qmainwindow.h>
65#include <QtWidgets/qsplitter.h>
66#include <QtWidgets/qmdiarea.h>
67#include <QtWidgets/qmenubar.h>
68#include <QtWidgets/qfiledialog.h>
69#include <QtWidgets/qheaderview.h>
70#include <QtWidgets/qwizard.h>
71#include <private/qlayoutengine_p.h>
72
73#include <QtGui/qaction.h>
74#include <QtGui/qactiongroup.h>
75
76#include <QtCore/qbuffer.h>
77#include <QtCore/qdir.h>
78#include <QtCore/qmetaobject.h>
79#include <QtCore/qdebug.h>
80#include <QtCore/qversionnumber.h>
81#include <QtCore/qxmlstream.h>
82
83#include <algorithm>
84#include <iterator>
85#include <memory>
86
87Q_DECLARE_METATYPE(QWidgetList)
88
89QT_BEGIN_NAMESPACE
90
91using namespace Qt::StringLiterals;
92
93using QFBE = QFormBuilderExtra;
94
95namespace {
96 using DomPropertyList = QList<DomProperty *>;
97}
98
99static constexpr auto currentUiVersion = "4.0"_L1;
100static constexpr auto clipboardObjectName = "__qt_fake_top_level"_L1;
101
102#define OLD_RESOURCE_FORMAT // Support pre 4.4 format.
103
104namespace qdesigner_internal {
105
106// PYSIDE-2492 (since 6.7): Write fully qualified enums for Python
107static bool supportsQualifiedEnums(const QVersionNumber &qtVersion)
108{
109 if (qtVersion >= QVersionNumber{6, 6, 2})
110 return true;
111
112 switch (qtVersion.majorVersion()) {
113 case 6: // Qt 6
114 switch (qtVersion.minorVersion()) {
115 case 5: // 6.5 LTS
116 if (qtVersion.microVersion() >= 4)
117 return true;
118 break;
119 case 2: // 6.2 LTS
120 if (qtVersion.microVersion() >= 13)
121 return true;
122 break;
123 }
124 break;
125
126 case 5: // Qt 5 LTS
127 if (qtVersion >= QVersionNumber{5, 15, 18})
128 return true;
129 break;
130 }
131 return false;
132}
133
134// Separate horizontal/vertical size constraints since Qt 7 (QTBUG-17730).
135static bool supportsSeparateSizeConstraints(const QVersionNumber &qtVersion)
136{
137 return qtVersion >= QVersionNumber{7, 0, 0};
138}
139
140// -------------------- QDesignerResourceBuilder: A resource builder that works on the property sheet icon types.
142{
143public:
144 QDesignerResourceBuilder(QDesignerFormEditorInterface *core, DesignerPixmapCache *pixmapCache, DesignerIconCache *iconCache);
145
146 void setPixmapCache(DesignerPixmapCache *pixmapCache) { m_pixmapCache = pixmapCache; }
147 void setIconCache(DesignerIconCache *iconCache) { m_iconCache = iconCache; }
148 bool isSaveRelative() const { return m_saveRelative; }
149 void setSaveRelative(bool relative) { m_saveRelative = relative; }
150 QStringList usedQrcFiles() const { return m_usedQrcFiles.keys(); }
152 QStringList loadedQrcFiles() const { return m_loadedQrcFiles.keys(); } // needed only for loading old resource attribute of <iconset> tag.
153#endif
154
155 QVariant loadResource(const QDir &workingDirectory, const DomProperty *icon) const override;
156
157 QVariant toNativeValue(const QVariant &value) const override;
158
159 DomProperty *saveResource(const QDir &workingDirectory, const QVariant &value) const override;
160
161 bool isResourceType(const QVariant &value) const override;
162private:
163
164 QDesignerFormEditorInterface *m_core;
165 DesignerPixmapCache *m_pixmapCache;
166 DesignerIconCache *m_iconCache;
167 const QDesignerLanguageExtension *m_lang;
168 bool m_saveRelative;
169 mutable QMap<QString, bool> m_usedQrcFiles;
170 mutable QMap<QString, bool> m_loadedQrcFiles;
171};
172
173QDesignerResourceBuilder::QDesignerResourceBuilder(QDesignerFormEditorInterface *core, DesignerPixmapCache *pixmapCache, DesignerIconCache *iconCache) :
174 m_core(core),
175 m_pixmapCache(pixmapCache),
176 m_iconCache(iconCache),
177 m_lang(qt_extension<QDesignerLanguageExtension *>(core->extensionManager(), core)),
178 m_saveRelative(true)
179{
180}
181
182static inline void setIconPixmap(QIcon::Mode m, QIcon::State s, const QDir &workingDirectory,
183 QString path, PropertySheetIconValue &icon,
184 const QDesignerLanguageExtension *lang = nullptr)
185{
186 if (lang == nullptr || !lang->isLanguageResource(path))
187 path = QFileInfo(workingDirectory, path).absoluteFilePath();
188 icon.setPixmap(m, s, PropertySheetPixmapValue(path));
189}
190
191QVariant QDesignerResourceBuilder::loadResource(const QDir &workingDirectory, const DomProperty *property) const
192{
193 switch (property->kind()) {
194 case DomProperty::Pixmap: {
196 DomResourcePixmap *dp = property->elementPixmap();
197 if (!dp->text().isEmpty()) {
198 if (m_lang != nullptr && m_lang->isLanguageResource(dp->text())) {
199 pixmap.setPath(dp->text());
200 } else {
201 pixmap.setPath(QFileInfo(workingDirectory, dp->text()).absoluteFilePath());
202 }
204 if (dp->hasAttributeResource())
205 m_loadedQrcFiles.insert(QFileInfo(workingDirectory, dp->attributeResource()).absoluteFilePath(), false);
206#endif
207 }
208 return QVariant::fromValue(pixmap);
209 }
210
211 case DomProperty::IconSet: {
213 DomResourceIcon *di = property->elementIconSet();
214 const bool hasTheme = di->hasAttributeTheme();
215 if (hasTheme) {
216 const QString &theme = di->attributeTheme();
217 const qsizetype themeEnum = theme.startsWith("QIcon::"_L1)
218 ? QDesignerResourceBuilder::themeIconIndex(theme) : -1;
219 if (themeEnum != -1)
220 icon.setThemeEnum(themeEnum);
221 else
222 icon.setTheme(theme);
223 }
224 if (const int flags = iconStateFlags(di)) { // new, post 4.4 format
225 if (flags & NormalOff)
226 setIconPixmap(QIcon::Normal, QIcon::Off, workingDirectory, di->elementNormalOff()->text(), icon, m_lang);
227 if (flags & NormalOn)
228 setIconPixmap(QIcon::Normal, QIcon::On, workingDirectory, di->elementNormalOn()->text(), icon, m_lang);
229 if (flags & DisabledOff)
230 setIconPixmap(QIcon::Disabled, QIcon::Off, workingDirectory, di->elementDisabledOff()->text(), icon, m_lang);
231 if (flags & DisabledOn)
232 setIconPixmap(QIcon::Disabled, QIcon::On, workingDirectory, di->elementDisabledOn()->text(), icon, m_lang);
233 if (flags & ActiveOff)
234 setIconPixmap(QIcon::Active, QIcon::Off, workingDirectory, di->elementActiveOff()->text(), icon, m_lang);
235 if (flags & ActiveOn)
236 setIconPixmap(QIcon::Active, QIcon::On, workingDirectory, di->elementActiveOn()->text(), icon, m_lang);
237 if (flags & SelectedOff)
238 setIconPixmap(QIcon::Selected, QIcon::Off, workingDirectory, di->elementSelectedOff()->text(), icon, m_lang);
239 if (flags & SelectedOn)
240 setIconPixmap(QIcon::Selected, QIcon::On, workingDirectory, di->elementSelectedOn()->text(), icon, m_lang);
241 } else if (!hasTheme) {
243 setIconPixmap(QIcon::Normal, QIcon::Off, workingDirectory, di->text(), icon, m_lang);
244 if (di->hasAttributeResource())
245 m_loadedQrcFiles.insert(QFileInfo(workingDirectory, di->attributeResource()).absoluteFilePath(), false);
246#endif
247 }
248 return QVariant::fromValue(icon);
249 }
250 default:
251 break;
252 }
253 return QVariant();
254}
255
256QVariant QDesignerResourceBuilder::toNativeValue(const QVariant &value) const
257{
258 if (value.canConvert<PropertySheetPixmapValue>()) {
259 if (m_pixmapCache)
260 return m_pixmapCache->pixmap(qvariant_cast<PropertySheetPixmapValue>(value));
261 } else if (value.canConvert<PropertySheetIconValue>()) {
262 if (m_iconCache)
263 return m_iconCache->icon(qvariant_cast<PropertySheetIconValue>(value));
264 }
265 return value;
266}
267
268DomProperty *QDesignerResourceBuilder::saveResource(const QDir &workingDirectory, const QVariant &value) const
269{
270 DomProperty *p = new DomProperty;
271 if (value.canConvert<PropertySheetPixmapValue>()) {
272 const PropertySheetPixmapValue pix = qvariant_cast<PropertySheetPixmapValue>(value);
273 DomResourcePixmap *rp = new DomResourcePixmap;
274 const QString pixPath = pix.path();
275 switch (pix.pixmapSource(m_core)) {
276 case PropertySheetPixmapValue::LanguageResourcePixmap:
277 rp->setText(pixPath);
278 break;
279 case PropertySheetPixmapValue::ResourcePixmap: {
280 rp->setText(pixPath);
281 const QString qrcFile = m_core->resourceModel()->qrcPath(pixPath);
282 if (!qrcFile.isEmpty()) {
283 m_usedQrcFiles.insert(qrcFile, false);
284#ifdef OLD_RESOURCE_FORMAT // Legacy: Add qrc path
285 rp->setAttributeResource(workingDirectory.relativeFilePath(qrcFile));
286#endif
287 }
288 }
289 break;
290 case PropertySheetPixmapValue::FilePixmap:
291 rp->setText(m_saveRelative ? workingDirectory.relativeFilePath(pixPath) : pixPath);
292 break;
293 }
294 p->setElementPixmap(rp);
295 return p;
296 }
297 if (value.canConvert<PropertySheetIconValue>()) {
298 const PropertySheetIconValue icon = qvariant_cast<PropertySheetIconValue>(value);
299 const auto &pixmaps = icon.paths();
300 const int themeEnum = icon.themeEnum();
301 const QString theme = themeEnum != -1
302 ? QDesignerResourceBuilder::fullyQualifiedThemeIconName(themeEnum) : icon.theme();
303 if (!pixmaps.isEmpty() || !theme.isEmpty()) {
304 DomResourceIcon *ri = new DomResourceIcon;
305 if (!theme.isEmpty())
306 ri->setAttributeTheme(theme);
307 for (auto itPix = pixmaps.cbegin(), end = pixmaps.cend(); itPix != end; ++itPix) {
308 const QIcon::Mode mode = itPix.key().first;
309 const QIcon::State state = itPix.key().second;
310 DomResourcePixmap *rp = new DomResourcePixmap;
311 const PropertySheetPixmapValue &pix = itPix.value();
312 const PropertySheetPixmapValue::PixmapSource ps = pix.pixmapSource(m_core);
313 const QString pixPath = pix.path();
314 rp->setText(ps == PropertySheetPixmapValue::FilePixmap && m_saveRelative ? workingDirectory.relativeFilePath(pixPath) : pixPath);
315 if (state == QIcon::Off) {
316 switch (mode) {
317 case QIcon::Normal:
318 ri->setElementNormalOff(rp);
319#ifdef OLD_RESOURCE_FORMAT // Legacy: Set Normal off as text/path in old format.
320 ri->setText(rp->text());
321#endif
322 if (ps == PropertySheetPixmapValue::ResourcePixmap) {
323 // Be sure that ri->text() file comes from active resourceSet (i.e. make appropriate
324 // resourceSet active before calling this method).
325 const QString qrcFile = m_core->resourceModel()->qrcPath(ri->text());
326 if (!qrcFile.isEmpty()) {
327 m_usedQrcFiles.insert(qrcFile, false);
328#ifdef OLD_RESOURCE_FORMAT // Legacy: Set Normal off as text/path in old format.
329 ri->setAttributeResource(workingDirectory.relativeFilePath(qrcFile));
330#endif
331 }
332 }
333 break;
334 case QIcon::Disabled: ri->setElementDisabledOff(rp); break;
335 case QIcon::Active: ri->setElementActiveOff(rp); break;
336 case QIcon::Selected: ri->setElementSelectedOff(rp); break;
337 }
338 } else {
339 switch (mode) {
340 case QIcon::Normal: ri->setElementNormalOn(rp); break;
341 case QIcon::Disabled: ri->setElementDisabledOn(rp); break;
342 case QIcon::Active: ri->setElementActiveOn(rp); break;
343 case QIcon::Selected: ri->setElementSelectedOn(rp); break;
344 }
345 }
346 }
347 p->setElementIconSet(ri);
348 return p;
349 }
350 }
351 delete p;
352 return nullptr;
353}
354
355bool QDesignerResourceBuilder::isResourceType(const QVariant &value) const
356{
357 return value.canConvert<PropertySheetPixmapValue>()
358 || value.canConvert<PropertySheetIconValue>();
359}
360// ------------------------- QDesignerTextBuilder
361
362template <class DomElement> // for DomString, potentially DomStringList
363inline void translationParametersToDom(const PropertySheetTranslatableData &data, DomElement *e)
364{
365 const QString propertyComment = data.disambiguation();
366 if (!propertyComment.isEmpty())
367 e->setAttributeComment(propertyComment);
368 const QString propertyExtracomment = data.comment();
369 if (!propertyExtracomment.isEmpty())
370 e->setAttributeExtraComment(propertyExtracomment);
371 const QString &id = data.id();
372 if (!id.isEmpty())
373 e->setAttributeId(id);
374 if (!data.translatable())
375 e->setAttributeNotr(u"true"_s);
376}
377
378template <class DomElement> // for DomString, potentially DomStringList
379inline void translationParametersFromDom(const DomElement *e, PropertySheetTranslatableData *data)
380{
381 if (e->hasAttributeComment())
382 data->setDisambiguation(e->attributeComment());
383 if (e->hasAttributeExtraComment())
384 data->setComment(e->attributeExtraComment());
385 if (e->hasAttributeId())
386 data->setId(e->attributeId());
387 if (e->hasAttributeNotr()) {
388 const QString notr = e->attributeNotr();
389 const bool translatable = !(notr == "true"_L1 || notr == "yes"_L1);
390 data->setTranslatable(translatable);
391 }
392}
393
395{
396public:
398
399 QVariant loadText(const DomProperty *icon) const override;
400
401 QVariant toNativeValue(const QVariant &value) const override;
402
403 DomProperty *saveText(const QVariant &value) const override;
404};
405
406QVariant QDesignerTextBuilder::loadText(const DomProperty *text) const
407{
408 if (const DomString *domString = text->elementString()) {
409 PropertySheetStringValue stringValue(domString->text());
410 translationParametersFromDom(domString, &stringValue);
411 return QVariant::fromValue(stringValue);
412 }
413 return QVariant(QString());
414}
415
416QVariant QDesignerTextBuilder::toNativeValue(const QVariant &value) const
417{
418 if (value.canConvert<PropertySheetStringValue>())
419 return QVariant::fromValue(qvariant_cast<PropertySheetStringValue>(value).value());
420 return value;
421}
422
423static inline DomProperty *stringToDomProperty(const QString &value)
424{
425 DomString *domString = new DomString();
426 domString->setText(value);
427 DomProperty *property = new DomProperty();
428 property->setElementString(domString);
429 return property;
430}
431
432static inline DomProperty *stringToDomProperty(const QString &value,
433 const PropertySheetTranslatableData &translatableData)
434{
435 DomString *domString = new DomString();
436 domString->setText(value);
437 translationParametersToDom(translatableData, domString);
438 DomProperty *property = new DomProperty();
439 property->setElementString(domString);
440 return property;
441}
442
443DomProperty *QDesignerTextBuilder::saveText(const QVariant &value) const
444{
445 if (value.canConvert<PropertySheetStringValue>()) {
446 const PropertySheetStringValue str = qvariant_cast<PropertySheetStringValue>(value);
447 return stringToDomProperty(str.value(), str);
448 }
449 if (value.canConvert<QString>())
450 return stringToDomProperty(value.toString());
451 return nullptr;
452}
453
456 m_formWindow(formWindow),
457 m_copyWidget(false),
458 m_selected(nullptr),
459 m_resourceBuilder(new QDesignerResourceBuilder(m_formWindow->core(), m_formWindow->pixmapCache(), m_formWindow->iconCache()))
460{
461 // Check language unless extension present (Jambi)
462 QDesignerFormEditorInterface *core = m_formWindow->core();
463 if (const QDesignerLanguageExtension *le = qt_extension<QDesignerLanguageExtension*>(core->extensionManager(), core))
464 d->m_language = le->name();
465
466 setWorkingDirectory(formWindow->absoluteDir());
467 setResourceBuilder(m_resourceBuilder);
468 setTextBuilder(new QDesignerTextBuilder());
469
470 // ### generalise
471 const QString designerWidget = u"QDesignerWidget"_s;
472 const QString layoutWidget = u"QLayoutWidget"_s;
473 const QString widget = u"QWidget"_s;
474 m_internal_to_qt.insert(layoutWidget, widget);
475 m_internal_to_qt.insert(designerWidget, widget);
476 m_internal_to_qt.insert(u"QDesignerDialog"_s, u"QDialog"_s);
477 m_internal_to_qt.insert(u"QDesignerMenuBar"_s, u"QMenuBar"_s);
478 m_internal_to_qt.insert(u"QDesignerMenu"_s, u"QMenu"_s);
479 m_internal_to_qt.insert(u"QDesignerDockWidget"_s, u"QDockWidget"_s);
480
481 // invert
482 for (auto it = m_internal_to_qt.cbegin(), cend = m_internal_to_qt.cend(); it != cend; ++it ) {
483 if (it.value() != designerWidget && it.value() != layoutWidget)
484 m_qt_to_internal.insert(it.value(), it.key());
485
486 }
487}
488
490
491DomUI *QDesignerResource::readUi(QIODevice *dev)
492{
493 return d->readUi(dev);
494}
495
496static inline QString messageBoxTitle()
497{
498 return QApplication::translate("Designer", "Qt Widgets Designer");
499}
500
501void QDesignerResource::save(QIODevice *dev, QWidget *widget)
502{
503 // Do not write fully qualified enumerations for spacer/line orientations
504 // and other enum/flag properties for older Qt versions since that breaks
505 // older uic.
506 d->m_saveVersion = m_formWindow->core()->integration()->qtVersion();
507 d->m_fullyQualifiedEnums = supportsQualifiedEnums(d->m_saveVersion);
508 QAbstractFormBuilder::save(dev, widget);
509}
510
511void QDesignerResource::saveDom(DomUI *ui, QWidget *widget)
512{
513 QAbstractFormBuilder::saveDom(ui, widget);
514
515 QDesignerPropertySheetExtension *sheet = qt_extension<QDesignerPropertySheetExtension*>(core()->extensionManager(), widget);
516 Q_ASSERT(sheet != nullptr);
517
518 const QVariant classVar = sheet->property(sheet->indexOf(u"objectName"_s));
519 QString classStr;
520 if (classVar.canConvert<QString>())
521 classStr = classVar.toString();
522 else
523 classStr = qvariant_cast<PropertySheetStringValue>(classVar).value();
524 ui->setElementClass(classStr);
525
526 for (int index = 0; index < m_formWindow->toolCount(); ++index) {
527 QDesignerFormWindowToolInterface *tool = m_formWindow->tool(index);
528 Q_ASSERT(tool != nullptr);
529 tool->saveToDom(ui, widget);
530 }
531
532 const QString author = m_formWindow->author();
533 if (!author.isEmpty()) {
534 ui->setElementAuthor(author);
535 }
536
537 const QString comment = m_formWindow->comment();
538 if (!comment.isEmpty()) {
539 ui->setElementComment(comment);
540 }
541
542 const QString exportMacro = m_formWindow->exportMacro();
543 if (!exportMacro.isEmpty()) {
544 ui->setElementExportMacro(exportMacro);
545 }
546
547 if (m_formWindow->useIdBasedTranslations())
548 ui->setAttributeIdbasedtr(true);
549
550 if (core()->integration()->qtVersion() >= QVersionNumber(6, 10, 0)) {
551 const QString label = m_formWindow->idBasedTranslationLabel();
552 if (!label.isEmpty())
553 ui->setAttributeLabel(label);
554 }
555
556 if (!m_formWindow->connectSlotsByName()) // Don't write out if true (default)
557 ui->setAttributeConnectslotsbyname(false);
558
559 const QVariantMap designerFormData = m_formWindow->formData();
560 if (!designerFormData.isEmpty()) {
561 DomPropertyList domPropertyList;
562 for (auto it = designerFormData.cbegin(), cend = designerFormData.cend(); it != cend; ++it) {
563 if (DomProperty *prop = variantToDomProperty(this, widget->metaObject(), it.key(), it.value()))
564 domPropertyList += prop;
565 }
566 if (!domPropertyList.isEmpty()) {
567 DomDesignerData* domDesignerFormData = new DomDesignerData;
568 domDesignerFormData->setElementProperty(domPropertyList);
569 ui->setElementDesignerdata(domDesignerFormData);
570 }
571 }
572
573 if (!m_formWindow->includeHints().isEmpty()) {
574 const QString local = u"local"_s;
575 const QString global = u"global"_s;
576 QList<DomInclude *> ui_includes;
577 const QStringList &includeHints = m_formWindow->includeHints();
578 ui_includes.reserve(includeHints.size());
579 for (QString includeHint : includeHints) {
580 if (includeHint.isEmpty())
581 continue;
582 DomInclude *incl = new DomInclude;
583 const QString location = includeHint.at(0) == u'<' ? global : local;
584 includeHint.remove(u'"');
585 includeHint.remove(u'<');
586 includeHint.remove(u'>');
587 incl->setAttributeLocation(location);
588 incl->setText(includeHint);
589 ui_includes.append(incl);
590 }
591
592 DomIncludes *includes = new DomIncludes;
593 includes->setElementInclude(ui_includes);
594 ui->setElementIncludes(includes);
595 }
596
597 int defaultMargin = INT_MIN, defaultSpacing = INT_MIN;
598 m_formWindow->layoutDefault(&defaultMargin, &defaultSpacing);
599
600 if (defaultMargin != INT_MIN || defaultSpacing != INT_MIN) {
601 DomLayoutDefault *def = new DomLayoutDefault;
602 if (defaultMargin != INT_MIN)
603 def->setAttributeMargin(defaultMargin);
604 if (defaultSpacing != INT_MIN)
605 def->setAttributeSpacing(defaultSpacing);
606 ui->setElementLayoutDefault(def);
607 }
608
609 QString marginFunction, spacingFunction;
610 m_formWindow->layoutFunction(&marginFunction, &spacingFunction);
611 if (!marginFunction.isEmpty() || !spacingFunction.isEmpty()) {
612 DomLayoutFunction *def = new DomLayoutFunction;
613
614 if (!marginFunction.isEmpty())
615 def->setAttributeMargin(marginFunction);
616 if (!spacingFunction.isEmpty())
617 def->setAttributeSpacing(spacingFunction);
618 ui->setElementLayoutFunction(def);
619 }
620
621 QString pixFunction = m_formWindow->pixmapFunction();
622 if (!pixFunction.isEmpty()) {
623 ui->setElementPixmapFunction(pixFunction);
624 }
625
626 if (QDesignerExtraInfoExtension *extra = qt_extension<QDesignerExtraInfoExtension*>(core()->extensionManager(), core()))
627 extra->saveUiExtraInfo(ui);
628
629 if (MetaDataBase *metaDataBase = qobject_cast<MetaDataBase *>(core()->metaDataBase())) {
630 const MetaDataBaseItem *item = metaDataBase->metaDataBaseItem(m_formWindow->mainContainer());
631 const QStringList fakeSlots = item->fakeSlots();
632 const QStringList fakeSignals =item->fakeSignals();
633 if (!fakeSlots.isEmpty() || !fakeSignals.isEmpty()) {
634 DomSlots *domSlots = new DomSlots();
635 domSlots->setElementSlot(fakeSlots);
636 domSlots->setElementSignal(fakeSignals);
637 ui->setElementSlots(domSlots);
638 }
639 }
640}
641
642QWidget *QDesignerResource::load(QIODevice *dev, QWidget *parentWidget)
643{
644 std::unique_ptr<DomUI> ui(readUi(dev));
645 return ui ? loadUi(ui.get(), parentWidget) : nullptr;
646}
647
648QWidget *QDesignerResource::loadUi(DomUI *ui, QWidget *parentWidget)
649{
650 QWidget *widget = create(ui, parentWidget);
651 // Store the class name as 'reset' value for the main container's object name.
652 if (widget)
653 widget->setProperty("_q_classname", widget->objectName());
654 else if (d->m_errorString.isEmpty())
655 d->m_errorString = QFormBuilderExtra::msgInvalidUiFile();
656 return widget;
657}
658
660{
661 return m_resourceBuilder->isSaveRelative();
662}
663
665{
666 m_resourceBuilder->setSaveRelative(relative);
667}
668
669QWidget *QDesignerResource::create(DomUI *ui, QWidget *parentWidget)
670{
671 // Load extra info extension. This is used by Jambi for preventing
672 // C++ UI files from being loaded
673 if (QDesignerExtraInfoExtension *extra = qt_extension<QDesignerExtraInfoExtension*>(core()->extensionManager(), core())) {
674 if (!extra->loadUiExtraInfo(ui)) {
675 const QString errorMessage = QApplication::translate("Designer", "This file cannot be read because the extra info extension failed to load.");
676 core()->dialogGui()->message(parentWidget->window(), QDesignerDialogGuiInterface::FormLoadFailureMessage,
677 QMessageBox::Warning, messageBoxTitle(), errorMessage, QMessageBox::Ok);
678 return nullptr;
679 }
680 }
681
682 qdesigner_internal::WidgetFactory *factory = qobject_cast<qdesigner_internal::WidgetFactory*>(core()->widgetFactory());
683 Q_ASSERT(factory != nullptr);
684
685 QDesignerFormWindowInterface *previousFormWindow = factory->currentFormWindow(m_formWindow);
686
687 m_isMainWidget = true;
688 QDesignerWidgetItemInstaller wii; // Make sure we use QDesignerWidgetItem.
689 QWidget *mainWidget = QAbstractFormBuilder::create(ui, parentWidget);
690
691 if (m_formWindow) {
692 m_formWindow->setUseIdBasedTranslations(ui->attributeIdbasedtr());
693 m_formWindow->setIdBasedTranslationLabel(ui->attributeLabel());
694 // Default to true unless set.
695 const bool connectSlotsByName = !ui->hasAttributeConnectslotsbyname() || ui->attributeConnectslotsbyname();
696 m_formWindow->setConnectSlotsByName(connectSlotsByName);
697 }
698
699 if (mainWidget && m_formWindow) {
700 m_formWindow->setAuthor(ui->elementAuthor());
701 m_formWindow->setComment(ui->elementComment());
702 m_formWindow->setExportMacro(ui->elementExportMacro());
703
704 // Designer data
705 QVariantMap designerFormData;
706 if (ui->hasElementDesignerdata()) {
707 const DomPropertyList domPropertyList = ui->elementDesignerdata()->elementProperty();
708 for (auto *prop : domPropertyList) {
709 const QVariant vprop = domPropertyToVariant(this, mainWidget->metaObject(), prop);
710 if (vprop.metaType().id() != QMetaType::UnknownType)
711 designerFormData.insert(prop->attributeName(), vprop);
712 }
713 }
714 m_formWindow->setFormData(designerFormData);
715
716 m_formWindow->setPixmapFunction(ui->elementPixmapFunction());
717
718 if (DomLayoutDefault *def = ui->elementLayoutDefault()) {
719 m_formWindow->setLayoutDefault(def->attributeMargin(), def->attributeSpacing());
720 }
721
722 if (DomLayoutFunction *fun = ui->elementLayoutFunction()) {
723 m_formWindow->setLayoutFunction(fun->attributeMargin(), fun->attributeSpacing());
724 }
725
726 if (DomIncludes *includes = ui->elementIncludes()) {
727 const auto global = "global"_L1;
728 QStringList includeHints;
729 const auto &elementInclude = includes->elementInclude();
730 for (DomInclude *incl : elementInclude) {
731 QString text = incl->text();
732
733 if (text.isEmpty())
734 continue;
735
736 if (incl->hasAttributeLocation() && incl->attributeLocation() == global ) {
737 text.prepend(u'<');
738 text.append(u'>');
739 } else {
740 text.prepend(u'"');
741 text.append(u'"');
742 }
743
744 includeHints.append(text);
745 }
746
747 m_formWindow->setIncludeHints(includeHints);
748 }
749
750 // Register all button groups the form builder adds as children of the main container for them to be found
751 // in the signal slot editor
752 auto *mdb = core()->metaDataBase();
753 for (auto *child : mainWidget->children()) {
754 if (QButtonGroup *bg = qobject_cast<QButtonGroup*>(child))
755 mdb->add(bg);
756 }
757 // Load tools
758 for (int index = 0; index < m_formWindow->toolCount(); ++index) {
759 QDesignerFormWindowToolInterface *tool = m_formWindow->tool(index);
760 Q_ASSERT(tool != nullptr);
761 tool->loadFromDom(ui, mainWidget);
762 }
763 }
764
765 factory->currentFormWindow(previousFormWindow);
766
767 if (const DomSlots *domSlots = ui->elementSlots()) {
768 if (MetaDataBase *metaDataBase = qobject_cast<MetaDataBase *>(core()->metaDataBase())) {
769 QStringList fakeSlots;
770 QStringList fakeSignals;
771 if (addFakeMethods(domSlots, fakeSlots, fakeSignals)) {
772 MetaDataBaseItem *item = metaDataBase->metaDataBaseItem(mainWidget);
773 item->setFakeSlots(fakeSlots);
774 item->setFakeSignals(fakeSignals);
775 }
776 }
777 }
778 if (mainWidget) {
779 // Initialize the mainwindow geometry. Has it been explicitly specified?
780 bool hasExplicitGeometry = false;
781 const auto &properties = ui->elementWidget()->elementProperty();
782 if (!properties.isEmpty()) {
783 for (const DomProperty *p : properties) {
784 if (p->attributeName() == "geometry"_L1) {
785 hasExplicitGeometry = true;
786 break;
787 }
788 }
789 }
790 if (hasExplicitGeometry) {
791 // Geometry was specified explicitly: Verify that smartMinSize is respected
792 // (changed fonts, label wrapping policies, etc). This does not happen automatically in docked mode.
793 const QSize size = mainWidget->size();
794 const QSize minSize = size.expandedTo(qSmartMinSize(mainWidget));
795 if (minSize != size)
796 mainWidget->resize(minSize);
797 } else {
798 // No explicit Geometry: perform an adjustSize() to resize the form correctly before embedding it into a container
799 // (which might otherwise squeeze the form)
800 mainWidget->adjustSize();
801 }
802 // Some integration wizards create forms with main containers
803 // based on derived classes of QWidget and load them into Designer
804 // without the plugin existing. This will trigger the auto-promotion
805 // mechanism of Designer, which will set container=false for
806 // QWidgets. For the main container, force container=true and warn.
807 const QDesignerWidgetDataBaseInterface *wdb = core()->widgetDataBase();
808 const int wdbIndex = wdb->indexOfObject(mainWidget);
809 if (wdbIndex != -1) {
810 QDesignerWidgetDataBaseItemInterface *item = wdb->item(wdbIndex);
811 // Promoted main container that is not of container type
812 if (item->isPromoted() && !item->isContainer()) {
813 item->setContainer(true);
814 qWarning("** WARNING The form's main container is an unknown custom widget '%s'."
815 " Defaulting to a promoted instance of '%s', assuming container.",
816 item->name().toUtf8().constData(), item->extends().toUtf8().constData());
817 }
818 }
819 }
820 return mainWidget;
821}
822
823QWidget *QDesignerResource::create(DomWidget *ui_widget, QWidget *parentWidget)
824{
825 const QString className = ui_widget->attributeClass();
826 if (!m_isMainWidget && className == "QWidget"_L1
827 && !ui_widget->elementLayout().isEmpty()
828 && !ui_widget->hasAttributeNative()) {
829 // ### check if elementLayout.size() == 1
830
831 QDesignerContainerExtension *container = qt_extension<QDesignerContainerExtension*>(core()->extensionManager(), parentWidget);
832
833 if (container == nullptr) {
834 // generate a QLayoutWidget iff the parent is not an QDesignerContainerExtension.
835 ui_widget->setAttributeClass(u"QLayoutWidget"_s);
836 }
837 }
838
839 // save the actions
840 const auto &actionRefs = ui_widget->elementAddAction();
841 ui_widget->setElementAddAction(QList<DomActionRef *>());
842
843 QWidget *w = QAbstractFormBuilder::create(ui_widget, parentWidget);
844
845 // restore the actions
846 ui_widget->setElementAddAction(actionRefs);
847
848 if (w == nullptr)
849 return nullptr;
850
851 // ### generalize using the extension manager
852 QDesignerMenu *menu = qobject_cast<QDesignerMenu*>(w);
853 QDesignerMenuBar *menuBar = qobject_cast<QDesignerMenuBar*>(w);
854
855 if (menu)
856 menu->hide();
857
858 for (DomActionRef *ui_action_ref : actionRefs) {
859 const QString name = ui_action_ref->attributeName();
860 if (name == "separator"_L1) {
861 QAction *sep = new QAction(w);
862 sep->setSeparator(true);
863 w->addAction(sep);
864 addMenuAction(sep);
865 } else if (QAction *a = d->m_actions.value(name)) {
866 w->addAction(a);
867 } else if (QActionGroup *g = d->m_actionGroups.value(name)) {
868 w->addActions(g->actions());
869 } else if (QMenu *menu = w->findChild<QMenu*>(name)) {
870 w->addAction(menu->menuAction());
871 addMenuAction(menu->menuAction());
872 }
873 }
874
875 if (menu)
876 menu->adjustSpecialActions();
877 else if (menuBar)
878 menuBar->adjustSpecialActions();
879
880 ui_widget->setAttributeClass(className); // fix the class name
881 applyExtensionDataFromDOM(this, core(), ui_widget, w);
882
883 return w;
884}
885
886QLayout *QDesignerResource::create(DomLayout *ui_layout, QLayout *layout, QWidget *parentWidget)
887{
888 QLayout *l = QAbstractFormBuilder::create(ui_layout, layout, parentWidget);
889
890 if (QGridLayout *gridLayout = qobject_cast<QGridLayout*>(l)) {
891 QLayoutSupport::createEmptyCells(gridLayout);
892 } else {
893 if (QFormLayout *formLayout = qobject_cast<QFormLayout*>(l))
894 QLayoutSupport::createEmptyCells(formLayout);
895 }
896 // While the actual values are applied by the form builder, we still need
897 // to mark them as 'changed'.
898 LayoutPropertySheet::markChangedStretchProperties(core(), l, ui_layout);
899 return l;
900}
901
902QLayoutItem *QDesignerResource::create(DomLayoutItem *ui_layoutItem, QLayout *layout, QWidget *parentWidget)
903{
904 if (ui_layoutItem->kind() == DomLayoutItem::Spacer) {
905 const DomSpacer *domSpacer = ui_layoutItem->elementSpacer();
906 Spacer *spacer = static_cast<Spacer*>(core()->widgetFactory()->createWidget(u"Spacer"_s, parentWidget));
907 if (domSpacer->hasAttributeName())
908 changeObjectName(spacer, domSpacer->attributeName());
909 core()->metaDataBase()->add(spacer);
910
911 spacer->setInteractiveMode(false);
912 applyProperties(spacer, ui_layoutItem->elementSpacer()->elementProperty());
913 spacer->setInteractiveMode(true);
914
915 if (m_formWindow) {
916 m_formWindow->manageWidget(spacer);
917 if (QDesignerPropertySheetExtension *sheet = qt_extension<QDesignerPropertySheetExtension*>(core()->extensionManager(), spacer))
918 sheet->setChanged(sheet->indexOf(u"orientation"_s), true);
919 }
920
921 return new QWidgetItem(spacer);
922 }
923 if (ui_layoutItem->kind() == DomLayoutItem::Layout && parentWidget) {
924 DomLayout *ui_layout = ui_layoutItem->elementLayout();
925 QLayoutWidget *layoutWidget = new QLayoutWidget(m_formWindow, parentWidget);
926 core()->metaDataBase()->add(layoutWidget);
927 if (m_formWindow)
928 m_formWindow->manageWidget(layoutWidget);
929 (void) create(ui_layout, nullptr, layoutWidget);
930 return new QWidgetItem(layoutWidget);
931 }
932 return QAbstractFormBuilder::create(ui_layoutItem, layout, parentWidget);
933}
934
935void QDesignerResource::changeObjectName(QObject *o, QString objName)
936{
937 m_formWindow->unify(o, objName, true);
938 o->setObjectName(objName);
939
940}
941
942/* If the property is a enum or flag value, retrieve
943 * the existing enum/flag via property sheet and use it to convert */
944
945static bool readDomEnumerationValue(const DomProperty *p,
946 const QDesignerPropertySheetExtension* sheet, int index,
947 QVariant &v)
948{
949 switch (p->kind()) {
950 case DomProperty::Set: {
951 const QVariant sheetValue = sheet->property(index);
952 if (sheetValue.canConvert<PropertySheetFlagValue>()) {
953 const PropertySheetFlagValue f = qvariant_cast<PropertySheetFlagValue>(sheetValue);
954 bool ok = false;
955 v = f.metaFlags.parseFlags(p->elementSet(), &ok);
956 if (!ok)
957 designerWarning(f.metaFlags.messageParseFailed(p->elementSet()));
958 return true;
959 }
960 }
961 break;
962 case DomProperty::Enum: {
963 const QVariant sheetValue = sheet->property(index);
964 if (sheetValue.canConvert<PropertySheetEnumValue>()) {
965 const PropertySheetEnumValue e = qvariant_cast<PropertySheetEnumValue>(sheetValue);
966 bool ok = false;
967 v = e.metaEnum.parseEnum(p->elementEnum(), &ok);
968 if (!ok)
969 designerWarning(e.metaEnum.messageParseFailed(p->elementEnum()));
970 return true;
971 }
972 }
973 break;
974 default:
975 break;
976 }
977 return false;
978}
979
980// ### fixme Qt 7 remove this: Exclude deprecated properties of Qt 5.
981static bool isDeprecatedQt5Property(const QObject *o, const DomProperty *p)
982{
983 const QString &propertyName = p->attributeName();
984 switch (p->kind()) {
985 case DomProperty::Set:
986 if (propertyName == u"features" && o->inherits("QDockWidget")
987 && p->elementSet() == u"QDockWidget::AllDockWidgetFeatures") {
988 return true;
989 }
990 break;
991 case DomProperty::Enum:
992 if (propertyName == u"sizeAdjustPolicy" && o->inherits("QComboBox")
993 && p->elementEnum() == u"QComboBox::AdjustToMinimumContentsLength") {
994 return true;
995 }
996 break;
997 default:
998 break;
999 }
1000 return false;
1001}
1002
1003void QDesignerResource::applyProperties(QObject *o, const QList<DomProperty*> &properties)
1004{
1005 if (properties.isEmpty())
1006 return;
1007
1008 auto *sheet = qt_extension<QDesignerPropertySheetExtension*>(core()->extensionManager(), o);
1009 if (!sheet)
1010 return;
1011
1012 auto *dynamicSheet = qt_extension<QDesignerDynamicPropertySheetExtension*>(core()->extensionManager(), o);
1013 if (dynamicSheet != nullptr && !dynamicSheet->dynamicPropertiesAllowed())
1014 dynamicSheet = nullptr;
1015
1016 for (DomProperty *p : properties) {
1017 if (isDeprecatedQt5Property(o, p)) // ### fixme Qt 7 remove this
1018 continue; // ### fixme Qt 7 remove this: Exclude deprecated value of Qt 5.
1019 const QString &propertyName = p->attributeName();
1020 if (propertyName == "numDigits"_L1 && o->inherits("QLCDNumber")) { // Deprecated in Qt 4, removed in Qt 5.
1021 applyProperty(o, p, u"digitCount"_s, sheet, dynamicSheet);
1022#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0) // Qt 6 reading Qt 7 forms
1023 } else if (propertyName == "horizontalSizeConstraint"_L1 && o->inherits("QLayout")) {
1024 applyProperty(o, p, u"sizeConstraint"_s, sheet, dynamicSheet);
1025#else // Qt 7 reading pre Qt 7 forms
1026 } else if (propertyName == "sizeConstraint"_L1 && o->inherits("QLayout")) {
1027 applyProperty(o, p, u"horizontalSizeConstraint"_s, sheet, dynamicSheet);
1028 applyProperty(o, p, u"verticalSizeConstraint"_s, sheet, dynamicSheet);
1029#endif
1030 } else {
1031 applyProperty(o, p, propertyName, sheet, dynamicSheet);
1032 }
1033 }
1034}
1035
1036void QDesignerResource::applyProperty(QObject *o, DomProperty* p, const QString &propertyName,
1039{
1040 const int index = sheet->indexOf(propertyName);
1041 QVariant v;
1042 if (!readDomEnumerationValue(p, sheet, index, v))
1043 v = toVariant(o->metaObject(), p);
1044 switch (p->kind()) {
1045 case DomProperty::String:
1046 if (index != -1 && sheet->property(index).userType() == qMetaTypeId<PropertySheetKeySequenceValue>()) {
1047 const DomString *key = p->elementString();
1048 PropertySheetKeySequenceValue keyVal(QKeySequence(key->text()));
1049 translationParametersFromDom(key, &keyVal);
1050 v = QVariant::fromValue(keyVal);
1051 } else {
1052 const DomString *str = p->elementString();
1053 PropertySheetStringValue strVal(v.toString());
1054 translationParametersFromDom(str, &strVal);
1055 v = QVariant::fromValue(strVal);
1056 }
1057 break;
1058 case DomProperty::StringList: {
1059 const DomStringList *list = p->elementStringList();
1060 PropertySheetStringListValue listValue(list->elementString());
1061 translationParametersFromDom(list, &listValue);
1062 v = QVariant::fromValue(listValue);
1063 }
1064 break;
1065 default:
1066 break;
1067 }
1068 d->applyPropertyInternally(o, propertyName, v);
1069 if (index != -1) {
1070 sheet->setProperty(index, v);
1071 sheet->setChanged(index, true);
1072 } else if (dynamicSheet != nullptr) {
1073 QVariant defaultValue = QVariant(v.metaType());
1074 bool isDefault = (v == defaultValue);
1075 if (v.canConvert<PropertySheetIconValue>()) {
1076 defaultValue = QVariant(QMetaType(QMetaType::QIcon));
1077 isDefault = (qvariant_cast<PropertySheetIconValue>(v) == PropertySheetIconValue());
1078 } else if (v.canConvert<PropertySheetPixmapValue>()) {
1079 defaultValue = QVariant(QMetaType(QMetaType::QPixmap));
1080 isDefault = (qvariant_cast<PropertySheetPixmapValue>(v) == PropertySheetPixmapValue());
1081 } else if (v.canConvert<PropertySheetStringValue>()) {
1082 defaultValue = QVariant(QMetaType(QMetaType::QString));
1083 isDefault = (qvariant_cast<PropertySheetStringValue>(v) == PropertySheetStringValue());
1084 } else if (v.canConvert<PropertySheetStringListValue>()) {
1085 defaultValue = QVariant(QMetaType(QMetaType::QStringList));
1086 isDefault = (qvariant_cast<PropertySheetStringListValue>(v) == PropertySheetStringListValue());
1087 } else if (v.canConvert<PropertySheetKeySequenceValue>()) {
1088 defaultValue = QVariant(QMetaType(QMetaType::QKeySequence));
1089 isDefault = (qvariant_cast<PropertySheetKeySequenceValue>(v) == PropertySheetKeySequenceValue());
1090 }
1091 if (defaultValue.metaType().id() != QMetaType::User) {
1092 const int idx = dynamicSheet->addDynamicProperty(p->attributeName(), defaultValue);
1093 if (idx != -1) {
1094 sheet->setProperty(idx, v);
1095 sheet->setChanged(idx, !isDefault);
1096 }
1097 }
1098 }
1099
1100 if (propertyName == "objectName"_L1)
1101 changeObjectName(o, o->objectName());
1102}
1103
1104QWidget *QDesignerResource::createWidget(const QString &widgetName, QWidget *parentWidget, const QString &_name)
1105{
1106 QString name = _name;
1107 if (m_isMainWidget)
1108 m_isMainWidget = false;
1109
1110 QWidget *w = core()->widgetFactory()->createWidget(widgetName, parentWidget);
1111 if (!w)
1112 return nullptr;
1113
1114 if (name.isEmpty()) {
1115 QDesignerWidgetDataBaseInterface *db = core()->widgetDataBase();
1116 if (QDesignerWidgetDataBaseItemInterface *item = db->item(db->indexOfObject(w)))
1117 name = qtify(item->name());
1118 }
1119
1120 changeObjectName(w, name);
1121
1122 QDesignerContainerExtension *container = qt_extension<QDesignerContainerExtension*>(core()->extensionManager(), parentWidget);
1123 if (!qobject_cast<QMenu*>(w) && (!parentWidget || !container)) {
1124 m_formWindow->manageWidget(w);
1125 if (parentWidget) {
1126 QWidgetList list = qvariant_cast<QWidgetList>(parentWidget->property("_q_widgetOrder"));
1127 list.append(w);
1128 parentWidget->setProperty("_q_widgetOrder", QVariant::fromValue(list));
1129 QWidgetList zOrder = qvariant_cast<QWidgetList>(parentWidget->property("_q_zOrder"));
1130 zOrder.append(w);
1131 parentWidget->setProperty("_q_zOrder", QVariant::fromValue(zOrder));
1132 }
1133 } else {
1134 core()->metaDataBase()->add(w);
1135 }
1136
1137 w->setWindowFlags(w->windowFlags() & ~Qt::Window);
1138 // Make sure it is non-modal (for example, KDialog calls setModal(true) in the constructor).
1139 w->setWindowModality(Qt::NonModal);
1140
1141 return w;
1142}
1143
1144QLayout *QDesignerResource::createLayout(const QString &layoutName, QObject *parent, const QString &name)
1145{
1146 QWidget *layoutBase = nullptr;
1147 QLayout *layout = qobject_cast<QLayout*>(parent);
1148
1149 if (parent->isWidgetType())
1150 layoutBase = static_cast<QWidget*>(parent);
1151 else {
1152 Q_ASSERT( layout != nullptr );
1153 layoutBase = layout->parentWidget();
1154 }
1155
1156 LayoutInfo::Type layoutType = LayoutInfo::layoutType(layoutName);
1157 if (layoutType == LayoutInfo::NoLayout) {
1158 designerWarning(QCoreApplication::translate("QDesignerResource", "The layout type '%1' is not supported, defaulting to grid.").arg(layoutName));
1159 layoutType = LayoutInfo::Grid;
1160 }
1161 QLayout *lay = core()->widgetFactory()->createLayout(layoutBase, layout, layoutType);
1162 if (lay != nullptr)
1163 changeObjectName(lay, name);
1164
1165 return lay;
1166}
1167
1168// save
1169DomWidget *QDesignerResource::createDom(QWidget *widget, DomWidget *ui_parentWidget, bool recursive)
1170{
1171 QDesignerMetaDataBaseItemInterface *item = core()->metaDataBase()->item(widget);
1172 if (!item)
1173 return nullptr;
1174
1175 if (qobject_cast<Spacer*>(widget) && !m_copyWidget)
1176 return nullptr;
1177
1178 const QDesignerWidgetDataBaseInterface *wdb = core()->widgetDataBase();
1179 QDesignerWidgetDataBaseItemInterface *widgetInfo = nullptr;
1180 const int widgetInfoIndex = wdb->indexOfObject(widget, false);
1181 if (widgetInfoIndex != -1) {
1182 widgetInfo = wdb->item(widgetInfoIndex);
1183 // Recursively add all dependent custom widgets
1184 QDesignerWidgetDataBaseItemInterface *customInfo = widgetInfo;
1185 while (customInfo && customInfo->isCustom()) {
1186 m_usedCustomWidgets.insert(customInfo, true);
1187 const QString extends = customInfo->extends();
1188 if (extends == customInfo->name())
1189 break; // There are faulty files around that have name==extends
1190 const int extendsIndex = wdb->indexOfClassName(customInfo->extends());
1191 customInfo = extendsIndex != -1 ? wdb->item(extendsIndex) : nullptr;
1192 }
1193 }
1194
1195 DomWidget *w = nullptr;
1196
1197 if (QTabWidget *tabWidget = qobject_cast<QTabWidget*>(widget))
1198 w = saveWidget(tabWidget, ui_parentWidget);
1199 else if (QStackedWidget *stackedWidget = qobject_cast<QStackedWidget*>(widget))
1200 w = saveWidget(stackedWidget, ui_parentWidget);
1201 else if (QToolBox *toolBox = qobject_cast<QToolBox*>(widget))
1202 w = saveWidget(toolBox, ui_parentWidget);
1203 else if (QToolBar *toolBar = qobject_cast<QToolBar*>(widget))
1204 w = saveWidget(toolBar, ui_parentWidget);
1205 else if (QDesignerDockWidget *dockWidget = qobject_cast<QDesignerDockWidget*>(widget))
1206 w = saveWidget(dockWidget, ui_parentWidget);
1207 else if (QDesignerContainerExtension *container = qt_extension<QDesignerContainerExtension*>(core()->extensionManager(), widget))
1208 w = saveWidget(widget, container, ui_parentWidget);
1209 else if (QWizardPage *wizardPage = qobject_cast<QWizardPage*>(widget))
1210 w = saveWidget(wizardPage, ui_parentWidget);
1211 else
1212 w = QAbstractFormBuilder::createDom(widget, ui_parentWidget, recursive);
1213
1214 Q_ASSERT( w != nullptr );
1215
1216 if (!qobject_cast<QLayoutWidget*>(widget) && w->attributeClass() == "QWidget"_L1)
1217 w->setAttributeNative(true);
1218
1219 const QString className = w->attributeClass();
1220 if (m_internal_to_qt.contains(className))
1221 w->setAttributeClass(m_internal_to_qt.value(className));
1222
1223 if (isPromoted( core(), widget)) { // is promoted?
1224 Q_ASSERT(widgetInfo != nullptr);
1225
1226 w->setAttributeClass(widgetInfo->name());
1227
1228 const auto &prop_list = w->elementProperty();
1229 for (DomProperty *prop : prop_list) {
1230 if (prop->attributeName() == "geometry"_L1) {
1231 if (DomRect *rect = prop->elementRect()) {
1232 rect->setElementX(widget->x());
1233 rect->setElementY(widget->y());
1234 }
1235 break;
1236 }
1237 }
1238 } else if (widgetInfo != nullptr && m_usedCustomWidgets.contains(widgetInfo)) {
1239 if (widgetInfo->name() != w->attributeClass())
1240 w->setAttributeClass(widgetInfo->name());
1241 }
1242 addExtensionDataToDOM(this, core(), w, widget);
1243 return w;
1244}
1245
1246DomLayout *QDesignerResource::createDom(QLayout *layout, DomLayout *ui_parentLayout, DomWidget *ui_parentWidget)
1247{
1248 QDesignerMetaDataBaseItemInterface *item = core()->metaDataBase()->item(layout);
1249
1250 if (item == nullptr) {
1251 layout = layout->findChild<QLayout*>();
1252 // refresh the meta database item
1253 item = core()->metaDataBase()->item(layout);
1254 }
1255
1256 if (item == nullptr) {
1257 // nothing to do.
1258 return nullptr;
1259 }
1260
1261 if (qobject_cast<QSplitter*>(layout->parentWidget()) != 0) {
1262 // nothing to do.
1263 return nullptr;
1264 }
1265
1266 m_chain.push(layout);
1267
1268 DomLayout *l = QAbstractFormBuilder::createDom(layout, ui_parentLayout, ui_parentWidget);
1269 Q_ASSERT(l != nullptr);
1270 LayoutPropertySheet::stretchAttributesToDom(core(), layout, l);
1271
1272 m_chain.pop();
1273
1274 return l;
1275}
1276
1277DomLayoutItem *QDesignerResource::createDom(QLayoutItem *item, DomLayout *ui_layout, DomWidget *ui_parentWidget)
1278{
1279 DomLayoutItem *ui_item = nullptr;
1280
1281 if (Spacer *s = qobject_cast<Spacer*>(item->widget())) {
1282 if (!core()->metaDataBase()->item(s))
1283 return nullptr;
1284
1285 DomSpacer *spacer = new DomSpacer();
1286 const QString objectName = s->objectName();
1287 if (!objectName.isEmpty())
1288 spacer->setAttributeName(objectName);
1289 // ### filter the properties
1290 spacer->setElementProperty(computeProperties(item->widget()));
1291
1292 ui_item = new DomLayoutItem();
1293 ui_item->setElementSpacer(spacer);
1294 d->m_laidout.insert(item->widget(), true);
1295 } else if (QLayoutWidget *layoutWidget = qobject_cast<QLayoutWidget*>(item->widget())) {
1296 // Do not save a QLayoutWidget if it is within a layout (else it is saved as "QWidget"
1297 Q_ASSERT(layoutWidget->layout());
1298 DomLayout *l = createDom(layoutWidget->layout(), ui_layout, ui_parentWidget);
1299 ui_item = new DomLayoutItem();
1300 ui_item->setElementLayout(l);
1301 d->m_laidout.insert(item->widget(), true);
1302 } else if (!item->spacerItem()) { // we use spacer as fake item in the Designer
1303 ui_item = QAbstractFormBuilder::createDom(item, ui_layout, ui_parentWidget);
1304 } else {
1305 return nullptr;
1306 }
1307 return ui_item;
1308}
1309
1310void QDesignerResource::createCustomWidgets(DomCustomWidgets *dom_custom_widgets)
1311{
1312 QSimpleResource::handleDomCustomWidgets(core(), dom_custom_widgets);
1313}
1314
1316{
1317 QDesignerMetaDataBaseItemInterface *item = core()->metaDataBase()->item(m_formWindow);
1318 Q_ASSERT(item);
1319
1320 QStringList tabStops;
1321 const QWidgetList &tabOrder = item->tabOrder();
1322 for (QWidget *widget : tabOrder) {
1323 if (m_formWindow->mainContainer()->isAncestorOf(widget))
1324 tabStops.append(widget->objectName());
1325 }
1326
1327 if (!tabStops.isEmpty()) {
1328 DomTabStops *dom = new DomTabStops;
1329 dom->setElementTabStop(tabStops);
1330 return dom;
1331 }
1332
1333 return nullptr;
1334}
1335
1336void QDesignerResource::applyTabStops(QWidget *widget, DomTabStops *tabStops)
1337{
1338 if (tabStops == nullptr || widget == nullptr)
1339 return;
1340
1341 QWidgetList tabOrder;
1342 const QStringList &elementTabStop = tabStops->elementTabStop();
1343 for (const QString &widgetName : elementTabStop) {
1344 if (QWidget *w = widget->findChild<QWidget*>(widgetName)) {
1345 tabOrder.append(w);
1346 }
1347 }
1348
1349 QDesignerMetaDataBaseItemInterface *item = core()->metaDataBase()->item(m_formWindow);
1350 Q_ASSERT(item);
1351 item->setTabOrder(tabOrder);
1352}
1353
1354/* Unmanaged container pages occur when someone adds a page in a custom widget
1355 * constructor. They don't have a meta DB entry which causes createDom
1356 * to return 0. */
1357inline QString msgUnmanagedPage(QDesignerFormEditorInterface *core,
1358 QWidget *container, int index, QWidget *page)
1359{
1360 return QCoreApplication::translate("QDesignerResource",
1361"The container extension of the widget '%1' (%2) returned a widget not managed by Designer '%3' (%4) when queried for page #%5.\n"
1362"Container pages should only be added by specifying them in XML returned by the domXml() method of the custom widget.").
1363 arg(container->objectName(), WidgetFactory::classNameOf(core, container),
1364 page->objectName(), WidgetFactory::classNameOf(core, page)).
1365 arg(index);
1366}
1367
1368DomWidget *QDesignerResource::saveWidget(QWidget *widget, QDesignerContainerExtension *container, DomWidget *ui_parentWidget)
1369{
1370 DomWidget *ui_widget = QAbstractFormBuilder::createDom(widget, ui_parentWidget, false);
1371 QList<DomWidget *> ui_widget_list;
1372
1373 for (int i=0; i<container->count(); ++i) {
1374 QWidget *page = container->widget(i);
1375 Q_ASSERT(page);
1376
1377 if (DomWidget *ui_page = createDom(page, ui_widget)) {
1378 ui_widget_list.append(ui_page);
1379 } else {
1380 designerWarning(msgUnmanagedPage(core(), widget, i, page));
1381 }
1382 }
1383
1384 ui_widget->setElementWidget(ui_widget_list);
1385
1386 return ui_widget;
1387}
1388
1389DomWidget *QDesignerResource::saveWidget(QStackedWidget *widget, DomWidget *ui_parentWidget)
1390{
1391 DomWidget *ui_widget = QAbstractFormBuilder::createDom(widget, ui_parentWidget, false);
1392 QList<DomWidget *> ui_widget_list;
1393 if (QDesignerContainerExtension *container = qt_extension<QDesignerContainerExtension*>(core()->extensionManager(), widget)) {
1394 for (int i=0; i<container->count(); ++i) {
1395 QWidget *page = container->widget(i);
1396 Q_ASSERT(page);
1397 if (DomWidget *ui_page = createDom(page, ui_widget)) {
1398 ui_widget_list.append(ui_page);
1399 } else {
1400 designerWarning(msgUnmanagedPage(core(), widget, i, page));
1401 }
1402 }
1403 }
1404
1405 ui_widget->setElementWidget(ui_widget_list);
1406
1407 return ui_widget;
1408}
1409
1410DomWidget *QDesignerResource::saveWidget(QToolBar *toolBar, DomWidget *ui_parentWidget)
1411{
1412 DomWidget *ui_widget = QAbstractFormBuilder::createDom(toolBar, ui_parentWidget, false);
1413 if (const QMainWindow *mainWindow = qobject_cast<QMainWindow*>(toolBar->parentWidget())) {
1414 const bool toolBarBreak = mainWindow->toolBarBreak(toolBar);
1415 const Qt::ToolBarArea area = mainWindow->toolBarArea(toolBar);
1416
1417 auto attributes = ui_widget->elementAttribute();
1418
1419 DomProperty *attr = new DomProperty();
1420 attr->setAttributeName(u"toolBarArea"_s);
1421 attr->setElementEnum(QLatin1StringView(toolBarAreaMetaEnum().valueToKey(area)));
1422 attributes << attr;
1423
1424 attr = new DomProperty();
1425 attr->setAttributeName(u"toolBarBreak"_s);
1426 attr->setElementBool(toolBarBreak ? u"true"_s : u"false"_s);
1427 attributes << attr;
1428 ui_widget->setElementAttribute(attributes);
1429 }
1430
1431 return ui_widget;
1432}
1433
1434DomWidget *QDesignerResource::saveWidget(QDesignerDockWidget *dockWidget, DomWidget *ui_parentWidget)
1435{
1436 DomWidget *ui_widget = QAbstractFormBuilder::createDom(dockWidget, ui_parentWidget, true);
1437 if (QMainWindow *mainWindow = qobject_cast<QMainWindow*>(dockWidget->parentWidget())) {
1438 const Qt::DockWidgetArea area = mainWindow->dockWidgetArea(dockWidget);
1439 DomProperty *attr = new DomProperty();
1440 attr->setAttributeName(u"dockWidgetArea"_s);
1441 attr->setElementNumber(int(area));
1442 ui_widget->setElementAttribute(ui_widget->elementAttribute() << attr);
1443 }
1444
1445 return ui_widget;
1446}
1447
1448DomWidget *QDesignerResource::saveWidget(QTabWidget *widget, DomWidget *ui_parentWidget)
1449{
1450 DomWidget *ui_widget = QAbstractFormBuilder::createDom(widget, ui_parentWidget, false);
1451 QList<DomWidget *> ui_widget_list;
1452
1453 if (QDesignerContainerExtension *container = qt_extension<QDesignerContainerExtension*>(core()->extensionManager(), widget)) {
1454 const int current = widget->currentIndex();
1455 for (int i=0; i<container->count(); ++i) {
1456 QWidget *page = container->widget(i);
1457 Q_ASSERT(page);
1458
1459 DomWidget *ui_page = createDom(page, ui_widget);
1460 if (!ui_page) {
1461 designerWarning(msgUnmanagedPage(core(), widget, i, page));
1462 continue;
1463 }
1464 QList<DomProperty*> ui_attribute_list;
1465
1466 // attribute `icon'
1467 widget->setCurrentIndex(i);
1468 QDesignerPropertySheetExtension *sheet = qt_extension<QDesignerPropertySheetExtension*>(core()->extensionManager(), widget);
1469 PropertySheetIconValue icon = qvariant_cast<PropertySheetIconValue>(sheet->property(sheet->indexOf(u"currentTabIcon"_s)));
1470 DomProperty *p = resourceBuilder()->saveResource(workingDirectory(), QVariant::fromValue(icon));
1471 if (p) {
1472 p->setAttributeName(QFormBuilderStrings::iconAttribute);
1473 ui_attribute_list.append(p);
1474 }
1475 // attribute `title'
1476 p = textBuilder()->saveText(sheet->property(sheet->indexOf(u"currentTabText"_s)));
1477 if (p) {
1478 p->setAttributeName(QFormBuilderStrings::titleAttribute);
1479 ui_attribute_list.append(p);
1480 }
1481
1482 // attribute `toolTip'
1483 QVariant v = sheet->property(sheet->indexOf(u"currentTabToolTip"_s));
1484 if (!qvariant_cast<PropertySheetStringValue>(v).value().isEmpty()) {
1485 p = textBuilder()->saveText(v);
1486 if (p) {
1487 p->setAttributeName(QFormBuilderStrings::toolTipAttribute);
1488 ui_attribute_list.append(p);
1489 }
1490 }
1491
1492 // attribute `whatsThis'
1493 v = sheet->property(sheet->indexOf(u"currentTabWhatsThis"_s));
1494 if (!qvariant_cast<PropertySheetStringValue>(v).value().isEmpty()) {
1495 p = textBuilder()->saveText(v);
1496 if (p) {
1497 p->setAttributeName(QFormBuilderStrings::whatsThisAttribute);
1498 ui_attribute_list.append(p);
1499 }
1500 }
1501
1502 ui_page->setElementAttribute(ui_attribute_list);
1503
1504 ui_widget_list.append(ui_page);
1505 }
1506 widget->setCurrentIndex(current);
1507 }
1508
1509 ui_widget->setElementWidget(ui_widget_list);
1510
1511 return ui_widget;
1512}
1513
1514DomWidget *QDesignerResource::saveWidget(QToolBox *widget, DomWidget *ui_parentWidget)
1515{
1516 DomWidget *ui_widget = QAbstractFormBuilder::createDom(widget, ui_parentWidget, false);
1517 QList<DomWidget *> ui_widget_list;
1518
1519 if (QDesignerContainerExtension *container = qt_extension<QDesignerContainerExtension*>(core()->extensionManager(), widget)) {
1520 const int current = widget->currentIndex();
1521 for (int i=0; i<container->count(); ++i) {
1522 QWidget *page = container->widget(i);
1523 Q_ASSERT(page);
1524
1525 DomWidget *ui_page = createDom(page, ui_widget);
1526 if (!ui_page) {
1527 designerWarning(msgUnmanagedPage(core(), widget, i, page));
1528 continue;
1529 }
1530
1531 // attribute `label'
1532 QList<DomProperty*> ui_attribute_list;
1533
1534 // attribute `icon'
1535 widget->setCurrentIndex(i);
1536 QDesignerPropertySheetExtension *sheet = qt_extension<QDesignerPropertySheetExtension*>(core()->extensionManager(), widget);
1537 PropertySheetIconValue icon = qvariant_cast<PropertySheetIconValue>(sheet->property(sheet->indexOf(u"currentItemIcon"_s)));
1538 DomProperty *p = resourceBuilder()->saveResource(workingDirectory(), QVariant::fromValue(icon));
1539 if (p) {
1540 p->setAttributeName(QFormBuilderStrings::iconAttribute);
1541 ui_attribute_list.append(p);
1542 }
1543 p = textBuilder()->saveText(sheet->property(sheet->indexOf(u"currentItemText"_s)));
1544 if (p) {
1545 p->setAttributeName(QFormBuilderStrings::labelAttribute);
1546 ui_attribute_list.append(p);
1547 }
1548
1549 // attribute `toolTip'
1550 QVariant v = sheet->property(sheet->indexOf(u"currentItemToolTip"_s));
1551 if (!qvariant_cast<PropertySheetStringValue>(v).value().isEmpty()) {
1552 p = textBuilder()->saveText(v);
1553 if (p) {
1554 p->setAttributeName(QFormBuilderStrings::toolTipAttribute);
1555 ui_attribute_list.append(p);
1556 }
1557 }
1558
1559 ui_page->setElementAttribute(ui_attribute_list);
1560
1561 ui_widget_list.append(ui_page);
1562 }
1563 widget->setCurrentIndex(current);
1564 }
1565
1566 ui_widget->setElementWidget(ui_widget_list);
1567
1568 return ui_widget;
1569}
1570
1571DomWidget *QDesignerResource::saveWidget(QWizardPage *wizardPage, DomWidget *ui_parentWidget)
1572{
1573 DomWidget *ui_widget = QAbstractFormBuilder::createDom(wizardPage, ui_parentWidget, true);
1574 QDesignerPropertySheetExtension *sheet = qt_extension<QDesignerPropertySheetExtension*>(core()->extensionManager(), wizardPage);
1575 // Save the page id (string) attribute, append to existing attributes
1576 const QString pageIdPropertyName = QLatin1StringView(QWizardPagePropertySheet::pageIdProperty);
1577 const int pageIdIndex = sheet->indexOf(pageIdPropertyName);
1578 if (pageIdIndex != -1 && sheet->isChanged(pageIdIndex)) {
1579 DomProperty *property = variantToDomProperty(this, wizardPage->metaObject(), pageIdPropertyName, sheet->property(pageIdIndex));
1580 Q_ASSERT(property);
1581 property->elementString()->setAttributeNotr(u"true"_s);
1582 DomPropertyList attributes = ui_widget->elementAttribute();
1583 attributes.push_back(property);
1584 ui_widget->setElementAttribute(attributes);
1585 }
1586 return ui_widget;
1587}
1588
1589// Do not save the 'currentTabName' properties of containers
1590static inline bool checkContainerProperty(const QWidget *w, const QString &propertyName)
1591{
1592 if (qobject_cast<const QToolBox *>(w))
1593 return QToolBoxWidgetPropertySheet::checkProperty(propertyName);
1594 if (qobject_cast<const QTabWidget *>(w))
1595 return QTabWidgetPropertySheet::checkProperty(propertyName);
1596 if (qobject_cast<const QStackedWidget *>(w))
1597 return QStackedWidgetPropertySheet::checkProperty(propertyName);
1598 if (qobject_cast<const QMdiArea *>(w))
1599 return QMdiAreaPropertySheet::checkProperty(propertyName);
1600 return true;
1601}
1602
1603bool QDesignerResource::checkProperty(QObject *obj, const QString &prop) const
1604{
1605 const QDesignerMetaObjectInterface *meta = core()->introspection()->metaObject(obj);
1606
1607 const int pindex = meta->indexOfProperty(prop);
1608 if (pindex != -1 && !meta->property(pindex)->attributes().testFlag(QDesignerMetaPropertyInterface::StoredAttribute))
1609 return false;
1610
1611 if (prop == "objectName"_L1 || prop == "spacerName"_L1) // ### don't store the property objectName
1612 return false;
1613
1614 if (!supportsSeparateSizeConstraints(d->m_saveVersion)
1615 && prop == "verticalSizeConstraint"_L1) { // 7.0
1616 return false;
1617 }
1618
1619 QWidget *check_widget = nullptr;
1620 if (obj->isWidgetType())
1621 check_widget = static_cast<QWidget*>(obj);
1622
1623 if (check_widget && prop == "geometry"_L1) {
1624 if (check_widget == m_formWindow->mainContainer())
1625 return true; // Save although maincontainer is technically laid-out by embedding container
1626 if (m_selected && m_selected == check_widget)
1627 return true;
1628
1629 return !LayoutInfo::isWidgetLaidout(core(), check_widget);
1630 }
1631
1632 if (check_widget && !checkContainerProperty(check_widget, prop))
1633 return false;
1634
1635 if (QDesignerPropertySheetExtension *sheet = qt_extension<QDesignerPropertySheetExtension*>(core()->extensionManager(), obj)) {
1636 QDesignerDynamicPropertySheetExtension *dynamicSheet = qt_extension<QDesignerDynamicPropertySheetExtension*>(core()->extensionManager(), obj);
1637 const int pindex = sheet->indexOf(prop);
1638 if (sheet->isAttribute(pindex))
1639 return false;
1640
1641 if (!dynamicSheet || !dynamicSheet->isDynamicProperty(pindex))
1642 return sheet->isChanged(pindex);
1643 if (!sheet->isVisible(pindex))
1644 return false;
1645 return true;
1646 }
1647
1648 return false;
1649}
1650
1651bool QDesignerResource::addItem(DomLayoutItem *ui_item, QLayoutItem *item, QLayout *layout)
1652{
1653 if (item->widget() == nullptr) {
1654 return false;
1655 }
1656
1657 QGridLayout *grid = qobject_cast<QGridLayout*>(layout);
1658 QBoxLayout *box = qobject_cast<QBoxLayout*>(layout);
1659
1660 if (grid != nullptr) {
1661 const int rowSpan = ui_item->hasAttributeRowSpan() ? ui_item->attributeRowSpan() : 1;
1662 const int colSpan = ui_item->hasAttributeColSpan() ? ui_item->attributeColSpan() : 1;
1663 grid->addWidget(item->widget(), ui_item->attributeRow(), ui_item->attributeColumn(), rowSpan, colSpan, item->alignment());
1664 return true;
1665 }
1666 if (box != nullptr) {
1667 box->addItem(item);
1668 return true;
1669 }
1670
1671 return QAbstractFormBuilder::addItem(ui_item, item, layout);
1672}
1673
1674bool QDesignerResource::addItem(DomWidget *ui_widget, QWidget *widget, QWidget *parentWidget)
1675{
1676 core()->metaDataBase()->add(widget); // ensure the widget is in the meta database
1677
1678 if (! QAbstractFormBuilder::addItem(ui_widget, widget, parentWidget) || qobject_cast<QMainWindow*> (parentWidget)) {
1679 if (QDesignerContainerExtension *container = qt_extension<QDesignerContainerExtension*>(core()->extensionManager(), parentWidget))
1680 container->addWidget(widget);
1681 }
1682
1683 if (QTabWidget *tabWidget = qobject_cast<QTabWidget*>(parentWidget)) {
1684 const int tabIndex = tabWidget->count() - 1;
1685 const int current = tabWidget->currentIndex();
1686
1687 tabWidget->setCurrentIndex(tabIndex);
1688
1689 const auto &attributes = ui_widget->elementAttribute();
1690 QDesignerPropertySheetExtension *sheet = qt_extension<QDesignerPropertySheetExtension*>(core()->extensionManager(), parentWidget);
1691 if (auto *picon = QFBE::propertyByName(attributes, QFormBuilderStrings::iconAttribute)) {
1692 QVariant v = resourceBuilder()->loadResource(workingDirectory(), picon);
1693 sheet->setProperty(sheet->indexOf(u"currentTabIcon"_s), v);
1694 }
1695 if (auto *ptext = QFBE::propertyByName(attributes, QFormBuilderStrings::titleAttribute)) {
1696 QVariant v = textBuilder()->loadText(ptext);
1697 sheet->setProperty(sheet->indexOf(u"currentTabText"_s), v);
1698 }
1699 if (auto *ptext = QFBE::propertyByName(attributes, QFormBuilderStrings::toolTipAttribute)) {
1700 QVariant v = textBuilder()->loadText(ptext);
1701 sheet->setProperty(sheet->indexOf(u"currentTabToolTip"_s), v);
1702 }
1703 if (auto *ptext = QFBE::propertyByName(attributes, QFormBuilderStrings::whatsThisAttribute)) {
1704 QVariant v = textBuilder()->loadText(ptext);
1705 sheet->setProperty(sheet->indexOf(u"currentTabWhatsThis"_s), v);
1706 }
1707 tabWidget->setCurrentIndex(current);
1708 } else if (QToolBox *toolBox = qobject_cast<QToolBox*>(parentWidget)) {
1709 const int itemIndex = toolBox->count() - 1;
1710 const int current = toolBox->currentIndex();
1711
1712 toolBox->setCurrentIndex(itemIndex);
1713
1714 const auto &attributes = ui_widget->elementAttribute();
1715 QDesignerPropertySheetExtension *sheet = qt_extension<QDesignerPropertySheetExtension*>(core()->extensionManager(), parentWidget);
1716 if (auto *picon = QFBE::propertyByName(attributes, QFormBuilderStrings::iconAttribute)) {
1717 QVariant v = resourceBuilder()->loadResource(workingDirectory(), picon);
1718 sheet->setProperty(sheet->indexOf(u"currentItemIcon"_s), v);
1719 }
1720 if (auto *ptext = QFBE::propertyByName(attributes, QFormBuilderStrings::labelAttribute)) {
1721 QVariant v = textBuilder()->loadText(ptext);
1722 sheet->setProperty(sheet->indexOf(u"currentItemText"_s), v);
1723 }
1724 if (auto *ptext = QFBE::propertyByName(attributes, QFormBuilderStrings::toolTipAttribute)) {
1725 QVariant v = textBuilder()->loadText(ptext);
1726 sheet->setProperty(sheet->indexOf(u"currentItemToolTip"_s), v);
1727 }
1728 toolBox->setCurrentIndex(current);
1729 }
1730
1731 return true;
1732}
1733
1734bool QDesignerResource::copy(QIODevice *dev, const FormBuilderClipboard &selection)
1735{
1736 m_copyWidget = true;
1737
1738 DomUI *ui = copy(selection);
1739
1740 d->m_laidout.clear();
1741 m_copyWidget = false;
1742
1743 if (!ui)
1744 return false;
1745
1746 QXmlStreamWriter writer(dev);
1747 writer.setAutoFormatting(true);
1748 writer.setAutoFormattingIndent(1);
1749 writer.writeStartDocument();
1750 ui->write(writer);
1751 writer.writeEndDocument();
1752 delete ui;
1753 return true;
1754}
1755
1756DomUI *QDesignerResource::copy(const FormBuilderClipboard &selection)
1757{
1758 if (selection.empty())
1759 return nullptr;
1760
1761 m_copyWidget = true;
1762
1763 DomWidget *ui_widget = new DomWidget();
1764 ui_widget->setAttributeName(clipboardObjectName);
1765 bool hasItems = false;
1766 // Widgets
1767 if (!selection.m_widgets.isEmpty()) {
1768 QList<DomWidget *> ui_widget_list;
1769 for (auto *w : selection.m_widgets) {
1770 m_selected = w;
1771 DomWidget *ui_child = createDom(w, ui_widget);
1772 m_selected = nullptr;
1773 if (ui_child)
1774 ui_widget_list.append(ui_child);
1775 }
1776 if (!ui_widget_list.isEmpty()) {
1777 ui_widget->setElementWidget(ui_widget_list);
1778 hasItems = true;
1779 }
1780 }
1781 // actions
1782 if (!selection.m_actions.isEmpty()) {
1783 QList<DomAction *> domActions;
1784 for (QAction* action : std::as_const(selection.m_actions)) {
1785 if (DomAction *domAction = createDom(action))
1786 domActions += domAction;
1787 }
1788 if (!domActions.isEmpty()) {
1789 ui_widget-> setElementAction(domActions);
1790 hasItems = true;
1791 }
1792 }
1793
1794 d->m_laidout.clear();
1795 m_copyWidget = false;
1796
1797 if (!hasItems) {
1798 delete ui_widget;
1799 return nullptr;
1800 }
1801 // UI
1802 DomUI *ui = new DomUI();
1803 ui->setAttributeVersion(currentUiVersion);
1804 ui->setElementWidget(ui_widget);
1805 ui->setElementResources(saveResources(m_resourceBuilder->usedQrcFiles()));
1806 if (DomCustomWidgets *cws = saveCustomWidgets())
1807 ui->setElementCustomWidgets(cws);
1808 return ui;
1809}
1810
1811FormBuilderClipboard QDesignerResource::paste(DomUI *ui, QWidget *widgetParent, QObject *actionParent)
1812{
1813 QDesignerWidgetItemInstaller wii; // Make sure we use QDesignerWidgetItem.
1814 const int saved = m_isMainWidget;
1815 m_isMainWidget = false;
1816
1818
1819 // Widgets
1820 const DomWidget *topLevel = ui->elementWidget();
1821 initialize(ui);
1822 const auto &domWidgets = topLevel->elementWidget();
1823 if (!domWidgets.isEmpty()) {
1824 const QPoint offset = m_formWindow->grid();
1825 for (DomWidget* domWidget : domWidgets) {
1826 if (QWidget *w = create(domWidget, widgetParent)) {
1827 w->move(w->pos() + offset);
1828 // ### change the init properties of w
1829 rc.m_widgets.append(w);
1830 }
1831 }
1832 }
1833 const auto domActions = topLevel->elementAction();
1834 for (DomAction *domAction : domActions) {
1835 if (QAction *a = create(domAction, actionParent))
1836 rc.m_actions .append(a);
1837 }
1838
1839 m_isMainWidget = saved;
1840
1841 if (QDesignerExtraInfoExtension *extra = qt_extension<QDesignerExtraInfoExtension*>(core()->extensionManager(), core()))
1842 extra->loadUiExtraInfo(ui);
1843
1844 createResources(ui->elementResources());
1845
1846 return rc;
1847}
1848
1849FormBuilderClipboard QDesignerResource::paste(QIODevice *dev, QWidget *widgetParent, QObject *actionParent)
1850{
1851 DomUI ui;
1852 QXmlStreamReader reader(dev);
1853 bool uiInitialized = false;
1854
1855 while (!reader.atEnd()) {
1856 if (reader.readNext() == QXmlStreamReader::StartElement) {
1857 if (reader.name().compare("ui"_L1, Qt::CaseInsensitive)) {
1858 ui.read(reader);
1859 uiInitialized = true;
1860 } else {
1861 //: Parsing clipboard contents
1862 reader.raiseError(QCoreApplication::translate("QDesignerResource", "Unexpected element <%1>").arg(reader.name().toString()));
1863 }
1864 }
1865 }
1866 if (reader.hasError()) {
1867 //: Parsing clipboard contents
1868 designerWarning(QCoreApplication::translate("QDesignerResource", "Error while pasting clipboard contents at line %1, column %2: %3")
1869 .arg(reader.lineNumber()).arg(reader.columnNumber())
1870 .arg(reader.errorString()));
1871 uiInitialized = false;
1872 } else if (!uiInitialized) {
1873 //: Parsing clipboard contents
1874 designerWarning(QCoreApplication::translate("QDesignerResource", "Error while pasting clipboard contents: The root element <ui> is missing."));
1875 }
1876
1877 if (!uiInitialized)
1878 return FormBuilderClipboard();
1879
1880 FormBuilderClipboard clipBoard = paste(&ui, widgetParent, actionParent);
1881
1882 return clipBoard;
1883}
1884
1885void QDesignerResource::layoutInfo(DomLayout *layout, QObject *parent, int *margin, int *spacing)
1886{
1887 QAbstractFormBuilder::layoutInfo(layout, parent, margin, spacing);
1888}
1889
1891{
1892 if (m_usedCustomWidgets.isEmpty())
1893 return nullptr;
1894
1895 // We would like the list to be in order of the widget database indexes
1896 // to ensure that base classes come first (nice optics)
1897 QDesignerFormEditorInterface *core = m_formWindow->core();
1898 QDesignerWidgetDataBaseInterface *db = core->widgetDataBase();
1899 const bool isInternalWidgetDataBase = qobject_cast<const WidgetDataBase *>(db);
1900 QMap<int, DomCustomWidget *> orderedMap;
1901
1902 for (auto it = m_usedCustomWidgets.cbegin(), end = m_usedCustomWidgets.cend(); it != end; ++it) {
1903 QDesignerWidgetDataBaseItemInterface *item = it.key();
1904 const QString name = item->name();
1905 DomCustomWidget *custom_widget = new DomCustomWidget;
1906
1907 custom_widget->setElementClass(name);
1908 if (item->isContainer())
1909 custom_widget->setElementContainer(item->isContainer());
1910
1911 if (!item->includeFile().isEmpty()) {
1912 DomHeader *header = new DomHeader;
1913 const IncludeSpecification spec = includeSpecification(item->includeFile());
1914 header->setText(spec.first);
1915 if (spec.second == IncludeGlobal) {
1916 header->setAttributeLocation(u"global"_s);
1917 }
1918 custom_widget->setElementHeader(header);
1919 custom_widget->setElementExtends(item->extends());
1920 }
1921
1922 if (isInternalWidgetDataBase) {
1923 WidgetDataBaseItem *internalItem = static_cast<WidgetDataBaseItem *>(item);
1924 const QStringList fakeSlots = internalItem->fakeSlots();
1925 const QStringList fakeSignals = internalItem->fakeSignals();
1926 if (!fakeSlots.isEmpty() || !fakeSignals.isEmpty()) {
1927 DomSlots *domSlots = new DomSlots();
1928 domSlots->setElementSlot(fakeSlots);
1929 domSlots->setElementSignal(fakeSignals);
1930 custom_widget->setElementSlots(domSlots);
1931 }
1932 const QString addPageMethod = internalItem->addPageMethod();
1933 if (!addPageMethod.isEmpty())
1934 custom_widget->setElementAddPageMethod(addPageMethod);
1935 }
1936
1937 orderedMap.insert(db->indexOfClassName(name), custom_widget);
1938 }
1939
1940 DomCustomWidgets *customWidgets = new DomCustomWidgets;
1941 customWidgets->setElementCustomWidget(orderedMap.values().toVector());
1942 return customWidgets;
1943}
1944
1945bool QDesignerResource::canCompressSpacings(QObject *object) const
1946{
1947 if (QDesignerPropertySheetExtension *sheet = qt_extension<QDesignerPropertySheetExtension*>(core()->extensionManager(), object)) {
1948 if (qobject_cast<QGridLayout *>(object)) {
1949 const int h = sheet->property(sheet->indexOf(u"horizontalSpacing"_s)).toInt();
1950 const int v = sheet->property(sheet->indexOf(u"verticalSpacing"_s)).toInt();
1951 if (h == v)
1952 return true;
1953 }
1954 }
1955 return false;
1956}
1957
1959{
1960 QList<DomProperty*> properties;
1961 if (QDesignerPropertySheetExtension *sheet = qt_extension<QDesignerPropertySheetExtension*>(core()->extensionManager(), object)) {
1962 QDesignerDynamicPropertySheetExtension *dynamicSheet = qt_extension<QDesignerDynamicPropertySheetExtension*>(core()->extensionManager(), object);
1963 const int count = sheet->count();
1964 QList<DomProperty *> spacingProperties;
1965 const bool compressSpacings = canCompressSpacings(object);
1966 for (int index = 0; index < count; ++index) {
1967 if (!sheet->isChanged(index) && (!dynamicSheet || !dynamicSheet->isDynamicProperty(index)))
1968 continue;
1969
1970 const QString propertyName = sheet->propertyName(index);
1971 // Suppress windowModality in legacy forms that have it set on child widgets
1972 if (propertyName == "windowModality"_L1 && !sheet->isVisible(index))
1973 continue;
1974
1975 const QVariant value = sheet->property(index);
1976 if (DomProperty *p = createProperty(object, propertyName, value)) {
1977 if (compressSpacings && (propertyName == "horizontalSpacing"_L1
1978 || propertyName == "verticalSpacing"_L1)) {
1979 spacingProperties.append(p);
1980 } else {
1981 properties.append(p);
1982 }
1983 }
1984 }
1985 if (compressSpacings) {
1986 if (spacingProperties.size() == 2) {
1987 DomProperty *spacingProperty = spacingProperties.at(0);
1988 spacingProperty->setAttributeName(u"spacing"_s);
1989 properties.append(spacingProperty);
1990 delete spacingProperties.at(1);
1991 } else {
1992 properties += spacingProperties;
1993 }
1994 }
1995 }
1996 return properties;
1997}
1998
1999DomProperty *QDesignerResource::applyProperStdSetAttribute(QObject *object, const QString &propertyName, DomProperty *property)
2000{
2001 if (!property)
2002 return nullptr;
2003
2004 QExtensionManager *mgr = core()->extensionManager();
2005 if (const QDesignerPropertySheetExtension *sheet = qt_extension<QDesignerPropertySheetExtension*>(mgr, object)) {
2006 const QDesignerDynamicPropertySheetExtension *dynamicSheet = qt_extension<QDesignerDynamicPropertySheetExtension*>(mgr, object);
2007 const QDesignerPropertySheet *designerSheet = qobject_cast<QDesignerPropertySheet*>(core()->extensionManager()->extension(object, Q_TYPEID(QDesignerPropertySheetExtension)));
2008 const int index = sheet->indexOf(propertyName);
2009 if ((dynamicSheet && dynamicSheet->isDynamicProperty(index)) || (designerSheet && designerSheet->isDefaultDynamicProperty(index)))
2010 property->setAttributeStdset(0);
2011 }
2012 return property;
2013}
2014
2015// Optimistic check for a standard setter function
2016static inline bool hasSetter(QDesignerFormEditorInterface *core, QObject *object, const QString &propertyName)
2017{
2018 const QDesignerMetaObjectInterface *meta = core->introspection()->metaObject(object);
2019 const int pindex = meta->indexOfProperty(propertyName);
2020 if (pindex == -1)
2021 return true;
2022 return meta->property(pindex)->hasSetter();
2023}
2024
2025DomProperty *QDesignerResource::createProperty(QObject *object, const QString &propertyName, const QVariant &value)
2026{
2027 if (!checkProperty(object, propertyName)) {
2028 return nullptr;
2029 }
2030
2031 if (value.canConvert<PropertySheetFlagValue>()) {
2032 const PropertySheetFlagValue f = qvariant_cast<PropertySheetFlagValue>(value);
2033 const auto mode = d->m_fullyQualifiedEnums
2034 ? DesignerMetaFlags::FullyQualified : DesignerMetaFlags::Qualified;
2035 const QString flagString = f.metaFlags.toString(f.value, mode);
2036 if (flagString.isEmpty())
2037 return nullptr;
2038
2039 DomProperty *p = new DomProperty;
2040 // check if we have a standard cpp set function
2041 if (!hasSetter(core(), object, propertyName))
2042 p->setAttributeStdset(0);
2043 p->setAttributeName(propertyName);
2044 p->setElementSet(flagString);
2045 return applyProperStdSetAttribute(object, propertyName, p);
2046 }
2047 if (value.canConvert<PropertySheetEnumValue>()) {
2048 const PropertySheetEnumValue e = qvariant_cast<PropertySheetEnumValue>(value);
2049 const auto mode = d->m_fullyQualifiedEnums
2050 ? DesignerMetaEnum::FullyQualified : DesignerMetaEnum::Qualified;
2051 bool ok;
2052 const QString id = e.metaEnum.toString(e.value, mode, &ok);
2053 if (!ok)
2054 designerWarning(e.metaEnum.messageToStringFailed(e.value));
2055 if (id.isEmpty())
2056 return nullptr;
2057
2058 DomProperty *p = new DomProperty;
2059 // check if we have a standard cpp set function
2060 if (!hasSetter(core(), object, propertyName))
2061 p->setAttributeStdset(0);
2062 // Map "horizontalSizeConstraint" to "sizeConstraint" for Qt 6
2063 if (!supportsSeparateSizeConstraints(d->m_saveVersion)
2064 && propertyName == "horizontalSizeConstraint"_L1
2065 && object->inherits("QLayout")) {
2066 p->setAttributeName("sizeConstraint"_L1);
2067 } else {
2068 p->setAttributeName(propertyName);
2069 }
2070 p->setElementEnum(id);
2071 return applyProperStdSetAttribute(object, propertyName, p);
2072 }
2073 if (value.canConvert<PropertySheetStringValue>()) {
2074 const PropertySheetStringValue strVal = qvariant_cast<PropertySheetStringValue>(value);
2075 DomProperty *p = stringToDomProperty(strVal.value(), strVal);
2076 if (!hasSetter(core(), object, propertyName))
2077 p->setAttributeStdset(0);
2078
2079 p->setAttributeName(propertyName);
2080
2081 return applyProperStdSetAttribute(object, propertyName, p);
2082 }
2083 if (value.canConvert<PropertySheetStringListValue>()) {
2084 const PropertySheetStringListValue listValue = qvariant_cast<PropertySheetStringListValue>(value);
2085 DomProperty *p = new DomProperty;
2086 if (!hasSetter(core(), object, propertyName))
2087 p->setAttributeStdset(0);
2088
2089 p->setAttributeName(propertyName);
2090
2091 DomStringList *domStringList = new DomStringList();
2092 domStringList->setElementString(listValue.value());
2093 translationParametersToDom(listValue, domStringList);
2094 p->setElementStringList(domStringList);
2095 return applyProperStdSetAttribute(object, propertyName, p);
2096 }
2097 if (value.canConvert<PropertySheetKeySequenceValue>()) {
2098 const PropertySheetKeySequenceValue keyVal = qvariant_cast<PropertySheetKeySequenceValue>(value);
2099 DomProperty *p = stringToDomProperty(keyVal.value().toString(), keyVal);
2100 if (!hasSetter(core(), object, propertyName))
2101 p->setAttributeStdset(0);
2102
2103 p->setAttributeName(propertyName);
2104
2105 return applyProperStdSetAttribute(object, propertyName, p);
2106 }
2107
2108 return applyProperStdSetAttribute(object, propertyName, QAbstractFormBuilder::createProperty(object, propertyName, value));
2109}
2110
2111QStringList QDesignerResource::mergeWithLoadedPaths(const QStringList &paths) const
2112{
2113 QStringList newPaths = paths;
2115 const QStringList loadedPaths = m_resourceBuilder->loadedQrcFiles();
2116 std::remove_copy_if(loadedPaths.cbegin(), loadedPaths.cend(),
2117 std::back_inserter(newPaths),
2118 [&newPaths] (const QString &path) { return newPaths.contains(path); });
2119#endif
2120 return newPaths;
2121}
2122
2123
2124void QDesignerResource::createResources(DomResources *resources)
2125{
2126 QStringList paths;
2127 if (resources != nullptr) {
2128 const auto &dom_include = resources->elementInclude();
2129 for (DomResource *res : dom_include) {
2130 QString path = QDir::cleanPath(m_formWindow->absoluteDir().absoluteFilePath(res->attributeLocation()));
2131 while (!QFile::exists(path)) {
2132 QWidget *dialogParent = m_formWindow->core()->topLevel();
2133 const QString promptTitle = QCoreApplication::translate("qdesigner_internal::QDesignerResource", "Loading qrc file");
2134 const QString prompt = QCoreApplication::translate("qdesigner_internal::QDesignerResource", "The specified qrc file <p><b>%1</b></p><p>could not be found. Do you want to update the file location?</p>").arg(path);
2135
2136 const QMessageBox::StandardButton answer = core()->dialogGui()->message(dialogParent, QDesignerDialogGuiInterface::ResourceLoadFailureMessage,
2137 QMessageBox::Warning, promptTitle, prompt, QMessageBox::Yes|QMessageBox::No, QMessageBox::Yes);
2138 if (answer == QMessageBox::Yes) {
2139 const QFileInfo fi(path);
2140 const QString fileDialogTitle = QCoreApplication::translate("qdesigner_internal::QDesignerResource", "New location for %1").arg(fi.fileName());
2141 const QString fileDialogPattern = QCoreApplication::translate("qdesigner_internal::QDesignerResource", "Resource files (*.qrc)");
2142 path = core()->dialogGui()->getOpenFileName(dialogParent, fileDialogTitle, fi.absolutePath(), fileDialogPattern);
2143 if (path.isEmpty())
2144 break;
2145 m_formWindow->setProperty("_q_resourcepathchanged", QVariant(true));
2146 } else {
2147 break;
2148 }
2149 }
2150 if (!path.isEmpty()) {
2151 paths << path;
2152 m_formWindow->addResourceFile(path);
2153 }
2154 }
2155 }
2156
2158 paths = mergeWithLoadedPaths(paths);
2159#endif
2160
2161 QtResourceSet *resourceSet = m_formWindow->resourceSet();
2162 if (resourceSet) {
2163 QStringList newPaths = resourceSet->activeResourceFilePaths();
2164 std::remove_copy_if(paths.cbegin(), paths.cend(),
2165 std::back_inserter(newPaths),
2166 [&newPaths] (const QString &path) { return newPaths.contains(path); });
2167 resourceSet->activateResourceFilePaths(newPaths);
2168 } else {
2169 resourceSet = m_formWindow->core()->resourceModel()->addResourceSet(paths);
2170 m_formWindow->setResourceSet(resourceSet);
2171 QObject::connect(m_formWindow->core()->resourceModel(), &QtResourceModel::resourceSetActivated,
2172 m_formWindow, &FormWindowBase::resourceSetActivated);
2173 }
2174}
2175
2177{
2178 QStringList paths;
2179 switch (m_formWindow->resourceFileSaveMode()) {
2180 case QDesignerFormWindowInterface::SaveAllResourceFiles:
2181 paths = m_formWindow->activeResourceFilePaths();
2182 break;
2183 case QDesignerFormWindowInterface::SaveOnlyUsedResourceFiles:
2184 paths = m_resourceBuilder->usedQrcFiles();
2185 break;
2186 case QDesignerFormWindowInterface::DontSaveResourceFiles:
2187 break;
2188 }
2189 return saveResources(paths);
2190}
2191
2192DomResources *QDesignerResource::saveResources(const QStringList &qrcPaths)
2193{
2194 QtResourceSet *resourceSet = m_formWindow->resourceSet();
2195 QList<DomResource *> dom_include;
2196 if (resourceSet) {
2197 const QStringList activePaths = resourceSet->activeResourceFilePaths();
2198 for (const QString &path : activePaths) {
2199 if (qrcPaths.contains(path)) {
2200 DomResource *dom_res = new DomResource;
2201 QString conv_path = path;
2202 if (m_resourceBuilder->isSaveRelative())
2203 conv_path = m_formWindow->absoluteDir().relativeFilePath(path);
2204 conv_path.replace(QDir::separator(), u'/');
2205 dom_res->setAttributeLocation(conv_path);
2206 dom_include.append(dom_res);
2207 }
2208 }
2209 }
2210
2211 DomResources *dom_resources = new DomResources;
2212 dom_resources->setElementInclude(dom_include);
2213
2214 return dom_resources;
2215}
2216
2217DomAction *QDesignerResource::createDom(QAction *action)
2218{
2219 if (!core()->metaDataBase()->item(action) || action->menu())
2220 return nullptr;
2221
2223}
2224
2225DomActionGroup *QDesignerResource::createDom(QActionGroup *actionGroup)
2226{
2227 if (core()->metaDataBase()->item(actionGroup) != nullptr) {
2228 return QAbstractFormBuilder::createDom(actionGroup);
2229 }
2230
2231 return nullptr;
2232}
2233
2234QAction *QDesignerResource::create(DomAction *ui_action, QObject *parent)
2235{
2236 if (QAction *action = QAbstractFormBuilder::create(ui_action, parent)) {
2237 core()->metaDataBase()->add(action);
2238 return action;
2239 }
2240
2241 return nullptr;
2242}
2243
2244QActionGroup *QDesignerResource::create(DomActionGroup *ui_action_group, QObject *parent)
2245{
2246 if (QActionGroup *actionGroup = QAbstractFormBuilder::create(ui_action_group, parent)) {
2247 core()->metaDataBase()->add(actionGroup);
2248 return actionGroup;
2249 }
2250
2251 return nullptr;
2252}
2253
2254DomActionRef *QDesignerResource::createActionRefDom(QAction *action)
2255{
2256 if (!core()->metaDataBase()->item(action)
2257 || (!action->isSeparator() && !action->menu() && action->objectName().isEmpty()))
2258 return nullptr;
2259
2261}
2262
2263void QDesignerResource::addMenuAction(QAction *action)
2264{
2265 core()->metaDataBase()->add(action);
2266}
2267
2268QAction *QDesignerResource::createAction(QObject *parent, const QString &name)
2269{
2270 if (QAction *action = QAbstractFormBuilder::createAction(parent, name)) {
2271 core()->metaDataBase()->add(action);
2272 return action;
2273 }
2274
2275 return nullptr;
2276}
2277
2278QActionGroup *QDesignerResource::createActionGroup(QObject *parent, const QString &name)
2279{
2280 if (QActionGroup *actionGroup = QAbstractFormBuilder::createActionGroup(parent, name)) {
2281 core()->metaDataBase()->add(actionGroup);
2282 return actionGroup;
2283 }
2284
2285 return nullptr;
2286}
2287
2288/* Apply the attributes to a widget via property sheet where appropriate,
2289 * that is, the sheet handles attributive fake properties */
2290void QDesignerResource::applyAttributesToPropertySheet(const DomWidget *ui_widget, QWidget *widget)
2291{
2292 const DomPropertyList attributes = ui_widget->elementAttribute();
2293 if (attributes.isEmpty())
2294 return;
2295 QDesignerPropertySheetExtension *sheet = qt_extension<QDesignerPropertySheetExtension*>(m_formWindow->core()->extensionManager(), widget);
2296 for (auto *prop : attributes) {
2297 const QString name = prop->attributeName();
2298 const int index = sheet->indexOf(name);
2299 if (index == -1) {
2300 const QString msg = "Unable to apply attributive property '%1' to '%2'. It does not exist."_L1.arg(name, widget->objectName());
2301 designerWarning(msg);
2302 } else {
2303 sheet->setProperty(index, domPropertyToVariant(this, widget->metaObject(), prop));
2304 sheet->setChanged(index, true);
2305 }
2306 }
2307}
2308
2309void QDesignerResource::loadExtraInfo(DomWidget *ui_widget, QWidget *widget, QWidget *parentWidget)
2310{
2311 QAbstractFormBuilder::loadExtraInfo(ui_widget, widget, parentWidget);
2312 // Apply the page id attribute of a QWizardPage (which is an attributive fake property)
2313 if (qobject_cast<const QWizardPage*>(widget))
2314 applyAttributesToPropertySheet(ui_widget, widget);
2315}
2316
2317}
2318
2319QT_END_NAMESPACE
virtual bool isDynamicProperty(int index) const =0
virtual bool isLanguageResource(const QString &path) const =0
friend class QWidget
Definition qpainter.h:432
QDesignerFormEditorInterface * core() const override
Returns a pointer to \QD's current QDesignerFormEditorInterface object.
void setLayoutDefault(int margin, int spacing) override
Sets the default margin and spacing for the form's layout.
void layoutDefault(int *margin, int *spacing) override
Fills in the default margin and spacing for the form's default layout in the margin and spacing varia...
QWidget * mainContainer() const override
Returns the main container widget for the form window.
void manageWidget(QWidget *w) override
Instructs the form window to manage the specified widget.
int toolCount() const override
Returns the number of tools available.
QVariant toNativeValue(const QVariant &value) const override
bool isResourceType(const QVariant &value) const override
QVariant loadResource(const QDir &workingDirectory, const DomProperty *icon) const override
QDesignerResourceBuilder(QDesignerFormEditorInterface *core, DesignerPixmapCache *pixmapCache, DesignerIconCache *iconCache)
DomProperty * saveResource(const QDir &workingDirectory, const QVariant &value) const override
void setIconCache(DesignerIconCache *iconCache)
void setPixmapCache(DesignerPixmapCache *pixmapCache)
DomActionGroup * createDom(QActionGroup *actionGroup) override
QWidget * load(QIODevice *dev, QWidget *parentWidget) override
Loads an XML representation of a widget from the given device, and constructs a new widget with the s...
bool addItem(DomLayoutItem *ui_item, QLayoutItem *item, QLayout *layout) override
DomLayoutItem * createDom(QLayoutItem *item, DomLayout *ui_layout, DomWidget *ui_parentWidget) override
DomLayout * createDom(QLayout *layout, DomLayout *ui_layout, DomWidget *ui_parentWidget) override
void applyProperties(QObject *o, const QList< DomProperty * > &properties) override
void createCustomWidgets(DomCustomWidgets *) override
QLayout * create(DomLayout *ui_layout, QLayout *layout, QWidget *parentWidget) override
void applyTabStops(QWidget *widget, DomTabStops *tabStops) override
DomWidget * saveWidget(QToolBar *toolBar, DomWidget *ui_parentWidget)
DomCustomWidgets * saveCustomWidgets() override
DomWidget * saveWidget(QDesignerDockWidget *dockWidget, DomWidget *ui_parentWidget)
DomUI * copy(const FormBuilderClipboard &selection) override
QAction * create(DomAction *ui_action, QObject *parent) override
QActionGroup * create(DomActionGroup *ui_action_group, QObject *parent) override
DomWidget * saveWidget(QWizardPage *wizardPage, DomWidget *ui_parentWidget)
void saveDom(DomUI *ui, QWidget *widget) override
void createResources(DomResources *) override
void save(QIODevice *dev, QWidget *widget) override
Saves an XML representation of the given widget to the specified device in the standard UI file forma...
QLayoutItem * create(DomLayoutItem *ui_layoutItem, QLayout *layout, QWidget *parentWidget) override
QWidget * create(DomUI *ui, QWidget *parentWidget) override
QWidget * loadUi(DomUI *ui, QWidget *parentWidget)
void loadExtraInfo(DomWidget *ui_widget, QWidget *widget, QWidget *parentWidget) override
DomWidget * createDom(QWidget *widget, DomWidget *ui_parentWidget, bool recursive=true) override
DomWidget * saveWidget(QTabWidget *widget, DomWidget *ui_parentWidget)
DomWidget * saveWidget(QToolBox *widget, DomWidget *ui_parentWidget)
FormBuilderClipboard paste(DomUI *ui, QWidget *widgetParent, QObject *actionParent=nullptr) override
DomWidget * saveWidget(QWidget *widget, QDesignerContainerExtension *container, DomWidget *ui_parentWidget)
bool addItem(DomWidget *ui_widget, QWidget *widget, QWidget *parentWidget) override
void layoutInfo(DomLayout *layout, QObject *parent, int *margin, int *spacing) override
FormBuilderClipboard paste(QIODevice *dev, QWidget *widgetParent, QObject *actionParent=nullptr) override
QList< DomProperty * > computeProperties(QObject *obj) override
DomWidget * saveWidget(QStackedWidget *widget, DomWidget *ui_parentWidget)
QWidget * create(DomWidget *ui_widget, QWidget *parentWidget) override
bool copy(QIODevice *dev, const FormBuilderClipboard &selection) override
QVariant toNativeValue(const QVariant &value) const override
DomProperty * saveText(const QVariant &value) const override
QVariant loadText(const DomProperty *icon) const override
static bool checkProperty(const QString &propertyName)
Auxiliary methods to store/retrieve settings.
static QString messageBoxTitle()
static void setIconPixmap(QIcon::Mode m, QIcon::State s, const QDir &workingDirectory, QString path, PropertySheetIconValue &icon, const QDesignerLanguageExtension *lang=nullptr)
static DomProperty * stringToDomProperty(const QString &value, const PropertySheetTranslatableData &translatableData)
QString msgUnmanagedPage(QDesignerFormEditorInterface *core, QWidget *container, int index, QWidget *page)
static DomProperty * stringToDomProperty(const QString &value)
void translationParametersToDom(const PropertySheetTranslatableData &data, DomElement *e)
static bool supportsQualifiedEnums(const QVersionNumber &qtVersion)
static bool hasSetter(QDesignerFormEditorInterface *core, QObject *object, const QString &propertyName)
static bool isDeprecatedQt5Property(const QObject *o, const DomProperty *p)
static bool readDomEnumerationValue(const DomProperty *p, const QDesignerPropertySheetExtension *sheet, int index, QVariant &v)
static bool checkContainerProperty(const QWidget *w, const QString &propertyName)
void translationParametersFromDom(const DomElement *e, PropertySheetTranslatableData *data)
static bool supportsSeparateSizeConstraints(const QVersionNumber &qtVersion)
#define OLD_RESOURCE_FORMAT
static constexpr auto clipboardObjectName
QFormBuilderExtra QFBE
static constexpr auto currentUiVersion