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 PyQt4.QtCore import *
00029 from PyQt4.QtGui import *
00030 from Koo.Fields.AbstractFieldWidget import *
00031 from Koo.Common import Api
00032 from Koo.Common import Notifier
00033 from Koo.Common import Icons
00034 from Koo.Common import Common
00035 from Koo.Common import Api
00036 from Koo.Rpc import Rpc
00037
00038 class ButtonFieldWidget( AbstractFieldWidget ):
00039 def __init__(self, parent, view, attributes) :
00040 AbstractFieldWidget.__init__( self, parent, view, attributes )
00041
00042 self.button = QPushButton( self )
00043 layout = QHBoxLayout(self)
00044 layout.setContentsMargins(0, 0, 0, 0)
00045 layout.addWidget( self.button )
00046
00047 self.button.setText( Common.normalizeLabel( attributes.get('string', 'unknown' ) ) )
00048 if 'icon' in attributes:
00049 self.button.setIcon( Icons.kdeIcon( attributes['icon'] ))
00050
00051 self.connect( self.button, SIGNAL('clicked()'), self.click)
00052
00053 def addShortcut(self, keys):
00054 if not keys:
00055 return
00056 shortcut = QShortcut(QKeySequence(keys), self)
00057 self.connect(shortcut, SIGNAL('activated()'), self.button.click)
00058
00059 def click( self ):
00060 if not self.record:
00061 return
00062
00063
00064 screen = self.view.screen
00065 self.view.store()
00066 if self.attrs.get('special', '') == 'cancel':
00067 screen.close()
00068 if 'name' in self.attrs.keys():
00069 result = Rpc.session.execute(
00070 '/object', 'execute', screen.name,
00071 self.attrs['name'], [], self.record.context()
00072 )
00073 datas = {}
00074 Api.instance.executeAction( result, datas, screen.context )
00075 return
00076
00077 if self.record.validate():
00078 id = screen.save()
00079 if not self.attrs.get('confirm',False) or \
00080 QMessageBox.question(self,_('Question'),self.attrs['confirm'], _("Yes"), _("No")) == 0:
00081 type = self.attrs.get('type', 'workflow')
00082 if type == 'workflow':
00083 QApplication.setOverrideCursor( Qt.WaitCursor )
00084 try:
00085
00086
00087 result = Rpc.session.execute('/object', 'exec_workflow', screen.name, self.name, id)
00088 if isinstance( result, dict ):
00089 if result['type'] == 'ir.actions.act_window_close':
00090 screen.close()
00091 else:
00092 Api.instance.executeAction( result, {'ids': [id]} )
00093 elif isinstance( result, list ):
00094 for r in result:
00095 Api.instance.executeAction( r, { 'ids': [id] } )
00096 except Rpc.RpcException, e:
00097 pass
00098 QApplication.restoreOverrideCursor()
00099 elif type == 'object':
00100 if not id:
00101 return
00102 QApplication.setOverrideCursor( Qt.WaitCursor )
00103 try:
00104 result = Rpc.session.execute('/object', 'execute', screen.name, self.name, [id], self.record.context())
00105 except Rpc.RpcException, e:
00106 QApplication.restoreOverrideCursor()
00107 return
00108 QApplication.restoreOverrideCursor()
00109 if isinstance( result, dict ):
00110 screen.close()
00111 Api.instance.executeAction( result, {}, screen.context)
00112
00113 elif type == 'action':
00114 action_id = int(self.attrs['name'])
00115 Api.instance.execute( action_id, {'model':screen.name, 'id': id, 'ids': [id]}, context=screen.context )
00116 else:
00117 Notifier.notifyError( _('Error in Button'), _('Button type not allowed'), _('Button type not allowed') )
00118
00119 QApplication.setOverrideCursor( Qt.WaitCursor )
00120 try:
00121 screen.reload()
00122 except Rpc.RpcException, e:
00123 pass
00124 QApplication.restoreOverrideCursor()
00125 else:
00126 Notifier.notifyWarning('',_('Invalid Form, correct red fields!'))
00127 screen.display()
00128
00129 def setReadOnly(self, value):
00130 AbstractFieldWidget.setReadOnly(self, value)
00131 self.button.setEnabled( not value )
00132
00133 def showValue(self):
00134 if not self.attrs.get('states', False):
00135 self.show()
00136 return
00137
00138 state = 'draft'
00139 if self.record and self.record.fieldExists('state'):
00140 state = self.record.value('state')
00141 states = self.attrs.get('states', '').split(',')
00142 if state in states:
00143 self.show()
00144 else:
00145 self.hide()
00146