Package Camelot :: Package camelot :: Package core :: Module utils |
[frames] | no frames]

# Source Code for Module Camelot.camelot.core.utils

      1  #  ============================================================================

      2  #

      3  #  Copyright (C) 2007-2008 Conceptive Engineering bvba. All rights reserved.

      4  #  www.conceptive.be / project-camelot@conceptive.be

      5  #

      6  #  This file is part of the Camelot Library.

      7  #

      8  #  This file may be used under the terms of the GNU General Public

      9  #  License version 2.0 as published by the Free Software Foundation

     10  #  and appearing in the file LICENSE.GPL included in the packaging of

     11  #  this file.  Please review the following information to ensure GNU

     12  #  General Public Licensing requirements will be met:

     13  #  http://www.trolltech.com/products/qt/opensource.html

     14  #

     15  #  If you are unsure which license is appropriate for your use, please

     16  #  review the following information:

     17  #  http://www.trolltech.com/products/qt/licensing.html or contact

     18  #  project-camelot@conceptive.be.

     19  #

     20  #  This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE

     21  #  WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.

     22  #

     23  #  For use of this library in commercial applications, please contact

     24  #  project-camelot@conceptive.be

     25  #

     26  #  ============================================================================

     27

     28  """Utility functions"""
     29

     30  from PyQt4 import QtCore, QtGui
     31

     32 -def create_constant_function(constant):
     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  """
     51 -def variant_to_pyobject(qvariant=None):
     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  # Global dictionary containing all user defined translations in the

     92  # current locale

     93  #

     94  _translations_ = {}
     95

     96 -def set_translation(source, value):
     97      """Store a tranlation in the global translation dictionary"""
     98      _translations_[source] = value
     99

    100 -def load_translations():
    101      """Fill the global dictionary of translations with all data from the

    102      database, to be able to do fast gui thread lookups of translations"""
    103      language = unicode(QtCore.QLocale().name())
    104      from camelot.model.i18n import Translation
    105      tls = Translation.query.filter(Translation.language==language)
    106      tls = tls.filter(Translation.value!=None).all()
    107      for t in tls:
    108          if t.value:
    109              _translations_[t.source] = t.value
    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

    129 -    def __str__(self):
    130          return ugettext(self._string_to_translate)
    131

    132 -    def __unicode__(self):
    133          return ugettext(self._string_to_translate)
    134