QmlDesigner: Implement preliminary support for qsTranslate

There are now 3 options for QML translations in the options.

* qsTr (default)
* qsTrId
* qsTranslate

In case of qsTranslate the context is preserved when editing the text
and the context can be changed by setting a binding on the text property.

Task-number: QTCREATORBUG-17714
Change-Id: Ied2ccb84d98d99d5b920fe47ee6d8cb67ff2bd5b
Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
This commit is contained in:
Thomas Hartmann
2017-02-10 15:49:35 +01:00
parent 6052e28b84
commit dfe3898041
9 changed files with 84 additions and 17 deletions

View File

@@ -50,6 +50,19 @@ Controls.TextField {
signal commitData
property string context
function setTranslateExpression()
{
if (translateFunction() === "qsTranslate") {
backendValue.expression = translateFunction()
+ "(\"" + backendValue.getTranslationContext()
+ "\", " + "\"" + trCheckbox.escapeString(text) + "\")"
} else {
backendValue.expression = translateFunction() + "(\"" + trCheckbox.escapeString(text) + "\")"
}
}
ExtendedFunctionButton {
x: 2
anchors.verticalCenter: parent.verticalCenter
@@ -96,7 +109,7 @@ Controls.TextField {
return
if (backendValue.isTranslated) {
backendValue.expression = translateFunction() + "(\"" + trCheckbox.escapeString(text) + "\")"
setTranslateExpression()
} else {
if (lineEdit.backendValue.value !== text)
lineEdit.backendValue.value = text;
@@ -142,7 +155,7 @@ Controls.TextField {
onClicked: {
if (trCheckbox.checked) {
lineEdit.backendValue.expression = translateFunction() + "(\"" + escapeString(lineEdit.text) + "\")"
setTranslateExpression()
} else {
var textValue = lineEdit.text
lineEdit.backendValue.value = textValue

View File

@@ -111,9 +111,17 @@ QColor PropertyEditorContextObject::colorFromString(const QString &colorString)
QString PropertyEditorContextObject::translateFunction()
{
if (QmlDesignerPlugin::instance()->settings().value(
DesignerSettingsKey::USE_QSTR_FUNCTION).toBool())
return QStringLiteral("qsTr");
return QStringLiteral("qsTrId");
DesignerSettingsKey::TYPE_OF_QSTR_FUNCTION).toInt())
switch (QmlDesignerPlugin::instance()->settings().value(
DesignerSettingsKey::TYPE_OF_QSTR_FUNCTION).toInt()) {
case 0: return "qsTr";
case 1: return "qsTrId";
case 2: return "qsTranslate";
default:
break;
}
return "qsTr";
}
QStringList PropertyEditorContextObject::autoComplete(const QString &text, int pos, bool explicitComplete, bool filter)

View File

@@ -243,7 +243,7 @@ bool PropertyEditorValue::isTranslated() const
if (modelNode().metaInfo().propertyTypeName(name()) == "QString" || modelNode().metaInfo().propertyTypeName(name()) == "string") {
const QmlDesigner::QmlObjectNode objectNode(modelNode());
if (objectNode.isValid() && objectNode.hasBindingProperty(name())) {
QRegExp rx("qsTr(|Id)\\(\".*\"\\)");
QRegExp rx("qsTr(|Id|anslate)\\(\".*\"\\)");
//qsTr()
if (objectNode.propertyAffectedByCurrentState(name())) {
return rx.exactMatch(expression());
@@ -328,6 +328,21 @@ void PropertyEditorValue::removeAliasExport()
emit removeAliasExportRequested(nameAsQString());
}
QString PropertyEditorValue::getTranslationContext() const
{
if (modelNode().isValid() && modelNode().metaInfo().isValid() && modelNode().metaInfo().hasProperty(name())) {
if (modelNode().metaInfo().propertyTypeName(name()) == "QString" || modelNode().metaInfo().propertyTypeName(name()) == "string") {
const QmlDesigner::QmlObjectNode objectNode(modelNode());
if (objectNode.isValid() && objectNode.hasBindingProperty(name())) {
QRegExp rx("qsTranslate\\(\"(.*)\"\\s*,\\s*\".*\"\\s*\\)");
if (rx.exactMatch(expression()))
return rx.cap(1);
}
}
}
return "";
}
void PropertyEditorValue::registerDeclarativeTypes()
{
qmlRegisterType<PropertyEditorValue>("HelperWidgets",2,0,"PropertyEditorValue");

View File

@@ -128,6 +128,8 @@ public:
Q_INVOKABLE bool isAttachedProperty() const;
Q_INVOKABLE void removeAliasExport();
Q_INVOKABLE QString getTranslationContext() const;
public slots:
void resetValue();
void setEnumeration(const QString &scope, const QString &name);

View File

@@ -466,11 +466,20 @@ QVariant QmlObjectNode::instanceValue(const ModelNode &modelNode, const Property
QString QmlObjectNode::generateTranslatableText(const QString &text)
{
#ifndef QMLDESIGNER_TEST
if (QmlDesignerPlugin::instance()->settings().value(
DesignerSettingsKey::USE_QSTR_FUNCTION).toBool())
return QString(QStringLiteral("qsTr(\"%1\")")).arg(text);
else
return QString(QStringLiteral("qsTrId(\"%1\")")).arg(text);
DesignerSettingsKey::TYPE_OF_QSTR_FUNCTION).toInt())
switch (QmlDesignerPlugin::instance()->settings().value(
DesignerSettingsKey::TYPE_OF_QSTR_FUNCTION).toInt()) {
case 0: return QString(QStringLiteral("qsTr(\"%1\")")).arg(text);
case 1: return QString(QStringLiteral("qsTrId(\"%1\")")).arg(text);
case 2: return QString(QStringLiteral("qsTranslate(\"\"\"%1\")")).arg(text);
default:
break;
}
return QString(QStringLiteral("qsTr(\"%1\")")).arg(text);
#else
Q_UNUSED(text);
return QString();

View File

@@ -61,7 +61,7 @@ void DesignerSettings::fromSettings(QSettings *settings)
restoreValue(settings, DesignerSettingsKey::ENABLE_DEBUGVIEW, false);
restoreValue(settings, DesignerSettingsKey::ALWAYS_SAFE_IN_CRUMBLEBAR, false);
restoreValue(settings, DesignerSettingsKey::USE_ONLY_FALLBACK_PUPPET, true);
restoreValue(settings, DesignerSettingsKey::USE_QSTR_FUNCTION, true);
restoreValue(settings, DesignerSettingsKey::TYPE_OF_QSTR_FUNCTION, 0);
restoreValue(settings, DesignerSettingsKey::PUPPET_FALLBACK_DIRECTORY);
restoreValue(settings, DesignerSettingsKey::PUPPET_TOPLEVEL_BUILD_DIRECTORY);
restoreValue(settings, DesignerSettingsKey::CONTROLS_STYLE);

View File

@@ -51,7 +51,7 @@ const char USE_ONLY_FALLBACK_PUPPET[] = "UseOnlyFallbackPuppet";
const char PUPPET_TOPLEVEL_BUILD_DIRECTORY[] = "PuppetToplevelBuildDirectory";
const char PUPPET_FALLBACK_DIRECTORY[] = "PuppetFallbackDirectory";
const char CONTROLS_STYLE[] = "ControlsStyle";
const char USE_QSTR_FUNCTION[] = "UseQsTrFunction";
const char TYPE_OF_QSTR_FUNCTION[] = "TypeOfQsTrFunction";
const char SHOW_PROPERTYEDITOR_WARNINGS[] = "ShowPropertyEditorWarnings";
const char ENABLE_MODEL_EXCEPTION_OUTPUT[] = "WarnException";
const char PUPPET_KILL_TIMEOUT[] = "PuppetKillTimeout";

View File

@@ -106,8 +106,19 @@ DesignerSettings SettingsPageWidget::settings() const
m_ui.designerEnableDebuggerCheckBox->isChecked());
settings.insert(DesignerSettingsKey::USE_ONLY_FALLBACK_PUPPET,
m_ui.useDefaultPuppetRadioButton->isChecked());
settings.insert(DesignerSettingsKey::USE_QSTR_FUNCTION,
m_ui.useQsTrFunctionRadioButton->isChecked());
int typeOfQsTrFunction;
if (m_ui.useQsTrFunctionRadioButton->isChecked())
typeOfQsTrFunction = 0;
else if (m_ui.useQsTrIdFunctionRadioButton->isChecked())
typeOfQsTrFunction = 1;
else if (m_ui.useQsTranslateFunctionRadioButton->isChecked())
typeOfQsTrFunction = 2;
else
typeOfQsTrFunction = 0;
settings.insert(DesignerSettingsKey::TYPE_OF_QSTR_FUNCTION, typeOfQsTrFunction);
settings.insert(DesignerSettingsKey::CONTROLS_STYLE, m_ui.styleLineEdit->text());
settings.insert(DesignerSettingsKey::FORWARD_PUPPET_OUTPUT,
m_ui.forwardPuppetOutputComboBox->currentText());
@@ -160,9 +171,11 @@ void SettingsPageWidget::setSettings(const DesignerSettings &settings)
m_ui.useQtRelatedPuppetRadioButton->setChecked(!settings.value(
DesignerSettingsKey::USE_ONLY_FALLBACK_PUPPET).toBool());
m_ui.useQsTrFunctionRadioButton->setChecked(settings.value(
DesignerSettingsKey::USE_QSTR_FUNCTION).toBool());
m_ui.useQsTrIdFunctionRadioButton->setChecked(!settings.value(
DesignerSettingsKey::USE_QSTR_FUNCTION).toBool());
DesignerSettingsKey::TYPE_OF_QSTR_FUNCTION).toInt() == 0);
m_ui.useQsTrIdFunctionRadioButton->setChecked(settings.value(
DesignerSettingsKey::TYPE_OF_QSTR_FUNCTION).toInt() == 1);
m_ui.useQsTranslateFunctionRadioButton->setChecked(settings.value(
DesignerSettingsKey::TYPE_OF_QSTR_FUNCTION).toInt() == 2);
m_ui.styleLineEdit->setText(settings.value(
DesignerSettingsKey::CONTROLS_STYLE).toString());

View File

@@ -349,6 +349,13 @@
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="useQsTranslateFunctionRadioButton">
<property name="text">
<string>qsTranslate()</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>