2008-04-14 15:05:49 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2007 by Aaron Seigo <aseigo@kde.org>
|
|
|
|
* Copyright 2008 by Marco Martin <notmart@gmail.com>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2008-07-30 22:39:58 +02:00
|
|
|
#include "toolbox_p.h"
|
2008-04-14 15:05:49 +02:00
|
|
|
|
2008-05-24 03:48:36 +02:00
|
|
|
#include <QAction>
|
2008-04-14 15:05:49 +02:00
|
|
|
#include <QGraphicsSceneHoverEvent>
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QRadialGradient>
|
|
|
|
|
|
|
|
#include <KColorScheme>
|
|
|
|
#include <KDebug>
|
|
|
|
|
2008-05-24 03:48:36 +02:00
|
|
|
#include <plasma/theme.h>
|
2008-10-29 18:46:33 +01:00
|
|
|
#include "widgets/iconwidget.h"
|
2008-05-24 03:48:36 +02:00
|
|
|
|
2008-04-14 15:05:49 +02:00
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
2008-07-01 20:56:43 +02:00
|
|
|
class ToolBoxPrivate
|
2008-04-14 15:05:49 +02:00
|
|
|
{
|
|
|
|
public:
|
2008-07-01 20:56:43 +02:00
|
|
|
ToolBoxPrivate()
|
2008-04-14 15:05:49 +02:00
|
|
|
: size(50),
|
|
|
|
iconSize(32, 32),
|
|
|
|
hidden(false),
|
2008-04-16 15:55:23 +02:00
|
|
|
showing(false),
|
2008-08-28 17:59:03 +02:00
|
|
|
corner(ToolBox::TopRight)
|
2008-04-14 15:05:49 +02:00
|
|
|
{}
|
|
|
|
|
|
|
|
int size;
|
|
|
|
QSize iconSize;
|
|
|
|
bool hidden;
|
|
|
|
bool showing;
|
2008-08-28 17:59:03 +02:00
|
|
|
ToolBox::Corner corner;
|
2008-04-14 15:05:49 +02:00
|
|
|
};
|
|
|
|
|
2008-05-24 14:25:56 +02:00
|
|
|
ToolBox::ToolBox(QGraphicsItem *parent)
|
2008-04-14 15:05:49 +02:00
|
|
|
: QGraphicsItem(parent),
|
2008-07-01 20:56:43 +02:00
|
|
|
d(new ToolBoxPrivate)
|
2008-04-14 15:05:49 +02:00
|
|
|
{
|
|
|
|
setAcceptsHoverEvents(true);
|
|
|
|
}
|
|
|
|
|
2008-05-24 14:25:56 +02:00
|
|
|
ToolBox::~ToolBox()
|
2008-05-09 09:04:36 +02:00
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
2008-08-28 17:59:03 +02:00
|
|
|
QPoint ToolBox::toolPosition(int toolHeight)
|
|
|
|
{
|
|
|
|
switch (d->corner) {
|
|
|
|
case TopRight:
|
2008-10-12 01:28:20 +02:00
|
|
|
return QPoint(d->size * 2, -toolHeight);
|
2008-08-28 17:59:03 +02:00
|
|
|
case Top:
|
2008-10-12 01:28:20 +02:00
|
|
|
return QPoint((int)boundingRect().center().x() - d->iconSize.width(), -toolHeight);
|
2008-08-28 17:59:03 +02:00
|
|
|
case TopLeft:
|
2008-10-12 01:28:20 +02:00
|
|
|
return QPoint(-d->size * 2, -toolHeight);
|
2008-08-28 17:59:03 +02:00
|
|
|
case Left:
|
2008-10-12 01:28:20 +02:00
|
|
|
return QPoint(-d->size * 2, (int)boundingRect().center().y() - d->iconSize.height());
|
2008-08-28 17:59:03 +02:00
|
|
|
case Right:
|
2008-10-12 01:28:20 +02:00
|
|
|
return QPoint(d->size * 2, (int)boundingRect().center().y() - d->iconSize.height());
|
2008-08-28 17:59:03 +02:00
|
|
|
case BottomLeft:
|
2008-10-12 01:28:20 +02:00
|
|
|
return QPoint(-d->size * 2, toolHeight);
|
2008-08-28 17:59:03 +02:00
|
|
|
case Bottom:
|
2008-10-12 01:28:20 +02:00
|
|
|
return QPoint((int)boundingRect().center().x() - d->iconSize.width(), toolHeight);
|
2008-08-28 17:59:03 +02:00
|
|
|
case BottomRight:
|
|
|
|
default:
|
2008-10-12 01:28:20 +02:00
|
|
|
return QPoint(d->size * 2, toolHeight);
|
2008-08-28 17:59:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-24 14:25:56 +02:00
|
|
|
void ToolBox::addTool(QAction *action)
|
2008-04-14 15:05:49 +02:00
|
|
|
{
|
2008-05-24 03:48:36 +02:00
|
|
|
if (!action) {
|
2008-04-14 15:05:49 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-10-29 18:46:33 +01:00
|
|
|
Plasma::IconWidget *tool = new Plasma::IconWidget(this);
|
2008-05-24 03:48:36 +02:00
|
|
|
|
|
|
|
tool->setAction(action);
|
|
|
|
tool->setDrawBackground(true);
|
|
|
|
tool->setOrientation(Qt::Horizontal);
|
2008-07-30 22:39:58 +02:00
|
|
|
tool->resize(tool->sizeFromIconSize(22));
|
2008-05-24 03:48:36 +02:00
|
|
|
|
2008-04-14 15:05:49 +02:00
|
|
|
tool->hide();
|
|
|
|
const int height = static_cast<int>(tool->boundingRect().height());
|
2008-08-28 17:59:03 +02:00
|
|
|
tool->setPos(toolPosition(height));
|
2008-04-14 15:05:49 +02:00
|
|
|
tool->setZValue(zValue() + 1);
|
|
|
|
|
2008-05-24 03:48:36 +02:00
|
|
|
//make enabled/disabled tools appear/disappear instantly
|
2008-05-24 14:25:56 +02:00
|
|
|
connect(tool, SIGNAL(changed()), this, SLOT(updateToolBox()));
|
2008-04-14 15:05:49 +02:00
|
|
|
}
|
|
|
|
|
2008-05-24 14:25:56 +02:00
|
|
|
void ToolBox::updateToolBox()
|
2008-04-14 15:05:49 +02:00
|
|
|
{
|
2008-10-12 01:28:20 +02:00
|
|
|
if (d->showing) {
|
2008-05-24 03:48:36 +02:00
|
|
|
d->showing = false;
|
2008-05-24 14:25:56 +02:00
|
|
|
showToolBox();
|
2008-04-14 15:05:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-24 14:25:56 +02:00
|
|
|
void ToolBox::removeTool(QAction *action)
|
2008-04-14 15:05:49 +02:00
|
|
|
{
|
|
|
|
foreach (QGraphicsItem *child, QGraphicsItem::children()) {
|
|
|
|
//kDebug() << "checking tool" << child << child->data(ToolName);
|
2008-10-29 18:46:33 +01:00
|
|
|
Plasma::IconWidget *tool = dynamic_cast<Plasma::IconWidget*>(child);
|
2008-05-24 03:48:36 +02:00
|
|
|
if (tool && tool->action() == action) {
|
2008-04-14 15:05:49 +02:00
|
|
|
//kDebug() << "tool found!";
|
2008-05-24 03:48:36 +02:00
|
|
|
tool->deleteLater();
|
|
|
|
break;
|
2008-04-14 15:05:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-24 14:25:56 +02:00
|
|
|
int ToolBox::size() const
|
2008-04-14 15:05:49 +02:00
|
|
|
{
|
|
|
|
return d->size;
|
|
|
|
}
|
|
|
|
|
2008-05-24 14:25:56 +02:00
|
|
|
void ToolBox::setSize(const int newSize)
|
2008-04-14 15:05:49 +02:00
|
|
|
{
|
|
|
|
d->size = newSize;
|
|
|
|
}
|
|
|
|
|
2008-05-24 14:25:56 +02:00
|
|
|
QSize ToolBox::iconSize() const
|
2008-04-14 15:05:49 +02:00
|
|
|
{
|
|
|
|
return d->iconSize;
|
|
|
|
}
|
|
|
|
|
2008-05-24 14:25:56 +02:00
|
|
|
void ToolBox::setIconSize(const QSize newSize)
|
2008-04-14 15:05:49 +02:00
|
|
|
{
|
|
|
|
d->iconSize = newSize;
|
|
|
|
}
|
|
|
|
|
2008-05-24 14:25:56 +02:00
|
|
|
bool ToolBox::showing() const
|
2008-04-14 15:05:49 +02:00
|
|
|
{
|
|
|
|
return d->showing;
|
|
|
|
}
|
|
|
|
|
2008-05-24 14:25:56 +02:00
|
|
|
void ToolBox::setShowing(const bool show)
|
2008-04-14 15:05:49 +02:00
|
|
|
{
|
2008-04-16 15:55:23 +02:00
|
|
|
d->showing = show;
|
|
|
|
}
|
|
|
|
|
2008-08-28 17:59:03 +02:00
|
|
|
void ToolBox::setCorner(const Corner corner)
|
2008-04-16 15:55:23 +02:00
|
|
|
{
|
2008-08-28 17:59:03 +02:00
|
|
|
d->corner = corner;
|
2008-04-16 15:55:23 +02:00
|
|
|
}
|
|
|
|
|
2008-08-28 17:59:03 +02:00
|
|
|
ToolBox::Corner ToolBox::corner() const
|
2008-04-16 15:55:23 +02:00
|
|
|
{
|
2008-08-28 17:59:03 +02:00
|
|
|
return d->corner;
|
2008-04-14 15:05:49 +02:00
|
|
|
}
|
|
|
|
|
2008-05-24 14:25:56 +02:00
|
|
|
void ToolBox::mousePressEvent(QGraphicsSceneMouseEvent *event)
|
2008-04-26 19:31:49 +02:00
|
|
|
{
|
|
|
|
event->accept();
|
|
|
|
}
|
|
|
|
|
2008-05-24 14:25:56 +02:00
|
|
|
void ToolBox::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
|
2008-04-26 19:31:49 +02:00
|
|
|
{
|
|
|
|
if (boundingRect().contains(event->pos())) {
|
|
|
|
emit toggled();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-14 15:05:49 +02:00
|
|
|
} // plasma namespace
|
|
|
|
|
|
|
|
#include "toolbox_p.moc"
|
|
|
|
|