2008-11-04 00:08:39 +01: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "paneltoolbox_p.h"
|
|
|
|
|
|
|
|
#include <QGraphicsSceneHoverEvent>
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QRadialGradient>
|
|
|
|
#include <QApplication>
|
|
|
|
|
2008-11-04 03:39:56 +01:00
|
|
|
#include <kcolorscheme.h>
|
|
|
|
#include <kdebug.h>
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
#include <plasma/applet.h>
|
2008-11-04 03:39:56 +01:00
|
|
|
#include <plasma/paintutils.h>
|
2009-01-28 22:07:38 +01:00
|
|
|
#include <plasma/svg.h>
|
2009-10-07 19:11:03 +02:00
|
|
|
#include <plasma/theme.h>
|
|
|
|
#include <plasma/tooltipcontent.h>
|
|
|
|
#include <plasma/tooltipmanager.h>
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
|
|
|
class PanelToolBoxPrivate
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
PanelToolBoxPrivate()
|
|
|
|
: icon("plasma"),
|
|
|
|
animId(0),
|
|
|
|
animFrame(0),
|
2009-04-24 21:29:16 +02:00
|
|
|
highlighting(false)
|
2008-11-18 01:12:05 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
KIcon icon;
|
|
|
|
int animId;
|
|
|
|
qreal animFrame;
|
2008-11-18 01:12:05 +01:00
|
|
|
QColor fgColor;
|
|
|
|
QColor bgColor;
|
2009-01-28 22:07:38 +01:00
|
|
|
Plasma::Svg *background;
|
2009-04-24 21:29:16 +02:00
|
|
|
bool highlighting;
|
2008-11-04 00:08:39 +01:00
|
|
|
};
|
|
|
|
|
2008-11-14 08:28:02 +01:00
|
|
|
PanelToolBox::PanelToolBox(Containment *parent)
|
2008-11-04 00:08:39 +01:00
|
|
|
: ToolBox(parent),
|
|
|
|
d(new PanelToolBoxPrivate)
|
|
|
|
{
|
|
|
|
connect(this, SIGNAL(toggled()), this, SLOT(toggle()));
|
|
|
|
|
|
|
|
setZValue(10000000);
|
|
|
|
setFlag(ItemClipsChildrenToShape, false);
|
|
|
|
//panel toolbox is allowed to zoom, otherwise a part of it will be displayed behind the desktop
|
|
|
|
//toolbox when the desktop is zoomed out
|
|
|
|
setFlag(ItemIgnoresTransformations, false);
|
2008-11-18 01:12:05 +01:00
|
|
|
assignColors();
|
|
|
|
connect(Plasma::Theme::defaultTheme(), SIGNAL(themeChanged()),
|
|
|
|
this, SLOT(assignColors()));
|
2009-01-28 22:07:38 +01:00
|
|
|
|
2009-04-24 21:29:16 +02:00
|
|
|
d->background = new Plasma::Svg(this);
|
2009-01-28 22:07:38 +01:00
|
|
|
d->background->setImagePath("widgets/toolbox");
|
|
|
|
d->background->setContainsMultipleImages(true);
|
2009-10-07 19:11:03 +02:00
|
|
|
|
|
|
|
ToolTipManager::self()->registerWidget(this);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
PanelToolBox::~PanelToolBox()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
2008-11-18 01:12:05 +01:00
|
|
|
void PanelToolBox::assignColors()
|
|
|
|
{
|
|
|
|
d->bgColor = Plasma::Theme::defaultTheme()->color(Plasma::Theme::BackgroundColor);
|
|
|
|
d->fgColor = Plasma::Theme::defaultTheme()->color(Plasma::Theme::TextColor);
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
QRectF PanelToolBox::boundingRect() const
|
|
|
|
{
|
2008-11-11 06:26:52 +01:00
|
|
|
QRectF r;
|
|
|
|
|
2008-11-18 01:12:05 +01:00
|
|
|
//Only Left,Right and Bottom supported, default to Right
|
2008-11-04 00:08:39 +01:00
|
|
|
if (corner() == ToolBox::Bottom) {
|
2008-11-18 01:12:05 +01:00
|
|
|
r = QRectF(0, 0, size() * 2, size());
|
2008-11-04 00:08:39 +01:00
|
|
|
} else if (corner() == ToolBox::Left) {
|
2008-11-11 06:26:52 +01:00
|
|
|
r = QRectF(0, 0, size(), size() * 2);
|
2008-11-04 00:08:39 +01:00
|
|
|
} else {
|
2008-11-18 01:12:05 +01:00
|
|
|
r = QRectF(0, 0, size(), size() * 2);
|
2008-11-11 06:26:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (parentItem()) {
|
|
|
|
QSizeF s = parentItem()->boundingRect().size();
|
|
|
|
|
|
|
|
if (r.height() > s.height()) {
|
|
|
|
r.setHeight(s.height());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r.width() > s.width()) {
|
|
|
|
r.setWidth(s.width());
|
|
|
|
}
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
2008-11-11 06:26:52 +01:00
|
|
|
|
|
|
|
return r;
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PanelToolBox::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
|
|
|
|
{
|
|
|
|
Q_UNUSED(option)
|
|
|
|
Q_UNUSED(widget)
|
|
|
|
|
|
|
|
const qreal progress = d->animFrame / size();
|
|
|
|
|
2009-01-28 22:07:38 +01:00
|
|
|
QRect backgroundRect;
|
2008-11-04 00:08:39 +01:00
|
|
|
QPoint gradientCenter;
|
2008-11-18 01:12:05 +01:00
|
|
|
QRectF rect = boundingRect();
|
2009-01-28 22:07:38 +01:00
|
|
|
QString cornerElement;
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
if (corner() == ToolBox::Bottom) {
|
2008-11-18 01:12:05 +01:00
|
|
|
gradientCenter = QPoint(rect.center().x(), rect.bottom());
|
2009-01-28 22:07:38 +01:00
|
|
|
cornerElement = "panel-south";
|
|
|
|
|
|
|
|
backgroundRect = d->background->elementRect(cornerElement).toRect();
|
|
|
|
backgroundRect.moveBottomLeft(shape().boundingRect().bottomLeft().toPoint());
|
|
|
|
} else if (corner() == ToolBox::Right) {
|
|
|
|
gradientCenter = QPoint(rect.right(), rect.center().y());
|
|
|
|
cornerElement = "panel-east";
|
|
|
|
|
|
|
|
backgroundRect = d->background->elementRect(cornerElement).toRect();
|
|
|
|
backgroundRect.moveTopRight(shape().boundingRect().topRight().toPoint());
|
2008-11-04 00:08:39 +01:00
|
|
|
} else {
|
2008-11-18 01:12:05 +01:00
|
|
|
gradientCenter = QPoint(rect.right(), rect.center().y());
|
2009-01-28 22:07:38 +01:00
|
|
|
cornerElement = "panel-west";
|
2008-11-04 00:08:39 +01:00
|
|
|
|
2009-01-28 22:07:38 +01:00
|
|
|
backgroundRect = d->background->elementRect(cornerElement).toRect();
|
|
|
|
backgroundRect.moveTopLeft(shape().boundingRect().topLeft().toPoint());
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
2009-01-28 22:07:38 +01:00
|
|
|
|
|
|
|
d->background->paint(painter, backgroundRect, cornerElement);
|
|
|
|
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
QRect iconRect;
|
|
|
|
|
2008-11-18 01:12:05 +01:00
|
|
|
//Only Left,Right and Bottom supported, default to Right
|
2008-11-04 00:08:39 +01:00
|
|
|
if (corner() == ToolBox::Bottom) {
|
|
|
|
iconRect = QRect(QPoint(gradientCenter.x() - iconSize().width() / 2,
|
2008-11-18 01:12:05 +01:00
|
|
|
(int)rect.bottom() - iconSize().height() - 2), iconSize());
|
2008-11-04 00:08:39 +01:00
|
|
|
} else if (corner() == ToolBox::Left) {
|
|
|
|
iconRect = QRect(QPoint(2, gradientCenter.y() - iconSize().height() / 2), iconSize());
|
|
|
|
} else {
|
2008-11-18 01:12:05 +01:00
|
|
|
iconRect = QRect(QPoint((int)rect.right() - iconSize().width() + 1,
|
2008-11-04 00:08:39 +01:00
|
|
|
gradientCenter.y() - iconSize().height() / 2), iconSize());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (qFuzzyCompare(qreal(1.0), progress)) {
|
|
|
|
d->icon.paint(painter, iconRect);
|
|
|
|
} else if (qFuzzyCompare(qreal(1.0), 1 + progress)) {
|
|
|
|
d->icon.paint(painter, iconRect, Qt::AlignCenter, QIcon::Disabled, QIcon::Off);
|
|
|
|
} else {
|
|
|
|
QPixmap disabled = d->icon.pixmap(iconSize(), QIcon::Disabled, QIcon::Off);
|
|
|
|
QPixmap enabled = d->icon.pixmap(iconSize());
|
|
|
|
QPixmap result = PaintUtils::transition(
|
|
|
|
d->icon.pixmap(iconSize(), QIcon::Disabled, QIcon::Off),
|
|
|
|
d->icon.pixmap(iconSize()), progress);
|
|
|
|
painter->drawPixmap(iconRect, result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QPainterPath PanelToolBox::shape() const
|
|
|
|
{
|
|
|
|
QPainterPath path;
|
|
|
|
int toolSize = size();// + (int)d->animFrame;
|
2008-11-18 01:12:05 +01:00
|
|
|
QRectF rect = boundingRect();
|
2008-11-04 00:08:39 +01:00
|
|
|
|
2008-11-18 01:12:05 +01:00
|
|
|
//Only Left,Right and Bottom supported, default to Right
|
2008-11-04 00:08:39 +01:00
|
|
|
if (corner() == ToolBox::Bottom) {
|
2008-11-18 01:12:05 +01:00
|
|
|
path.moveTo(rect.bottomLeft());
|
|
|
|
path.arcTo(QRectF(rect.center().x() - toolSize,
|
|
|
|
rect.bottom() - toolSize,
|
2008-11-04 00:08:39 +01:00
|
|
|
toolSize * 2,
|
|
|
|
toolSize * 2), 0, 180);
|
|
|
|
} else if (corner() == ToolBox::Left) {
|
2008-11-18 01:12:05 +01:00
|
|
|
path.arcTo(QRectF(rect.left(),
|
|
|
|
rect.center().y() - toolSize,
|
2008-11-04 00:08:39 +01:00
|
|
|
toolSize * 2,
|
|
|
|
toolSize * 2), 90, -180);
|
|
|
|
} else {
|
2008-11-18 01:12:05 +01:00
|
|
|
path.moveTo(rect.topRight());
|
|
|
|
path.arcTo(QRectF(rect.left(),
|
|
|
|
rect.center().y() - toolSize,
|
2008-11-04 00:08:39 +01:00
|
|
|
toolSize * 2,
|
|
|
|
toolSize * 2), 90, 180);
|
|
|
|
}
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PanelToolBox::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
|
|
|
|
{
|
2009-04-24 21:29:16 +02:00
|
|
|
highlight(true);
|
2008-11-04 00:08:39 +01:00
|
|
|
QGraphicsItem::hoverEnterEvent(event);
|
|
|
|
}
|
|
|
|
|
2009-04-24 21:29:16 +02:00
|
|
|
void PanelToolBox::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
|
2008-11-04 00:08:39 +01:00
|
|
|
{
|
2009-04-24 21:29:16 +02:00
|
|
|
//kDebug() << event->pos() << event->scenePos()
|
|
|
|
if (!showing()) {
|
|
|
|
highlight(false);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
2009-04-24 21:29:16 +02:00
|
|
|
QGraphicsItem::hoverLeaveEvent(event);
|
|
|
|
}
|
2008-11-04 00:08:39 +01:00
|
|
|
|
2009-04-24 21:29:16 +02:00
|
|
|
void PanelToolBox::showToolBox()
|
|
|
|
{
|
2008-11-04 00:08:39 +01:00
|
|
|
setShowing(true);
|
2009-04-24 21:29:16 +02:00
|
|
|
highlight(true);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
2009-04-24 21:29:16 +02:00
|
|
|
void PanelToolBox::hideToolBox()
|
2008-11-04 00:08:39 +01:00
|
|
|
{
|
2009-04-24 21:29:16 +02:00
|
|
|
setShowing(false);
|
|
|
|
highlight(false);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
2009-10-07 19:11:03 +02:00
|
|
|
void PanelToolBox::toolTipAboutToShow()
|
|
|
|
{
|
|
|
|
if (showing()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ToolTipContent c(i18n("Panel Tool Box"),
|
|
|
|
i18n("Click to access size, location and hiding controls as well as to add "
|
|
|
|
"new widgets to the panel."),
|
|
|
|
KIcon("plasma"));
|
|
|
|
c.setAutohide(false);
|
|
|
|
ToolTipManager::self()->setContent(this, c);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PanelToolBox::toolTipHidden()
|
|
|
|
{
|
|
|
|
ToolTipManager::self()->clearContent(this);
|
|
|
|
}
|
|
|
|
|
2009-04-24 21:29:16 +02:00
|
|
|
void PanelToolBox::highlight(bool highlighting)
|
2008-11-04 00:08:39 +01:00
|
|
|
{
|
2009-04-24 21:29:16 +02:00
|
|
|
if (d->highlighting == highlighting) {
|
2008-11-04 00:08:39 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Plasma::Animator *animdriver = Plasma::Animator::self();
|
|
|
|
|
|
|
|
if (d->animId) {
|
|
|
|
animdriver->stopCustomAnimation(d->animId);
|
|
|
|
}
|
|
|
|
|
2009-04-24 21:29:16 +02:00
|
|
|
d->highlighting = highlighting;
|
|
|
|
d->animId = animdriver->customAnimation(10, 240,
|
|
|
|
highlighting ? Plasma::Animator::EaseInCurve
|
|
|
|
: Plasma::Animator::EaseOutCurve,
|
|
|
|
this, "animate");
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PanelToolBox::animate(qreal progress)
|
|
|
|
{
|
2009-04-24 21:29:16 +02:00
|
|
|
if (d->highlighting) {
|
2008-11-04 00:08:39 +01:00
|
|
|
d->animFrame = size() * progress;
|
|
|
|
} else {
|
|
|
|
d->animFrame = size() * (1.0 - progress);
|
|
|
|
}
|
|
|
|
|
|
|
|
//kDebug() << "animating at" << progress << "for" << d->animFrame;
|
|
|
|
|
|
|
|
if (progress >= 1) {
|
|
|
|
d->animId = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PanelToolBox::toggle()
|
|
|
|
{
|
2009-04-24 21:29:16 +02:00
|
|
|
setShowing(!showing());
|
|
|
|
highlight(showing());
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} // plasma namespace
|
|
|
|
|
|
|
|
#include "paneltoolbox_p.moc"
|
|
|
|
|