forked from qt-creator/qt-creator
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:
@@ -50,6 +50,19 @@ Controls.TextField {
|
|||||||
|
|
||||||
signal commitData
|
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 {
|
ExtendedFunctionButton {
|
||||||
x: 2
|
x: 2
|
||||||
anchors.verticalCenter: parent.verticalCenter
|
anchors.verticalCenter: parent.verticalCenter
|
||||||
@@ -96,7 +109,7 @@ Controls.TextField {
|
|||||||
return
|
return
|
||||||
|
|
||||||
if (backendValue.isTranslated) {
|
if (backendValue.isTranslated) {
|
||||||
backendValue.expression = translateFunction() + "(\"" + trCheckbox.escapeString(text) + "\")"
|
setTranslateExpression()
|
||||||
} else {
|
} else {
|
||||||
if (lineEdit.backendValue.value !== text)
|
if (lineEdit.backendValue.value !== text)
|
||||||
lineEdit.backendValue.value = text;
|
lineEdit.backendValue.value = text;
|
||||||
@@ -142,7 +155,7 @@ Controls.TextField {
|
|||||||
|
|
||||||
onClicked: {
|
onClicked: {
|
||||||
if (trCheckbox.checked) {
|
if (trCheckbox.checked) {
|
||||||
lineEdit.backendValue.expression = translateFunction() + "(\"" + escapeString(lineEdit.text) + "\")"
|
setTranslateExpression()
|
||||||
} else {
|
} else {
|
||||||
var textValue = lineEdit.text
|
var textValue = lineEdit.text
|
||||||
lineEdit.backendValue.value = textValue
|
lineEdit.backendValue.value = textValue
|
||||||
|
|||||||
@@ -111,9 +111,17 @@ QColor PropertyEditorContextObject::colorFromString(const QString &colorString)
|
|||||||
QString PropertyEditorContextObject::translateFunction()
|
QString PropertyEditorContextObject::translateFunction()
|
||||||
{
|
{
|
||||||
if (QmlDesignerPlugin::instance()->settings().value(
|
if (QmlDesignerPlugin::instance()->settings().value(
|
||||||
DesignerSettingsKey::USE_QSTR_FUNCTION).toBool())
|
DesignerSettingsKey::TYPE_OF_QSTR_FUNCTION).toInt())
|
||||||
return QStringLiteral("qsTr");
|
|
||||||
return QStringLiteral("qsTrId");
|
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)
|
QStringList PropertyEditorContextObject::autoComplete(const QString &text, int pos, bool explicitComplete, bool filter)
|
||||||
|
|||||||
@@ -243,7 +243,7 @@ bool PropertyEditorValue::isTranslated() const
|
|||||||
if (modelNode().metaInfo().propertyTypeName(name()) == "QString" || modelNode().metaInfo().propertyTypeName(name()) == "string") {
|
if (modelNode().metaInfo().propertyTypeName(name()) == "QString" || modelNode().metaInfo().propertyTypeName(name()) == "string") {
|
||||||
const QmlDesigner::QmlObjectNode objectNode(modelNode());
|
const QmlDesigner::QmlObjectNode objectNode(modelNode());
|
||||||
if (objectNode.isValid() && objectNode.hasBindingProperty(name())) {
|
if (objectNode.isValid() && objectNode.hasBindingProperty(name())) {
|
||||||
QRegExp rx("qsTr(|Id)\\(\".*\"\\)");
|
QRegExp rx("qsTr(|Id|anslate)\\(\".*\"\\)");
|
||||||
//qsTr()
|
//qsTr()
|
||||||
if (objectNode.propertyAffectedByCurrentState(name())) {
|
if (objectNode.propertyAffectedByCurrentState(name())) {
|
||||||
return rx.exactMatch(expression());
|
return rx.exactMatch(expression());
|
||||||
@@ -328,6 +328,21 @@ void PropertyEditorValue::removeAliasExport()
|
|||||||
emit removeAliasExportRequested(nameAsQString());
|
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()
|
void PropertyEditorValue::registerDeclarativeTypes()
|
||||||
{
|
{
|
||||||
qmlRegisterType<PropertyEditorValue>("HelperWidgets",2,0,"PropertyEditorValue");
|
qmlRegisterType<PropertyEditorValue>("HelperWidgets",2,0,"PropertyEditorValue");
|
||||||
|
|||||||
@@ -128,6 +128,8 @@ public:
|
|||||||
Q_INVOKABLE bool isAttachedProperty() const;
|
Q_INVOKABLE bool isAttachedProperty() const;
|
||||||
Q_INVOKABLE void removeAliasExport();
|
Q_INVOKABLE void removeAliasExport();
|
||||||
|
|
||||||
|
Q_INVOKABLE QString getTranslationContext() const;
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void resetValue();
|
void resetValue();
|
||||||
void setEnumeration(const QString &scope, const QString &name);
|
void setEnumeration(const QString &scope, const QString &name);
|
||||||
|
|||||||
@@ -466,11 +466,20 @@ QVariant QmlObjectNode::instanceValue(const ModelNode &modelNode, const Property
|
|||||||
QString QmlObjectNode::generateTranslatableText(const QString &text)
|
QString QmlObjectNode::generateTranslatableText(const QString &text)
|
||||||
{
|
{
|
||||||
#ifndef QMLDESIGNER_TEST
|
#ifndef QMLDESIGNER_TEST
|
||||||
|
|
||||||
if (QmlDesignerPlugin::instance()->settings().value(
|
if (QmlDesignerPlugin::instance()->settings().value(
|
||||||
DesignerSettingsKey::USE_QSTR_FUNCTION).toBool())
|
DesignerSettingsKey::TYPE_OF_QSTR_FUNCTION).toInt())
|
||||||
return QString(QStringLiteral("qsTr(\"%1\")")).arg(text);
|
|
||||||
else
|
switch (QmlDesignerPlugin::instance()->settings().value(
|
||||||
return QString(QStringLiteral("qsTrId(\"%1\")")).arg(text);
|
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
|
#else
|
||||||
Q_UNUSED(text);
|
Q_UNUSED(text);
|
||||||
return QString();
|
return QString();
|
||||||
|
|||||||
@@ -61,7 +61,7 @@ void DesignerSettings::fromSettings(QSettings *settings)
|
|||||||
restoreValue(settings, DesignerSettingsKey::ENABLE_DEBUGVIEW, false);
|
restoreValue(settings, DesignerSettingsKey::ENABLE_DEBUGVIEW, false);
|
||||||
restoreValue(settings, DesignerSettingsKey::ALWAYS_SAFE_IN_CRUMBLEBAR, false);
|
restoreValue(settings, DesignerSettingsKey::ALWAYS_SAFE_IN_CRUMBLEBAR, false);
|
||||||
restoreValue(settings, DesignerSettingsKey::USE_ONLY_FALLBACK_PUPPET, true);
|
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_FALLBACK_DIRECTORY);
|
||||||
restoreValue(settings, DesignerSettingsKey::PUPPET_TOPLEVEL_BUILD_DIRECTORY);
|
restoreValue(settings, DesignerSettingsKey::PUPPET_TOPLEVEL_BUILD_DIRECTORY);
|
||||||
restoreValue(settings, DesignerSettingsKey::CONTROLS_STYLE);
|
restoreValue(settings, DesignerSettingsKey::CONTROLS_STYLE);
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ const char USE_ONLY_FALLBACK_PUPPET[] = "UseOnlyFallbackPuppet";
|
|||||||
const char PUPPET_TOPLEVEL_BUILD_DIRECTORY[] = "PuppetToplevelBuildDirectory";
|
const char PUPPET_TOPLEVEL_BUILD_DIRECTORY[] = "PuppetToplevelBuildDirectory";
|
||||||
const char PUPPET_FALLBACK_DIRECTORY[] = "PuppetFallbackDirectory";
|
const char PUPPET_FALLBACK_DIRECTORY[] = "PuppetFallbackDirectory";
|
||||||
const char CONTROLS_STYLE[] = "ControlsStyle";
|
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 SHOW_PROPERTYEDITOR_WARNINGS[] = "ShowPropertyEditorWarnings";
|
||||||
const char ENABLE_MODEL_EXCEPTION_OUTPUT[] = "WarnException";
|
const char ENABLE_MODEL_EXCEPTION_OUTPUT[] = "WarnException";
|
||||||
const char PUPPET_KILL_TIMEOUT[] = "PuppetKillTimeout";
|
const char PUPPET_KILL_TIMEOUT[] = "PuppetKillTimeout";
|
||||||
|
|||||||
@@ -106,8 +106,19 @@ DesignerSettings SettingsPageWidget::settings() const
|
|||||||
m_ui.designerEnableDebuggerCheckBox->isChecked());
|
m_ui.designerEnableDebuggerCheckBox->isChecked());
|
||||||
settings.insert(DesignerSettingsKey::USE_ONLY_FALLBACK_PUPPET,
|
settings.insert(DesignerSettingsKey::USE_ONLY_FALLBACK_PUPPET,
|
||||||
m_ui.useDefaultPuppetRadioButton->isChecked());
|
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::CONTROLS_STYLE, m_ui.styleLineEdit->text());
|
||||||
settings.insert(DesignerSettingsKey::FORWARD_PUPPET_OUTPUT,
|
settings.insert(DesignerSettingsKey::FORWARD_PUPPET_OUTPUT,
|
||||||
m_ui.forwardPuppetOutputComboBox->currentText());
|
m_ui.forwardPuppetOutputComboBox->currentText());
|
||||||
@@ -160,9 +171,11 @@ void SettingsPageWidget::setSettings(const DesignerSettings &settings)
|
|||||||
m_ui.useQtRelatedPuppetRadioButton->setChecked(!settings.value(
|
m_ui.useQtRelatedPuppetRadioButton->setChecked(!settings.value(
|
||||||
DesignerSettingsKey::USE_ONLY_FALLBACK_PUPPET).toBool());
|
DesignerSettingsKey::USE_ONLY_FALLBACK_PUPPET).toBool());
|
||||||
m_ui.useQsTrFunctionRadioButton->setChecked(settings.value(
|
m_ui.useQsTrFunctionRadioButton->setChecked(settings.value(
|
||||||
DesignerSettingsKey::USE_QSTR_FUNCTION).toBool());
|
DesignerSettingsKey::TYPE_OF_QSTR_FUNCTION).toInt() == 0);
|
||||||
m_ui.useQsTrIdFunctionRadioButton->setChecked(!settings.value(
|
m_ui.useQsTrIdFunctionRadioButton->setChecked(settings.value(
|
||||||
DesignerSettingsKey::USE_QSTR_FUNCTION).toBool());
|
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(
|
m_ui.styleLineEdit->setText(settings.value(
|
||||||
DesignerSettingsKey::CONTROLS_STYLE).toString());
|
DesignerSettingsKey::CONTROLS_STYLE).toString());
|
||||||
|
|
||||||
|
|||||||
@@ -349,6 +349,13 @@
|
|||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QRadioButton" name="useQsTranslateFunctionRadioButton">
|
||||||
|
<property name="text">
|
||||||
|
<string>qsTranslate()</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
</layout>
|
</layout>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
|
|||||||
Reference in New Issue
Block a user