Package Camelot :: Package camelot :: Package view :: Package controls :: Module user_translatable_label |
[frames] | no frames]

# Source Code for Module Camelot.camelot.view.controls.user_translatable_label

     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  from PyQt4 import QtGui, QtCore
    29  from PyQt4.QtCore import Qt
    30
    31  from camelot.core.utils import ugettext_lazy
    32  from camelot.core.utils import ugettext as _
    33  from camelot.view.art import Icon
    34
    35 -class TranslateLabelAction(QtGui.QAction):
    36
    37      translate_icon = Icon( 'tango/16x16/apps/preferences-desktop-locale.png' )
    38
    39 -    def __init__(self, parent):
    40          super(TranslateLabelAction, self).__init__(_('Change translation'), parent)
    41          self.setIcon(self.translate_icon.getQIcon())
    42
    43 -class UserTranslatableLabel(QtGui.QLabel):
    44      """A QLabel that allows the user to translate the text contained
    45  within by right clicking on it and selecting the appropriate submenu.
    46  """
    47
    48 -    def __init__ (self, text, parent=None):
    49          """:param text: the text to be displayed within the label, this can
    50          be either a normal string or a ugettext_lazy string, only in the last
    51          case, the label will be translatable"""
    52          super(UserTranslatableLabel, self).__init__(unicode(text),
    53                                                      parent)
    54          self.setContextMenuPolicy(Qt.ActionsContextMenu)
    55          if isinstance(text, (ugettext_lazy)):
    56              self._text = text
    57              translate_action = TranslateLabelAction(self)
    58              self.connect(translate_action, QtCore.SIGNAL('triggered()'), self.change_translation)
    59              self.addAction(translate_action)
    60          else:
    61              self._text = None
    62
    63 -    def change_translation(self):
    64          if self._text:
    65              new_translation, ok = QtGui.QInputDialog.getText(self,
    66                                                               _('Change translation'),
    67                                                               _('Translation'),
    68                                                               QtGui.QLineEdit.Normal,
    69                                                               unicode(self._text))
    70              if ok:
    71                  from camelot.core.utils import set_translation
    72                  self.setText(new_translation)
    73                  set_translation(self._text._string_to_translate, new_translation)
    74                  from camelot.view.model_thread import post
    75                  post(self.create_update_translation_table(self._text._string_to_translate,
    76                                                            unicode(QtCore.QLocale().name()),
    77                                                            unicode(new_translation)))
    78
    79 -    def create_update_translation_table(self, source, language, value):
    80
    81          def update_translation_table():
    82              from camelot.model.i18n import Translation
    83              from sqlalchemy.orm.session import Session
    84              t = Translation.get_by(source=source, language=language)
    85              if not t:
    86                  t = Translation(source=source, language=language)
    87              t.value = value
    88              Session.object_session( t ).flush( [t] )
    89
    90          return update_translation_table
    91