2009-08-30 23:17:52 +02:00
|
|
|
/***************************************************************************
|
|
|
|
* Copyright 2009 by Alessandro Diaferia <alediaferia@gmail.com> *
|
|
|
|
* Copyright 2009 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 General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, 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 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 "itembackground.h"
|
|
|
|
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QStyleOptionGraphicsItem>
|
|
|
|
|
2009-08-31 02:18:14 +02:00
|
|
|
#include <kdebug.h>
|
2009-08-30 23:17:52 +02:00
|
|
|
|
2009-08-31 02:18:14 +02:00
|
|
|
#include <plasma/framesvg.h>
|
|
|
|
#include <plasma/animator.h>
|
|
|
|
#include <plasma/theme.h>
|
2009-08-30 23:41:06 +02:00
|
|
|
namespace Plasma
|
|
|
|
{
|
2009-08-30 23:17:52 +02:00
|
|
|
|
|
|
|
class ItemBackgroundPrivate
|
|
|
|
{
|
|
|
|
public:
|
2009-08-31 11:19:00 +02:00
|
|
|
ItemBackgroundPrivate(ItemBackground *parent)
|
2009-08-31 12:04:17 +02:00
|
|
|
: q(parent),
|
|
|
|
target(0)
|
2009-08-30 23:17:52 +02:00
|
|
|
{}
|
2009-08-31 11:19:00 +02:00
|
|
|
|
|
|
|
void animationUpdate(qreal progress);
|
2009-08-31 12:13:44 +02:00
|
|
|
void targetDestroyed(QObject*);
|
2009-08-31 14:14:47 +02:00
|
|
|
void frameSvgChanged();
|
2009-08-31 11:19:00 +02:00
|
|
|
|
2009-08-31 14:14:47 +02:00
|
|
|
ItemBackground * const q;
|
2009-08-31 12:04:17 +02:00
|
|
|
QGraphicsItem *target;
|
2009-08-30 23:17:52 +02:00
|
|
|
Plasma::FrameSvg *frameSvg;
|
|
|
|
QRectF oldGeometry;
|
|
|
|
QRectF newGeometry;
|
|
|
|
int animId;
|
|
|
|
qreal opacity;
|
|
|
|
bool fading;
|
|
|
|
bool fadeIn;
|
2009-08-31 02:47:24 +02:00
|
|
|
bool immediate;
|
2009-08-30 23:17:52 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
ItemBackground::ItemBackground(QGraphicsWidget *parent)
|
2009-08-31 01:34:25 +02:00
|
|
|
: QGraphicsWidget(parent),
|
2009-08-31 11:19:00 +02:00
|
|
|
d(new ItemBackgroundPrivate(this))
|
2009-08-30 23:17:52 +02:00
|
|
|
{
|
2009-08-31 14:14:47 +02:00
|
|
|
setCacheMode(DeviceCoordinateCache);
|
|
|
|
setFlag(ItemIsMovable, false);
|
|
|
|
setFlag(ItemIsSelectable, false);
|
|
|
|
setFlag(ItemIsFocusable, false);
|
|
|
|
|
2009-08-30 23:17:52 +02:00
|
|
|
d->frameSvg = new Plasma::FrameSvg(this);
|
|
|
|
d->animId = 0;
|
|
|
|
d->opacity = 1;
|
|
|
|
d->fading = false;
|
|
|
|
d->fadeIn = false;
|
2009-08-31 02:47:24 +02:00
|
|
|
d->immediate = false;
|
2009-08-30 23:17:52 +02:00
|
|
|
|
|
|
|
d->frameSvg->setImagePath("widgets/viewitem");
|
|
|
|
d->frameSvg->setEnabledBorders(Plasma::FrameSvg::AllBorders);
|
|
|
|
d->frameSvg->setCacheAllRenderedFrames(true);
|
|
|
|
d->frameSvg->setElementPrefix("hover");
|
2009-08-31 14:29:44 +02:00
|
|
|
|
|
|
|
qreal l, t, r, b;
|
|
|
|
d->frameSvg->getMargins(l, t, r, b);
|
|
|
|
setContentsMargins(l, t, r, b);
|
|
|
|
|
2009-08-31 14:14:47 +02:00
|
|
|
connect(d->frameSvg, SIGNAL(repaintNeeded()), this, SLOT(frameSvgChanged()));
|
2009-08-30 23:17:52 +02:00
|
|
|
|
|
|
|
setAcceptedMouseButtons(0);
|
2009-08-30 23:56:33 +02:00
|
|
|
setZValue(-800);
|
2009-08-30 23:17:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ItemBackground::~ItemBackground()
|
2009-08-31 09:22:56 +02:00
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
2009-08-30 23:17:52 +02:00
|
|
|
|
|
|
|
void ItemBackground::setTarget(const QRectF &newGeometry)
|
|
|
|
{
|
|
|
|
d->oldGeometry = geometry();
|
2009-08-31 14:29:44 +02:00
|
|
|
d->newGeometry = newGeometry;
|
2009-08-30 23:17:52 +02:00
|
|
|
|
2009-09-18 23:24:14 +02:00
|
|
|
if (!isVisible() && (!d->target || !d->target->isVisible())) {
|
2009-08-30 23:56:33 +02:00
|
|
|
setGeometry(d->newGeometry);
|
|
|
|
return;
|
|
|
|
}
|
2009-08-31 11:19:00 +02:00
|
|
|
|
2009-08-30 23:17:52 +02:00
|
|
|
QGraphicsWidget *pw = parentWidget();
|
|
|
|
if (pw) {
|
|
|
|
d->newGeometry = d->newGeometry.intersected(QRectF(QPointF(0,0), pw->size()));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (d->animId != 0) {
|
|
|
|
Plasma::Animator::self()->stopCustomAnimation(d->animId);
|
|
|
|
}
|
|
|
|
|
2009-09-18 23:24:14 +02:00
|
|
|
if (d->target && d->target->isVisible() && !isVisible()) {
|
|
|
|
setGeometry(newGeometry);
|
|
|
|
d->oldGeometry = newGeometry;
|
|
|
|
show();
|
|
|
|
}
|
|
|
|
|
2009-08-30 23:17:52 +02:00
|
|
|
d->fading = false;
|
|
|
|
d->opacity = 1;
|
|
|
|
d->animId = Plasma::Animator::self()->customAnimation(
|
|
|
|
15, 250, Plasma::Animator::EaseInOutCurve, this, "animationUpdate");
|
|
|
|
}
|
|
|
|
|
|
|
|
void ItemBackground::setTargetItem(QGraphicsItem *target)
|
|
|
|
{
|
2009-08-31 12:04:17 +02:00
|
|
|
if (d->target && d->target != target) {
|
|
|
|
d->target->removeSceneEventFilter(this);
|
2009-08-31 12:13:44 +02:00
|
|
|
|
|
|
|
QObject *obj = 0;
|
2009-09-19 15:30:58 +02:00
|
|
|
if (d->target->isWidget()) {
|
|
|
|
obj = static_cast<QGraphicsWidget*>(d->target);
|
2009-08-31 12:13:44 +02:00
|
|
|
} else {
|
2009-09-19 15:30:58 +02:00
|
|
|
obj = dynamic_cast<QObject *>(d->target);
|
2009-08-31 12:13:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (obj) {
|
2009-09-19 15:30:58 +02:00
|
|
|
disconnect(obj, 0, this, 0);
|
2009-08-31 12:13:44 +02:00
|
|
|
}
|
2009-08-31 12:04:17 +02:00
|
|
|
}
|
|
|
|
|
2009-09-21 08:17:04 +02:00
|
|
|
d->target = target;
|
2009-08-31 11:19:00 +02:00
|
|
|
if (target) {
|
2009-09-01 01:47:11 +02:00
|
|
|
setZValue(target->zValue() - 1);
|
|
|
|
setParentItem(target->parentItem());
|
2009-08-31 11:19:00 +02:00
|
|
|
QRectF rect = target->boundingRect();
|
|
|
|
rect.moveTopLeft(target->pos());
|
|
|
|
setTarget(rect);
|
2009-08-31 12:04:17 +02:00
|
|
|
|
|
|
|
if (d->target != target) {
|
2009-09-19 17:50:06 +02:00
|
|
|
target->installSceneEventFilter(this);
|
2009-08-31 12:13:44 +02:00
|
|
|
|
|
|
|
QObject *obj = 0;
|
|
|
|
if (target->isWidget()) {
|
|
|
|
obj = static_cast<QGraphicsWidget*>(target);
|
|
|
|
} else {
|
|
|
|
obj = dynamic_cast<QObject *>(target);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (obj) {
|
|
|
|
connect(obj, SIGNAL(destroyed(QObject*)), this, SLOT(targetDestroyed(QObject*)));
|
|
|
|
}
|
2009-08-31 12:04:17 +02:00
|
|
|
}
|
2009-08-31 11:19:00 +02:00
|
|
|
} else {
|
|
|
|
hide();
|
|
|
|
}
|
2009-08-30 23:17:52 +02:00
|
|
|
}
|
|
|
|
|
2009-08-31 12:04:17 +02:00
|
|
|
bool ItemBackground::sceneEventFilter(QGraphicsItem *watched, QEvent *event)
|
|
|
|
{
|
|
|
|
if (watched == d->target) {
|
|
|
|
if (event->type() == QEvent::GraphicsSceneResize ||
|
|
|
|
event->type() == QEvent::GraphicsSceneMove) {
|
|
|
|
setTargetItem(d->target);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-08-31 13:35:37 +02:00
|
|
|
void ItemBackground::resizeEvent(QGraphicsSceneResizeEvent *)
|
|
|
|
{
|
|
|
|
d->frameSvg->resizeFrame(size());
|
|
|
|
}
|
|
|
|
|
2009-08-30 23:17:52 +02:00
|
|
|
QVariant ItemBackground::itemChange(GraphicsItemChange change, const QVariant &value)
|
|
|
|
{
|
2009-08-31 02:47:24 +02:00
|
|
|
if (d->immediate) {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2009-08-30 23:17:52 +02:00
|
|
|
if (change == ItemVisibleChange) {
|
|
|
|
bool visible = value.toBool();
|
2009-08-31 00:44:47 +02:00
|
|
|
bool retVisible = visible;
|
|
|
|
if (visible == isVisible() || d->animId == 0) {
|
|
|
|
retVisible = true;
|
|
|
|
}
|
2009-08-30 23:17:52 +02:00
|
|
|
d->fading = true;
|
|
|
|
d->fadeIn = visible;
|
|
|
|
|
|
|
|
if (d->animId != 0) {
|
|
|
|
Plasma::Animator::self()->stopCustomAnimation(d->animId);
|
|
|
|
}
|
|
|
|
|
|
|
|
d->animId = Plasma::Animator::self()->customAnimation(
|
|
|
|
10, 250, Plasma::Animator::EaseInCurve, this, "animationUpdate");
|
2009-08-31 00:44:47 +02:00
|
|
|
return retVisible;
|
2009-08-30 23:17:52 +02:00
|
|
|
}
|
2009-08-31 00:44:47 +02:00
|
|
|
return value;
|
2009-08-30 23:17:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ItemBackground::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
|
|
|
|
{
|
|
|
|
Q_UNUSED(widget)
|
|
|
|
|
|
|
|
if (qFuzzyCompare(d->opacity, (qreal)1.0)) {
|
|
|
|
d->frameSvg->paintFrame(painter, option->rect.topLeft());
|
|
|
|
} else if (qFuzzyCompare(d->opacity+1, (qreal)1.0)) {
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
QPixmap framePix = d->frameSvg->framePixmap();
|
|
|
|
QPainter bufferPainter(&framePix);
|
|
|
|
bufferPainter.setCompositionMode(QPainter::CompositionMode_DestinationIn);
|
|
|
|
bufferPainter.fillRect(framePix.rect(), QColor(0, 0, 0, 255 * d->opacity));
|
|
|
|
bufferPainter.end();
|
|
|
|
painter->drawPixmap(framePix.rect(), framePix, framePix.rect());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-31 11:19:00 +02:00
|
|
|
void ItemBackgroundPrivate::animationUpdate(qreal progress)
|
2009-08-30 23:17:52 +02:00
|
|
|
{
|
|
|
|
if (progress == 1) {
|
2009-08-31 11:19:00 +02:00
|
|
|
animId = 0;
|
2009-08-30 23:17:52 +02:00
|
|
|
}
|
|
|
|
|
2009-08-31 11:19:00 +02:00
|
|
|
if (fading) {
|
|
|
|
opacity = fadeIn?progress:1-progress;
|
|
|
|
if (!fadeIn && qFuzzyCompare(opacity+1, (qreal)1.0)) {
|
|
|
|
immediate = true;
|
|
|
|
q->hide();
|
|
|
|
immediate = false;
|
2009-08-30 23:17:52 +02:00
|
|
|
}
|
2009-09-18 23:24:14 +02:00
|
|
|
} else if (oldGeometry != newGeometry) {
|
2009-08-31 11:19:00 +02:00
|
|
|
q->setGeometry(oldGeometry.x() + (newGeometry.x() - oldGeometry.x()) * progress,
|
|
|
|
oldGeometry.y() + (newGeometry.y() - oldGeometry.y()) * progress,
|
|
|
|
oldGeometry.width() + (newGeometry.width() - oldGeometry.width()) * progress,
|
|
|
|
oldGeometry.height() + (newGeometry.height() - oldGeometry.height()) * progress);
|
2009-08-30 23:17:52 +02:00
|
|
|
}
|
2009-08-31 11:19:00 +02:00
|
|
|
|
|
|
|
q->update();
|
2009-08-30 23:17:52 +02:00
|
|
|
}
|
2009-08-30 23:41:06 +02:00
|
|
|
|
2009-08-31 12:13:44 +02:00
|
|
|
void ItemBackgroundPrivate::targetDestroyed(QObject*)
|
|
|
|
{
|
2009-09-21 07:26:12 +02:00
|
|
|
target = 0;
|
2009-08-31 12:13:44 +02:00
|
|
|
q->setTargetItem(0);
|
|
|
|
}
|
|
|
|
|
2009-08-31 14:14:47 +02:00
|
|
|
void ItemBackgroundPrivate::frameSvgChanged()
|
|
|
|
{
|
|
|
|
qreal l, t, r, b;
|
|
|
|
frameSvg->getMargins(l, t, r, b);
|
|
|
|
q->setContentsMargins(l, t, r, b);
|
|
|
|
q->update();
|
|
|
|
emit q->appearanceChanged();
|
2009-08-30 23:41:06 +02:00
|
|
|
}
|
|
|
|
|
2009-08-31 14:14:47 +02:00
|
|
|
} // Plasma namespace
|
|
|
|
|
|
|
|
|
2009-08-31 11:19:00 +02:00
|
|
|
#include "itembackground.moc"
|
|
|
|
|
|
|
|
|