#!/usr/bin/env python

# monkey-patching trick found on http://www.themacaque.com/?p=816

### import and wrap compiler.UICompiler and
from PyQt4.uic.Compiler import compiler, qtproxies, indenter

# pylint: disable=C0103
class _UICompiler(compiler.UICompiler):
    """Speciallized compiler for qt .ui files."""

    def createToplevelWidget(self, classname, widgetname):
        o = indenter.getIndenter()
        o.level = 0
        o.write("""
import sys
if sys.platform == "win32":
    from liveusb import utf8_gettext as _
else:
    from liveusb import _
from liveusb import branding

def translate_and_brand(string):
    return _(string) % branding
""")
        return super(_UICompiler, self).createToplevelWidget(classname, widgetname)

compiler.UICompiler = _UICompiler

### wrap qtproxies.i18n_string
class _i18n_string(qtproxies.i18n_string):
    """Provide a translated text."""

    def __str__(self):
        return "translate_and_brand('%s')" % self.string.encode('string-escape')

qtproxies.i18n_string = _i18n_string

class _QPixmap(qtproxies.LiteralProxyClass):
    def __init__(self, arg):
        self._uic_name = "QtGui.QPixmap(%s %% branding)" % \
                     (qtproxies.obj_to_argument(arg),)

qtproxies.QtGui.QPixmap = _QPixmap

# there's no main function, so just import the module
import PyQt4.uic.pyuic
