2007-11-19 02:19:12 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2007 by Aaron Seigo <aseigo@kde.org>
|
2008-04-14 23:09:15 +02:00
|
|
|
* Copyright 2008 by Marco Martin <notmart@gmail.com>
|
2007-11-19 02:19:12 +01:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2007-12-21 02:14:32 +01:00
|
|
|
#include "desktoptoolbox_p.h"
|
2007-11-19 02:19:12 +01:00
|
|
|
|
|
|
|
#include <QGraphicsSceneHoverEvent>
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QRadialGradient>
|
2008-04-28 21:41:43 +02:00
|
|
|
#include <QGraphicsView>
|
2007-11-19 02:19:12 +01:00
|
|
|
|
2008-01-07 05:33:44 +01:00
|
|
|
#include <plasma/theme.h>
|
|
|
|
#include <KColorScheme>
|
|
|
|
|
2007-12-05 21:22:25 +01:00
|
|
|
#include <KDebug>
|
|
|
|
|
2008-04-13 23:47:47 +02:00
|
|
|
#include <plasma/applet.h>
|
|
|
|
|
2007-11-19 02:19:12 +01:00
|
|
|
|
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
2008-01-10 22:05:56 +01:00
|
|
|
class EmptyGraphicsItem : public QGraphicsItem
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
EmptyGraphicsItem(QGraphicsItem *parent)
|
|
|
|
: QGraphicsItem(parent)
|
|
|
|
{
|
|
|
|
setAcceptsHoverEvents(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
QRectF boundingRect() const
|
|
|
|
{
|
|
|
|
return QRectF(QPointF(0, 0), m_rect.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
QRectF rect() const
|
|
|
|
{
|
|
|
|
return m_rect;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setRect(const QRectF &rect)
|
|
|
|
{
|
|
|
|
//kDebug() << "setting rect to" << rect;
|
|
|
|
prepareGeometryChange();
|
|
|
|
m_rect = rect;
|
|
|
|
setPos(rect.topLeft());
|
|
|
|
}
|
|
|
|
|
|
|
|
void paint(QPainter * p, const QStyleOptionGraphicsItem*, QWidget*)
|
|
|
|
{
|
|
|
|
Q_UNUSED(p)
|
|
|
|
//p->setPen(Qt::red);
|
|
|
|
//p->drawRect(boundingRect());
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
QRectF m_rect;
|
|
|
|
};
|
|
|
|
|
2007-12-05 21:22:25 +01:00
|
|
|
// used with QGrahphicsItem::setData
|
|
|
|
static const int ToolName = 7001;
|
|
|
|
|
2008-04-14 15:05:49 +02:00
|
|
|
class DesktopToolbox::Private
|
2007-11-19 02:19:12 +01:00
|
|
|
{
|
2008-04-14 15:05:49 +02:00
|
|
|
public:
|
|
|
|
Private()
|
|
|
|
: icon("plasma"),
|
|
|
|
toolBacker(0),
|
2008-05-19 02:41:41 +02:00
|
|
|
animCircleId(0),
|
|
|
|
animHighlightId(0),
|
|
|
|
animCircleFrame(0),
|
|
|
|
animHighlightFrame(0),
|
|
|
|
hovering(0)
|
2008-04-14 15:05:49 +02:00
|
|
|
{}
|
|
|
|
|
|
|
|
KIcon icon;
|
|
|
|
EmptyGraphicsItem *toolBacker;
|
2008-05-19 02:41:41 +02:00
|
|
|
int animCircleId;
|
|
|
|
int animHighlightId;
|
|
|
|
qreal animCircleFrame;
|
|
|
|
qreal animHighlightFrame;
|
|
|
|
bool hovering : 1;
|
2008-04-14 15:05:49 +02:00
|
|
|
};
|
2007-11-19 02:19:12 +01:00
|
|
|
|
2008-04-14 15:05:49 +02:00
|
|
|
DesktopToolbox::DesktopToolbox(QGraphicsItem *parent)
|
|
|
|
: Toolbox(parent),
|
|
|
|
d(new Private)
|
2007-11-19 02:19:12 +01:00
|
|
|
{
|
2008-04-26 22:24:02 +02:00
|
|
|
connect(Plasma::Animator::self(), SIGNAL(movementFinished(QGraphicsItem*)), this, SLOT(toolMoved(QGraphicsItem*)));
|
2008-04-20 16:23:37 +02:00
|
|
|
|
|
|
|
setZValue(10000000);
|
|
|
|
setFlag(ItemClipsToShape, true);
|
|
|
|
setFlag(ItemClipsChildrenToShape, false);
|
|
|
|
setFlag(ItemIgnoresTransformations, true);
|
|
|
|
}
|
|
|
|
|
2008-05-11 23:30:28 +02:00
|
|
|
DesktopToolbox::~DesktopToolbox()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
2008-04-20 16:23:37 +02:00
|
|
|
QRectF DesktopToolbox::boundingRect() const
|
|
|
|
{
|
|
|
|
return QRectF(0, 0, -size()*2, size()*2);
|
2007-11-19 02:19:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void DesktopToolbox::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
|
|
|
|
{
|
|
|
|
Q_UNUSED(option)
|
|
|
|
Q_UNUSED(widget)
|
|
|
|
|
2008-04-14 15:05:49 +02:00
|
|
|
painter->save();
|
|
|
|
painter->translate(boundingRect().topLeft());
|
|
|
|
|
2008-01-07 05:33:44 +01:00
|
|
|
QColor color1 = KColorScheme(QPalette::Active, KColorScheme::Window,
|
2008-04-22 23:10:14 +02:00
|
|
|
Plasma::Theme::defaultTheme()->colorScheme()).background().color();
|
2008-01-07 05:33:44 +01:00
|
|
|
color1.setAlpha(64);
|
|
|
|
|
|
|
|
QColor color2 = KColorScheme(QPalette::Active, KColorScheme::Window,
|
2008-04-22 23:10:14 +02:00
|
|
|
Plasma::Theme::defaultTheme()->colorScheme()).foreground().color();
|
2008-01-07 05:33:44 +01:00
|
|
|
color2.setAlpha(64);
|
|
|
|
|
2007-11-19 02:19:12 +01:00
|
|
|
QPainterPath p = shape();
|
2008-05-19 02:41:41 +02:00
|
|
|
QRadialGradient gradient(boundingRect().topLeft(), size() + d->animCircleFrame);
|
2008-04-14 15:05:49 +02:00
|
|
|
gradient.setFocalPoint(boundingRect().topLeft());
|
2008-01-07 05:33:44 +01:00
|
|
|
gradient.setColorAt(0, color1);
|
2008-01-07 06:46:53 +01:00
|
|
|
gradient.setColorAt(.87, color1);
|
|
|
|
gradient.setColorAt(.97, color2);
|
2008-01-08 00:01:38 +01:00
|
|
|
color2.setAlpha(0);
|
|
|
|
gradient.setColorAt(1, color2);
|
2007-11-19 02:19:12 +01:00
|
|
|
painter->save();
|
|
|
|
painter->setPen(Qt::NoPen);
|
|
|
|
painter->setRenderHint(QPainter::Antialiasing, true);
|
|
|
|
painter->setBrush(gradient);
|
|
|
|
painter->drawPath(p);
|
|
|
|
painter->restore();
|
2008-01-07 23:52:21 +01:00
|
|
|
|
2008-05-19 02:41:41 +02:00
|
|
|
const qreal progress = d->animHighlightFrame;
|
2008-01-07 23:52:21 +01:00
|
|
|
|
2008-01-15 08:09:21 +01:00
|
|
|
if (progress <= 0.9) {
|
2008-04-14 15:05:49 +02:00
|
|
|
d->icon.paint(painter, QRect(QPoint((int)boundingRect().left() - iconSize().width() + 2, 2), iconSize()), Qt::AlignCenter, QIcon::Disabled, QIcon::Off);
|
2008-01-07 23:52:21 +01:00
|
|
|
}
|
2008-01-08 01:41:53 +01:00
|
|
|
|
2008-01-15 08:09:21 +01:00
|
|
|
if (progress > 0.1) {
|
2008-01-07 23:52:21 +01:00
|
|
|
painter->save();
|
2008-01-15 08:09:21 +01:00
|
|
|
painter->setOpacity(progress);
|
2008-04-14 15:05:49 +02:00
|
|
|
d->icon.paint(painter, QRect(QPoint((int)boundingRect().left() - iconSize().width() + 2, 2), iconSize()));
|
2008-01-07 23:52:21 +01:00
|
|
|
painter->restore();
|
|
|
|
}
|
2008-04-14 15:05:49 +02:00
|
|
|
|
|
|
|
painter->restore();
|
2007-11-19 02:19:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QPainterPath DesktopToolbox::shape() const
|
|
|
|
{
|
|
|
|
QPainterPath path;
|
2008-05-19 02:41:41 +02:00
|
|
|
int toolSize = size() + (int)d->animCircleFrame;
|
2008-04-14 15:05:49 +02:00
|
|
|
path.arcTo(QRectF(boundingRect().left() - toolSize, boundingRect().top() - toolSize, toolSize*2, toolSize*2), 180, 90);
|
|
|
|
|
2007-11-19 02:19:12 +01:00
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DesktopToolbox::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
|
|
|
|
{
|
2008-05-19 02:41:41 +02:00
|
|
|
if (showing() || d->hovering) {
|
2008-01-08 06:04:11 +01:00
|
|
|
QGraphicsItem::hoverEnterEvent(event);
|
|
|
|
return;
|
|
|
|
}
|
2008-05-19 02:41:41 +02:00
|
|
|
Plasma::Animator* animdriver = Plasma::Animator::self();
|
|
|
|
if (d->animHighlightId) {
|
|
|
|
animdriver->stopCustomAnimation(d->animHighlightId);
|
2008-03-03 20:53:13 +01:00
|
|
|
}
|
2008-05-19 02:41:41 +02:00
|
|
|
d->hovering = true;
|
|
|
|
d->animHighlightId = animdriver->customAnimation(10, 240, Plasma::Animator::EaseInCurve, this, "animateHighlight");
|
2008-03-03 20:53:13 +01:00
|
|
|
|
2008-01-21 16:10:19 +01:00
|
|
|
QGraphicsItem::hoverEnterEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DesktopToolbox::showToolbox()
|
|
|
|
{
|
2008-04-14 15:05:49 +02:00
|
|
|
if (showing()) {
|
2008-01-21 16:10:19 +01:00
|
|
|
return;
|
|
|
|
}
|
2008-01-08 06:04:11 +01:00
|
|
|
|
2008-01-05 22:16:35 +01:00
|
|
|
int maxwidth = 0;
|
|
|
|
foreach (QGraphicsItem* tool, QGraphicsItem::children()) {
|
|
|
|
if (!tool->isEnabled()) {
|
|
|
|
continue;
|
|
|
|
}
|
2008-01-08 06:04:11 +01:00
|
|
|
maxwidth = qMax(static_cast<int>(tool->boundingRect().width()), maxwidth);
|
2008-01-05 22:16:35 +01:00
|
|
|
}
|
2008-01-08 06:04:11 +01:00
|
|
|
|
|
|
|
// put tools 5px from icon edge
|
2008-01-07 23:53:52 +01:00
|
|
|
const int iconWidth = 32;
|
2008-04-14 15:05:49 +02:00
|
|
|
int x = (int)boundingRect().left() - maxwidth - iconWidth - 5;
|
|
|
|
int y = (int)boundingRect().top() + 5;
|
2008-04-25 05:11:59 +02:00
|
|
|
Plasma::Animator* animdriver = Plasma::Animator::self();
|
2007-11-19 02:19:12 +01:00
|
|
|
foreach (QGraphicsItem* tool, QGraphicsItem::children()) {
|
2008-04-14 15:05:49 +02:00
|
|
|
if (tool == d->toolBacker) {
|
2008-01-30 04:40:17 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!tool->isEnabled()) {
|
|
|
|
if (tool->isVisible()) {
|
|
|
|
const int height = static_cast<int>(tool->boundingRect().height());
|
2008-04-25 05:11:59 +02:00
|
|
|
animdriver->moveItem(tool, Plasma::Animator::SlideOutMovement, QPoint(size() * 2, -height));
|
2008-01-30 04:40:17 +01:00
|
|
|
}
|
2007-12-05 21:22:25 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-01-08 02:25:09 +01:00
|
|
|
//kDebug() << "let's show and move" << tool << tool->boundingRect();
|
2007-11-19 02:19:12 +01:00
|
|
|
tool->show();
|
2008-04-25 05:11:59 +02:00
|
|
|
animdriver->moveItem(tool, Plasma::Animator::SlideInMovement, QPoint(x, y));
|
2007-11-19 02:19:12 +01:00
|
|
|
//x += 0;
|
|
|
|
y += static_cast<int>(tool->boundingRect().height()) + 5;
|
|
|
|
}
|
|
|
|
|
2008-04-14 15:05:49 +02:00
|
|
|
if (!d->toolBacker) {
|
|
|
|
d->toolBacker = new EmptyGraphicsItem(this);
|
2008-01-10 22:05:56 +01:00
|
|
|
}
|
2008-04-14 15:05:49 +02:00
|
|
|
d->toolBacker->setRect(QRectF(QPointF(x, 0), QSizeF(maxwidth, y - 10)));
|
|
|
|
d->toolBacker->show();
|
2008-01-08 06:04:11 +01:00
|
|
|
|
2008-05-19 02:41:41 +02:00
|
|
|
if (d->animCircleId) {
|
|
|
|
animdriver->stopCustomAnimation(d->animCircleId);
|
2007-11-19 02:19:12 +01:00
|
|
|
}
|
|
|
|
|
2008-04-14 15:05:49 +02:00
|
|
|
setShowing(true);
|
2008-01-03 10:43:13 +01:00
|
|
|
// TODO: 10 and 200 shouldn't be hardcoded here. There needs to be a way to
|
|
|
|
// match whatever the time is that moveItem() takes. Same in hoverLeaveEvent().
|
2008-05-19 02:41:41 +02:00
|
|
|
d->animCircleId = animdriver->customAnimation(10, 240, Plasma::Animator::EaseInCurve, this, "animateCircle");
|
2007-11-19 02:19:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void DesktopToolbox::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
|
|
|
|
{
|
2008-04-14 15:05:49 +02:00
|
|
|
//kDebug() << event->pos() << event->scenePos() << d->toolBacker->rect().contains(event->scenePos().toPoint());
|
2008-05-19 02:41:41 +02:00
|
|
|
if (! d->hovering) {
|
2008-01-08 06:04:11 +01:00
|
|
|
QGraphicsItem::hoverLeaveEvent(event);
|
|
|
|
return;
|
|
|
|
}
|
2008-05-19 02:41:41 +02:00
|
|
|
|
2008-01-21 16:10:19 +01:00
|
|
|
hideToolbox();
|
2008-05-19 02:41:41 +02:00
|
|
|
Plasma::Animator* animdriver = Plasma::Animator::self();
|
|
|
|
if (d->animHighlightId) {
|
|
|
|
animdriver->stopCustomAnimation(d->animHighlightId);
|
|
|
|
}
|
|
|
|
d->hovering = false;
|
|
|
|
d->animHighlightId = animdriver->customAnimation(10, 240, Plasma::Animator::EaseOutCurve, this, "animateHighlight");
|
|
|
|
|
2008-01-21 16:10:19 +01:00
|
|
|
QGraphicsItem::hoverLeaveEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DesktopToolbox::hideToolbox()
|
|
|
|
{
|
2008-04-14 15:05:49 +02:00
|
|
|
if (!showing()) {
|
2008-01-21 16:10:19 +01:00
|
|
|
return;
|
|
|
|
}
|
2008-01-08 06:04:11 +01:00
|
|
|
|
2008-04-14 15:05:49 +02:00
|
|
|
int x = size() * 2;
|
2007-11-19 02:19:12 +01:00
|
|
|
int y = 0;
|
2008-04-25 05:11:59 +02:00
|
|
|
Plasma::Animator* animdriver = Plasma::Animator::self();
|
2007-11-19 02:19:12 +01:00
|
|
|
foreach (QGraphicsItem* tool, QGraphicsItem::children()) {
|
2008-04-14 15:05:49 +02:00
|
|
|
if (tool == d->toolBacker) {
|
2008-01-10 22:05:56 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const int height = static_cast<int>(tool->boundingRect().height());
|
2008-04-25 05:11:59 +02:00
|
|
|
animdriver->moveItem(tool, Plasma::Animator::SlideOutMovement, QPoint(x, y-height));
|
2007-11-19 02:19:12 +01:00
|
|
|
}
|
|
|
|
|
2008-05-19 02:41:41 +02:00
|
|
|
if (d->animCircleId) {
|
|
|
|
animdriver->stopCustomAnimation(d->animCircleId);
|
2007-11-19 02:19:12 +01:00
|
|
|
}
|
|
|
|
|
2008-04-14 15:05:49 +02:00
|
|
|
setShowing(false);
|
2008-05-19 02:41:41 +02:00
|
|
|
d->animCircleId = animdriver->customAnimation(10, 240, Plasma::Animator::EaseOutCurve, this, "animateCircle");
|
2008-01-10 22:05:56 +01:00
|
|
|
|
2008-04-14 15:05:49 +02:00
|
|
|
if (d->toolBacker) {
|
|
|
|
d->toolBacker->hide();
|
2008-01-10 22:05:56 +01:00
|
|
|
}
|
2007-11-19 02:19:12 +01:00
|
|
|
}
|
|
|
|
|
2008-05-19 02:41:41 +02:00
|
|
|
void DesktopToolbox::animateCircle(qreal progress)
|
2007-11-19 02:19:12 +01:00
|
|
|
{
|
2008-04-14 15:05:49 +02:00
|
|
|
if (showing()) {
|
2008-05-19 02:41:41 +02:00
|
|
|
d->animCircleFrame = size() * progress;
|
2007-11-19 02:19:12 +01:00
|
|
|
} else {
|
2008-05-19 02:41:41 +02:00
|
|
|
d->animCircleFrame = size() * (1.0 - progress);
|
2007-11-19 02:19:12 +01:00
|
|
|
}
|
|
|
|
|
2008-05-19 02:41:41 +02:00
|
|
|
if (progress >= 1) {
|
|
|
|
d->animCircleId = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DesktopToolbox::animateHighlight(qreal progress)
|
|
|
|
{
|
|
|
|
if (d->hovering) {
|
|
|
|
d->animHighlightFrame = progress;
|
|
|
|
} else {
|
|
|
|
d->animHighlightFrame = 1.0 - progress;
|
|
|
|
}
|
2007-11-19 02:19:12 +01:00
|
|
|
|
|
|
|
if (progress >= 1) {
|
2008-05-19 02:41:41 +02:00
|
|
|
d->animHighlightId = 0;
|
2007-11-19 02:19:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DesktopToolbox::toolMoved(QGraphicsItem *item)
|
|
|
|
{
|
2008-01-08 02:25:09 +01:00
|
|
|
//kDebug() << "geometry is now " << static_cast<Plasma::Widget*>(item)->geometry();
|
2008-04-14 15:05:49 +02:00
|
|
|
if (!showing() &&
|
2008-04-13 23:47:47 +02:00
|
|
|
QGraphicsItem::children().indexOf(static_cast<Plasma::Applet*>(item)) != -1) {
|
2007-11-19 02:19:12 +01:00
|
|
|
item->hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-19 02:41:41 +02:00
|
|
|
void DesktopToolbox::toggle()
|
|
|
|
{
|
|
|
|
if (showing()) {
|
|
|
|
hideToolbox();
|
|
|
|
} else {
|
|
|
|
showToolbox();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-19 02:19:12 +01:00
|
|
|
} // plasma namespace
|
|
|
|
|
2007-12-21 02:14:32 +01:00
|
|
|
#include "desktoptoolbox_p.moc"
|
2007-11-19 02:19:12 +01:00
|
|
|
|