2007-05-29 09:59:50 +02:00
|
|
|
/*
|
2007-08-08 14:13:05 +02:00
|
|
|
* Copyright 2007 by Aaron Seigo <aseigo@kde.org>
|
|
|
|
* Copyright 2007 by Riccardo Iaconelli <riccardo@kde.org>
|
|
|
|
* Copyright 2007 by Matt Broadstone <mbroadst@gmail.com>
|
|
|
|
* Copyright 2006-2007 Fredrik Höglund <fredrik@kde.org>
|
2007-05-29 09:59:50 +02:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
2007-09-14 22:17:11 +02:00
|
|
|
* it under the terms of the GNU Library General Public License as
|
2007-09-14 21:06:18 +02:00
|
|
|
* published by the Free Software Foundation; either version 2, or
|
|
|
|
* (at your option) any later version.
|
2007-05-29 09:59:50 +02:00
|
|
|
*
|
|
|
|
* 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"
|
2007-08-05 19:11:46 +02:00
|
|
|
#include "icon_p.h"
|
2007-07-23 00:01:10 +02:00
|
|
|
|
|
|
|
#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-08-05 19:11:46 +02:00
|
|
|
#include <QStyleOptionGraphicsItem>
|
|
|
|
#include <QTextLayout>
|
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
|
|
|
|
2007-08-05 19:11:46 +02:00
|
|
|
#include <KIconEffect>
|
|
|
|
#include <KIconLoader>
|
2007-08-11 05:30:54 +02:00
|
|
|
#include <KIcon>
|
2007-08-05 19:11:46 +02:00
|
|
|
#include <K3Icon>
|
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-08-05 19:11:46 +02:00
|
|
|
Icon::Private::Private()
|
|
|
|
: svg("widgets/iconbutton"),
|
|
|
|
svgElements(0),
|
2007-08-08 07:29:50 +02:00
|
|
|
size(48*1.1, 48*1.1),
|
|
|
|
iconSize(48, 48),
|
2007-08-05 19:11:46 +02:00
|
|
|
state(Private::NoState),
|
|
|
|
orientation(Qt::Vertical),
|
|
|
|
calculateSizeRequested(true) // First time always true
|
2007-05-29 09:59:50 +02:00
|
|
|
{
|
2007-08-05 19:11:46 +02:00
|
|
|
svg.setContentType(Plasma::Svg::ImageSet);
|
|
|
|
svg.resize(size);
|
2007-07-21 06:07:41 +02:00
|
|
|
|
2007-08-05 19:11:46 +02:00
|
|
|
//TODO: recheck when svg changes
|
|
|
|
checkSvgElements();
|
|
|
|
}
|
2007-07-21 06:07:41 +02:00
|
|
|
|
2007-08-05 19:11:46 +02:00
|
|
|
Icon::Private::~Private()
|
2007-07-21 06:07:41 +02:00
|
|
|
{
|
2007-08-05 19:11:46 +02:00
|
|
|
qDeleteAll(cornerActions);
|
|
|
|
}
|
2007-07-21 06:07:41 +02:00
|
|
|
|
2007-08-05 19:11:46 +02:00
|
|
|
void Icon::Private::checkSvgElements()
|
|
|
|
{
|
|
|
|
if (svg.elementExists("background")) {
|
|
|
|
svgElements |= SvgBackground;
|
|
|
|
}
|
2007-08-01 02:52:26 +02:00
|
|
|
|
2007-08-05 19:11:46 +02:00
|
|
|
if (svg.elementExists("background-hover")) {
|
|
|
|
svgElements |= SvgBackgroundHover;
|
|
|
|
}
|
2007-05-29 22:37:21 +02:00
|
|
|
|
2007-08-05 19:11:46 +02:00
|
|
|
if (svg.elementExists("background-pressed")) {
|
|
|
|
svgElements |= SvgBackgroundPressed;
|
|
|
|
}
|
2007-05-29 22:37:21 +02:00
|
|
|
|
2007-08-05 19:11:46 +02:00
|
|
|
if (svg.elementExists("foreground")) {
|
|
|
|
svgElements |= SvgForeground;
|
|
|
|
}
|
2007-05-29 22:37:21 +02:00
|
|
|
|
2007-08-05 19:11:46 +02:00
|
|
|
if (svg.elementExists("foreground-hover")) {
|
|
|
|
svgElements |= SvgForegroundHover;
|
|
|
|
}
|
2007-05-29 22:37:21 +02:00
|
|
|
|
2007-08-05 19:11:46 +02:00
|
|
|
if (svg.elementExists("foreground-pressed")) {
|
|
|
|
svgElements |= SvgForegroundPressed;
|
|
|
|
}
|
2007-05-29 22:37:21 +02:00
|
|
|
|
2007-08-05 19:11:46 +02:00
|
|
|
if (svg.elementExists("minibutton")) {
|
|
|
|
svgElements |= SvgMinibutton;
|
|
|
|
}
|
2007-06-19 23:56:40 +02:00
|
|
|
|
2007-08-05 19:11:46 +02:00
|
|
|
if (svg.elementExists("minibutton-hover")) {
|
|
|
|
svgElements |= SvgMinibuttonHover;
|
|
|
|
}
|
2007-06-19 23:56:40 +02:00
|
|
|
|
2007-08-05 19:11:46 +02:00
|
|
|
if (svg.elementExists("minibutton-pressed")) {
|
|
|
|
svgElements |= SvgMinibuttonPressed;
|
|
|
|
}
|
|
|
|
}
|
2007-06-19 23:56:40 +02:00
|
|
|
|
2007-07-21 00:30:33 +02:00
|
|
|
|
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: {
|
2007-08-05 19:13:07 +02:00
|
|
|
// kDebug() << "IconAction::event got a QEvent::MouseButtonRelease, " << isSelected();
|
2007-07-21 06:07:41 +02:00
|
|
|
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-08-05 19:11:46 +02:00
|
|
|
: Plasma::Widget(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)
|
2007-08-05 19:11:46 +02:00
|
|
|
: Plasma::Widget(parent),
|
2007-07-20 08:28:51 +02:00
|
|
|
d(new Private)
|
|
|
|
{
|
|
|
|
setText(text);
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
2007-08-11 00:24:49 +02:00
|
|
|
Icon::Icon(const QIcon &icon, const QString &text, QGraphicsItem *parent)
|
2007-08-05 19:11:46 +02:00
|
|
|
: Plasma::Widget(parent),
|
2007-07-20 08:28:51 +02:00
|
|
|
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()
|
|
|
|
{
|
2007-08-05 19:11:46 +02:00
|
|
|
// setAcceptedMouseButtons(Qt::LeftButton);
|
2007-07-20 08:28:51 +02:00
|
|
|
setAcceptsHoverEvents(true);
|
2007-08-05 19:11:46 +02:00
|
|
|
|
|
|
|
int focusHMargin = QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin);
|
|
|
|
int focusVMargin = QApplication::style()->pixelMetric(QStyle::PM_FocusFrameVMargin);
|
|
|
|
|
|
|
|
// Margins for horizontal mode (list views, tree views, table views)
|
|
|
|
d->setHorizontalMargin(Private::TextMargin, focusHMargin, focusVMargin);
|
|
|
|
d->setHorizontalMargin(Private::IconMargin, focusHMargin, focusVMargin);
|
|
|
|
d->setHorizontalMargin(Private::ItemMargin, 0, 0);
|
|
|
|
|
|
|
|
// Margins for vertical mode (icon views)
|
|
|
|
d->setVerticalMargin(Private::TextMargin, 6, 2);
|
|
|
|
d->setVerticalMargin(Private::IconMargin, focusHMargin, focusVMargin);
|
|
|
|
d->setVerticalMargin(Private::ItemMargin, 0, 0);
|
2007-08-08 07:29:50 +02:00
|
|
|
|
|
|
|
calculateSize();
|
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) {
|
2007-08-02 15:12:59 +02:00
|
|
|
kDebug() << "Icon::addAction(QAction*) no more room for more actions!";
|
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
|
|
|
}
|
|
|
|
|
2007-08-05 19:11:46 +02:00
|
|
|
QSizeF Icon::Private::displaySizeHint(const QStyleOptionGraphicsItem *option) const
|
2007-07-20 08:28:51 +02:00
|
|
|
{
|
2007-08-05 19:11:46 +02:00
|
|
|
QString label = text;
|
2007-08-05 20:03:26 +02:00
|
|
|
// const qreal maxWidth = (orientation == Qt::Vertical) ? iconSize.width() + 10 : 32757;
|
|
|
|
// NOTE: find a way to use the other layoutText, it currently returns nominal width, when
|
|
|
|
// we actually need the actual width.
|
|
|
|
|
2007-08-05 19:11:46 +02:00
|
|
|
|
|
|
|
// To compute the nominal size for the label + info, we'll just append
|
|
|
|
// the information string to the label
|
|
|
|
const QString info = infoText;
|
|
|
|
if (!info.isEmpty())
|
|
|
|
label += QString(QChar::LineSeparator) + info;
|
|
|
|
|
|
|
|
QTextLayout layout;
|
|
|
|
setLayoutOptions(layout, option);
|
|
|
|
QSizeF size = layoutText(layout, option, label, QSizeF(iconSize.width() + 10, 32757));
|
|
|
|
|
|
|
|
return addMargin(size, TextMargin);
|
|
|
|
}
|
|
|
|
|
2007-08-08 07:29:50 +02:00
|
|
|
void Icon::calculateSize()
|
|
|
|
{
|
|
|
|
if (d->calculateSizeRequested = true) {
|
|
|
|
// We do this to get size hint information before the icon has been drawn, as
|
|
|
|
// we have no access to the style option before that time. So we create a dummy.
|
|
|
|
QStyleOptionGraphicsItem option;
|
|
|
|
option.state = QStyle::State_None;
|
|
|
|
option.rect = boundingRect().toRect();
|
|
|
|
calculateSize(&option);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
d->calculateSizeRequested = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-05 19:11:46 +02:00
|
|
|
void Icon::calculateSize(const QStyleOptionGraphicsItem *option)
|
|
|
|
{
|
|
|
|
if (!d->calculateSizeRequested)
|
|
|
|
return;
|
|
|
|
|
2007-07-20 08:28:51 +02:00
|
|
|
prepareGeometryChange();
|
2007-08-05 19:11:46 +02:00
|
|
|
d->setActiveMargins();
|
|
|
|
|
|
|
|
const QSizeF displaySize = d->displaySizeHint(option);
|
|
|
|
const QSizeF decorationSize = d->addMargin(d->iconSize, Private::IconMargin);
|
|
|
|
|
|
|
|
QSizeF newSize;
|
|
|
|
if (d->orientation == Qt::Vertical)
|
|
|
|
{
|
|
|
|
newSize.rwidth() = qMax(decorationSize.width(), displaySize.width());
|
|
|
|
newSize.rheight() = decorationSize.height() + displaySize.height() + 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
newSize.rwidth() = decorationSize.width() + displaySize.width() + 1;
|
|
|
|
newSize.rheight() = qMax(decorationSize.height(), displaySize.height());
|
|
|
|
}
|
2007-07-20 08:28:51 +02:00
|
|
|
|
2007-08-05 19:11:46 +02:00
|
|
|
d->size = d->addMargin(newSize, Private::ItemMargin);
|
2007-07-20 08:28:51 +02:00
|
|
|
d->svg.resize(d->size);
|
2007-08-05 19:11:46 +02:00
|
|
|
d->calculateSizeRequested = false;
|
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
|
|
|
}
|
|
|
|
|
2007-08-01 02:52:26 +02:00
|
|
|
void Icon::Private::drawBackground(QPainter *painter)
|
|
|
|
{
|
|
|
|
QString element;
|
|
|
|
if (svgElements & Private::SvgBackground) {
|
|
|
|
element = "background";
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
case Private::NoState:
|
|
|
|
break;
|
|
|
|
case Private::HoverState:
|
|
|
|
if (svgElements & Private::SvgBackgroundHover) {
|
|
|
|
element = "background-hover";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Private::PressedState:
|
|
|
|
if (svgElements & Private::SvgBackgroundPressed) {
|
|
|
|
element = "background-pressed";
|
|
|
|
} else if (svgElements & Private::SvgBackgroundHover) {
|
|
|
|
element = "background-hover";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!element.isEmpty()) {
|
|
|
|
svg.paint(painter, 0, 0, element);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Icon::Private::drawForeground(QPainter *painter)
|
|
|
|
{
|
|
|
|
QString element;
|
|
|
|
if (svgElements & Private::SvgForeground) {
|
|
|
|
element = "foreground";
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
case Private::NoState:
|
|
|
|
break;
|
|
|
|
case Private::HoverState:
|
|
|
|
if (svgElements & Private::SvgForegroundHover) {
|
|
|
|
element = "foreground-hover";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Private::PressedState:
|
|
|
|
if (svgElements & Private::SvgForegroundPressed) {
|
|
|
|
element = "foreground-pressed";
|
|
|
|
} else if (svgElements & Private::SvgForegroundHover) {
|
|
|
|
element = "foreground-hover";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!element.isEmpty()) {
|
|
|
|
svg.paint(painter, 0, 0, element);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-05 19:11:46 +02:00
|
|
|
QPixmap Icon::Private::decoration(const QStyleOptionGraphicsItem *option) const
|
|
|
|
{
|
|
|
|
QPixmap result;
|
|
|
|
|
|
|
|
QIcon::Mode mode = option->state & QStyle::State_Enabled ? QIcon::Normal : QIcon::Disabled;
|
|
|
|
QIcon::State state = option->state & QStyle::State_Open ? QIcon::On : QIcon::Off;
|
|
|
|
const QSize size = icon.actualSize(iconSize.toSize(), mode, state);
|
|
|
|
result = icon.pixmap(size, mode, state);
|
|
|
|
|
|
|
|
if (!result.isNull())
|
|
|
|
{
|
|
|
|
// Apply the configured hover effect NOTE: This has no visible effect because we
|
|
|
|
// do drawForeground() -MB
|
|
|
|
if (option->state & QStyle::State_MouseOver) {
|
|
|
|
KIconEffect *effect = KIconLoader::global()->iconEffect();
|
|
|
|
|
|
|
|
// Note that in KIconLoader terminology, active = hover.
|
|
|
|
// ### We're assuming that the icon group is desktop/filemanager, since this
|
|
|
|
// is KFileItemDelegate.
|
|
|
|
if (effect->hasEffect(K3Icon::Desktop, K3Icon::ActiveState)) {
|
|
|
|
result = effect->apply(result, K3Icon::Desktop, K3Icon::ActiveState);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
QPointF Icon::Private::iconPosition(const QStyleOptionGraphicsItem *option, const QPixmap &pixmap) const
|
|
|
|
{
|
|
|
|
const QRectF itemRect = subtractMargin(option->rect, Private::ItemMargin);
|
|
|
|
Qt::Alignment alignment = Qt::AlignHCenter | Qt::AlignTop; // NOTE: This assumes always top-center -MB
|
|
|
|
|
|
|
|
// Compute the nominal decoration rectangle
|
|
|
|
const QSizeF size = addMargin(iconSize, Private::IconMargin);
|
|
|
|
const QRect iconRect = QStyle::alignedRect(option->direction, alignment, size.toSize(), itemRect.toRect());
|
|
|
|
|
|
|
|
// Position the pixmap in the center of the rectangle
|
|
|
|
QRect pixmapRect = pixmap.rect();
|
|
|
|
pixmapRect.moveCenter(iconRect.center());
|
|
|
|
|
|
|
|
// add a gimmicky margin of 5px to y, TEMP TEMP TEMP
|
|
|
|
// pixmapRect = pixmapRect.adjusted(0, 5, 0, 0);
|
|
|
|
|
|
|
|
return QPointF(pixmapRect.topLeft());
|
|
|
|
}
|
|
|
|
|
|
|
|
QRectF Icon::Private::labelRectangle(const QStyleOptionGraphicsItem *option, const QPixmap &icon,
|
|
|
|
const QString &string) const
|
|
|
|
{
|
|
|
|
Q_UNUSED(string)
|
|
|
|
|
|
|
|
if (icon.isNull())
|
|
|
|
return option->rect;
|
|
|
|
|
|
|
|
const QSizeF decoSize = addMargin(iconSize, Private::IconMargin);
|
|
|
|
|
|
|
|
const QRectF itemRect = subtractMargin(option->rect, Private::ItemMargin);
|
|
|
|
QRectF textArea(QPointF(0, 0), itemRect.size());
|
|
|
|
|
|
|
|
textArea.setTop(decoSize.height() + 1);
|
|
|
|
textArea.translate(itemRect.topLeft());
|
|
|
|
return QRectF(QStyle::visualRect(option->direction, option->rect, textArea.toRect()));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lays the text out in a rectangle no larger than constraints, eliding it as necessary
|
|
|
|
QSizeF Icon::Private::layoutText(QTextLayout &layout, const QStyleOptionGraphicsItem *option,
|
|
|
|
const QString &text, const QSizeF &constraints) const
|
|
|
|
{
|
|
|
|
const QSizeF size = layoutText(layout, text, constraints.width());
|
|
|
|
|
|
|
|
if (size.width() > constraints.width() || size.height() > constraints.height())
|
|
|
|
{
|
|
|
|
const QString elided = elidedText(layout, option, constraints);
|
|
|
|
return layoutText(layout, elided, constraints.width());
|
|
|
|
}
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lays the text out in a rectangle no wider than maxWidth
|
|
|
|
QSizeF Icon::Private::layoutText(QTextLayout &layout, const QString &text, qreal maxWidth) const
|
|
|
|
{
|
|
|
|
QFontMetricsF metrics(layout.font());
|
|
|
|
qreal leading = metrics.leading();
|
|
|
|
qreal height = 0.0;
|
|
|
|
qreal widthUsed = 0.0;
|
|
|
|
QTextLine line;
|
|
|
|
|
|
|
|
layout.setText(text);
|
|
|
|
|
|
|
|
layout.beginLayout();
|
|
|
|
|
|
|
|
while ((line = layout.createLine()).isValid())
|
|
|
|
{
|
|
|
|
line.setLineWidth(maxWidth);
|
|
|
|
height += leading;
|
|
|
|
line.setPosition(QPointF(0.0, height));
|
|
|
|
height += line.height();
|
|
|
|
widthUsed = qMax(widthUsed, line.naturalTextWidth());
|
|
|
|
}
|
|
|
|
layout.endLayout();
|
|
|
|
|
|
|
|
return QSizeF(widthUsed, height);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Elides the text in the layout, by iterating over each line in the layout, eliding
|
|
|
|
// or word breaking the line if it's wider than the max width, and finally adding an
|
|
|
|
// ellipses at the end of the last line, if there are more lines than will fit within
|
|
|
|
// the vertical size constraints.
|
|
|
|
QString Icon::Private::elidedText(QTextLayout &layout, const QStyleOptionGraphicsItem *option,
|
|
|
|
const QSizeF &size) const
|
|
|
|
{
|
|
|
|
Q_UNUSED(option)
|
|
|
|
|
|
|
|
QFontMetricsF metrics(layout.font());
|
|
|
|
const QString text = layout.text();
|
|
|
|
qreal maxWidth = size.width();
|
|
|
|
qreal maxHeight = size.height();
|
|
|
|
qreal height = 0;
|
|
|
|
|
|
|
|
// If the string contains a single line of text it shouldn't be word wrapped
|
|
|
|
if (text.indexOf(QChar::LineSeparator) == -1)
|
|
|
|
return metrics.elidedText(text, Qt::ElideRight, maxWidth);
|
|
|
|
|
|
|
|
// Elide each line that has already been laid out in the layout.
|
|
|
|
QString elided;
|
|
|
|
elided.reserve(text.length());
|
|
|
|
|
|
|
|
for (int i = 0; i < layout.lineCount(); i++)
|
|
|
|
{
|
|
|
|
QTextLine line = layout.lineAt(i);
|
|
|
|
int start = line.textStart();
|
|
|
|
int length = line.textLength();
|
|
|
|
|
|
|
|
height += metrics.leading();
|
|
|
|
if (height + line.height() + metrics.lineSpacing() > maxHeight)
|
|
|
|
{
|
|
|
|
// Unfortunately, if the line ends because of a line separator, elidedText() will be too
|
|
|
|
// clever and keep adding lines until it finds one that's too wide.
|
|
|
|
if (line.naturalTextWidth() < maxWidth && text[start + length - 1] == QChar::LineSeparator)
|
|
|
|
elided += text.mid(start, length - 1);
|
|
|
|
else
|
|
|
|
elided += metrics.elidedText(text.mid(start), Qt::ElideRight, maxWidth);
|
|
|
|
break;
|
2007-08-01 03:25:01 +02:00
|
|
|
}
|
2007-08-05 19:11:46 +02:00
|
|
|
else if (line.naturalTextWidth() > maxWidth)
|
|
|
|
elided += metrics.elidedText(text.mid(start, length), Qt::ElideRight, maxWidth);
|
|
|
|
else
|
|
|
|
elided += text.mid(start, length);
|
|
|
|
|
|
|
|
height += line.height();
|
2007-08-01 03:25:01 +02:00
|
|
|
}
|
2007-08-05 19:11:46 +02:00
|
|
|
|
|
|
|
return elided;
|
2007-08-01 03:25:01 +02:00
|
|
|
}
|
|
|
|
|
2007-08-05 19:11:46 +02:00
|
|
|
void Icon::Private::layoutTextItems(const QStyleOptionGraphicsItem *option,
|
|
|
|
const QPixmap &icon, QTextLayout *labelLayout,
|
|
|
|
QTextLayout *infoLayout, QRectF *textBoundingRect) const
|
2007-08-01 03:25:01 +02:00
|
|
|
{
|
2007-08-05 19:11:46 +02:00
|
|
|
bool showInformation = false;
|
|
|
|
|
|
|
|
setLayoutOptions(*labelLayout, option);
|
|
|
|
|
|
|
|
QFontMetricsF fm(labelLayout->font());
|
|
|
|
const QRectF textArea = labelRectangle(option, icon, text);
|
|
|
|
QRectF textRect = subtractMargin(textArea, Private::TextMargin);
|
2007-08-01 03:25:01 +02:00
|
|
|
|
2007-08-05 19:11:46 +02:00
|
|
|
// Sizes and constraints for the different text parts
|
|
|
|
QSizeF maxLabelSize = textRect.size();
|
|
|
|
QSizeF maxInfoSize = textRect.size();
|
|
|
|
QSizeF labelSize;
|
|
|
|
QSizeF infoSize;
|
2007-08-01 03:25:01 +02:00
|
|
|
|
2007-08-05 19:11:46 +02:00
|
|
|
// If we have additional info text, and there's space for at least two lines of text,
|
|
|
|
// adjust the max label size to make room for at least one line of the info text
|
|
|
|
if (!infoText.isEmpty() && textRect.height() >= fm.lineSpacing() * 2)
|
|
|
|
{
|
|
|
|
infoLayout->setFont(labelLayout->font());
|
|
|
|
infoLayout->setTextOption(labelLayout->textOption());
|
|
|
|
|
|
|
|
maxLabelSize.rheight() -= fm.lineSpacing();
|
|
|
|
showInformation = true;
|
2007-08-01 03:25:01 +02:00
|
|
|
}
|
2007-08-05 19:11:46 +02:00
|
|
|
|
|
|
|
// Lay out the label text, and adjust the max info size based on the label size
|
|
|
|
labelSize = layoutText(*labelLayout, option, text, maxLabelSize);
|
|
|
|
maxInfoSize.rheight() -= labelSize.height();
|
|
|
|
|
|
|
|
// Lay out the info text
|
|
|
|
if (showInformation)
|
|
|
|
infoSize = layoutText(*infoLayout, option, infoText, maxInfoSize);
|
|
|
|
else
|
|
|
|
infoSize = QSizeF(0, 0);
|
|
|
|
|
|
|
|
// Compute the bounding rect of the text
|
|
|
|
const Qt::Alignment alignment = labelLayout->textOption().alignment();
|
|
|
|
const QSizeF size(qMax(labelSize.width(), infoSize.width()), labelSize.height() + infoSize.height());
|
|
|
|
*textBoundingRect = QStyle::alignedRect(option->direction, alignment, size.toSize(), textRect.toRect());
|
|
|
|
|
|
|
|
// Compute the positions where we should draw the layouts
|
|
|
|
labelLayout->setPosition(QPointF(textRect.x(), textBoundingRect->y()));
|
|
|
|
infoLayout->setPosition(QPointF(textRect.x(), textBoundingRect->y() + labelSize.height()));
|
2007-08-01 03:25:01 +02:00
|
|
|
}
|
|
|
|
|
2007-08-05 19:11:46 +02:00
|
|
|
QBrush Icon::Private::foregroundBrush(const QStyleOptionGraphicsItem *option) const
|
|
|
|
{
|
|
|
|
const QPalette::ColorGroup group = option->state & QStyle::State_Enabled ?
|
|
|
|
QPalette::Normal : QPalette::Disabled;
|
|
|
|
|
|
|
|
// Always use the highlight color for selected items
|
|
|
|
if (option->state & QStyle::State_Selected)
|
|
|
|
return option->palette.brush(group, QPalette::HighlightedText);
|
|
|
|
|
|
|
|
return option->palette.brush(group, QPalette::Text);
|
|
|
|
}
|
|
|
|
|
|
|
|
QBrush Icon::Private::backgroundBrush(const QStyleOptionGraphicsItem *option) const
|
|
|
|
{
|
|
|
|
const QPalette::ColorGroup group = option->state & QStyle::State_Enabled ?
|
|
|
|
QPalette::Normal : QPalette::Disabled;
|
|
|
|
|
|
|
|
QBrush background(Qt::NoBrush);
|
|
|
|
|
|
|
|
// Always use the highlight color for selected items
|
|
|
|
if (option->state & QStyle::State_Selected)
|
|
|
|
background = option->palette.brush(group, QPalette::Highlight);
|
|
|
|
|
|
|
|
return background;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Icon::Private::drawTextItems(QPainter *painter, const QStyleOptionGraphicsItem *option,
|
|
|
|
const QTextLayout &labelLayout, const QTextLayout &infoLayout) const
|
|
|
|
{
|
|
|
|
QPen pen(foregroundBrush(option), 0);
|
|
|
|
painter->setPen(pen);
|
|
|
|
labelLayout.draw(painter, QPointF());
|
|
|
|
|
|
|
|
if (!infoLayout.text().isEmpty())
|
|
|
|
{
|
|
|
|
QColor color;
|
|
|
|
if (option->state & QStyle::State_Selected)
|
|
|
|
{
|
|
|
|
color = option->palette.color(QPalette::HighlightedText);
|
|
|
|
color.setAlphaF(.5);
|
|
|
|
} else
|
|
|
|
color = option->palette.color(QPalette::Highlight);
|
|
|
|
|
|
|
|
painter->setPen(color);
|
|
|
|
infoLayout.draw(painter, QPointF());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Icon::paintWidget(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
|
2007-05-29 09:59:50 +02:00
|
|
|
{
|
|
|
|
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-08-05 19:11:46 +02:00
|
|
|
calculateSize(option);
|
|
|
|
d->setActiveMargins();
|
|
|
|
|
|
|
|
// Compute the metrics, and lay out the text items
|
|
|
|
// ========================================================================
|
|
|
|
QPixmap icon = d->decoration(option);
|
|
|
|
const QPointF iconPos = d->iconPosition(option, icon);
|
|
|
|
|
|
|
|
QTextLayout labelLayout, infoLayout;
|
|
|
|
QRectF textBoundingRect;
|
|
|
|
d->layoutTextItems(option, icon, &labelLayout, &infoLayout, &textBoundingRect);
|
|
|
|
|
2007-07-21 06:07:41 +02:00
|
|
|
d->svg.resize(d->size);
|
2007-08-01 02:52:26 +02:00
|
|
|
d->drawBackground(painter);
|
2007-08-05 19:11:46 +02:00
|
|
|
|
|
|
|
// draw icon
|
|
|
|
painter->drawPixmap(iconPos, icon);
|
2007-05-29 09:59:50 +02:00
|
|
|
|
2007-08-01 02:52:26 +02:00
|
|
|
d->drawForeground(painter);
|
2007-06-17 00:35:31 +02:00
|
|
|
|
2007-08-01 02:52:26 +02:00
|
|
|
// Draw corner actions
|
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
|
2007-08-05 19:11:46 +02:00
|
|
|
d->drawTextItems(painter, option, labelLayout, infoLayout);
|
|
|
|
|
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-08-08 07:29:50 +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;
|
2007-08-08 07:29:50 +02:00
|
|
|
calculateSize();
|
2007-07-28 21:27:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QString Icon::infoText() const
|
|
|
|
{
|
|
|
|
return d->infoText;
|
|
|
|
}
|
|
|
|
|
2007-08-11 00:24:49 +02:00
|
|
|
QIcon Icon::icon() const
|
2007-08-05 20:03:26 +02:00
|
|
|
{
|
|
|
|
return d->icon;
|
|
|
|
}
|
|
|
|
|
2007-05-29 09:59:50 +02:00
|
|
|
void Icon::setIcon(const QString& icon)
|
|
|
|
{
|
|
|
|
if (icon.isEmpty()) {
|
2007-08-11 00:24:49 +02:00
|
|
|
setIcon(QIcon());
|
2007-05-29 09:59:50 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-08-11 05:30:54 +02:00
|
|
|
setIcon(KIcon(icon));
|
2007-05-29 09:59:50 +02:00
|
|
|
}
|
|
|
|
|
2007-08-11 00:24:49 +02:00
|
|
|
void Icon::setIcon(const QIcon& icon)
|
2007-05-29 09:59:50 +02:00
|
|
|
{
|
|
|
|
d->icon = icon;
|
2007-08-08 07:29:50 +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-08-08 07:29:50 +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-08-05 19:11:46 +02:00
|
|
|
|
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-08-05 19:11:46 +02:00
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2007-08-05 19:11:46 +02:00
|
|
|
else 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-08-05 19:11:46 +02:00
|
|
|
d->state = Private::NoState;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
d->state = Private::NoState;
|
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-08-14 08:59:36 +02:00
|
|
|
QSizeF Icon::sizeHint() const
|
|
|
|
{
|
|
|
|
return d->size;
|
|
|
|
}
|
|
|
|
|
2007-08-31 17:21:30 +02:00
|
|
|
Qt::Orientations Icon::expandingDirections() const
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-08-08 07:29:50 +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());
|
|
|
|
}
|
2007-08-08 07:29:50 +02:00
|
|
|
*/
|
2007-05-29 09:59:50 +02:00
|
|
|
|
|
|
|
} // namespace Plasma
|
|
|
|
|
|
|
|
#include "icon.moc"
|