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

Source Code for Module Camelot.camelot.view.controls.editors.one2manyeditor

  1  import logging 
  2  
 
  3  logger = logging.getLogger( 'camelot.view.controls.editors.onetomanyeditor' ) 
  4  from customeditor import CustomEditor, QtCore, QtGui, Qt 
  5  from wideeditor import WideEditor 
  6  
 
  7  from camelot.view.art import Icon 
  8  from camelot.view.model_thread import gui_function, model_function, post 
  9  from camelot.core.utils import ugettext as _ 
10 11 -class One2ManyEditor( CustomEditor, WideEditor ):
12
13 - def __init__( self, 14 admin = None, 15 parent = None, 16 create_inline = False, 17 editable = True, 18 vertical_header_clickable = True, 19 **kw ):
20 """ 21 :param admin: the Admin interface for the objects on the one side of the 22 relation 23 24 :param create_inline: if False, then a new entity will be created within a 25 new window, if True, it will be created inline 26 27 :param vertical_header_clickable: True if the vertical header is clickable by the user, False if not. 28 29 after creating the editor, set_value needs to be called to set the 30 actual data to the editor 31 """ 32 33 CustomEditor.__init__( self, parent ) 34 layout = QtGui.QHBoxLayout() 35 layout.setContentsMargins( 0, 0, 0, 0 ) 36 # 37 # Setup table 38 # 39 from camelot.view.controls.tableview import TableWidget 40 # parent set by layout manager 41 self.table = TableWidget() 42 rowHeight = QtGui.QFontMetrics( self.font() ).height() + 5 43 self.table.verticalHeader().setDefaultSectionSize( rowHeight ) 44 layout.setSizeConstraint( QtGui.QLayout.SetNoConstraint ) 45 self.setSizePolicy( QtGui.QSizePolicy.Expanding, 46 QtGui.QSizePolicy.Expanding ) 47 if vertical_header_clickable: 48 self.connect( self.table.verticalHeader(), 49 QtCore.SIGNAL( 'sectionClicked(int)' ), 50 self.createFormForIndex ) 51 self.admin = admin 52 self.editable = editable 53 self.create_inline = create_inline 54 layout.addWidget( self.table ) 55 self.setupButtons( layout ) 56 self.setLayout( layout ) 57 self.model = None
58
59 - def setupButtons( self, layout ):
60 button_layout = QtGui.QVBoxLayout() 61 button_layout.setSpacing( 0 ) 62 delete_button = QtGui.QToolButton() 63 icon = Icon( 'tango/16x16/places/user-trash.png' ).getQIcon() 64 delete_button.setIcon( icon ) 65 delete_button.setAutoRaise( True ) 66 delete_button.setToolTip(_('delete')) 67 self.connect( delete_button, 68 QtCore.SIGNAL( 'clicked()' ), 69 self.deleteSelectedRows ) 70 add_button = QtGui.QToolButton() 71 icon = Icon( 'tango/16x16/actions/document-new.png' ).getQIcon() 72 add_button.setIcon( icon ) 73 add_button.setAutoRaise( True ) 74 add_button.setToolTip(_('new')) 75 self.connect( add_button, QtCore.SIGNAL( 'clicked()' ), self.newRow ) 76 copy_button = QtGui.QToolButton() 77 icon = Icon( 'tango/16x16/actions/edit-copy.png' ).getQIcon() 78 copy_button.setIcon( icon ) 79 copy_button.setAutoRaise( True ) 80 copy_button.setToolTip(_('copy')) 81 self.connect( copy_button, QtCore.SIGNAL( 'clicked()' ), self.copy_selected_rows ) 82 export_button = QtGui.QToolButton() 83 export_button.setIcon( Icon( 'tango/16x16/mimetypes/x-office-spreadsheet.png' ).getQIcon() ) 84 export_button.setAutoRaise( True ) 85 export_button.setToolTip(_('export as spreadsheet')) 86 self.connect( export_button, 87 QtCore.SIGNAL( 'clicked()' ), 88 self.exportToExcel ) 89 button_layout.addStretch() 90 if self.editable: 91 button_layout.addWidget( add_button ) 92 button_layout.addWidget( copy_button ) 93 button_layout.addWidget( delete_button ) 94 button_layout.addWidget( export_button ) 95 layout.addLayout( button_layout )
96
97 - def exportToExcel( self ):
98 from camelot.view.export.excel import open_data_with_excel 99 100 def export(): 101 title = self.admin.get_verbose_name_plural() 102 columns = self.admin.get_columns() 103 if self.model: 104 data = list( self.model.getData() ) 105 open_data_with_excel( title, columns, data )
106 107 post( export )
108
109 - def getModel( self ):
110 return self.model
111
112 - def update_delegates( self, *args ):
113 if self.model: 114 delegate = self.model.getItemDelegate() 115 if delegate: 116 self.table.setItemDelegate( delegate ) 117 for i in range( self.model.columnCount() ): 118 txtwidth = self.model.headerData( i, Qt.Horizontal, Qt.SizeHintRole ).toSize().width() 119 colwidth = self.table.columnWidth( i ) 120 self.table.setColumnWidth( i, max( txtwidth, colwidth ) )
121
122 - def set_value( self, model ):
123 model = CustomEditor.set_value( self, model ) 124 if model: 125 self.model = model 126 # sorted_model = QtGui.QSortFilterProxyModel( self ) 127 # sorted_model.setSourceModel( model ) 128 self.table.setModel( model ) 129 130 def create_fill_model_cache( model ): 131 132 def fill_model_cache(): 133 model._extend_cache( 0, 10 )
134 135 return fill_model_cache 136 137 post( create_fill_model_cache( model ), self.update_delegates ) 138 139 @gui_function
140 - def activate_editor( self, row ):
141 index = self.model.index( row, 0 ) 142 self.table.scrollToBottom() 143 self.table.setCurrentIndex( index ) 144 self.table.edit( index )
145
146 - def newRow( self ):
147 from camelot.view.workspace import get_workspace 148 workspace = get_workspace() 149 150 if self.create_inline: 151 152 @model_function 153 def create(): 154 o = self.admin.entity() 155 row = self.model.insertEntityInstance( 0, o ) 156 self.admin.set_defaults( o ) 157 return row
158 159 post( create, self.activate_editor ) 160 161 else: 162 prependentity = lambda o: self.model.insertEntityInstance( 0, o ) 163 removeentity = lambda o: self.model.removeEntityInstance( o ) 164 # 165 # We cannot use the workspace as a parent, in case of working with 166 # the NoDesktopWorkspaces 167 # 168 form = self.admin.create_new_view( parent = None, 169 oncreate = prependentity, 170 onexpunge = removeentity ) 171 workspace.addSubWindow( form ) 172 form.show() 173
174 - def copy_selected_rows( self ):
175 """Copy the selected rows in this tableview""" 176 logger.debug( 'delete selected rows called' ) 177 for row in set( map( lambda x: x.row(), self.table.selectedIndexes() ) ): 178 self.model.copy_row( row )
179
180 - def deleteSelectedRows( self ):
181 """Delete the selected rows in this tableview""" 182 logger.debug( 'delete selected rows called' ) 183 for row in set( map( lambda x: x.row(), self.table.selectedIndexes() ) ): 184 self.model.removeRow( row )
185
186 - def createFormForIndex( self, index ):
187 from camelot.view.proxy.collection_proxy import CollectionProxy 188 from camelot.view.workspace import get_workspace 189 model = CollectionProxy( self.admin, 190 self.model.collection_getter, 191 self.admin.get_fields, 192 max_number_of_rows = 1, 193 edits = None ) 194 form = self.admin.create_form_view( u'', model, self.model.map_to_source(index), get_workspace() ) 195 get_workspace().addSubWindow( form ) 196 form.show()
197