Browse Source

added settings ts generator

pull/2/head
Skycoder42 7 years ago
parent
commit
00037ee16b
  1. 38
      bin/qsettingstranslator.py
  2. 8
      examples/mvvmcore/SampleCore/SampleCore.pro
  3. 36
      examples/mvvmcore/SampleCore/settings.xml
  4. 23
      mkspecs/features/qsettingstranslator.prf
  5. 11
      src/mvvmcore/mvvmcore.pro

38
bin/qsettingstranslator.py

@ -0,0 +1,38 @@
#!/usr/bin/env python3
# Usage: qsettingstranslator.py <in> <out>
import sys
import os
from xml.etree.ElementTree import Element, parse
if len(sys.argv) != 3:
print("Usage: qsettingstranslator.py <in> <out>", file=sys.stderr)
exit(1)
infile = sys.argv[1]
outfile = sys.argv[2]
strings = set()
tree = parse(infile)
root = Element("TS")
for elem in tree.iter():
if elem.tag == "SearchKey":
strings.add(elem.text)
else:
if "title" in elem.attrib:
strings.add(elem.attrib["title"])
if "tooltip" in elem.attrib:
strings.add(elem.attrib["tooltip"])
if "ts" in elem.attrib and elem.attrib["ts"].lower() == "true":
strings.add(elem.text)
if "tsdefault" in elem.attrib and elem.attrib["tsdefault"].lower() == "true":
strings.add(elem.attrib["default"])
with open(outfile, "w") as file:
file.write("#include <QCoreApplication>\n\n")
file.write("void dummyfn() {\n")
for string in strings:
string = string.replace("\\", "\\\\")
string = string.replace("\"", "\\\"")
file.write("\tQCoreApplication::translate(\"{}\", \"{}\");\n".format(os.path.basename(infile), string))
file.write("}\n")

8
examples/mvvmcore/SampleCore/SampleCore.pro

@ -22,8 +22,12 @@ SOURCES += \
drawerviewmodel.cpp \ drawerviewmodel.cpp \
tabviewmodel.cpp tabviewmodel.cpp
RESOURCES += \
sample_core.qrc
QTMVVM_TS_SETTINGS += settings.xml
target.path = $$[QT_INSTALL_EXAMPLES]/mvvmcore/$$TARGET target.path = $$[QT_INSTALL_EXAMPLES]/mvvmcore/$$TARGET
INSTALLS += target INSTALLS += target
RESOURCES += \ samples_in_build: QMAKE_QSETTINGSTRANSLATOR = $$PWD/../../../bin/qsettingstranslator.py
sample_core.qrc

36
examples/mvvmcore/SampleCore/settings.xml

@ -15,7 +15,7 @@
<Entry key="prop2" <Entry key="prop2"
type="string" type="string"
title="Enter a &amp;name"> title="Enter a &amp;name">
<Property key="placeholderText" type="string">Enter a nice name</Property> <Property key="placeholderText" type="string" ts="true">Enter a nice name</Property>
</Entry> </Entry>
</Group> </Group>
<Group title="Sub-Group" tooltip="This is a tooltip"> <Group title="Sub-Group" tooltip="This is a tooltip">
@ -24,17 +24,18 @@
title="Open &amp;system settings"> title="Open &amp;system settings">
<Property key="text" type="string">Trigger Action</Property> <Property key="text" type="string">Trigger Action</Property>
<Property key="args" type="object"> <Property key="args" type="object">
<Property key="hint" type="string">You can use this to trigger whatever kind of action you need</Property> <Property key="hint" type="string" ts="true">You can use this to trigger whatever kind of action you need</Property>
</Property> </Property>
</Entry> </Entry>
<Entry key="prop4" <Entry key="prop4"
type="selection" type="selection"
title="Select a &amp;mode" title="Select a &amp;mode"
default="Variant B"> default="Variant B"
tsdefault="true">
<Property key="listElements" type="list"> <Property key="listElements" type="list">
<Element type="string">Variant A</Element> <Element type="string" ts="true">Variant A</Element>
<Element type="string">Variant B</Element> <Element type="string" ts="true">Variant B</Element>
<Element type="string">Variant C</Element> <Element type="string" ts="true">Variant C</Element>
</Property> </Property>
</Entry> </Entry>
</Group> </Group>
@ -53,31 +54,31 @@
default="3"> default="3">
<Property key="listElements" type="list"> <Property key="listElements" type="list">
<Element type="object"> <Element type="object">
<Property key="name" type="string">Value A</Property> <Property key="name" type="string" ts="true">Value A</Property>
<Property key="value" type="int">1</Property> <Property key="value" type="int">1</Property>
</Element> </Element>
<Element type="object"> <Element type="object">
<Property key="name" type="string">Value B</Property> <Property key="name" type="string" ts="true">Value B</Property>
<Property key="value" type="int">2</Property> <Property key="value" type="int">2</Property>
</Element> </Element>
<Element type="object"> <Element type="object">
<Property key="name" type="string">Value C</Property> <Property key="name" type="string" ts="true">Value C</Property>
<Property key="value" type="int">4</Property> <Property key="value" type="int">4</Property>
</Element> </Element>
<Element type="object"> <Element type="object">
<Property key="name" type="string">Value A+B</Property> <Property key="name" type="string" ts="true">Value A+B</Property>
<Property key="value" type="int">3</Property> <Property key="value" type="int">3</Property>
</Element> </Element>
<Element type="object"> <Element type="object">
<Property key="name" type="string">Value A+C</Property> <Property key="name" type="string" ts="true">Value A+C</Property>
<Property key="value" type="int">5</Property> <Property key="value" type="int">5</Property>
</Element> </Element>
<Element type="object"> <Element type="object">
<Property key="name" type="string">Value B+C</Property> <Property key="name" type="string" ts="true">Value B+C</Property>
<Property key="value" type="int">6</Property> <Property key="value" type="int">6</Property>
</Element> </Element>
<Element type="object"> <Element type="object">
<Property key="name" type="string">Value A+B+C</Property> <Property key="name" type="string" ts="true">Value A+B+C</Property>
<Property key="value" type="int">7</Property> <Property key="value" type="int">7</Property>
</Element> </Element>
</Property> </Property>
@ -92,17 +93,18 @@
<Entry key="prop8" <Entry key="prop8"
type="selection" type="selection"
title="Select a &amp;mode" title="Select a &amp;mode"
default="Text 2"> default="Text 2"
tsdefault="true">
<Property key="editable" type="bool">true</Property> <Property key="editable" type="bool">true</Property>
<Property key="listElements" type="list"> <Property key="listElements" type="list">
<Element type="string">Text 1</Element> <Element type="string" ts="true">Text 1</Element>
<Element type="string">Text 2</Element> <Element type="string" ts="true">Text 2</Element>
</Property> </Property>
</Entry> </Entry>
<Entry key="prop9" <Entry key="prop9"
type="url" type="url"
title="Enter a &amp;website"> title="Enter a &amp;website">
<Property key="placeholderText" type="string">https://example.org/test</Property> <Property key="placeholderText" type="string" ts="true">https://example.org/test</Property>
</Entry> </Entry>
<Entry key="prop10" <Entry key="prop10"
type="QFont" type="QFont"

