1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 """Utility functions"""
29
30 from PyQt4 import QtCore, QtGui
31
33 return lambda:constant
34 """
35 A Note on GUI Types
36
37 Because QVariant is part of the QtCore library, it cannot provide conversion functions to data types defined in QtGui, such as
38 QColor, QImage, and QPixmap. In other words, there is no toColor() function.
39 Instead, you can use the QVariant.value() or the qVariantValue() template function. For example:
40
41 QVariant variant;
42 ...
43 QColor color = variant.value<QColor>();
44
45 The inverse conversion (e.g., from QColor to QVariant) is automatic for all data types supported by QVariant, including GUI-related
46 types:
47
48 QColor color = palette().background().color();
49 QVariant variant = color;
50 """
52 """Try to convert a QVariant to a python object as good
53 as possible"""
54 import datetime
55 if not qvariant:
56 return None
57 if qvariant.isNull():
58 return None
59 type = qvariant.type()
60 if type == QtCore.QVariant.String:
61 value = unicode(qvariant.toString())
62 elif type == QtCore.QVariant.Date:
63 value = qvariant.toDate()
64 value = datetime.date(year=value.year(),
65 month=value.month(),
66 day=value.day())
67 elif type == QtCore.QVariant.Int:
68 value = int(qvariant.toInt()[0])
69 elif type == QtCore.QVariant.LongLong:
70 value = int(qvariant.toLongLong()[0])
71 elif type == QtCore.QVariant.Double:
72 value = float(qvariant.toDouble()[0])
73 elif type == QtCore.QVariant.Bool:
74 value = bool(qvariant.toBool())
75 elif type == QtCore.QVariant.Time:
76 value = qvariant.toTime()
77 value = datetime.time(hour = value.hour(),
78 minute = value.minute(),
79 second = value.second())
80 elif type == QtCore.QVariant.DateTime:
81 value = qvariant.toDateTime()
82 value = value.toPyDateTime ()
83 elif type == QtCore.QVariant.Color:
84 value = QtGui.QColor(qvariant)
85 else:
86 value = qvariant.toPyObject()
87
88 return value
89
90
91
92
93
94 _translations_ = {}
95
99
110
111 -def ugettext(string_to_translate):
112 """Translate the string_to_translate to the language of the current locale.
113 This is a two step process. First the function will try to get the
114 translation out of the Translation entity, if this is not successfull, the
115 function will ask QCoreApplication to translate string_to_translate
116 (which tries to get the translation from the .po files)"""
117 assert isinstance(string_to_translate, basestring)
118 result = _translations_.get(string_to_translate, None)
119 if not result:
120 result = unicode(QtCore.QCoreApplication.translate('', QtCore.QString(string_to_translate)))
121 return result
122
123 -class ugettext_lazy(object):
124
125 - def __init__(self, string_to_translate):
126 assert isinstance(string_to_translate, basestring)
127 self._string_to_translate = string_to_translate
128
130 return ugettext(self._string_to_translate)
131
132 - def __unicode__(self):
133 return ugettext(self._string_to_translate)
134