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-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),
|
|
|
|
animId(0),
|
|
|
|
animFrame(0)
|
|
|
|
{}
|
|
|
|
|
|
|
|
KIcon icon;
|
|
|
|
EmptyGraphicsItem *toolBacker;
|
|
|
|
QTime stopwatch;
|
2008-04-24 23:26:36 +02:00
|
|
|
int animId;
|
2008-04-14 15:05:49 +02:00
|
|
|
qreal animFrame;
|
|
|
|
};
|
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-14 15:05:49 +02:00
|
|
|
connect(Plasma::Phase::self(), SIGNAL(movementComplete(QGraphicsItem*)), this, SLOT(toolMoved(QGraphicsItem*)));
|
2008-04-20 16:23:37 +02:00
|
|
|
|
|
|
|
setZValue(10000000);
|
|
|
|
setFlag(ItemClipsToShape, true);
|
|
|
|
setFlag(ItemClipsChildrenToShape, false);
|
|
|
|
setFlag(ItemIgnoresTransformations, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
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-04-14 15:05:49 +02:00
|
|
|
QRadialGradient gradient(boundingRect().topLeft(), size() + d->animFrame);
|
|
|
|
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-04-14 15:05:49 +02:00
|
|
|
const qreal progress = d->animFrame / size();
|
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-04-14 15:05:49 +02:00
|
|
|
int toolSize = size() + (int)d->animFrame;
|
|
|
|
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-04-14 15:05:49 +02:00
|
|
|
if (showing() || d->stopwatch.elapsed() < 100) {
|
2008-01-08 06:04:11 +01:00
|
|
|
QGraphicsItem::hoverEnterEvent(event);
|
|
|
|
return;
|
|
|
|
}
|
2008-03-03 20:53:13 +01:00
|
|
|
|
|
|
|
QPainterPath path;
|
2008-04-14 15:05:49 +02:00
|
|
|
int toolSize = size() + (int)d->animFrame - 15;
|
|
|
|
path.moveTo(size()*2, 0);
|
|
|
|
path.arcTo(QRectF(size() * 2 - toolSize, -toolSize, toolSize*2, toolSize*2), 180, 90);
|
|
|
|
path.lineTo(size()*2, 0);
|
2008-03-03 20:53:13 +01:00
|
|
|
|
|
|
|
if (path.contains(event->pos())) {
|
|
|
|
QGraphicsItem::hoverEnterEvent(event);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-01-21 16:10:19 +01:00
|
|
|
showToolbox();
|
|
|
|
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;
|
2007-11-19 02:19:12 +01:00
|
|
|
Plasma::Phase* phase = Plasma::Phase::self();
|
|
|
|
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-24 23:06:08 +02:00
|
|
|
phase->moveItem(tool, Plasma::Phase::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-24 23:06:08 +02:00
|
|
|
phase->moveItem(tool, Plasma::Phase::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-04-14 15:05:49 +02:00
|
|
|
if (d->animId) {
|
|
|
|
phase->stopCustomAnimation(d->animId);
|
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-04-14 15:05:49 +02:00
|
|
|
d->animId = phase->customAnimation(10, 240, Plasma::Phase::EaseInCurve, this, "animate");
|
|
|
|
d->stopwatch.restart();
|
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());
|
|
|
|
if ((d->toolBacker && d->toolBacker->rect().contains(event->scenePos().toPoint())) ||
|
|
|
|
d->stopwatch.elapsed() < 100) {
|
2008-01-08 06:04:11 +01:00
|
|
|
QGraphicsItem::hoverLeaveEvent(event);
|
|
|
|
return;
|
|
|
|
}
|
2008-01-21 16:10:19 +01:00
|
|
|
hideToolbox();
|
|
|
|
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;
|
|
|
|
Plasma::Phase* phase = Plasma::Phase::self();
|
|
|
|
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-24 23:06:08 +02:00
|
|
|
phase->moveItem(tool, Plasma::Phase::SlideOutMovement, QPoint(x, y-height));
|
2007-11-19 02:19:12 +01:00
|
|
|
}
|
|
|
|
|
2008-04-14 15:05:49 +02:00
|
|
|
if (d->animId) {
|
|
|
|
phase->stopCustomAnimation(d->animId);
|
2007-11-19 02:19:12 +01:00
|
|
|
}
|
|
|
|
|
2008-04-14 15:05:49 +02:00
|
|
|
setShowing(false);
|
|
|
|
d->animId = phase->customAnimation(10, 240, Plasma::Phase::EaseOutCurve, this, "animate");
|
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
|
|
|
}
|
2008-01-30 04:40:17 +01:00
|
|
|
|
2008-04-14 15:05:49 +02:00
|
|
|
d->stopwatch.restart();
|
2007-11-19 02:19:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void DesktopToolbox::animate(qreal progress)
|
|
|
|
{
|
2008-04-14 15:05:49 +02:00
|
|
|
if (showing()) {
|
|
|
|
d->animFrame = size() * progress;
|
2007-11-19 02:19:12 +01:00
|
|
|
} else {
|
2008-04-14 15:05:49 +02:00
|
|
|
d->animFrame = size() * (1.0 - progress);
|
2007-11-19 02:19:12 +01:00
|
|
|
}
|
|
|
|
|
2008-04-14 15:05:49 +02:00
|
|
|
//kDebug() << "animating at" << progress << "for" << d->animFrame;
|
2007-11-19 02:19:12 +01:00
|
|
|
|
|
|
|
if (progress >= 1) {
|
2008-04-14 15:05:49 +02:00
|
|
|
d->animId = 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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // plasma namespace
|
|
|
|
|
2007-12-21 02:14:32 +01:00
|
|
|
#include "desktoptoolbox_p.moc"
|
2007-11-19 02:19:12 +01:00
|
|
|
|