decorators: Use memoize() to implement all factory functions
[git-cola.git] / cola / app.py
blob402d02441021de4e52e36b431b6a403c150cf72a
1 # Copyright (C) 2009, David Aguilar <davvid@gmail.com>
2 """Provides the cola QApplication subclass"""
3 # style note: we use camelCase here since we're masquerading a Qt class
5 import os
7 from PyQt4 import QtCore
8 from PyQt4 import QtGui
10 from cola import utils
11 from cola import resources
12 from cola import i18n
13 from cola.decorators import memoize
16 @memoize
17 def instance(argv):
18 return QtGui.QApplication(list(argv))
21 class ColaApplication(object):
22 """The main cola application
24 ColaApplication handles i18n of user-visible data
25 """
27 def __init__(self, argv, locale=None, gui=True):
28 """Initialize our QApplication for translation
29 """
30 if locale:
31 os.environ['LANG'] = locale
32 i18n.install()
34 # monkey-patch Qt's translate() to use our translate()
35 if gui:
36 self._app = instance(tuple(argv))
37 self._app.setWindowIcon(QtGui.QIcon(resources.icon('git.svg')))
38 self._translate_base = QtGui.QApplication.translate
39 QtGui.QApplication.translate = self.translate
40 else:
41 self._app = QtCore.QCoreApplication(argv)
42 self._translate_base = QtCore.QCoreApplication.translate
43 QtCore.QCoreApplication.translate = self.translate
45 def translate(self, context, txt):
46 """
47 Translate strings with gettext
49 Supports @@noun/@@verb specifiers.
51 """
52 trtxt = i18n.gettext(txt)
53 if trtxt[-6:-4] == '@@': # handle @@verb / @@noun
54 trtxt = trtxt[:-6]
55 return trtxt
57 def activeWindow(self):
58 """Wrap activeWindow()"""
59 return self._app.activeWindow()
61 def exec_(self):
62 """Wrap exec_()"""
63 return self._app.exec_()
65 def setStyleSheet(self, txt):
66 """Wrap setStyleSheet(txt)"""
67 return self._app.setStyleSheet(txt)