Add support for applet handles in plasma containments.

Still a couple of issues to flesh out, but that's usable already.

svn path=/trunk/KDE/kdebase/workspace/libs/plasma/; revision=735845
This commit is contained in:
Kevin Ottens 2007-11-12 19:27:21 +00:00
parent 27374262f5
commit bf77e378a6
7 changed files with 448 additions and 58 deletions

View File

@ -24,6 +24,7 @@ set(plasma_LIB_SRCS
appletbrowser/kcategorizeditemsviewmodels.cpp
appletbrowser/plasmaappletitemmodel.cpp
appletbrowser/customdragtreeview.cpp
applethandle.cpp
configxml.cpp
containment.cpp
corona.cpp

View File

@ -110,6 +110,7 @@ public:
void init(Applet* applet)
{
applet->setAcceptsHoverEvents(true);
applet->setZValue(100);
kioskImmutable = applet->globalConfig().isImmutable() ||
applet->config().isImmutable();
@ -574,8 +575,6 @@ bool Applet::isImmutable() const
void Applet::setImmutable(bool immutable)
{
d->immutable = immutable;
setFlag(QGraphicsItem::ItemIsMovable, d->immutable || d->kioskImmutable ||
!scene() || !static_cast<Corona*>(scene())->isImmutable());
}
bool Applet::drawStandardBackground()

314
applethandle.cpp Normal file
View File

@ -0,0 +1,314 @@
/*
* Copyright 2007 by Kevin Ottens <ervin@kde.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "applethandle_p.h"
#include <QtGui/QGraphicsSceneMouseEvent>
#include <QtGui/QPainter>
#include <KIcon>
#include <cmath>
#include "containment.h"
#include "applet.h"
namespace Plasma
{
AppletHandle::AppletHandle(Containment *parent, Applet *applet)
: QObject(), QGraphicsItem(parent),
m_buttonsOnRight(false), m_pressedButton(NoButton),
m_containment(parent), m_applet(applet),
m_svg("widgets/iconbutton"), m_opacity(0.0),
m_anim(FadeIn), m_animId(0), m_angle(0.0), m_scale(1.0)
{
m_originalMatrix = m_applet->transform();
m_rect = m_applet->boundingRect();
m_rect = m_applet->mapToParent(m_rect).boundingRect();
if (m_rect.height()<158.0) {
float delta = 158.0-m_rect.height();
delta = delta/2.0;
if (delta>0.0) {
m_rect.adjust(0.0, -delta, 0.0, delta);
}
}
m_rect.adjust(-20.0, -20.0, 20.0, 20.0);
if (m_applet->pos().x()<=60.0) {
m_rect.adjust(0.0, 0.0, 40.0, 0.0);
m_buttonsOnRight = true;
} else {
m_rect.adjust(-40.0, 0.0, 0.0, 0.0);
}
m_applet->setParentItem(this);
setAcceptsHoverEvents(true);
startFading(FadeIn);
}
AppletHandle::~AppletHandle()
{
QPointF center = m_applet->boundingRect().center();
// TODO: Keep the rotation only, to apply the scaling force
// a new pixel size to applets (avoid upscaling aliasing, and
// insane downscaling), probably requires support in the Applet class.
QTransform matrix = m_originalMatrix;
matrix.translate(center.x(), center.y());
matrix.rotateRadians(m_angle);
matrix.scale(m_scale, m_scale);
matrix.translate(-center.x(), -center.y());
QPointF newPos = transform().inverted().map(m_applet->pos());
m_applet->setParentItem(m_containment);
m_applet->setPos(mapToParent(newPos));
m_applet->setTransform(matrix);
}
Applet *AppletHandle::applet() const
{
return m_applet;
}
QRectF Plasma::AppletHandle::boundingRect() const
{
return m_rect;
}
void AppletHandle::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(option);
Q_UNUSED(widget);
painter->save();
painter->setOpacity(m_opacity);
m_svg.paint(painter, boundingRect(), "background");
m_svg.paint(painter, boundingRect(), "foreground-hover");
QPointF point = m_rect.topLeft();
if (m_buttonsOnRight) {
point+= QPointF(m_rect.width()-52.0, 20.0);
} else {
point+= QPointF(20.0, 20.0);
}
QPointF shiftM;
QPointF shiftC;
QPointF shiftD;
QPointF shiftR;
switch(m_pressedButton)
{
case MoveButton:
shiftM = QPointF(2, 2);
break;
case ConfigureButton:
shiftC = QPointF(2, 2);
break;
case RemoveButton:
shiftD = QPointF(2, 2);
break;
case RotateButton:
shiftR = QPointF(2, 2);
break;
default:
break;
}
painter->drawPixmap(point+shiftM, KIcon("exec").pixmap(32,32)); // FIXME: I'd like a "transform-move" here
point+=QPointF(0.0, 42.0);
if (m_applet->hasConfigurationInterface()) {
painter->drawPixmap(point+shiftC, KIcon("configure").pixmap(32,32));
}
point+=QPointF(0.0, 42.0);
painter->drawPixmap(point+shiftD, KIcon("edit-delete").pixmap(32,32));
point+=QPointF(0.0, 42.0);
painter->drawPixmap(point+shiftR, KIcon("transform-rotate").pixmap(32,32));
painter->restore();
}
AppletHandle::ButtonType AppletHandle::mapToButton(const QPointF &point) const
{
QPointF basePoint = m_rect.topLeft();
if (m_buttonsOnRight) {
basePoint+= QPointF(m_rect.width()-52.0, 20.0);
} else {
basePoint+= QPointF(20.0, 20.0);
}
QPolygonF activeArea = QPolygonF(QRectF(basePoint, QSizeF(32.0, 32.0)));
if (activeArea.containsPoint(point, Qt::OddEvenFill)) {
return MoveButton;
}
activeArea.translate(QPointF(0.0, 42.0));
if (activeArea.containsPoint(point, Qt::OddEvenFill)
&& m_applet->hasConfigurationInterface()) {
return ConfigureButton;
}
activeArea.translate(QPointF(0.0, 42.0));
if (activeArea.containsPoint(point, Qt::OddEvenFill)) {
return RemoveButton;
}
activeArea.translate(QPointF(0.0, 42.0));
if (activeArea.containsPoint(point, Qt::OddEvenFill)) {
return RotateButton;
}
return NoButton;
}
void AppletHandle::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
if (event->button()==Qt::LeftButton) {
m_pressedButton = mapToButton(event->pos());
update();
}
}
void AppletHandle::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
ButtonType releasedAtButton = mapToButton(event->pos());
if (event->button()==Qt::LeftButton && m_pressedButton==releasedAtButton) {
if (m_pressedButton==ConfigureButton) {
m_applet->showConfigurationInterface();
} else if (m_pressedButton==RemoveButton) {
Phase::self()->animateItem(m_applet, Phase::Disappear);
forceDisappear();
}
}
m_pressedButton = NoButton;
update();
}
qreal _k_distanceForPoint(QPointF point)
{
return ::sqrt(point.x()*point.x()+point.y()*point.y());
}
qreal _k_angleForPoints(const QPointF &center, const QPointF &pt1, const QPointF &pt2)
{
QPointF vec1 = pt1 - center;
QPointF vec2 = pt2 - center;
qreal alpha = ::atan2(vec1.y(), vec1.x());
qreal beta = ::atan2(vec2.y(), vec2.x());
return beta - alpha;
}
void AppletHandle::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
if (m_pressedButton==MoveButton) {
QPointF delta = event->pos()-event->lastPos();
setPos(pos()+delta);
} else if (m_pressedButton==RotateButton) {
if (_k_distanceForPoint(event->pos()-event->lastPos()) <= 1.0) {
return;
}
QPointF pressPos = mapFromScene(event->buttonDownScenePos(Qt::LeftButton));
QRectF rect = QRectF(m_applet->pos(), m_applet->size());
QPointF center = rect.center();
m_angle = _k_angleForPoints(center, pressPos, event->pos());
m_scale = _k_distanceForPoint(event->pos()-center) / _k_distanceForPoint(pressPos-center);
QTransform matrix;
matrix.translate(center.x(), center.y());
matrix.rotateRadians(m_angle);
matrix.scale(m_scale, m_scale);
matrix.translate(-center.x(), -center.y());
setTransform(matrix);
} else {
QGraphicsItem::mouseMoveEvent(event);
}
}
void AppletHandle::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
Q_UNUSED(event);
startFading(FadeIn);
}
void AppletHandle::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
Q_UNUSED(event);
startFading(FadeOut);
}
void AppletHandle::fadeAnimation(qreal progress)
{
qreal endOpacity = 1.0;
if (m_anim==FadeOut) {
endOpacity = 0.0;
}
m_opacity += (endOpacity-m_opacity)*progress;
if (progress>=1.0 && m_anim==FadeOut) {
emit disappearDone(this);
}
update();
}
void AppletHandle::startFading(FadeType anim)
{
if (m_animId!=0) {
Phase::self()->stopCustomAnimation(m_animId);
}
qreal time = 500;
if (anim==FadeIn) {
time *= 1.0-m_opacity;
} else {
time *= m_opacity;
}
m_anim = anim;
m_animId = Phase::self()->customAnimation(40, (int)time,
Phase::EaseInOutCurve,
this, "fadeAnimation");
}
void AppletHandle::forceDisappear()
{
setAcceptsHoverEvents(false);
startFading(FadeOut);
}
}
#include "applethandle_p.moc"

83
applethandle_p.h Normal file
View File

@ -0,0 +1,83 @@
/*
* Copyright 2007 by Kevin Ottens <ervin@kde.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef PLASMA_APPLETHANDLE
#define PLASMA_APPLETHANDLE
#include <QtCore/QObject>
#include <QtGui/QGraphicsItem>
#include "phase.h"
#include "svg.h"
namespace Plasma
{
class Applet;
class Containment;
class AppletHandle : public QObject, public QGraphicsItem
{
Q_OBJECT
public:
enum FadeType { FadeIn, FadeOut };
enum ButtonType { NoButton, MoveButton, RotateButton, ConfigureButton, RemoveButton };
AppletHandle(Containment *parent, Applet *applet);
virtual ~AppletHandle();
Applet *applet() const;
QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *event);
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
Q_SIGNALS:
void disappearDone(AppletHandle *self);
private Q_SLOTS:
void fadeAnimation(qreal progress);
private:
void startFading(FadeType anim);
void forceDisappear();
ButtonType mapToButton(const QPointF &point) const;
QRectF m_rect;
bool m_buttonsOnRight;
ButtonType m_pressedButton;
Containment *m_containment;
Applet *m_applet;
Svg m_svg;
qreal m_opacity;
FadeType m_anim;
Phase::AnimId m_animId;
qreal m_angle;
qreal m_scale;
QTransform m_originalMatrix;
};
}
#endif // multiple inclusion guard

View File

@ -36,6 +36,7 @@
#include <KServiceTypeTrader>
#include <KStandardDirs>
#include "applethandle_p.h"
#include "corona.h"
#include "phase.h"
#include "svg.h"
@ -68,6 +69,7 @@ public:
FormFactor formFactor;
Location location;
Applet::List applets;
QMap<Applet*, AppletHandle*> handles;
int screen;
bool immutable;
};
@ -102,7 +104,7 @@ void Containment::init()
//TODO: would be nice to not do this on init, as it causes Phase to init
connect(Phase::self(), SIGNAL(animationComplete(QGraphicsItem*,Plasma::Phase::Animation)),
this, SLOT(appletDisappearComplete(QGraphicsItem*,Plasma::Phase::Animation)));
this, SLOT(appletAnimationComplete(QGraphicsItem*,Plasma::Phase::Animation)));
}
void Containment::initConstraints(KConfigGroup* group)
@ -182,45 +184,10 @@ void Containment::contextMenuEvent(QGraphicsSceneContextMenuEvent* event)
foreach(QAction* action, actions) {
desktopMenu.addAction(action);
}
} else if (applet->isImmutable()) {
} else {
kDebug() << "immutable applet";
QGraphicsItem::contextMenuEvent(event);
return;
} else {
bool hasEntries = false;
if (applet->hasConfigurationInterface()) {
QAction* configureApplet = new QAction(i18n("%1 Settings...", applet->name()), &desktopMenu);
connect(configureApplet, SIGNAL(triggered(bool)),
applet, SLOT(showConfigurationInterface()));
desktopMenu.addAction(configureApplet);
hasEntries = true;
}
if (scene() && !static_cast<Corona*>(scene())->isImmutable()) {
QAction* closeApplet = new QAction(i18n("Remove this %1", applet->name()), &desktopMenu);
QVariant appletV;
appletV.setValue((QObject*)applet);
closeApplet->setData(appletV);
connect(closeApplet, SIGNAL(triggered(bool)),
this, SLOT(destroyApplet()));
desktopMenu.addAction(closeApplet);
hasEntries = true;
}
QList<QAction*> actions = applet->contextActions();
if (!actions.isEmpty()) {
desktopMenu.addSeparator();
foreach(QAction* action, actions) {
desktopMenu.addAction(action);
}
hasEntries = true;
}
if (!hasEntries) {
QGraphicsItem::contextMenuEvent(event);
kDebug() << "no entries";
return;
}
}
event->accept();
@ -375,19 +342,7 @@ void Containment::appletDestroyed(QObject* object)
}
}
void Containment::destroyApplet()
{
QAction *action = qobject_cast<QAction*>(sender());
if (!action) {
return;
}
Applet *applet = qobject_cast<Applet*>(action->data().value<QObject*>());
Phase::self()->animateItem(applet, Phase::Disappear);
}
void Containment::appletDisappearComplete(QGraphicsItem *item, Plasma::Phase::Animation anim)
void Containment::appletAnimationComplete(QGraphicsItem *item, Plasma::Phase::Animation anim)
{
if (anim == Phase::Disappear) {
if (item->parentItem() == this) {
@ -397,6 +352,10 @@ void Containment::appletDisappearComplete(QGraphicsItem *item, Plasma::Phase::An
applet->destroy();
}
}
} else if (anim == Phase::Appear) {
if (type()==DesktopContainment) {
item->installSceneEventFilter(this);
}
}
}
@ -550,6 +509,39 @@ void Containment::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
Q_UNUSED(event)
}
bool Containment::sceneEventFilter(QGraphicsItem *watched, QEvent *event)
{
Applet *applet = qgraphicsitem_cast<Applet*>(watched);
//QEvent::GraphicsSceneHoverEnter
// Otherwise we're watching something we shouldn't be...
Q_ASSERT(applet!=0);
Q_ASSERT(d->applets.contains(applet));
switch (event->type())
{
case QEvent::GraphicsSceneHoverEnter:
if (!d->immutable && !applet->isImmutable()
&& !d->handles.contains(applet)) {
AppletHandle *handle = new AppletHandle(this, applet);
d->handles[applet] = handle;
connect(handle, SIGNAL(disappearDone(AppletHandle*)),
this, SLOT(handleDisappeared(AppletHandle*)));
}
break;
default:
break;
}
return false;
}
void Containment::handleDisappeared(AppletHandle *handle)
{
d->handles.remove(handle->applet());
handle->deleteLater();
}
}
#include "containment.moc"

View File

@ -33,6 +33,7 @@
namespace Plasma
{
class AppletHandle;
class DataEngine;
class Package;
class Corona;
@ -226,16 +227,19 @@ class PLASMA_EXPORT Containment : public Applet
void contextMenuEvent(QGraphicsSceneContextMenuEvent * event);
void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
bool sceneEventFilter(QGraphicsItem *watched, QEvent *event);
protected Q_SLOTS:
/**
* @internal
*/
void appletDestroyed(QObject*);
void destroyApplet();
void appletDisappearComplete(QGraphicsItem *item, Plasma::Phase::Animation anim);
void appletAnimationComplete(QGraphicsItem *item, Plasma::Phase::Animation anim);
void dropEvent(QGraphicsSceneDragDropEvent* event);
private Q_SLOTS:
void handleDisappeared(AppletHandle *handle);
private:
Q_DISABLE_COPY(Containment)

View File

@ -415,9 +415,6 @@ void Corona::setImmutable(bool immutable)
}
d->immutable = immutable;
foreach (QGraphicsItem* item, items()) {
item->setFlag(QGraphicsItem::ItemIsMovable, immutable);
}
}
} // namespace Plasma