From fa94c62ea34f1210ef524a76bbda2310972204bb Mon Sep 17 00:00:00 2001 From: Siraj Razick Date: Sun, 4 Mar 2007 19:45:17 +0000 Subject: [PATCH] A Simple SVG button svn path=/trunk/KDE/kdebase/workspace/plasma/lib/; revision=639265 --- widgets/button.cpp | 193 +++++++++++++++++++++++++++++++++++++++++++++ widgets/button.h | 77 ++++++++++++++++++ 2 files changed, 270 insertions(+) create mode 100644 widgets/button.cpp create mode 100644 widgets/button.h diff --git a/widgets/button.cpp b/widgets/button.cpp new file mode 100644 index 000000000..38c302198 --- /dev/null +++ b/widgets/button.cpp @@ -0,0 +1,193 @@ +/* + * Copyright (C) 2007 by Siraj Razick + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Library General Public License version 2 as + * published by the Free Software Foundation + * + * 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 +#include +#include +#include +#include +#include "button.h" +#include "button.moc" +#include "theme.h" + +namespace Plasma +{ + +class Button::Private +{ + public: + Private() { }; + ~Private() { }; + QString labelText; + QString labelIcon; + QColor labelTextColor; + int labelTextOpacity; + int height; + int width; + int radius; + QTimer * updateTimer; + ButtonState state; +}; + + +Button::Button(QGraphicsItem *parent) + : Widget(parent), + d(new Private) +{ + setDefaultStates(); +} + +Button::~Button() +{ + delete d; +} + +void Button::setDefaultStates() +{ + setAcceptedMouseButtons(Qt::LeftButton); + setAcceptsHoverEvents(true); + setEnabled(true); + d->height = 180; + d->width = 260 ; + setPos(QPointF(0.0,0.0)); + d->state= Button::NONE; + d->labelText=tr("Ok"); + d->labelTextColor= QColor(201,201,255); +} + +void Button::setText(const QString& text) const +{ + d->labelText = text; +} + +QString Button::text() +{ + return d->labelText; +} + +void Button::paint ( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget ) +{ + Q_UNUSED(widget) + Q_UNUSED(option) + painter->setCompositionMode(QPainter::CompositionMode_SourceOver); + painter->setPen(d->labelTextColor); + + if (d->state == Button::NONE ) { + drawSVG(painter,"buttonnormalbg"); + painter->drawText(QRectF(x(),y(),d->width,d->height), Qt::AlignCenter, this->text()); + drawSVG(painter,"buttonoverylay"); + }else if (d->state == PRESSED) { + drawSVG(painter,"buttonnormalbg"); + // drawSVG(painter,"buttonoverylay"); + }else { + drawSVG(painter,"buttonhoverbg"); + painter->drawText(QRectF(x(),y(),d->width,d->height), Qt::AlignCenter, this->text()); + drawSVG(painter,"buttonoverylay"); + } +} + +QRectF Button::boundingRect() const +{ + return QRectF(x(),y(),d->width,d->height); +} + +void Button::hoverLeaveEvent ( QGraphicsSceneHoverEvent * event ) +{ + d->state = Button::NONE; + update(); +} + +void Button::hoverEnterEvent ( QGraphicsSceneHoverEvent * event ) +{ + d->state = Button::HOVER; + update(); +} + +void Button::mousePressEvent ( QGraphicsSceneMouseEvent * event) +{ + event->accept(); + Button::ButtonState tmp = d->state; + d->state = PRESSED; + update(); + d->state = tmp; + update(); + emit clicked(); +} + +int Button::height() +{ + return d->height; +} + +int Button::width() +{ + return d->width; +} + +void Button::setHeight(int h) +{ + prepareGeometryChange (); + d->height = h; + update(); +} + +void Button::setWidth(int w) +{ + prepareGeometryChange (); + d->width = w; + update(); +} + +QSize Button::size() +{ + return QSize(d->width,d->height); +} + +void Button::setSize(QSize s) +{ + prepareGeometryChange (); + d->width = s.width(); + d->height = s.height(); + update(); +} + +void Button::drawOverlay(QPainter * painter) +{ + //TODO + //Draw overlay when no SVG was found +} + +void Button::drawSVG(QPainter * painter , const QString& imageName) +{ + QString file = Theme().imagePath(imageName); + if (!file.isEmpty()) { + QSvgRenderer svgimg(file); + svgimg.render(painter,this->boundingRect()); + } +} + +void Button::drawBackDrop(QPainter * painter) +{ + //TODO + //Drawbackground when no SVG was found +} + + + +} // Plasma namespace + diff --git a/widgets/button.h b/widgets/button.h new file mode 100644 index 000000000..1ce50f535 --- /dev/null +++ b/widgets/button.h @@ -0,0 +1,77 @@ +/* + * Copyright (C) 2007 by Siraj Razick + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Library General Public License version 2 as + * published by the Free Software Foundation + * + * 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 BUTTON_H_ +#define BUTTON_H_ + +//QT +#include +// KDE + +//Plasma +#include "widget.h" + + +namespace Plasma +{ + +class KDE_EXPORT Button : public Widget +{ + Q_OBJECT + public: + typedef enum {RECT=0,ROUND,COUSTOM} ButtonShape; + typedef enum {NONE,HOVER,PRESSED,RELEASED} ButtonState; + + Button(QGraphicsItem * parent = 0); + virtual ~Button(); + void paint ( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0 ); + QRectF boundingRect() const; + void setText(const QString&) const; + QString text(); + QSize size(); + void setSize(QSize size); + void setWidth(int width); + void setHeight(int height); + int width(); + int height(); + + public slots: + virtual void data(const DataSource::Data&) {}; + signals: + void clicked(); + + protected: + /** + initializer called from constructor + **/ + void setDefaultStates(); + virtual void hoverLeaveEvent ( QGraphicsSceneHoverEvent * event ); + virtual void hoverEnterEvent ( QGraphicsSceneHoverEvent * event ); + virtual void mousePressEvent ( QGraphicsSceneMouseEvent * event); + void drawOverlay(QPainter * painter); + void drawBackDrop(QPainter * patiner); + void drawSVG(QPainter * painter , const QString & imageName); + private: + class Private; + Private * const d; + +}; + +} // Plasma namespace + +#endif