Icon now draws text, also lots of api love

svn path=/trunk/KDE/kdebase/workspace/libs/plasma/; revision=690149
This commit is contained in:
Matt Broadstone 2007-07-20 06:28:51 +00:00
parent 98e4e20fe2
commit 6ea5d0771b
2 changed files with 120 additions and 77 deletions

View File

@ -1,5 +1,6 @@
/* /*
* Copyright (C) 2007 by Aaron Seigo aseigo@kde.org * Copyright (C) 2007 by Aaron Seigo aseigo@kde.org
* Copyright (C) 2007 by Matt Broadstone <mbroadst@gmail.com>
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License version 2 as * it under the terms of the GNU Library General Public License version 2 as
@ -16,8 +17,7 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/ */
#include "icon.h" #include <QApplication>
#include <QPainter> #include <QPainter>
#include <QGraphicsSceneMouseEvent> #include <QGraphicsSceneMouseEvent>
#include <QGraphicsView> #include <QGraphicsView>
@ -38,6 +38,7 @@
#include "phase.h" #include "phase.h"
#include "svg.h" #include "svg.h"
#include "effects/blur.cpp" #include "effects/blur.cpp"
#include "icon.h"
namespace Plasma namespace Plasma
{ {
@ -122,7 +123,7 @@ class Icon::Private
SvgMinibuttonHover = 256, SvgMinibuttonHover = 256,
SvgMinibuttonPressed = 128}; SvgMinibuttonPressed = 128};
KUrl url; // KUrl url;
QString text; QString text;
QSizeF size; QSizeF size;
QSizeF iconSize; QSizeF iconSize;
@ -138,14 +139,27 @@ class Icon::Private
}; };
Icon::Icon(QGraphicsItem *parent) Icon::Icon(QGraphicsItem *parent)
: QObject(), : QGraphicsItem(parent),
QGraphicsItem(parent),
d(new Private) d(new Private)
{ {
setAcceptedMouseButtons(Qt::LeftButton); init();
setAcceptsHoverEvents(true); }
setEnabled(true);
setPos(QPointF(0.0,0.0)); Icon::Icon(const QString &text, QGraphicsItem *parent)
: QGraphicsItem(parent),
d(new Private)
{
setText(text);
init();
}
Icon::Icon(const QIcon &icon, const QString &text, QGraphicsItem *parent)
: QGraphicsItem(parent),
d(new Private)
{
setText(text);
setIcon(icon);
init();
} }
Icon::~Icon() Icon::~Icon()
@ -153,6 +167,26 @@ Icon::~Icon()
delete d; delete d;
} }
void Icon::init()
{
setAcceptedMouseButtons(Qt::LeftButton);
setAcceptsHoverEvents(true);
}
void Icon::calculateSize()
{
prepareGeometryChange();
QFontMetrics fm(QApplication::font()); // TODO: get the font somewhere more appropriate
QSizeF fmSize = fm.size(Qt::AlignHCenter | Qt::AlignTop, d->text);
int margin = 6; // hmmm
qreal height = d->iconSize.height() + (margin*2) + fmSize.height();
qreal width = margin + qMax(fmSize.width(), d->iconSize.width()) + margin;
d->size = QSizeF(width, height);
d->svg.resize(d->size);
update();
}
QRectF Icon::boundingRect() const QRectF Icon::boundingRect() const
{ {
return QRectF(QPointF(0, 0), d->size); return QRectF(QPointF(0, 0), d->size);
@ -225,8 +259,6 @@ void Icon::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWid
} }
} }
//TODO: draw text
// Make it default // Make it default
if (d->svgElements & Private::SvgForeground) { if (d->svgElements & Private::SvgForeground) {
element = "foreground"; element = "foreground";
@ -259,12 +291,26 @@ void Icon::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWid
if (d->button1AnimId) { if (d->button1AnimId) {
painter->drawPixmap(6, 6, Phase::self()->animationResult(d->button1AnimId)); painter->drawPixmap(6, 6, Phase::self()->animationResult(d->button1AnimId));
} }
// Draw text last because its overlayed
if (!d->text.isEmpty()) {
qreal offset = (d->iconSize.height() + 12); // TODO this shouldn't be hardcoded?
QRectF textRect(0, offset, d->size.width(), d->size.height() - offset);
QTextOption textOpt;
textOpt.setAlignment(Qt::AlignHCenter | Qt::AlignTop);
textOpt.setWrapMode(QTextOption::WordWrap);
painter->setPen(Qt::white);
painter->drawText(textRect, d->text, textOpt);
}
} }
void Icon::setText(const QString& text) void Icon::setText(const QString& text)
{ {
d->text = text; d->text = text;
//TODO: implement this puppy calculateSize(); calculateSize();
} }
QString Icon::text() const QString Icon::text() const
@ -274,7 +320,6 @@ 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;
@ -286,22 +331,7 @@ void Icon::setIcon(const QString& icon)
void Icon::setIcon(const QIcon& icon) void Icon::setIcon(const QIcon& icon)
{ {
d->icon = icon; d->icon = icon;
update(); calculateSize();
}
void Icon::setUrl(const KUrl& url)
{
d->url = url;
}
KUrl Icon::url() const
{
return d->url;
}
QSizeF Icon::size() const
{
return d->size;
} }
QSizeF Icon::iconSize() const QSizeF Icon::iconSize() const
@ -309,18 +339,15 @@ QSizeF Icon::iconSize() const
return d->iconSize; return d->iconSize;
} }
void Icon::setSize(const QSizeF& s) void Icon::setIconSize(const QSizeF& s)
{ {
prepareGeometryChange();
d->iconSize = s; d->iconSize = s;
d->size = s * 1.1; calculateSize();
d->svg.resize(d->size);
update();
} }
void Icon::setSize(int w, int h) void Icon::setIconSize(int w, int h)
{ {
setSize(QSizeF(w, h)); setIconSize(QSizeF(w, h));
} }
bool Icon::isDown() bool Icon::isDown()
@ -351,11 +378,13 @@ void Icon::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
QRectF button1(6, 6, 32, 32); // The top-left circle QRectF button1(6, 6, 32, 32); // The top-left circle
d->button1Hovered = button1.contains(event->pos()); d->button1Hovered = button1.contains(event->pos());
/*
if (d->button1Hovered && if (d->button1Hovered &&
d->button1Pressed && d->url.isValid()) { d->button1Pressed && d->url.isValid()) {
KRun::runUrl(d->url, KMimeType::findByUrl(d->url)->name(), 0); KRun::runUrl(d->url, KMimeType::findByUrl(d->url)->name(), 0);
wasClicked = false; wasClicked = false;
} }
*/
} else { } else {
d->state = Private::NoState; d->state = Private::NoState;
} }
@ -498,20 +527,16 @@ qreal Icon::widthForHeight(qreal h) const
QRectF Icon::geometry() const QRectF Icon::geometry() const
{ {
return boundingRect().toRect(); return boundingRect();
} }
void Icon::setGeometry(const QRectF &r) void Icon::setGeometry(const QRectF &r)
{ {
setSize(r.size()); // TODO: this is wrong, but we should probably never call setGeometry anyway!
setIconSize(r.size());
setPos(r.x(),r.y()); setPos(r.x(),r.y());
} }
int Icon::boundsForIconSize(int iconSize)
{
return iconSize * 1.1;
}
} // namespace Plasma } // namespace Plasma
#include "icon.moc" #include "icon.moc"

