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 ###############

View File

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

View File

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