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
00029 from PyQt4.QtGui import *
00030
00031 from Koo.Fields.AbstractFieldWidget import *
00032 from Koo.Fields.AbstractFieldDelegate import *
00033
00034 class BooleanFieldWidget(AbstractFieldWidget):
00035 def __init__(self, parent, model, attrs={}):
00036 AbstractFieldWidget.__init__(self, parent, model, attrs)
00037 self.setSizePolicy( QSizePolicy.Preferred, QSizePolicy.Fixed )
00038 self.widget = QCheckBox( self )
00039 self.widget.setSizePolicy( QSizePolicy.Fixed, QSizePolicy.Fixed )
00040 layout = QHBoxLayout( self )
00041 layout.setContentsMargins( 0, 0, 0, 0 )
00042 layout.setSpacing( 0 )
00043 layout.addWidget( self.widget )
00044 layout.setAlignment( Qt.AlignLeft )
00045 self.installPopupMenu( self.widget )
00046 self.connect( self.widget, SIGNAL('stateChanged(int)'), self.callModified )
00047
00048 def callModified(self, value):
00049 self.modified()
00050
00051 def setReadOnly(self, value):
00052 AbstractFieldWidget.setReadOnly(self, value)
00053 self.widget.setEnabled( not value )
00054
00055 def storeValue(self):
00056 self.record.setValue(self.name, self.widget.isChecked())
00057
00058 def clear(self):
00059 self.widget.setChecked(False)
00060
00061 def showValue(self):
00062 self.widget.setChecked(self.record.value(self.name))
00063
00064 def colorWidget(self):
00065 return self.widget
00066
00067 class BooleanFieldDelegate( AbstractFieldDelegate ):
00068 def createEditor(self, parent, option, index):
00069 return QCheckBox(parent)
00070
00071 def setEditorData(self, editor, index):
00072 editor.setChecked( index.data(Qt.EditRole).toBool() )
00073
00074 def setModelData(self, editor, model, index):
00075 model.setData( index, QVariant( editor.isChecked() ), Qt.EditRole )
00076
00077 def paint(self, painter, option, index):
00078
00079 itemOption = QStyleOptionViewItemV4(option)
00080
00081
00082 QApplication.style().drawControl(QStyle.CE_ItemViewItem, itemOption, painter, None)
00083
00084
00085 op = QStyleOptionButton()
00086 op.rect = option.rect
00087 value = index.data(Qt.DisplayRole).toBool()
00088 if value:
00089 op.state = QStyle.State_On
00090 else:
00091 op.state = QStyle.State_Off
00092
00093
00094 QApplication.style().drawControl(QStyle.CE_CheckBox, op, painter, None)
00095