icon buttons generally work now. huzzah. you can theme them with iconbutton.svg with the following elements, all of which are optional:

- background
 - background-hover
 - background-pressed
 - foreground
 - foregound-hover
 - foreground-pressed

what each does is left as an exercise for the reader ;)

svn path=/trunk/KDE/kdebase/workspace/plasma/lib/; revision=669620
This commit is contained in:
Aaron J. Seigo 2007-05-29 20:37:21 +00:00
parent c321d2f03e
commit 77307ba1dd
3 changed files with 129 additions and 57 deletions

View File

@ -1,4 +1,4 @@
include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/widgets) include_directories(${CMAKE_CURRENT_SOURCE_DIR})
########### next target ############### ########### next target ###############

View File

@ -32,31 +32,65 @@ class Icon::Private
{ {
public: public:
Private() Private()
: size(40, 40), : size(128*1.1, 128*1.1),
state(Icon::None), state(Private::NoState),
background("widgets/iconbg"), svg("widgets/iconbutton")
backgroundPressed("widgets/iconbgpressed"),
foreground("widgets/iconfg"),
foregroundHover("widgets/iconfghover"),
foregroundPressed("widgets/iconfgpressed")
{ {
svg.setContentType(Plasma::Svg::ImageSet);
svg.resize(size);
minSize = size; minSize = size;
maxSize = size; maxSize = size;
state = Icon::None;
if (svg.elementExists("background")) {
svgElements |= SvgBackground;
}
if (svg.elementExists("background-hover")) {
svgElements |= SvgBackgroundHover;
}
if (svg.elementExists("background-pressed")) {
svgElements |= SvgBackgroundPressed;
}
if (svg.elementExists("foreground")) {
svgElements |= SvgForeground;
}
if (svg.elementExists("foreground-hover")) {
svgElements |= SvgForegroundHover;
}
if (svg.elementExists("foreground-pressed")) {
svgElements |= SvgForegroundPressed;
}
} }
~Private() {} ~Private() {}
enum ButtonState
{
NoState,
HoverState,
PressedState
};
enum { NoSvg = 0,
SvgBackground = 1,
SvgBackgroundHover = 2,
SvgBackgroundPressed = 4,
SvgForeground = 8,
SvgForegroundHover = 16,
SvgForegroundPressed = 32 };
QString text; QString text;
QIcon icon; QIcon icon;
QSizeF size; QSizeF size;
QSizeF minSize; QSizeF minSize;
QSizeF maxSize; QSizeF maxSize;
Icon::ButtonState state; ButtonState state;
Svg background; Svg svg;
Svg backgroundPressed; int svgElements;
Svg foreground;
Svg foregroundHover;
Svg foregroundPressed;
}; };
Icon::Icon(QGraphicsItem *parent) Icon::Icon(QGraphicsItem *parent)
@ -77,7 +111,7 @@ Icon::~Icon()
QRectF Icon::boundingRect() const QRectF Icon::boundingRect() const
{ {
return QRectF(pos(), d->size); return QRectF(QPointF(0, 0), d->size);
} }
void Icon::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) void Icon::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
@ -85,37 +119,66 @@ void Icon::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWid
Q_UNUSED(option) Q_UNUSED(option)
Q_UNUSED(widget) Q_UNUSED(widget)
QRectF rect = boundingRect(); // QRectF rect = boundingRect();
QString element;
if (d->svgElements & Private::SvgBackground) {
element = "background";
}
switch (d->state) { switch (d->state) {
case None: case Private::NoState:
case Hover:
d->background.paint(painter, 0, 0);
break; break;
case Pressed: case Private::HoverState:
d->backgroundPressed.paint(painter, 0, 0); if (d->svgElements & Private::SvgBackgroundHover) {
element = "background-hover";
}
break;
case Private::PressedState:
if (d->svgElements & Private::SvgBackgroundPressed) {
element = "background-pressed";
}
break; break;
} }
if (!element.isEmpty()) {
//kDebug() << "painting " << element << endl;
d->svg.paint(painter, 0, 0, element);
element = QString();
}
if (!d->icon.isNull()) { if (!d->icon.isNull()) {
int deltaX = d->size.width() * 0.05; int deltaX = d->size.width() * 0.1;
int deltaY = d->size.height() * 0.05; int deltaY = d->size.height() * 0.1;
painter->drawPixmap(deltaX, deltaY, d->icon.pixmap((d->size * 0.9).toSize())); painter->drawPixmap(deltaX, deltaY, d->icon.pixmap((d->size * 0.9).toSize()));
} }
//TODO: draw text //TODO: draw text
if (d->svgElements & Private::SvgForeground) {
element = "foreground";
}
switch (d->state) { switch (d->state) {
case None: case Private::NoState:
d->foreground.paint(painter, 0, 0);
break; break;
case Hover: case Private::HoverState:
d->foregroundHover.paint(painter, 0, 0); if (d->svgElements & Private::SvgForegroundHover) {
element = "foreground-hover";
}
break; break;
case Pressed: case Private::PressedState:
d->foregroundPressed.paint(painter, 0, 0); if (d->svgElements & Private::SvgForegroundPressed) {
element = "foreground-pressed";
}
break; break;
} }
if (!element.isEmpty()) {
//kDebug() << "painting " << element << endl;
d->svg.paint(painter, 0, 0, element);
}
} }
void Icon::setText(const QString& text) void Icon::setText(const QString& text)
@ -154,11 +217,7 @@ void Icon::setSize(const QSizeF& s)
{ {
prepareGeometryChange(); prepareGeometryChange();
d->size = s.boundedTo(d->maxSize); //FIXME: maxSize always == size means it can be changed. wtf. =) d->size = s.boundedTo(d->maxSize); //FIXME: maxSize always == size means it can be changed. wtf. =)
d->background.resize(s); d->svg.resize(d->size);
d->backgroundPressed.resize(s);
d->foreground.resize(s);
d->foregroundHover.resize(s);
d->foregroundPressed.resize(s);
update(); update();
} }
@ -169,28 +228,46 @@ void Icon::setSize(int w, int h)
bool Icon::isDown() bool Icon::isDown()
{ {
return d->state == Icon::Pressed; return d->state == Private::PressedState;
} }
void Icon::mousePressEvent(QGraphicsSceneMouseEvent *event) void Icon::mousePressEvent(QGraphicsSceneMouseEvent *event)
{ {
event->accept(); d->state = Private::PressedState;
d->state = Icon::Pressed;
update();
emit pressed(true); emit pressed(true);
QGraphicsItem::mousePressEvent(event);
update();
} }
void Icon::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) void Icon::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{ {
event->accept(); bool inside = boundingRect().contains(event->pos());
bool wasClicked = d->state == Icon::Pressed && boundingRect().contains(event->scenePos()); bool wasClicked = d->state == Private::PressedState && inside;
d->state = Icon::None;
update(); if (inside) {
d->state = Private::HoverState;
} else {
d->state = Private::NoState;
}
if (wasClicked) { if (wasClicked) {
emit pressed(false); emit pressed(false);
emit clicked(); emit clicked();
} }
QGraphicsItem::mouseReleaseEvent(event);
update();
}
void Icon::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
d->state = Private::HoverState;
QGraphicsItem::hoverEnterEvent(event);
}
void Icon::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
d->state = Private::NoState;
QGraphicsItem::hoverLeaveEvent(event);
} }
QSizeF Icon::sizeHint() const QSizeF Icon::sizeHint() const

