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.QtGui import *
00029 from PyQt4.QtCore import *
00030 from Koo.Common import Api
00031 from Koo.Common import Common
00032 from Koo.Plugins import *
00033 from Koo import Rpc
00034
00035
00036
00037 class Action(QAction):
00038
00039 def __init__(self, parent):
00040 QAction.__init__(self, parent)
00041 self._data = None
00042 self._type = None
00043 self._model = None
00044
00045
00046 def setData(self, data):
00047 self._data = data
00048
00049
00050 def data(self):
00051 return self._data
00052
00053
00054 def setType(self, type):
00055 self._type = type
00056
00057
00058 def type(self):
00059 return self._type
00060
00061
00062 def setModel(self, model):
00063 self._model = model
00064
00065
00066 def model(self):
00067 return self._model
00068
00069
00070
00071 def execute(self, currentId, selectedIds, context):
00072 if self._type == 'relate':
00073 self.executeRelate( currentId, context )
00074 elif self._type in ( 'action', 'print' ):
00075 self.executeAction( currentId, selectedIds, context )
00076 else:
00077 self.executePlugin( currentId, selectedIds, context )
00078
00079
00080 def executeRelate(self, currentId, context ):
00081 if not currentId:
00082 QMessageBox.information( self, _('Information'), _('You must select a record to use the relate button !'))
00083 Api.instance.executeAction(self._data, {
00084 'id': currentId
00085 }, context)
00086
00087
00088 def executeAction(self, currentId, selectedIds, context):
00089 if not currentId and not selectedIds:
00090 QMessageBox.information(self, _('Information'), _('You must save this record to use the relate button !'))
00091 return False
00092
00093 if not currentId:
00094 currentId = selectedIds[0]
00095 elif not selectedIds:
00096 selectedIds = [currentId]
00097 if self._type == 'print':
00098 QApplication.setOverrideCursor( Qt.WaitCursor )
00099 try:
00100 Api.instance.executeAction(self._data, {
00101 'id': currentId,
00102 'ids': selectedIds,
00103 'model': self._model
00104 }, context )
00105 except Rpc.RpcException:
00106 pass
00107 if self._type == 'print':
00108 QApplication.restoreOverrideCursor()
00109
00110
00111 def executePlugin(self, currentId, selectedIds, context):
00112 Plugins.execute( self._data, self._model, currentId, selectedIds, context )
00113
00114
00115
00116
00117 class ActionFactory:
00118
00119
00120
00121
00122 @staticmethod
00123 def create(parent, definition, model):
00124 if not definition:
00125
00126
00127 definition = {
00128 'print': [],
00129 'action': [],
00130 'relate': []
00131 }
00132
00133
00134 definition['print'].append({
00135 'name': 'Print Screen',
00136 'string': _('Print Screen'),
00137 'report_name': 'printscreen.list',
00138 'type': 'ir.actions.report.xml'
00139 })
00140
00141 actions = []
00142 for icontype in ( 'print','action','relate' ):
00143 for tool in definition[icontype]:
00144 action = Action( parent )
00145 action.setIcon( QIcon( ":/images/%s.png" % icontype) )
00146 action.setText( Common.normalizeLabel( tool['string'] ) )
00147 action.setType( icontype )
00148 action.setData( tool )
00149 action.setModel( model )
00150
00151 number = len(actions)
00152 shortcut = 'Ctrl+'
00153 if number > 9:
00154 shortcut += 'Shift+'
00155 number -= 10
00156 if number < 10:
00157 shortcut += str(number)
00158 action.setShortcut( QKeySequence( shortcut ) )
00159 action.setToolTip( action.text() + ' (%s)' % shortcut )
00160
00161 actions.append( action )
00162
00163 plugs = Plugins.list(model)
00164 for p in sorted( plugs.keys(), key=lambda x:plugs[x].get('string','') ) :
00165 action = Action( parent )
00166 action.setIcon( QIcon( ":/images/exec.png" ) )
00167 action.setText( unicode( plugs[p]['string'] ) )
00168 action.setData( p )
00169 action.setType( 'plugin' )
00170 action.setModel( model )
00171 actions.append( action )
00172 return actions
00173