[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-panel-devel
Subject:    Using PopupApplets with script engines without subclassing them
From:       Richard Dale <richard.j.dale () gmail ! com>
Date:       2009-05-01 15:00:20
Message-ID: 491684420905010800q39365d0fk352d415f50892042 () mail ! gmail ! com
[Download RAW message or body]

I recently added a PopupApplet service that allows applet scripting
engines to use Plasma::PopupApplets. However, the C++ api assumes that
the applet can be sub-classed, and that isn't possible with the
scripting engine api.

To use an ExtenderItem with an Applet you need to be able to override
Applet::initExtenderItem() method. In addition the PopApplet class has
two virtual methods that need to be overridden, widget() and
graphicsWidget(), if you want to associate a QWidget or
QGraphicsWidget with the applet:

    /**
     * Implement either this function or graphicsWidget().
     * @return the widget that will get shown in either a layout, in
the applet or in a Dialog,
     * depending on the form factor of the applet.
     */
    virtual QWidget *widget();

    /**
     * Implement either this function or widget().
     * @return the widget that will get shown in either a layout, in
the applet or in a Dialog,
     * depending on the form factor of the applet.
     */
    virtual QGraphicsWidget *graphicsWidget();

The default implementation of graphicsWidget() returns any Extender
that is associated with the popup:

QGraphicsWidget *PopupApplet::graphicsWidget()
{
    return static_cast<Applet*>(this)->d->extender;
}

So if you create an Extender in a scripting language with your applet
as the parent, you can customize the behaviour of graphicsWidget()
because it will return your Extender.

So I've been thinking about what is the minimum change to allow
scripting applets to use Extenders and avoid any BIC problems. The
attached patch adds a setWidget() method to Plasma::PopupApplet, along
with an instance variable in the d pointer to hold the value. Existing
implementations of PopupApplet::widget() should carry on working find.

To handle initExtenderItem() I've added a initScriptExtenderItem()
signal to Plasma::Applet which is emitted by
Applet::initExtenderItem() when the applet has an associated script.
It is up to the script engine to connect to the signal and do
something appropriate. It wasn't posssible to add an
initExtenderItem() virtual method to appletscripting.h as I assume
that would be BIC.

The case where is QGraphicsItem which isn't an ExtenderItem, is
returned by PopupApplet::graphicsWidget() isn't handled. Should it be,
and if so could it be added to the PopupApplet d pointer, with a
setGraphicsWidget() method to set it. Or is it a rare use case that
isn't really needed for scripting applets? In fact are Extenders a
rare use case not needed for scripting, and the patch is a bit
redundant?

-- Richard

["popupapplet_scripting.patch" (text/x-diff)]

Index: popupapplet.h
===================================================================
--- popupapplet.h	(revision 961828)
+++ popupapplet.h	(working copy)
@@ -63,6 +63,8 @@
      */
     void setPopupIcon(const QString &iconName);
 
+    void setWidget(QWidget * widget);
+
     /**
      * @return the icon that is displayed when the applet is in a panel.
      */
Index: applet.h
===================================================================
--- applet.h	(revision 961828)
+++ applet.h	(working copy)
@@ -673,7 +673,12 @@
          */
         void appletDestroyed(Plasma::Applet *applet);
 
+        /**
+         * Emitted when an ExtenderItem in a scripting applet needs to be initialized
+         */
+        void initScriptExtenderItem(Plasma::ExtenderItem *item);
 
+
     public Q_SLOTS:
         /**
          * Sets the immutability type for this applet (not immutable,
Index: popupapplet.cpp
===================================================================
--- popupapplet.cpp	(revision 961828)
+++ popupapplet.cpp	(working copy)
@@ -110,9 +110,14 @@
     return d->icon ? d->icon->icon() : QIcon();
 }
 
+void PopupApplet::setWidget(QWidget * widget)
+{
+    d->widget = widget;
+}
+
 QWidget *PopupApplet::widget()
 {
-    return 0;
+    return d->widget;
 }
 
 QGraphicsWidget *PopupApplet::graphicsWidget()
@@ -479,6 +484,7 @@
           icon(0),
           dialog(0),
           proxy(0),
+          widget(0),
           popupPlacement(Plasma::FloatingPopup),
           savedAspectRatio(Plasma::InvalidAspectRatioMode),
           timer(0),
Index: applet.cpp
===================================================================
--- applet.cpp	(revision 961828)
+++ applet.cpp	(working copy)
@@ -649,11 +649,15 @@
 
 void Applet::initExtenderItem(ExtenderItem *item)
 {
-    kWarning() << "Missing implementation of initExtenderItem in the applet "
-               << item->config().readEntry("SourceAppletPluginName", "")
-               << "!\n Any applet that uses extenders should implement initExtenderItem to "
-               << "instantiate a widget. Destroying the item...";
-    item->destroy();
+    if (d->script) {
+        emit initScriptExtenderItem(item);
+    } else {
+        kWarning() << "Missing implementation of initExtenderItem in the applet "
+                   << item->config().readEntry("SourceAppletPluginName", "")
+                   << "!\n Any applet that uses extenders should implement initExtenderItem to "
+                   << "instantiate a widget. Destroying the item...";
+        item->destroy();
+    }
 }
 
 Extender *Applet::extender() const
Index: private/popupapplet_p.h
===================================================================
--- private/popupapplet_p.h	(revision 961828)
+++ private/popupapplet_p.h	(working copy)
@@ -43,6 +43,7 @@
     Plasma::IconWidget *icon;
     QPointer<Plasma::Dialog> dialog;
     QGraphicsProxyWidget *proxy;
+    QWidget *widget;
     Plasma::PopupPlacement popupPlacement;
     Plasma::AspectRatioMode savedAspectRatio;
     QTimer *timer;


_______________________________________________
Plasma-devel mailing list
Plasma-devel@kde.org
https://mail.kde.org/mailman/listinfo/plasma-devel


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic