8#include <QtUiPlugin/customwidget.h>
10#include <formbuilder.h>
11#include <formbuilderextra_p.h>
12#include <textbuilder_p.h>
15#include <QtWidgets/qapplication.h>
16#include <QtWidgets/qlayout.h>
17#include <QtWidgets/qwidget.h>
18#include <QtWidgets/qtabwidget.h>
19#include <QtWidgets/qtreewidget.h>
20#include <QtWidgets/qlistwidget.h>
21#include <QtWidgets/qtablewidget.h>
22#include <QtWidgets/qtoolbox.h>
23#include <QtWidgets/qcombobox.h>
24#include <QtWidgets/qfontcombobox.h>
26#include <QtGui/qaction.h>
27#include <QtGui/qactiongroup.h>
29#include <QtCore/qdebug.h>
30#include <QtCore/qdatastream.h>
31#include <QtCore/qmap.h>
32#include <QtCore/qdir.h>
33#include <QtCore/qlibraryinfo.h>
43#ifndef QT_NO_DATASTREAM
48 out << s.qualifier() << s.value();
54 QByteArray qualifier, value;
55 in >> qualifier >> value;
56 s.setQualifier(qualifier);
62QString QUiTranslatableStringValue::translate(
const QByteArray &className,
bool idBased)
const
65 ? qtTrId(m_qualifier.constData())
66 : QCoreApplication::translate(className.constData(), m_value.constData(), m_qualifier.constData());
69#ifdef QFORMINTERNAL_NAMESPACE
70namespace QFormInternal
84 bool idBased()
const {
return m_idBased; }
89 QByteArray m_className;
94 const DomString *str = text->elementString();
97 if (str->hasAttributeNotr()) {
98 const QString notr = str->attributeNotr();
99 if (notr == QStringLiteral(
"true") || notr == QStringLiteral(
"yes"))
100 return QVariant::fromValue(str->text());
102 QUiTranslatableStringValue strVal;
103 strVal.setValue(str->text().toUtf8());
105 strVal.setQualifier(str->attributeId().toUtf8());
106 else if (str->hasAttributeComment())
107 strVal.setQualifier(str->attributeComment().toUtf8());
108 return QVariant::fromValue(strVal);
113 if (value.canConvert<QUiTranslatableStringValue>()) {
114 QUiTranslatableStringValue tsv = qvariant_cast<QUiTranslatableStringValue>(value);
116 return QString::fromUtf8(tsv.value().constData());
117 return QVariant::fromValue(tsv.translate(m_className, m_idBased));
119 if (value.canConvert<QString>())
120 return QVariant::fromValue(qvariant_cast<QString>(value));
126 { Qt::DisplayRole, Qt::DisplayPropertyRole },
127#if QT_CONFIG(tooltip)
128 { Qt::ToolTipRole, Qt::ToolTipPropertyRole },
130#if QT_CONFIG(statustip)
131 { Qt::StatusTipRole, Qt::StatusTipPropertyRole },
133#if QT_CONFIG(whatsthis)
134 { Qt::WhatsThisRole, Qt::WhatsThisPropertyRole },
143 int cnt = item->columnCount();
144 for (
int i = 0; i < cnt; ++i) {
145 for (
unsigned j = 0; irs[j]
.shadowRole >= 0; j++) {
146 QVariant v = item->data(i, irs[j].shadowRole);
148 QUiTranslatableStringValue tsv = qvariant_cast<QUiTranslatableStringValue>(v);
149 item->setData(i, irs[j]
.realRole, tsv.translate(class_name, idBased));
154 cnt = item->childCount();
155 for (
int i = 0; i < cnt; ++i)
156 recursiveReTranslate(item->child(i), class_name, idBased);
164 for (
unsigned j = 0; irs[j]
.shadowRole >= 0; j++) {
165 QVariant v = item->data(irs[j].shadowRole);
167 QUiTranslatableStringValue tsv = qvariant_cast<QUiTranslatableStringValue>(v);
168 item->setData(irs[j]
.realRole, tsv.translate(class_name, idBased));
176 reTranslateWidgetItem(item, class_name, idBased);
179#define RETRANSLATE_SUBWIDGET_PROP(mainWidget, setter, propName)
181 QVariant v = mainWidget->widget(i)->property(propName);
183 QUiTranslatableStringValue tsv = qvariant_cast<QUiTranslatableStringValue>(v);
184 mainWidget->setter(i, tsv.translate(m_className, m_idBased));
213#if QT_CONFIG(tabwidget)
216 for (
int i = 0;
i <
cnt; ++
i) {
218#if QT_CONFIG(tooltip)
221#if QT_CONFIG(whatsthis)
226#if QT_CONFIG(listwidget)
229 for (
int i = 0;
i <
cnt; ++
i)
232#if QT_CONFIG(treewidget)
237 for (
int i = 0;
i <
cnt; ++
i) {
242#if QT_CONFIG(tablewidget)
254#if QT_CONFIG(combobox)
258 for (
int i = 0;
i <
cnt; ++
i) {
267#if QT_CONFIG(toolbox)
270 for (
int i = 0;
i <
cnt; ++
i) {
272#if QT_CONFIG(tooltip)
303 return ParentClass::createWidget(className, parent, name);
308 return ParentClass::createLayout(className, parent, name);
313 return ParentClass::createAction(parent, name);
318 return ParentClass::createActionGroup(parent, name);
323 if (
QWidget *widget =
loader->createWidget(className, parent, name)) {
324 widget->setObjectName(name);
333 if (QLayout *layout =
loader->createLayout(className, parent, name)) {
334 layout->setObjectName(name);
343 if (QActionGroup *actionGroup =
loader->createActionGroup(parent, name)) {
344 actionGroup->setObjectName(name);
353 if (QAction *action =
loader->createAction(parent, name)) {
354 action->setObjectName(name);
369 bool m_idBased =
false;
373 bool idBased, QUiTranslatableStringValue *strVal)
375 if (p->kind() != DomProperty::String)
377 const DomString *dom_str = p->elementString();
380 if (dom_str->hasAttributeNotr()) {
381 const QString notr = dom_str->attributeNotr();
382 if (notr == QStringLiteral(
"yes") || notr == QStringLiteral(
"true"))
385 strVal->setValue(dom_str->text().toUtf8());
386 strVal->setQualifier(idBased ? dom_str->attributeId().toUtf8() : dom_str->attributeComment().toUtf8());
387 if (strVal->value().isEmpty() && strVal->qualifier().isEmpty())
389 return strVal->translate(className, idBased);
394 QFormBuilder::applyProperties(o, properties);
397 m_trwatch =
new TranslationWatcher(o, m_class, m_idBased);
399 if (properties.isEmpty())
406 for (
const DomProperty *p : properties) {
407 QUiTranslatableStringValue strVal;
408 const QString text = convertTranslatable(p, m_class, m_idBased, &strVal);
411 const QByteArray name = p->attributeName().toUtf8();
414 o->setProperty(dynname, QVariant::fromValue(strVal));
417 if (p->elementString()->text() != text)
418 o->setProperty(name, text);
421 o->installEventFilter(m_trwatch);
426 m_class = ui->elementClass().toUtf8();
428 m_idBased = ui->attributeIdbasedtr();
429 setTextBuilder(
new TranslatingTextBuilder(m_idBased, trEnabled, m_class));
430 return QFormBuilder::create(ui, parentWidget);
435 QWidget *w = QFormBuilder::create(ui_widget, parentWidget);
440#if QT_CONFIG(tabwidget)
441 }
else if (qobject_cast<QTabWidget*>(w)) {
443#if QT_CONFIG(listwidget)
444 }
else if (qobject_cast<QListWidget*>(w)) {
446#if QT_CONFIG(treewidget)
447 }
else if (qobject_cast<QTreeWidget*>(w)) {
449#if QT_CONFIG(tablewidget)
450 }
else if (qobject_cast<QTableWidget*>(w)) {
452#if QT_CONFIG(combobox)
453 }
else if (qobject_cast<QComboBox*>(w)) {
454 if (qobject_cast<QFontComboBox*>(w))
457#if QT_CONFIG(toolbox)
458 }
else if (qobject_cast<QToolBox*>(w)) {
464 w->installEventFilter(m_trwatch);
468#define TRANSLATE_SUBWIDGET_PROP(mainWidget, attribute, setter, propName)
470 if (const auto *p = attributes.value(attribute)) {
471 QUiTranslatableStringValue strVal;
472 const QString text = convertTranslatable(p, m_class, m_idBased, &strVal);
473 if (!text.isEmpty()) {
475 mainWidget->widget(i)->setProperty(propName, QVariant::fromValue(strVal));
476 mainWidget->setter(i, text);
483 if (parentWidget ==
nullptr)
486 if (!ParentClass::addItem(ui_widget, widget, parentWidget))
490 const QString className = QLatin1String(parentWidget->metaObject()->className());
491 if (!d->customWidgetAddPageMethod(className).isEmpty())
495#if QT_CONFIG(tabwidget)
496 }
else if (QTabWidget *tabWidget = qobject_cast<QTabWidget*>(parentWidget)) {
497 const DomPropertyHash attributes = propertyMap(ui_widget->elementAttribute());
498 const int i = tabWidget->count() - 1;
499 TRANSLATE_SUBWIDGET_PROP(tabWidget, QFormBuilderStrings::titleAttribute,
500 setTabText, PROP_TABPAGETEXT);
501#if QT_CONFIG(tooltip)
502 TRANSLATE_SUBWIDGET_PROP(tabWidget, QFormBuilderStrings::toolTipAttribute,
503 setTabToolTip, PROP_TABPAGETOOLTIP);
505#if QT_CONFIG(whatsthis)
506 TRANSLATE_SUBWIDGET_PROP(tabWidget, QFormBuilderStrings::whatsThisAttribute,
507 setTabWhatsThis, PROP_TABPAGEWHATSTHIS);
510#if QT_CONFIG(toolbox)
511 }
else if (QToolBox *toolBox = qobject_cast<QToolBox*>(parentWidget)) {
512 const DomPropertyHash attributes = propertyMap(ui_widget->elementAttribute());
513 const int i = toolBox->count() - 1;
514 TRANSLATE_SUBWIDGET_PROP(toolBox, QFormBuilderStrings::labelAttribute,
515 setItemText, PROP_TOOLITEMTEXT);
516#if QT_CONFIG(tooltip)
517 TRANSLATE_SUBWIDGET_PROP(toolBox, QFormBuilderStrings::toolTipAttribute,
518 setItemToolTip, PROP_TOOLITEMTOOLTIP);
526#ifdef QFORMINTERNAL_NAMESPACE
533#ifdef QFORMINTERNAL_NAMESPACE
544 if (!g_widgets()->isEmpty())
547#define DECLARE_WIDGET(a, b) g_widgets()->insert(QLatin1String(#a), true);
548#define DECLARE_LAYOUT(a, b)
550#include "widgets.table"
553#undef DECLARE_WIDGET_1
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
613
614
615QUiLoader::QUiLoader(QObject *parent)
616 : QObject(parent), d_ptr(
new QUiLoaderPrivate)
620#ifndef QT_NO_DATASTREAM
621 static int metaTypeId = 0;
623 metaTypeId = qRegisterMetaType<QUiTranslatableStringValue>(
"QUiTranslatableStringValue");
626 d->builder.loader =
this;
628#if QT_CONFIG(library)
630 const QStringList &libraryPaths = QApplication::libraryPaths();
631 for (
const QString &path : libraryPaths) {
632 QString libPath = path;
633 libPath += QDir::separator();
634 libPath += QStringLiteral(
"designer");
635 paths.append(libPath);
638 d->builder.setPluginPath(paths);
643
644
645QUiLoader::~QUiLoader() =
default;
648
649
650
651
652
653QWidget *QUiLoader::load(QIODevice *device, QWidget *parentWidget)
657 if (!device->isOpen())
658 device->open(QIODevice::ReadOnly|QIODevice::Text);
659 return d->builder.load(device, parentWidget);
663
664
665
666
667
668QStringList QUiLoader::pluginPaths()
const
670 Q_D(
const QUiLoader);
671 return d->builder.pluginPaths();
675
676
677
678
679
680void QUiLoader::clearPluginPaths()
683 d->builder.clearPluginPaths();
687
688
689
690
691
692void QUiLoader::addPluginPath(
const QString &path)
695 d->builder.addPluginPath(path);
699
700
701
702
703
704
705
706
707
708
709
710
711QWidget *QUiLoader::createWidget(
const QString &className, QWidget *parent,
const QString &name)
714 return d->builder.defaultCreateWidget(className, parent, name);
718
719
720
721
722
723
724
725
726
727
728
729QLayout *QUiLoader::createLayout(
const QString &className, QObject *parent,
const QString &name)
732 return d->builder.defaultCreateLayout(className, parent, name);
736
737
738
739
740
741
742
743
744
745
746QActionGroup *QUiLoader::createActionGroup(QObject *parent,
const QString &name)
749 return d->builder.defaultCreateActionGroup(parent, name);
753
754
755
756
757
758
759
760
761
762
763QAction *QUiLoader::createAction(QObject *parent,
const QString &name)
766 return d->builder.defaultCreateAction(parent, name);
770
771
772
773
774
775
776
777QStringList QUiLoader::availableWidgets()
const
779 Q_D(
const QUiLoader);
782 widget_map available = *g_widgets();
784 const auto &customWidgets = d->builder.customWidgets();
785 for (QDesignerCustomWidgetInterface *plugin : customWidgets)
786 available.insert(plugin->name(),
true);
788 return available.keys();
793
794
795
796
797
798
800QStringList QUiLoader::availableLayouts()
const
803#define DECLARE_WIDGET(a, b)
804#define DECLARE_LAYOUT(a, b) rc.push_back(QLatin1String(#a));
806#include "widgets.table"
814
815
816
817
818
819
821void QUiLoader::setWorkingDirectory(
const QDir &dir)
824 d->builder.setWorkingDirectory(dir);
828
829
830
831
833QDir QUiLoader::workingDirectory()
const
835 Q_D(
const QUiLoader);
836 return d->builder.workingDirectory();
839
840
841
842
843
844
845
846
848void QUiLoader::setLanguageChangeEnabled(
bool enabled)
851 d->builder.dynamicTr = enabled;
855
856
857
858
859
860
861
863bool QUiLoader::isLanguageChangeEnabled()
const
865 Q_D(
const QUiLoader);
866 return d->builder.dynamicTr;
870
871
872
873
874
875
876
877
878
879
881void QUiLoader::setTranslationEnabled(
bool enabled)
884 d->builder.trEnabled = enabled;
888
889
890
891
892
893
894
896bool QUiLoader::isTranslationEnabled()
const
898 Q_D(
const QUiLoader);
899 return d->builder.trEnabled;
903
904
905
906
907
909QString QUiLoader::errorString()
const
911 Q_D(
const QUiLoader);
912 return d->builder.errorString();
917#include "quiloader.moc"
friend class QT_PREPEND_NAMESPACE(QQmlJS::LoggerCategory)
void setupWidgetMap() const
FormBuilderPrivate builder
QVariant toNativeValue(const QVariant &value) const override
QVariant loadText(const DomProperty *icon) const override
TranslatingTextBuilder(bool idBased, bool trEnabled, const QByteArray &className)
Combined button and popup list for selecting options.
Q_GLOBAL_STATIC(QReadWriteLock, g_updateMutex)
const QUiItemRolePair qUiItemRoles[]
static void recursiveReTranslate(QTreeWidgetItem *item, const QByteArray &class_name, bool idBased)
static void reTranslateWidgetItem(T *item, const QByteArray &class_name, bool idBased)
QT_BEGIN_NAMESPACE typedef QMap< QString, bool > widget_map
static QString convertTranslatable(const DomProperty *p, const QByteArray &className, bool idBased, QUiTranslatableStringValue *strVal)
static void reTranslateTableItem(QTableWidgetItem *item, const QByteArray &class_name, bool idBased)
#define PROP_GENERIC_PREFIX