Skip to content

Commit

Permalink
feat: support set the translate context on DSettingsDialog::updateSet…
Browse files Browse the repository at this point in the history
…tings

Change-Id: I3c8ddecf05e9b69e0fcaf037eb1f84385bddba4e
  • Loading branch information
zccrs committed Jul 23, 2018
1 parent 5af9fa2 commit 0afad0c
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 28 deletions.
2 changes: 0 additions & 2 deletions debian/libdtkwidget2.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions src/widgets/dsettingsdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
1 change: 1 addition & 0 deletions src/widgets/dsettingsdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<DSettingsDialogPrivate> d_ptr;
Expand Down
62 changes: 47 additions & 15 deletions src/widgets/dsettingswidgetfactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <QGridLayout>
#include <QRadioButton>
#include <QGroupBox>
#include <QCoreApplication>

#include <DSettingsOption>

Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -143,9 +160,10 @@ QWidget *createShortcutEditOptionHandle(QObject *opt)

QWidget *createCheckboxOptionHandle(QObject *opt)
{
auto translateContext = opt->property(PRIVATE_PROPERTY_translateContext).toByteArray();
auto option = qobject_cast<DTK_CORE_NAMESPACE::DSettingsOption *>(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);
Expand All @@ -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) {
Expand All @@ -180,15 +198,16 @@ QWidget *createCheckboxOptionHandle(QObject *opt)

QWidget *createLineEditOptionHandle(QObject *opt)
{
auto translateContext = opt->property(PRIVATE_PROPERTY_translateContext).toByteArray();
auto option = qobject_cast<DTK_CORE_NAMESPACE::DSettingsOption *>(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, [ = ]() {
Expand All @@ -205,16 +224,17 @@ QWidget *createLineEditOptionHandle(QObject *opt)

QWidget *createComboBoxOptionHandle(QObject *opt)
{
auto translateContext = opt->property(PRIVATE_PROPERTY_translateContext).toByteArray();
auto option = qobject_cast<DTK_CORE_NAMESPACE::DSettingsOption *>(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();
Expand All @@ -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));
}

Expand Down Expand Up @@ -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,
Expand All @@ -309,6 +330,7 @@ QWidget *createButtonGroupOptionHandle(QObject *opt)

QWidget *createRadioGroupOptionHandle(QObject *opt)
{
auto translateContext = opt->property(PRIVATE_PROPERTY_translateContext).toByteArray();
auto option = qobject_cast<DTK_CORE_NAMESPACE::DSettingsOption *>(opt);
auto items = option->data("items").toStringList();

Expand All @@ -323,7 +345,7 @@ QWidget *createRadioGroupOptionHandle(QObject *opt)
auto index = 0;
QList<QRadioButton *> 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);
Expand All @@ -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,
Expand Down Expand Up @@ -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<void (QSpinBox::*)(int value)>(&QSpinBox::valueChanged),
option, [ = ](int value) {
Expand All @@ -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) {
Expand All @@ -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;
Expand Down Expand Up @@ -468,6 +493,13 @@ void DSettingsWidgetFactory::registerWidget(const QString &viewType, std::functi

QWidget *DSettingsWidgetFactory::createWidget(QPointer<DTK_CORE_NAMESPACE::DSettingsOption> option)
{
return createWidget(QByteArray(), option);
}

QWidget *DSettingsWidgetFactory::createWidget(const QByteArray &translateContext, QPointer<DTK_CORE_NAMESPACE::DSettingsOption> option)
{
option->setProperty(PRIVATE_PROPERTY_translateContext, translateContext);

Q_D(DSettingsWidgetFactory);
auto handle = d->widgetCreateHandles.value(option->viewType());
if (handle) {
Expand Down
2 changes: 2 additions & 0 deletions src/widgets/dsettingswidgetfactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ class LIBDTKWIDGETSHARED_EXPORT DSettingsWidgetFactory : public QObject
void registerWidget(const QString &viewType, std::function<WidgetCreateHandler> handler);

QWidget *createWidget(QPointer<DTK_CORE_NAMESPACE::DSettingsOption> settings);
QWidget *createWidget(const QByteArray &translateContext, QPointer<DTK_CORE_NAMESPACE::DSettingsOption> 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<DSettingsWidgetFactoryPrivate> d_ptr;
Expand Down
11 changes: 7 additions & 4 deletions src/widgets/private/settings/content.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <QScrollBar>
#include <QScrollArea>
#include <QVBoxLayout>
#include <QCoreApplication>

#include <DSettings>
#include <DSettingsGroup>
Expand Down Expand Up @@ -134,15 +135,16 @@ void Content::onScrollToGroup(const QString &key)
// qDebug() << "onScrollToGroup" << key;
}

void Content::updateSettings(QPointer<DTK_CORE_NAMESPACE::DSettings> settings)
void Content::updateSettings(const QByteArray &translateContext, QPointer<DTK_CORE_NAMESPACE::DSettings> settings)
{
Q_D(Content);
for (auto groupKey : settings->groupKeys()) {
auto group = settings->group(groupKey);
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);
Expand All @@ -161,7 +163,8 @@ void Content::updateSettings(QPointer<DTK_CORE_NAMESPACE::DSettings> 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; "
Expand All @@ -178,7 +181,7 @@ void Content::updateSettings(QPointer<DTK_CORE_NAMESPACE::DSettings> 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);
Expand Down
2 changes: 1 addition & 1 deletion src/widgets/private/settings/content.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class LIBDTKWIDGETSHARED_EXPORT Content : public QFrame

public Q_SLOTS:
void onScrollToGroup(const QString &key);
void updateSettings(QPointer<DTK_CORE_NAMESPACE::DSettings> settings);
void updateSettings(const QByteArray &translateContext, QPointer<DTK_CORE_NAMESPACE::DSettings> settings);

private:
QScopedPointer<ContentPrivate> d_ptr;
Expand Down
9 changes: 6 additions & 3 deletions src/widgets/private/settings/navigation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <QVBoxLayout>
#include <QListView>
#include <QStandardItemModel>
#include <QCoreApplication>

#include <DSettings>
#include <DSettingsGroup>
Expand Down Expand Up @@ -101,7 +102,7 @@ void Navigation::onSelectGroup(const QString &key)
}
}

void Navigation::updateSettings(QPointer<DTK_CORE_NAMESPACE::DSettings> settings)
void Navigation::updateSettings(const QByteArray &translateContext, QPointer<DTK_CORE_NAMESPACE::DSettings> settings)
{
Q_D(Navigation);

Expand All @@ -120,7 +121,8 @@ void Navigation::updateSettings(QPointer<DTK_CORE_NAMESPACE::DSettings> 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<QFont>());
// QFontMetrics fm(font);
Expand All @@ -142,7 +144,8 @@ void Navigation::updateSettings(QPointer<DTK_CORE_NAMESPACE::DSettings> 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<QFont>());
// QFontMetrics fm(font);
Expand Down
2 changes: 1 addition & 1 deletion src/widgets/private/settings/navigation.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class LIBDTKWIDGETSHARED_EXPORT Navigation : public QFrame

public Q_SLOTS:
void onSelectGroup(const QString &key);
void updateSettings(QPointer<DTK_CORE_NAMESPACE::DSettings> settings);
void updateSettings(const QByteArray &translateContext, QPointer<DTK_CORE_NAMESPACE::DSettings> settings);

private:
QScopedPointer<NavigationPrivate> d_ptr;
Expand Down

0 comments on commit 0afad0c

Please sign in to comment.