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

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

     1  from PyQt4 import QtGui, QtCore
     2
     3  from camelot.core.utils import ugettext as _
     4  from camelot.admin.object_admin import ObjectAdmin
     5  from camelot.view.controls.editors.one2manyeditor import One2ManyEditor
     6  from user_translatable_label import UserTranslatableLabel
     7
     8 -class Attribute(object):
     9      """Helper class representing a field attribute's name and its value"""
    10 -    def __init__(self, name, value):
    11          self.name = unicode(name)
    12          self.value = unicode(value)
    13
    14 -    class Admin(ObjectAdmin):
    15          list_display = ['name', 'value']
    16          field_attributes = {'name':{'minimal_column_width':25},
    17                              'value':{'minimal_column_width':25}}
    18
    19 -class FieldLabel(UserTranslatableLabel):
    20      """A Label widget used to display the name of a field on a form.
    21      This label provides the user with the possibility to change the translation
    22      of the label and review its field attributes.
    23      """
    24
    25 -    def __init__(self, field_name, text, field_attributes, admin, parent=None):
    26          """
    27          :param field_name: the name of the field
    28          :param text: user translatable string to be used as field label
    29          :param field_attributes: the field attributes associated with the field for which
    30          this is a label
    31          :param admin: the admin of the object of the field
    32          """
    33          super(FieldLabel, self).__init__(text, parent)
    34          show_field_attributes_action = QtGui.QAction(_('View attributes'), self)
    35          self.connect(show_field_attributes_action, QtCore.SIGNAL('triggered()'), self.show_field_attributes)
    36          self.addAction(show_field_attributes_action)
    37          self._field_name = field_name
    38          self._admin = admin
    39          self._field_attributes = field_attributes
    40
    41 -    def get_attributes(self):
    42          import inspect
    43
    44          def attribute_value_to_string(key, value):
    45              if inspect.isclass(value):
    46                  return value.__name__
    47              return unicode(value)
    48
    49          return [Attribute(key,attribute_value_to_string(key, value)) for key,value in self._field_attributes.items()]
    50
    51 -    def show_field_attributes(self):
    52          from camelot.view.proxy.collection_proxy import CollectionProxy
    53
    54          admin = self._admin.get_related_entity_admin(Attribute)
    55          attributes_collection = CollectionProxy(admin=admin,
    56                                                  collection_getter=self.get_attributes,
    57                                                  columns_getter=admin.get_columns)
    58
    59          class FieldAttributesDialog(QtGui.QDialog):
    60
    61              def __init__(self, field_name, parent=None):
    62                  super(FieldAttributesDialog, self).__init__(parent)
    63                  self.setWindowTitle(_('Field Attributes'))
    64                  layout = QtGui.QVBoxLayout()
    65                  layout.addWidget( QtGui.QLabel(field_name) )
    66                  editor = One2ManyEditor(admin=admin, editable=False, parent=self)
    67                  editor.set_value(attributes_collection)
    68                  editor.setMinimumSize(600, 400)
    69                  layout.addWidget(editor)
    70                  self.setLayout(layout)
    71
    72          dialog = FieldAttributesDialog(self._field_name, self)
    73          dialog.exec_()
    74