00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 from Koo import Rpc
00029 from FieldPreferencesDialog import *
00030 from PyQt4.QtGui import *
00031 from PyQt4.QtCore import *
00032
00033
00034
00035
00036 class AbstractFieldDelegate(QStyledItemDelegate):
00037
00038
00039
00040 def __init__(self, parent, attributes):
00041 QStyledItemDelegate.__init__(self, parent)
00042 self.name = attributes['name']
00043 self.attributes = attributes
00044 self.colors = {
00045 'invalid' : '#FF6969',
00046 'readonly' : '#e3e3e3',
00047 'required' : '#ddddff',
00048 'normal' : 'white'
00049 }
00050 self.defaultMenuEntries = [
00051
00052 ]
00053
00054 def createEditor(self, parent, option, index):
00055
00056 model = index.model().recordFromIndex( index )
00057 if model and not model.isFieldValid( self.name ):
00058 name = 'invalid'
00059 elif self.attributes.get('readonly', False):
00060 name = 'readonly'
00061 elif self.attributes.get('required', False):
00062 name = 'required'
00063 else:
00064 name = 'normal'
00065 editor = QStyledItemDelegate.createEditor( self, parent, option, index )
00066 color = QColor( self.colors.get( name, 'white' ) )
00067 palette = QPalette()
00068 palette.setColor(QPalette.Base, color)
00069 editor.setPalette(palette);
00070 return editor
00071
00072
00073
00074
00075 def menuEntries(self, record):
00076 return []
00077
00078
00079
00080 def showPopupMenu(self, parent, position):
00081
00082 index = parent.indexAt( parent.mapFromGlobal( position ) )
00083 if not index or not index.isValid():
00084 return
00085 record = index.model().recordFromIndex( index )
00086
00087 entries = self.defaultMenuEntries[:]
00088 new = self.menuEntries( record )
00089 if len(new) > 0:
00090 entries = entries + [(None, None, None)] + new
00091 if not entries:
00092 return
00093 menu = QMenu( parent )
00094 for title, slot, enabled in entries:
00095 if title:
00096 item = QAction( title, menu )
00097 if slot:
00098 self.connect( item, SIGNAL("triggered()"), slot )
00099 item.setEnabled( enabled )
00100 menu.addAction( item )
00101 else:
00102 menu.addSeparator()
00103 menu.popup( position )
00104
00105
00106
00107
00108 def setToDefault(self):
00109 try:
00110 model = self.record.group.resource
00111 res = Rpc.session.call('/object', 'execute', model, 'default_get', [self.attrs['name']])
00112 self.record.setValue(self.name, res.get(self.name, False))
00113 self.display()
00114 except:
00115 QMessageBox.warning(None, _('Operation not permited'), _('You can not set to the default value here !') )
00116 return False