icons draw text now, moved corner items to qaction based objects, and support 4 corners. still a bit messy.

svn path=/trunk/KDE/kdebase/workspace/libs/plasma/; revision=690398
This commit is contained in:
Matt Broadstone 2007-07-20 22:30:33 +00:00
parent 23c1f996d2
commit 55be0c7a1c
2 changed files with 328 additions and 130 deletions

View File

@ -43,19 +43,14 @@
namespace Plasma namespace Plasma
{ {
class Icon::Private class PrivateSvgInfo
{ {
public: public:
Private() PrivateSvgInfo()
: size(128*1.1, 128*1.1), : svg("widgets/iconbutton"),
iconSize(128, 128), svgElements(0)
state(Private::NoState),
svg("widgets/iconbutton"),
svgElements(0),
button1AnimId(0)
{ {
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();
@ -98,12 +93,37 @@ class Icon::Private
if (svg.elementExists("minibutton-pressed")) { if (svg.elementExists("minibutton-pressed")) {
svgElements |= SvgMinibuttonPressed; svgElements |= SvgMinibuttonPressed;
} }
button1Pressed = false;
button1Hovered = false;
} }
~Private() {} enum {
NoSvg = 0,
SvgBackground = 1,
SvgBackgroundHover = 2,
SvgBackgroundPressed = 4,
SvgForeground = 8,
SvgForegroundHover = 16,
SvgForegroundPressed = 32,
SvgMinibutton = 64,
SvgMinibuttonHover = 256,
SvgMinibuttonPressed = 128
};
Svg svg;
int svgElements;
};
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,
@ -111,33 +131,212 @@ class Icon::Private
PressedState PressedState
}; };
enum { NoSvg = 0,
SvgBackground = 1,
SvgBackgroundHover = 2,
SvgBackgroundPressed = 4,
SvgForeground = 8,
SvgForegroundHover = 16,
SvgForegroundPressed = 32,
SvgMinibutton = 64,
SvgMinibuttonHover = 256,
SvgMinibuttonPressed = 128};
// KUrl url;
QString text; QString text;
QSizeF size; QSizeF size;
QSizeF iconSize; QSizeF iconSize;
QIcon icon; QIcon icon;
ButtonState state; ButtonState state;
Svg svg;
int svgElements; IconAction *testAction;
//TODO: create a proper state int for this, as we do with ButtonState QHash<Icon::ActionPosition, IconAction*> cornerActions;
// for each possible button
bool button1Hovered;
bool button1Pressed;
Phase::AnimId button1AnimId;
}; };
class IconAction::Private : public PrivateSvgInfo
{
public:
Private() :
hovered(false),
pressed(false),
visible(false),
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()
{
if (d->animationId)
Phase::self()->stopElementAnimation(d->animationId);
rebuildPixmap(); // caching needs to be done
QGraphicsItem *parentItem = dynamic_cast<QGraphicsItem*>(parent()); // dangerous
d->animationId = Phase::self()->animateElement(parentItem, Phase::ElementAppear);
Phase::self()->setAnimationPixmap(d->animationId, d->pixmap);
d->visible = true;
}
void IconAction::hide()
{
if (d->animationId)
Phase::self()->stopElementAnimation(d->animationId);
rebuildPixmap(); // caching needs to be done
QGraphicsItem *parentItem = dynamic_cast<QGraphicsItem*>(parent()); // dangerous
d->animationId = Phase::self()->animateElement(parentItem, Phase::ElementDisappear);
Phase::self()->setAnimationPixmap(d->animationId, d->pixmap);
d->visible = false;
}
bool IconAction::isVisible() const
{
return d->visible;
}
bool IconAction::isPressed() const
{
return d->pressed;
}
bool IconAction::isHovered() const
{
return d->hovered;
}
void IconAction::setSelected(bool selected)
{
d->selected = selected;
}
bool IconAction::isSelected() const
{
return d->selected;
}
void IconAction::setRect(const QRectF &rect)
{
d->rect = rect;
}
QRectF IconAction::rect() const
{
return d->rect;
}
void IconAction::rebuildPixmap()
{
// Determine proper QIcon mode based on selection status
QIcon::Mode mode = QIcon::Normal;
if (d->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
d->pixmap = QPixmap(26, 26);
d->pixmap.fill(Qt::transparent);
QPainter painter(&d->pixmap);
// first paint the foreground element
if (!element.isEmpty()) {
d->svg.resize(d->pixmap.size());
d->svg.paint(&painter, 0, 0, element);
}
// then paint the icon
icon().paint(&painter, 2, 2, 22, 22, Qt::AlignCenter, mode); // more assumptions
}
bool IconAction::event(QEvent::Type type, const QPointF &pos)
{
switch (type)
{
case QEvent::MouseButtonPress:
{
setSelected(d->rect.contains(pos));
return isSelected();
}
break;
case QEvent::MouseMove:
{
bool wasSelected = isSelected();
bool active = d->rect.contains(pos);
setSelected(wasSelected && active);
return (wasSelected != isSelected()) || active;
}
break;
case QEvent::MouseButtonRelease:
{
bool wasSelected = isSelected();
setSelected(false);
if (wasSelected)
trigger();
return wasSelected;
}
break;
case QEvent::GraphicsSceneHoverEnter:
d->pressed = false;
d->hovered = true;
break;
case QEvent::GraphicsSceneHoverLeave:
d->pressed = false;
d->hovered = false;
break;
default:
break;
}
return false;
}
Phase::AnimId IconAction::animationId() const
{
return d->animationId;
}
void IconAction::paint(QPainter *painter) const
{
painter->drawPixmap(d->rect.toRect(), Phase::self()->animationResult(d->animationId));
}
Icon::Icon(QGraphicsItem *parent) Icon::Icon(QGraphicsItem *parent)
: QGraphicsItem(parent), : QGraphicsItem(parent),
d(new Private) d(new Private)
@ -171,6 +370,35 @@ void Icon::init()
{ {
setAcceptedMouseButtons(Qt::LeftButton); setAcceptedMouseButtons(Qt::LeftButton);
setAcceptsHoverEvents(true); setAcceptsHoverEvents(true);
d->testAction = new IconAction(KIcon("exec"), this);
setCornerAction(TopLeft, d->testAction);
}
void Icon::setCornerAction(ActionPosition pos, IconAction *action)
{
if (d->cornerActions[pos])
{
// TODO: do we delete it, or just clear it (watch out for leaks with the latter!)
}
d->cornerActions[pos] = action;
switch (pos)
{
case TopLeft:
{
QRectF rect(6, 6, 32, 32);
action->setRect(rect);
}
break;
case TopRight:
break;
case BottomLeft:
break;
case BottomRight:
break;
}
} }
void Icon::calculateSize() void Icon::calculateSize()
@ -288,8 +516,10 @@ void Icon::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWid
} }
// Draw top-left button // Draw top-left button
if (d->button1AnimId) { foreach (IconAction *action, d->cornerActions)
painter->drawPixmap(6, 6, Phase::self()->animationResult(d->button1AnimId)); {
if (action->animationId())
action->paint(painter);
} }
// Draw text last because its overlayed // Draw text last because its overlayed
@ -357,127 +587,62 @@ bool Icon::isDown()
void Icon::mousePressEvent(QGraphicsSceneMouseEvent *event) void Icon::mousePressEvent(QGraphicsSceneMouseEvent *event)
{ {
QRectF button1(6, 6, 32, 32); // The top-left circle foreach (IconAction *action, d->cornerActions)
d->button1Pressed = button1.contains(event->pos()); action->event(event->type(), event->pos());
if (!d->button1Pressed) {
d->state = Private::PressedState; QGraphicsItem::mousePressEvent(event);
QGraphicsItem::mousePressEvent(event);
update();
d->button1Pressed = false;
}
} }
void Icon::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) void Icon::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{ {
kDebug() << "Icon::mouseReleaseEvent" <<endl;
bool inside = boundingRect().contains(event->pos()); bool inside = boundingRect().contains(event->pos());
bool wasClicked = d->state == Private::PressedState && inside; bool wasClicked = d->state == Private::PressedState && inside;
if (inside) { if (inside) {
d->state = Private::HoverState; d->state = Private::HoverState;
QRectF button1(6, 6, 32, 32); // The top-left circle foreach (IconAction *action, d->cornerActions)
d->button1Hovered = button1.contains(event->pos()); action->event(event->type(), event->pos());
/*
if (d->button1Hovered &&
d->button1Pressed && d->url.isValid()) {
KRun::runUrl(d->url, KMimeType::findByUrl(d->url)->name(), 0);
wasClicked = false;
}
*/
} else {
d->state = Private::NoState;
}
if (wasClicked) { }
else
d->state = Private::NoState;
if (wasClicked)
{
emit pressed(false); emit pressed(false);
emit clicked(); emit clicked();
} }
d->button1Pressed = false;
QGraphicsItem::mouseReleaseEvent(event); QGraphicsItem::mouseReleaseEvent(event);
update();
}
QPixmap Icon::buttonPixmap()
{
//TODO this is just full of assumptions such as sizes and icon names. ugh!
QPixmap pix(26, 26);
pix.fill(Qt::transparent);
QPainter painter(&pix);
QString element;
if (d->svgElements & Private::SvgMinibutton) {
element = "minibutton";
}
if (d->button1Pressed) {
if (d->svgElements & Private::SvgMinibuttonPressed) {
element = "minibutton-pressed";
} else if (d->svgElements & Private::SvgMinibuttonHover) {
element = "minibutton-hover";
}
} else if (d->button1Hovered) {
if (d->svgElements & Private::SvgMinibuttonHover) {
element = "minibutton-hover";
}
}
//paint foreground element
if (!element.isEmpty()) {
//kDebug() << "painting " << element << endl;
d->svg.resize(pix.size());
d->svg.paint(&painter, 0, 0, element);
d->svg.resize(boundingRect().size());
}
KIcon exec("exec");
painter.setRenderHint(QPainter::Antialiasing);
painter.setPen(Qt::NoPen);
painter.drawPixmap(2, 2, exec.pixmap(22,22));
return pix;
} }
void Icon::hoverEnterEvent(QGraphicsSceneHoverEvent *event) void Icon::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{ {
d->state = Private::HoverState; foreach (IconAction *action, d->cornerActions)
d->button1Pressed = false; {
QRectF button1(6, 6, 32, 32); // The top-left circle action->show();
d->button1Hovered = button1.contains(event->pos()); action->event(event->type(), event->pos());
if (d->button1AnimId) {
Phase::self()->stopElementAnimation(d->button1AnimId);
} }
d->button1AnimId = Phase::self()->animateElement(this, Phase::ElementAppear); d->state = Private::HoverState;
Phase::self()->setAnimationPixmap(d->button1AnimId, buttonPixmap());
QGraphicsItem::hoverEnterEvent(event); QGraphicsItem::hoverEnterEvent(event);
} }
void Icon::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) void Icon::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{ {
d->button1Pressed = false; foreach (IconAction *action, d->cornerActions)
d->button1Hovered = false; {
action->hide();
if (d->button1AnimId) { action->event(event->type(), event->pos());
Phase::self()->stopElementAnimation(d->button1AnimId);
} }
d->button1AnimId = Phase::self()->animateElement(this, Phase::ElementDisappear);
Phase::self()->setAnimationPixmap(d->button1AnimId, buttonPixmap());
d->state = Private::NoState; d->state = Private::NoState;
QGraphicsItem::hoverLeaveEvent(event); QGraphicsItem::hoverLeaveEvent(event);
} }
void Icon::mouseMoveEvent(QGraphicsSceneMouseEvent *event) void Icon::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{ {
if (d->button1Pressed) {
d->button1Pressed = false;
}
if (d->state == Private::PressedState) { if (d->state == Private::PressedState) {
d->state = Private::HoverState; d->state = Private::HoverState;
} }
@ -485,6 +650,7 @@ void Icon::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
QGraphicsItem::mouseMoveEvent(event); QGraphicsItem::mouseMoveEvent(event);
} }
QSizeF Icon::sizeHint() const QSizeF Icon::sizeHint() const
{ {
return d->size; return d->size;

View File

@ -20,8 +20,9 @@
#ifndef ICON_H #ifndef ICON_H
#define ICON_H #define ICON_H
#include <QtCore/QObject> #include <QObject>
#include <QtGui/QGraphicsTextItem> #include <QGraphicsTextItem>
#include <QAction>
#include <plasma/dataengine.h> #include <plasma/dataengine.h>
#include <plasma/plasma_export.h> #include <plasma/plasma_export.h>
@ -35,9 +36,42 @@ class KUrl;
namespace Plasma namespace Plasma
{ {
/** class PLASMA_EXPORT IconAction : public QAction
* Icon class, for URIs and menu popups in panels {
*/ 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
@ -57,16 +91,14 @@ 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
enum Position
{ {
TopLeft = 0, TopLeft = 0,
TopRight, TopRight,
BottomLeft, BottomLeft,
BottomRight BottomRight
}; };
void setCornerAction(Position pos, QAction *action); void setCornerAction(ActionPosition pos, IconAction *action);
*/
// Layout stuff // Layout stuff
Qt::Orientations expandingDirections() const; Qt::Orientations expandingDirections() const;