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-12-26 13:37:54 +01:00
|
|
|
* Copyright 2007 by Marco Martin <notmart@gmail.com>
|
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-11-19 16:40:55 +01:00
|
|
|
#include "effects/blur.cpp"
|
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-06-17 00:35:31 +02:00
|
|
|
#include <KUrl>
|
|
|
|
#include <KRun>
|
|
|
|
#include <KMimeType>
|
2007-05-29 09:59:50 +02:00
|
|
|
#include <KDebug>
|
2007-12-23 11:51:00 +01:00
|
|
|
#include <KColorScheme>
|
|
|
|
|
|
|
|
#include <plasma/theme.h>
|
2007-05-29 09:59:50 +02:00
|
|
|
|
2007-06-19 23:56:40 +02:00
|
|
|
#include "phase.h"
|
2007-05-29 09:59:50 +02:00
|
|
|
#include "svg.h"
|
|
|
|
|
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
2007-08-05 19:11:46 +02:00
|
|
|
Icon::Private::Private()
|
|
|
|
: svg("widgets/iconbutton"),
|
2007-12-20 17:28:47 +01:00
|
|
|
iconSvg(0),
|
2007-08-05 19:11:46 +02:00
|
|
|
svgElements(0),
|
2007-08-08 07:29:50 +02:00
|
|
|
iconSize(48, 48),
|
2007-12-16 11:09:07 +01:00
|
|
|
states(Private::NoState),
|
2007-12-28 12:29:56 +01:00
|
|
|
orientation(Qt::Vertical)
|
2007-05-29 09:59:50 +02:00
|
|
|
{
|
2007-08-05 19:11:46 +02:00
|
|
|
svg.setContentType(Plasma::Svg::ImageSet);
|
2007-07-21 06:07:41 +02:00
|
|
|
|
2007-08-05 19:11:46 +02:00
|
|
|
//TODO: recheck when svg changes
|
|
|
|
checkSvgElements();
|
2007-12-23 11:51:00 +01:00
|
|
|
|
2007-12-23 16:13:44 +01:00
|
|
|
textColor = KColorScheme(QPalette::Active, KColorScheme::Window,
|
2007-12-23 11:51:00 +01:00
|
|
|
Plasma::Theme::self()->colors()).foreground().color();
|
2007-12-23 16:13:44 +01:00
|
|
|
shadowColor = KColorScheme(QPalette::Active, KColorScheme::Window,
|
2007-12-23 11:51:00 +01:00
|
|
|
Plasma::Theme::self()->colors()).background().color();
|
|
|
|
shadowColor.setAlphaF(.6);
|
2007-08-05 19:11:46 +02:00
|
|
|
}
|
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-12-20 17:28:47 +01:00
|
|
|
delete iconSvg;
|
2007-08-05 19:11:46 +02:00
|
|
|
}
|
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) {
|
2007-12-21 22:35:07 +01:00
|
|
|
case QEvent::GraphicsSceneMousePress: {
|
2007-07-21 06:07:41 +02:00
|
|
|
setSelected(m_rect.contains(pos));
|
|
|
|
return isSelected();
|
|
|
|
}
|
|
|
|
break;
|
2007-07-21 00:30:33 +02:00
|
|
|
|
2007-12-21 22:35:07 +01:00
|
|
|
case QEvent::GraphicsSceneMouseMove: {
|
2007-07-21 06:07:41 +02:00
|
|
|
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-12-21 22:35:07 +01:00
|
|
|
case QEvent::GraphicsSceneMouseRelease: {
|
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
|
|
|
|
2007-12-26 13:37:54 +01:00
|
|
|
d->setActiveMargins();
|
|
|
|
currentSize = QSizeF(-1,-1);
|
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-11-30 02:06:27 +01:00
|
|
|
iconAction->setRect(QRectF(size().width() - 38, 6, 32, 32));
|
2007-07-21 06:07:41 +02:00
|
|
|
break;
|
|
|
|
case Private::BottomLeft:
|
2007-11-30 02:06:27 +01:00
|
|
|
iconAction->setRect(QRectF(6, size().height() - 38, 32, 32));
|
2007-07-21 06:07:41 +02:00
|
|
|
break;
|
|
|
|
case Private::BottomRight:
|
2007-11-30 02:06:27 +01:00
|
|
|
iconAction->setRect(QRectF(size().width() - 38, 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-12-26 14:44:57 +01:00
|
|
|
int Icon::numDisplayLines()
|
|
|
|
{
|
|
|
|
return d->numDisplayLines;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Icon::setNumDisplayLines(int numLines)
|
|
|
|
{
|
|
|
|
if(numLines > d->maxDisplayLines) {
|
|
|
|
d->numDisplayLines = d->maxDisplayLines;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
d->numDisplayLines = numLines;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QSizeF Icon::sizeHint() const
|
|
|
|
{
|
|
|
|
return currentSize;
|
|
|
|
}
|
|
|
|
|
2007-12-23 11:51:00 +01:00
|
|
|
QSizeF Icon::Private::displaySizeHint(const QStyleOptionGraphicsItem *option, const qreal width) const
|
|
|
|
{
|
|
|
|
if (text.isEmpty() && infoText.isEmpty()) {
|
|
|
|
return QSizeF( .0, .0 );
|
|
|
|
}
|
|
|
|
QString label = text;
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
|
|
|
|
qreal textWidth = width -
|
|
|
|
horizontalMargin[Private::TextMargin].left -
|
|
|
|
horizontalMargin[Private::TextMargin].right;
|
|
|
|
|
|
|
|
//allow only five lines of text
|
2007-12-26 14:44:57 +01:00
|
|
|
const qreal maxHeight = numDisplayLines*Plasma::Theme::self()->fontMetrics().lineSpacing();
|
2007-12-23 11:51:00 +01: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(textWidth, maxHeight));
|
|
|
|
|
|
|
|
return addMargin(size, TextMargin);
|
|
|
|
}
|
|
|
|
|
2007-12-26 13:37:54 +01:00
|
|
|
void Icon::layoutIcons(const QStyleOptionGraphicsItem *option)
|
2007-08-05 19:11:46 +02:00
|
|
|
{
|
2007-12-26 13:37:54 +01:00
|
|
|
if (size() == currentSize) {
|
2007-08-05 19:11:46 +02:00
|
|
|
return;
|
2007-12-26 13:37:54 +01:00
|
|
|
}
|
2007-08-05 19:11:46 +02:00
|
|
|
|
2007-12-26 13:37:54 +01:00
|
|
|
currentSize = size();
|
2007-08-05 19:11:46 +02:00
|
|
|
d->setActiveMargins();
|
|
|
|
|
2007-12-26 13:37:54 +01:00
|
|
|
//calculate icon size based on the available space
|
|
|
|
qreal iconWidth;
|
2007-07-20 08:28:51 +02:00
|
|
|
|
2007-12-28 12:29:56 +01:00
|
|
|
if (d->orientation == Qt::Vertical) {
|
|
|
|
qreal heightAvail;
|
|
|
|
//if there is text resize the icon in order to make room for the text
|
|
|
|
if (!d->text.isEmpty() || !d->infoText.isEmpty()) {
|
|
|
|
heightAvail = currentSize.height() -
|
|
|
|
d->displaySizeHint(option, currentSize.width()).height() -
|
|
|
|
d->verticalMargin[Private::TextMargin].top -
|
|
|
|
d->verticalMargin[Private::TextMargin].bottom;
|
|
|
|
//never make a label higher than half the total height
|
|
|
|
heightAvail = qMax(heightAvail, currentSize.height()/2);
|
|
|
|
}else{
|
|
|
|
heightAvail = currentSize.height();
|
|
|
|
}
|
|
|
|
|
|
|
|
//aspect ratio very "tall"
|
|
|
|
if (currentSize.width() < heightAvail) {
|
|
|
|
iconWidth = currentSize.width() -
|
|
|
|
d->horizontalMargin[Private::IconMargin].left -
|
|
|
|
d->horizontalMargin[Private::IconMargin].right;
|
|
|
|
}else{
|
|
|
|
iconWidth = heightAvail -
|
|
|
|
d->verticalMargin[Private::IconMargin].top -
|
|
|
|
d->verticalMargin[Private::IconMargin].bottom;
|
|
|
|
}
|
|
|
|
//Horizontal layout
|
2007-12-26 13:37:54 +01:00
|
|
|
}else{
|
2007-12-28 12:29:56 +01:00
|
|
|
qreal widthAvail;
|
|
|
|
QFontMetricsF fm(font());
|
|
|
|
|
|
|
|
//make room for at most 14 characters
|
|
|
|
qreal textWidth = qMax(fm.width(d->text.left(12)),
|
|
|
|
fm.width(d->infoText.left(12))) +
|
|
|
|
fm.width("xx") +
|
|
|
|
d->horizontalMargin[Private::TextMargin].left +
|
|
|
|
d->horizontalMargin[Private::TextMargin].right;
|
|
|
|
|
|
|
|
//if there is text resize the icon in order to make room for the text
|
|
|
|
if (!d->text.isEmpty() || !d->infoText.isEmpty()) {
|
|
|
|
widthAvail = currentSize.width() -
|
|
|
|
//FIXME: fontmetrics
|
|
|
|
textWidth -
|
|
|
|
d->horizontalMargin[Private::TextMargin].left -
|
|
|
|
d->horizontalMargin[Private::TextMargin].right;
|
|
|
|
}else{
|
|
|
|
widthAvail = currentSize.width();
|
|
|
|
}
|
|
|
|
|
|
|
|
//aspect ratio very "wide"
|
|
|
|
if (currentSize.height() < widthAvail) {
|
|
|
|
iconWidth = currentSize.height() -
|
|
|
|
d->verticalMargin[Private::IconMargin].top -
|
|
|
|
d->verticalMargin[Private::IconMargin].bottom;
|
|
|
|
}else{
|
|
|
|
iconWidth = widthAvail -
|
|
|
|
d->horizontalMargin[Private::IconMargin].left -
|
|
|
|
d->horizontalMargin[Private::IconMargin].right;
|
|
|
|
}
|
2007-12-26 13:37:54 +01:00
|
|
|
}
|
2007-12-28 12:29:56 +01:00
|
|
|
|
2007-12-26 13:37:54 +01:00
|
|
|
d->iconSize = QSizeF(iconWidth, iconWidth);
|
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:
|
2007-12-26 13:37:54 +01:00
|
|
|
iconAction->setRect(QRectF(currentSize.width() - 38, 6, 32, 32));
|
2007-07-21 06:19:00 +02:00
|
|
|
break;
|
|
|
|
case Private::BottomLeft:
|
2007-12-26 13:37:54 +01:00
|
|
|
iconAction->setRect(QRectF(6, currentSize.height() - 38, 32, 32));
|
2007-07-21 06:19:00 +02:00
|
|
|
break;
|
|
|
|
case Private::BottomRight:
|
2007-12-26 13:37:54 +01:00
|
|
|
iconAction->setRect(QRectF(currentSize.width() - 38, currentSize.height() - 38, 32, 32));
|
2007-07-21 06:19:00 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
|
2007-07-20 08:28:51 +02:00
|
|
|
}
|
|
|
|
|
2007-12-22 06:31:57 +01:00
|
|
|
void Icon::setSvg(const QString &svgFilePath, const QString &elementId)
|
2007-12-19 21:32:31 +01:00
|
|
|
{
|
2007-12-20 17:28:47 +01:00
|
|
|
if (!d->iconSvg) {
|
|
|
|
d->iconSvg = new Plasma::Svg(svgFilePath);
|
|
|
|
} else {
|
|
|
|
d->iconSvg->setFile(svgFilePath);
|
|
|
|
}
|
2007-12-22 06:31:57 +01:00
|
|
|
|
|
|
|
d->iconSvgElement = elementId;
|
2007-12-19 21:32:31 +01:00
|
|
|
}
|
|
|
|
|
2007-12-16 11:09:07 +01:00
|
|
|
void Icon::Private::drawBackground(QPainter *painter, IconState state)
|
2007-08-01 02:52:26 +02:00
|
|
|
{
|
|
|
|
QString element;
|
|
|
|
if (svgElements & Private::SvgBackground) {
|
|
|
|
element = "background";
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
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;
|
2007-12-16 11:09:07 +01:00
|
|
|
default:
|
|
|
|
break;
|
2007-08-01 02:52:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!element.isEmpty()) {
|
|
|
|
svg.paint(painter, 0, 0, element);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-16 11:09:07 +01:00
|
|
|
void Icon::Private::drawForeground(QPainter *painter, IconState state)
|
2007-08-01 02:52:26 +02:00
|
|
|
{
|
|
|
|
QString element;
|
|
|
|
if (svgElements & Private::SvgForeground) {
|
|
|
|
element = "foreground";
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
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;
|
2007-12-16 11:09:07 +01:00
|
|
|
default:
|
|
|
|
break;
|
2007-08-01 02:52:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!element.isEmpty()) {
|
|
|
|
svg.paint(painter, 0, 0, element);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-20 17:28:47 +01:00
|
|
|
QPixmap Icon::Private::decoration(const QStyleOptionGraphicsItem *option, bool useHoverEffect)
|
2007-08-05 19:11:46 +02:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
2007-12-20 17:28:47 +01:00
|
|
|
if (iconSvg) {
|
|
|
|
if (iconSvgPixmap.size() != iconSize.toSize()) {
|
|
|
|
QImage img(iconSize.toSize(), QImage::Format_ARGB32_Premultiplied);
|
|
|
|
{
|
|
|
|
QPainter p(&img);
|
2007-12-22 06:31:57 +01:00
|
|
|
iconSvg->paint(&p, img.rect(), iconSvgElement);
|
2007-12-20 17:28:47 +01:00
|
|
|
}
|
|
|
|
iconSvgPixmap = QPixmap::fromImage(img);
|
|
|
|
}
|
|
|
|
result = iconSvgPixmap;
|
|
|
|
} else {
|
|
|
|
const QSize size = icon.actualSize(iconSize.toSize(), mode, state);
|
|
|
|
result = icon.pixmap(size, mode, state);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!result.isNull() && useHoverEffect) {
|
2007-12-16 11:09:07 +01:00
|
|
|
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(KIconLoader::Desktop, KIconLoader::ActiveState)) {
|
|
|
|
result = effect->apply(result, KIconLoader::Desktop, KIconLoader::ActiveState);
|
2007-08-05 19:11:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
QPointF Icon::Private::iconPosition(const QStyleOptionGraphicsItem *option, const QPixmap &pixmap) const
|
|
|
|
{
|
|
|
|
const QRectF itemRect = subtractMargin(option->rect, Private::ItemMargin);
|
|
|
|
|
|
|
|
// Compute the nominal decoration rectangle
|
|
|
|
const QSizeF size = addMargin(iconSize, Private::IconMargin);
|
2007-12-28 12:29:56 +01:00
|
|
|
|
|
|
|
//alignment depends from orientation and option->direction
|
|
|
|
Qt::Alignment alignment;
|
|
|
|
if (text.isEmpty() && infoText.isEmpty()) {
|
|
|
|
alignment = Qt::AlignCenter;
|
|
|
|
}else if (orientation == Qt::Vertical) {
|
|
|
|
alignment = Qt::Alignment(Qt::AlignHCenter | Qt::AlignTop);
|
|
|
|
//Horizontal
|
|
|
|
}else{
|
|
|
|
alignment = QStyle::visualAlignment(option->direction, Qt::Alignment(Qt::AlignLeft | Qt::AlignVCenter));
|
|
|
|
}
|
|
|
|
|
2007-08-05 19:11:46 +02:00
|
|
|
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)
|
|
|
|
|
2007-11-30 02:06:27 +01:00
|
|
|
if (icon.isNull()) {
|
2007-08-05 19:11:46 +02:00
|
|
|
return option->rect;
|
2007-11-30 02:06:27 +01:00
|
|
|
}
|
2007-08-05 19:11:46 +02:00
|
|
|
|
|
|
|
const QSizeF decoSize = addMargin(iconSize, Private::IconMargin);
|
|
|
|
|
|
|
|
const QRectF itemRect = subtractMargin(option->rect, Private::ItemMargin);
|
|
|
|
QRectF textArea(QPointF(0, 0), itemRect.size());
|
|
|
|
|
2007-12-28 12:29:56 +01:00
|
|
|
|
|
|
|
if (orientation == Qt::Vertical) {
|
|
|
|
textArea.setTop(decoSize.height() + 1);
|
|
|
|
//Horizontal
|
|
|
|
}else{
|
|
|
|
textArea.setLeft(decoSize.width() + 1);
|
|
|
|
}
|
|
|
|
|
2007-08-05 19:11:46 +02:00
|
|
|
textArea.translate(itemRect.topLeft());
|
2007-12-28 12:29:56 +01:00
|
|
|
|
2007-08-05 19:11:46 +02:00
|
|
|
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;
|
|
|
|
|
|
|
|
// 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.
|
2007-12-26 14:44:57 +01:00
|
|
|
if (line.naturalTextWidth() < maxWidth && start+length > 0 && text[start + length - 1] == QChar::LineSeparator)
|
2007-08-05 19:11:46 +02:00
|
|
|
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
|
|
|
|
{
|
2007-12-23 11:51:00 +01:00
|
|
|
painter->setPen(textColor);
|
|
|
|
labelLayout.draw(painter, QPointF());
|
2007-08-05 19:11:46 +02:00
|
|
|
|
2007-12-23 11:51:00 +01:00
|
|
|
if (!infoLayout.text().isEmpty()) {
|
|
|
|
painter->setPen(textColor);
|
2007-08-05 19:11:46 +02:00
|
|
|
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-12-26 13:37:54 +01:00
|
|
|
//Lay out the main icon and action icons
|
|
|
|
layoutIcons(option);
|
2007-08-05 19:11:46 +02:00
|
|
|
|
|
|
|
// Compute the metrics, and lay out the text items
|
|
|
|
// ========================================================================
|
2007-12-16 11:09:07 +01:00
|
|
|
Private::IconState state = Private::NoState;
|
|
|
|
if (d->states & Private::ManualPressedState) {
|
|
|
|
state = Private::PressedState;
|
|
|
|
} else if (d->states & Private::PressedState) {
|
|
|
|
if (d->states & Private::HoverState) {
|
|
|
|
state = Private::PressedState;
|
|
|
|
}
|
|
|
|
} else if (d->states & Private::HoverState) {
|
|
|
|
state = Private::HoverState;
|
|
|
|
}
|
|
|
|
|
2007-12-18 17:56:34 +01:00
|
|
|
QPixmap icon = d->decoration(option, state != Private::NoState);
|
2007-08-05 19:11:46 +02:00
|
|
|
const QPointF iconPos = d->iconPosition(option, icon);
|
|
|
|
|
|
|
|
QTextLayout labelLayout, infoLayout;
|
|
|
|
QRectF textBoundingRect;
|
|
|
|
d->layoutTextItems(option, icon, &labelLayout, &infoLayout, &textBoundingRect);
|
|
|
|
|
2007-12-23 11:51:00 +01:00
|
|
|
//round to the bottom integer; otherwise a pixel could be chopped
|
|
|
|
d->svg.resize(QSize((int)size().width(), (int)size().height()));
|
2007-12-16 11:09:07 +01:00
|
|
|
d->drawBackground(painter, state);
|
2007-08-05 19:11:46 +02:00
|
|
|
|
|
|
|
// draw icon
|
|
|
|
painter->drawPixmap(iconPos, icon);
|
2007-05-29 09:59:50 +02:00
|
|
|
|
2007-12-16 11:09:07 +01:00
|
|
|
d->drawForeground(painter, state);
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Icon::text() const
|
|
|
|
{
|
|
|
|
return d->text;
|
|
|
|
}
|
|
|
|
|
2007-07-28 21:27:56 +02:00
|
|
|
void Icon::setInfoText(const QString& text)
|
|
|
|
{
|
|
|
|
d->infoText = text;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-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-05-29 09:59:50 +02:00
|
|
|
bool Icon::isDown()
|
|
|
|
{
|
2007-12-16 11:09:07 +01:00
|
|
|
return d->states & Private::PressedState;
|
2007-05-29 09:59:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Icon::mousePressEvent(QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
2007-10-30 21:59:12 +01:00
|
|
|
if (event->button() != Qt::LeftButton) {
|
|
|
|
Widget::mousePressEvent(event);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-12-16 11:09:07 +01:00
|
|
|
d->states |= Private::PressedState;
|
|
|
|
|
|
|
|
bool handled = false;
|
2007-07-21 06:07:41 +02:00
|
|
|
foreach (IconAction *action, d->cornerActions) {
|
2007-12-16 11:09:07 +01:00
|
|
|
handled = action->event(event->type(), event->pos());
|
|
|
|
if (handled) {
|
|
|
|
break;
|
|
|
|
}
|
2007-07-21 06:07:41 +02:00
|
|
|
}
|
2007-07-21 00:30:33 +02:00
|
|
|
|
2007-12-16 11:09:07 +01:00
|
|
|
if (!handled) {
|
|
|
|
emit pressed(true);
|
|
|
|
}
|
2007-08-05 19:11:46 +02:00
|
|
|
|
2007-07-28 21:27:56 +02:00
|
|
|
update();
|
2007-05-29 09:59:50 +02:00
|
|
|
}
|
|
|
|
|
2007-12-16 11:09:07 +01:00
|
|
|
void Icon::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
|
2007-05-29 09:59:50 +02:00
|
|
|
{
|
2007-12-16 11:09:07 +01:00
|
|
|
if (~d->states & Private::PressedState) {
|
|
|
|
Widget::mouseMoveEvent(event);
|
|
|
|
return;
|
|
|
|
}
|
2007-06-17 22:56:03 +02:00
|
|
|
|
2007-12-16 11:09:07 +01:00
|
|
|
if (boundingRect().contains(event->pos())) {
|
|
|
|
if (~d->states & Private::HoverState) {
|
|
|
|
d->states |= Private::HoverState;
|
|
|
|
update();
|
2007-07-21 06:07:41 +02:00
|
|
|
}
|
2007-10-22 18:16:30 +02:00
|
|
|
} else {
|
2007-12-16 11:09:07 +01:00
|
|
|
if (d->states & Private::HoverState) {
|
|
|
|
d->states &= ~Private::HoverState;
|
|
|
|
update();
|
|
|
|
}
|
2007-07-21 06:07:41 +02:00
|
|
|
}
|
2007-12-16 11:09:07 +01:00
|
|
|
}
|
2007-10-22 18:16:30 +02:00
|
|
|
|
2007-12-16 11:09:07 +01:00
|
|
|
void Icon::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
|
|
|
if (~d->states & Private::PressedState) {
|
|
|
|
Widget::mouseMoveEvent(event);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
d->states &= ~Private::PressedState;
|
|
|
|
|
|
|
|
bool handled = false;
|
|
|
|
foreach (IconAction *action, d->cornerActions) {
|
|
|
|
if (action->event(event->type(), event->pos())) {
|
|
|
|
handled = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-07-28 10:22:44 +02:00
|
|
|
|
2007-12-16 11:09:07 +01:00
|
|
|
if (!handled) {
|
|
|
|
if (boundingRect().contains(event->pos())) {
|
2007-07-28 10:22:44 +02:00
|
|
|
emit clicked();
|
|
|
|
}
|
2007-12-16 11:09:07 +01:00
|
|
|
emit pressed(false);
|
2007-08-05 19:11:46 +02:00
|
|
|
}
|
2007-06-17 22:56:03 +02:00
|
|
|
|
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-12-16 11:09:07 +01:00
|
|
|
d->states |= Private::HoverState;
|
|
|
|
update();
|
|
|
|
|
2007-10-24 08:52:45 +02:00
|
|
|
Widget::hoverEnterEvent(event);
|
2007-05-29 22:37:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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-12-16 11:09:07 +01:00
|
|
|
d->states &= ~Private::HoverState;
|
|
|
|
update();
|
|
|
|
|
2007-10-24 08:52:45 +02:00
|
|
|
Widget::hoverLeaveEvent(event);
|
2007-05-29 09:59:50 +02:00
|
|
|
}
|
|
|
|
|
2007-12-16 11:09:07 +01:00
|
|
|
void Icon::setPressed(bool pressed)
|
2007-06-17 22:56:03 +02:00
|
|
|
{
|
2007-12-16 11:09:07 +01:00
|
|
|
if (pressed) {
|
|
|
|
d->states |= Private::ManualPressedState;
|
|
|
|
} else {
|
|
|
|
d->states &= ~Private::ManualPressedState;
|
2007-06-17 22:56:03 +02:00
|
|
|
}
|
2007-12-16 11:09:07 +01:00
|
|
|
update();
|
|
|
|
}
|
2007-06-17 22:56:03 +02:00
|
|
|
|
2007-12-16 11:09:07 +01:00
|
|
|
void Icon::setUnpressed()
|
|
|
|
{
|
|
|
|
setPressed(false);
|
2007-06-17 22:56:03 +02:00
|
|
|
}
|
|
|
|
|
2007-12-28 12:29:56 +01:00
|
|
|
void Icon::setOrientation(Qt::Orientation orientation)
|
2007-11-10 16:16:33 +01:00
|
|
|
{
|
2007-12-28 12:29:56 +01:00
|
|
|
d->orientation = orientation;
|
2007-11-10 16:16:33 +01:00
|
|
|
}
|
|
|
|
|
2007-12-23 11:51:00 +01:00
|
|
|
QSizeF Icon::sizeFromIconSize(const qreal iconWidth) const
|
|
|
|
{
|
|
|
|
//no text, less calculations
|
|
|
|
if (d->text.isEmpty() && d->infoText.isEmpty()) {
|
|
|
|
return d->addMargin(d->addMargin(QSizeF(iconWidth, iconWidth),
|
|
|
|
Private::IconMargin),
|
|
|
|
Private::ItemMargin);
|
|
|
|
}
|
|
|
|
|
|
|
|
QFontMetricsF fm(font());
|
|
|
|
//make room for at most 14 characters
|
|
|
|
qreal width = qMax(fm.width(d->text.left(12)),
|
|
|
|
fm.width(d->infoText.left(12))) +
|
|
|
|
fm.width("xx") +
|
|
|
|
d->horizontalMargin[Private::TextMargin].left +
|
|
|
|
d->horizontalMargin[Private::TextMargin].right;
|
|
|
|
|
2007-12-28 12:29:56 +01:00
|
|
|
if (d->orientation == Qt::Vertical) {
|
|
|
|
width = qMax(width,
|
|
|
|
iconWidth +
|
|
|
|
d->horizontalMargin[Private::IconMargin].left +
|
|
|
|
d->horizontalMargin[Private::IconMargin].right);
|
|
|
|
}
|
2007-12-23 11:51:00 +01:00
|
|
|
|
|
|
|
qreal height;
|
|
|
|
qreal textHeight;
|
|
|
|
|
|
|
|
QStyleOptionGraphicsItem option;
|
|
|
|
option.state = QStyle::State_None;
|
|
|
|
option.rect = boundingRect().toRect();
|
|
|
|
textHeight = d->displaySizeHint(&option, width).height();
|
|
|
|
|
2007-12-28 12:29:56 +01:00
|
|
|
if (d->orientation == Qt::Vertical) {
|
|
|
|
height = iconWidth + textHeight +
|
|
|
|
d->verticalMargin[Private::TextMargin].top +
|
|
|
|
d->verticalMargin[Private::TextMargin].bottom +
|
|
|
|
d->verticalMargin[Private::IconMargin].top +
|
|
|
|
d->verticalMargin[Private::IconMargin].bottom;
|
|
|
|
//Horizontal
|
|
|
|
}else{
|
|
|
|
height = qMax(iconWidth +
|
|
|
|
d->verticalMargin[Private::IconMargin].top +
|
|
|
|
d->verticalMargin[Private::IconMargin].bottom,
|
|
|
|
textHeight +
|
|
|
|
d->verticalMargin[Private::TextMargin].top +
|
|
|
|
d->verticalMargin[Private::IconMargin].bottom);
|
|
|
|
width = width + iconWidth +
|
|
|
|
d->horizontalMargin[Private::IconMargin].left +
|
|
|
|
d->horizontalMargin[Private::IconMargin].right;
|
|
|
|
}
|
2007-12-23 11:51:00 +01:00
|
|
|
|
|
|
|
return d->addMargin(QSizeF(width, height), Private::ItemMargin);
|
|
|
|
}
|
|
|
|
|
2007-05-29 09:59:50 +02:00
|
|
|
} // namespace Plasma
|
|
|
|
|
|
|
|
#include "icon.moc"
|