as the name says, ehi, it's a toolbutton :)

svn path=/trunk/KDE/kdelibs/; revision=882689
This commit is contained in:
Marco Martin 2008-11-11 09:30:05 +00:00
parent cdf562df11
commit 3cc9af418d
3 changed files with 458 additions and 0 deletions

View File

@ -112,6 +112,7 @@ set(plasma_LIB_SRCS
widgets/scrollbar.cpp
widgets/signalplotter.cpp
widgets/slider.cpp
widgets/toolbutton.cpp
widgets/busywidget.cpp
widgets/svgwidget.cpp
widgets/tabbar.cpp
@ -230,6 +231,7 @@ install(FILES
widgets/lineedit.h
widgets/meter.h
widgets/pushbutton.h
widgets/toolbutton.h
widgets/radiobutton.h
widgets/scrollbar.h
widgets/signalplotter.h

341
widgets/toolbutton.cpp Normal file
View File

@ -0,0 +1,341 @@
/*
* 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 "toolbutton.h"
#include <QStyleOptionGraphicsItem>
#include <QPainter>
#include <QDir>
#include <QToolButton>
#include <QApplication>
#include <kicon.h>
#include <kiconeffect.h>
#include <kmimetype.h>
#include <kcolorutils.h>
#include "theme.h"
#include "svg.h"
#include "framesvg.h"
#include "animator.h"
#include "paintutils.h"
namespace Plasma
{
class ToolButtonPrivate
{
public:
ToolButtonPrivate(ToolButton *toolButton)
: q(toolButton),
background(0),
animId(0),
fadeIn(false),
svg(0)
{
}
~ToolButtonPrivate()
{
delete svg;
}
void setPixmap(ToolButton *q)
{
if (imagePath.isEmpty()) {
return;
}
KMimeType::Ptr mime = KMimeType::findByPath(absImagePath);
QPixmap pm(q->size().toSize());
if (mime->is("image/svg+xml")) {
svg = new Svg();
QPainter p(&pm);
svg->paint(&p, pm.rect());
} else {
pm = QPixmap(absImagePath);
}
static_cast<QToolButton*>(q->widget())->setIcon(KIcon(pm));
}
void syncActiveRect();
void syncBorders();
void animationUpdate(qreal progress);
ToolButton *q;
FrameSvg *background;
int animId;
bool fadeIn;
qreal opacity;
QRectF activeRect;
QString imagePath;
QString absImagePath;
Svg *svg;
};
void ToolButtonPrivate::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 ToolButtonPrivate::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 ToolButtonPrivate::animationUpdate(qreal progress)
{
if (progress == 1) {
animId = 0;
fadeIn = true;
}
opacity = fadeIn ? progress : 1 - progress;
// explicit update
q->update();
}
ToolButton::ToolButton(QGraphicsWidget *parent)
: QGraphicsProxyWidget(parent),
d(new ToolButtonPrivate(this))
{
QToolButton *native = new QToolButton;
connect(native, SIGNAL(clicked()), this, SIGNAL(clicked()));
setWidget(native);
native->setAttribute(Qt::WA_NoSystemBackground);
d->background = new FrameSvg(this);
d->background->setImagePath("widgets/button");
d->background->setCacheAllRenderedFrames(true);
d->background->setElementPrefix("normal");
d->syncBorders();
setAcceptHoverEvents(true);
connect(Plasma::Theme::defaultTheme(), SIGNAL(themeChanged()), SLOT(syncBorders()));
}
ToolButton::~ToolButton()
{
delete d;
}
void ToolButton::setText(const QString &text)
{
static_cast<QToolButton*>(widget())->setText(text);
}
QString ToolButton::text() const
{
return static_cast<QToolButton*>(widget())->text();
}
void ToolButton::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
(path[0] == '/' || path.startsWith(":/"))
#endif
;
if (absolutePath) {
d->absImagePath = path;
} else {
//TODO: package support
d->absImagePath = Theme::defaultTheme()->imagePath(path);
}
d->setPixmap(this);
}
QString ToolButton::image() const
{
return d->imagePath;
}
void ToolButton::setStyleSheet(const QString &stylesheet)
{
widget()->setStyleSheet(stylesheet);
}
QString ToolButton::styleSheet()
{
return widget()->styleSheet();
}
QToolButton *ToolButton::nativeWidget() const
{
return static_cast<QToolButton*>(widget());
}
void ToolButton::resizeEvent(QGraphicsSceneResizeEvent *event)
{
d->setPixmap(this);
if (d->background) {
//resize all four panels
d->background->setElementPrefix("pressed");
d->background->resizeFrame(size());
d->background->setElementPrefix("focus");
d->background->resizeFrame(size());
d->syncActiveRect();
d->background->setElementPrefix("active");
d->background->resizeFrame(d->activeRect.size());
d->background->setElementPrefix("normal");
d->background->resizeFrame(size());
}
QGraphicsProxyWidget::resizeEvent(event);
}
void ToolButton::paint(QPainter *painter,
const QStyleOptionGraphicsItem *option,
QWidget *widget)
{
if (!styleSheet().isNull()) {
QGraphicsProxyWidget::paint(painter, option, widget);
return;
}
QToolButton *button = nativeWidget();
QStyleOptionToolButton buttonOpt;
buttonOpt.initFrom(button);
buttonOpt.icon = button->icon();
buttonOpt.text = button->text();
buttonOpt.iconSize = button->iconSize();
buttonOpt.toolButtonStyle = button->toolButtonStyle();
if (d->animId || (buttonOpt.state & QStyle::State_MouseOver) || (buttonOpt.state & QStyle::State_On)) {
if (button->isDown() || (buttonOpt.state & QStyle::State_On)) {
d->background->setElementPrefix("pressed");
} else {
d->background->setElementPrefix("normal");
}
d->background->resizeFrame(size());
if (d->animId) {
QPixmap buffer = d->background->framePixmap();
QPainter bufferPainter(&buffer);
bufferPainter.setCompositionMode(QPainter::CompositionMode_DestinationIn);
QColor alphaColor(Qt::black);
alphaColor.setAlphaF(qMin(0.95, d->opacity));
bufferPainter.fillRect(buffer.rect(), alphaColor);
bufferPainter.end();
painter->drawPixmap(QPoint(0,0), buffer);
buttonOpt.palette.setColor(QPalette::ButtonText, KColorUtils::mix(Plasma::Theme::defaultTheme()->color(Plasma::Theme::ButtonTextColor), Plasma::Theme::defaultTheme()->color(Plasma::Theme::TextColor), 1-d->opacity));
} else {
d->background->paintFrame(painter);
buttonOpt.palette.setColor(QPalette::ButtonText, Plasma::Theme::defaultTheme()->color(Plasma::Theme::ButtonTextColor));
}
} else {
buttonOpt.palette.setColor(QPalette::ButtonText, Plasma::Theme::defaultTheme()->color(Plasma::Theme::TextColor));
}
button->style()->drawControl(QStyle::CE_ToolButtonLabel, &buttonOpt, painter, button);
}
void ToolButton::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
if (nativeWidget()->isDown()) {
return;
}
const int FadeInDuration = 75;
if (d->animId) {
Plasma::Animator::self()->stopCustomAnimation(d->animId);
}
d->animId = Plasma::Animator::self()->customAnimation(
40 / (1000 / FadeInDuration), FadeInDuration,
Plasma::Animator::LinearCurve, this, "animationUpdate");
d->background->setElementPrefix("active");
QGraphicsProxyWidget::hoverEnterEvent(event);
}
void ToolButton::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
if (nativeWidget()->isDown()) {
return;
}
const int FadeOutDuration = 150;
if (d->animId) {
Plasma::Animator::self()->stopCustomAnimation(d->animId);
}
d->fadeIn = false;
d->animId = Plasma::Animator::self()->customAnimation(
40 / (1000 / FadeOutDuration), FadeOutDuration,
Plasma::Animator::LinearCurve, this, "animationUpdate");
d->background->setElementPrefix("active");
QGraphicsProxyWidget::hoverLeaveEvent(event);
}
} // namespace Plasma
#include <toolbutton.moc>

115
widgets/toolbutton.h Normal file
View File

@ -0,0 +1,115 @@
/*
* Copyright 2008 Aaron Seigo <aseigo@kde.org>
*
* 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.
*/
#ifndef PLASMA_TOOLBUTTON_H
#define PLASMA_TOOLBUTTON_H
#include <QtGui/QGraphicsProxyWidget>
class QToolButton;
#include <plasma/plasma_export.h>
namespace Plasma
{
class ToolButtonPrivate;
/**
* @class ToolButton plasma/widgets/pushbutton.h <Plasma/Widgets/ToolButton>
*
* @short Provides a plasma-themed QToolButton.
*/
class PLASMA_EXPORT ToolButton : public QGraphicsProxyWidget
{
Q_OBJECT
Q_PROPERTY(QGraphicsWidget *parentWidget READ parentWidget)
Q_PROPERTY(QString text READ text WRITE setText)
Q_PROPERTY(QString image READ image WRITE setImage)
Q_PROPERTY(QString stylesheet READ styleSheet WRITE setStyleSheet)
Q_PROPERTY(QToolButton *nativeWidget READ nativeWidget)
public:
explicit ToolButton(QGraphicsWidget *parent = 0);
~ToolButton();
/**
* Sets the display text for this ToolButton
*
* @arg text the text to display; should be translated.
*/
void setText(const QString &text);
/**
* @return the display text
*/
QString text() const;
/**
* Sets the path to an image to display.
*
* @arg path the path to the image; if a relative path, then a themed image will be loaded.
*/
void setImage(const QString &path);
/**
* @return the image path being displayed currently, or an empty string if none.
*/
QString image() const;
/**
* Sets the stylesheet used to control the visual display of this ToolButton
*
* @arg stylesheet a CSS string
*/
void setStyleSheet(const QString &stylesheet);
/**
* @return the stylesheet currently used with this widget
*/
QString styleSheet();
/**
* @return the native widget wrapped by this ToolButton
*/
QToolButton *nativeWidget() const;
Q_SIGNALS:
void clicked();
protected:
void paint(QPainter *painter,
const QStyleOptionGraphicsItem *option,
QWidget *widget = 0);
void resizeEvent(QGraphicsSceneResizeEvent *event);
void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
private:
ToolButtonPrivate *const d;
friend class ToolButtonPrivate;
Q_PRIVATE_SLOT(d, void syncBorders())
Q_PRIVATE_SLOT(d, void animationUpdate(qreal progress))
};
} // namespace Plasma
#endif // multiple inclusion guard