* coding style guides are not a cute suggestion but a promise to be fulfilled.

* take QAction*s, which belong to whatever throws them at us
* don't polute the public API with unecessary classes
* avoid subclassing Private classes

i'm still not overly happy with this class as it stands right now.


svn path=/trunk/KDE/kdebase/workspace/libs/plasma/; revision=690430
This commit is contained in:
Aaron J. Seigo 2007-07-21 04:07:41 +00:00
parent 001cce34dc
commit 01ab982d54
2 changed files with 228 additions and 221 deletions

View File

@ -22,9 +22,9 @@
#include <QGraphicsSceneMouseEvent> #include <QGraphicsSceneMouseEvent>
#include <QGraphicsView> #include <QGraphicsView>
//#define PROVE_IT_CAN_BE_DONE //#define BACKINGSTORE_BLUR_HACK
#ifdef PROVE_IT_CAN_BE_DONE #ifdef BACKINGSTORE_BLUR_HACK
#include <private/qwindowsurface_p.h> #include <private/qwindowsurface_p.h>
#endif #endif
@ -43,19 +43,68 @@
namespace Plasma namespace Plasma
{ {
class PrivateSvgInfo class PLASMA_EXPORT IconAction
{ {
public: public:
PrivateSvgInfo() IconAction(Icon* icon, QAction* action);
void show();
void hide();
bool isVisible() const;
Phase::AnimId animationId() const;
QAction* action() const;
void paint(QPainter *painter) const;
bool event(QEvent::Type type, const QPointF &pos);
void setSelected(bool selected);
bool isSelected() const;
bool isHovered() const;
bool isPressed() const;
void setRect(const QRectF &rect);
QRectF rect() const;
private:
void rebuildPixmap();
Icon* m_icon;
QAction* m_action;
QPixmap m_pixmap;
QRectF m_rect;
bool m_hovered;
bool m_pressed;
bool m_selected;
bool m_visible;
Phase::AnimId m_animationId;
};
class Icon::Private
{
public:
Private()
: svg("widgets/iconbutton"), : svg("widgets/iconbutton"),
svgElements(0) svgElements(0),
size(128*1.1, 128*1.1),
iconSize(128, 128),
state(Private::NoState)
{ {
svg.setContentType(Plasma::Svg::ImageSet); svg.setContentType(Plasma::Svg::ImageSet);
svg.resize(size);
//TODO: recheck when svg changes //TODO: recheck when svg changes
checkSvgElements(); checkSvgElements();
} }
~Private()
{
qDeleteAll(cornerActions);
}
void checkSvgElements() void checkSvgElements()
{ {
if (svg.elementExists("background")) { if (svg.elementExists("background")) {
@ -104,26 +153,18 @@ class PrivateSvgInfo
SvgForegroundHover = 16, SvgForegroundHover = 16,
SvgForegroundPressed = 32, SvgForegroundPressed = 32,
SvgMinibutton = 64, SvgMinibutton = 64,
SvgMinibuttonHover = 256, SvgMinibuttonHover = 128,
SvgMinibuttonPressed = 128 SvgMinibuttonPressed = 256
}; };
Svg svg; enum ActionPosition {
int svgElements; TopLeft = 0,
TopRight,
BottomLeft,
BottomRight,
LastIconPosition
}; };
class Icon::Private : public PrivateSvgInfo
{
public:
Private()
: size(128*1.1, 128*1.1),
iconSize(128, 128),
state(Private::NoState)
{
svg.resize(size);
}
enum ButtonState enum ButtonState
{ {
NoState, NoState,
@ -132,189 +173,148 @@ class Icon::Private : public PrivateSvgInfo
}; };
QString text; QString text;
Svg svg;
int svgElements;
QSizeF size; QSizeF size;
QSizeF iconSize; QSizeF iconSize;
QIcon icon; QIcon icon;
ButtonState state; ButtonState state;
IconAction *testAction; QAction *testAction;
QHash<Icon::ActionPosition, IconAction*> cornerActions; QList<IconAction*> cornerActions;
}; };
class IconAction::Private : public PrivateSvgInfo IconAction::IconAction(Icon* icon, QAction *action)
{ : m_icon(icon),
public: m_action(action),
Private() : m_hovered(false),
hovered(false), m_pressed(false),
pressed(false), m_visible(false),
visible(false), m_animationId(-1)
animationId(-1)
{}
QPixmap pixmap;
QRectF rect;
bool hovered;
bool pressed;
bool selected;
bool visible;
Phase::AnimId animationId;
};
IconAction::IconAction(QObject *parent)
: QAction(parent),
d(new Private)
{
}
IconAction::IconAction(const QIcon &icon, QObject *parent)
: QAction(icon, QString(), parent),
d(new Private)
{ {
} }
void IconAction::show() void IconAction::show()
{ {
if (d->animationId) if (m_animationId) {
Phase::self()->stopElementAnimation(d->animationId); Phase::self()->stopElementAnimation(m_animationId);
}
rebuildPixmap(); // caching needs to be done rebuildPixmap();
QGraphicsItem *parentItem = dynamic_cast<QGraphicsItem*>(parent()); // dangerous m_animationId = Phase::self()->animateElement(m_icon, Phase::ElementAppear);
d->animationId = Phase::self()->animateElement(parentItem, Phase::ElementAppear); Phase::self()->setAnimationPixmap(m_animationId, m_pixmap);
Phase::self()->setAnimationPixmap(d->animationId, d->pixmap); m_visible = true;
d->visible = true;
} }
void IconAction::hide() void IconAction::hide()
{ {
if (d->animationId) if (m_animationId) {
Phase::self()->stopElementAnimation(d->animationId); Phase::self()->stopElementAnimation(m_animationId);
}
rebuildPixmap(); // caching needs to be done rebuildPixmap();
QGraphicsItem *parentItem = dynamic_cast<QGraphicsItem*>(parent()); // dangerous m_animationId = Phase::self()->animateElement(m_icon, Phase::ElementDisappear);
d->animationId = Phase::self()->animateElement(parentItem, Phase::ElementDisappear); Phase::self()->setAnimationPixmap(m_animationId, m_pixmap);
Phase::self()->setAnimationPixmap(d->animationId, d->pixmap); m_visible = false;
d->visible = false;
} }
bool IconAction::isVisible() const bool IconAction::isVisible() const
{ {
return d->visible; return m_visible;
} }
bool IconAction::isPressed() const bool IconAction::isPressed() const
{ {
return d->pressed; return m_pressed;
} }
bool IconAction::isHovered() const bool IconAction::isHovered() const
{ {
return d->hovered; return m_hovered;
} }
void IconAction::setSelected(bool selected) void IconAction::setSelected(bool selected)
{ {
d->selected = selected; m_selected = selected;
} }
bool IconAction::isSelected() const bool IconAction::isSelected() const
{ {
return d->selected; return m_selected;
} }
void IconAction::setRect(const QRectF &rect) void IconAction::setRect(const QRectF &rect)
{ {
d->rect = rect; m_rect = rect;
} }
QRectF IconAction::rect() const QRectF IconAction::rect() const
{ {
return d->rect; return m_rect;
} }
void IconAction::rebuildPixmap() void IconAction::rebuildPixmap()
{ {
// Determine proper QIcon mode based on selection status // Determine proper QIcon mode based on selection status
QIcon::Mode mode = QIcon::Normal; QIcon::Mode mode = QIcon::Normal;
if (d->selected) if (m_selected) {
mode = QIcon::Selected; mode = QIcon::Selected;
// Determine proper svg element
QString element;
if (d->svgElements & Private::SvgMinibutton)
element = "minibutton";
if (d->pressed)
{
if (d->svgElements & Private::SvgMinibuttonPressed)
element = "minibutton-pressed";
else if (d->svgElements & Private::SvgMinibuttonHover)
element = "minibutton-hover";
}
else if (d->hovered)
{
if (d->svgElements & Private::SvgMinibuttonHover)
element = "minibutton-hover";
} }
// Draw everything // Draw everything
d->pixmap = QPixmap(26, 26); m_pixmap = QPixmap(26, 26);
d->pixmap.fill(Qt::transparent); m_pixmap.fill(Qt::transparent);
QPainter painter(&d->pixmap); int element = Icon::Private::SvgMinibutton;
// first paint the foreground element if (m_pressed) {
if (!element.isEmpty()) { element = Icon::Private::SvgMinibuttonPressed;
d->svg.resize(d->pixmap.size()); } else if (m_hovered) {
d->svg.paint(&painter, 0, 0, element); element = Icon::Private::SvgMinibuttonHover;
} }
// then paint the icon QPainter painter(&m_pixmap);
icon().paint(&painter, 2, 2, 22, 22, Qt::AlignCenter, mode); // more assumptions m_icon->drawActionButtonBase(&painter, m_pixmap.size(), element);
m_action->icon().paint(&painter, 2, 2, 22, 22, Qt::AlignCenter, mode);
} }
bool IconAction::event(QEvent::Type type, const QPointF &pos) bool IconAction::event(QEvent::Type type, const QPointF &pos)
{ {
switch (type) switch (type) {
{ case QEvent::MouseButtonPress: {
case QEvent::MouseButtonPress: setSelected(m_rect.contains(pos));
{
setSelected(d->rect.contains(pos));
return isSelected(); return isSelected();
} }
break; break;
case QEvent::MouseMove: case QEvent::MouseMove: {
{
bool wasSelected = isSelected(); bool wasSelected = isSelected();
bool active = d->rect.contains(pos); bool active = m_rect.contains(pos);
setSelected(wasSelected && active); setSelected(wasSelected && active);
return (wasSelected != isSelected()) || active; return (wasSelected != isSelected()) || active;
} }
break; break;
case QEvent::MouseButtonRelease: case QEvent::MouseButtonRelease: {
{
bool wasSelected = isSelected(); bool wasSelected = isSelected();
setSelected(false); setSelected(false);
if (wasSelected) if (wasSelected) {
trigger(); m_action->trigger();
}
return wasSelected; return wasSelected;
} }
break; break;
case QEvent::GraphicsSceneHoverEnter: case QEvent::GraphicsSceneHoverEnter:
d->pressed = false; m_pressed = false;
d->hovered = true; m_hovered = true;
break; break;
case QEvent::GraphicsSceneHoverLeave: case QEvent::GraphicsSceneHoverLeave:
d->pressed = false; m_pressed = false;
d->hovered = false; m_hovered = false;
break; break;
default: default:
@ -326,17 +326,19 @@ bool IconAction::event(QEvent::Type type, const QPointF &pos)
Phase::AnimId IconAction::animationId() const Phase::AnimId IconAction::animationId() const
{ {
return d->animationId; return m_animationId;
}
QAction* IconAction::action() const
{
return m_action;
} }
void IconAction::paint(QPainter *painter) const void IconAction::paint(QPainter *painter) const
{ {
painter->drawPixmap(d->rect.toRect(), Phase::self()->animationResult(d->animationId)); painter->drawPixmap(m_rect.toRect(), Phase::self()->animationResult(m_animationId));
} }
Icon::Icon(QGraphicsItem *parent) Icon::Icon(QGraphicsItem *parent)
: QGraphicsItem(parent), : QGraphicsItem(parent),
d(new Private) d(new Private)
@ -371,33 +373,44 @@ void Icon::init()
setAcceptedMouseButtons(Qt::LeftButton); setAcceptedMouseButtons(Qt::LeftButton);
setAcceptsHoverEvents(true); setAcceptsHoverEvents(true);
d->testAction = new IconAction(KIcon("exec"), this); d->testAction = new QAction(KIcon("exec"), i18n("Open"), this);
setCornerAction(TopLeft, d->testAction); addAction(d->testAction);
} }
void Icon::setCornerAction(ActionPosition pos, IconAction *action) void Icon::addAction(QAction *action)
{ {
if (d->cornerActions[pos]) int count = d->cornerActions.count();
{ if (count > 3) {
// TODO: do we delete it, or just clear it (watch out for leaks with the latter!) kDebug() << "Icon::addAction(QAction*) no more room for more actions!" << endl;
} }
d->cornerActions[pos] = action; IconAction* iconAction = new IconAction(this, action);
switch (pos) d->cornerActions.append(iconAction);
{ connect(action, SIGNAL(destroyed(QObject*)), this, SLOT(actionDestroyed(QObject*)));
case TopLeft:
{
QRectF rect(6, 6, 32, 32);
action->setRect(rect);
}
break;
case TopRight: //FIXME: set other icon rects properly, and shift them when we change our size
switch (count) {
case Private::TopLeft:
iconAction->setRect(QRectF(6, 6, 32, 32));
break; break;
case BottomLeft: case Private::TopRight:
break; break;
case BottomRight: case Private::BottomLeft:
break; break;
case Private::BottomRight:
break;
}
}
void Icon::actionDestroyed(QObject* action)
{
QList<IconAction*>::iterator it = d->cornerActions.begin();
while (it != d->cornerActions.end()) {
if ((*it)->action() == action) {
d->cornerActions.erase(it);
break;
}
} }
} }
@ -425,7 +438,7 @@ void Icon::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWid
Q_UNUSED(option) Q_UNUSED(option)
Q_UNUSED(widget) Q_UNUSED(widget)
#ifdef PROVE_IT_CAN_BE_DONE #ifdef BACKINGSTORE_BLUR_HACK
if (d->state == Private::HoverState && scene()) { if (d->state == Private::HoverState && scene()) {
QList<QGraphicsView*> views = scene()->views(); QList<QGraphicsView*> views = scene()->views();
if (views.count() > 0) { if (views.count() > 0) {
@ -441,6 +454,8 @@ void Icon::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWid
} }
#endif #endif
d->svg.resize(d->size);
QString element; QString element;
if (d->svgElements & Private::SvgBackground) { if (d->svgElements & Private::SvgBackground) {
element = "background"; element = "background";
@ -516,11 +531,11 @@ void Icon::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWid
} }
// Draw top-left button // Draw top-left button
foreach (IconAction *action, d->cornerActions) foreach (IconAction *action, d->cornerActions) {
{ if (action->animationId()) {
if (action->animationId())
action->paint(painter); action->paint(painter);
} }
}
// Draw text last because its overlayed // Draw text last because its overlayed
if (!d->text.isEmpty()) { if (!d->text.isEmpty()) {
@ -537,6 +552,32 @@ void Icon::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWid
} }
void Icon::drawActionButtonBase(QPainter* painter, const QSize &size, int element)
{
// Determine proper svg element
QString id;
if (d->svgElements & Private::SvgMinibutton) {
id = "minibutton";
}
if (element == Private::SvgMinibuttonPressed) {
if (d->svgElements & Private::SvgMinibuttonPressed) {
id = "minibutton-pressed";
} else if (d->svgElements & Private::SvgMinibuttonHover) {
id = "minibutton-hover";
}
} else if (element == Icon::Private::SvgMinibuttonHover &&
d->svgElements & Private::SvgMinibuttonHover) {
id = "minibutton-hover";
}
if (!id.isEmpty()) {
d->svg.resize(size);
d->svg.paint(painter, 0, 0, id);
}
}
void Icon::setText(const QString& text) void Icon::setText(const QString& text)
{ {
d->text = text; d->text = text;
@ -587,8 +628,9 @@ bool Icon::isDown()
void Icon::mousePressEvent(QGraphicsSceneMouseEvent *event) void Icon::mousePressEvent(QGraphicsSceneMouseEvent *event)
{ {
foreach (IconAction *action, d->cornerActions) foreach (IconAction *action, d->cornerActions) {
action->event(event->type(), event->pos()); action->event(event->type(), event->pos());
}
QGraphicsItem::mousePressEvent(event); QGraphicsItem::mousePressEvent(event);
} }
@ -601,15 +643,15 @@ void Icon::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
if (inside) { if (inside) {
d->state = Private::HoverState; d->state = Private::HoverState;
foreach (IconAction *action, d->cornerActions) foreach (IconAction *action, d->cornerActions) {
action->event(event->type(), event->pos()); action->event(event->type(), event->pos());
} }
else
d->state = Private::NoState;
if (wasClicked) } else {
{ d->state = Private::NoState;
}
if (wasClicked) {
emit pressed(false); emit pressed(false);
emit clicked(); emit clicked();
} }
@ -619,8 +661,7 @@ void Icon::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
void Icon::hoverEnterEvent(QGraphicsSceneHoverEvent *event) void Icon::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{ {
foreach (IconAction *action, d->cornerActions) foreach (IconAction *action, d->cornerActions) {
{
action->show(); action->show();
action->event(event->type(), event->pos()); action->event(event->type(), event->pos());
} }
@ -631,8 +672,7 @@ void Icon::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
void Icon::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) void Icon::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{ {
foreach (IconAction *action, d->cornerActions) foreach (IconAction *action, d->cornerActions) {
{
action->hide(); action->hide();
action->event(event->type(), event->pos()); action->event(event->type(), event->pos());
} }

View File

@ -37,42 +37,6 @@ class KUrl;
namespace Plasma namespace Plasma
{ {
class PLASMA_EXPORT IconAction : public QAction
{
Q_OBJECT
public:
IconAction(QObject *parent = 0);
IconAction(const QIcon &icon, QObject *parent = 0);
void show();
void hide();
bool isVisible() const;
Phase::AnimId animationId() const;
void paint(QPainter *painter) const;
bool event(QEvent::Type type, const QPointF &pos);
void setSelected(bool selected);
bool isSelected() const;
bool isHovered() const;
bool isPressed() const;
void setRect(const QRectF &rect);
QRectF rect() const;
signals:
void clicked();
private:
void rebuildPixmap();
class Private;
Private * const d;
};
class PLASMA_EXPORT Icon : public QObject, public QGraphicsItem, public LayoutItem class PLASMA_EXPORT Icon : public QObject, public QGraphicsItem, public LayoutItem
{ {
Q_OBJECT Q_OBJECT
@ -92,14 +56,7 @@ class PLASMA_EXPORT Icon : public QObject, public QGraphicsItem, public LayoutIt
void setIconSize(const QSizeF& size); void setIconSize(const QSizeF& size);
void setIconSize(int height, int width); void setIconSize(int height, int width);
enum ActionPosition void addAction(QAction* action);
{
TopLeft = 0,
TopRight,
BottomLeft,
BottomRight
};
void setCornerAction(ActionPosition pos, IconAction *action);
// Layout stuff // Layout stuff
Qt::Orientations expandingDirections() const; Qt::Orientations expandingDirections() const;
@ -135,13 +92,23 @@ class PLASMA_EXPORT Icon : public QObject, public QGraphicsItem, public LayoutIt
void hoverEnterEvent(QGraphicsSceneHoverEvent *event); void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
void hoverLeaveEvent(QGraphicsSceneHoverEvent *event); void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
public:
class Private;
/**
* @internal
**/
void drawActionButtonBase(QPainter* painter, const QSize &size, int element);
private: private:
QPixmap buttonPixmap(); QPixmap buttonPixmap();
void init(); void init();
void calculateSize(); void calculateSize();
class Private;
Private * const d; Private * const d;
private Q_SLOTS:
void actionDestroyed(QObject* obj);
}; };
} // namespace Plasma } // namespace Plasma