2 * Copyright 2008 by Richard Dale <richard.j.dale@gmail.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.
20 require 'plasma_applet'
22 module PlasmaScripting
23 class Applet < Qt::Object
24 slots "setImmutability(Plasma::ImmutabilityType)",
26 :showConfigurationInterface,
29 :flushPendingConstraintsEvents,
32 signals :releaseVisualFocus,
37 attr_accessor :applet_script
39 def initialize(parent, args = nil)
41 @applet_script = parent
42 connect(@applet_script.applet, SIGNAL(:releaseVisualFocus), self, SIGNAL(:releaseVisualFocus))
43 connect(@applet_script.applet, SIGNAL(:geometryChanged), self, SIGNAL(:geometryChanged))
44 connect(@applet_script.applet, SIGNAL(:configNeedsSaving), self, SIGNAL(:configNeedsSaving))
45 connect(@applet_script.applet, SIGNAL(:activate), self, SIGNAL(:activate))
48 # If a method is called on a PlasmaScripting::Applet instance is found to be missing
49 # then try calling the method on the underlying Plasma::Applet in the ScriptEngine.
50 def method_missing(method, *args)
54 applet_script.applet.method_missing(method, *args)
58 def self.const_missing(name)
62 Plasma::Applet.const_missing(name)
66 def paintInterface(painter, option, contentsRect)
77 def constraintsEvent(constraints)
84 def createConfigurationInterface(dialog)
87 def showConfigurationInterface
88 dialogId = "#{@applet_script.applet.id}settings#{@applet_script.applet.name}"
89 windowTitle = KDE::i18nc("@title:window", "%s Settings" % @applet_script.applet.name)
90 @nullManager = KDE::ConfigSkeleton.new(nil)
91 @dialog = KDE::ConfigDialog.new(nil, dialogId, @nullManager)
92 @dialog.faceType = KDE::PageDialog::Auto
93 @dialog.windowTitle = windowTitle
94 @dialog.setAttribute(Qt::WA_DeleteOnClose, true)
95 createConfigurationInterface(@dialog)
96 # TODO: would be nice to not show dialog if there are no pages added?
97 # Don't connect to the deleteLater() slot in Ruby as it causes crashes
98 # connect(@dialog, SIGNAL(:finished), @nullManager, SLOT(:deleteLater))
99 # TODO: Apply button does not correctly work for now, so do not show it
100 @dialog.showButton(KDE::Dialog::Apply, false)
104 def dataEngine(engine)
105 @applet_script.dataEngine(engine)
109 @applet_script.package
112 def setImmutability(immutabilityType)
113 @applet_script.applet.setImmutability(immutabilityType)
116 def immutability=(immutabilityType)
117 setImmutability(immutabilityType)
121 @applet_script.applet.destroy
125 @applet_script.applet.raise
129 @applet_script.applet.lower
132 def flushPendingConstraintsEvents
133 @applet_script.applet.flushPendingConstraintsEvents
138 module PlasmaScriptengineRuby
139 class Applet < Plasma::AppletScript
140 def initialize(parent, args)
145 str.gsub(/(^|[_-])(.)/) { $2.upcase }
149 oldSize = applet.size
151 puts "RubyAppletScript::Applet#init mainScript: #{mainScript}"
152 program = Qt::FileInfo.new(mainScript)
154 load Qt::File.encodeName(program.filePath).to_s
155 moduleName = camelize(Qt::Dir.new(package.path).dirName)
156 className = camelize(program.baseName)
157 puts "RubyAppletScript::Applet#init instantiating: #{moduleName}::#{className}"
158 klass = Object.const_get(moduleName.to_sym).const_get(className.to_sym)
159 @applet_script = klass.new(self)
161 if oldSize.height > 10 && oldSize.width > 10
162 applet.resize(oldSize)
165 set_up_event_handlers
169 def paintInterface(painter, option, contentsRect)
170 @applet_script.paintInterface(painter, option, contentsRect)
173 def constraintsEvent(constraints)
174 @applet_script.constraintsEvent(constraints)
177 def contextualActions
178 @applet_script.contextualActions
181 def showConfigurationInterface
182 @applet_script.showConfigurationInterface
187 def eventFilter(obj, event)
188 handler = @event_handlers[event.type.to_i]
190 @applet_script.send(handler, event)
199 def set_up_event_handlers
202 if @applet_script.respond_to?(:mousePressEvent)
203 @event_handlers[Qt::Event::GraphicsSceneMousePress.to_i] = :mousePressEvent
206 if @applet_script.respond_to?(:contextMenuEvent)
207 @event_handlers[Qt::Event::GraphicsSceneContextMenu.to_i] = :contextMenuEvent
210 if @applet_script.respond_to?(:dragEnterEvent)
211 @event_handlers[Qt::Event::GraphicsSceneDragEnter.to_i] = :dragEnterEvent
214 if @applet_script.respond_to?(:dragLeaveEvent)
215 @event_handlers[Qt::Event::GraphicsSceneDragLeave.to_i] = :dragLeaveEvent
218 if @applet_script.respond_to?(:dragMoveEvent)
219 @event_handlers[Qt::Event::GraphicsSceneDragMove.to_i] = :dragMoveEvent
222 if @applet_script.respond_to?(:dropEvent)
223 @event_handlers[Qt::Event::GraphicsSceneDrop.to_i] = :dropEvent
226 if @applet_script.respond_to?(:focusInEvent)
227 @event_handlers[Qt::Event::FocusIn.to_i] = :focusInEvent
230 if @applet_script.respond_to?(:focusOutEvent)
231 @event_handlers[Qt::Event::FocusOut.to_i] = :focusOutEvent
234 if @applet_script.respond_to?(:hoverEnterEvent)
235 @event_handlers[Qt::Event::GraphicsSceneHoverEnter.to_i] = :hoverEnterEvent
238 if @applet_script.respond_to?(:hoverLeaveEvent)
239 @event_handlers[Qt::Event::GraphicsSceneHoverLeave.to_i] = :hoverLeaveEvent
242 if @applet_script.respond_to?(:hoverMoveEvent)
243 @event_handlers[Qt::Event::GraphicsSceneHoverMove.to_i] = :hoverMoveEvent
246 if @applet_script.respond_to?(:inputMethodEvent)
247 @event_handlers[Qt::Event::InputMethod.to_i] = :inputMethodEvent
250 if @applet_script.respond_to?(:keyPressEvent)
251 @event_handlers[Qt::Event::KeyPress.to_i] = :keyPressEvent
254 if @applet_script.respond_to?(:keyReleaseEvent)
255 @event_handlers[Qt::Event::KeyRelease.to_i] = :keyReleaseEvent
258 if @applet_script.respond_to?(:mouseDoubleClickEvent)
259 @event_handlers[Qt::Event::GraphicsSceneMouseDoubleClick.to_i] = :mouseDoubleClickEvent
262 if @applet_script.respond_to?(:mouseMoveEvent)
263 @event_handlers[Qt::Event::GraphicsSceneMouseMove.to_i] = :mouseMoveEvent
266 if @applet_script.respond_to?(:mousePressEvent)
267 @event_handlers[Qt::Event::GraphicsSceneMousePress.to_i] = :mousePressEvent
270 if @applet_script.respond_to?(:mouseReleaseEvent)
271 @event_handlers[Qt::Event::GraphicsSceneMouseRelease.to_i] = :mouseReleaseEvent
274 if @applet_script.respond_to?(:wheelEvent)
275 @event_handlers[Qt::Event::GraphicsSceneWheel.to_i] = :wheelEvent
278 if !@event_handlers.empty?
279 applet.installEventFilter(self)
288 # Because a PlasmaScript::Applet is not actually a Plasma::Applet we
289 # need to 'cheat' in the api, to pretend that it is. So the constructors
290 # in the Plasma widget classes will substitute any PlasmaScript::Applet
291 # argument passed for the real Plasma::Applet in the ScriptEngine
294 class BusyWidget < Qt::Base
295 def initialize(parent = nil)
296 if parent.kind_of?(PlasmaScripting::Applet)
297 super(parent.applet_script.applet)
304 class CheckBox < Qt::Base
305 def initialize(parent = nil)
306 if parent.kind_of?(PlasmaScripting::Applet)
307 super(parent.applet_script.applet)
314 class ComboBox < Qt::Base
315 def initialize(parent = nil)
316 if parent.kind_of?(PlasmaScripting::Applet)
317 super(parent.applet_script.applet)
324 class Extender < Qt::Base
325 def initialize(parent = nil)
326 if parent.kind_of?(PlasmaScripting::Applet)
327 super(parent.applet_script.applet)
334 class FlashingLabel < Qt::Base
335 def initialize(parent = nil)
336 if parent.kind_of?(PlasmaScripting::Applet)
337 super(parent.applet_script.applet)
344 class Frame < Qt::Base
345 def initialize(parent = nil)
346 if parent.kind_of?(PlasmaScripting::Applet)
347 super(parent.applet_script.applet)
354 class GroupBox < Qt::Base
355 def initialize(parent = nil)
356 if parent.kind_of?(PlasmaScripting::Applet)
357 super(parent.applet_script.applet)
364 class IconWidget < Qt::Base
365 def initialize(*args)
367 for i in 0...args.length do
368 if args[i].kind_of?(PlasmaScripting::Applet)
369 sargs << args[i].applet_script.applet
378 class Label < Qt::Base
379 def initialize(parent = nil)
380 if parent.kind_of?(PlasmaScripting::Applet)
381 super(parent.applet_script.applet)
388 class LineEdit < Qt::Base
389 def initialize(parent = nil)
390 if parent.kind_of?(PlasmaScripting::Applet)
391 super(parent.applet_script.applet)
398 class Meter < Qt::Base
399 def initialize(parent = nil)
400 if parent.kind_of?(PlasmaScripting::Applet)
401 super(parent.applet_script.applet)
408 class PushButton < Qt::Base
409 def initialize(parent = nil)
410 if parent.kind_of?(PlasmaScripting::Applet)
411 super(parent.applet_script.applet)
418 class RadioButton < Qt::Base
419 def initialize(parent = nil)
420 if parent.kind_of?(PlasmaScripting::Applet)
421 super(parent.applet_script.applet)
428 class ScrollBar < Qt::Base
429 def initialize(orientation, parent = nil)
430 if parent.kind_of?(PlasmaScripting::Applet)
431 super(orientation, parent.applet_script.applet)
438 class SignalPlotter < Qt::Base
439 def initialize(parent = nil)
440 if parent.kind_of?(PlasmaScripting::Applet)
441 super(parent.applet_script.applet)
448 class Slider < Qt::Base
449 def initialize(parent = nil)
450 if parent.kind_of?(PlasmaScripting::Applet)
451 super(parent.applet_script.applet)
458 class SvgWidget < Qt::Base
459 def initialize(*args)
460 if args.length > 0 && args[0].kind_of?(PlasmaScripting::Applet)
461 args[0] = args[0].applet_script.applet
463 elsif args.length > 2 && args[2].kind_of?(PlasmaScripting::Applet)
464 args[2] = args[2].applet_script.applet
472 class TabBar < Qt::Base
473 def initialize(parent = nil)
474 if parent.kind_of?(PlasmaScripting::Applet)
475 super(parent.applet_script.applet)
482 class TextEdit < Qt::Base
483 def initialize(parent = nil)
484 if parent.kind_of?(PlasmaScripting::Applet)
485 super(parent.applet_script.applet)
492 class TreeView < Qt::Base
493 def initialize(parent = nil)
494 if parent.kind_of?(PlasmaScripting::Applet)
495 super(parent.applet_script.applet)
502 class WebView < Qt::Base
503 def initialize(parent = nil)
504 if parent.kind_of?(PlasmaScripting::Applet)
505 super(parent.applet_script.applet)
515 class GraphicsProxyWidget < Qt::Base
516 def initialize(parent = nil, wFlags = nil)
517 if parent.kind_of?(PlasmaScripting::Applet)
518 super(parent.applet_script.applet, wFlags)
524 def parentItem=(item)
528 def setParentItem(item)
529 if item.kind_of?(PlasmaScripting::Applet)
530 super(item.applet_script.applet)
537 method_missing(:type, *args)
541 class QAbstractGraphicsShapeItem < Qt::Base
542 def initialize(parent = nil, wFlags = nil)
543 if parent.kind_of?(PlasmaScripting::Applet)
544 super(parent.applet_script.applet, wFlags)
550 def parentItem=(item)
554 def setParentItem(item)
555 if item.kind_of?(PlasmaScripting::Applet)
556 super(item.applet_script.applet)
563 method_missing(:type, *args)
567 class GraphicsEllipseItem < Qt::Base
568 def initialize(parent = nil, wFlags = nil)
569 if parent.kind_of?(PlasmaScripting::Applet)
570 super(parent.applet_script.applet, wFlags)
576 def parentItem=(item)
580 def setParentItem(item)
581 if item.kind_of?(PlasmaScripting::Applet)
582 super(item.applet_script.applet)
589 method_missing(:type, *args)
593 class GraphicsItemGroup < Qt::Base
594 def initialize(parent = nil, wFlags = nil)
595 if parent.kind_of?(PlasmaScripting::Applet)
596 super(parent.applet_script.applet, wFlags)
602 def parentItem=(item)
606 def setParentItem(item)
607 if item.kind_of?(PlasmaScripting::Applet)
608 super(item.applet_script.applet)
615 method_missing(:type, *args)
619 class GraphicsLineItem < Qt::Base
620 def initialize(parent = nil, wFlags = nil)
621 if parent.kind_of?(PlasmaScripting::Applet)
622 super(parent.applet_script.applet, wFlags)
628 def parentItem=(item)
632 def setParentItem(item)
633 if item.kind_of?(PlasmaScripting::Applet)
634 super(item.applet_script.applet)
641 method_missing(:type, *args)
645 class GraphicsPathItem < Qt::Base
646 def initialize(parent = nil, wFlags = nil)
647 if parent.kind_of?(PlasmaScripting::Applet)
648 super(parent.applet_script.applet, wFlags)
654 def parentItem=(item)
658 def setParentItem(item)
659 if item.kind_of?(PlasmaScripting::Applet)
660 super(item.applet_script.applet)
667 method_missing(:type, *args)
671 class GraphicsPixmapItem < Qt::Base
672 def initialize(parent = nil, wFlags = nil)
673 if parent.kind_of?(PlasmaScripting::Applet)
674 super(parent.applet_script.applet, wFlags)
680 def parentItem=(item)
684 def setParentItem(item)
685 if item.kind_of?(PlasmaScripting::Applet)
686 super(item.applet_script.applet)
693 method_missing(:type, *args)
697 class GraphicsPolygonItem < Qt::Base
698 def initialize(parent = nil, wFlags = nil)
699 if parent.kind_of?(PlasmaScripting::Applet)
700 super(parent.applet_script.applet, wFlags)
706 def parentItem=(item)
710 def setParentItem(item)
711 if item.kind_of?(PlasmaScripting::Applet)
712 super(item.applet_script.applet)
719 method_missing(:type, *args)
723 class GraphicsRectItem < Qt::Base
724 def initialize(parent = nil, wFlags = nil)
725 if parent.kind_of?(PlasmaScripting::Applet)
726 super(parent.applet_script.applet, wFlags)
732 def parentItem=(item)
736 def setParentItem(item)
737 if item.kind_of?(PlasmaScripting::Applet)
738 super(item.applet_script.applet)
745 method_missing(:type, *args)
749 class GraphicsSimpleTextItem < Qt::Base
750 def initialize(parent = nil, wFlags = nil)
751 if parent.kind_of?(PlasmaScripting::Applet)
752 super(parent.applet_script.applet, wFlags)
758 def parentItem=(item)
762 def setParentItem(item)
763 if item.kind_of?(PlasmaScripting::Applet)
764 super(item.applet_script.applet)
771 method_missing(:type, *args)
775 class GraphicsSvgItem < Qt::Base
776 def initialize(parent = nil, wFlags = nil)
777 if parent.kind_of?(PlasmaScripting::Applet)
778 super(parent.applet_script.applet, wFlags)
784 def parentItem=(item)
788 def setParentItem(item)
789 if item.kind_of?(PlasmaScripting::Applet)
790 super(item.applet_script.applet)
797 method_missing(:type, *args)
801 class GraphicsTextItem < Qt::Base
802 def initialize(parent = nil, wFlags = nil)
803 if parent.kind_of?(PlasmaScripting::Applet)
804 super(parent.applet_script.applet, wFlags)
810 def parentItem=(item)
814 def setParentItem(item)
815 if item.kind_of?(PlasmaScripting::Applet)
816 super(item.applet_script.applet)
823 method_missing(:type, *args)
827 class GraphicsWidget < Qt::Base
828 def initialize(parent = nil, wFlags = nil)
829 if parent.kind_of?(PlasmaScripting::Applet)
830 super(parent.applet_script.applet, wFlags)
836 def parentItem=(item)
840 def setParentItem(item)
841 if item.kind_of?(PlasmaScripting::Applet)
842 super(item.applet_script.applet)
849 method_missing(:type, *args)
853 class GraphicsGridLayout < Qt::Base
854 def initialize(parent = nil)
855 if parent.kind_of?(PlasmaScripting::Applet)
856 super(parent.applet_script.applet)
864 for i in 0...args.length do
865 if args[i].kind_of?(PlasmaScripting::Applet)
866 sargs << args[i].applet_script.applet
875 if item.kind_of?(PlasmaScripting::Applet)
876 super(item.applet_script.applet)
882 def setAlignment(item, alignment)
883 if item.kind_of?(PlasmaScripting::Applet)
884 super(item.applet_script.applet, alignment)
891 class GraphicsLinearLayout < Qt::Base
892 def initialize(*args)
894 for i in 0...args.length do
895 if args[i].kind_of?(PlasmaScripting::Applet)
896 sargs << args[i].applet_script.applet
906 for i in 0...args.length do
907 if args[i].kind_of?(PlasmaScripting::Applet)
908 sargs << args[i].applet_script.applet
917 if item.kind_of?(PlasmaScripting::Applet)
918 super(item.applet_script.applet)
924 def insertItem(index, item)
925 if item.kind_of?(PlasmaScripting::Applet)
926 super(index, item.applet_script.applet)
932 def setAlignment(item, alignment)
933 if item.kind_of?(PlasmaScripting::Applet)
934 super(item.applet_script.applet, alignment)
940 def setStretchFactor(item, stretch)
941 if item.kind_of?(PlasmaScripting::Applet)
942 super(item.applet_script.applet, stretch)
948 def stretchFactor(item)
949 if item.kind_of?(PlasmaScripting::Applet)
950 super(item.applet_script.applet)
958 # kate: space-indent on; indent-width 2; replace-tabs on; mixed-indent off;