2007-05-29 09:59:50 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2007 by Aaron Seigo aseigo@kde.org
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* published by the Free Software Foundation
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this program; if not, write to the
|
|
|
|
* Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
2007-05-30 18:47:36 +02:00
|
|
|
#include "icon.h"
|
|
|
|
|
2007-05-29 09:59:50 +02:00
|
|
|
#include <QPainter>
|
|
|
|
#include <QGraphicsSceneMouseEvent>
|
|
|
|
|
|
|
|
#include <KIcon>
|
|
|
|
#include <KDebug>
|
|
|
|
|
|
|
|
#include "svg.h"
|
|
|
|
|
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
|
|
|
class Icon::Private
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Private()
|
2007-05-29 22:37:21 +02:00
|
|
|
: size(128*1.1, 128*1.1),
|
|
|
|
state(Private::NoState),
|
|
|
|
svg("widgets/iconbutton")
|
2007-05-29 09:59:50 +02:00
|
|
|
{
|
2007-05-29 22:37:21 +02:00
|
|
|
svg.setContentType(Plasma::Svg::ImageSet);
|
|
|
|
svg.resize(size);
|
2007-05-29 09:59:50 +02:00
|
|
|
minSize = size;
|
|
|
|
maxSize = size;
|
2007-05-29 22:37:21 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2007-05-29 09:59:50 +02:00
|
|
|
}
|
|
|
|
~Private() {}
|
2007-05-29 22:37:21 +02:00
|
|
|
enum ButtonState
|
|
|
|
{
|
|
|
|
NoState,
|
|
|
|
HoverState,
|
|
|
|
PressedState
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
enum { NoSvg = 0,
|
|
|
|
SvgBackground = 1,
|
|
|
|
SvgBackgroundHover = 2,
|
|
|
|
SvgBackgroundPressed = 4,
|
|
|
|
SvgForeground = 8,
|
|
|
|
SvgForegroundHover = 16,
|
|
|
|
SvgForegroundPressed = 32 };
|
2007-05-29 09:59:50 +02:00
|
|
|
|
|
|
|
QString text;
|
|
|
|
QIcon icon;
|
|
|
|
QSizeF size;
|
|
|
|
QSizeF minSize;
|
|
|
|
QSizeF maxSize;
|
2007-05-29 22:37:21 +02:00
|
|
|
ButtonState state;
|
|
|
|
Svg svg;
|
|
|
|
int svgElements;
|
2007-05-29 09:59:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
Icon::Icon(QGraphicsItem *parent)
|
|
|
|
: QObject(),
|
|
|
|
QGraphicsItem(parent),
|
|
|
|
d(new Private)
|
|
|
|
{
|
|
|
|
setAcceptedMouseButtons(Qt::LeftButton);
|
|
|
|
setAcceptsHoverEvents(true);
|
|
|
|
setEnabled(true);
|
|
|
|
setPos(QPointF(0.0,0.0));
|
|
|
|
}
|
|
|
|
|
|
|
|
Icon::~Icon()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
|
|
|
QRectF Icon::boundingRect() const
|
|
|
|
{
|
2007-05-29 22:37:21 +02:00
|
|
|
return QRectF(QPointF(0, 0), d->size);
|
2007-05-29 09:59:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Icon::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
|
|
|
|
{
|
|
|
|
Q_UNUSED(option)
|
|
|
|
Q_UNUSED(widget)
|
|
|
|
|
2007-05-29 22:37:21 +02:00
|
|
|
// QRectF rect = boundingRect();
|
|
|
|
|
|
|
|
QString element;
|
|
|
|
if (d->svgElements & Private::SvgBackground) {
|
|
|
|
element = "background";
|
|
|
|
}
|
2007-05-29 09:59:50 +02:00
|
|
|
|
|
|
|
switch (d->state) {
|
2007-05-29 22:37:21 +02:00
|
|
|
case Private::NoState:
|
2007-05-29 09:59:50 +02:00
|
|
|
break;
|
2007-05-29 22:37:21 +02:00
|
|
|
case Private::HoverState:
|
|
|
|
if (d->svgElements & Private::SvgBackgroundHover) {
|
|
|
|
element = "background-hover";
|
|
|
|
}
|
2007-05-29 09:59:50 +02:00
|
|
|
break;
|
2007-05-29 22:37:21 +02:00
|
|
|
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();
|
2007-05-29 09:59:50 +02:00
|
|
|
}
|
|
|
|
|
2007-05-29 22:37:21 +02:00
|
|
|
|
2007-05-29 09:59:50 +02:00
|
|
|
if (!d->icon.isNull()) {
|
2007-06-05 04:54:48 +02:00
|
|
|
qreal deltaX = d->size.width() * 0.1;
|
|
|
|
qreal deltaY = d->size.height() * 0.1;
|
2007-05-29 09:59:50 +02:00
|
|
|
painter->drawPixmap(deltaX, deltaY, d->icon.pixmap((d->size * 0.9).toSize()));
|
|
|
|
}
|
|
|
|
|
|
|
|
//TODO: draw text
|
|
|
|
|
2007-05-29 22:37:21 +02:00
|
|
|
if (d->svgElements & Private::SvgForeground) {
|
|
|
|
element = "foreground";
|
|
|
|
}
|
|
|
|
|
2007-05-29 09:59:50 +02:00
|
|
|
switch (d->state) {
|
2007-05-29 22:37:21 +02:00
|
|
|
case Private::NoState:
|
2007-05-29 09:59:50 +02:00
|
|
|
break;
|
2007-05-29 22:37:21 +02:00
|
|
|
case Private::HoverState:
|
|
|
|
if (d->svgElements & Private::SvgForegroundHover) {
|
|
|
|
element = "foreground-hover";
|
|
|
|
}
|
2007-05-29 09:59:50 +02:00
|
|
|
break;
|
2007-05-29 22:37:21 +02:00
|
|
|
case Private::PressedState:
|
|
|
|
if (d->svgElements & Private::SvgForegroundPressed) {
|
|
|
|
element = "foreground-pressed";
|
|
|
|
}
|
2007-05-29 09:59:50 +02:00
|
|
|
break;
|
|
|
|
}
|
2007-05-29 22:37:21 +02:00
|
|
|
|
|
|
|
if (!element.isEmpty()) {
|
|
|
|
//kDebug() << "painting " << element << endl;
|
|
|
|
d->svg.paint(painter, 0, 0, element);
|
|
|
|
}
|
2007-05-29 09:59:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Icon::setText(const QString& text)
|
|
|
|
{
|
|
|
|
d->text = text;
|
|
|
|
//TODO: implement this puppy calculateSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Icon::text() const
|
|
|
|
{
|
|
|
|
return d->text;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Icon::setIcon(const QString& icon)
|
|
|
|
{
|
|
|
|
if (icon.isEmpty()) {
|
|
|
|
setIcon(QIcon());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setIcon(KIcon(icon));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Icon::setIcon(const QIcon& icon)
|
|
|
|
{
|
|
|
|
d->icon = icon;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
QSizeF Icon::size() const
|
|
|
|
{
|
|
|
|
return d->size;
|
|
|
|
}
|
|
|
|
|
2007-06-05 04:54:48 +02:00
|
|
|
QSizeF Icon::iconSize() const
|
|
|
|
{
|
|
|
|
return d->size * .9;
|
|
|
|
}
|
|
|
|
|
2007-05-29 09:59:50 +02:00
|
|
|
void Icon::setSize(const QSizeF& s)
|
|
|
|
{
|
|
|
|
prepareGeometryChange();
|
|
|
|
d->size = s.boundedTo(d->maxSize); //FIXME: maxSize always == size means it can be changed. wtf. =)
|
2007-05-29 22:37:21 +02:00
|
|
|
d->svg.resize(d->size);
|
2007-05-29 09:59:50 +02:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Icon::setSize(int w, int h)
|
|
|
|
{
|
|
|
|
setSize(QSizeF(w, h));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Icon::isDown()
|
|
|
|
{
|
2007-05-29 22:37:21 +02:00
|
|
|
return d->state == Private::PressedState;
|
2007-05-29 09:59:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Icon::mousePressEvent(QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
2007-05-29 22:37:21 +02:00
|
|
|
d->state = Private::PressedState;
|
2007-05-29 09:59:50 +02:00
|
|
|
emit pressed(true);
|
2007-05-29 22:37:21 +02:00
|
|
|
QGraphicsItem::mousePressEvent(event);
|
|
|
|
update();
|
2007-05-29 09:59:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Icon::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
2007-05-29 22:37:21 +02:00
|
|
|
bool inside = boundingRect().contains(event->pos());
|
|
|
|
bool wasClicked = d->state == Private::PressedState && inside;
|
|
|
|
|
|
|
|
if (inside) {
|
|
|
|
d->state = Private::HoverState;
|
|
|
|
} else {
|
|
|
|
d->state = Private::NoState;
|
|
|
|
}
|
2007-05-29 09:59:50 +02:00
|
|
|
|
|
|
|
if (wasClicked) {
|
|
|
|
emit pressed(false);
|
|
|
|
emit clicked();
|
|
|
|
}
|
2007-05-29 22:37:21 +02:00
|
|
|
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);
|
2007-05-29 09:59:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QSizeF Icon::sizeHint() const
|
|
|
|
{
|
|
|
|
return d->size;
|
|
|
|
}
|
|
|
|
|
|
|
|
QSizeF Icon::minimumSize() const
|
|
|
|
{
|
|
|
|
return d->minSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
QSizeF Icon::maximumSize() const
|
|
|
|
{
|
|
|
|
return d->maxSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
Qt::Orientations Icon::expandingDirections() const
|
|
|
|
{
|
|
|
|
return Qt::Horizontal;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Icon::hasHeightForWidth() const
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
qreal Icon::heightForWidth(qreal w) const
|
|
|
|
{
|
|
|
|
return w; //FIXME: we shouldn't assume squareness but actually calculate based on text and what not
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Icon::hasWidthForHeight() const
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
qreal Icon::widthForHeight(qreal h) const
|
|
|
|
{
|
|
|
|
return h; //FIXME: we shouldn't assume squareness but actually calculate based on text and what not
|
|
|
|
}
|
|
|
|
|
|
|
|
QRectF Icon::geometry() const
|
|
|
|
{
|
|
|
|
return boundingRect().toRect();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Icon::setGeometry(const QRectF &r)
|
|
|
|
{
|
|
|
|
setSize(r.size());
|
|
|
|
setPos(r.x(),r.y());
|
|
|
|
}
|
|
|
|
|
2007-06-05 04:54:48 +02:00
|
|
|
int Icon::boundsForIconSize(int iconSize)
|
|
|
|
{
|
|
|
|
return iconSize * 1.1;
|
|
|
|
}
|
|
|
|
|
2007-05-29 09:59:50 +02:00
|
|
|
} // namespace Plasma
|
|
|
|
|
|
|
|
#include "icon.moc"
|