not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / plasma / scriptengines / python / pyappletscript.py
blob7839485de2d6e7f0ee8af65ba793f7bf51e473bd
2 # Copyright 2008 Simon Edwards <simon@simonzone.com>
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU Library General Public License as
6 # published by the Free Software Foundation; either version 2, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details
14 # You should have received a copy of the GNU Library General Public
15 # License along with this program; if not, write to the
16 # Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 from PyQt4.QtCore import *
20 from PyQt4.QtGui import *
21 from PyKDE4.plasma import Plasma
22 import plasma_importer
24 class PythonAppletScript(Plasma.AppletScript):
25 importer = None
27 def __init__(self, parent):
28 Plasma.AppletScript.__init__(self,parent)
29 #print("PythonAppletScript()")
30 if PythonAppletScript.importer is None:
31 PythonAppletScript.importer = plasma_importer.PlasmaImporter()
32 self.initialized = False
34 def init(self):
35 #print("Script Name: " + str(self.applet().name()))
36 #print("Script Category: " + str(self.applet().category()))
38 # applet() cannot be relied on to give the right details in the destructor,
39 # so the plugin name is stored aside. (n.b module.__name__ cannot be relied
40 # on either; it might have been changed in the module itself)
41 self.m_moduleName = str(self.applet().pluginName())
42 #print("pluginname: " + str(self.applet().pluginName()))
44 self.plugin_name = str(self.applet().pluginName()).replace('-','_')
46 PythonAppletScript.importer.register_top_level(self.plugin_name, str(self.applet().package().path()))
48 #print("mainScript: " + str(self.mainScript()))
49 #print("package path: " + str(self.applet().package().path()))
51 # import the code at the file name reported by mainScript()
52 self.module = __import__(self.plugin_name+'.main')
53 self.pyapplet = self.module.main.CreateApplet(None)
54 self.pyapplet.setApplet(self.applet())
55 self.pyapplet.init()
57 self._setUpEventHandlers()
58 self.initialized = True
59 return True
61 def __dtor__(self):
62 #print("~PythonAppletScript()")
63 PythonAppletScript.importer.unregister_top_level(self.plugin_name)
64 self.pyapplet = None
66 def constraintsEvent(self, constraints):
67 if not self.initialized:
68 return
69 self.pyapplet.constraintsEvent(constraints)
71 def showConfigurationInterface(self):
72 if not self.initialized:
73 return
74 #print("Script: showConfigurationInterface")
75 self.pyapplet.showConfigurationInterface()
77 def paintInterface(self, painter, option, contentsRect):
78 if not self.initialized:
79 return
80 self.pyapplet.paintInterface(painter, option, contentsRect)
82 def contextualActions(self):
83 if not self.initialized:
84 return
86 #print("pythonapplet contextualActions()")
87 return self.pyapplet.contextualActions()
89 def shape(self):
90 if not self.initialized:
91 return
92 return self.pyapplet.shape()
94 def eventFilter(self, obj, event):
95 handler = self.event_handlers.get(event.type(),None)
96 if handler is not None:
97 apply(getattr(self.pyapplet,handler), (event,) )
98 return True
99 else:
100 return False
102 def _setUpEventHandlers(self):
103 self.event_handlers = {}
105 self.pyapplet._disableForwardToApplet()
106 for event_type,handler in {
107 QEvent.GraphicsSceneMousePress: "mousePressEvent",
108 QEvent.GraphicsSceneContextMenu: "contextMenuEvent",
109 QEvent.GraphicsSceneDragEnter: "dragEnterEvent",
110 QEvent.GraphicsSceneDragLeave: "dragLeaveEvent",
111 QEvent.GraphicsSceneDragMove: "dragMoveEvent",
112 QEvent.GraphicsSceneDrop: "dropEvent",
113 QEvent.FocusIn: "focusInEvent",
114 QEvent.FocusOut: "focusOutEvent",
115 QEvent.GraphicsSceneHoverEnter: "hoverEnterEvent",
116 QEvent.GraphicsSceneHoverLeave: "hoverLeaveEvent",
117 QEvent.GraphicsSceneHoverMove: "hoverMoveEvent",
118 QEvent.InputMethod: "inputMethodEvent",
119 QEvent.KeyPress: "keyPressEvent",
120 QEvent.KeyRelease: "keyReleaseEvent",
121 QEvent.GraphicsSceneMouseDoubleClick: "mouseDoubleClickEvent",
122 QEvent.GraphicsSceneMouseMove: "mouseMoveEvent",
123 QEvent.GraphicsSceneMousePress: "mousePressEvent",
124 QEvent.GraphicsSceneMouseRelease: "mouseReleaseEvent",
125 QEvent.GraphicsSceneWheel: "wheelEvent"
126 }.iteritems():
127 if hasattr(self.pyapplet,handler):
128 self.event_handlers[event_type] = handler
129 self.pyapplet._enableForwardToApplet()
131 if self.event_handlers:
132 self.applet().installEventFilter(self)
134 def CreatePlugin(widget_parent, parent, component_data):
135 return PythonAppletScript(parent)