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

List:       kde-commits
Subject:    KDE/kdebase/workspace
From:       Marco Martin <notmart () gmail ! com>
Date:       2008-02-21 10:26:40
Message-ID: 1203589600.841444.32076.nullmailer () svn ! kde ! org
[Download RAW message or body]

SVN commit 777705 by mart:

transplant of sceneEventFilter and mouseMoveEvent from the icon applet to the applet \
class, this will cause a lot less duplication. if you want to make a widget draggable \
around just call the new function  void watchForMouseMove(QGraphicsItem * watched, \
bool watch)

also, some style fixes.

 M  +73 -8     libs/plasma/applet.cpp  
 M  +24 -2     libs/plasma/applet.h  
 M  +1 -45     plasma/applets/icon/icon.cpp  
 M  +0 -2      plasma/applets/icon/icon.h  


--- trunk/KDE/kdebase/workspace/libs/plasma/applet.cpp #777704:777705
@@ -33,6 +33,7 @@
 #include <QTextDocument>
 #include <QTimer>
 #include <QUiLoader>
+#include <QGraphicsSceneMouseEvent>
 
 #include <KIcon>
 #include <KColorScheme>
@@ -284,6 +285,7 @@
     KPluginInfo appletDescription;
     Package* package;
     QList<QObject*> watchedForFocus;
+    QList<QGraphicsItem*> watchedForMouseMove;
     QStringList loadedEngines;
     Plasma::SvgPanel *background;
     Plasma::LineEdit *failureText;
