Skip to content

Commit

Permalink
support qt5 for c++, installation of .qm files #31
Browse files Browse the repository at this point in the history
  • Loading branch information
pustotnik committed Jan 27, 2022
1 parent 4cee082 commit 1f66f1a
Show file tree
Hide file tree
Showing 5 changed files with 234 additions and 51 deletions.
4 changes: 2 additions & 2 deletions demos/qt5/02-someapp/buildconf.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ tasks:

qutil:
features : cxxshlib qt5
source : qutil/**/*.cpp
source : qutil/**/*.cpp qutil/**/*.ts
use : Qt5Core
langdir-defname: TRANSLATIONS_DIR

myqt5app:
features : cxxprogram qt5
source : prog/**/*.cpp prog/**/*.qrc prog/**/*.ui prog/**/*.ts
moc : prog/**/*.h # suppose all .h files in the 'prog' have Q_OBJECT
rclangprefix: lang
#qmpathprefix : prog/i18n
#bld-langprefix : prog/i18n
#unique-qmpaths: true
use : Qt5Widgets qutil util
configure:
Expand Down
3 changes: 2 additions & 1 deletion demos/qt5/02-someapp/qutil/qutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ QString getTranslated()
{
QTranslator translator;
translator.load(QLocale(), QLatin1String("qutil_lang"), QLatin1String("_"),
QLatin1String("qutil/i18n"));
// TRANSLATIONS_DIR is defined via buildconf.yml
QString::fromLocal8Bit(TRANSLATIONS_DIR));

return translator.translate("qutil", "translated message");
}
Expand Down
96 changes: 71 additions & 25 deletions src/zenmake/zm/features/qt5.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,53 @@ def process_mocs(tgen):

qt5.process_mocs(tgen)

def _createTranslationTasks(tgen):
def _checkQmPathUnique(tgen, qmpath, qmInfo):

otherQm = qmInfo.get(qmpath)
if otherQm is not None:
msg = "Tasks '%s' and '%s'" % (tgen.name, otherQm['tgen-name'])
msg += " have the same output .qm path:\n %s" % qmpath
msg += "\nUse the 'bld-langprefix'/'unique-qmpaths' task "
msg += "parameters to fix it. Or don't use the same .ts files"
msg += " in these tasks."
raise error.ZenMakeError(msg)

def _createRcTranslTasks(tgen, qmTasks, rclangprefix):

rcnode = 'rclang-%s' % tgen.name
rcnode = tgen.path.find_or_declare('%s.%d.qrc' % (rcnode, tgen.idx))

qmNodes = [x.outputs[0] for x in qmTasks]
kwargs = { 'qrcprefix' : rclangprefix }
qm2rccTask = tgen.create_task('qm2qrc', qmNodes, rcnode, **kwargs)
rccTask = qt5.create_rcc_task(tgen, qm2rccTask.outputs[0])
tgen.link_task.inputs.append(rccTask.outputs[0])

def _createQmInstallTasks(tgen, qmTasks, taskParams):

bld = tgen.bld
if not bld.is_install or not taskParams:
return None

qmNodes = [x.outputs[0] for x in qmTasks]

destdir = taskParams.get('install-langdir')
if destdir is None:
destdir = '$(appdatadir)/translations'
destdir = utils.substBuiltInVars(destdir, tgen.env['$builtin-vars'])
destdir = os.path.normpath(getNativePath(destdir))

taskParams = taskParams.copy()
taskParams.pop('install-files', None)
taskParams['install-files'] = [{
'src' : [x.abspath() for x in qmNodes],
'dst' : destdir,
'do' : 'copy'
}]
bld.setUpInstallFiles(taskParams)
return destdir

def _createTranslTasks(tgen):

tsFiles = getattr(tgen, 'lang', None)
if not tsFiles:
Expand All @@ -256,11 +302,20 @@ def _createTranslationTasks(tgen):
btypeNode = bld.root.make_node(bconfManager.root.selectedBuildTypeDir)

