From 72dbcaf4f012dbfe1db31a01a4a02480a046deb6 Mon Sep 17 00:00:00 2001 From: Matt Broadstone Date: Tue, 13 Mar 2007 03:00:09 +0000 Subject: [PATCH] Clean up the push button class svn path=/trunk/KDE/kdebase/workspace/plasma/lib/; revision=642016 --- applet.h | 6 ++ datavisualization.h | 3 +- widgets/pushbutton.cpp | 123 ++++++++++++++++++++--------------------- widgets/pushbutton.h | 71 ++++++++++++++++-------- 4 files changed, 115 insertions(+), 88 deletions(-) diff --git a/applet.h b/applet.h index b37ca1615..eae8fe5c1 100644 --- a/applet.h +++ b/applet.h @@ -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("libplasmaapplet_" #libname)) + + } // Plasma namespace #endif // multiple inclusion guard diff --git a/datavisualization.h b/datavisualization.h index c7f9a9ed6..0d1b19812 100644 --- a/datavisualization.h +++ b/datavisualization.h @@ -29,8 +29,7 @@ namespace Plasma // this will end up being multiple-inherited? class KDE_EXPORT DataVisualization : public QObject { - Q_OBJECT - + Q_OBJECT public: DataVisualization(QObject* parent = 0); virtual ~DataVisualization(); diff --git a/widgets/pushbutton.cpp b/widgets/pushbutton.cpp index cedb3d97e..eea905afa 100644 --- a/widgets/pushbutton.cpp +++ b/widgets/pushbutton.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #include "pushbutton.h" #include "pushbutton.moc" @@ -32,24 +33,25 @@ namespace Plasma class PushButton::Private { public: - Private() { }; - ~Private() { }; - QString labelText; - QString labelIcon; - QColor labelTextColor; - QIcon icon; - QSize iconSize; - bool hasIcon; - int labelTextOpacity; - int height; - int width; - int maxWidth; - int minWidth; - int minHeight; - int maxHeight; - int radius; - QTimer * updateTimer; - PushButton::ButtonState state; + Private() {} + ~Private() {} + + QString labelText; + QString labelIcon; + QColor labelTextColor; + QIcon icon; + QSize iconSize; + bool hasIcon; + int labelTextOpacity; + int height; + int width; + int maxWidth; + int minWidth; + int minHeight; + int maxHeight; + int radius; + QTimer * updateTimer; + PushButton::ButtonState state; }; PushButton::PushButton(QGraphicsItem *parent) @@ -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,68 +116,68 @@ 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)) { - prepareGeometryChange (); - d->width = w; - update(); + if (!(w >= d->maxWidth)) + { + prepareGeometryChange (); + d->width = w; + update(); } } void PushButton::setIcon(const QString& path) { - QPixmap _iconPixmap (path); - if (!path.isNull()) { - d->icon = QIcon(_iconPixmap); - d->iconSize = _iconPixmap.size(); - d->hasIcon=true; - }else { - d->hasIcon = false; + if (!path.isNull()) + { + QPixmap iconPixmap(path); + d->icon = QIcon(iconPixmap); + d->iconSize = iconPixmap.size(); + d->hasIcon=true; } + 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) { - return true; - } + 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 diff --git a/widgets/pushbutton.h b/widgets/pushbutton.h index 6ae64978f..8a89fff3e 100644 --- a/widgets/pushbutton.h +++ b/widgets/pushbutton.h @@ -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 + 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 ;