@@ -1104,22 +1106,40 @@
 
 void Applet::watchForFocus(QObject *widget, bool watch)
 {
-    if ( !widget ) {
+    if (!widget) {
         return;
     }
 
     int index = d->watchedForFocus.indexOf(widget);
-    if ( watch ) {
-        if ( index == -1 ) {
-            d->watchedForFocus.append( widget );
-            widget->installEventFilter( this );
+    if (watch) {
+        if (index == -1) {
+            d->watchedForFocus.append(widget);
+            widget->installEventFilter(this);
         }
-    } else if ( index != -1 ) {
-        d->watchedForFocus.removeAt( index );
-        widget->removeEventFilter( this );
+    } else if (index != -1) {
+        d->watchedForFocus.removeAt(index);
+        widget->removeEventFilter(this);
     }
 }
 
+void Applet::watchForMouseMove( QGraphicsItem * watched, bool watch )
+{
+    if (!watched) {
+        return;
+    }
+
+    int index = d->watchedForMouseMove.indexOf(watched);
+    if (watch) {
+        if (index == -1) {
+            d->watchedForMouseMove.append(watched);
+            watched->installSceneEventFilter(this);
+        }
+    } else if (index != -1) {
+        d->watchedForMouseMove.removeAt(index);
+        watched->removeSceneEventFilter(this);
+    }
+}
+
 void Applet::needsFocus(bool focus)
 {
     if (focus == QGraphicsItem::hasFocus()) {
@@ -1154,6 +1174,51 @@
     return QObject::eventFilter(o, e);
 }
 
+bool Applet::sceneEventFilter( QGraphicsItem * watched, QEvent * event )
+{
+    switch (event->type()) {
+        case QEvent::GraphicsSceneMouseMove: {
+            if (d->watchedForMouseMove.contains( watched )) {
+                mouseMoveEvent(static_cast<QGraphicsSceneMouseEvent*>(event));
+                return true;
+            }
+            break;
+        }
+
+        default:
+            break;
+    }
+
+    return QGraphicsItem::sceneEventFilter(watched, event);
+}
+
+void Applet::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
+{
+    if (!isImmutable() && formFactor() == Plasma::Planar) {
+        QGraphicsItem *parent = parentItem();
+        Plasma::Applet *applet = qgraphicsitem_cast<Plasma::Applet*>(parent);
+
+        if (applet && applet->isContainment()) {
+            // our direct parent is a containment. just move ourselves.
+            QPointF curPos = event->pos();
+            QPointF lastPos = event->lastPos();
+            QPointF delta = curPos-lastPos;
+
+            moveBy(delta.x(),delta.y());
+        } else if (parent) {
+            //don't move the icon as well because our parent (usually an \
appletHandle) will do it for us +            //parent->moveBy(delta.x(),delta.y());
+            QPointF curPos = parent->transform().map(event->pos());
+            QPointF lastPos = parent->transform().map(event->lastPos());
+            QPointF delta = curPos-lastPos;
+
+            parent->setPos(parent->pos() + delta);
+        }
+
+    }
+}
+
+
 void Applet::showConfigurationInterface()
 {
     if (d->package && d->configXml) {
--- trunk/KDE/kdebase/workspace/libs/plasma/applet.h #777704:777705
@@ -722,14 +722,26 @@
         /**
         * Register widgets that can receive keyboard focus.
         *
-        * Calling this results in an eventFilter being places on the widget.
+        * Calling this results in an eventFilter being placed on the widget.
         *
         * @param widget the widget to watch for keyboard focus
         * @param watch whether to start watching the widget, or to stop doing so
         */
-        void watchForFocus( QObject *widget, bool watch = true );
+        void watchForFocus(QObject *widget, bool watch = true);
 
         /**
+         * Register the widgets that manages mouse clicks but you still want
+         * to be able to drag the applet around when holding the mouse pointer
+         * on that widgets.
+         *
+         * Calling this results in an eventFilter being places on the widget.
+         *
+         * @param widget the widget to watch for mouse move
+         * @param watch whether to start watching the widget, or to stop doing so
+         */
+        void watchForMouseMove(QGraphicsItem * watched, bool watch);
+
+        /**
         * Call this whenever focus is needed or not needed.
         *
         * You do not have to call this method for widgets that have
@@ -746,6 +758,16 @@
         bool eventFilter( QObject *o, QEvent *e );
 
         /**
+         * @internal scene event filter; used to manage applet dragging
+         */
+        bool sceneEventFilter ( QGraphicsItem * watched, QEvent * event );
+
+        /**
+         * @internal manage the mouse movement to drag the applet around
+         */
+        void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
+
+        /**
          * Reimpmlemented from LayoutItem
          */
         QPointF topLeft() const;
--- trunk/KDE/kdebase/workspace/plasma/applets/icon/icon.cpp #777704:777705
@@ -64,7 +64,7 @@
     setUrl(cg.readEntry("Url", m_url));
     setDisplayLines(2);
 
-    m_icon->installSceneEventFilter(this);
+    watchForMouseMove(m_icon, true);
     m_icon->resize(contentSize());
 
     // we do this right away since we may have our config
@@ -299,50 +299,6 @@
     return m_icon->shape();
 }
 
-bool IconApplet::sceneEventFilter( QGraphicsItem * watched, QEvent * event )
-{
-    switch (event->type()) {
-        case QEvent::GraphicsSceneMouseMove: {
-            mouseMoveEvent(static_cast<QGraphicsSceneMouseEvent*>(event));
-            break;
-        }
-
-        default:
-            break;
-    }
-
-    return QGraphicsItem::sceneEventFilter(watched, event);
-}
-
-void IconApplet::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
-{
-    if (!isImmutable() && formFactor() == Plasma::Planar) {
-        QGraphicsItem *parent = parentItem();
-        Plasma::Applet *applet = qgraphicsitem_cast<Plasma::Applet*>(parent);
-
-        if (applet && applet->isContainment()) {
-            // our direct parent is a containment. just move ourselves.
-            QPointF curPos = event->pos();
-            QPointF lastPos = event->lastPos();
-            QPointF delta = curPos-lastPos;
-
-            moveBy(delta.x(),delta.y());
-        } else if (parent) {
-            //don't move the icon as well because our parent (usually an \
                appletHandle) will do it for us
-            //parent->moveBy(delta.x(),delta.y());
-            QPointF curPos = parent->transform().map(event->pos());
-            QPointF lastPos = parent->transform().map(event->lastPos());
-            QPointF delta = curPos-lastPos;
-
-            parent->setPos(parent->pos() + delta);
-        }
-
-        // We don't want any events on mouse release
-        m_icon->setUnpressed();
-    }
-}
-
-
 //dropUrls from DolphinDropController
 void IconApplet::dropUrls(const KUrl::List& urls,
                           const KUrl& destination,
--- trunk/KDE/kdebase/workspace/plasma/applets/icon/icon.h #777704:777705
@@ -59,8 +59,6 @@
     protected:
         void dropEvent(QGraphicsSceneDragDropEvent *event);
         void saveState(KConfigGroup *cg) const;
-        bool sceneEventFilter ( QGraphicsItem * watched, QEvent * event );
-        void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
 
     protected slots:
         void acceptedPropertiesDialog();


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

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