View File

@ -1,5 +1,6 @@
/* /*
* Copyright (C) 2007 by Siraj Razick siraj@kde.org * Copyright (C) 2007 by Siraj Razick siraj@kde.org
* Copyright (C) 2007 by Matt Broadstone <mbroadst@gmail.com>
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License version 2 as * it under the terms of the GNU Library General Public License version 2 as
@ -42,26 +43,32 @@ class PLASMA_EXPORT Icon : public QObject, public QGraphicsItem, public LayoutIt
Q_OBJECT Q_OBJECT
public: public:
Icon(QGraphicsItem *parent = 0); Icon(QGraphicsItem *parent = 0);
Icon(const QString &text, QGraphicsItem *parent = 0);
Icon(const QIcon & icon, const QString &text, QGraphicsItem *parent = 0);
virtual ~Icon(); virtual ~Icon();
QString text() const; QString text() const;
void setText(const QString &name); void setText(const QString &name);
QSizeF size() const;
QSizeF iconSize() const;
void setSize(const QSizeF& size);
void setSize(int height, int width);
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); QSizeF iconSize() const;
KUrl url() const; void setIconSize(const QSizeF& size);
void setIconSize(int height, int width);
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); /*
QRectF boundingRect() const; enum Position
{
TopLeft = 0,
TopRight,
BottomLeft,
BottomRight
};
void setCornerAction(Position pos, QAction *action);
*/
//layout stufff // Layout stuff
Qt::Orientations expandingDirections() const; Qt::Orientations expandingDirections() const;
QSizeF minimumSize() const; QSizeF minimumSize() const;
@ -78,23 +85,27 @@ class PLASMA_EXPORT Icon : public QObject, public QGraphicsItem, public LayoutIt
QSizeF sizeHint() const; QSizeF sizeHint() const;
static int boundsForIconSize(int iconSize); protected:
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
QRectF boundingRect() const;
Q_SIGNALS: Q_SIGNALS:
void pressed(bool down); void pressed(bool down);
void clicked(); void clicked();
void openUrl();
protected: protected:
bool isDown(); bool isDown();
void mousePressEvent(QGraphicsSceneMouseEvent *event); void mousePressEvent(QGraphicsSceneMouseEvent *event);
void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
void hoverEnterEvent(QGraphicsSceneHoverEvent *event); void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
void hoverLeaveEvent(QGraphicsSceneHoverEvent *event); void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
private: private:
QPixmap buttonPixmap(); QPixmap buttonPixmap();
void init();
void calculateSize();
class Private; class Private;
Private * const d; Private * const d;
@ -102,4 +113,11 @@ class PLASMA_EXPORT Icon : public QObject, public QGraphicsItem, public LayoutIt
} // namespace Plasma } // namespace Plasma
/*
// Add these to UrlIcon
void setUrl(const KUrl& url);
KUrl url() const;
*/
#endif #endif