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
):
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
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())
57 self
._setUpEventHandlers
()
58 self
.initialized
= True
62 #print("~PythonAppletScript()")
63 PythonAppletScript
.importer
.unregister_top_level(self
.plugin_name
)
66 def constraintsEvent(self
, constraints
):
67 if not self
.initialized
:
69 self
.pyapplet
.constraintsEvent(constraints
)
71 def showConfigurationInterface(self
):
72 if not self
.initialized
:
74 #print("Script: showConfigurationInterface")
75 self
.pyapplet
.showConfigurationInterface()
77 def paintInterface(self
, painter
, option
, contentsRect
):
78 if not self
.initialized
:
80 self
.pyapplet
.paintInterface(painter
, option
, contentsRect
)
82 def contextualActions(self
):
83 if not self
.initialized
:
86 #print("pythonapplet contextualActions()")
87 return self
.pyapplet
.contextualActions()
90 if not self
.initialized
:
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
,) )
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"
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
)