2008-11-04 00:08:39 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2008 Aaron Seigo <aseigo@kde.org>
|
|
|
|
* Copyright 2008 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 "pushbutton.h"
|
|
|
|
|
|
|
|
#include <QStyleOptionGraphicsItem>
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QDir>
|
|
|
|
#include <QApplication>
|
|
|
|
|
2010-01-21 21:07:33 +01:00
|
|
|
#include <QWeakPointer>
|
|
|
|
#include <QPropertyAnimation>
|
|
|
|
|
2008-11-04 03:55:37 +01:00
|
|
|
#include <kicon.h>
|
|
|
|
#include <kiconeffect.h>
|
|
|
|
#include <kmimetype.h>
|
|
|
|
#include <kpushbutton.h>
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
#include "theme.h"
|
|
|
|
#include "svg.h"
|
|
|
|
#include "framesvg.h"
|
|
|
|
#include "animator.h"
|
|
|
|
#include "paintutils.h"
|
2009-04-24 09:40:52 +02:00
|
|
|
#include "private/actionwidgetinterface_p.h"
|
2010-05-14 14:45:54 +02:00
|
|
|
#include <plasma/private/focusindicator_p.h>
|
2010-01-24 20:48:18 +01:00
|
|
|
#include "animations/animation.h"
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
2009-04-24 09:40:52 +02:00
|
|
|
class PushButtonPrivate : public ActionWidgetInterface<PushButton>
|
2008-11-04 00:08:39 +01:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
PushButtonPrivate(PushButton *pushButton)
|
2009-04-24 09:40:52 +02:00
|
|
|
: ActionWidgetInterface<PushButton>(pushButton),
|
|
|
|
q(pushButton),
|
2008-11-04 00:08:39 +01:00
|
|
|
background(0),
|
|
|
|
fadeIn(false),
|
2009-09-12 17:05:39 +02:00
|
|
|
svg(0),
|
|
|
|
customFont(false)
|
2008-11-04 00:08:39 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~PushButtonPrivate()
|
|
|
|
{
|
|
|
|
delete svg;
|
|
|
|
}
|
|
|
|
|
2009-09-13 19:13:55 +02:00
|
|
|
void setPixmap()
|
2008-11-04 00:08:39 +01:00
|
|
|
{
|
|
|
|
if (imagePath.isEmpty()) {
|
2009-09-13 19:15:56 +02:00
|
|
|
delete svg;
|
|
|
|
svg = 0;
|
2008-11-04 00:08:39 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
KMimeType::Ptr mime = KMimeType::findByPath(absImagePath);
|
2009-09-27 23:26:46 +02:00
|
|
|
QPixmap pm;
|
2008-11-04 00:08:39 +01:00
|
|
|
|
2009-07-28 23:07:19 +02:00
|
|
|
if (mime->is("image/svg+xml") || mime->is("image/svg+xml-compressed")) {
|
2009-09-27 23:26:46 +02:00
|
|
|
if (!svg || svg->imagePath() != absImagePath) {
|
2009-09-13 19:13:55 +02:00
|
|
|
delete svg;
|
|
|
|
svg = new Svg();
|
|
|
|
svg->setImagePath(imagePath);
|
|
|
|
QObject::connect(svg, SIGNAL(repaintNeeded()), q, SLOT(setPixmap()));
|
2009-09-27 23:26:46 +02:00
|
|
|
if (!svgElement.isNull()) {
|
|
|
|
svg->setContainsMultipleImages(true);
|
|
|
|
}
|
2009-09-13 19:13:55 +02:00
|
|
|
}
|
2009-07-28 23:07:19 +02:00
|
|
|
|
2009-09-27 23:26:46 +02:00
|
|
|
//QPainter p(&pm);
|
2010-06-08 19:16:01 +02:00
|
|
|
|
|
|
|
if (!svgElement.isEmpty() && svg->hasElement(svgElement)) {
|
|
|
|
svg->resize();
|
2009-07-28 23:07:19 +02:00
|
|
|
QSizeF elementSize = svg->elementSize(svgElement);
|
2010-06-08 19:16:01 +02:00
|
|
|
float scale = q->nativeWidget()->iconSize().width() / qMax(elementSize.width(), elementSize.height());
|
2009-07-28 23:07:19 +02:00
|
|
|
|
2010-06-08 19:16:01 +02:00
|
|
|
svg->resize(elementSize * scale);
|
2009-09-27 23:26:46 +02:00
|
|
|
pm = svg->pixmap(svgElement);
|
2009-07-28 23:07:19 +02:00
|
|
|
} else {
|
2010-06-08 19:16:01 +02:00
|
|
|
svg->resize(q->nativeWidget()->iconSize());
|
2009-09-27 23:26:46 +02:00
|
|
|
pm = svg->pixmap();
|
2009-07-28 23:07:19 +02:00
|
|
|
}
|
2008-11-04 00:08:39 +01:00
|
|
|
} else {
|
2009-09-13 19:15:56 +02:00
|
|
|
delete svg;
|
|
|
|
svg = 0;
|
2008-11-04 00:08:39 +01:00
|
|
|
pm = QPixmap(absImagePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
static_cast<KPushButton*>(q->widget())->setIcon(KIcon(pm));
|
|
|
|
}
|
|
|
|
|
|
|
|
void syncActiveRect();
|
|
|
|
void syncBorders();
|
|
|
|
|
|
|
|
PushButton *q;
|
|
|
|
|
|
|
|
FrameSvg *background;
|
|
|
|
bool fadeIn;
|
|
|
|
qreal opacity;
|
|
|
|
QRectF activeRect;
|
2010-05-14 14:45:54 +02:00
|
|
|
|
2010-01-24 20:48:18 +01:00
|
|
|
Animation *hoverAnimation;
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
QString imagePath;
|
|
|
|
QString absImagePath;
|
|
|
|
Svg *svg;
|
2009-07-28 23:07:19 +02:00
|
|
|
QString svgElement;
|
2009-09-12 17:05:39 +02:00
|
|
|
bool customFont;
|
2008-11-04 00:08:39 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
void PushButtonPrivate::syncActiveRect()
|
|
|
|
{
|
2010-05-14 17:36:19 +02:00
|
|
|
background->setElementPrefix("normal");
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
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));
|
|
|
|
|
2010-05-14 17:36:19 +02:00
|
|
|
background->setElementPrefix("normal");
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PushButtonPrivate::syncBorders()
|
|
|
|
{
|
|
|
|
//set margins from the normal element
|
|
|
|
qreal left, top, right, bottom;
|
|
|
|
|
2010-05-14 17:36:19 +02:00
|
|
|
background->setElementPrefix("normal");
|
2008-11-04 00:08:39 +01:00
|
|
|
background->getMargins(left, top, right, bottom);
|
|
|
|
q->setContentsMargins(left, top, right, bottom);
|
|
|
|
|
|
|
|
//calc the rect for the over effect
|
|
|
|
syncActiveRect();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PushButton::PushButton(QGraphicsWidget *parent)
|
|
|
|
: QGraphicsProxyWidget(parent),
|
|
|
|
d(new PushButtonPrivate(this))
|
|
|
|
{
|
2009-12-21 17:12:16 +01:00
|
|
|
d->background = new FrameSvg(this);
|
|
|
|
d->background->setImagePath("widgets/button");
|
|
|
|
d->background->setCacheAllRenderedFrames(true);
|
2010-05-14 17:36:19 +02:00
|
|
|
|
|
|
|
d->background->setElementPrefix("normal");
|
2009-12-21 17:12:16 +01:00
|
|
|
|
2010-01-24 20:48:18 +01:00
|
|
|
d->hoverAnimation = Animator::create(Animator::PixmapTransitionAnimation);
|
|
|
|
d->hoverAnimation->setTargetWidget(this);
|
|
|
|
d->hoverAnimation->setProperty("startPixmap", d->background->framePixmap());
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
KPushButton *native = new KPushButton;
|
2009-09-13 18:50:00 +02:00
|
|
|
connect(native, SIGNAL(pressed()), this, SIGNAL(pressed()));
|
|
|
|
connect(native, SIGNAL(released()), this, SIGNAL(released()));
|
2008-11-04 00:08:39 +01:00
|
|
|
connect(native, SIGNAL(clicked()), this, SIGNAL(clicked()));
|
2009-06-05 03:29:19 +02:00
|
|
|
connect(native, SIGNAL(toggled(bool)), this, SIGNAL(toggled(bool)));
|
2008-11-04 00:08:39 +01:00
|
|
|
setWidget(native);
|
|
|
|
native->setAttribute(Qt::WA_NoSystemBackground);
|
|
|
|
|
2009-01-22 17:59:13 +01:00
|
|
|
setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
|
|
|
|
|
2010-05-14 14:45:54 +02:00
|
|
|
FocusIndicator *focusIndicator = new FocusIndicator(this, "widgets/button");
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
d->syncBorders();
|
|
|
|
setAcceptHoverEvents(true);
|
|
|
|
connect(Plasma::Theme::defaultTheme(), SIGNAL(themeChanged()), SLOT(syncBorders()));
|
|
|
|
}
|
|
|
|
|
|
|
|
PushButton::~PushButton()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PushButton::setText(const QString &text)
|
|
|
|
{
|
|
|
|
static_cast<KPushButton*>(widget())->setText(text);
|
2010-01-07 13:50:56 +01:00
|
|
|
updateGeometry();
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QString PushButton::text() const
|
|
|
|
{
|
|
|
|
return static_cast<KPushButton*>(widget())->text();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PushButton::setImage(const QString &path)
|
|
|
|
{
|
|
|
|
if (d->imagePath == path) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
delete d->svg;
|
|
|
|
d->svg = 0;
|
|
|
|
d->imagePath = path;
|
|
|
|
|
|
|
|
bool absolutePath = !path.isEmpty() &&
|
|
|
|
#ifdef Q_WS_WIN
|
|
|
|
!QDir::isRelativePath(path)
|
|
|
|
#else
|
2009-10-13 14:40:13 +02:00
|
|
|
(path[0] == '/' || path.startsWith(QLatin1String(":/")))
|
2008-11-04 00:08:39 +01:00
|
|
|
#endif
|
|
|
|
;
|
|
|
|
|
|
|
|
if (absolutePath) {
|
|
|
|
d->absImagePath = path;
|
|
|
|
} else {
|
|
|
|
//TODO: package support
|
|
|
|
d->absImagePath = Theme::defaultTheme()->imagePath(path);
|
|
|
|
}
|
|
|
|
|
2009-09-13 19:13:55 +02:00
|
|
|
d->setPixmap();
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
2009-07-28 23:07:19 +02:00
|
|
|
void PushButton::setImage(const QString &path, const QString &elementid)
|
|
|
|
{
|
|
|
|
d->svgElement = elementid;
|
|
|
|
setImage(path);
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
QString PushButton::image() const
|
|
|
|
{
|
|
|
|
return d->imagePath;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PushButton::setStyleSheet(const QString &stylesheet)
|
|
|
|
{
|
|
|
|
widget()->setStyleSheet(stylesheet);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString PushButton::styleSheet()
|
|
|
|
{
|
|
|
|
return widget()->styleSheet();
|
|
|
|
}
|
|
|
|
|
2009-04-24 09:40:52 +02:00
|
|
|
void PushButton::setAction(QAction *action)
|
|
|
|
{
|
|
|
|
d->setAction(action);
|
|
|
|
}
|
|
|
|
|
|
|
|
QAction *PushButton::action() const
|
|
|
|
{
|
|
|
|
return d->action;
|
|
|
|
}
|
|
|
|
|
2009-09-13 19:37:36 +02:00
|
|
|
void PushButton::setIcon(const KIcon &icon)
|
2009-04-24 09:40:52 +02:00
|
|
|
{
|
|
|
|
nativeWidget()->setIcon(icon);
|
|
|
|
}
|
|
|
|
|
2009-09-13 19:37:36 +02:00
|
|
|
void PushButton::setIcon(const QIcon &icon)
|
|
|
|
{
|
|
|
|
setIcon(KIcon(icon));
|
|
|
|
}
|
|
|
|
|
2009-04-24 09:40:52 +02:00
|
|
|
QIcon PushButton::icon() const
|
|
|
|
{
|
|
|
|
return nativeWidget()->icon();
|
|
|
|
}
|
|
|
|
|
2009-06-05 03:29:19 +02:00
|
|
|
void PushButton::setCheckable(bool checkable)
|
|
|
|
{
|
|
|
|
nativeWidget()->setCheckable(checkable);
|
|
|
|
}
|
|
|
|
|
2009-11-14 02:06:58 +01:00
|
|
|
bool PushButton::isCheckable() const
|
|
|
|
{
|
|
|
|
return nativeWidget()->isCheckable();
|
|
|
|
}
|
|
|
|
|
2009-06-05 03:29:19 +02:00
|
|
|
void PushButton::setChecked(bool checked)
|
|
|
|
{
|
|
|
|
nativeWidget()->setChecked(checked);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PushButton::isChecked() const
|
|
|
|
{
|
|
|
|
return nativeWidget()->isChecked();
|
|
|
|
}
|
|
|
|
|
2009-09-13 20:12:32 +02:00
|
|
|
bool PushButton::isDown() const
|
|
|
|
{
|
|
|
|
return nativeWidget()->isDown();
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
KPushButton *PushButton::nativeWidget() const
|
|
|
|
{
|
|
|
|
return static_cast<KPushButton*>(widget());
|
|
|
|
}
|
|
|
|
|
|
|
|
void PushButton::resizeEvent(QGraphicsSceneResizeEvent *event)
|
|
|
|
{
|
2009-09-13 19:13:55 +02:00
|
|
|
d->setPixmap();
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
if (d->background) {
|
|
|
|
//resize all four panels
|
2010-05-14 17:36:19 +02:00
|
|
|
d->background->setElementPrefix("pressed");
|
2008-11-11 19:31:21 +01:00
|
|
|
d->background->resizeFrame(size());
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
d->syncActiveRect();
|
|
|
|
|
|
|
|
d->background->setElementPrefix("active");
|
|
|
|
d->background->resizeFrame(d->activeRect.size());
|
2010-01-24 20:48:18 +01:00
|
|
|
d->hoverAnimation->setProperty("targetPixmap", d->background->framePixmap());
|
2009-10-22 21:28:19 +02:00
|
|
|
d->background->setElementPrefix("focus");
|
|
|
|
d->background->resizeFrame(d->activeRect.size());
|
2008-11-04 00:08:39 +01:00
|
|
|
|
2010-05-14 17:36:19 +02:00
|
|
|
d->background->setElementPrefix("normal");
|
2008-11-04 00:08:39 +01:00
|
|
|
d->background->resizeFrame(size());
|
2010-01-24 20:48:18 +01:00
|
|
|
d->hoverAnimation->setProperty("startPixmap", d->background->framePixmap());
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QGraphicsProxyWidget::resizeEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PushButton::paint(QPainter *painter,
|
|
|
|
const QStyleOptionGraphicsItem *option,
|
|
|
|
QWidget *widget)
|
|
|
|
{
|
2009-02-24 23:42:06 +01:00
|
|
|
if (!styleSheet().isNull() || Theme::defaultTheme()->useNativeWidgetStyle()) {
|
2008-11-04 00:08:39 +01:00
|
|
|
QGraphicsProxyWidget::paint(painter, option, widget);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QPixmap bufferPixmap;
|
|
|
|
|
|
|
|
//Normal button, pressed or not
|
|
|
|
if (isEnabled()) {
|
2009-03-26 22:45:05 +01:00
|
|
|
if (nativeWidget()->isDown() || nativeWidget()->isChecked()) {
|
2010-05-14 17:36:19 +02:00
|
|
|
d->background->setElementPrefix("pressed");
|
2008-11-04 00:08:39 +01:00
|
|
|
} else {
|
2010-05-14 17:36:19 +02:00
|
|
|
d->background->setElementPrefix("normal");
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
2010-01-24 20:48:18 +01:00
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
//flat or disabled
|
|
|
|
} else if (!isEnabled() || nativeWidget()->isFlat()) {
|
|
|
|
bufferPixmap = QPixmap(rect().size().toSize());
|
|
|
|
bufferPixmap.fill(Qt::transparent);
|
|
|
|
|
|
|
|
QPainter buffPainter(&bufferPixmap);
|
|
|
|
d->background->paintFrame(&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
|
2010-05-14 14:45:54 +02:00
|
|
|
if (!nativeWidget()->isDown() && !nativeWidget()->isChecked() && isEnabled() && acceptHoverEvents() && d->background->hasElementPrefix("active")) {
|
2010-01-24 20:48:18 +01:00
|
|
|
if (d->hoverAnimation->state() == QAbstractAnimation::Running && !isUnderMouse() && !nativeWidget()->isDefault()) {
|
2008-11-04 00:08:39 +01:00
|
|
|
d->background->setElementPrefix("active");
|
2010-01-24 20:48:18 +01:00
|
|
|
d->background->paintFrame(painter, d->activeRect.topLeft());
|
|
|
|
} else {
|
2008-11-04 00:08:39 +01:00
|
|
|
painter->drawPixmap(
|
|
|
|
d->activeRect.topLeft(),
|
2010-01-24 20:48:18 +01:00
|
|
|
d->hoverAnimation->property("currentPixmap").value<QPixmap>());
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
2010-05-14 15:26:52 +02:00
|
|
|
} else if (isEnabled()) {
|
2010-02-05 18:41:42 +01:00
|
|
|
d->background->paintFrame(painter);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
painter->setPen(Plasma::Theme::defaultTheme()->color(Theme::ButtonTextColor));
|
|
|
|
|
|
|
|
if (nativeWidget()->isDown()) {
|
|
|
|
painter->translate(QPoint(1, 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
QRectF rect = contentsRect();
|
|
|
|
|
|
|
|
if (!nativeWidget()->icon().isNull()) {
|
2009-08-29 16:22:52 +02:00
|
|
|
const qreal iconSize = qMin(rect.width(), rect.height());
|
2009-08-29 15:00:22 +02:00
|
|
|
QPixmap iconPix = nativeWidget()->icon().pixmap(iconSize);
|
2008-11-04 00:08:39 +01:00
|
|
|
if (!isEnabled()) {
|
|
|
|
KIconEffect *effect = KIconLoader::global()->iconEffect();
|
|
|
|
iconPix = effect->apply(iconPix, KIconLoader::Toolbar, KIconLoader::DisabledState);
|
|
|
|
}
|
|
|
|
|
2009-09-19 17:59:20 +02:00
|
|
|
QRect pixmapRect;
|
|
|
|
if (nativeWidget()->text().isEmpty()) {
|
|
|
|
pixmapRect = nativeWidget()->style()->alignedRect(option->direction, Qt::AlignCenter, iconPix.size(), rect.toRect());
|
|
|
|
} else {
|
|
|
|
pixmapRect = nativeWidget()->style()->alignedRect(option->direction, Qt::AlignLeft|Qt::AlignVCenter, iconPix.size(), rect.toRect());
|
|
|
|
}
|
|
|
|
painter->drawPixmap(pixmapRect.topLeft(), iconPix);
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
if (option->direction == Qt::LeftToRight) {
|
|
|
|
rect.adjust(rect.height(), 0, 0, 0);
|
|
|
|
} else {
|
|
|
|
rect.adjust(0, 0, -rect.height(), 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-12 17:05:39 +02:00
|
|
|
QFont widgetFont;
|
|
|
|
if (d->customFont) {
|
|
|
|
widgetFont = font();
|
2009-11-10 10:46:14 +01:00
|
|
|
} else {
|
|
|
|
widgetFont = Plasma::Theme::defaultTheme()->font(Plasma::Theme::DefaultFont);
|
2009-09-12 17:05:39 +02:00
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
//if there is not enough room for the text make it to fade out
|
2009-09-12 17:05:39 +02:00
|
|
|
QFontMetricsF fm(widgetFont);
|
2008-11-04 00:08:39 +01:00
|
|
|
if (rect.width() < fm.width(nativeWidget()->text())) {
|
|
|
|
if (bufferPixmap.isNull()) {
|
|
|
|
bufferPixmap = QPixmap(rect.size().toSize());
|
|
|
|
}
|
|
|
|
bufferPixmap.fill(Qt::transparent);
|
|
|
|
|
|
|
|
QPainter p(&bufferPixmap);
|
|
|
|
p.setPen(painter->pen());
|
2009-09-12 17:05:39 +02:00
|
|
|
p.setFont(widgetFont);
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
// 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 {
|
2009-09-12 17:05:39 +02:00
|
|
|
painter->setFont(widgetFont);
|
2008-11-04 00:08:39 +01:00
|
|
|
painter->drawText(rect, Qt::AlignCenter, nativeWidget()->text());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PushButton::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
|
|
|
|
{
|
2010-05-14 14:45:54 +02:00
|
|
|
if (nativeWidget()->isDown() || d->background->hasElementPrefix("hover")) {
|
2008-11-09 18:00:49 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-01-24 20:48:18 +01:00
|
|
|
d->hoverAnimation->setProperty("duration", 75);
|
2010-05-14 14:45:54 +02:00
|
|
|
|
2010-05-14 17:36:19 +02:00
|
|
|
d->background->setElementPrefix("normal");
|
2010-01-24 20:48:18 +01:00
|
|
|
d->hoverAnimation->setProperty("startPixmap", d->background->framePixmap());
|
2010-01-21 21:07:33 +01:00
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
d->background->setElementPrefix("active");
|
2010-01-24 20:48:18 +01:00
|
|
|
d->hoverAnimation->setProperty("targetPixmap", d->background->framePixmap());
|
|
|
|
|
|
|
|
d->hoverAnimation->start();
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
QGraphicsProxyWidget::hoverEnterEvent(event);
|
|
|
|
}
|
|
|
|
|
2009-09-12 17:05:39 +02:00
|
|
|
void PushButton::changeEvent(QEvent *event)
|
|
|
|
{
|
|
|
|
if (event->type() == QEvent::FontChange) {
|
|
|
|
d->customFont = true;
|
|
|
|
}
|
2009-09-13 15:13:55 +02:00
|
|
|
|
|
|
|
QGraphicsProxyWidget::changeEvent(event);
|
2009-09-12 17:05:39 +02:00
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
void PushButton::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
|
|
|
|
{
|
2010-05-14 14:45:54 +02:00
|
|
|
if (nativeWidget()->isDown() || d->background->hasElementPrefix("hover")) {
|
2008-11-09 18:00:49 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-01-24 20:48:18 +01:00
|
|
|
d->hoverAnimation->setProperty("duration", 150);
|
2010-05-14 14:45:54 +02:00
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
d->background->setElementPrefix("active");
|
2010-01-24 20:48:18 +01:00
|
|
|
d->hoverAnimation->setProperty("startPixmap", d->background->framePixmap());
|
2008-11-04 00:08:39 +01:00
|
|
|
|
2010-05-14 17:36:19 +02:00
|
|
|
d->background->setElementPrefix("normal");
|
2010-01-24 20:48:18 +01:00
|
|
|
d->hoverAnimation->setProperty("targetPixmap", d->background->framePixmap());
|
2010-05-14 14:45:54 +02:00
|
|
|
|
2010-01-24 20:48:18 +01:00
|
|
|
d->hoverAnimation->start();
|
2010-05-14 14:45:54 +02:00
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
|
2010-01-24 20:48:18 +01:00
|
|
|
QGraphicsProxyWidget::hoverLeaveEvent(event);
|
2010-01-21 21:07:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-07 13:50:56 +01:00
|
|
|
QSizeF PushButton::sizeHint(Qt::SizeHint which, const QSizeF & constraint) const
|
|
|
|
{
|
|
|
|
QSizeF hint = QGraphicsProxyWidget::sizeHint(which, constraint);
|
|
|
|
|
|
|
|
if (hint.isEmpty()) {
|
|
|
|
return hint;
|
|
|
|
}
|
|
|
|
|
|
|
|
//replace the native margin with the Svg one
|
|
|
|
QStyleOption option;
|
|
|
|
option.initFrom(nativeWidget());
|
|
|
|
int nativeMargin = nativeWidget()->style()->pixelMetric(QStyle::PM_ButtonMargin, &option, nativeWidget());
|
|
|
|
qreal left, top, right, bottom;
|
|
|
|
d->background->getMargins(left, top, right, bottom);
|
|
|
|
hint = hint - QSize(nativeMargin, nativeMargin) + QSize(left+right, top+bottom);
|
|
|
|
return hint;
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
} // namespace Plasma
|
|
|
|
|
|
|
|
#include <pushbutton.moc>
|
|
|
|
|