From 0afad0c2301cd50fe21cbd1a1379034d6e6f1048 Mon Sep 17 00:00:00 2001 From: zccrs Date: Fri, 20 Jul 2018 17:47:27 +0800 Subject: [PATCH] feat: support set the translate context on DSettingsDialog::updateSettings Change-Id: I3c8ddecf05e9b69e0fcaf037eb1f84385bddba4e --- debian/libdtkwidget2.symbols | 2 - src/widgets/dsettingsdialog.cpp | 9 ++- src/widgets/dsettingsdialog.h | 1 + src/widgets/dsettingswidgetfactory.cpp | 62 ++++++++++++++++----- src/widgets/dsettingswidgetfactory.h | 2 + src/widgets/private/settings/content.cpp | 11 ++-- src/widgets/private/settings/content.h | 2 +- src/widgets/private/settings/navigation.cpp | 9 ++- src/widgets/private/settings/navigation.h | 2 +- 9 files changed, 72 insertions(+), 28 deletions(-) diff --git a/debian/libdtkwidget2.symbols b/debian/libdtkwidget2.symbols index ac0a3310b..49bcc13ab 100644 --- a/debian/libdtkwidget2.symbols +++ b/debian/libdtkwidget2.symbols @@ -408,7 +408,6 @@ libdtkwidget.so.2 libdtkwidget2 #MINVER# _ZN3Dtk6Widget10Navigation11qt_metacastEPKc@Base 2.0.7.2 _ZN3Dtk6Widget10Navigation13onSelectGroupERK7QString@Base 2.0.7.2 _ZN3Dtk6Widget10Navigation13selectedGroupERK7QString@Base 2.0.7.2 - _ZN3Dtk6Widget10Navigation14updateSettingsE8QPointerINS_4Core9DSettingsEE@Base 2.0.7.2 _ZN3Dtk6Widget10Navigation16staticMetaObjectE@Base 2.0.7.2 _ZN3Dtk6Widget10NavigationC1EP7QWidget@Base 2.0.7.2 _ZN3Dtk6Widget10NavigationC2EP7QWidget@Base 2.0.7.2 @@ -2625,7 +2624,6 @@ libdtkwidget.so.2 libdtkwidget2 #MINVER# _ZN3Dtk6Widget7Content11qt_metacallEN11QMetaObject4CallEiPPv@Base 2.0.7.2 _ZN3Dtk6Widget7Content11qt_metacastEPKc@Base 2.0.7.2 _ZN3Dtk6Widget7Content13scrollToGroupERK7QString@Base 2.0.7.2 - _ZN3Dtk6Widget7Content14updateSettingsE8QPointerINS_4Core9DSettingsEE@Base 2.0.7.2 _ZN3Dtk6Widget7Content15onScrollToGroupERK7QString@Base 2.0.7.2 _ZN3Dtk6Widget7Content16staticMetaObjectE@Base 2.0.7.2 _ZN3Dtk6Widget7ContentC1EP7QWidget@Base 2.0.7.2 diff --git a/src/widgets/dsettingsdialog.cpp b/src/widgets/dsettingsdialog.cpp index 74a5f5bf8..8d36e19df 100644 --- a/src/widgets/dsettingsdialog.cpp +++ b/src/widgets/dsettingsdialog.cpp @@ -105,10 +105,15 @@ DSettingsWidgetFactory *DSettingsDialog::widgetFactory() const } void DSettingsDialog::updateSettings(Dtk::Core::DSettings *settings) +{ + updateSettings(QByteArray(), settings); +} + +void DSettingsDialog::updateSettings(const QByteArray &translateContext, Core::DSettings *settings) { Q_D(DSettingsDialog); - d->leftFrame->updateSettings(settings); - d->content->updateSettings(settings); + d->leftFrame->updateSettings(translateContext, settings); + d->content->updateSettings(translateContext, settings); adjustSize(); } diff --git a/src/widgets/dsettingsdialog.h b/src/widgets/dsettingsdialog.h index 4c0611ae2..496d9a5aa 100644 --- a/src/widgets/dsettingsdialog.h +++ b/src/widgets/dsettingsdialog.h @@ -39,6 +39,7 @@ class LIBDTKWIDGETSHARED_EXPORT DSettingsDialog : public DAbstractDialog public Q_SLOTS: void updateSettings(DTK_CORE_NAMESPACE::DSettings *settings); + void updateSettings(const QByteArray &translateContext, DTK_CORE_NAMESPACE::DSettings *settings); private: QScopedPointer d_ptr; diff --git a/src/widgets/dsettingswidgetfactory.cpp b/src/widgets/dsettingswidgetfactory.cpp index 2e8703407..583a7bf4c 100644 --- a/src/widgets/dsettingswidgetfactory.cpp +++ b/src/widgets/dsettingswidgetfactory.cpp @@ -29,6 +29,7 @@ #include #include #include +#include #include @@ -38,7 +39,22 @@ DWIDGET_BEGIN_NAMESPACE +#define PRIVATE_PROPERTY_translateContext "_d_DSettingsWidgetFactory_translateContext" + +static inline QString tr(const QByteArray &translateContext, const char *sourceText) +{ + if (translateContext.isEmpty()) + return QObject::tr(sourceText); + + return qApp->translate(translateContext, sourceText); +} + QWidget *DSettingsWidgetFactory::createTwoColumWidget(DTK_CORE_NAMESPACE::DSettingsOption *option, QWidget *rightWidget) +{ + return createTwoColumWidget(QByteArray(), option, rightWidget); +} + +QWidget *DSettingsWidgetFactory::createTwoColumWidget(const QByteArray &translateContext, DTK_CORE_NAMESPACE::DSettingsOption *option, QWidget *rightWidget) { auto optionFrame = new QFrame; optionFrame->setMinimumHeight(30); @@ -58,7 +74,7 @@ QWidget *DSettingsWidgetFactory::createTwoColumWidget(DTK_CORE_NAMESPACE::DSetti optionLayout->setColumnStretch(0, 10); optionLayout->setColumnStretch(1, 100); - auto trName = QObject::tr(option->name().toStdString().c_str()); + auto trName = DWIDGET_NAMESPACE::tr(translateContext, option->name().toStdString().c_str()); auto labelWidget = new QLabel(trName); labelWidget->setContentsMargins(5, 0, 0, 0); labelWidget->setAlignment(Qt::AlignLeft | Qt::AlignVCenter); @@ -107,7 +123,8 @@ QWidget *createShortcutEditOptionHandle(QObject *opt) auto optionValue = option->value(); updateWidgetValue(optionValue); - auto optionWidget = DSettingsWidgetFactory::createTwoColumWidget(option, rightWidget); + auto translateContext = opt->property(PRIVATE_PROPERTY_translateContext).toByteArray(); + auto optionWidget = DSettingsWidgetFactory::createTwoColumWidget(translateContext, option, rightWidget); // keep raw value type switch (optionValue.type()) { @@ -143,9 +160,10 @@ QWidget *createShortcutEditOptionHandle(QObject *opt) QWidget *createCheckboxOptionHandle(QObject *opt) { + auto translateContext = opt->property(PRIVATE_PROPERTY_translateContext).toByteArray(); auto option = qobject_cast(opt); auto value = option->data("text").toString(); - auto trName = QObject::tr(value.toStdString().c_str()); + auto trName = DWIDGET_NAMESPACE::tr(translateContext, value.toStdString().c_str()); auto checkboxFrame = new QWidget; auto checkboxLayout = new QHBoxLayout(checkboxFrame); @@ -163,7 +181,7 @@ QWidget *createCheckboxOptionHandle(QObject *opt) rightWidget->setObjectName("OptionCheckbox"); rightWidget->setChecked(option->value().toBool()); - auto optionWidget = DSettingsWidgetFactory::createTwoColumWidget(option, checkboxFrame); + auto optionWidget = DSettingsWidgetFactory::createTwoColumWidget(translateContext, option, checkboxFrame); option->connect(rightWidget, &QCheckBox::stateChanged, option, [ = ](int status) { @@ -180,15 +198,16 @@ QWidget *createCheckboxOptionHandle(QObject *opt) QWidget *createLineEditOptionHandle(QObject *opt) { + auto translateContext = opt->property(PRIVATE_PROPERTY_translateContext).toByteArray(); auto option = qobject_cast(opt); auto value = option->data("text").toString(); - auto trName = QObject::tr(value.toStdString().c_str()); + auto trName = DWIDGET_NAMESPACE::tr(translateContext, value.toStdString().c_str()); auto rightWidget = new QLineEdit(trName); rightWidget->setFixedHeight(24); rightWidget->setObjectName("OptionLineEdit"); rightWidget->setText(option->value().toString()); - auto optionWidget = DSettingsWidgetFactory::createTwoColumWidget(option, rightWidget); + auto optionWidget = DSettingsWidgetFactory::createTwoColumWidget(translateContext, option, rightWidget); option->connect(rightWidget, &QLineEdit::editingFinished, option, [ = ]() { @@ -205,16 +224,17 @@ QWidget *createLineEditOptionHandle(QObject *opt) QWidget *createComboBoxOptionHandle(QObject *opt) { + auto translateContext = opt->property(PRIVATE_PROPERTY_translateContext).toByteArray(); auto option = qobject_cast(opt); auto rightWidget = new ComboBox(); rightWidget->setFocusPolicy(Qt::StrongFocus); rightWidget->setFixedHeight(24); rightWidget->setObjectName("OptionLineEdit"); - auto optionWidget = DSettingsWidgetFactory::createTwoColumWidget(option, rightWidget); + auto optionWidget = DSettingsWidgetFactory::createTwoColumWidget(translateContext, option, rightWidget); auto initComboxList = [ = ](const QStringList & data) { for (auto item : data) { - auto trName = QObject::tr(item.toStdString().c_str()); + auto trName = DWIDGET_NAMESPACE::tr(translateContext, item.toStdString().c_str()); rightWidget->addItem(trName); } auto current = option->value().toInt(); @@ -235,7 +255,7 @@ QWidget *createComboBoxOptionHandle(QObject *opt) auto values = data.value("values").toStringList(); for (int i = 0; i < keys.length(); ++i) { - auto trName = QObject::tr(values.value(i).toStdString().c_str()); + auto trName = DWIDGET_NAMESPACE::tr(translateContext, values.value(i).toStdString().c_str()); rightWidget->addItem(trName, keys.value(i)); } @@ -292,7 +312,8 @@ QWidget *createButtonGroupOptionHandle(QObject *opt) rightWidget->setButtons(items); rightWidget->setCheckedButton(0); - auto optionWidget = DSettingsWidgetFactory::createTwoColumWidget(option, rightWidget); + auto translateContext = opt->property(PRIVATE_PROPERTY_translateContext).toByteArray(); + auto optionWidget = DSettingsWidgetFactory::createTwoColumWidget(translateContext, option, rightWidget); rightWidget->setParent(optionWidget); option->connect(rightWidget, &ButtonGroup::buttonChecked, @@ -309,6 +330,7 @@ QWidget *createButtonGroupOptionHandle(QObject *opt) QWidget *createRadioGroupOptionHandle(QObject *opt) { + auto translateContext = opt->property(PRIVATE_PROPERTY_translateContext).toByteArray(); auto option = qobject_cast(opt); auto items = option->data("items").toStringList(); @@ -323,7 +345,7 @@ QWidget *createRadioGroupOptionHandle(QObject *opt) auto index = 0; QList buttonList; for (auto item : items) { - auto rb = new QRadioButton(QObject::tr(item.toStdString().c_str())); + auto rb = new QRadioButton(DWIDGET_NAMESPACE::tr(translateContext, item.toStdString().c_str())); rb->setFixedHeight(24); rb->setProperty("_dtk_widget_settings_radiogroup_index", index); rgLayout->addWidget(rb); @@ -338,7 +360,7 @@ QWidget *createRadioGroupOptionHandle(QObject *opt) } rightWidget->setLayout(rgLayout); - auto optionWidget = DSettingsWidgetFactory::createTwoColumWidget(option, rightWidget); + auto optionWidget = DSettingsWidgetFactory::createTwoColumWidget(translateContext, option, rightWidget); rightWidget->setParent(optionWidget); option->connect(option, &DTK_CORE_NAMESPACE::DSettingsOption::valueChanged, @@ -373,7 +395,8 @@ QWidget *createSpinButtonOptionHandle(QObject *opt) rightWidget->setMinimum(option->data("min").toInt()); } - auto optionWidget = DSettingsWidgetFactory::createTwoColumWidget(option, rightWidget); + auto translateContext = opt->property(PRIVATE_PROPERTY_translateContext).toByteArray(); + auto optionWidget = DSettingsWidgetFactory::createTwoColumWidget(translateContext, option, rightWidget); option->connect(rightWidget, static_cast(&QSpinBox::valueChanged), option, [ = ](int value) { @@ -398,7 +421,8 @@ QWidget *createSliderOptionHandle(QObject *opt) rightWidget->setMinimum(option->data("min").toInt()); rightWidget->setValue(option->value().toInt()); - auto optionWidget = DSettingsWidgetFactory::createTwoColumWidget(option, rightWidget); + auto translateContext = opt->property(PRIVATE_PROPERTY_translateContext).toByteArray(); + auto optionWidget = DSettingsWidgetFactory::createTwoColumWidget(translateContext, option, rightWidget); option->connect(rightWidget, &QSlider::valueChanged, option, [ = ](int value) { @@ -422,7 +446,8 @@ QWidget *createUnsupportHandle(QObject *opt) rightWidget->setObjectName("OptionUnsupport"); rightWidget->setText("Unsupport option type: " + option->viewType()); - auto optionWidget = DSettingsWidgetFactory::createTwoColumWidget(option, rightWidget); + auto translateContext = opt->property(PRIVATE_PROPERTY_translateContext).toByteArray(); + auto optionWidget = DSettingsWidgetFactory::createTwoColumWidget(translateContext, option, rightWidget); // optionWidget->setStyleSheet("QFrame{border: 1px solid red;}"); return optionWidget; @@ -468,6 +493,13 @@ void DSettingsWidgetFactory::registerWidget(const QString &viewType, std::functi QWidget *DSettingsWidgetFactory::createWidget(QPointer option) { + return createWidget(QByteArray(), option); +} + +QWidget *DSettingsWidgetFactory::createWidget(const QByteArray &translateContext, QPointer option) +{ + option->setProperty(PRIVATE_PROPERTY_translateContext, translateContext); + Q_D(DSettingsWidgetFactory); auto handle = d->widgetCreateHandles.value(option->viewType()); if (handle) { diff --git a/src/widgets/dsettingswidgetfactory.h b/src/widgets/dsettingswidgetfactory.h index 390129a89..cbc4336fb 100644 --- a/src/widgets/dsettingswidgetfactory.h +++ b/src/widgets/dsettingswidgetfactory.h @@ -43,8 +43,10 @@ class LIBDTKWIDGETSHARED_EXPORT DSettingsWidgetFactory : public QObject void registerWidget(const QString &viewType, std::function handler); QWidget *createWidget(QPointer settings); + QWidget *createWidget(const QByteArray &translateContext, QPointer settings); static QWidget *createTwoColumWidget(DTK_CORE_NAMESPACE::DSettingsOption *option, QWidget *rightWidget); + static QWidget *createTwoColumWidget(const QByteArray &translateContext, DTK_CORE_NAMESPACE::DSettingsOption *option, QWidget *rightWidget); private: QScopedPointer d_ptr; diff --git a/src/widgets/private/settings/content.cpp b/src/widgets/private/settings/content.cpp index 0b6df1bd0..6f941d676 100644 --- a/src/widgets/private/settings/content.cpp +++ b/src/widgets/private/settings/content.cpp @@ -23,6 +23,7 @@ #include #include #include +#include #include #include @@ -134,7 +135,7 @@ void Content::onScrollToGroup(const QString &key) // qDebug() << "onScrollToGroup" << key; } -void Content::updateSettings(QPointer settings) +void Content::updateSettings(const QByteArray &translateContext, QPointer settings) { Q_D(Content); for (auto groupKey : settings->groupKeys()) { @@ -142,7 +143,8 @@ void Content::updateSettings(QPointer settings) if (group->isHidden()) { continue; } - auto trName = QObject::tr(group->name().toStdString().c_str()); + auto trName = translateContext.isEmpty() ? QObject::tr(group->name().toStdString().c_str()) + : qApp->translate(translateContext.constData(), group->name().toStdString().c_str()); auto title = new ContentTitle; title->setTitle(trName); title->setProperty("key", groupKey); @@ -161,7 +163,8 @@ void Content::updateSettings(QPointer settings) title->setAlignment(Qt::AlignLeft | Qt::AlignVCenter); title->setObjectName("ContentSubTitleText"); title->setFixedHeight(20); - auto trName = QObject::tr(subgroup->name().toStdString().c_str()); + auto trName = translateContext.isEmpty() ? QObject::tr(subgroup->name().toStdString().c_str()) + : qApp->translate(translateContext.constData(), subgroup->name().toStdString().c_str()); title->setText(trName); title->setProperty("key", subgroup->key()); title->setStyleSheet("#ContentSubTitleText{font-weight: 520; " @@ -178,7 +181,7 @@ void Content::updateSettings(QPointer settings) continue; } - auto widget = d->widgetFactory->createWidget(option); + auto widget = d->widgetFactory->createWidget(translateContext, option); if (widget) { d->contentLayout->addWidget(widget); widget->setParent(d->contentFrame); diff --git a/src/widgets/private/settings/content.h b/src/widgets/private/settings/content.h index a83935167..554ba7f22 100644 --- a/src/widgets/private/settings/content.h +++ b/src/widgets/private/settings/content.h @@ -44,7 +44,7 @@ class LIBDTKWIDGETSHARED_EXPORT Content : public QFrame public Q_SLOTS: void onScrollToGroup(const QString &key); - void updateSettings(QPointer settings); + void updateSettings(const QByteArray &translateContext, QPointer settings); private: QScopedPointer d_ptr; diff --git a/src/widgets/private/settings/navigation.cpp b/src/widgets/private/settings/navigation.cpp index 4acb42b5a..188469874 100644 --- a/src/widgets/private/settings/navigation.cpp +++ b/src/widgets/private/settings/navigation.cpp @@ -21,6 +21,7 @@ #include #include #include +#include #include #include @@ -101,7 +102,7 @@ void Navigation::onSelectGroup(const QString &key) } } -void Navigation::updateSettings(QPointer settings) +void Navigation::updateSettings(const QByteArray &translateContext, QPointer settings) { Q_D(Navigation); @@ -120,7 +121,8 @@ void Navigation::updateSettings(QPointer settings continue; } auto item = new QStandardItem; - auto trName = QObject::tr(group->name().toStdString().c_str()); + auto trName = translateContext.isEmpty() ? QObject::tr(group->name().toStdString().c_str()) + : qApp->translate(translateContext.constData(), group->name().toStdString().c_str()); // qDebug() << settings->group(groupKey)->name() << trName; // QFont font(item->data(Qt::FontRole).value()); // QFontMetrics fm(font); @@ -142,7 +144,8 @@ void Navigation::updateSettings(QPointer settings } auto item = new QStandardItem; - auto trName = QObject::tr(subgroup->name().toStdString().c_str()); + auto trName = translateContext.isEmpty() ? QObject::tr(subgroup->name().toStdString().c_str()) + : qApp->translate(translateContext.constData(), subgroup->name().toStdString().c_str()); // qDebug() << subgroup->name() << trName; // QFont font(item->data(Qt::FontRole).value()); // QFontMetrics fm(font); diff --git a/src/widgets/private/settings/navigation.h b/src/widgets/private/settings/navigation.h index f1f707d8a..c878e2ca2 100644 --- a/src/widgets/private/settings/navigation.h +++ b/src/widgets/private/settings/navigation.h @@ -41,7 +41,7 @@ class LIBDTKWIDGETSHARED_EXPORT Navigation : public QFrame public Q_SLOTS: void onSelectGroup(const QString &key); - void updateSettings(QPointer settings); + void updateSettings(const QByteArray &translateContext, QPointer settings); private: QScopedPointer d_ptr;