2007-05-29 09:59:50 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2007 by Aaron Seigo aseigo@kde.org
|
2007-07-20 08:28:51 +02:00
|
|
|
* Copyright (C) 2007 by Matt Broadstone <mbroadst@gmail.com>
|
2007-05-29 09:59:50 +02:00
|
|
|
*
|
|
|
|
* 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-07-23 00:01:10 +02:00
|
|
|
#include "icon.h"
|
|
|
|
|
|
|
|
#include <QAction>
|
2007-07-20 08:28:51 +02:00
|
|
|
#include <QApplication>
|
2007-05-29 09:59:50 +02:00
|
|
|
#include <QPainter>
|
|
|
|
#include <QGraphicsSceneMouseEvent>
|
2007-06-07 21:07:45 +02:00
|
|
|
#include <QGraphicsView>
|
2007-06-08 07:39:34 +02:00
|
|
|
|
2007-07-21 06:07:41 +02:00
|
|
|
//#define BACKINGSTORE_BLUR_HACK
|
2007-06-08 07:39:34 +02:00
|
|
|
|
2007-07-21 06:07:41 +02:00
|
|
|
#ifdef BACKINGSTORE_BLUR_HACK
|
2007-06-07 21:07:45 +02:00
|
|
|
#include <private/qwindowsurface_p.h>
|
2007-06-08 07:39:34 +02:00
|
|
|
#endif
|
2007-05-29 09:59:50 +02:00
|
|
|
|
|
|
|
#include <KIcon>
|
2007-06-07 21:07:45 +02:00
|
|
|
#include <KImageEffect>
|
2007-06-17 00:35:31 +02:00
|
|
|
#include <KUrl>
|
|
|
|
#include <KRun>
|
|
|
|
#include <KMimeType>
|
2007-05-29 09:59:50 +02:00
|
|
|
#include <KDebug>
|
|
|
|
|
2007-06-19 23:56:40 +02:00
|
|
|
#include "phase.h"
|
2007-05-29 09:59:50 +02:00
|
|
|
#include "svg.h"
|
2007-06-07 21:10:48 +02:00
|
|
|
#include "effects/blur.cpp"
|
2007-05-29 09:59:50 +02:00
|
|
|
|
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
2007-07-21 06:07:41 +02:00
|
|
|
class PLASMA_EXPORT IconAction
|
2007-05-29 09:59:50 +02:00
|
|
|
{
|
|
|
|
public:
|
2007-07-21 06:07:41 +02:00
|
|
|
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()
|
2007-07-21 00:30:33 +02:00
|
|
|
: svg("widgets/iconbutton"),
|
2007-07-21 06:07:41 +02:00
|
|
|
svgElements(0),
|
|
|
|
size(128*1.1, 128*1.1),
|
|
|
|
iconSize(128, 128),
|
|
|
|
state(Private::NoState)
|
2007-05-29 09:59:50 +02:00
|
|
|
{
|
2007-05-29 22:37:21 +02:00
|
|
|
svg.setContentType(Plasma::Svg::ImageSet);
|
2007-07-21 06:07:41 +02:00
|
|
|
svg.resize(size);
|
2007-05-29 22:37:21 +02:00
|
|
|
|
2007-06-19 23:56:40 +02:00
|
|
|
//TODO: recheck when svg changes
|
|
|
|
checkSvgElements();
|
|
|
|
}
|
|
|
|
|
2007-07-21 06:07:41 +02:00
|
|
|
~Private()
|
|
|
|
{
|
|
|
|
qDeleteAll(cornerActions);
|
|
|
|
}
|
|
|
|
|
2007-06-19 23:56:40 +02:00
|
|
|
void checkSvgElements()
|
|
|
|
{
|
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-06-19 23:56:40 +02:00
|
|
|
|
|
|
|
if (svg.elementExists("minibutton")) {
|
|
|
|
svgElements |= SvgMinibutton;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (svg.elementExists("minibutton-hover")) {
|
|
|
|
svgElements |= SvgMinibuttonHover;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (svg.elementExists("minibutton-pressed")) {
|
|
|
|
svgElements |= SvgMinibuttonPressed;
|
|
|
|
}
|
2007-07-21 00:30:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
enum {
|
|
|
|
NoSvg = 0,
|
|
|
|
SvgBackground = 1,
|
|
|
|
SvgBackgroundHover = 2,
|
|
|
|
SvgBackgroundPressed = 4,
|
|
|
|
SvgForeground = 8,
|
|
|
|
SvgForegroundHover = 16,
|
|
|
|
SvgForegroundPressed = 32,
|
|
|
|
SvgMinibutton = 64,
|
2007-07-21 06:07:41 +02:00
|
|
|
SvgMinibuttonHover = 128,
|
|
|
|
SvgMinibuttonPressed = 256
|
2007-07-21 00:30:33 +02:00
|
|
|
};
|
|
|
|
|
2007-07-21 06:07:41 +02:00
|
|
|
enum ActionPosition {
|
|
|
|
TopLeft = 0,
|
|
|
|
TopRight,
|
|
|
|
BottomLeft,
|
|
|
|
BottomRight,
|
|
|
|
LastIconPosition
|
|
|
|
};
|
2007-06-20 01:32:04 +02:00
|
|
|
|
2007-05-29 22:37:21 +02:00
|
|
|
enum ButtonState
|
|
|
|
{
|
|
|
|
NoState,
|
|
|
|
HoverState,
|
|
|
|
PressedState
|
|
|
|
};
|
|
|
|
|
2007-05-29 09:59:50 +02:00
|
|
|
QString text;
|
2007-07-28 21:27:56 +02:00
|
|
|
QString infoText;
|
2007-07-21 06:07:41 +02:00
|
|
|
Svg svg;
|
|
|
|
int svgElements;
|
2007-05-29 09:59:50 +02:00
|
|
|
QSizeF size;
|
2007-06-05 21:48:14 +02:00
|
|
|
QSizeF iconSize;
|
|
|
|
QIcon icon;
|
2007-05-29 22:37:21 +02:00
|
|
|
ButtonState state;
|
2007-07-21 00:30:33 +02:00
|
|
|
|
2007-07-21 06:07:41 +02:00
|
|
|
QList<IconAction*> cornerActions;
|
2007-05-29 09:59:50 +02:00
|
|
|
};
|
|
|
|
|
2007-07-21 06:07:41 +02:00
|
|
|
IconAction::IconAction(Icon* icon, QAction *action)
|
|
|
|
: m_icon(icon),
|
|
|
|
m_action(action),
|
|
|
|
m_hovered(false),
|
|
|
|
m_pressed(false),
|
|
|
|
m_visible(false),
|
|
|
|
m_animationId(-1)
|
2007-07-21 00:30:33 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void IconAction::show()
|
|
|
|
{
|
2007-07-21 06:07:41 +02:00
|
|
|
if (m_animationId) {
|
|
|
|
Phase::self()->stopElementAnimation(m_animationId);
|
|
|
|
}
|
2007-07-21 00:30:33 +02:00
|
|
|
|
2007-07-21 06:07:41 +02:00
|
|
|
rebuildPixmap();
|
2007-07-21 00:30:33 +02:00
|
|
|
|
2007-07-21 06:07:41 +02:00
|
|
|
m_animationId = Phase::self()->animateElement(m_icon, Phase::ElementAppear);
|
|
|
|
Phase::self()->setAnimationPixmap(m_animationId, m_pixmap);
|
|
|
|
m_visible = true;
|
2007-07-21 00:30:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void IconAction::hide()
|
|
|
|
{
|
2007-07-21 06:07:41 +02:00
|
|
|
if (m_animationId) {
|
|
|
|
Phase::self()->stopElementAnimation(m_animationId);
|
|
|
|
}
|
2007-07-21 00:30:33 +02:00
|
|
|
|
2007-07-21 06:07:41 +02:00
|
|
|
rebuildPixmap();
|
2007-07-21 00:30:33 +02:00
|
|
|
|
2007-07-21 06:07:41 +02:00
|
|
|
m_animationId = Phase::self()->animateElement(m_icon, Phase::ElementDisappear);
|
|
|
|
Phase::self()->setAnimationPixmap(m_animationId, m_pixmap);
|
|
|
|
m_visible = false;
|
2007-07-21 00:30:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IconAction::isVisible() const
|
|
|
|
{
|
2007-07-21 06:07:41 +02:00
|
|
|
return m_visible;
|
2007-07-21 00:30:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IconAction::isPressed() const
|
|
|
|
{
|
2007-07-21 06:07:41 +02:00
|
|
|
return m_pressed;
|
2007-07-21 00:30:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IconAction::isHovered() const
|
|
|
|
{
|
2007-07-21 06:07:41 +02:00
|
|
|
return m_hovered;
|
2007-07-21 00:30:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void IconAction::setSelected(bool selected)
|
|
|
|
{
|
2007-07-21 06:07:41 +02:00
|
|
|
m_selected = selected;
|
2007-07-21 00:30:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IconAction::isSelected() const
|
|
|
|
{
|
2007-07-21 06:07:41 +02:00
|
|
|
return m_selected;
|
2007-07-21 00:30:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void IconAction::setRect(const QRectF &rect)
|
|
|
|
{
|
2007-07-21 06:07:41 +02:00
|
|
|
m_rect = rect;
|
2007-07-21 00:30:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QRectF IconAction::rect() const
|
|
|
|
{
|
2007-07-21 06:07:41 +02:00
|
|
|
return m_rect;
|
2007-07-21 00:30:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void IconAction::rebuildPixmap()
|
|
|
|
{
|
|
|
|
// Determine proper QIcon mode based on selection status
|
|
|
|
QIcon::Mode mode = QIcon::Normal;
|
2007-07-21 06:07:41 +02:00
|
|
|
if (m_selected) {
|
2007-07-21 00:30:33 +02:00
|
|
|
mode = QIcon::Selected;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw everything
|
2007-07-21 06:07:41 +02:00
|
|
|
m_pixmap = QPixmap(26, 26);
|
|
|
|
m_pixmap.fill(Qt::transparent);
|
|
|
|
|
|
|
|
int element = Icon::Private::SvgMinibutton;
|
|
|
|
if (m_pressed) {
|
|
|
|
element = Icon::Private::SvgMinibuttonPressed;
|
|
|
|
} else if (m_hovered) {
|
|
|
|
element = Icon::Private::SvgMinibuttonHover;
|
2007-07-21 00:30:33 +02:00
|
|
|
}
|
|
|
|
|
2007-07-21 06:07:41 +02:00
|
|
|
QPainter painter(&m_pixmap);
|
|
|
|
m_icon->drawActionButtonBase(&painter, m_pixmap.size(), element);
|
|
|
|
m_action->icon().paint(&painter, 2, 2, 22, 22, Qt::AlignCenter, mode);
|
2007-07-21 00:30:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IconAction::event(QEvent::Type type, const QPointF &pos)
|
|
|
|
{
|
2007-07-21 06:07:41 +02:00
|
|
|
switch (type) {
|
|
|
|
case QEvent::MouseButtonPress: {
|
|
|
|
setSelected(m_rect.contains(pos));
|
|
|
|
return isSelected();
|
|
|
|
}
|
|
|
|
break;
|
2007-07-21 00:30:33 +02:00
|
|
|
|
2007-07-21 06:07:41 +02:00
|
|
|
case QEvent::MouseMove: {
|
|
|
|
bool wasSelected = isSelected();
|
|
|
|
bool active = m_rect.contains(pos);
|
|
|
|
setSelected(wasSelected && active);
|
|
|
|
return (wasSelected != isSelected()) || active;
|
|
|
|
}
|
|
|
|
break;
|
2007-07-21 00:30:33 +02:00
|
|
|
|
2007-07-21 06:07:41 +02:00
|
|
|
case QEvent::MouseButtonRelease: {
|
|
|
|
bool wasSelected = isSelected();
|
|
|
|
setSelected(false);
|
|
|
|
if (wasSelected) {
|
|
|
|
m_action->trigger();
|
|
|
|
}
|
2007-07-21 00:30:33 +02:00
|
|
|
|
2007-07-21 06:07:41 +02:00
|
|
|
return wasSelected;
|
|
|
|
}
|
|
|
|
break;
|
2007-07-21 00:30:33 +02:00
|
|
|
|
2007-07-21 06:07:41 +02:00
|
|
|
case QEvent::GraphicsSceneHoverEnter:
|
|
|
|
m_pressed = false;
|
|
|
|
m_hovered = true;
|
|
|
|
break;
|
2007-07-21 00:30:33 +02:00
|
|
|
|
2007-07-21 06:07:41 +02:00
|
|
|
case QEvent::GraphicsSceneHoverLeave:
|
|
|
|
m_pressed = false;
|
|
|
|
m_hovered = false;
|
|
|
|
break;
|
2007-07-21 00:30:33 +02:00
|
|
|
|
2007-07-21 06:07:41 +02:00
|
|
|
default:
|
2007-07-21 00:30:33 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Phase::AnimId IconAction::animationId() const
|
|
|
|
{
|
2007-07-21 06:07:41 +02:00
|
|
|
return m_animationId;
|
2007-07-21 00:30:33 +02:00
|
|
|
}
|
|
|
|
|
2007-07-21 06:07:41 +02:00
|
|
|
QAction* IconAction::action() const
|
2007-07-21 00:30:33 +02:00
|
|
|
{
|
2007-07-21 06:07:41 +02:00
|
|
|
return m_action;
|
2007-07-21 00:30:33 +02:00
|
|
|
}
|
|
|
|
|
2007-07-21 06:07:41 +02:00
|
|
|
void IconAction::paint(QPainter *painter) const
|
|
|
|
{
|
|
|
|
painter->drawPixmap(m_rect.toRect(), Phase::self()->animationResult(m_animationId));
|
|
|
|
}
|
2007-07-21 00:30:33 +02:00
|
|
|
|
2007-05-29 09:59:50 +02:00
|
|
|
Icon::Icon(QGraphicsItem *parent)
|
2007-07-20 08:28:51 +02:00
|
|
|
: QGraphicsItem(parent),
|
2007-05-29 09:59:50 +02:00
|
|
|
d(new Private)
|
|
|
|
{
|
2007-07-20 08:28:51 +02:00
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2007-05-29 09:59:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Icon::~Icon()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
2007-07-20 08:28:51 +02:00
|
|
|
void Icon::init()
|
|
|
|
{
|
|
|
|
setAcceptedMouseButtons(Qt::LeftButton);
|
|
|
|
setAcceptsHoverEvents(true);
|
2007-07-21 00:30:33 +02:00
|
|
|
}
|
|
|
|
|
2007-07-21 06:07:41 +02:00
|
|
|
void Icon::addAction(QAction *action)
|
2007-07-21 00:30:33 +02:00
|
|
|
{
|
2007-07-21 06:07:41 +02:00
|
|
|
int count = d->cornerActions.count();
|
|
|
|
if (count > 3) {
|
|
|
|
kDebug() << "Icon::addAction(QAction*) no more room for more actions!" << endl;
|
2007-07-21 00:30:33 +02:00
|
|
|
}
|
|
|
|
|
2007-07-21 06:07:41 +02:00
|
|
|
IconAction* iconAction = new IconAction(this, action);
|
|
|
|
d->cornerActions.append(iconAction);
|
|
|
|
connect(action, SIGNAL(destroyed(QObject*)), this, SLOT(actionDestroyed(QObject*)));
|
|
|
|
|
2007-07-21 06:19:00 +02:00
|
|
|
//TODO: fewer magic numbers, please =) 38, 32, 6, etc... needs to go.
|
|
|
|
// at the very least static const ints with nice names.
|
2007-07-21 06:07:41 +02:00
|
|
|
switch (count) {
|
|
|
|
case Private::TopLeft:
|
|
|
|
iconAction->setRect(QRectF(6, 6, 32, 32));
|
|
|
|
break;
|
|
|
|
case Private::TopRight:
|
2007-07-21 06:19:00 +02:00
|
|
|
iconAction->setRect(QRectF(d->size.width() - 38, 6, 32, 32));
|
2007-07-21 06:07:41 +02:00
|
|
|
break;
|
|
|
|
case Private::BottomLeft:
|
2007-07-21 06:19:00 +02:00
|
|
|
iconAction->setRect(QRectF(6, d->size.height() - 38, 32, 32));
|
2007-07-21 06:07:41 +02:00
|
|
|
break;
|
|
|
|
case Private::BottomRight:
|
2007-07-21 06:19:00 +02:00
|
|
|
iconAction->setRect(QRectF(d->size.width() - 38, d->size.height() - 38, 32, 32));
|
2007-07-21 06:07:41 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-07-21 00:30:33 +02:00
|
|
|
|
2007-07-21 06:07:41 +02:00
|
|
|
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);
|
2007-07-21 00:30:33 +02:00
|
|
|
break;
|
2007-07-21 06:07:41 +02:00
|
|
|
}
|
2007-07-21 00:30:33 +02:00
|
|
|
}
|
2007-07-28 21:27:56 +02:00
|
|
|
|
|
|
|
update(); // redraw since an action has been deleted.
|
2007-07-20 08:28:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2007-07-21 06:19:00 +02:00
|
|
|
|
|
|
|
int count = 0;
|
|
|
|
foreach (IconAction* iconAction, d->cornerActions) {
|
|
|
|
//TODO: fewer magic numbers, please =) 38, 32, 6, etc... needs to go.
|
|
|
|
// at the very least static const ints with nice names.
|
|
|
|
switch (count) {
|
|
|
|
case Private::TopLeft:
|
|
|
|
// top left never changes, so don't bother setting it
|
|
|
|
//iconAction->setRect(QRectF(6, 6, 32, 32));
|
|
|
|
break;
|
|
|
|
case Private::TopRight:
|
|
|
|
iconAction->setRect(QRectF(d->size.width() - 38, 6, 32, 32));
|
|
|
|
break;
|
|
|
|
case Private::BottomLeft:
|
|
|
|
iconAction->setRect(QRectF(6, d->size.height() - 38, 32, 32));
|
|
|
|
break;
|
|
|
|
case Private::BottomRight:
|
|
|
|
iconAction->setRect(QRectF(d->size.width() - 38, d->size.height() - 38, 32, 32));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
|
2007-07-20 08:28:51 +02:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2007-05-29 09:59:50 +02:00
|
|
|
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-07-21 06:07:41 +02:00
|
|
|
#ifdef BACKINGSTORE_BLUR_HACK
|
2007-06-17 19:49:49 +02:00
|
|
|
if (d->state == Private::HoverState && scene()) {
|
|
|
|
QList<QGraphicsView*> views = scene()->views();
|
|
|
|
if (views.count() > 0) {
|
|
|
|
QPixmap* pix = static_cast<QPixmap*>(views[0]->windowSurface()->paintDevice());
|
|
|
|
QImage image(boundingRect().size().toSize(), QImage::Format_ARGB32_Premultiplied);
|
|
|
|
{
|
|
|
|
QPainter p(&image);
|
|
|
|
p.drawPixmap(image.rect(), *pix, sceneBoundingRect());
|
|
|
|
}
|
|
|
|
expblur<16,7>(image, 8);
|
|
|
|
painter->drawImage(0, 0, image);
|
|
|
|
}
|
|
|
|
}
|
2007-06-08 07:39:34 +02:00
|
|
|
#endif
|
2007-06-07 21:07:45 +02:00
|
|
|
|
2007-07-21 06:07:41 +02:00
|
|
|
d->svg.resize(d->size);
|
|
|
|
|
2007-05-29 22:37:21 +02:00
|
|
|
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";
|
2007-06-06 22:48:06 +02:00
|
|
|
} else if (d->svgElements & Private::SvgBackgroundHover) {
|
|
|
|
element = "background-hover";
|
2007-05-29 22:37:21 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!element.isEmpty()) {
|
2007-06-06 22:48:06 +02:00
|
|
|
d->svg.paint(painter, 0, 0, element);
|
2007-05-29 22:37:21 +02:00
|
|
|
element = QString();
|
2007-05-29 09:59:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!d->icon.isNull()) {
|
2007-06-06 07:19:44 +02:00
|
|
|
qreal iw = d->iconSize.width();
|
|
|
|
qreal ih = d->iconSize.height();
|
|
|
|
qreal deltaX = (d->size.width() - iw) / 2;
|
|
|
|
qreal deltaY = (d->size.height() - ih) / 2 ;
|
2007-06-20 01:32:04 +02:00
|
|
|
if (d->state == Private::PressedState) {
|
2007-06-06 21:45:00 +02:00
|
|
|
painter->save();
|
|
|
|
painter->setRenderHint(QPainter::SmoothPixmapTransform);
|
|
|
|
painter->scale(0.95, 0.95);
|
|
|
|
deltaY *= 0.95;
|
|
|
|
deltaX *= 0.95;
|
2007-06-17 00:35:31 +02:00
|
|
|
painter->drawPixmap((int)(deltaX + (iw * .025)), (int)(deltaY + (ih * .025)),
|
2007-06-06 21:45:00 +02:00
|
|
|
d->icon.pixmap(d->iconSize.toSize()));
|
|
|
|
painter->restore();
|
2007-06-20 01:32:04 +02:00
|
|
|
} else {
|
2007-06-17 00:35:31 +02:00
|
|
|
painter->drawPixmap((int)deltaX, (int)deltaY, d->icon.pixmap(d->iconSize.toSize()));
|
2007-06-06 07:19:44 +02:00
|
|
|
}
|
2007-05-29 09:59:50 +02:00
|
|
|
}
|
|
|
|
|
2007-06-17 00:35:31 +02:00
|
|
|
// Make it default
|
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-06-06 22:48:06 +02:00
|
|
|
} else if (d->svgElements & Private::SvgForegroundHover) {
|
|
|
|
element = "foreground-hover";
|
2007-05-29 22:37:21 +02:00
|
|
|
}
|
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-06-17 22:56:03 +02:00
|
|
|
element.clear();
|
2007-05-29 22:37:21 +02:00
|
|
|
}
|
2007-06-17 00:35:31 +02:00
|
|
|
|
|
|
|
// Draw top-left button
|
2007-07-21 06:07:41 +02:00
|
|
|
foreach (IconAction *action, d->cornerActions) {
|
|
|
|
if (action->animationId()) {
|
2007-07-21 00:30:33 +02:00
|
|
|
action->paint(painter);
|
2007-07-21 06:07:41 +02:00
|
|
|
}
|
2007-06-17 00:35:31 +02:00
|
|
|
}
|
2007-07-20 08:28:51 +02:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2007-05-29 09:59:50 +02:00
|
|
|
}
|
|
|
|
|
2007-07-21 06:07:41 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-05-29 09:59:50 +02:00
|
|
|
void Icon::setText(const QString& text)
|
|
|
|
{
|
|
|
|
d->text = text;
|
2007-07-20 08:28:51 +02:00
|
|
|
calculateSize();
|
2007-05-29 09:59:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QString Icon::text() const
|
|
|
|
{
|
|
|
|
return d->text;
|
|
|
|
}
|
|
|
|
|
2007-07-28 21:27:56 +02:00
|
|
|
void Icon::setInfoText(const QString& text)
|
|
|
|
{
|
|
|
|
d->infoText = text;
|
|
|
|
calculateSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Icon::infoText() const
|
|
|
|
{
|
|
|
|
return d->infoText;
|
|
|
|
}
|
|
|
|
|
2007-05-29 09:59:50 +02:00
|
|
|
void Icon::setIcon(const QString& icon)
|
|
|
|
{
|
|
|
|
if (icon.isEmpty()) {
|
|
|
|
setIcon(QIcon());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setIcon(KIcon(icon));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Icon::setIcon(const QIcon& icon)
|
|
|
|
{
|
|
|
|
d->icon = icon;
|
2007-07-20 08:28:51 +02:00
|
|
|
calculateSize();
|
2007-05-29 09:59:50 +02:00
|
|
|
}
|
|
|
|
|
2007-06-05 04:54:48 +02:00
|
|
|
QSizeF Icon::iconSize() const
|
|
|
|
{
|
2007-06-05 21:48:14 +02:00
|
|
|
return d->iconSize;
|
2007-06-05 04:54:48 +02:00
|
|
|
}
|
|
|
|
|
2007-07-20 08:28:51 +02:00
|
|
|
void Icon::setIconSize(const QSizeF& s)
|
2007-05-29 09:59:50 +02:00
|
|
|
{
|
2007-06-05 21:48:14 +02:00
|
|
|
d->iconSize = s;
|
2007-07-20 08:28:51 +02:00
|
|
|
calculateSize();
|
2007-05-29 09:59:50 +02:00
|
|
|
}
|
|
|
|
|
2007-07-20 08:28:51 +02:00
|
|
|
void Icon::setIconSize(int w, int h)
|
2007-05-29 09:59:50 +02:00
|
|
|
{
|
2007-07-20 08:28:51 +02:00
|
|
|
setIconSize(QSizeF(w, h));
|
2007-05-29 09:59:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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-07-21 06:07:41 +02:00
|
|
|
foreach (IconAction *action, d->cornerActions) {
|
2007-07-21 00:30:33 +02:00
|
|
|
action->event(event->type(), event->pos());
|
2007-07-21 06:07:41 +02:00
|
|
|
}
|
2007-07-21 00:30:33 +02:00
|
|
|
|
2007-07-28 10:22:44 +02:00
|
|
|
d->state = Private::PressedState;
|
2007-07-21 00:30:33 +02:00
|
|
|
QGraphicsItem::mousePressEvent(event);
|
2007-07-28 21:27:56 +02:00
|
|
|
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());
|
2007-07-28 10:22:44 +02:00
|
|
|
Private::ButtonState was = d->state;
|
2007-05-29 22:37:21 +02:00
|
|
|
|
|
|
|
if (inside) {
|
|
|
|
d->state = Private::HoverState;
|
2007-06-17 22:56:03 +02:00
|
|
|
|
2007-07-21 06:07:41 +02:00
|
|
|
foreach (IconAction *action, d->cornerActions) {
|
2007-07-28 10:22:44 +02:00
|
|
|
if (action->event(event->type(), event->pos())) {
|
|
|
|
inside = false;
|
|
|
|
break;
|
|
|
|
}
|
2007-07-21 06:07:41 +02:00
|
|
|
}
|
|
|
|
} else {
|
2007-07-21 00:30:33 +02:00
|
|
|
d->state = Private::NoState;
|
2007-07-21 06:07:41 +02:00
|
|
|
}
|
2007-05-29 09:59:50 +02:00
|
|
|
|
2007-07-28 10:22:44 +02:00
|
|
|
if (was == Private::PressedState) {
|
2007-05-29 09:59:50 +02:00
|
|
|
emit pressed(false);
|
2007-07-28 10:22:44 +02:00
|
|
|
|
|
|
|
if (inside) {
|
|
|
|
emit clicked();
|
|
|
|
}
|
2007-05-29 09:59:50 +02:00
|
|
|
}
|
2007-06-17 22:56:03 +02:00
|
|
|
|
2007-05-29 22:37:21 +02:00
|
|
|
QGraphicsItem::mouseReleaseEvent(event);
|
2007-07-28 21:27:56 +02:00
|
|
|
update();
|
2007-06-19 23:56:40 +02:00
|
|
|
}
|
|
|
|
|
2007-05-29 22:37:21 +02:00
|
|
|
void Icon::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
|
|
|
|
{
|
2007-07-21 06:07:41 +02:00
|
|
|
foreach (IconAction *action, d->cornerActions) {
|
2007-07-21 00:30:33 +02:00
|
|
|
action->show();
|
|
|
|
action->event(event->type(), event->pos());
|
2007-06-20 00:48:42 +02:00
|
|
|
}
|
|
|
|
|
2007-07-21 00:30:33 +02:00
|
|
|
d->state = Private::HoverState;
|
2007-05-29 22:37:21 +02:00
|
|
|
QGraphicsItem::hoverEnterEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Icon::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
|
|
|
|
{
|
2007-07-21 06:07:41 +02:00
|
|
|
foreach (IconAction *action, d->cornerActions) {
|
2007-07-21 00:30:33 +02:00
|
|
|
action->hide();
|
|
|
|
action->event(event->type(), event->pos());
|
2007-06-20 00:48:42 +02:00
|
|
|
}
|
|
|
|
|
2007-06-19 23:56:40 +02:00
|
|
|
d->state = Private::NoState;
|
2007-05-29 22:37:21 +02:00
|
|
|
QGraphicsItem::hoverLeaveEvent(event);
|
2007-05-29 09:59:50 +02:00
|
|
|
}
|
|
|
|
|
2007-06-17 22:56:03 +02:00
|
|
|
void Icon::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
|
|
|
if (d->state == Private::PressedState) {
|
2007-06-17 23:24:50 +02:00
|
|
|
d->state = Private::HoverState;
|
2007-06-17 22:56:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QGraphicsItem::mouseMoveEvent(event);
|
|
|
|
}
|
|
|
|
|
2007-07-21 00:30:33 +02:00
|
|
|
|
2007-05-29 09:59:50 +02:00
|
|
|
QSizeF Icon::sizeHint() const
|
|
|
|
{
|
|
|
|
return d->size;
|
|
|
|
}
|
|
|
|
|
|
|
|
QSizeF Icon::minimumSize() const
|
|
|
|
{
|
2007-06-05 21:48:14 +02:00
|
|
|
return d->size; // probably should be more flexible on this =)
|
2007-05-29 09:59:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QSizeF Icon::maximumSize() const
|
|
|
|
{
|
2007-06-05 21:48:14 +02:00
|
|
|
return d->size;
|
2007-05-29 09:59:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2007-07-20 08:28:51 +02:00
|
|
|
return boundingRect();
|
2007-05-29 09:59:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Icon::setGeometry(const QRectF &r)
|
|
|
|
{
|
2007-07-20 08:28:51 +02:00
|
|
|
// TODO: this is wrong, but we should probably never call setGeometry anyway!
|
|
|
|
setIconSize(r.size());
|
2007-05-29 09:59:50 +02:00
|
|
|
setPos(r.x(),r.y());
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Plasma
|
|
|
|
|
|
|
|
#include "icon.moc"
|