View File

@ -22,7 +22,7 @@
#include <QtCore/QObject> #include <QtCore/QObject>
#include <QtGui/QGraphicsTextItem> #include <QtGui/QGraphicsTextItem>
#include <layoutitem.h> #include <widgets/layoutitem.h>
#include <dataengine.h> #include <dataengine.h>
#include <plasma_export.h> #include <plasma_export.h>
@ -41,13 +41,6 @@ class PLASMA_EXPORT Icon : public QObject,
{ {
Q_OBJECT Q_OBJECT
public: public:
enum ButtonState
{
None,
Hover,
Pressed
};
Icon(QGraphicsItem *parent = 0); Icon(QGraphicsItem *parent = 0);
virtual ~Icon(); virtual ~Icon();
@ -65,10 +58,10 @@ class PLASMA_EXPORT Icon : public QObject,
QRectF boundingRect() const; QRectF boundingRect() const;
//layout stufff //layout stufff
Qt::Orientations expandingDirections() const = 0; Qt::Orientations expandingDirections() const;
QSizeF minimumSize() const = 0; QSizeF minimumSize() const;
QSizeF maximumSize() const = 0; QSizeF maximumSize() const;
bool hasHeightForWidth() const; bool hasHeightForWidth() const;
qreal heightForWidth(qreal w) const; qreal heightForWidth(qreal w) const;
@ -76,10 +69,10 @@ class PLASMA_EXPORT Icon : public QObject,
bool hasWidthForHeight() const; bool hasWidthForHeight() const;
qreal widthForHeight(qreal h) const; qreal widthForHeight(qreal h) const;
QRectF geometry() const = 0; QRectF geometry() const;
void setGeometry(const QRectF& r) = 0; void setGeometry(const QRectF& r);
QSizeF sizeHint() const = 0; QSizeF sizeHint() const;
Q_SIGNALS: Q_SIGNALS:
void pressed(bool down); void pressed(bool down);
@ -89,6 +82,8 @@ class PLASMA_EXPORT Icon : public QObject,
bool isDown(); bool isDown();
void mousePressEvent(QGraphicsSceneMouseEvent *event); void mousePressEvent(QGraphicsSceneMouseEvent *event);
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
void hoverEnterEvent (QGraphicsSceneHoverEvent * event);
void hoverLeaveEvent (QGraphicsSceneHoverEvent * event);
private: private:
class Private; class Private;