summaryrefslogtreecommitdiff
path: root/src/designer/src/uitools/quiloader.cpp
blob: d8a883c9f6d14bba078fc9abe585858639cbf1c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
/****************************************************************************
**
** Copyright (C) 2018 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Designer of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
**   * Redistributions of source code must retain the above copyright
**     notice, this list of conditions and the following disclaimer.
**   * Redistributions in binary form must reproduce the above copyright
**     notice, this list of conditions and the following disclaimer in
**     the documentation and/or other materials provided with the
**     distribution.
**   * Neither the name of The Qt Company Ltd nor the names of its
**     contributors may be used to endorse or promote products derived
**     from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/


#include "quiloader.h"
#include "quiloader_p.h"

#include <QtUiPlugin/customwidget.h>

#include <formbuilder.h>
#include <formbuilderextra_p.h>
#include <textbuilder_p.h>
#include <ui4_p.h>

#include <QtCore/qdebug.h>
#include <QtCore/QDataStream>
#include <QtWidgets/QAction>
#include <QtWidgets/QActionGroup>
#include <QtWidgets/QApplication>
#include <QtCore/QDir>
#include <QtCore/QLibraryInfo>
#include <QtWidgets/QLayout>
#include <QtWidgets/QWidget>
#include <QtCore/QMap>
#include <QtWidgets/QTabWidget>
#include <QtWidgets/QTreeWidget>
#include <QtWidgets/QListWidget>
#include <QtWidgets/QTableWidget>
#include <QtWidgets/QToolBox>
#include <QtWidgets/QComboBox>
#include <QtWidgets/QFontComboBox>

QT_BEGIN_NAMESPACE

typedef QMap<QString, bool> widget_map;
Q_GLOBAL_STATIC(widget_map, g_widgets)

class QUiLoader;
class QUiLoaderPrivate;

#ifndef QT_NO_DATASTREAM
// QUiTranslatableStringValue must be streamable since they become part of the QVariant-based
// mime data when dragging items in views with QAbstractItemView::InternalMove.
QDataStream &operator<<(QDataStream &out, const QUiTranslatableStringValue &s)
{
    out << s.qualifier() << s.value();
    return out;
}

QDataStream &operator>>(QDataStream &in, QUiTranslatableStringValue &s)
{
    in >> s.m_qualifier >> s.m_value;
    return in;
}
#endif // QT_NO_DATASTREAM

QString QUiTranslatableStringValue::translate(const QByteArray &className, bool idBased) const
{
    return idBased
        ? qtTrId(m_qualifier.constData())
        : QCoreApplication::translate(className.constData(), m_value.constData(), m_qualifier.constData());
}

#ifdef QFORMINTERNAL_NAMESPACE
namespace QFormInternal
{
#endif

class TranslatingTextBuilder : public QTextBuilder
{
public:
    explicit TranslatingTextBuilder(bool idBased, bool trEnabled, const QByteArray &className) :
        m_idBased(idBased), m_trEnabled(trEnabled), m_className(className) {}

    TranslatingTextBuilder(bool trEnabled, const QByteArray &className) :
        m_trEnabled(trEnabled), m_className(className) {}

    QVariant loadText(const DomProperty *icon) const override;

    QVariant toNativeValue(const QVariant &value) const override;

    bool idBased() const { return m_idBased; }

private:
    bool m_idBased;
    bool m_trEnabled;
    QByteArray m_className;
};

QVariant TranslatingTextBuilder::loadText(const DomProperty *text) const
{
    const DomString *str = text->elementString();
    if (!str)
        return QVariant();
    if (str->hasAttributeNotr()) {
        const QString notr = str->attributeNotr();
        if (notr == QStringLiteral("true") || notr == QStringLiteral("yes"))
            return QVariant::fromValue(str->text());
    }
    QUiTranslatableStringValue strVal;
    strVal.setValue(str->text().toUtf8());
    if (m_idBased)
        strVal.setQualifier(str->attributeId().toUtf8());
    else if (str->hasAttributeComment())
        strVal.setQualifier(str->attributeComment().toUtf8());
    return QVariant::fromValue(strVal);
}

QVariant TranslatingTextBuilder::toNativeValue(const QVariant &value) const
{
    if (value.canConvert<QUiTranslatableStringValue>()) {
        QUiTranslatableStringValue tsv = qvariant_cast<QUiTranslatableStringValue>(value);
        if (!m_trEnabled)
            return QString::fromUtf8(tsv.value().constData());
        return QVariant::fromValue(tsv.translate(m_className, m_idBased));
    }
    if (value.canConvert<QString>())
        return QVariant::fromValue(qvariant_cast<QString>(value));
    return value;
}

// This is "exported" to linguist
const QUiItemRolePair qUiItemRoles[] = {
    { Qt::DisplayRole, Qt::DisplayPropertyRole },
#ifndef QT_NO_TOOLTIP
    { Qt::ToolTipRole, Qt::ToolTipPropertyRole },
#endif
#ifndef QT_NO_STATUSTIP
    { Qt::StatusTipRole, Qt::StatusTipPropertyRole },
#endif
#ifndef QT_NO_WHATSTHIS
    { Qt::WhatsThisRole, Qt::WhatsThisPropertyRole },
#endif
    { -1 , -1 }
};

static void recursiveReTranslate(QTreeWidgetItem *item, const QByteArray &class_name, bool idBased)
{
    const QUiItemRolePair *irs = qUiItemRoles;

    int cnt = item->columnCount();
    for (int i = 0; i < cnt; ++i) {
        for (unsigned j = 0; irs[j].shadowRole >= 0; j++) {
            QVariant v = item->data(i, irs[j].shadowRole);
            if (v.isValid()) {
                QUiTranslatableStringValue tsv = qvariant_cast<QUiTranslatableStringValue>(v);
                item->setData(i, irs[j].realRole, tsv.translate(class_name, idBased));
            }
        }
    }

    cnt = item->childCount();
    for (int i = 0; i < cnt; ++i)
        recursiveReTranslate(item->child(i), class_name, idBased);
}

template<typename T>
static void reTranslateWidgetItem(T *item, const QByteArray &class_name, bool idBased)
{
    const QUiItemRolePair *irs = qUiItemRoles;

    for (unsigned j = 0; irs[j].shadowRole >= 0; j++) {
        QVariant v = item->data(irs[j].shadowRole);
        if (v.isValid()) {
            QUiTranslatableStringValue tsv = qvariant_cast<QUiTranslatableStringValue>(v);
            item->setData(irs[j].realRole, tsv.translate(class_name, idBased));
        }
    }
}

static void reTranslateTableItem(QTableWidgetItem *item, const QByteArray &class_name, bool idBased)
{
    if (item)
        reTranslateWidgetItem(item, class_name, idBased);
}

#define RETRANSLATE_SUBWIDGET_PROP(mainWidget, setter, propName) \
    do { \
        QVariant v = mainWidget->widget(i)->property(propName); \
        if (v.isValid()) { \
            QUiTranslatableStringValue tsv = qvariant_cast<QUiTranslatableStringValue>(v); \
            mainWidget->setter(i, tsv.translate(m_className, m_idBased)); \
        } \
    } while (0)

class TranslationWatcher: public QObject
{
    Q_OBJECT

public:
    explicit TranslationWatcher(QObject *parent, const QByteArray &className, bool idBased):
        QObject(parent),
        m_className(className),
        m_idBased(idBased)
    {
    }

    virtual bool eventFilter(QObject *o, QEvent *event)
    {
        if (event->type() == QEvent::LanguageChange) {
            const QList<QByteArray> &dynamicPropertyNames = o->dynamicPropertyNames();
            for (const QByteArray &prop : dynamicPropertyNames) {
                if (prop.startsWith(PROP_GENERIC_PREFIX)) {
                    const QByteArray propName = prop.mid(sizeof(PROP_GENERIC_PREFIX) - 1);
                    const QUiTranslatableStringValue tsv =
                                qvariant_cast<QUiTranslatableStringValue>(o->property(prop));
                    o->setProperty(propName, tsv.translate(m_className, m_idBased));
                }
            }
            if (0) {
#ifndef QT_NO_TABWIDGET
            } else if (QTabWidget *tabw = qobject_cast<QTabWidget*>(o)) {
                const int cnt = tabw->count();
                for (int i = 0; i < cnt; ++i) {
                    RETRANSLATE_SUBWIDGET_PROP(tabw, setTabText, PROP_TABPAGETEXT);
# ifndef QT_NO_TOOLTIP
                    RETRANSLATE_SUBWIDGET_PROP(tabw, setTabToolTip, PROP_TABPAGETOOLTIP);
# endif
# ifndef QT_NO_WHATSTHIS
                    RETRANSLATE_SUBWIDGET_PROP(tabw, setTabWhatsThis, PROP_TABPAGEWHATSTHIS);
# endif
                }
#endif
#ifndef QT_NO_LISTWIDGET
            } else if (QListWidget *listw = qobject_cast<QListWidget*>(o)) {
                const int cnt = listw->count();
                for (int i = 0; i < cnt; ++i)
                    reTranslateWidgetItem(listw->item(i), m_className, m_idBased);
#endif
#ifndef QT_NO_TREEWIDGET
            } else if (QTreeWidget *treew = qobject_cast<QTreeWidget*>(o)) {
                if (QTreeWidgetItem *item = treew->headerItem())
                    recursiveReTranslate(item, m_className, m_idBased);
                const int cnt = treew->topLevelItemCount();
                for (int i = 0; i < cnt; ++i) {
                    QTreeWidgetItem *item = treew->topLevelItem(i);
                    recursiveReTranslate(item, m_className, m_idBased);
                }
#endif
#ifndef QT_NO_TABLEWIDGET
            } else if (QTableWidget *tablew = qobject_cast<QTableWidget*>(o)) {
                const int row_cnt = tablew->rowCount();
                const int col_cnt = tablew->columnCount();
                for (int j = 0; j < col_cnt; ++j)
                    reTranslateTableItem(tablew->horizontalHeaderItem(j), m_className, m_idBased);
                for (int i = 0; i < row_cnt; ++i) {
                    reTranslateTableItem(tablew->verticalHeaderItem(i), m_className, m_idBased);
                    for (int j = 0; j < col_cnt; ++j)
                        reTranslateTableItem(tablew->item(i, j), m_className, m_idBased);
                }
#endif
#ifndef QT_NO_COMBOBOX
            } else if (QComboBox *combow = qobject_cast<QComboBox*>(o)) {
                if (!qobject_cast<QFontComboBox*>(o)) {
                    const int cnt = combow->count();
                    for (int i = 0; i < cnt; ++i) {
                        const QVariant v = combow->itemData(i, Qt::DisplayPropertyRole);
                        if (v.isValid()) {
                            QUiTranslatableStringValue tsv = qvariant_cast<QUiTranslatableStringValue>(v);
                            combow->setItemText(i, tsv.translate(m_className, m_idBased));
                        }
                    }
                }
#endif
#ifndef QT_NO_TOOLBOX
            } else if (QToolBox *toolw = qobject_cast<QToolBox*>(o)) {
                const int cnt = toolw->count();
                for (int i = 0; i < cnt; ++i) {
                    RETRANSLATE_SUBWIDGET_PROP(toolw, setItemText, PROP_TOOLITEMTEXT);
# ifndef QT_NO_TOOLTIP
                    RETRANSLATE_SUBWIDGET_PROP(toolw, setItemToolTip, PROP_TOOLITEMTOOLTIP);
# endif
                }
#endif
            }
        }
        return false;
    }

private:
    QByteArray m_className;
    bool m_idBased;
};

class FormBuilderPrivate: public QFormBuilder
{
    friend class QT_PREPEND_NAMESPACE(QUiLoader);
    friend class QT_PREPEND_NAMESPACE(QUiLoaderPrivate);
    typedef QFormBuilder ParentClass;

public:
    QUiLoader *loader;

    bool dynamicTr;
    bool trEnabled;

    FormBuilderPrivate(): loader(nullptr), dynamicTr(false), trEnabled(true),
        m_trwatch(nullptr), m_idBased(false) {}

    QWidget *defaultCreateWidget(const QString &className, QWidget *parent, const QString &name)
    {
        return ParentClass::createWidget(className, parent, name);
    }

    QLayout *defaultCreateLayout(const QString &className, QObject *parent, const QString &name)
    {
        return ParentClass::createLayout(className, parent, name);
    }

    QAction *defaultCreateAction(QObject *parent, const QString &name)
    {
        return ParentClass::createAction(parent, name);
    }

    QActionGroup *defaultCreateActionGroup(QObject *parent, const QString &name)
    {
        return ParentClass::createActionGroup(parent, name);
    }

    QWidget *createWidget(const QString &className, QWidget *parent, const QString &name) override
    {
        if (QWidget *widget = loader->createWidget(className, parent, name)) {
            widget->setObjectName(name);
            return widget;
        }

        return 0;
    }

    QLayout *createLayout(const QString &className, QObject *parent, const QString &name) override
    {
        if (QLayout *layout = loader->createLayout(className, parent, name)) {
            layout->setObjectName(name);
            return layout;
        }

        return 0;
    }

    QActionGroup *createActionGroup(QObject *parent, const QString &name) override
    {
        if (QActionGroup *actionGroup = loader->createActionGroup(parent, name)) {
            actionGroup->setObjectName(name);
            return actionGroup;
        }

        return 0;
    }

    QAction *createAction(QObject *parent, const QString &name)  override
    {
        if (QAction *action = loader->createAction(parent, name)) {
            action->setObjectName(name);
            return action;
        }

        return 0;
    }

    void applyProperties(QObject *o, const QList<DomProperty*> &properties) override;
    QWidget *create(DomUI *ui, QWidget *parentWidget) override;
    QWidget *create(DomWidget *ui_widget, QWidget *parentWidget) override;
    bool addItem(DomWidget *ui_widget, QWidget *widget, QWidget *parentWidget) override;

private:
    QByteArray m_class;
    TranslationWatcher *m_trwatch;
    bool m_idBased;
};

static QString convertTranslatable(const DomProperty *p, const QByteArray &className,
                                   bool idBased, QUiTranslatableStringValue *strVal)
{
    if (p->kind() != DomProperty::String)
        return QString();
    const DomString *dom_str = p->elementString();
    if (!dom_str)
        return QString();
    if (dom_str->hasAttributeNotr()) {
        const QString notr = dom_str->attributeNotr();
        if (notr == QStringLiteral("yes") || notr == QStringLiteral("true"))
            return QString();
    }
    strVal->setValue(dom_str->text().toUtf8());
    strVal->setQualifier(idBased ? dom_str->attributeId().toUtf8() : dom_str->attributeComment().toUtf8());
    if (strVal->value().isEmpty() && strVal->qualifier().isEmpty())
        return QString();
    return strVal->translate(className, idBased);
}

void FormBuilderPrivate::applyProperties(QObject *o, const QList<DomProperty*> &properties)
{
    QFormBuilder::applyProperties(o, properties);

    if (!m_trwatch)
        m_trwatch = new TranslationWatcher(o, m_class, m_idBased);

    if (properties.empty())
        return;

    // Unlike string item roles, string properties are not loaded via the textBuilder
    // (as they are "shadowed" by the property sheets in designer). So do the initial
    // translation here.
    bool anyTrs = false;
    for (const DomProperty *p : properties) {
        QUiTranslatableStringValue strVal;
        const QString text = convertTranslatable(p, m_class, m_idBased, &strVal);
        if (text.isEmpty())
            continue;
        const QByteArray name = p->attributeName().toUtf8();
        if (dynamicTr) {
            const QByteArray dynname = QByteArray(PROP_GENERIC_PREFIX + name);
            o->setProperty(dynname, QVariant::fromValue(strVal));
            anyTrs = trEnabled;
        }
        o->setProperty(name, text);
    }
    if (anyTrs)
        o->installEventFilter(m_trwatch);
}

QWidget *FormBuilderPrivate::create(DomUI *ui, QWidget *parentWidget)
{
    m_class = ui->elementClass().toUtf8();
    m_trwatch = 0;
    m_idBased = ui->attributeIdbasedtr();
    setTextBuilder(new TranslatingTextBuilder(m_idBased, trEnabled, m_class));
    return QFormBuilder::create(ui, parentWidget);
}

QWidget *FormBuilderPrivate::create(DomWidget *ui_widget, QWidget *parentWidget)
{
    QWidget *w = QFormBuilder::create(ui_widget, parentWidget);
    if (w == 0)
        return 0;

    if (0) {
#ifndef QT_NO_TABWIDGET
    } else if (qobject_cast<QTabWidget*>(w)) {
#endif
#ifndef QT_NO_LISTWIDGET
    } else if (qobject_cast<QListWidget*>(w)) {
#endif
#ifndef QT_NO_TREEWIDGET
    } else if (qobject_cast<QTreeWidget*>(w)) {
#endif
#ifndef QT_NO_TABLEWIDGET
    } else if (qobject_cast<QTableWidget*>(w)) {
#endif
#ifndef QT_NO_COMBOBOX
    } else if (qobject_cast<QComboBox*>(w)) {
        if (qobject_cast<QFontComboBox*>(w))
            return w;
#endif
#ifndef QT_NO_TOOLBOX
    } else if (qobject_cast<QToolBox*>(w)) {
#endif
    } else {
        return w;
    }
    if (dynamicTr && trEnabled)
        w->installEventFilter(m_trwatch);
    return w;
}

#define TRANSLATE_SUBWIDGET_PROP(mainWidget, attribute, setter, propName) \
    do { \
        if (const DomProperty *p##attribute = attributes.value(strings.attribute)) { \
            QUiTranslatableStringValue strVal; \
            const QString text = convertTranslatable(p##attribute, m_class, m_idBased, &strVal); \
            if (!text.isEmpty()) { \
                if (dynamicTr) \
                    mainWidget->widget(i)->setProperty(propName, QVariant::fromValue(strVal)); \
                mainWidget->setter(i, text); \
            } \
        } \
    } while (0)

bool FormBuilderPrivate::addItem(DomWidget *ui_widget, QWidget *widget, QWidget *parentWidget)
{
    if (parentWidget == 0)
        return true;

    if (!ParentClass::addItem(ui_widget, widget, parentWidget))
        return false;

    // Check special cases. First: Custom container
    const QString className = QLatin1String(parentWidget->metaObject()->className());
    if (!d->customWidgetAddPageMethod(className).isEmpty())
        return true;

    const QFormBuilderStrings &strings = QFormBuilderStrings::instance();

    if (0) {
#ifndef QT_NO_TABWIDGET
    } else if (QTabWidget *tabWidget = qobject_cast<QTabWidget*>(parentWidget)) {
        const DomPropertyHash attributes = propertyMap(ui_widget->elementAttribute());
        const int i = tabWidget->count() - 1;
        TRANSLATE_SUBWIDGET_PROP(tabWidget, titleAttribute, setTabText, PROP_TABPAGETEXT);
# ifndef QT_NO_TOOLTIP
        TRANSLATE_SUBWIDGET_PROP(tabWidget, toolTipAttribute, setTabToolTip, PROP_TABPAGETOOLTIP);
# endif
# ifndef QT_NO_WHATSTHIS
        TRANSLATE_SUBWIDGET_PROP(tabWidget, whatsThisAttribute, setTabWhatsThis, PROP_TABPAGEWHATSTHIS);
# endif
#endif
#ifndef QT_NO_TOOLBOX
    } else if (QToolBox *toolBox = qobject_cast<QToolBox*>(parentWidget)) {
        const DomPropertyHash attributes = propertyMap(ui_widget->elementAttribute());
        const int i = toolBox->count() - 1;
        TRANSLATE_SUBWIDGET_PROP(toolBox, labelAttribute, setItemText, PROP_TOOLITEMTEXT);
# ifndef QT_NO_TOOLTIP
        TRANSLATE_SUBWIDGET_PROP(toolBox, toolTipAttribute, setItemToolTip, PROP_TOOLITEMTOOLTIP);
# endif
#endif
    }

    return true;
}

#ifdef QFORMINTERNAL_NAMESPACE
}
#endif

class QUiLoaderPrivate
{
public:
#ifdef QFORMINTERNAL_NAMESPACE
    QFormInternal::FormBuilderPrivate builder;
#else
    FormBuilderPrivate builder;
#endif

    void setupWidgetMap() const;
};

void QUiLoaderPrivate::setupWidgetMap() const
{
    if (!g_widgets()->isEmpty())
        return;

#define DECLARE_WIDGET(a, b) g_widgets()->insert(QLatin1String(#a), true);
#define DECLARE_LAYOUT(a, b)

#include "widgets.table"

#undef DECLARE_WIDGET
#undef DECLARE_WIDGET_1
#undef DECLARE_LAYOUT
}

/*!
    \class QUiLoader
    \inmodule QtUiTools

    \brief The QUiLoader class enables standalone applications to
    dynamically create user interfaces at run-time using the
    information stored in UI files or specified in plugin paths.

    In addition, you can customize or create your own user interface by
    deriving your own loader class.

    If you have a custom component or an application that embeds \QD, you can
    also use the QFormBuilder class provided by the QtDesigner module to create
    user interfaces from UI files.

    The QUiLoader class provides a collection of functions allowing you to
    create widgets based on the information stored in UI files (created
    with \QD) or available in the specified plugin paths. The specified plugin
    paths can be retrieved using the pluginPaths() function. Similarly, the
    contents of a UI file can be retrieved using the load() function. For
    example:

    \snippet quiloader/mywidget.cpp 0

    \if !defined(qtforpython)
    By including the user interface in the form's resources (\c myform.qrc), we
    ensure that it will be present at run-time:

    \quotefile quiloader/mywidget.qrc
    \endif

    The availableWidgets() function returns a QStringList with the class names
    of the widgets available in the specified plugin paths. To create these
    widgets, simply use the createWidget() function. For example:

    \snippet quiloader/main.cpp 0

    To make a custom widget available to the loader, you can use the
    addPluginPath() function; to remove all available widgets, you can call
    the clearPluginPaths() function.

    The createAction(), createActionGroup(), createLayout(), and createWidget()
    functions are used internally by the QUiLoader class whenever it has to
    create an action, action group, layout, or widget respectively. For that
    reason, you can subclass the QUiLoader class and reimplement these
    functions to intervene the process of constructing a user interface. For
    example, you might want to have a list of the actions created when loading
    a form or creating a custom widget.

    For a complete example using the QUiLoader class, see the
    \l{Calculator Builder Example}.

    \sa {Qt UI Tools}, QFormBuilder
*/

