2007-11-19 02:19:12 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2007 by Aaron Seigo <aseigo@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.
|
|
|
|
*/
|
|
|
|
|
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>
|
|
|
|
|
2007-11-19 02:19:12 +01:00
|
|
|
#include "widgets/widget.h"
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2007-11-19 02:19:12 +01:00
|
|
|
DesktopToolbox::DesktopToolbox(QGraphicsItem *parent)
|
|
|
|
: QGraphicsItem(parent),
|
2008-01-01 20:57:40 +01:00
|
|
|
m_icon("plasma"),
|
2008-01-10 22:05:56 +01:00
|
|
|
m_toolBacker(0),
|
2007-11-19 02:19:12 +01:00
|
|
|
m_size(50),
|
|
|
|
m_showing(false),
|
|
|
|
m_animId(0),
|
|
|
|
m_animFrame(0)
|
|
|
|
{
|
|
|
|
setAcceptsHoverEvents(true);
|
|
|
|
setZValue(10000);
|
|
|
|
setFlag(ItemClipsToShape, true);
|
|
|
|
setFlag(ItemClipsChildrenToShape, false);
|
|
|
|
|
|
|
|
connect(Plasma::Phase::self(), SIGNAL(movementComplete(QGraphicsItem*)), this, SLOT(toolMoved(QGraphicsItem*)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*QRectF DesktopToolbox::sizeHint() const
|
|
|
|
{
|
|
|
|
return boundingRect();
|
|
|
|
}*/
|
|
|
|
|
|
|
|
QRectF DesktopToolbox::boundingRect() const
|
|
|
|
{
|
|
|
|
return QRectF(0, 0, m_size*2, m_size*2);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DesktopToolbox::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
|
|
|
|
{
|
|
|
|
Q_UNUSED(option)
|
|
|
|
Q_UNUSED(widget)
|
|
|
|
|
2008-01-07 05:33:44 +01:00
|
|
|
QColor color1 = KColorScheme(QPalette::Active, KColorScheme::Window,
|
|
|
|
Plasma::Theme::self()->colors()).background().color();
|
|
|
|
color1.setAlpha(64);
|
|
|
|
|
|
|
|
QColor color2 = KColorScheme(QPalette::Active, KColorScheme::Window,
|
|
|
|
Plasma::Theme::self()->colors()).foreground().color();
|
|
|
|
color2.setAlpha(64);
|
|
|
|
|
2007-11-19 02:19:12 +01:00
|
|
|
QPainterPath p = shape();
|
2008-01-06 03:50:34 +01:00
|
|
|
QRadialGradient gradient(QPoint(m_size*2, 0), m_size + m_animFrame);
|
2007-11-19 02:19:12 +01:00
|
|
|
gradient.setFocalPoint(QPointF(m_size*2, 0));
|
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
|
|
|
|
|
|
|
const qreal progress = m_animFrame / m_size;
|
|
|
|
|
2008-01-15 08:09:21 +01:00
|
|
|
if (progress <= 0.9) {
|
|
|
|
m_icon.paint(painter, QRect(m_size*2 - 34, 2, 32, 32), 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);
|
|
|
|
m_icon.paint(painter, QRect(m_size*2 - 34, 2, 32, 32));
|
2008-01-07 23:52:21 +01:00
|
|
|
painter->restore();
|
|
|
|
}
|
2007-11-19 02:19:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QPainterPath DesktopToolbox::shape() const
|
|
|
|
{
|
|
|
|
QPainterPath path;
|
2008-01-07 23:52:21 +01:00
|
|
|
int size = m_size + (int)m_animFrame;
|
2007-11-19 02:19:12 +01:00
|
|
|
path.moveTo(m_size*2, 0);
|
|
|
|
path.arcTo(QRectF(m_size*2 - size, -size, size*2, size*2), 180, 90);
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DesktopToolbox::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
|
|
|
|
{
|
2008-01-30 04:40:17 +01:00
|
|
|
if (m_showing || m_stopwatch.elapsed() < 100) {
|
2008-01-08 06:04:11 +01:00
|
|
|
QGraphicsItem::hoverEnterEvent(event);
|
|
|
|
return;
|
|
|
|
}
|
2008-01-21 16:10:19 +01:00
|
|
|
showToolbox();
|
|
|
|
QGraphicsItem::hoverEnterEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DesktopToolbox::showToolbox()
|
|
|
|
{
|
|
|
|
if (m_showing) {
|
|
|
|
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;
|
|
|
|
int x = m_size*2 - maxwidth - iconWidth - 5;
|
2008-01-05 22:16:35 +01:00
|
|
|
int y = 5; // pos().y();
|
2007-11-19 02:19:12 +01:00
|
|
|
Plasma::Phase* phase = Plasma::Phase::self();
|
|
|
|
foreach (QGraphicsItem* tool, QGraphicsItem::children()) {
|
2008-01-30 04:40:17 +01:00
|
|
|
if (tool == m_toolBacker) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!tool->isEnabled()) {
|
|
|
|
if (tool->isVisible()) {
|
|
|
|
const int height = static_cast<int>(tool->boundingRect().height());
|
|
|
|
phase->moveItem(tool, Plasma::Phase::SlideOut, QPoint(m_size * 2, -height));
|
|
|
|
}
|
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();
|
|
|
|
phase->moveItem(tool, Plasma::Phase::SlideIn, QPoint(x, y));
|
|
|
|
//x += 0;
|
|
|
|
y += static_cast<int>(tool->boundingRect().height()) + 5;
|
|
|
|
}
|
|
|
|
|
2008-01-10 22:05:56 +01:00
|
|
|
if (!m_toolBacker) {
|
|
|
|
m_toolBacker = new EmptyGraphicsItem(this);
|
|
|
|
}
|
|
|
|
m_toolBacker->setRect(QRectF(QPointF(x, 0), QSizeF(maxwidth, y - 10)));
|
|
|
|
m_toolBacker->show();
|
2008-01-08 06:04:11 +01:00
|
|
|
|
2007-11-19 02:19:12 +01:00
|
|
|
if (m_animId) {
|
|
|
|
phase->stopCustomAnimation(m_animId);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_showing = 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-01-08 15:51:05 +01:00
|
|
|
m_animId = phase->customAnimation(10, 240, Plasma::Phase::EaseInCurve, this, "animate");
|
2008-01-30 04:40:17 +01:00
|
|
|
m_stopwatch.restart();
|
2007-11-19 02:19:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void DesktopToolbox::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
|
|
|
|
{
|
2008-01-10 22:05:56 +01:00
|
|
|
//kDebug() << event->pos() << event->scenePos() << m_toolBacker->rect().contains(event->scenePos().toPoint());
|
2008-01-30 04:40:17 +01:00
|
|
|
if ((m_toolBacker && m_toolBacker->rect().contains(event->scenePos().toPoint())) ||
|
|
|
|
m_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()
|
|
|
|
{
|
|
|
|
if (!m_showing) {
|
|
|
|
return;
|
|
|
|
}
|
2008-01-08 06:04:11 +01:00
|
|
|
|
2008-01-30 04:40:17 +01:00
|
|
|
int x = m_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-01-10 22:05:56 +01:00
|
|
|
if (tool == m_toolBacker) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const int height = static_cast<int>(tool->boundingRect().height());
|
2008-01-17 19:35:23 +01:00
|
|
|
phase->moveItem(tool, Plasma::Phase::SlideOut, QPoint(x, y-height));
|
2007-11-19 02:19:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (m_animId) {
|
|
|
|
phase->stopCustomAnimation(m_animId);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_showing = false;
|
2008-01-08 15:51:05 +01:00
|
|
|
m_animId = phase->customAnimation(10, 240, Plasma::Phase::EaseOutCurve, this, "animate");
|
2008-01-10 22:05:56 +01:00
|
|
|
|
|
|
|
if (m_toolBacker) {
|
|
|
|
m_toolBacker->hide();
|
|
|
|
}
|
2008-01-30 04:40:17 +01:00
|
|
|
|
|
|
|
m_stopwatch.restart();
|
2007-11-19 02:19:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void DesktopToolbox::animate(qreal progress)
|
|
|
|
{
|
|
|
|
if (m_showing) {
|
2008-01-07 23:52:21 +01:00
|
|
|
m_animFrame = m_size * progress;
|
2007-11-19 02:19:12 +01:00
|
|
|
} else {
|
2008-01-07 23:52:21 +01:00
|
|
|
m_animFrame = m_size * (1.0 - progress);
|
2007-11-19 02:19:12 +01:00
|
|
|
}
|
|
|
|
|
2008-01-08 02:25:09 +01:00
|
|
|
//kDebug() << "animating at" << progress << "for" << m_animFrame;
|
2007-11-19 02:19:12 +01:00
|
|
|
|
|
|
|
if (progress >= 1) {
|
|
|
|
m_animId = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DesktopToolbox::toolMoved(QGraphicsItem *item)
|
|
|
|
{
|
2008-01-08 02:25:09 +01:00
|
|
|
//kDebug() << "geometry is now " << static_cast<Plasma::Widget*>(item)->geometry();
|
2007-11-19 02:19:12 +01:00
|
|
|
if (!m_showing &&
|
|
|
|
QGraphicsItem::children().indexOf(static_cast<Plasma::Widget*>(item)) != -1) {
|
|
|
|
item->hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-05 21:22:25 +01:00
|
|
|
void DesktopToolbox::addTool(QGraphicsItem *tool, const QString &name)
|
2007-11-19 02:19:12 +01:00
|
|
|
{
|
|
|
|
if (!tool) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
tool->hide();
|
2008-01-05 22:16:35 +01:00
|
|
|
const int height = static_cast<int>(tool->boundingRect().height());
|
|
|
|
tool->setPos(QPoint(m_size*2,-height));
|
2007-11-19 02:19:12 +01:00
|
|
|
tool->setZValue(zValue() + 1);
|
|
|
|
tool->setParentItem(this);
|
2007-12-05 21:22:25 +01:00
|
|
|
tool->setData(ToolName, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DesktopToolbox::enableTool(const QString &toolName, bool visible)
|
|
|
|
{
|
2008-01-08 02:25:09 +01:00
|
|
|
//kDebug() << (visible? "enabling" : "disabling") << "tool" << toolName;
|
2007-12-19 03:41:45 +01:00
|
|
|
QGraphicsItem *t = tool(toolName);
|
|
|
|
|
2008-01-30 04:40:17 +01:00
|
|
|
if (t && t->isEnabled() != visible) {
|
2007-12-19 03:41:45 +01:00
|
|
|
t->setEnabled(visible);
|
2008-01-30 04:40:17 +01:00
|
|
|
// adjust the visible items
|
|
|
|
if (m_showing) {
|
|
|
|
m_showing = false;
|
|
|
|
showToolbox();
|
|
|
|
}
|
2007-12-19 03:41:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DesktopToolbox::isToolEnabled(const QString &toolName) const
|
|
|
|
{
|
|
|
|
QGraphicsItem *t = tool(toolName);
|
|
|
|
|
|
|
|
if (t) {
|
|
|
|
return t->isEnabled();
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QGraphicsItem* DesktopToolbox::tool(const QString &toolName) const
|
|
|
|
{
|
2007-12-05 21:22:25 +01:00
|
|
|
foreach (QGraphicsItem *child, QGraphicsItem::children()) {
|
2008-01-08 02:25:09 +01:00
|
|
|
//kDebug() << "checking tool" << child << child->data(ToolName);
|
2007-12-05 21:22:25 +01:00
|
|
|
if (child->data(ToolName).toString() == toolName) {
|
2008-01-08 02:25:09 +01:00
|
|
|
//kDebug() << "tool found!";
|
2007-12-19 03:41:45 +01:00
|
|
|
return child;
|
2007-12-05 21:22:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-19 03:41:45 +01:00
|
|
|
return 0;
|
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
|
|
|
|