Much nicer icons.

Implemented the concept for icons from David Vignoni (screencast coming...).
Context menu now shows "Plasma".

svn path=/trunk/KDE/kdebase/workspace/libs/plasma/; revision=676455
This commit is contained in:
Riccardo Iaconelli 2007-06-16 22:35:31 +00:00
parent 5a3e39e685
commit 7a70eac7fe
3 changed files with 132 additions and 25 deletions

View File

@ -257,6 +257,7 @@ void Corona::dropEvent(QGraphicsSceneDragDropEvent *event)
foreach (const KUrl& url, urls) { foreach (const KUrl& url, urls) {
Plasma::Icon *icon = new Plasma::Icon(0); Plasma::Icon *icon = new Plasma::Icon(0);
icon->setIcon(KMimeType::iconNameForUrl(url)); icon->setIcon(KMimeType::iconNameForUrl(url));
icon->setUrl(url);
icon->setSize(128,128); icon->setSize(128,128);
//TODO: associate the url with the icon, use the Button plasmoid here //TODO: associate the url with the icon, use the Button plasmoid here
icon->setPos(event->scenePos()-QPoint(icon->boundingRect().width()/2,icon->boundingRect().height()/2)); icon->setPos(event->scenePos()-QPoint(icon->boundingRect().width()/2,icon->boundingRect().height()/2));
@ -291,7 +292,7 @@ void Corona::contextMenuEvent(QGraphicsSceneContextMenuEvent *contextMenuEvent)
return; return;
} }
desktopMenu.setTitle("Corona"); desktopMenu.addTitle("Plasma");
desktopMenu.addAction(d->engineExplorerAction); desktopMenu.addAction(d->engineExplorerAction);
} else { } else {
desktopMenu.addTitle(applet->name()); desktopMenu.addTitle(applet->name());

View File

@ -18,11 +18,12 @@
#include "icon.h" #include "icon.h"
#include <QTimeLine>
#include <QPainter> #include <QPainter>
#include <QGraphicsSceneMouseEvent> #include <QGraphicsSceneMouseEvent>
#include <QGraphicsView> #include <QGraphicsView>
//#define PROVE_IT_CAN_BE_DONE // #define PROVE_IT_CAN_BE_DONE
#ifdef PROVE_IT_CAN_BE_DONE #ifdef PROVE_IT_CAN_BE_DONE
#include <private/qwindowsurface_p.h> #include <private/qwindowsurface_p.h>
@ -30,6 +31,9 @@
#include <KIcon> #include <KIcon>
#include <KImageEffect> #include <KImageEffect>
#include <KUrl>
#include <KRun>
#include <KMimeType>
#include <KDebug> #include <KDebug>
#include "svg.h" #include "svg.h"
@ -73,7 +77,10 @@ class Icon::Private
if (svg.elementExists("foreground-pressed")) { if (svg.elementExists("foreground-pressed")) {
svgElements |= SvgForegroundPressed; svgElements |= SvgForegroundPressed;
} }
stepMenu = 0;
timeline = new QTimeLine;
button1Pressed = false;
button1Hovered = false;
} }
~Private() {} ~Private() {}
enum ButtonState enum ButtonState
@ -92,13 +99,18 @@ class Icon::Private
SvgForegroundHover = 16, SvgForegroundHover = 16,
SvgForegroundPressed = 32 }; SvgForegroundPressed = 32 };
KUrl url;
QString text; QString text;
QSizeF size; QSizeF size;
QSizeF iconSize; QSizeF iconSize;
QIcon icon; QIcon icon;
QTimeLine *timeline;
ButtonState state; ButtonState state;
Svg svg; Svg svg;
int svgElements; int svgElements;
int stepMenu;
bool button1Hovered;
bool button1Pressed;
}; };
Icon::Icon(QGraphicsItem *parent) Icon::Icon(QGraphicsItem *parent)
@ -111,6 +123,7 @@ Icon::Icon(QGraphicsItem *parent)
setEnabled(true); setEnabled(true);
setFlags(ItemIsMovable); setFlags(ItemIsMovable);
setPos(QPointF(0.0,0.0)); setPos(QPointF(0.0,0.0));
d->timeline->setDuration(500);
} }
Icon::~Icon() Icon::~Icon()
@ -129,23 +142,24 @@ void Icon::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWid
Q_UNUSED(widget) Q_UNUSED(widget)
#ifdef PROVE_IT_CAN_BE_DONE #ifdef PROVE_IT_CAN_BE_DONE
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) {
QPixmap* pix = static_cast<QPixmap*>(views[0]->windowSurface()->paintDevice()); // QPixmap* pix = static_cast<QPixmap*>(views[0]->windowSurface()->paintDevice());
QImage image(boundingRect().size().toSize(), QImage::Format_ARGB32_Premultiplied); // QImage image(boundingRect().size().toSize(), QImage::Format_ARGB32_Premultiplied);
{ // {
QPainter p(&image); // QPainter p(&image);
p.drawPixmap(image.rect(), *pix, sceneBoundingRect()); // p.drawPixmap(image.rect(), *pix, sceneBoundingRect());
} // }
expblur<16,7>(image, 10); // expblur<16,7>(image, 8);
painter->save(); // painter->save();
painter->drawImage(0, 0, image); // painter->drawImage(0, 0, image);
painter->restore(); // painter->restore();
} // }
} // }
#endif #endif
QString element; QString element;
if (d->svgElements & Private::SvgBackground) { if (d->svgElements & Private::SvgBackground) {
element = "background"; element = "background";
@ -153,6 +167,7 @@ void Icon::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWid
switch (d->state) { switch (d->state) {
case Private::NoState: case Private::NoState:
element = "background";
break; break;
case Private::HoverState: case Private::HoverState:
if (d->svgElements & Private::SvgBackgroundHover) { if (d->svgElements & Private::SvgBackgroundHover) {
@ -185,24 +200,26 @@ void Icon::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWid
painter->scale(0.95, 0.95); painter->scale(0.95, 0.95);
deltaY *= 0.95; deltaY *= 0.95;
deltaX *= 0.95; deltaX *= 0.95;
painter->drawPixmap(deltaX + (iw * .025), deltaY + (ih * .025), painter->drawPixmap((int)(deltaX + (iw * .025)), (int)(deltaY + (ih * .025)),
d->icon.pixmap(d->iconSize.toSize())); d->icon.pixmap(d->iconSize.toSize()));
painter->restore(); painter->restore();
} }
else else
{ {
painter->drawPixmap(deltaX, deltaY, d->icon.pixmap(d->iconSize.toSize())); painter->drawPixmap((int)deltaX, (int)deltaY, d->icon.pixmap(d->iconSize.toSize()));
} }
} }
//TODO: draw text //TODO: draw text
// Make it default
if (d->svgElements & Private::SvgForeground) { if (d->svgElements & Private::SvgForeground) {
element = "foreground"; element = "foreground";
} }
switch (d->state) { switch (d->state) {
case Private::NoState: case Private::NoState:
element = "foreground";
break; break;
case Private::HoverState: case Private::HoverState:
if (d->svgElements & Private::SvgForegroundHover) { if (d->svgElements & Private::SvgForegroundHover) {
@ -218,16 +235,49 @@ void Icon::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWid
break; break;
} }
//paint foreground element
if (!element.isEmpty()) { if (!element.isEmpty()) {
//kDebug() << "painting " << element << endl; //kDebug() << "painting " << element << endl;
d->svg.paint(painter, 0, 0, element); d->svg.paint(painter, 0, 0, element);
} }
// Draw top-left button
QColor colorButton1;
if (d->button1Hovered) {
// colorButton1 = QColor(0, 49, 110); //Oxygen color
element = "button1-hover";
} else if (d->button1Pressed) {
// colorButton1 = QColor(29, 10, 85); //Oxygen color
element = "button1-pressed";
} else {
// colorButton1 = QColor(0, 67, 138); //Oxygen color
element = "button1";
}
KIcon exec("exec");
painter->setRenderHint(QPainter::Antialiasing);
painter->setOpacity(d->stepMenu*0.04);
// painter->setBrush(colorButton1);
painter->setPen(Qt::NoPen);
if (!element.isEmpty()) {
//kDebug() << "painting " << element << endl;
d->svg.paint(painter, QRect(6, 6, 32, 32), element);
}
painter->drawPixmap(11, 11, exec.pixmap(22,22));
}
void Icon::animateBubbles(int step)
{
d->stepMenu = step;
// kDebug() << "pop in: " << d->stepMenu << endl;
update();
} }
void Icon::setText(const QString& text) void Icon::setText(const QString& text)
{ {
d->text = text; d->text = text;
//TODO: implement this puppy calculateSize(); //TODO: implement this puppy calculateSize();
} }
QString Icon::text() const QString Icon::text() const
@ -237,6 +287,7 @@ QString Icon::text() const
void Icon::setIcon(const QString& icon) void Icon::setIcon(const QString& icon)
{ {
kDebug() << "Icon set: " << icon << endl;
if (icon.isEmpty()) { if (icon.isEmpty()) {
setIcon(QIcon()); setIcon(QIcon());
return; return;
@ -251,6 +302,11 @@ void Icon::setIcon(const QIcon& icon)
update(); update();
} }
void Icon::setUrl(const KUrl& url)
{
d->url = url;
}
QSizeF Icon::size() const QSizeF Icon::size() const
{ {
return d->size; return d->size;
@ -282,10 +338,18 @@ bool Icon::isDown()
void Icon::mousePressEvent(QGraphicsSceneMouseEvent *event) void Icon::mousePressEvent(QGraphicsSceneMouseEvent *event)
{ {
d->state = Private::PressedState; QRectF button1(6, 6, 32, 32); // The top-left circle
emit pressed(true); if (button1.contains(event->pos())) {
QGraphicsItem::mousePressEvent(event); if (d->url.isValid()) {
update(); KRun::runUrl(d->url, KMimeType::findByUrl(d->url)->name(), 0);
}
d->button1Pressed = true;
} else {
d->state = Private::PressedState;
QGraphicsItem::mousePressEvent(event);
update();
d->button1Pressed = false;
}
} }
void Icon::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) void Icon::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
@ -303,18 +367,54 @@ void Icon::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
emit pressed(false); emit pressed(false);
emit clicked(); emit clicked();
} }
d->button1Pressed = false;
QRectF button1(6, 6, 32, 32); // The top-left circle
if (button1.contains(event->pos())) {
d->button1Hovered = true;
} else {
d->button1Hovered = false;
}
QGraphicsItem::mouseReleaseEvent(event); QGraphicsItem::mouseReleaseEvent(event);
update(); update();
} }
void Icon::hoverEnterEvent(QGraphicsSceneHoverEvent *event) void Icon::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{ {
d->button1Pressed = false;
QRectF button1(6, 6, 32, 32); // The top-left circle
if (button1.contains(event->pos())) {
d->button1Hovered = true;
} else {
d->button1Hovered = false;
}
if (d->timeline->state() == QTimeLine::Running) {
d->timeline->setPaused(true);
}
d->timeline->setFrameRange(0, 25);
d->timeline->setDirection(QTimeLine::Forward);
connect(d->timeline, SIGNAL(frameChanged(int)), this, SLOT(animateBubbles(int)));
d->state = Private::HoverState; d->state = Private::HoverState;
QGraphicsItem::hoverEnterEvent(event); QGraphicsItem::hoverEnterEvent(event);
d->timeline->start();
} }
void Icon::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) void Icon::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{ {
if (d->timeline->state() == QTimeLine::Running) {
d->timeline->setPaused(true);
}
d->timeline->setFrameRange(0, 25);
d->timeline->setDirection(QTimeLine::Backward);
connect(d->timeline, SIGNAL(frameChanged(int)), this, SLOT(animateBubbles(int)));
d->state = Private::HoverState;
QGraphicsItem::hoverEnterEvent(event);
d->timeline->start();
d->state = Private::NoState; d->state = Private::NoState;
QGraphicsItem::hoverLeaveEvent(event); QGraphicsItem::hoverLeaveEvent(event);
} }

View File

@ -55,6 +55,8 @@ class PLASMA_EXPORT Icon : public QObject,
void setIcon(const QString& icon); void setIcon(const QString& icon);
void setIcon(const QIcon& icon); void setIcon(const QIcon& icon);
void setUrl(const KUrl& url);
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
QRectF boundingRect() const; QRectF boundingRect() const;
@ -80,6 +82,7 @@ class PLASMA_EXPORT Icon : public QObject,
Q_SIGNALS: Q_SIGNALS:
void pressed(bool down); void pressed(bool down);
void clicked(); void clicked();
void openUrl();
protected: protected:
bool isDown(); bool isDown();
@ -88,6 +91,9 @@ class PLASMA_EXPORT Icon : public QObject,
void hoverEnterEvent (QGraphicsSceneHoverEvent * event); void hoverEnterEvent (QGraphicsSceneHoverEvent * event);
void hoverLeaveEvent (QGraphicsSceneHoverEvent * event); void hoverLeaveEvent (QGraphicsSceneHoverEvent * event);
private Q_SLOTS:
void animateBubbles(int step);
private: private:
class Private; class Private;
Private * const d; Private * const d;