/*!
    Creates a form loader with the given \a parent.
*/
QUiLoader::QUiLoader(QObject *parent)
    : QObject(parent), d_ptr(new QUiLoaderPrivate)
{
    Q_D(QUiLoader);

#ifndef QT_NO_DATASTREAM
    static int metaTypeId = 0;
    if (!metaTypeId) {
        metaTypeId = qRegisterMetaType<QUiTranslatableStringValue>("QUiTranslatableStringValue");
        qRegisterMetaTypeStreamOperators<QUiTranslatableStringValue>("QUiTranslatableStringValue");
    }
#endif // QT_NO_DATASTREAM
    d->builder.loader = this;

#if QT_CONFIG(library)
    QStringList paths;
    const QStringList &libraryPaths = QApplication::libraryPaths();
    for (const QString &path : libraryPaths) {
        QString libPath = path;
        libPath  += QDir::separator();
        libPath  += QStringLiteral("designer");
        paths.append(libPath);
    }

    d->builder.setPluginPath(paths);
#endif // QT_CONFIG(library)
}

/*!
    Destroys the loader.
*/
QUiLoader::~QUiLoader()
{
}

/*!
    Loads a form from the given \a device and creates a new widget with the
    given \a parentWidget to hold its contents.

    \sa createWidget(), errorString()
*/
QWidget *QUiLoader::load(QIODevice *device, QWidget *parentWidget)
{
    Q_D(QUiLoader);
    // QXmlStreamReader will report errors on open failure.
    if (!device->isOpen())
        device->open(QIODevice::ReadOnly|QIODevice::Text);
    return d->builder.load(device, parentWidget);
}

