2009-10-19 23:13:54 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2009 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 "focusindicator_p.h"
|
|
|
|
|
|
|
|
#include <QGraphicsSceneResizeEvent>
|
2010-05-14 14:45:54 +02:00
|
|
|
#include <QPainter>
|
|
|
|
#include <QStyleOptionGraphicsItem>
|
2009-10-19 23:13:54 +02:00
|
|
|
|
|
|
|
#include <plasma/theme.h>
|
|
|
|
#include <plasma/framesvg.h>
|
2009-10-22 19:39:43 +02:00
|
|
|
|
|
|
|
#include <plasma/animator.h>
|
2009-10-19 23:13:54 +02:00
|
|
|
|
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
2009-10-22 19:39:43 +02:00
|
|
|
FocusIndicator::FocusIndicator(QGraphicsWidget *parent, QString widget)
|
2009-10-19 23:13:54 +02:00
|
|
|
: QGraphicsWidget(parent),
|
2009-10-22 19:39:43 +02:00
|
|
|
m_parent(parent),
|
2010-05-14 20:34:33 +02:00
|
|
|
m_background(0)
|
2009-10-19 23:13:54 +02:00
|
|
|
{
|
|
|
|
setFlag(QGraphicsItem::ItemStacksBehindParent);
|
|
|
|
setAcceptsHoverEvents(true);
|
2009-10-22 19:39:43 +02:00
|
|
|
|
2009-10-19 23:13:54 +02:00
|
|
|
m_background = new Plasma::FrameSvg(this);
|
2009-10-22 19:39:43 +02:00
|
|
|
m_background->setImagePath(widget);
|
2009-10-19 23:13:54 +02:00
|
|
|
m_background->setElementPrefix("hover");
|
|
|
|
m_background->setCacheAllRenderedFrames(true);
|
|
|
|
|
2009-10-22 19:39:43 +02:00
|
|
|
m_fade = Animator::create(Animator::FadeAnimation, this);
|
2010-01-04 10:19:08 +01:00
|
|
|
m_fade->setTargetWidget(this);
|
2009-10-22 19:39:43 +02:00
|
|
|
m_fade->setProperty("startOpacity", 0.0);
|
|
|
|
m_fade->setProperty("targetOpacity", 1.0);
|
2010-05-14 14:45:54 +02:00
|
|
|
|
|
|
|
m_hoverAnimation = Animator::create(Animator::PixmapTransitionAnimation);
|
|
|
|
m_hoverAnimation->setProperty("duration", 250);
|
|
|
|
m_hoverAnimation->setTargetWidget(this);
|
|
|
|
if (m_background->hasElementPrefix("shadow")) {
|
|
|
|
m_background->setElementPrefix("shadow");
|
|
|
|
m_prefix = "shadow";
|
|
|
|
syncGeometry();
|
|
|
|
m_hoverAnimation->setProperty("startPixmap", m_background->framePixmap());
|
|
|
|
}
|
2009-10-19 23:13:54 +02:00
|
|
|
|
|
|
|
parent->installEventFilter(this);
|
|
|
|
connect(Plasma::Theme::defaultTheme(), SIGNAL(themeChanged()), SLOT(syncGeometry()));
|
2009-10-20 00:19:04 +02:00
|
|
|
syncGeometry();
|
2009-10-19 23:13:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
FocusIndicator::~FocusIndicator()
|
|
|
|
{
|
2010-05-17 12:44:34 +02:00
|
|
|
m_parent->removeEventFilter(this);
|
2009-10-22 19:39:43 +02:00
|
|
|
delete m_fade;
|
2009-10-19 23:13:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void FocusIndicator::setCustomGeometry(const QRectF &geometry)
|
|
|
|
{
|
|
|
|
m_customGeometry = geometry;
|
|
|
|
syncGeometry();
|
|
|
|
}
|
|
|
|
|
2009-11-27 14:42:00 +01:00
|
|
|
void FocusIndicator::setCustomPrefix(const QString &prefix)
|
|
|
|
{
|
|
|
|
m_customPrefix = prefix;
|
|
|
|
}
|
|
|
|
|
2009-10-19 23:13:54 +02:00
|
|
|
bool FocusIndicator::eventFilter(QObject *watched, QEvent *event)
|
|
|
|
{
|
2010-05-17 12:44:34 +02:00
|
|
|
if (static_cast<QGraphicsWidget *>(watched) != m_parent || !m_parent) {
|
2009-10-19 23:13:54 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!m_parent->hasFocus() && event->type() == QEvent::GraphicsSceneHoverEnter) {
|
2009-11-27 14:42:00 +01:00
|
|
|
m_prefix = m_customPrefix + "hover";
|
2009-12-21 18:30:21 +01:00
|
|
|
syncGeometry();
|
2010-05-14 14:45:54 +02:00
|
|
|
m_hoverAnimation->stop();
|
|
|
|
m_background->setElementPrefix("shadow");
|
|
|
|
m_hoverAnimation->setProperty("startPixmap", m_background->framePixmap());
|
|
|
|
m_background->setElementPrefix("hover");
|
|
|
|
m_hoverAnimation->setProperty("targetPixmap", m_background->framePixmap());
|
|
|
|
m_hoverAnimation->start();
|
2009-10-19 23:13:54 +02:00
|
|
|
} else if (!m_parent->hasFocus() && event->type() == QEvent::GraphicsSceneHoverLeave) {
|
2010-05-14 14:45:54 +02:00
|
|
|
m_prefix = m_customPrefix + "shadow";
|
|
|
|
syncGeometry();
|
|
|
|
m_hoverAnimation->stop();
|
|
|
|
m_background->setElementPrefix("hover");
|
|
|
|
m_hoverAnimation->setProperty("startPixmap", m_background->framePixmap());
|
|
|
|
m_background->setElementPrefix("shadow");
|
|
|
|
m_hoverAnimation->setProperty("targetPixmap", m_background->framePixmap());
|
|
|
|
m_hoverAnimation->start();
|
2009-10-19 23:13:54 +02:00
|
|
|
} else if (event->type() == QEvent::GraphicsSceneResize) {
|
|
|
|
syncGeometry();
|
|
|
|
} else if (event->type() == QEvent::FocusIn) {
|
2009-11-27 14:42:00 +01:00
|
|
|
m_prefix = m_customPrefix + "focus";
|
2009-12-21 18:30:21 +01:00
|
|
|
syncGeometry();
|
2010-05-14 14:45:54 +02:00
|
|
|
m_hoverAnimation->stop();
|
|
|
|
m_hoverAnimation->setProperty("startPixmap", m_background->framePixmap());
|
|
|
|
m_background->setElementPrefix("focus");
|
|
|
|
m_hoverAnimation->setProperty("targetPixmap", m_background->framePixmap());
|
|
|
|
m_hoverAnimation->start();
|
2009-10-19 23:13:54 +02:00
|
|
|
} else if (!m_parent->isUnderMouse() && event->type() == QEvent::FocusOut) {
|
2010-05-14 14:45:54 +02:00
|
|
|
m_prefix = m_customPrefix + "shadow";
|
|
|
|
syncGeometry();
|
|
|
|
m_hoverAnimation->stop();
|
|
|
|
m_background->setElementPrefix("focus");
|
|
|
|
m_hoverAnimation->setProperty("startPixmap", m_background->framePixmap());
|
|
|
|
m_background->setElementPrefix("shadow");
|
|
|
|
m_hoverAnimation->setProperty("targetPixmap", m_background->framePixmap());
|
|
|
|
m_hoverAnimation->start();
|
2009-10-19 23:13:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FocusIndicator::resizeEvent(QGraphicsSceneResizeEvent *event)
|
|
|
|
{
|
2010-05-14 14:45:54 +02:00
|
|
|
m_background->setElementPrefix("shadow");
|
|
|
|
m_background->resizeFrame(event->newSize());
|
2009-11-26 18:26:06 +01:00
|
|
|
m_background->setElementPrefix("hover");
|
|
|
|
m_background->resizeFrame(event->newSize());
|
|
|
|
m_background->setElementPrefix("focus");
|
2009-10-19 23:13:54 +02:00
|
|
|
m_background->resizeFrame(event->newSize());
|
2010-05-14 14:45:54 +02:00
|
|
|
|
|
|
|
if (m_hoverAnimation->state() == QAbstractAnimation::Running) {
|
|
|
|
m_hoverAnimation->stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
m_background->setElementPrefix(m_prefix);
|
|
|
|
m_hoverAnimation->setProperty("startPixmap", m_background->framePixmap());
|
|
|
|
m_hoverAnimation->setProperty("targetPixmap", m_background->framePixmap());
|
2009-10-19 23:13:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void FocusIndicator::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
|
|
|
|
{
|
2009-11-12 20:31:39 +01:00
|
|
|
Q_UNUSED(option)
|
|
|
|
Q_UNUSED(widget)
|
|
|
|
|
2010-05-14 14:45:54 +02:00
|
|
|
painter->drawPixmap(
|
|
|
|
option->rect,
|
|
|
|
m_hoverAnimation->property("currentPixmap").value<QPixmap>());
|
2009-10-19 23:13:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void FocusIndicator::syncGeometry()
|
|
|
|
{
|
|
|
|
QRectF geom;
|
2010-05-14 14:45:54 +02:00
|
|
|
if (!m_customGeometry.isEmpty()) {
|
2009-10-19 23:13:54 +02:00
|
|
|
geom = m_customGeometry;
|
|
|
|
} else {
|
2009-10-20 00:19:04 +02:00
|
|
|
geom = m_parent->boundingRect();
|
2009-10-19 23:13:54 +02:00
|
|
|
}
|
|
|
|
|
2009-11-12 20:31:39 +01:00
|
|
|
if (m_background->hasElementPrefix(m_prefix)) {
|
2010-05-14 14:45:54 +02:00
|
|
|
//always take borders from hover to make it stable
|
|
|
|
m_background->setElementPrefix("hover");
|
2009-11-12 20:31:39 +01:00
|
|
|
qreal left, top, right, bottom;
|
|
|
|
m_background->getMargins(left, top, right, bottom);
|
2010-05-14 14:45:54 +02:00
|
|
|
m_background->setElementPrefix(m_prefix);
|
2009-11-12 20:31:39 +01:00
|
|
|
setGeometry(QRectF(geom.topLeft() + QPointF(-left, -top), geom.size() + QSize(left+right, top+bottom)));
|
2010-05-14 14:45:54 +02:00
|
|
|
} else if (m_background->hasElement(m_prefix)) {
|
2009-11-12 20:31:39 +01:00
|
|
|
QRectF elementRect = m_background->elementRect(m_prefix);
|
|
|
|
elementRect.moveCenter(geom.center());
|
|
|
|
setGeometry(elementRect);
|
|
|
|
}
|
2009-10-19 23:13:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#include <focusindicator_p.moc>
|
|
|
|
|