A Simple SVG button

svn path=/trunk/KDE/kdebase/workspace/plasma/lib/; revision=639265
This commit is contained in:
Siraj Razick 2007-03-04 19:45:17 +00:00
parent fa16f5700c
commit fa94c62ea3
2 changed files with 270 additions and 0 deletions

193
widgets/button.cpp Normal file
View File

@ -0,0 +1,193 @@
/*
* Copyright (C) 2007 by Siraj Razick <siraj@kdemail.net>
*
* 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 <QPainter>
#include <QPalette>
#include <QTimer>
#include <QSvgRenderer>
#include <QGraphicsSceneMouseEvent>
#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

77
widgets/button.h Normal file
View File

@ -0,0 +1,77 @@
/*
* Copyright (C) 2007 by Siraj Razick <siraj@kdemali.net>
*
* 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 <QGraphicsItem>
// 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