/*!
    Returns a list naming the paths in which the loader will search when
    locating custom widget plugins.

    \sa addPluginPath(), clearPluginPaths()
*/
QStringList QUiLoader::pluginPaths() const
{
    Q_D(const QUiLoader);
    return d->builder.pluginPaths();
}

/*!
    Clears the list of paths in which the loader will search when locating
    plugins.

    \sa addPluginPath(), pluginPaths()
*/
void QUiLoader::clearPluginPaths()
{
    Q_D(QUiLoader);
    d->builder.clearPluginPaths();
}

/*!
    Adds the given \a path to the list of paths in which the loader will search
    when locating plugins.

    \sa pluginPaths(), clearPluginPaths()
*/
void QUiLoader::addPluginPath(const QString &path)
{
    Q_D(QUiLoader);
    d->builder.addPluginPath(path);
}

/*!
    Creates a new widget with the given \a parent and \a name using the class
    specified by \a className. You can use this function to create any of the
    widgets returned by the availableWidgets() function.

    The function is also used internally by the QUiLoader class whenever it
    creates a widget. Hence, you can subclass QUiLoader and reimplement this
    function to intervene process of constructing a user interface or widget.
    However, in your implementation, ensure that you call QUiLoader's version
    first.

    \sa availableWidgets(), load()
*/
QWidget *QUiLoader::createWidget(const QString &className, QWidget *parent, const QString &name)
{
    Q_D(QUiLoader);
    return d->builder.defaultCreateWidget(className, parent, name);
}

