9#include <QtWidgets/QApplication>
10#include <QtWidgets/QFontComboBox>
11#include <QtWidgets/QFrame>
12#include <QtWidgets/QGridLayout>
13#include <QtWidgets/QListWidget>
14#include <QtWidgets/QMdiArea>
15#include <QtWidgets/QMdiSubWindow>
16#include <QtWidgets/QMenu>
17#include <QtWidgets/QStackedLayout>
18#include <QtWidgets/QStackedWidget>
19#include <QtWidgets/QTableWidget>
20#include <QtWidgets/QTabWidget>
21#include <QtWidgets/QToolBox>
22#include <QtWidgets/QTreeWidget>
23#include <QtWidgets/QScrollArea>
25#include <QtGui/QAction>
27#include <QtCore/QDebug>
28#include <QtCore/QTime>
32#if defined(Q_CC_SUN) || defined(Q_CC_HPACC) || defined(Q_CC_XLC)
33size_t qHash(
const QUiTranslatableStringValue &tsv)
38 return qHash(tsv.value()) ^ qHash(tsv.qualifier());
41static bool operator==(
const QUiTranslatableStringValue &tsv1,
const QUiTranslatableStringValue &tsv2)
43 return tsv1.value() == tsv2.value() && tsv1.qualifier() == tsv2.qualifier();
46#define INSERT_TARGET(_tsv, _type, _target, _prop)
49 target.target._target;
51 (*targets)[qvariant_cast<QUiTranslatableStringValue>(_tsv)].append(target);
56 const QUiItemRolePair *irs = QFormInternal::qUiItemRoles;
58 int cnt = item->columnCount();
59 for (
int i = 0; i < cnt; ++i) {
60 for (
unsigned j = 0; irs[j].shadowRole >= 0; j++) {
61 QVariant v = item->data(i, irs[j].shadowRole);
64 target.prop.treeIndex
.column = i;
70 cnt = item->childCount();
71 for (
int j = 0; j < cnt; ++j)
72 registerTreeItem(item->child(j), targets);
75#define REGISTER_ITEM_CORE(item, propType, targetName)
76 const QUiItemRolePair *irs = QFormInternal::qUiItemRoles;
77 for (unsigned j = 0
; irs[j].shadowRole >= 0
; j++) {
78 QVariant v = item->data(irs[j].shadowRole);
98#define REGISTER_SUBWIDGET_PROP(mainWidget, propType, propName)
100 QVariant v = mainWidget->widget(i)->property(propName);
109 const auto propNames = o->dynamicPropertyNames();
110 for (
const QByteArray &prop : propNames) {
111 if (prop.startsWith(PROP_GENERIC_PREFIX)) {
112 const QByteArray propName = prop.mid(
sizeof(PROP_GENERIC_PREFIX) - 1);
114 TranslatableProperty, object = o, name = qstrdup(propName.data()));
118#ifndef QT_NO_TABWIDGET
119 }
else if (QTabWidget *tabw = qobject_cast<QTabWidget*>(o)) {
120 const int cnt = tabw->count();
121 for (
int i = 0; i < cnt; ++i) {
123# ifndef QT_NO_TOOLTIP
126# ifndef QT_NO_WHATSTHIS
132 }
else if (QToolBox *toolw = qobject_cast<QToolBox*>(o)) {
133 const int cnt = toolw->count();
134 for (
int i = 0; i < cnt; ++i) {
136# ifndef QT_NO_TOOLTIP
141#ifndef QT_NO_COMBOBOX
142 }
else if (QComboBox *combow = qobject_cast<QComboBox*>(o)) {
143 if (!qobject_cast<QFontComboBox*>(o)) {
144 const int cnt = combow->count();
145 for (
int i = 0; i < cnt; ++i) {
146 const QVariant v = combow->itemData(i, Qt::DisplayPropertyRole);
152#ifndef QT_NO_LISTWIDGET
153 }
else if (QListWidget *listw = qobject_cast<QListWidget*>(o)) {
154 const int cnt = listw->count();
155 for (
int i = 0; i < cnt; ++i)
156 registerListItem(listw->item(i), targets);
158#ifndef QT_NO_TABLEWIDGET
159 }
else if (QTableWidget *tablew = qobject_cast<QTableWidget*>(o)) {
160 const int row_cnt = tablew->rowCount();
161 const int col_cnt = tablew->columnCount();
162 for (
int j = 0; j < col_cnt; ++j)
163 registerTableItem(tablew->horizontalHeaderItem(j), targets);
164 for (
int i = 0; i < row_cnt; ++i) {
165 registerTableItem(tablew->verticalHeaderItem(i), targets);
166 for (
int j = 0; j < col_cnt; ++j)
167 registerTableItem(tablew->item(i, j), targets);
170#ifndef QT_NO_TREEWIDGET
171 }
else if (QTreeWidget *treew = qobject_cast<QTreeWidget*>(o)) {
172 if (QTreeWidgetItem *item = treew->headerItem())
173 registerTreeItem(item, targets);
174 const int cnt = treew->topLevelItemCount();
175 for (
int i = 0; i < cnt; ++i)
176 registerTreeItem(treew->topLevelItem(i), targets);
179 for (QObject *co : o->children())
180 buildTargets(co, targets);
185 for (
const auto &targetList : std::as_const(*targets))
186 for (
const TranslatableEntry &target : targetList)
187 if (target.type == TranslatableProperty)
188 delete target.prop.name;
194 switch (target
.type) {
196 target.target.object->setProperty(target.prop
.name, text);
198#ifndef QT_NO_TABWIDGET
200 target.target.tabWidget->setTabText(target.prop
.index, text);
202# ifndef QT_NO_TOOLTIP
204 target.target.tabWidget->setTabToolTip(target.prop
.index, text);
207# ifndef QT_NO_WHATSTHIS
209 target.target.tabWidget->setTabWhatsThis(target.prop
.index, text);
215 target.target.toolBox->setItemText(target.prop
.index, text);
217# ifndef QT_NO_TOOLTIP
219 target.target.toolBox->setItemToolTip(target.prop
.index, text);
223#ifndef QT_NO_COMBOBOX
225 target.target.comboBox->setItemText(target.prop
.index, text);
228#ifndef QT_NO_LISTWIDGET
230 target.target.listWidgetItem->setData(target.prop
.index, text);
233#ifndef QT_NO_TABLEWIDGET
235 target.target.tableWidgetItem->setData(target.prop
.index, text);
238#ifndef QT_NO_TREEWIDGET
240 target.target.treeWidgetItem->setData(target.prop.treeIndex
.column, target.prop.treeIndex
.index, text);
247 const QList<TranslatableEntry> &targets,
const QUiTranslatableStringValue &tsv,
248 const DataModel *dataModel,
const QString &className)
250 QString sourceText = QString::fromUtf8(tsv.value());
253 className, sourceText, QString::fromUtf8(tsv.qualifier())))
254 text = msg->translation();
255 if (text.isEmpty() && !tsv.value().isEmpty())
256 text = QLatin1Char(
'#') + sourceText;
258 for (
const TranslatableEntry &target : targets)
259 retranslateTarget(target, text);
264 for (;
QWidget *pw = w->parentWidget(); w = pw) {
265#ifndef QT_NO_STACKEDWIDGET
266 if (QStackedWidget *stack = qobject_cast<QStackedWidget *>(pw)) {
267#ifndef QT_NO_TABWIDGET
270 if (QTabWidget *tab = qobject_cast<QTabWidget *>(stack->parent()))
271 tab->setCurrentWidget(w);
274 stack->setCurrentWidget(w);
279 if (QScrollArea *sv = qobject_cast<QScrollArea *>(pw)) {
280 if (QToolBox *tb = qobject_cast<QToolBox *>(sv->parent()))
281 tb->setCurrentWidget(w);
289 QVariant br = item->data(col, Qt::BackgroundRole + 500);
290 QVariant fr = item->data(col, Qt::ForegroundRole + 500);
292 if (!br.isValid() && !fr.isValid()) {
293 item->setData(col, Qt::BackgroundRole + 500, item->data(col, Qt::BackgroundRole));
294 item->setData(col, Qt::ForegroundRole + 500, item->data(col, Qt::ForegroundRole));
295 QPalette pal = qApp->palette();
296 item->setData(col, Qt::BackgroundRole, pal.color(QPalette::Dark));
297 item->setData(col, Qt::ForegroundRole, pal.color(QPalette::Light));
300 if (br.isValid() || fr.isValid()) {
301 item->setData(col, Qt::BackgroundRole, br);
302 item->setData(col, Qt::ForegroundRole, fr);
303 item->setData(col, Qt::BackgroundRole + 500, QVariant());
304 item->setData(col, Qt::ForegroundRole + 500, QVariant());
312 QVariant br = item->data(Qt::BackgroundRole + 500);
313 QVariant fr = item->data(Qt::ForegroundRole + 500);
315 if (!br.isValid() && !fr.isValid()) {
316 item->setData(Qt::BackgroundRole + 500, item->data(Qt::BackgroundRole));
317 item->setData(Qt::ForegroundRole + 500, item->data(Qt::ForegroundRole));
318 QPalette pal = qApp->palette();
319 item->setData(Qt::BackgroundRole, pal.color(QPalette::Dark));
320 item->setData(Qt::ForegroundRole, pal.color(QPalette::Light));
323 if (br.isValid() || fr.isValid()) {
324 item->setData(Qt::BackgroundRole, br);
325 item->setData(Qt::ForegroundRole, fr);
326 item->setData(Qt::BackgroundRole + 500, QVariant());
327 item->setData(Qt::ForegroundRole + 500, QVariant());
332#define AUTOFILL_BACKUP_PROP "_q_linguist_autoFillBackup"
333#define PALETTE_BACKUP_PROP "_q_linguist_paletteBackup"
334#define FONT_BACKUP_PROP "_q_linguist_fontBackup"
342 if (!bak.isValid()) {
343 QFont fnt = qApp->font();
344 a->setProperty(
FONT_BACKUP_PROP, QVariant::fromValue(a->font().resolve(fnt)));
351 a->setFont(qvariant_cast<QFont>(bak));
355 for (QObject *o : a->associatedObjects()) {
356 if (QWidget *w = qobject_cast<QWidget *>(o))
357 highlightWidget(w, on);
365 if (!bak.isValid()) {
366 QPalette pal = qApp->palette();
367 for (QObject *co : w->children())
368 if (QWidget *cw = qobject_cast<QWidget *>(co))
369 cw->setPalette(cw->palette().resolve(pal));
372 QColor col1 = pal.color(QPalette::Dark);
373 QColor col2 = pal.color(QPalette::Light);
374 pal.setColor(QPalette::Base, col1);
375 pal.setColor(QPalette::Window, col1);
376 pal.setColor(QPalette::Button, col1);
377 pal.setColor(QPalette::Text, col2);
378 pal.setColor(QPalette::WindowText, col2);
379 pal.setColor(QPalette::ButtonText, col2);
380 pal.setColor(QPalette::BrightText, col2);
382 w->setAutoFillBackground(
true);
386 w->setPalette(qvariant_cast<QPalette>(bak));
392 if (QMenu *m = qobject_cast<QMenu *>(w))
394 highlightAction(m->menuAction(), on);
399 switch (target
.type) {
401 if (QAction *a = qobject_cast<QAction *>(target.target.object)) {
403 }
else if (
QWidget *w = qobject_cast<
QWidget *>(target.target.object)) {
408#ifndef QT_NO_COMBOBOX
410 static_cast<QComboBox *>(target.target.object)->setCurrentIndex(target.prop
.index);
411 goto frontAndHighlight;
413#ifndef QT_NO_TABWIDGET
415 static_cast<QTabWidget *>(target.target.object)->setCurrentIndex(target.prop
.index);
416 goto frontAndHighlight;
417# ifndef QT_NO_TOOLTIP
420# ifndef QT_NO_WHATSTHIS
426# ifndef QT_NO_TOOLTIP
430#if !defined(QT_NO_COMBOBOX) || !defined(QT_NO_TABWIDGET)
436#ifndef QT_NO_LISTWIDGET
438 bringToFront(target.target.listWidgetItem->listWidget());
439 highlightWidgetItem(target.target.listWidgetItem, on);
442#ifndef QT_NO_TABLEWIDGET
444 bringToFront(target.target.tableWidgetItem->tableWidget());
445 highlightWidgetItem(target.target.tableWidgetItem, on);
448#ifndef QT_NO_TREEWIDGET
450 bringToFront(target.target.treeWidgetItem->treeWidget());
451 highlightTreeWidgetItem(target.target.treeWidgetItem, target.prop.treeIndex
.column, on);
459 for (
const TranslatableEntry &target : targets)
460 highlightTarget(target, on);
464 : QMainWindow(parent), m_form(0), m_dataModel(dataModel)
466 m_mdiSubWindow =
new QMdiSubWindow;
467 m_mdiSubWindow->setWindowFlags(m_mdiSubWindow->windowFlags() & ~Qt::WindowSystemMenuHint);
468 m_mdiArea =
new QMdiArea(
this);
469 m_mdiArea->addSubWindow(m_mdiSubWindow);
470 setCentralWidget(m_mdiArea);
471 m_mdiArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
472 m_mdiArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
477 if (model < 0 || !messageItem) {
482 QDir dir = QFileInfo(m_dataModel->srcFileName(model)).dir();
483 QString fileName = QDir::cleanPath(dir.absoluteFilePath(messageItem->fileName()));
484 if (m_lastFormName != fileName) {
487 m_lastFormName.clear();
488 m_highlights.clear();
489 destroyTargets(&m_targets);
491 static QUiLoader *uiLoader;
493 uiLoader =
new QUiLoader(
this);
494 uiLoader->setLanguageChangeEnabled(
true);
495 uiLoader->setTranslationEnabled(
false);
498 QFile file(fileName);
499 if (!file.open(QIODevice::ReadOnly)) {
500 qDebug() <<
"CANNOT OPEN FORM" << fileName;
501 m_mdiSubWindow->hide();
504 m_form = uiLoader->load(&file, m_mdiSubWindow);
506 qDebug() <<
"CANNOT LOAD FORM" << fileName;
507 m_mdiSubWindow->hide();
511 buildTargets(m_form, &m_targets);
513 setToolTip(fileName);
515 m_form->setWindowFlags(Qt::Widget);
516 m_form->setWindowModality(Qt::NonModal);
517 m_form->setFocusPolicy(Qt::NoFocus);
519 m_mdiSubWindow->setWidget(m_form);
520 m_mdiSubWindow->setWindowTitle(m_form->windowTitle());
521 m_mdiSubWindow->show();
522 m_mdiArea->cascadeSubWindows();
523 m_lastFormName = fileName;
524 m_lastClassName = messageItem->context();
527 highlightTargets(m_highlights,
false);
529 QUiTranslatableStringValue tsv;
530 tsv.setValue(messageItem->text().toUtf8());
531 tsv.setQualifier(messageItem->comment().toUtf8());
532 m_highlights = m_targets.value(tsv);
533 if (m_lastModel != model) {
534 for (
auto it = m_targets.cbegin(), end = m_targets.cend(); it != end; ++it)
535 retranslateTargets(*it, it.key(), m_dataModel->model(model), m_lastClassName);
538 retranslateTargets(m_highlights, tsv, m_dataModel->model(model), m_lastClassName);
540 highlightTargets(m_highlights,
true);
Combined button and popup list for selecting options.
TranslatableEntryType type