use focusindicator to do button shadows too (perfectly retrocompatible by the way).

this makes buttons be correctly aligned in layouts

svn path=/trunk/KDE/kdelibs/; revision=1126591
This commit is contained in:
Marco Martin 2010-05-14 12:45:54 +00:00
parent 72931328b8
commit 1bbbe5f173
3 changed files with 69 additions and 34 deletions

View File

@ -20,6 +20,8 @@
#include "focusindicator_p.h"
#include <QGraphicsSceneResizeEvent>
#include <QPainter>
#include <QStyleOptionGraphicsItem>
#include <plasma/theme.h>
#include <plasma/framesvg.h>
@ -47,7 +49,16 @@ FocusIndicator::FocusIndicator(QGraphicsWidget *parent, QString widget)
m_fade->setTargetWidget(this);
m_fade->setProperty("startOpacity", 0.0);
m_fade->setProperty("targetOpacity", 1.0);
setOpacity(0);
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());
}
parent->installEventFilter(this);
connect(Plasma::Theme::defaultTheme(), SIGNAL(themeChanged()), SLOT(syncGeometry()));
@ -77,29 +88,42 @@ bool FocusIndicator::eventFilter(QObject *watched, QEvent *event)
}
if (!m_parent->hasFocus() && event->type() == QEvent::GraphicsSceneHoverEnter) {
m_background->setElementPrefix("hover");
m_prefix = m_customPrefix + "hover";
m_fade->setProperty("startOpacity", 0.0);
m_fade->setProperty("targetOpacity", 1.0);
syncGeometry();
m_fade->start();
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();
} else if (!m_parent->hasFocus() && event->type() == QEvent::GraphicsSceneHoverLeave) {
m_fade->setProperty("startOpacity", 1.0);
m_fade->setProperty("targetOpacity", 0.0);
m_fade->start();
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();
} else if (event->type() == QEvent::GraphicsSceneResize) {
syncGeometry();
} else if (event->type() == QEvent::FocusIn) {
m_background->setElementPrefix("focus");
m_prefix = m_customPrefix + "focus";
m_fade->setProperty("startOpacity", 0.0);
m_fade->setProperty("targetOpacity", 1.0);
syncGeometry();
m_fade->start();
m_hoverAnimation->stop();
m_hoverAnimation->setProperty("startPixmap", m_background->framePixmap());
m_background->setElementPrefix("focus");
m_hoverAnimation->setProperty("targetPixmap", m_background->framePixmap());
m_hoverAnimation->start();
} else if (!m_parent->isUnderMouse() && event->type() == QEvent::FocusOut) {
m_fade->setProperty("startOpacity", 1.0);
m_fade->setProperty("targetOpacity", 0.0);
m_fade->start();
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();
}
return false;
@ -107,10 +131,20 @@ bool FocusIndicator::eventFilter(QObject *watched, QEvent *event)
void FocusIndicator::resizeEvent(QGraphicsSceneResizeEvent *event)
{
m_background->setElementPrefix("shadow");
m_background->resizeFrame(event->newSize());
m_background->setElementPrefix("hover");
m_background->resizeFrame(event->newSize());
m_background->setElementPrefix("focus");
m_background->resizeFrame(event->newSize());
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());
}
void FocusIndicator::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
@ -118,27 +152,28 @@ void FocusIndicator::paint(QPainter *painter, const QStyleOptionGraphicsItem *op
Q_UNUSED(option)
Q_UNUSED(widget)
if (m_background->hasElementPrefix(m_prefix)) {
m_background->paintFrame(painter);
} else {
m_background->paint(painter, QPoint(0,0), m_prefix);
}
painter->drawPixmap(
option->rect,
m_hoverAnimation->property("currentPixmap").value<QPixmap>());
}
void FocusIndicator::syncGeometry()
{
QRectF geom;
if (!m_customGeometry.isNull()) {
if (!m_customGeometry.isEmpty()) {
geom = m_customGeometry;
} else {
geom = m_parent->boundingRect();
}
if (m_background->hasElementPrefix(m_prefix)) {
//always take borders from hover to make it stable
m_background->setElementPrefix("hover");
qreal left, top, right, bottom;
m_background->getMargins(left, top, right, bottom);
m_background->setElementPrefix(m_prefix);
setGeometry(QRectF(geom.topLeft() + QPointF(-left, -top), geom.size() + QSize(left+right, top+bottom)));
} else {
} else if (m_background->hasElement(m_prefix)) {
QRectF elementRect = m_background->elementRect(m_prefix);
elementRect.moveCenter(geom.center());
setGeometry(elementRect);

View File

@ -52,6 +52,7 @@ private:
QGraphicsWidget *m_parent;
Plasma::FrameSvg *m_background;
Animation *m_fade;
Animation *m_hoverAnimation;
QRectF m_customGeometry;
QString m_prefix;
QString m_customPrefix;

View File

@ -39,6 +39,7 @@
#include "animator.h"
#include "paintutils.h"
#include "private/actionwidgetinterface_p.h"
#include <plasma/private/focusindicator_p.h>
#include "animations/animation.h"
namespace Plasma
@ -178,6 +179,8 @@ PushButton::PushButton(QGraphicsWidget *parent)
setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
FocusIndicator *focusIndicator = new FocusIndicator(this, "widgets/button");
d->syncBorders();
setAcceptHoverEvents(true);
connect(Plasma::Theme::defaultTheme(), SIGNAL(themeChanged()), SLOT(syncBorders()));
@ -362,7 +365,7 @@ void PushButton::paint(QPainter *painter,
//if is under mouse draw the animated glow overlay
if (!nativeWidget()->isDown() && !nativeWidget()->isChecked() && isEnabled() && acceptHoverEvents()) {
if (!nativeWidget()->isDown() && !nativeWidget()->isChecked() && isEnabled() && acceptHoverEvents() && d->background->hasElementPrefix("active")) {
if (d->hoverAnimation->state() == QAbstractAnimation::Running && !isUnderMouse() && !nativeWidget()->isDefault()) {
d->background->setElementPrefix("active");
d->background->paintFrame(painter, d->activeRect.topLeft());
@ -375,10 +378,6 @@ void PushButton::paint(QPainter *painter,
d->background->paintFrame(painter);
}
if (nativeWidget()->hasFocus()) {
d->background->setElementPrefix("focus");
d->background->paintFrame(painter, d->activeRect.topLeft());
}
painter->setPen(Plasma::Theme::defaultTheme()->color(Theme::ButtonTextColor));
@ -457,7 +456,7 @@ void PushButton::paint(QPainter *painter,
void PushButton::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
if (nativeWidget()->isDown()) {
if (nativeWidget()->isDown() || d->background->hasElementPrefix("hover")) {
return;
}
@ -485,7 +484,7 @@ void PushButton::changeEvent(QEvent *event)
void PushButton::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
if (nativeWidget()->isDown()) {
if (nativeWidget()->isDown() || d->background->hasElementPrefix("hover")) {
return;
}