/*!
    Creates a new layout with the given \a parent and \a name using the class
    specified by \a className.

    The function is also used internally by the QUiLoader class whenever it
    creates a widget. Hence, you can subclass QUiLoader and reimplement this
    function to intervene process of constructing a user interface or widget.
    However, in your implementation, ensure that you call QUiLoader's version
    first.

    \sa createWidget(), load()
*/
QLayout *QUiLoader::createLayout(const QString &className, QObject *parent, const QString &name)
{
    Q_D(QUiLoader);
    return d->builder.defaultCreateLayout(className, parent, name);
}

/*!
    Creates a new action group with the given \a parent and \a name.

    The function is also used internally by the QUiLoader class whenever it
    creates a widget. Hence, you can subclass QUiLoader and reimplement this
    function to intervene process of constructing a user interface or widget.
    However, in your implementation, ensure that you call QUiLoader's version
    first.

    \sa createAction(), createWidget(), load()
 */
QActionGroup *QUiLoader::createActionGroup(QObject *parent, const QString &name)
{
    Q_D(QUiLoader);
    return d->builder.defaultCreateActionGroup(parent, name);
}

/*!
    Creates a new action with the given \a parent and \a name.

    The function is also used internally by the QUiLoader class whenever it
    creates a widget. Hence, you can subclass QUiLoader and reimplement this
    function to intervene process of constructing a user interface or widget.
    However, in your implementation, ensure that you call QUiLoader's version
    first.

    \sa createActionGroup(), createWidget(), load()
*/
QAction *QUiLoader::createAction(QObject *parent, const QString &name)
{
    Q_D(QUiLoader);
    return d->builder.defaultCreateAction(parent, name);
}

