Clean up the push button class

svn path=/trunk/KDE/kdebase/workspace/plasma/lib/; revision=642016
This commit is contained in:
Matt Broadstone 2007-03-13 03:00:09 +00:00
parent 19f05aab0c
commit 72dbcaf4f0
4 changed files with 115 additions and 88 deletions

View File

@ -108,6 +108,12 @@ class KDE_EXPORT Applet : public QWidget, public QGraphicsItemGroup
Private* d;
};
#define K_EXPORT_PLASMA_APPLET(libname, classname) \
K_EXPORT_COMPONENT_FACTORY( \
plasmaapplet_##libname, \
KGenericFactory<classname>("libplasmaapplet_" #libname))
} // Plasma namespace
#endif // multiple inclusion guard

View File

@ -30,7 +30,6 @@ namespace Plasma
class KDE_EXPORT DataVisualization : public QObject
{
Q_OBJECT
public:
DataVisualization(QObject* parent = 0);
virtual ~DataVisualization();

View File

@ -22,6 +22,7 @@
#include <QWidget>
#include <QPainter>
#include <QGraphicsSceneMouseEvent>
#include <QDebug>
#include "pushbutton.h"
#include "pushbutton.moc"
@ -32,8 +33,9 @@ namespace Plasma
class PushButton::Private
{
public:
Private() { };
~Private() { };
Private() {}
~Private() {}
QString labelText;
QString labelIcon;
QColor labelTextColor;
@ -61,6 +63,7 @@ PushButton::PushButton(QGraphicsItem *parent)
setAcceptedMouseButtons(Qt::LeftButton);
setAcceptsHoverEvents(true);
setEnabled(true);
d->height = 40;
d->width = 100 ;
d->minWidth = d->width;
@ -68,23 +71,21 @@ PushButton::PushButton(QGraphicsItem *parent)
d->minHeight = d->height;
d->maxHeight = d->height;
setPos(QPointF(0.0,0.0));
d->state= PushButton::NONE;
d->labelText=tr("Plasma");
d->labelTextColor= QColor(201,201,255);
d->state = PushButton::None;
d->labelText = tr("Plasma");
d->labelTextColor = QColor(201,201,255);
d->hasIcon = false;
d->iconSize=QSize(32,32);
d->iconSize = QSize(32,32);
}
PushButton::~PushButton()
{
delete d;
delete d;
}
QRectF PushButton::boundingRect() const
{
return QRectF(x(),y(),d->width,d->height);
return QRectF(x(), y(), d->width, d->height);
}
void PushButton::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
@ -93,11 +94,11 @@ void PushButton::paint(QPainter *painter, const QStyleOptionGraphicsItem *option
options.initFrom(widget);
options.state = option->state;
options.state |= isDown() ? QStyle::State_Sunken : QStyle::State_Raised;
options.rect = boundingRect().toRect();
options.text = text();
if (d->hasIcon){
if (d->hasIcon)
{
options.icon= d->icon;
options.iconSize = d->iconSize;
}
@ -105,7 +106,6 @@ void PushButton::paint(QPainter *painter, const QStyleOptionGraphicsItem *option
widget->style()->drawPrimitive(QStyle::PE_PanelButtonCommand, &options, painter, widget);
widget->style()->drawPrimitive(QStyle::PE_FrameFocusRect, &options, painter, widget);
widget-> style()->drawControl(QStyle::CE_PushButton, &options, painter, widget);
}
@ -116,40 +116,39 @@ void PushButton::data(const DataSource::Data&)
void PushButton::setText(const QString& text)
{
d->labelText = text;
QFont * _font = new QFont ( text );
QFontMetrics fm ( *_font );
if ( fm.width(text) >= d->width ) {
setWidth(fm.width(text)+4);
}
delete _font;
QFont font(text);
QFontMetrics fm(font);
if (fm.width(text) >= d->width)
setWidth(fm.width(text) + 4);
}
QString PushButton::text()
QString PushButton::text() const
{
return d->labelText;
}
int PushButton::height()
int PushButton::height() const
{
return d->height;
}
int PushButton::width()
int PushButton::width() const
{
return d->width;
}
void PushButton::setHeight(int h)
{
prepareGeometryChange ();
prepareGeometryChange();
d->height = h;
update();
}
void PushButton::setWidth(int w)
{
if (!(w >= d->maxWidth)) {
if (!(w >= d->maxWidth))
{
prepareGeometryChange ();
d->width = w;
update();
@ -158,26 +157,27 @@ void PushButton::setWidth(int w)
void PushButton::setIcon(const QString& path)
{
QPixmap _iconPixmap (path);
if (!path.isNull()) {
d->icon = QIcon(_iconPixmap);
d->iconSize = _iconPixmap.size();
if (!path.isNull())
{
QPixmap iconPixmap(path);
d->icon = QIcon(iconPixmap);
d->iconSize = iconPixmap.size();
d->hasIcon=true;
}else {
d->hasIcon = false;
}
else
d->hasIcon = false;
}
QSize PushButton::size()
QSize PushButton::size() const
{
return QSize(d->width,d->height);
return QSize(d->width, d->height);
}
void PushButton::setSize(QSize s)
{
prepareGeometryChange ();
if (!d->maxWidth >= s.width() ) {
prepareGeometryChange();
if (!d->maxWidth >= s.width())
d->width = s.width();
}
d->height = s.height();
update();
}
@ -189,16 +189,15 @@ void PushButton::setMaximumWidth(int w)
bool PushButton::isDown()
{
if (d->state == PushButton::PRESSED) {
if (d->state == PushButton::Pressed)
return true;
}
return false;
}
void PushButton::mousePressEvent ( QGraphicsSceneMouseEvent * event )
{
event->accept();
d->state = PushButton::PRESSED;
d->state = PushButton::Pressed;
update();
// emit clicked();
}
@ -211,7 +210,6 @@ void PushButton::mouseReleaseEvent ( QGraphicsSceneMouseEvent * event )
emit clicked();
}
QSize PushButton::sizeHint() const
{
return QSize(d->width,d->height);
@ -242,4 +240,5 @@ QRect PushButton::geometry() const
{
return boundingRect().toRect();
}
} // namespace Plasma

View File

@ -32,45 +32,68 @@
namespace Plasma
{
class KDE_EXPORT PushButton : public DataVisualization,public QGraphicsItem, public QLayoutItem
class KDE_EXPORT PushButton : public DataVisualization,
public QGraphicsItem,
public QLayoutItem
{
Q_OBJECT
public:
typedef enum {RECT=0,ROUND,COUSTOM} ButtonShape;
typedef enum {NONE,HOVER,PRESSED,RELEASED} ButtonState;
enum ButtonShape
{
Rectangle = 0,
Round,
Custom
};
enum ButtonState
{
None,
Hover,
Pressed,
Released
};
public:
PushButton(QGraphicsItem *parent = 0);
virtual ~PushButton();
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
QRectF boundingRect() const;
void setText(const QString&) ;
QString text();
QSize size();
int height();
int width();
void setIcon(const QString& path);
QString text() const;
void setText(const QString &name);
QSize size() const;
void setSize(QSize size);
void setWidth(int width);
int height() const;
void setHeight(int height);
int width() const;
void setWidth(int width);
void setIcon(const QString& path);
void setMaximumWidth(int maxwidth);
//layout stufff
QSize sizeHint() const ;
QSize minimumSize() const;
QSize maximumSize() const ;
Qt::Orientations expandingDirections() const;
void setGeometry(const QRect& r);
QRect geometry() const ;
bool isEmpty() const {return false;}
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
virtual QRectF boundingRect() const;
public slots:
//layout stufff
virtual QSize sizeHint() const ;
virtual QSize minimumSize() const;
virtual QSize maximumSize() const ;
virtual Qt::Orientations expandingDirections() const;
virtual void setGeometry(const QRect& r);
virtual QRect geometry() const ;
virtual bool isEmpty() const {return false;}
public Q_SLOTS:
void data(const DataSource::Data&);
signals:
Q_SIGNALS:
void clicked();
protected:
bool isDown();
void mousePressEvent ( QGraphicsSceneMouseEvent * event );
void mousePressEvent(QGraphicsSceneMouseEvent *event);
void mouseReleaseEvent ( QGraphicsSceneMouseEvent * event );
private:
class Private ;