now PushButton has a pretty animated svg theme (uses button.svgz)
svn path=/trunk/KDE/kdebase/workspace/libs/plasma/; revision=838998
This commit is contained in:
parent
34aa5d4185
commit
9eb10b594f
@ -20,13 +20,18 @@
|
|||||||
#include "pushbutton.h"
|
#include "pushbutton.h"
|
||||||
|
|
||||||
#include <KPushButton>
|
#include <KPushButton>
|
||||||
|
#include <QStyleOptionGraphicsItem>
|
||||||
#include <QPainter>
|
#include <QPainter>
|
||||||
#include <QDir>
|
#include <QDir>
|
||||||
|
#include <QApplication>
|
||||||
|
|
||||||
#include <KMimeType>
|
#include <KMimeType>
|
||||||
|
#include <KIconEffect>
|
||||||
|
|
||||||
#include "theme.h"
|
#include "theme.h"
|
||||||
#include "svg.h"
|
#include "svg.h"
|
||||||
|
#include "panelsvg.h"
|
||||||
|
#include "animator.h"
|
||||||
|
|
||||||
namespace Plasma
|
namespace Plasma
|
||||||
{
|
{
|
||||||
@ -34,8 +39,12 @@ namespace Plasma
|
|||||||
class PushButtonPrivate
|
class PushButtonPrivate
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
PushButtonPrivate()
|
PushButtonPrivate(PushButton *pushButton)
|
||||||
: svg(0)
|
: q(pushButton),
|
||||||
|
background(0),
|
||||||
|
activeBackgroundPixmap(0),
|
||||||
|
animId(0),
|
||||||
|
svg(0)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -64,19 +73,91 @@ public:
|
|||||||
static_cast<KPushButton*>(q->widget())->setIcon(QIcon(pm));
|
static_cast<KPushButton*>(q->widget())->setIcon(QIcon(pm));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void renderActiveBackgroundPixmap();
|
||||||
|
void syncActiveRect();
|
||||||
|
void syncBorders();
|
||||||
|
void elementAnimationFinished(int id);
|
||||||
|
|
||||||
|
PushButton *q;
|
||||||
|
|
||||||
|
PanelSvg *background;
|
||||||
|
QPixmap *activeBackgroundPixmap;
|
||||||
|
int animId;
|
||||||
|
QRectF activeRect;
|
||||||
|
|
||||||
QString imagePath;
|
QString imagePath;
|
||||||
QString absImagePath;
|
QString absImagePath;
|
||||||
Svg *svg;
|
Svg *svg;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
void PushButtonPrivate::renderActiveBackgroundPixmap()
|
||||||
|
{
|
||||||
|
background->setElementPrefix("active");
|
||||||
|
|
||||||
|
activeBackgroundPixmap = new QPixmap(activeRect.size().toSize());
|
||||||
|
activeBackgroundPixmap->fill(Qt::transparent);
|
||||||
|
|
||||||
|
QPainter painter(activeBackgroundPixmap);
|
||||||
|
background->paintPanel(&painter);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PushButtonPrivate::syncActiveRect()
|
||||||
|
{
|
||||||
|
background->setElementPrefix("normal");
|
||||||
|
|
||||||
|
qreal left, top, right, bottom;
|
||||||
|
background->getMargins(left, top, right, bottom);
|
||||||
|
|
||||||
|
background->setElementPrefix("active");
|
||||||
|
qreal activeLeft, activeTop, activeRight, activeBottom;
|
||||||
|
background->getMargins(activeLeft, activeTop, activeRight, activeBottom);
|
||||||
|
|
||||||
|
activeRect = QRectF(QPointF(0,0), q->size());
|
||||||
|
activeRect.adjust(left - activeLeft, top - activeTop, -(right - activeRight), -(bottom - activeBottom));
|
||||||
|
|
||||||
|
background->setElementPrefix("normal");
|
||||||
|
}
|
||||||
|
|
||||||
|
void PushButtonPrivate::syncBorders()
|
||||||
|
{
|
||||||
|
//set margins from the normal element
|
||||||
|
qreal left, top, right, bottom;
|
||||||
|
|
||||||
|
background->setElementPrefix("normal");
|
||||||
|
background->getMargins(left, top, right, bottom);
|
||||||
|
q->setContentsMargins(left, top, right, bottom);
|
||||||
|
|
||||||
|
//calc the rect for the over effect
|
||||||
|
syncActiveRect();
|
||||||
|
}
|
||||||
|
|
||||||
|
void PushButtonPrivate::elementAnimationFinished(int id)
|
||||||
|
{
|
||||||
|
if (id == animId) {
|
||||||
|
animId = -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
PushButton::PushButton(QGraphicsWidget *parent)
|
PushButton::PushButton(QGraphicsWidget *parent)
|
||||||
: QGraphicsProxyWidget(parent),
|
: QGraphicsProxyWidget(parent),
|
||||||
d(new PushButtonPrivate)
|
d(new PushButtonPrivate(this))
|
||||||
{
|
{
|
||||||
KPushButton* native = new KPushButton;
|
KPushButton* native = new KPushButton;
|
||||||
connect(native, SIGNAL(clicked()), this, SIGNAL(clicked()));
|
connect(native, SIGNAL(clicked()), this, SIGNAL(clicked()));
|
||||||
setWidget(native);
|
setWidget(native);
|
||||||
native->setAttribute(Qt::WA_NoSystemBackground);
|
native->setAttribute(Qt::WA_NoSystemBackground);
|
||||||
|
|
||||||
|
d->background = new PanelSvg(this);
|
||||||
|
d->background->setImagePath("widgets/button");
|
||||||
|
d->background->setCacheAllRenderedPanels(true);
|
||||||
|
d->background->setElementPrefix("normal");
|
||||||
|
d->syncBorders();
|
||||||
|
setAcceptHoverEvents(true);
|
||||||
|
connect(Plasma::Animator::self(), SIGNAL(elementAnimationFinished(int)), this, SLOT(elementAnimationFinished(int)));
|
||||||
|
connect(Plasma::Theme::defaultTheme(), SIGNAL(themeChanged()), SLOT(syncBorders()));
|
||||||
}
|
}
|
||||||
|
|
||||||
PushButton::~PushButton()
|
PushButton::~PushButton()
|
||||||
@ -145,9 +226,175 @@ KPushButton* PushButton::nativeWidget() const
|
|||||||
void PushButton::resizeEvent(QGraphicsSceneResizeEvent *event)
|
void PushButton::resizeEvent(QGraphicsSceneResizeEvent *event)
|
||||||
{
|
{
|
||||||
d->setPixmap(this);
|
d->setPixmap(this);
|
||||||
|
|
||||||
|
if (d->background) {
|
||||||
|
//resize all four panels
|
||||||
|
d->background->setElementPrefix("normal");
|
||||||
|
d->background->resizePanel(size());
|
||||||
|
d->background->setElementPrefix("pressed");
|
||||||
|
d->background->resizePanel(size());
|
||||||
|
d->background->setElementPrefix("focus");
|
||||||
|
d->background->resizePanel(size());
|
||||||
|
|
||||||
|
d->syncActiveRect();
|
||||||
|
|
||||||
|
d->background->setElementPrefix("active");
|
||||||
|
d->background->resizePanel(d->activeRect.size());
|
||||||
|
|
||||||
|
d->background->setElementPrefix("normal");
|
||||||
|
|
||||||
|
if (d->activeBackgroundPixmap) {
|
||||||
|
delete d->activeBackgroundPixmap;
|
||||||
|
d->activeBackgroundPixmap = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
QGraphicsProxyWidget::resizeEvent(event);
|
QGraphicsProxyWidget::resizeEvent(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void PushButton::paint(QPainter *painter,
|
||||||
|
const QStyleOptionGraphicsItem *option,
|
||||||
|
QWidget *widget)
|
||||||
|
{
|
||||||
|
if (!styleSheet().isNull()) {
|
||||||
|
QGraphicsProxyWidget::paint(painter, option, widget);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
QPixmap *bufferPixmap = 0;
|
||||||
|
|
||||||
|
//Normal button, pressed or not
|
||||||
|
if (isEnabled()) {
|
||||||
|
if (nativeWidget()->isDown()) {
|
||||||
|
d->background->setElementPrefix("pressed");
|
||||||
|
} else {
|
||||||
|
d->background->setElementPrefix("normal");
|
||||||
|
}
|
||||||
|
d->background->paintPanel(painter);
|
||||||
|
//flat or disabled
|
||||||
|
} else if (!isEnabled() || nativeWidget()->isFlat()) {
|
||||||
|
bufferPixmap = new QPixmap(rect().size().toSize());
|
||||||
|
bufferPixmap->fill(Qt::transparent);
|
||||||
|
|
||||||
|
QPainter buffPainter(bufferPixmap);
|
||||||
|
d->background->paintPanel(&buffPainter);
|
||||||
|
buffPainter.setCompositionMode(QPainter::CompositionMode_DestinationIn);
|
||||||
|
buffPainter.fillRect(bufferPixmap->rect(), QColor(0,0,0,128));
|
||||||
|
|
||||||
|
painter->drawPixmap( 0, 0, *bufferPixmap);
|
||||||
|
}
|
||||||
|
|
||||||
|
//if is under mouse draw the animated glow overlay
|
||||||
|
if (!nativeWidget()->isDown() && isEnabled() && acceptHoverEvents()) {
|
||||||
|
if (d->animId != -1) {
|
||||||
|
painter->drawPixmap(d->activeRect.topLeft(), Plasma::Animator::self()->currentPixmap(d->animId) );
|
||||||
|
} else if (isUnderMouse() || nativeWidget()->isDefault()) {
|
||||||
|
if (d->activeBackgroundPixmap == 0) {
|
||||||
|
d->renderActiveBackgroundPixmap();
|
||||||
|
}
|
||||||
|
painter->drawPixmap( d->activeRect.topLeft(), *d->activeBackgroundPixmap );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (nativeWidget()->hasFocus()) {
|
||||||
|
d->background->setElementPrefix("focus");
|
||||||
|
d->background->paintPanel(painter);
|
||||||
|
}
|
||||||
|
|
||||||
|
//FIXME: theme will need a ButtonColor and ButtonTextColor
|
||||||
|
painter->setPen(Plasma::Theme::defaultTheme()->color(Theme::BackgroundColor));
|
||||||
|
|
||||||
|
|
||||||
|
if (nativeWidget()->isDown()) {
|
||||||
|
painter->translate(QPoint(1, 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
QRectF rect = contentsRect();
|
||||||
|
|
||||||
|
if (!nativeWidget()->icon().isNull()) {
|
||||||
|
QPixmap iconPix = nativeWidget()->icon().pixmap(rect.height(), rect.height());
|
||||||
|
if (!isEnabled()) {
|
||||||
|
KIconEffect *effect = KIconLoader::global()->iconEffect();
|
||||||
|
iconPix = effect->apply(iconPix, KIconLoader::Toolbar, KIconLoader::DisabledState);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (option->direction == Qt::LeftToRight) {
|
||||||
|
painter->drawPixmap(rect.topLeft(), iconPix);
|
||||||
|
rect.adjust(rect.height(), 0, 0, 0);
|
||||||
|
} else {
|
||||||
|
painter->drawPixmap(rect.topRight() - QPoint(rect.height(), 0), iconPix);
|
||||||
|
rect.adjust(0, 0, -rect.height(), 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//if there is not enough room for the text make it to fade out
|
||||||
|
QFontMetricsF fm(QApplication::font());
|
||||||
|
if (rect.width() < fm.width(nativeWidget()->text())) {
|
||||||
|
if (!bufferPixmap) {
|
||||||
|
bufferPixmap = new QPixmap(rect.size().toSize());
|
||||||
|
}
|
||||||
|
|
||||||
|
bufferPixmap->fill(Qt::transparent);
|
||||||
|
|
||||||
|
QPainter p(bufferPixmap);
|
||||||
|
p.setPen(painter->pen());
|
||||||
|
|
||||||
|
// Create the alpha gradient for the fade out effect
|
||||||
|
QLinearGradient alphaGradient(0, 0, 1, 0);
|
||||||
|
alphaGradient.setCoordinateMode(QGradient::ObjectBoundingMode);
|
||||||
|
if (option->direction == Qt::LeftToRight) {
|
||||||
|
alphaGradient.setColorAt(0, QColor(0, 0, 0, 255));
|
||||||
|
alphaGradient.setColorAt(1, QColor(0, 0, 0, 0));
|
||||||
|
p.drawText(bufferPixmap->rect(), Qt::AlignLeft|Qt::AlignVCenter, nativeWidget()->text() );
|
||||||
|
} else {
|
||||||
|
alphaGradient.setColorAt(0, QColor(0, 0, 0, 0));
|
||||||
|
alphaGradient.setColorAt(1, QColor(0, 0, 0, 255));
|
||||||
|
p.drawText(bufferPixmap->rect(), Qt::AlignRight|Qt::AlignVCenter, nativeWidget()->text() );
|
||||||
|
}
|
||||||
|
|
||||||
|
p.setCompositionMode(QPainter::CompositionMode_DestinationIn);
|
||||||
|
p.fillRect(bufferPixmap->rect(), alphaGradient);
|
||||||
|
|
||||||
|
painter->drawPixmap(rect.topLeft(), *bufferPixmap);
|
||||||
|
} else {
|
||||||
|
painter->drawText(rect, Qt::AlignCenter, nativeWidget()->text() );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void PushButton::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
|
||||||
|
{
|
||||||
|
if (d->animId != -1) {
|
||||||
|
Plasma::Animator::self()->stopElementAnimation(d->animId);
|
||||||
|
}
|
||||||
|
d->animId = Plasma::Animator::self()->animateElement(this, Plasma::Animator::AppearAnimation);
|
||||||
|
|
||||||
|
d->background->setElementPrefix("active");
|
||||||
|
|
||||||
|
if (!d->activeBackgroundPixmap) {
|
||||||
|
d->renderActiveBackgroundPixmap();
|
||||||
|
}
|
||||||
|
Plasma::Animator::self()->setInitialPixmap( d->animId, *d->activeBackgroundPixmap );
|
||||||
|
|
||||||
|
QGraphicsProxyWidget::hoverEnterEvent(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PushButton::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
|
||||||
|
{
|
||||||
|
if (d->animId != -1) {
|
||||||
|
Plasma::Animator::self()->stopElementAnimation(d->animId);
|
||||||
|
}
|
||||||
|
d->animId = Plasma::Animator::self()->animateElement(this, Plasma::Animator::DisappearAnimation);
|
||||||
|
|
||||||
|
d->background->setElementPrefix("active");
|
||||||
|
|
||||||
|
if (!d->activeBackgroundPixmap) {
|
||||||
|
d->renderActiveBackgroundPixmap();
|
||||||
|
}
|
||||||
|
Plasma::Animator::self()->setInitialPixmap( d->animId, *d->activeBackgroundPixmap );
|
||||||
|
|
||||||
|
QGraphicsProxyWidget::hoverLeaveEvent(event);
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace Plasma
|
} // namespace Plasma
|
||||||
|
|
||||||
#include <pushbutton.moc>
|
#include <pushbutton.moc>
|
||||||
|
@ -91,10 +91,19 @@ Q_SIGNALS:
|
|||||||
void clicked();
|
void clicked();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
void paint(QPainter *painter,
|
||||||
|
const QStyleOptionGraphicsItem *option,
|
||||||
|
QWidget *widget = 0);
|
||||||
void resizeEvent(QGraphicsSceneResizeEvent *event);
|
void resizeEvent(QGraphicsSceneResizeEvent *event);
|
||||||
|
void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
|
||||||
|
void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
PushButtonPrivate * const d;
|
PushButtonPrivate * const d;
|
||||||
|
|
||||||
|
friend class PushButtonPrivate;
|
||||||
|
Q_PRIVATE_SLOT(d, void syncBorders())
|
||||||
|
Q_PRIVATE_SLOT(d, void elementAnimationFinished(int id))
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace Plasma
|
} // namespace Plasma
|
||||||
|
Loading…
Reference in New Issue
Block a user