23
mkspecs/features/qsettingstranslator.prf

@ -0,0 +1,23 @@
isEmpty(QMAKE_QSETTINGSTRANSLATOR): qtPrepareTool(QMAKE_QSETTINGSTRANSLATOR, qsettingstranslator.py)
isEmpty(QSETTINGSTRANSLATOR_DIR): QSETTINGSTRANSLATOR_DIR = ./.ts-dummy
!ReleaseBuild:!DebugBuild: {
qsettingstranslator_c.name = QSETTINGSTRANSLATOR of ${QMAKE_FILE_IN}
qsettingstranslator_c.input = QTMVVM_TS_SETTINGS
qsettingstranslator_c.variable_out = CPP_DUMMIES
qsettingstranslator_c.commands = $$QMAKE_QSETTINGSTRANSLATOR ${QMAKE_FILE_IN} ${QMAKE_FILE_OUT}
qsettingstranslator_c.output = $$QSETTINGSTRANSLATOR_DIR/${QMAKE_FILE_BASE}.tsdummy$${first(QMAKE_EXT_CPP)}
qsettingstranslator_c.CONFIG += no_link
qsettingstranslator_c.depends += $$QMAKE_QSETTINGSTRANSLATOR
QMAKE_EXTRA_COMPILERS += qsettingstranslator_c
qtmvvm_tsgen_target.target = qtmvvm-tsgen
qtmvvm_tsgen_target.depends += compiler_qsettingstranslator_c_make_all
QMAKE_EXTRA_TARGETS += qtmvvm_tsgen_target
}
QMAKE_DIR_REPLACE += QSETTINGSTRANSLATOR_DIR
QMAKE_DIR_REPLACE_SANE += QSETTINGSTRANSLATOR_DIR
_never_true_condition: SOURCES += $$files($$TRANSLATIONS_DIR/*.tsdummy$${first(QMAKE_EXT_CPP)})

11
src/mvvmcore/mvvmcore.pro

@ -1,6 +1,7 @@
TARGET = QtMvvmCore TARGET = QtMvvmCore
QT = core gui QT = core gui
MODULE_CONFIG += qsettingstranslator
HEADERS += \ HEADERS += \
viewmodel.h \ viewmodel.h \
@ -45,6 +46,16 @@ qpmx_ts_target.depends += lrelease
load(qt_module) load(qt_module)
FEATURES += ../../mkspecs/features/qsettingstranslator.prf
features.files = $$FEATURES
features.path = $$[QT_HOST_DATA]/mkspecs/features/
SCRIPTS += ../../bin/qsettingstranslator.py
scripts.files = $$SCRIPTS
scripts.path = $$[QT_INSTALL_BINS]
INSTALLS += features scripts
win32 { win32 {
QMAKE_TARGET_PRODUCT = "$$TARGET" QMAKE_TARGET_PRODUCT = "$$TARGET"
QMAKE_TARGET_COMPANY = "Skycoder42" QMAKE_TARGET_COMPANY = "Skycoder42"

Loading…
Cancel
Save