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

# Source Code for Module Camelot.camelot.view.controls.editors.embeddedmany2oneeditor

     1  import logging
     2  logger = logging.getLogger('camelot.view.controls.editors.embeddedmany2oneeditor')
     3

     4  from customeditor import CustomEditor, QtCore, QtGui
     5  from wideeditor import WideEditor
     6  from camelot.view.model_thread import post
     7  from camelot.view.proxy import ValueLoading
     8

     9 -class EmbeddedMany2OneEditor( CustomEditor, WideEditor ):
    10      """Widget for editing a many 2 one relation a a form embedded in another

    11    form.

    12    """
    13

    14 -    def __init__( self, admin = None, parent = None, **kwargs ):
    15          assert admin != None
    16          CustomEditor.__init__( self, parent )
    17          self.admin = admin
    18          #

    19          # The admin class of the current entity can be different from

    20          # self.admin, since the current entity can be a subclass of

    21          # the entity for which self.admin was made

    22          #

    23          self.current_entity_admin = None
    24          self.layout = QtGui.QHBoxLayout()
    25          self.entity_instance_getter = None
    26          self.form = None
    27          self.model = None
    28          self.setLayout( self.layout )
    29          self.setEntity( lambda:ValueLoading, propagate = False )
    30

    31 -    def set_value( self, value ):
    32          value = CustomEditor.set_value( self, value )
    33          if value:
    34              self.setEntity( value, propagate = False )
    35

    36 -    def setEntity( self, entity_instance_getter, propagate = True ):
    37

    38          def create_instance_getter( entity_instance ):
    39              return lambda:entity_instance
    40

    41          def set_entity_instance():
    42              entity = entity_instance_getter()
    43              current_entity_admin = None
    44              if entity:
    45                  if entity!=ValueLoading:
    46                      self.entity_instance_getter = create_instance_getter( entity )
    47                      current_entity_admin = self.admin.get_related_entity_admin( entity.__class__ )
    48                  else:
    49                      return False, False, current_entity_admin
    50              else:
    51                  try:
    52                      new_entity = self.admin.entity()
    53                  except Exception, e:
    54                      logger.error('ProgrammingError : could not create a new entity of type %s'%(self.admin.entity.__name__), exc_info=e)
    55                  self.entity_instance_getter = create_instance_getter( new_entity )
    56                  current_entity_admin = self.admin
    57              return True, propagate, current_entity_admin
    58

    59          post( set_entity_instance, self.update_form )
    60

    61 -    def update_form(self, update_form_and_propagate ):
    62          from camelot.view.proxy.collection_proxy import CollectionProxy
    63

    64          update_form, propagate, current_entity_admin = update_form_and_propagate
    65

    66          if update_form:
    67

    68              def create_collection_getter( instance_getter ):
    69                  return lambda:[instance_getter()]
    70

    71              if self.model==None or self.model.get_admin()!=current_entity_admin:
    72                  # We cannot reuse the current model and form

    73                  if self.form:
    74                      self.form.deleteLater()
    75                      self.layout.removeWidget( self.form )
    76

    77                  self.model = CollectionProxy( current_entity_admin,

    78                                                create_collection_getter( self.entity_instance_getter ),

    79                                                current_entity_admin.get_fields )
    80                  self.form = current_entity_admin.create_form_view( '', self.model, 0, self )
    81                  self.layout.addWidget( self.form )
    82              else:
    83                  # We can reuse the form, just update the content of the collection

    84                  self.model.set_collection_getter(create_collection_getter( self.entity_instance_getter ))
    85

    86          if propagate:
    87              self.emit( QtCore.SIGNAL( 'editingFinished()' ) )
    88