/*!
    Returns a list naming all available widgets that can be built using the
    createWidget() function, i.e all the widgets specified within the given
    plugin paths.

    \sa pluginPaths(), createWidget()

*/
QStringList QUiLoader::availableWidgets() const
{
    Q_D(const QUiLoader);

    d->setupWidgetMap();
    widget_map available = *g_widgets();

    const auto &customWidgets = d->builder.customWidgets();
    for (QDesignerCustomWidgetInterface *plugin : customWidgets)
        available.insert(plugin->name(), true);

    return available.keys();
}


/*!
    \since 4.5
    Returns a list naming all available layouts that can be built using the
    createLayout() function

    \sa createLayout()
*/

QStringList QUiLoader::availableLayouts() const
{
    QStringList rc;
#define DECLARE_WIDGET(a, b)
#define DECLARE_LAYOUT(a, b) rc.push_back(QLatin1String(#a));

#include "widgets.table"

#undef DECLARE_WIDGET
#undef DECLARE_LAYOUT
    return rc;
}

/*!
    Sets the working directory of the loader to \a dir. The loader will look
    for other resources, such as icons and resource files, in paths relative to
    this directory.

    \sa workingDirectory()
*/

void QUiLoader::setWorkingDirectory(const QDir &dir)
{
    Q_D(QUiLoader);
    d->builder.setWorkingDirectory(dir);
}

