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.

svn path=/trunk/KDE/kdebase/workspace/libs/plasma/; revision=777705
This commit is contained in:
Marco Martin 2008-02-21 10:26:40 +00:00
parent 74e2ce31ff
commit b1a362b67f
2 changed files with 97 additions and 10 deletions

View File

@ -33,6 +33,7 @@
#include <QTextDocument> #include <QTextDocument>
#include <QTimer> #include <QTimer>
#include <QUiLoader> #include <QUiLoader>
#include <QGraphicsSceneMouseEvent>
#include <KIcon> #include <KIcon>
#include <KColorScheme> #include <KColorScheme>
@ -284,6 +285,7 @@ public:
KPluginInfo appletDescription; KPluginInfo appletDescription;
Package* package; Package* package;
QList<QObject*> watchedForFocus; QList<QObject*> watchedForFocus;
QList<QGraphicsItem*> watchedForMouseMove;
QStringList loadedEngines; QStringList loadedEngines;
Plasma::SvgPanel *background; Plasma::SvgPanel *background;
Plasma::LineEdit *failureText; Plasma::LineEdit *failureText;
@ -1104,19 +1106,37 @@ QString Applet::instanceName() const
void Applet::watchForFocus(QObject *widget, bool watch) void Applet::watchForFocus(QObject *widget, bool watch)
{ {
if ( !widget ) { if (!widget) {
return; return;
} }
int index = d->watchedForFocus.indexOf(widget); int index = d->watchedForFocus.indexOf(widget);
if ( watch ) { if (watch) {
if ( index == -1 ) { if (index == -1) {
d->watchedForFocus.append( widget ); d->watchedForFocus.append(widget);
widget->installEventFilter( this ); widget->installEventFilter(this);
} }
} else if ( index != -1 ) { } else if (index != -1) {
d->watchedForFocus.removeAt( index ); d->watchedForFocus.removeAt(index);
widget->removeEventFilter( this ); 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);
} }
} }
@ -1154,6 +1174,51 @@ bool Applet::eventFilter( QObject *o, QEvent * e )
return QObject::eventFilter(o, e); 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() void Applet::showConfigurationInterface()
{ {
if (d->package && d->configXml) { if (d->package && d->configXml) {

View File

@ -722,12 +722,24 @@ class PLASMA_EXPORT Applet : public Widget
/** /**
* Register widgets that can receive keyboard focus. * 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 widget the widget to watch for keyboard focus
* @param watch whether to start watching the widget, or to stop doing so * @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. * Call this whenever focus is needed or not needed.
@ -745,6 +757,16 @@ class PLASMA_EXPORT Applet : public Widget
**/ **/
bool eventFilter( QObject *o, QEvent *e ); 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 * Reimpmlemented from LayoutItem
*/ */