fnprefix = ''
pathprefix = None
qmpathprefix = None
langDirDefine = None
if zmTaskParams:
if zmTaskParams.get('unique-qmpaths', False) and rclangprefix is None:
fnprefix = zmTaskParams['name'] + '_'
pathprefix = getNativePath(zmTaskParams.get('qmpathprefix'))
qmpathprefix = getNativePath(zmTaskParams.get('bld-langprefix'))
langDirDefine = zmTaskParams.get('langdir-defname')

if qmpathprefix is None:
qmpathprefix = '@translations'

qmNodeDir = None
if rclangprefix is None:
qmNodeDir = btypeNode.find_or_declare(qmpathprefix)

try:
qmInfo = bld.qmInfo
Expand All @@ -274,38 +329,29 @@ def getQmNode(snode):
fname = fname[:extIdx] if extIdx > 0 else fname
fname = '%s%s%s' % (fnprefix, fname, '.qm')

if rclangprefix is None:
prefix = snode.parent.srcpath() if pathprefix is None else pathprefix
qmnode = btypeNode.find_or_declare('%s%s%s' % (prefix, os.sep, fname))
if qmNodeDir is not None:
qmnode = qmNodeDir.find_or_declare(fname)
else:
qmnode = snode.change_ext('.%d.qm' % tgen.idx)

# check if .qm paths are unique
qmpath = qmnode.abspath()
otherQm = qmInfo.get(qmpath)
if otherQm is not None:
msg = "Tasks '%s' and '%s'" % (tgen.name, otherQm['tgen-name'])
msg += " have the same output .qm path:\n %s" % qmpath
msg += "\nUse the 'qmpathprefix'/'unique-qmpaths' task "
msg += "parameters to fix it. Or don't use the same .ts files"
msg += " in these tasks."
raise error.ZenMakeError(msg)
# check if .qm paths are unique
_checkQmPathUnique(tgen, qmpath, qmInfo)

qmInfo[qmpath] = { 'tgen-name' : tgen.name, 'alies' : fname }

return qmnode

qmTasks = [tgen.create_task('ts2qm', x, getQmNode(x)) for x in tsFiles]

if rclangprefix is not None:
rcnode = 'rclang-%s' % tgen.name
rcnode = tgen.path.find_or_declare('%s.%d.qrc' % (rcnode, tgen.idx))

qmNodes = [x.outputs[0] for x in qmTasks]
kwargs = { 'qrcprefix' : rclangprefix }
qm2rccTask = tgen.create_task('qm2qrc', qmNodes, rcnode, **kwargs)
rccTask = qt5.create_rcc_task(tgen, qm2rccTask.outputs[0])
tgen.link_task.inputs.append(rccTask.outputs[0])
if rclangprefix is None:
qmdestdir = _createQmInstallTasks(tgen, qmTasks, zmTaskParams)
if langDirDefine:
if qmdestdir is None:
qmdestdir = qmNodeDir.abspath()
tgen.env.append_value('DEFINES', '%s="%s"' % (langDirDefine, qmdestdir))
else:
_createRcTranslTasks(tgen, qmTasks, rclangprefix)

@feature('qt5')
@after('apply_link')
Expand All @@ -328,7 +374,7 @@ def apply_qt5(tgen):
env.append_value('MOC_FLAGS', mocFlags)

# .ts -> .qm and other related tasks
_createTranslationTasks(tgen)
_createTranslTasks(tgen)

class qm2qrc(Task):
"""
Expand Down
4 changes: 3 additions & 1 deletion src/zenmake/zm/features/qt5_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
CONF_TASKSCHEME_SPEC = {
'base' : {
'moc' : PATHS_SCHEME,
'qmpathprefix' : { 'type': 'str' },
'bld-langprefix' : { 'type': 'str' },
'unique-qmpaths' : { 'type': 'bool' },
'rclangprefix' : { 'type': 'str' },
'langdir-defname': { 'type': 'str' },
'install-langdir': { 'type': 'str', 'traits': ['one-path', 'abs'], },
},
# Can be boolean or list of particular param names
# True means all keys from 'base'
Expand Down
Loading

0 comments on commit 1f66f1a

Please sign in to comment.