/*!
    Returns the working directory of the loader.

    \sa setWorkingDirectory()
*/

QDir QUiLoader::workingDirectory() const
{
    Q_D(const QUiLoader);
    return d->builder.workingDirectory();
}
/*!
    \since 4.5

    If \a enabled is true, user interfaces loaded by this loader will
    automatically retranslate themselves upon receiving a language change
    event. Otherwise, the user interfaces will not be retranslated.

    \sa isLanguageChangeEnabled()
*/

void QUiLoader::setLanguageChangeEnabled(bool enabled)
{
    Q_D(QUiLoader);
    d->builder.dynamicTr = enabled;
}

/*!
    \since 4.5

    Returns true if dynamic retranslation on language change is enabled;
    returns false otherwise.

    \sa setLanguageChangeEnabled()
*/

bool QUiLoader::isLanguageChangeEnabled() const
{
    Q_D(const QUiLoader);
    return d->builder.dynamicTr;
}

/*!
    \internal
    \since 4.5

    If \a enabled is true, user interfaces loaded by this loader will be
    translated. Otherwise, the user interfaces will not be translated.

    \note This is orthogonal to languageChangeEnabled.

    \sa isLanguageChangeEnabled(), setLanguageChangeEnabled()
*/

void QUiLoader::setTranslationEnabled(bool enabled)
{
    Q_D(QUiLoader);
    d->builder.trEnabled = enabled;
}

/*!
    \internal
    \since 4.5

    Returns true if translation is enabled; returns false otherwise.

    \sa setTranslationEnabled()
*/

bool QUiLoader::isTranslationEnabled() const
{
    Q_D(const QUiLoader);
    return d->builder.trEnabled;
}

/*!
    Returns a human-readable description of the last error occurred in load().

    \since 5.0
    \sa load()
*/

QString QUiLoader::errorString() const
{
    Q_D(const QUiLoader);
    return d->builder.errorString();
}

QT_END_NAMESPACE

#include "quiloader.moc"