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>
|
2008-04-14 16:09:13 +02:00
|
|
|
* Copyright 2008 by Alexis Ménard <darktears31@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
|
|
|
|
2008-01-11 21:30:25 +01:00
|
|
|
#include <KGlobalSettings>
|
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>
|
|
|
|
|
2008-07-04 01:50:13 +02:00
|
|
|
#include <plasma/paintutils.h>
|
2008-07-03 22:43:00 +02:00
|
|
|
#include <plasma/theme.h>
|
2007-05-29 09:59:50 +02:00
|
|
|
|
2008-04-25 05:23:31 +02:00
|
|
|
#include "animator.h"
|
2007-05-29 09:59:50 +02:00
|
|
|
#include "svg.h"
|
|
|
|
|
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
2008-07-01 20:56:43 +02:00
|
|
|
IconPrivate::IconPrivate(Icon *i)
|
2008-05-24 03:48:36 +02:00
|
|
|
: q(i),
|
|
|
|
iconSvg(0),
|
2008-08-19 23:34:23 +02:00
|
|
|
m_fadeIn(false),
|
|
|
|
m_hoverAnimId(-1),
|
2008-10-12 03:31:54 +02:00
|
|
|
m_hoverAlpha(20 / 255),
|
2007-08-08 07:29:50 +02:00
|
|
|
iconSize(48, 48),
|
2008-07-01 20:56:43 +02:00
|
|
|
states(IconPrivate::NoState),
|
2008-01-01 21:58:36 +01:00
|
|
|
orientation(Qt::Vertical),
|
2008-03-02 13:30:29 +01:00
|
|
|
numDisplayLines(2),
|
2008-01-03 22:22:24 +01:00
|
|
|
invertLayout(false),
|
2008-05-24 03:48:36 +02:00
|
|
|
drawBg(false),
|
2008-07-30 02:11:41 +02:00
|
|
|
action(0),
|
|
|
|
activeMargins(0)
|
2007-05-29 09:59:50 +02:00
|
|
|
{
|
2007-08-05 19:11:46 +02:00
|
|
|
}
|
2007-07-21 06:07:41 +02:00
|
|
|
|
2008-07-01 20:56:43 +02:00
|
|
|
IconPrivate::~IconPrivate()
|
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
|
|
|
|
2008-10-12 03:31:54 +02:00
|
|
|
void Icon::readColors()
|
2008-02-27 01:50:07 +01:00
|
|
|
{
|
2008-04-24 00:36:00 +02:00
|
|
|
d->textColor = Plasma::Theme::defaultTheme()->color(Theme::TextColor);
|
|
|
|
d->shadowColor = Plasma::Theme::defaultTheme()->color(Theme::BackgroundColor);
|
2008-02-27 01:50:07 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-10-12 03:31:54 +02:00
|
|
|
IconAction::IconAction(Icon *icon, QAction *action)
|
2007-07-21 06:07:41 +02:00
|
|
|
: m_icon(icon),
|
|
|
|
m_action(action),
|
|
|
|
m_hovered(false),
|
|
|
|
m_pressed(false),
|
2008-08-19 23:34:23 +02:00
|
|
|
m_selected(false),
|
2008-08-20 01:10:50 +02:00
|
|
|
m_visible(false),
|
2007-07-21 06:07:41 +02:00
|
|
|
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) {
|
2008-04-25 05:11:59 +02:00
|
|
|
Animator::self()->stopElementAnimation(m_animationId);
|
2007-07-21 06:07:41 +02:00
|
|
|
}
|
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
|
|
|
|
2008-04-25 05:11:59 +02:00
|
|
|
m_animationId = Animator::self()->animateElement(m_icon, Animator::AppearAnimation);
|
|
|
|
Animator::self()->setInitialPixmap(m_animationId, m_pixmap);
|
2007-07-21 06:07:41 +02:00
|
|
|
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) {
|
2008-04-25 05:11:59 +02:00
|
|
|
Animator::self()->stopElementAnimation(m_animationId);
|
2007-07-21 06:07:41 +02:00
|
|
|
}
|
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
|
|
|
|
2008-04-25 05:11:59 +02:00
|
|
|
m_animationId = Animator::self()->animateElement(m_icon, Animator::DisappearAnimation);
|
|
|
|
Animator::self()->setInitialPixmap(m_animationId, m_pixmap);
|
2007-07-21 06:07:41 +02:00
|
|
|
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);
|
|
|
|
|
2008-07-01 20:56:43 +02:00
|
|
|
int element = IconPrivate::Minibutton;
|
2007-07-21 06:07:41 +02:00
|
|
|
if (m_pressed) {
|
2008-07-01 20:56:43 +02:00
|
|
|
element = IconPrivate::MinibuttonPressed;
|
2007-07-21 06:07:41 +02:00
|
|
|
} else if (m_hovered) {
|
2008-07-01 20:56:43 +02:00
|
|
|
element = IconPrivate::MinibuttonHover;
|
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)
|
|
|
|
{
|
2008-10-12 03:31:54 +02:00
|
|
|
if (m_icon->size().width() < m_rect.width() * 2.0 ||
|
|
|
|
m_icon->size().height() < m_rect.height() * 2.0) {
|
2008-01-20 21:07:22 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-07-21 06:07:41 +02:00
|
|
|
switch (type) {
|
2008-10-12 03:31:54 +02:00
|
|
|
case QEvent::GraphicsSceneMousePress:
|
|
|
|
{
|
2007-07-21 06:07:41 +02:00
|
|
|
setSelected(m_rect.contains(pos));
|
|
|
|
return isSelected();
|
2008-10-12 03:31:54 +02:00
|
|
|
}
|
|
|
|
break;
|
2007-07-21 00:30:33 +02:00
|
|
|
|
2008-10-12 03:31:54 +02: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;
|
2008-10-12 03:31:54 +02:00
|
|
|
}
|
|
|
|
break;
|
2007-07-21 00:30:33 +02:00
|
|
|
|
2008-10-12 03:31:54 +02:00
|
|
|
case QEvent::GraphicsSceneMouseRelease:
|
|
|
|
{
|
2008-01-08 02:25:09 +01: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;
|
2008-10-12 03:31:54 +02:00
|
|
|
}
|
|
|
|
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:
|
2008-10-12 03:31:54 +02:00
|
|
|
break;
|
2007-07-21 00:30:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-04-24 23:26:36 +02:00
|
|
|
int IconAction::animationId() const
|
2007-07-21 00:30:33 +02:00
|
|
|
{
|
2007-07-21 06:07:41 +02:00
|
|
|
return m_animationId;
|
2007-07-21 00:30:33 +02:00
|
|
|
}
|
|
|
|
|
2008-10-12 03:31:54 +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
|
|
|
|
{
|
2008-10-12 03:31:54 +02:00
|
|
|
if (m_icon->size().width() < m_rect.width() * 2.0 ||
|
|
|
|
m_icon->size().height() < m_rect.height() * 2.0) {
|
2008-01-20 21:07:22 +01:00
|
|
|
return;
|
|
|
|
}
|
2008-02-15 12:39:10 +01:00
|
|
|
|
2008-04-25 05:11:59 +02:00
|
|
|
QPixmap animPixmap = Animator::self()->currentPixmap(m_animationId);
|
2008-02-15 12:39:10 +01:00
|
|
|
|
|
|
|
if (m_visible && animPixmap.isNull()) {
|
|
|
|
painter->drawPixmap(m_rect.toRect(), m_pixmap);
|
|
|
|
} else {
|
|
|
|
painter->drawPixmap(m_rect.toRect(), animPixmap);
|
|
|
|
}
|
2007-07-21 06:07:41 +02:00
|
|
|
}
|
2007-07-21 00:30:33 +02:00
|
|
|
|
2007-05-29 09:59:50 +02:00
|
|
|
Icon::Icon(QGraphicsItem *parent)
|
2008-04-13 23:47:47 +02:00
|
|
|
: QGraphicsWidget(parent),
|
2008-07-01 20:56:43 +02:00
|
|
|
d(new IconPrivate(this))
|
2007-05-29 09:59:50 +02:00
|
|
|
{
|
2007-07-20 08:28:51 +02:00
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
Icon::Icon(const QString &text, QGraphicsItem *parent)
|
2008-04-13 23:47:47 +02:00
|
|
|
: QGraphicsWidget(parent),
|
2008-07-01 20:56:43 +02:00
|
|
|
d(new IconPrivate(this))
|
2007-07-20 08:28:51 +02:00
|
|
|
{
|
|
|
|
init();
|
2008-07-30 02:30:03 +02:00
|
|
|
setText(text);
|
2007-07-20 08:28:51 +02:00
|
|
|
}
|
|
|
|
|
2007-08-11 00:24:49 +02:00
|
|
|
Icon::Icon(const QIcon &icon, const QString &text, QGraphicsItem *parent)
|
2008-04-13 23:47:47 +02:00
|
|
|
: QGraphicsWidget(parent),
|
2008-07-01 20:56:43 +02:00
|
|
|
d(new IconPrivate(this))
|
2007-07-20 08:28:51 +02:00
|
|
|
{
|
2008-07-29 05:02:04 +02:00
|
|
|
init();
|
2007-07-20 08:28:51 +02:00
|
|
|
setText(text);
|
|
|
|
setIcon(icon);
|
2007-05-29 09:59:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Icon::~Icon()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
2007-07-20 08:28:51 +02:00
|
|
|
void Icon::init()
|
|
|
|
{
|
2008-02-27 01:50:07 +01:00
|
|
|
readColors();
|
2008-04-23 20:35:03 +02:00
|
|
|
connect(Plasma::Theme::defaultTheme(), SIGNAL(themeChanged()), SLOT(readColors()));
|
2008-02-27 01:50:07 +01:00
|
|
|
|
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)
|
2008-07-01 20:56:43 +02:00
|
|
|
d->setHorizontalMargin(IconPrivate::TextMargin, focusHMargin, focusVMargin);
|
|
|
|
d->setHorizontalMargin(IconPrivate::IconMargin, focusHMargin, focusVMargin);
|
|
|
|
d->setHorizontalMargin(IconPrivate::ItemMargin, 0, 0);
|
2007-08-05 19:11:46 +02:00
|
|
|
|
|
|
|
// Margins for vertical mode (icon views)
|
2008-07-01 20:56:43 +02:00
|
|
|
d->setVerticalMargin(IconPrivate::TextMargin, 6, 2);
|
|
|
|
d->setVerticalMargin(IconPrivate::IconMargin, focusHMargin, focusVMargin);
|
|
|
|
d->setVerticalMargin(IconPrivate::ItemMargin, 0, 0);
|
2007-08-08 07:29:50 +02:00
|
|
|
|
2007-12-26 13:37:54 +01:00
|
|
|
d->setActiveMargins();
|
2008-01-07 06:09:09 +01:00
|
|
|
d->currentSize = QSizeF(-1, -1);
|
2008-02-27 01:50:07 +01:00
|
|
|
//setDrawStandardBackground(false);
|
2007-07-21 00:30:33 +02:00
|
|
|
}
|
|
|
|
|
2008-05-24 03:48:36 +02:00
|
|
|
void Icon::addIconAction(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) {
|
2008-05-24 03:48:36 +02:00
|
|
|
kDebug() << "no more room for more actions!";
|
2007-07-21 00:30:33 +02:00
|
|
|
}
|
|
|
|
|
2008-10-12 03:31:54 +02:00
|
|
|
IconAction *iconAction = new IconAction(this, action);
|
2007-07-21 06:07:41 +02:00
|
|
|
d->cornerActions.append(iconAction);
|
|
|
|
connect(action, SIGNAL(destroyed(QObject*)), this, SLOT(actionDestroyed(QObject*)));
|
|
|
|
|
2008-07-01 20:56:43 +02:00
|
|
|
iconAction->setRect(d->actionRect((IconPrivate::ActionPosition)count));
|
2007-07-21 06:07:41 +02:00
|
|
|
}
|
2007-07-21 00:30:33 +02:00
|
|
|
|
2008-05-24 03:48:36 +02:00
|
|
|
void Icon::setAction(QAction *action)
|
|
|
|
{
|
|
|
|
if (d->action) {
|
|
|
|
disconnect(d->action, 0, this, 0);
|
|
|
|
disconnect(this, 0, d->action, 0);
|
|
|
|
}
|
|
|
|
d->action = action;
|
|
|
|
if (action) {
|
|
|
|
connect(action, SIGNAL(changed()), this, SLOT(syncToAction()));
|
|
|
|
connect(this, SIGNAL(clicked()), action, SLOT(trigger()));
|
|
|
|
d->syncToAction();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-12 03:31:54 +02:00
|
|
|
QAction *Icon::action() const
|
2008-05-24 03:48:36 +02:00
|
|
|
{
|
|
|
|
return d->action;
|
|
|
|
}
|
|
|
|
|
2008-10-12 03:31:54 +02:00
|
|
|
void Icon::actionDestroyed(QObject *action)
|
2007-07-21 06:07:41 +02:00
|
|
|
{
|
|
|
|
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;
|
2008-02-27 01:50:07 +01:00
|
|
|
} else {
|
2007-12-26 14:44:57 +01:00
|
|
|
d->numDisplayLines = numLines;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-03 22:22:24 +01:00
|
|
|
void Icon::setDrawBackground(bool draw)
|
|
|
|
{
|
|
|
|
if (d->drawBg != draw) {
|
|
|
|
d->drawBg = draw;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Icon::drawBackground() const
|
|
|
|
{
|
|
|
|
return d->drawBg;
|
|
|
|
}
|
|
|
|
|
2008-01-11 19:17:24 +01:00
|
|
|
QPainterPath Icon::shape() const
|
|
|
|
{
|
|
|
|
if (d->currentSize.width() < 1) {
|
|
|
|
return QGraphicsItem::shape();
|
|
|
|
}
|
|
|
|
|
2008-10-12 03:31:54 +02:00
|
|
|
return PaintUtils::roundedRectangle(
|
|
|
|
QRectF(QPointF(0.0, 0.0), d->currentSize).adjusted(-2, -2, 2, 2), 10.0);
|
2008-01-11 19:17:24 +01:00
|
|
|
}
|
|
|
|
|
2008-07-01 20:56:43 +02:00
|
|
|
QSizeF IconPrivate::displaySizeHint(const QStyleOptionGraphicsItem *option, const qreal width) const
|
2007-12-23 11:51:00 +01:00
|
|
|
{
|
|
|
|
if (text.isEmpty() && infoText.isEmpty()) {
|
2008-10-12 03:31:54 +02:00
|
|
|
return QSizeF(.0, .0);
|
2007-12-23 11:51:00 +01:00
|
|
|
}
|
|
|
|
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 -
|
2008-07-01 20:56:43 +02:00
|
|
|
horizontalMargin[IconPrivate::TextMargin].left -
|
|
|
|
horizontalMargin[IconPrivate::TextMargin].right;
|
2007-12-23 11:51:00 +01:00
|
|
|
|
|
|
|
//allow only five lines of text
|
2008-10-12 03:31:54 +02:00
|
|
|
const qreal maxHeight =
|
|
|
|
numDisplayLines * Plasma::Theme::defaultTheme()->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
|
2008-01-01 21:53:46 +01:00
|
|
|
if (!infoText.isEmpty()) {
|
|
|
|
label += QString(QChar::LineSeparator) + infoText;
|
|
|
|
}
|
2007-12-23 11:51:00 +01:00
|
|
|
|
|
|
|
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
|
|
|
{
|
2008-01-01 20:21:33 +01:00
|
|
|
if (size() == d->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
|
|
|
|
2008-01-01 20:21:33 +01:00
|
|
|
d->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()) {
|
2008-01-01 20:21:33 +01:00
|
|
|
heightAvail = d->currentSize.height() -
|
|
|
|
d->displaySizeHint(option, d->currentSize.width()).height() -
|
2008-07-01 20:56:43 +02:00
|
|
|
d->verticalMargin[IconPrivate::TextMargin].top -
|
|
|
|
d->verticalMargin[IconPrivate::TextMargin].bottom;
|
2007-12-28 12:29:56 +01:00
|
|
|
//never make a label higher than half the total height
|
2008-10-12 03:31:54 +02:00
|
|
|
heightAvail = qMax(heightAvail, d->currentSize.height() / 2);
|
2008-01-07 06:09:09 +01:00
|
|
|
} else {
|
2008-01-01 20:21:33 +01:00
|
|
|
heightAvail = d->currentSize.height();
|
2007-12-28 12:29:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//aspect ratio very "tall"
|
2008-01-01 20:21:33 +01:00
|
|
|
if (d->currentSize.width() < heightAvail) {
|
|
|
|
iconWidth = d->currentSize.width() -
|
2008-07-01 20:56:43 +02:00
|
|
|
d->horizontalMargin[IconPrivate::IconMargin].left -
|
|
|
|
d->horizontalMargin[IconPrivate::IconMargin].right;
|
2008-01-07 06:09:09 +01:00
|
|
|
} else {
|
2007-12-28 12:29:56 +01:00
|
|
|
iconWidth = heightAvail -
|
2008-07-01 20:56:43 +02:00
|
|
|
d->verticalMargin[IconPrivate::IconMargin].top -
|
|
|
|
d->verticalMargin[IconPrivate::IconMargin].bottom;
|
2007-12-28 12:29:56 +01:00
|
|
|
}
|
2008-01-07 06:09:09 +01:00
|
|
|
} else {
|
2008-03-03 20:35:24 +01:00
|
|
|
//Horizontal layout
|
2007-12-28 12:29:56 +01:00
|
|
|
QFontMetricsF fm(font());
|
|
|
|
|
|
|
|
//if there is text resize the icon in order to make room for the text
|
2008-03-03 20:35:24 +01:00
|
|
|
if (d->text.isEmpty() && d->infoText.isEmpty()) {
|
|
|
|
// with no text, we just take up the whole geometry
|
|
|
|
iconWidth = d->currentSize.width() -
|
2008-07-01 20:56:43 +02:00
|
|
|
d->horizontalMargin[IconPrivate::IconMargin].left -
|
|
|
|
d->horizontalMargin[IconPrivate::IconMargin].right;
|
2008-01-07 06:09:09 +01:00
|
|
|
} else {
|
2008-01-01 20:21:33 +01:00
|
|
|
iconWidth = d->currentSize.height() -
|
2008-07-01 20:56:43 +02:00
|
|
|
d->verticalMargin[IconPrivate::IconMargin].top -
|
|
|
|
d->verticalMargin[IconPrivate::IconMargin].bottom;
|
2007-12-28 12:29:56 +01:00
|
|
|
}
|
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;
|
2008-10-12 03:31:54 +02:00
|
|
|
foreach (IconAction *iconAction, d->cornerActions) {
|
2008-07-01 20:56:43 +02:00
|
|
|
iconAction->setRect(d->actionRect((IconPrivate::ActionPosition)count));
|
2007-07-21 06:19:00 +02:00
|
|
|
++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) {
|
2008-08-19 23:34:23 +02:00
|
|
|
d->iconSvg = new Plasma::Svg(this);
|
2007-12-20 17:28:47 +01:00
|
|
|
}
|
2007-12-22 06:31:57 +01:00
|
|
|
|
2008-04-23 15:07:41 +02:00
|
|
|
d->iconSvg->setImagePath(svgFilePath);
|
2008-09-03 01:28:59 +02:00
|
|
|
d->iconSvg->setContainsMultipleImages(!elementId.isNull());
|
2007-12-22 06:31:57 +01:00
|
|
|
d->iconSvgElement = elementId;
|
2007-12-19 21:32:31 +01:00
|
|
|
}
|
|
|
|
|
2008-02-27 01:50:07 +01:00
|
|
|
void Icon::hoverEffect(bool show)
|
|
|
|
{
|
2008-05-25 23:47:09 +02:00
|
|
|
if (show) {
|
2008-07-01 20:56:43 +02:00
|
|
|
d->states |= IconPrivate::HoverState;
|
2008-05-25 23:47:09 +02:00
|
|
|
}
|
|
|
|
|
2008-02-27 01:50:07 +01:00
|
|
|
d->m_fadeIn = show;
|
|
|
|
const int FadeInDuration = 150;
|
|
|
|
|
|
|
|
if (d->m_hoverAnimId != -1) {
|
2008-04-25 05:11:59 +02:00
|
|
|
Animator::self()->stopCustomAnimation(d->m_hoverAnimId);
|
2008-02-27 01:50:07 +01:00
|
|
|
}
|
2008-10-12 03:31:54 +02:00
|
|
|
d->m_hoverAnimId = Animator::self()->customAnimation(
|
|
|
|
40 / (1000 / FadeInDuration), FadeInDuration,
|
|
|
|
Animator::EaseOutCurve, this, "hoverAnimationUpdate");
|
2008-02-27 01:50:07 +01:00
|
|
|
}
|
|
|
|
|
2008-10-12 03:31:54 +02:00
|
|
|
void Icon::hoverAnimationUpdate(qreal progress)
|
2008-02-27 01:50:07 +01:00
|
|
|
{
|
|
|
|
if (d->m_fadeIn) {
|
|
|
|
d->m_hoverAlpha = progress;
|
|
|
|
} else {
|
|
|
|
// If we mouse leaves before the fade in is done, fade out from where we were,
|
|
|
|
// not from fully faded in
|
2008-10-16 06:27:20 +02:00
|
|
|
d->m_hoverAlpha = qMin(1 - progress, d->m_hoverAlpha);
|
2008-02-27 01:50:07 +01:00
|
|
|
}
|
2008-10-16 06:27:20 +02:00
|
|
|
|
|
|
|
if (qFuzzyCompare(qreal(1.0), progress)) {
|
2008-02-27 01:50:07 +01:00
|
|
|
d->m_hoverAnimId = -1;
|
2008-10-16 06:27:20 +02:00
|
|
|
|
|
|
|
if (!d->m_fadeIn) {
|
|
|
|
d->states &= ~IconPrivate::HoverState;
|
|
|
|
}
|
2008-02-27 01:50:07 +01:00
|
|
|
}
|
2008-10-16 06:27:20 +02:00
|
|
|
|
2008-02-27 01:50:07 +01:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2008-07-01 20:56:43 +02:00
|
|
|
void IconPrivate::drawBackground(QPainter *painter, IconState state)
|
2007-08-01 02:52:26 +02:00
|
|
|
{
|
2008-01-03 22:22:24 +01:00
|
|
|
if (!drawBg) {
|
|
|
|
return;
|
|
|
|
}
|
2007-08-01 02:52:26 +02:00
|
|
|
|
2008-01-03 22:22:24 +01:00
|
|
|
bool darkShadow = shadowColor.value() < 128;
|
|
|
|
QColor shadow = shadowColor;
|
2008-01-24 19:35:22 +01:00
|
|
|
QColor border = textColor;
|
2007-08-01 02:52:26 +02:00
|
|
|
|
2008-01-03 22:22:24 +01:00
|
|
|
switch (state) {
|
2008-07-01 20:56:43 +02:00
|
|
|
case IconPrivate::HoverState:
|
2008-10-12 03:31:54 +02:00
|
|
|
shadow.setHsv(
|
|
|
|
shadow.hue(),
|
|
|
|
shadow.saturation(),
|
|
|
|
shadow.value() + (int)(darkShadow ? 50 * m_hoverAlpha: -50 * m_hoverAlpha),
|
|
|
|
200 + (int)m_hoverAlpha * 55); // opacity
|
2007-08-01 02:52:26 +02:00
|
|
|
break;
|
2008-07-01 20:56:43 +02:00
|
|
|
case IconPrivate::PressedState:
|
2008-10-12 03:31:54 +02:00
|
|
|
shadow.setHsv(
|
|
|
|
shadow.hue(),
|
|
|
|
shadow.saturation(),
|
|
|
|
shadow.value() + (darkShadow ?
|
|
|
|
(int)(50 * m_hoverAlpha) : (int)(-50 * m_hoverAlpha)),
|
|
|
|
204); //80% opacity
|
2007-08-01 02:52:26 +02:00
|
|
|
break;
|
2007-12-16 11:09:07 +01:00
|
|
|
default:
|
|
|
|
break;
|
2007-08-01 02:52:26 +02:00
|
|
|
}
|
2008-10-12 03:31:54 +02:00
|
|
|
|
|
|
|
border.setAlphaF(0.3 * m_hoverAlpha);
|
|
|
|
shadow.setAlphaF(0.6 * m_hoverAlpha);
|
2008-03-03 20:35:24 +01:00
|
|
|
|
2008-01-03 19:38:11 +01:00
|
|
|
painter->save();
|
2008-02-03 17:56:53 +01:00
|
|
|
painter->translate(0.5, 0.5);
|
2008-01-03 19:38:11 +01:00
|
|
|
painter->setRenderHint(QPainter::Antialiasing);
|
|
|
|
painter->setBrush(shadow);
|
2008-02-03 17:56:53 +01:00
|
|
|
painter->setPen(QPen(border, 1));
|
2008-10-12 03:31:54 +02:00
|
|
|
painter->drawPath(
|
|
|
|
PaintUtils::roundedRectangle(
|
|
|
|
QRectF(QPointF(1, 1), QSize((int)currentSize.width() - 2,
|
|
|
|
(int)currentSize.height() - 2)),
|
|
|
|
5.0));
|
2008-01-03 19:38:11 +01:00
|
|
|
painter->restore();
|
2007-08-01 02:52:26 +02:00
|
|
|
}
|
|
|
|
|
2008-07-01 20:56:43 +02:00
|
|
|
QPixmap IconPrivate::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);
|
|
|
|
{
|
2007-12-30 22:00:38 +01:00
|
|
|
img.fill(0);
|
2007-12-20 17:28:47 +01:00
|
|
|
QPainter p(&img);
|
2007-12-30 22:00:38 +01:00
|
|
|
iconSvg->resize(iconSize);
|
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);
|
|
|
|
}
|
|
|
|
|
2008-02-27 01:50:07 +01:00
|
|
|
// We disable the iconeffect here since we cannot get it into sync with
|
|
|
|
// the fade animation. TODO: Enable it when animations are switched off
|
2008-09-30 18:15:00 +02:00
|
|
|
if (!result.isNull() && useHoverEffect) {
|
2007-12-16 11:09:07 +01:00
|
|
|
KIconEffect *effect = KIconLoader::global()->iconEffect();
|
|
|
|
// Note that in KIconLoader terminology, active = hover.
|
2008-02-27 01:50:07 +01:00
|
|
|
// We're assuming that the icon group is desktop/filemanager, since this
|
|
|
|
// is KFileItemDelegate.
|
2007-12-16 11:09:07 +01:00
|
|
|
if (effect->hasEffect(KIconLoader::Desktop, KIconLoader::ActiveState)) {
|
2008-09-30 18:15:00 +02:00
|
|
|
if (qFuzzyCompare(qreal(1.0), m_hoverAlpha)) {
|
|
|
|
result = effect->apply(result, KIconLoader::Desktop, KIconLoader::ActiveState);
|
|
|
|
} else {
|
2008-10-12 03:31:54 +02:00
|
|
|
result = PaintUtils::transition(
|
|
|
|
result,
|
|
|
|
effect->apply(result, KIconLoader::Desktop,
|
|
|
|
KIconLoader::ActiveState), m_hoverAlpha);
|
2008-09-30 18:15:00 +02:00
|
|
|
}
|
2007-08-05 19:11:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2008-10-12 03:31:54 +02:00
|
|
|
QPointF IconPrivate::iconPosition(const QStyleOptionGraphicsItem *option,
|
|
|
|
const QPixmap &pixmap) const
|
2007-08-05 19:11:46 +02:00
|
|
|
{
|
2008-07-01 20:56:43 +02:00
|
|
|
const QRectF itemRect = subtractMargin(option->rect, IconPrivate::ItemMargin);
|
2007-08-05 19:11:46 +02:00
|
|
|
|
|
|
|
// Compute the nominal decoration rectangle
|
2008-07-01 20:56:43 +02:00
|
|
|
const QSizeF size = addMargin(iconSize, IconPrivate::IconMargin);
|
2007-12-28 12:29:56 +01:00
|
|
|
|
2008-01-01 21:58:36 +01:00
|
|
|
Qt::LayoutDirection direction = iconDirection(option);
|
|
|
|
|
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;
|
2008-10-12 03:31:54 +02:00
|
|
|
} else if (orientation == Qt::Vertical) {
|
2007-12-28 12:29:56 +01:00
|
|
|
alignment = Qt::Alignment(Qt::AlignHCenter | Qt::AlignTop);
|
|
|
|
//Horizontal
|
2008-10-12 03:31:54 +02:00
|
|
|
} else {
|
|
|
|
alignment = QStyle::visualAlignment(
|
|
|
|
direction, Qt::Alignment(Qt::AlignLeft | Qt::AlignVCenter));
|
2007-12-28 12:29:56 +01:00
|
|
|
}
|
|
|
|
|
2008-10-12 03:31:54 +02:00
|
|
|
const QRect iconRect =
|
|
|
|
QStyle::alignedRect(direction, alignment, size.toSize(), itemRect.toRect());
|
2007-08-05 19:11:46 +02:00
|
|
|
|
|
|
|
// 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());
|
|
|
|
}
|
|
|
|
|
2008-10-12 03:31:54 +02:00
|
|
|
QRectF IconPrivate::labelRectangle(const QStyleOptionGraphicsItem *option,
|
|
|
|
const QPixmap &icon,
|
|
|
|
const QString &string) const
|
2007-08-05 19:11:46 +02:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2008-07-01 20:56:43 +02:00
|
|
|
const QSizeF decoSize = addMargin(iconSize, IconPrivate::IconMargin);
|
|
|
|
const QRectF itemRect = subtractMargin(option->rect, IconPrivate::ItemMargin);
|
2007-08-05 19:11:46 +02:00
|
|
|
QRectF textArea(QPointF(0, 0), itemRect.size());
|
|
|
|
|
2007-12-28 12:29:56 +01:00
|
|
|
if (orientation == Qt::Vertical) {
|
|
|
|
textArea.setTop(decoSize.height() + 1);
|
2008-03-03 20:35:24 +01:00
|
|
|
} else {
|
|
|
|
//Horizontal
|
2007-12-28 12:29:56 +01:00
|
|
|
textArea.setLeft(decoSize.width() + 1);
|
|
|
|
}
|
|
|
|
|
2007-08-05 19:11:46 +02:00
|
|
|
textArea.translate(itemRect.topLeft());
|
2008-01-01 21:58:36 +01:00
|
|
|
return QRectF(QStyle::visualRect(iconDirection(option), option->rect, textArea.toRect()));
|
2007-08-05 19:11:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lays the text out in a rectangle no larger than constraints, eliding it as necessary
|
2008-07-01 20:56:43 +02:00
|
|
|
QSizeF IconPrivate::layoutText(QTextLayout &layout, const QStyleOptionGraphicsItem *option,
|
2007-08-05 19:11:46 +02:00
|
|
|
const QString &text, const QSizeF &constraints) const
|
|
|
|
{
|
|
|
|
const QSizeF size = layoutText(layout, text, constraints.width());
|
|
|
|
|
2008-10-12 03:31:54 +02:00
|
|
|
if (size.width() > constraints.width() || size.height() > constraints.height()) {
|
2007-08-05 19:11:46 +02:00
|
|
|
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
|
2008-07-01 20:56:43 +02:00
|
|
|
QSizeF IconPrivate::layoutText(QTextLayout &layout, const QString &text, qreal maxWidth) const
|
2007-08-05 19:11:46 +02:00
|
|
|
{
|
|
|
|
QFontMetricsF metrics(layout.font());
|
|
|
|
qreal leading = metrics.leading();
|
|
|
|
qreal height = 0.0;
|
|
|
|
qreal widthUsed = 0.0;
|
|
|
|
QTextLine line;
|
|
|
|
|
|
|
|
layout.setText(text);
|
|
|
|
|
|
|
|
layout.beginLayout();
|
|
|
|
|
2008-10-12 03:31:54 +02:00
|
|
|
while ((line = layout.createLine()).isValid()) {
|
2007-08-05 19:11:46 +02:00
|
|
|
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.
|
2008-07-01 20:56:43 +02:00
|
|
|
QString IconPrivate::elidedText(QTextLayout &layout, const QStyleOptionGraphicsItem *option,
|
2007-08-05 19:11:46 +02:00
|
|
|
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());
|
|
|
|
|
2008-10-12 03:31:54 +02:00
|
|
|
for (int i = 0; i < layout.lineCount(); i++) {
|
2007-08-05 19:11:46 +02:00
|
|
|
QTextLine line = layout.lineAt(i);
|
|
|
|
int start = line.textStart();
|
|
|
|
int length = line.textLength();
|
|
|
|
|
|
|
|
height += metrics.leading();
|
2008-10-12 03:31:54 +02:00
|
|
|
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 &&
|
|
|
|
start + length > 0 &&
|
|
|
|
text[start + length - 1] == QChar::LineSeparator) {
|
2007-08-05 19:11:46 +02:00
|
|
|
elided += text.mid(start, length - 1);
|
2008-10-12 03:31:54 +02:00
|
|
|
} else {
|
2007-08-05 19:11:46 +02:00
|
|
|
elided += metrics.elidedText(text.mid(start), Qt::ElideRight, maxWidth);
|
2008-10-12 03:31:54 +02:00
|
|
|
}
|
2007-08-05 19:11:46 +02:00
|
|
|
break;
|
2008-10-12 03:31:54 +02:00
|
|
|
} else if (line.naturalTextWidth() > maxWidth) {
|
2007-08-05 19:11:46 +02:00
|
|
|
elided += metrics.elidedText(text.mid(start, length), Qt::ElideRight, maxWidth);
|
2008-10-12 03:31:54 +02:00
|
|
|
} else {
|
2007-08-05 19:11:46 +02:00
|
|
|
elided += text.mid(start, length);
|
2008-10-12 03:31:54 +02:00
|
|
|
}
|
2007-08-05 19:11:46 +02:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2008-07-01 20:56:43 +02:00
|
|
|
void IconPrivate::layoutTextItems(const QStyleOptionGraphicsItem *option,
|
2007-08-05 19:11:46 +02:00
|
|
|
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);
|
2008-07-01 20:56:43 +02:00
|
|
|
QRectF textRect = subtractMargin(textArea, IconPrivate::TextMargin);
|
2007-08-01 03:25:01 +02:00
|
|
|
|
2008-03-03 20:35:24 +01:00
|
|
|
//kDebug() << this << "text area" << textArea << "text rect" << textRect;
|
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
|
2008-10-12 03:31:54 +02:00
|
|
|
if (!infoText.isEmpty() && textRect.height() >= fm.lineSpacing() * 2) {
|
2007-08-05 19:11:46 +02:00
|
|
|
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
|
2008-02-27 01:50:07 +01:00
|
|
|
if (showInformation) {
|
2007-08-05 19:11:46 +02:00
|
|
|
infoSize = layoutText(*infoLayout, option, infoText, maxInfoSize);
|
2008-02-27 01:50:07 +01:00
|
|
|
} else {
|
2007-08-05 19:11:46 +02:00
|
|
|
infoSize = QSizeF(0, 0);
|
2008-02-27 01:50:07 +01:00
|
|
|
}
|
2007-08-05 19:11:46 +02:00
|
|
|
// Compute the bounding rect of the text
|
|
|
|
const Qt::Alignment alignment = labelLayout->textOption().alignment();
|
2008-10-12 03:31:54 +02:00
|
|
|
const QSizeF size(qMax(labelSize.width(), infoSize.width()),
|
|
|
|
labelSize.height() + infoSize.height());
|
|
|
|
*textBoundingRect =
|
|
|
|
QStyle::alignedRect(iconDirection(option), alignment, size.toSize(), textRect.toRect());
|
2007-08-05 19:11:46 +02:00
|
|
|
|
|
|
|
// 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()));
|
2008-03-03 20:35:24 +01:00
|
|
|
//kDebug() << "final position is" << labelLayout->position();
|
2007-08-01 03:25:01 +02:00
|
|
|
}
|
|
|
|
|
2008-07-01 20:56:43 +02:00
|
|
|
QBrush IconPrivate::foregroundBrush(const QStyleOptionGraphicsItem *option) const
|
2007-08-05 19:11:46 +02:00
|
|
|
{
|
|
|
|
const QPalette::ColorGroup group = option->state & QStyle::State_Enabled ?
|
|
|
|
QPalette::Normal : QPalette::Disabled;
|
|
|
|
|
|
|
|
// Always use the highlight color for selected items
|
2008-02-27 01:50:07 +01:00
|
|
|
if (option->state & QStyle::State_Selected) {
|
2007-08-05 19:11:46 +02:00
|
|
|
return option->palette.brush(group, QPalette::HighlightedText);
|
2008-02-27 01:50:07 +01:00
|
|
|
}
|
2007-08-05 19:11:46 +02:00
|
|
|
return option->palette.brush(group, QPalette::Text);
|
|
|
|
}
|
|
|
|
|
2008-07-01 20:56:43 +02:00
|
|
|
QBrush IconPrivate::backgroundBrush(const QStyleOptionGraphicsItem *option) const
|
2007-08-05 19:11:46 +02:00
|
|
|
{
|
|
|
|
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
|
2008-02-27 01:50:07 +01:00
|
|
|
if (option->state & QStyle::State_Selected) {
|
2007-08-05 19:11:46 +02:00
|
|
|
background = option->palette.brush(group, QPalette::Highlight);
|
2008-02-27 01:50:07 +01:00
|
|
|
}
|
2007-08-05 19:11:46 +02:00
|
|
|
return background;
|
|
|
|
}
|
|
|
|
|
2008-10-12 03:31:54 +02:00
|
|
|
void IconPrivate::drawTextItems(QPainter *painter,
|
|
|
|
const QStyleOptionGraphicsItem *option,
|
|
|
|
const QTextLayout &labelLayout,
|
|
|
|
const QTextLayout &infoLayout) const
|
2007-08-05 19:11:46 +02:00
|
|
|
{
|
2008-01-03 22:22:24 +01:00
|
|
|
Q_UNUSED(option)
|
|
|
|
|
2008-03-03 20:35:24 +01:00
|
|
|
painter->save();
|
2007-12-23 11:51:00 +01:00
|
|
|
painter->setPen(textColor);
|
2008-03-03 20:35:24 +01:00
|
|
|
|
|
|
|
// the translation prevents odd rounding errors in labelLayout.position()
|
|
|
|
// when applied to the canvas
|
|
|
|
painter->translate(0.5, 0.5);
|
|
|
|
|
2007-12-23 11:51:00 +01:00
|
|
|
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());
|
|
|
|
}
|
2008-03-03 20:35:24 +01:00
|
|
|
painter->restore();
|
2007-08-05 19:11:46 +02:00
|
|
|
}
|
|
|
|
|
2008-04-13 23:47:47 +02:00
|
|
|
void Icon::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
|
2007-05-29 09:59:50 +02:00
|
|
|
{
|
2008-10-12 03:31:54 +02:00
|
|
|
Q_UNUSED(widget);
|
2007-05-29 09:59:50 +02:00
|
|
|
|
2007-07-21 06:07:41 +02:00
|
|
|
#ifdef BACKINGSTORE_BLUR_HACK
|
2008-07-01 20:56:43 +02:00
|
|
|
if (d->state == IconPrivate::HoverState && scene()) {
|
2007-06-17 19:49:49 +02:00
|
|
|
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
|
|
|
|
// ========================================================================
|
2008-07-01 20:56:43 +02:00
|
|
|
IconPrivate::IconState state = IconPrivate::NoState;
|
|
|
|
if (d->states & IconPrivate::ManualPressedState) {
|
|
|
|
state = IconPrivate::PressedState;
|
|
|
|
} else if (d->states & IconPrivate::PressedState) {
|
|
|
|
if (d->states & IconPrivate::HoverState) {
|
|
|
|
state = IconPrivate::PressedState;
|
2007-12-16 11:09:07 +01:00
|
|
|
}
|
2008-07-01 20:56:43 +02:00
|
|
|
} else if (d->states & IconPrivate::HoverState) {
|
|
|
|
state = IconPrivate::HoverState;
|
2007-12-16 11:09:07 +01:00
|
|
|
}
|
|
|
|
|
2008-07-01 20:56:43 +02:00
|
|
|
QPixmap icon = d->decoration(option, state != IconPrivate::NoState);
|
2007-08-05 19:11:46 +02:00
|
|
|
const QPointF iconPos = d->iconPosition(option, icon);
|
|
|
|
|
2007-12-16 11:09:07 +01:00
|
|
|
d->drawBackground(painter, state);
|
2007-08-05 19:11:46 +02:00
|
|
|
|
|
|
|
// draw icon
|
2008-01-25 08:36:33 +01:00
|
|
|
if (!icon.isNull()) {
|
|
|
|
painter->drawPixmap(iconPos, icon);
|
|
|
|
}
|
2007-05-29 09:59:50 +02:00
|
|
|
|
2007-08-01 02:52:26 +02:00
|
|
|
// Draw corner actions
|
2008-03-03 20:35:24 +01:00
|
|
|
foreach (const IconAction *action, d->cornerActions) {
|
2007-07-21 06:07:41 +02:00
|
|
|
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
|
2008-03-03 20:35:24 +01:00
|
|
|
QTextLayout labelLayout, infoLayout;
|
|
|
|
QRectF textBoundingRect;
|
|
|
|
d->layoutTextItems(option, icon, &labelLayout, &infoLayout, &textBoundingRect);
|
2008-07-03 19:04:56 +02:00
|
|
|
|
2008-10-12 03:31:54 +02:00
|
|
|
QImage shadow(textBoundingRect.size().toSize() + QSize(4, 4),
|
|
|
|
QImage::Format_ARGB32_Premultiplied);
|
2008-07-03 19:04:56 +02:00
|
|
|
shadow.fill(Qt::transparent);
|
|
|
|
{
|
|
|
|
QPainter buffPainter(&shadow);
|
|
|
|
buffPainter.translate(-textBoundingRect.x(), -textBoundingRect.y());
|
|
|
|
d->drawTextItems(&buffPainter, option, labelLayout, infoLayout);
|
|
|
|
}
|
2008-07-03 21:22:49 +02:00
|
|
|
|
2008-10-12 03:31:54 +02:00
|
|
|
QPoint shadowOffset = QPoint(1, 2);
|
2008-07-03 21:22:49 +02:00
|
|
|
if (d->shadowColor.value() > 128) {
|
2008-10-12 03:31:54 +02:00
|
|
|
shadowOffset = QPoint(0, 1);
|
2008-07-03 21:22:49 +02:00
|
|
|
}
|
|
|
|
|
2008-07-04 01:50:13 +02:00
|
|
|
PaintUtils::shadowBlur(shadow, 2, d->shadowColor);
|
2008-10-12 03:31:54 +02:00
|
|
|
painter->drawImage(textBoundingRect.topLeft() + shadowOffset, shadow);
|
2007-08-05 19:11:46 +02:00
|
|
|
d->drawTextItems(painter, option, labelLayout, infoLayout);
|
2007-05-29 09:59:50 +02:00
|
|
|
}
|
|
|
|
|
2008-10-12 03:31:54 +02:00
|
|
|
void Icon::drawActionButtonBase(QPainter *painter, const QSize &size, int element)
|
2007-07-21 06:07:41 +02:00
|
|
|
{
|
2008-10-12 03:31:54 +02:00
|
|
|
qreal radius = size.width() / 2;
|
2008-01-03 19:38:11 +01:00
|
|
|
QRadialGradient gradient(radius, radius, radius, radius, radius);
|
|
|
|
int alpha;
|
2007-07-21 06:07:41 +02:00
|
|
|
|
2008-07-01 20:56:43 +02:00
|
|
|
if (element == IconPrivate::MinibuttonPressed) {
|
2008-01-03 19:38:11 +01:00
|
|
|
alpha = 255;
|
2008-07-01 20:56:43 +02:00
|
|
|
} else if (element == IconPrivate::MinibuttonHover) {
|
2008-01-03 19:38:11 +01:00
|
|
|
alpha = 200;
|
|
|
|
} else {
|
|
|
|
alpha = 160;
|
|
|
|
}
|
|
|
|
gradient.setColorAt(0, QColor::fromRgb(d->textColor.red(),
|
|
|
|
d->textColor.green(),
|
|
|
|
d->textColor.blue(), alpha));
|
|
|
|
gradient.setColorAt(1, QColor::fromRgb(d->textColor.red(),
|
|
|
|
d->textColor.green(),
|
|
|
|
d->textColor.blue(), 0));
|
|
|
|
|
|
|
|
painter->setBrush(gradient);
|
|
|
|
painter->setPen(Qt::NoPen);
|
|
|
|
painter->drawEllipse(QRectF(QPointF(.0, .0), size));
|
2007-07-21 06:07:41 +02:00
|
|
|
}
|
|
|
|
|
2008-10-12 03:31:54 +02:00
|
|
|
void Icon::setText(const QString &text)
|
2007-05-29 09:59:50 +02:00
|
|
|
{
|
|
|
|
d->text = text;
|
2008-01-07 06:09:09 +01:00
|
|
|
// cause a relayout
|
|
|
|
d->currentSize = QSizeF(-1, -1);
|
2008-07-31 22:50:15 +02:00
|
|
|
//try to relayout, needed if an icon was never shown before
|
|
|
|
if (!isVisible()) {
|
2008-08-12 01:04:07 +02:00
|
|
|
QStyleOptionGraphicsItem styleoption;
|
|
|
|
layoutIcons(&styleoption);
|
2008-07-31 22:50:15 +02:00
|
|
|
}
|
2008-07-29 02:28:59 +02:00
|
|
|
resize(sizeFromIconSize(d->iconSize.width()));
|
2007-05-29 09:59:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QString Icon::text() const
|
|
|
|
{
|
|
|
|
return d->text;
|
|
|
|
}
|
|
|
|
|
2008-10-12 03:31:54 +02:00
|
|
|
void Icon::setInfoText(const QString &text)
|
2007-07-28 21:27:56 +02:00
|
|
|
{
|
|
|
|
d->infoText = text;
|
2008-01-07 06:09:09 +01:00
|
|
|
// cause a relayout
|
|
|
|
d->currentSize = QSizeF(-1, -1);
|
2008-07-31 22:50:15 +02:00
|
|
|
//try to relayout, needed if an icon was never shown before
|
|
|
|
if (!isVisible()) {
|
|
|
|
layoutIcons(new QStyleOptionGraphicsItem);
|
|
|
|
}
|
2008-07-29 02:28:59 +02:00
|
|
|
resize(sizeFromIconSize(d->iconSize.width()));
|
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;
|
|
|
|
}
|
|
|
|
|
2008-10-12 03:31:54 +02:00
|
|
|
void Icon::setIcon(const QString &icon)
|
2007-05-29 09:59:50 +02:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2008-10-12 03:31:54 +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()
|
|
|
|
{
|
2008-07-01 20:56:43 +02:00
|
|
|
return d->states & IconPrivate::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) {
|
2008-04-13 23:47:47 +02:00
|
|
|
QGraphicsWidget::mousePressEvent(event);
|
2007-10-30 21:59:12 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-07-01 20:56:43 +02:00
|
|
|
d->states |= IconPrivate::PressedState;
|
2008-01-13 06:33:05 +01:00
|
|
|
d->clickStartPos = scenePos();
|
2007-12-16 11:09:07 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
2008-06-20 22:48:49 +02:00
|
|
|
if (!handled && geometry().contains(event->pos())) {
|
2007-12-16 11:09:07 +01:00
|
|
|
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
|
|
|
{
|
2008-07-01 20:56:43 +02:00
|
|
|
if (~d->states & IconPrivate::PressedState) {
|
2008-04-13 23:47:47 +02:00
|
|
|
QGraphicsWidget::mouseMoveEvent(event);
|
2007-12-16 11:09:07 +01:00
|
|
|
return;
|
|
|
|
}
|
2007-06-17 22:56:03 +02:00
|
|
|
|
2007-12-16 11:09:07 +01:00
|
|
|
if (boundingRect().contains(event->pos())) {
|
2008-07-01 20:56:43 +02:00
|
|
|
if (~d->states & IconPrivate::HoverState) {
|
|
|
|
d->states |= IconPrivate::HoverState;
|
2007-12-16 11:09:07 +01:00
|
|
|
update();
|
2007-07-21 06:07:41 +02:00
|
|
|
}
|
2007-10-22 18:16:30 +02:00
|
|
|
} else {
|
2008-07-01 20:56:43 +02:00
|
|
|
if (d->states & IconPrivate::HoverState) {
|
|
|
|
d->states &= ~IconPrivate::HoverState;
|
2007-12-16 11:09:07 +01:00
|
|
|
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)
|
|
|
|
{
|
2008-07-01 20:56:43 +02:00
|
|
|
if (~d->states & IconPrivate::PressedState) {
|
2008-04-13 23:47:47 +02:00
|
|
|
QGraphicsWidget::mouseMoveEvent(event);
|
2007-12-16 11:09:07 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-07-01 20:56:43 +02:00
|
|
|
d->states &= ~IconPrivate::PressedState;
|
2007-12-16 11:09:07 +01:00
|
|
|
|
2008-01-15 19:56:23 +01:00
|
|
|
//don't pass click when the mouse was moved
|
|
|
|
bool handled = d->clickStartPos != scenePos();
|
2008-01-15 19:51:43 +01:00
|
|
|
if (!handled) {
|
2008-01-15 19:56:23 +01:00
|
|
|
foreach (IconAction *action, d->cornerActions) {
|
|
|
|
if (action->event(event->type(), event->pos())) {
|
|
|
|
handled = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-01-15 19:51:43 +01:00
|
|
|
}
|
2008-01-13 06:33:05 +01: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();
|
2008-01-11 21:30:25 +01:00
|
|
|
if (KGlobalSettings::singleClick()) {
|
|
|
|
emit activated();
|
|
|
|
}
|
2007-07-28 10:22:44 +02:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2008-01-11 00:29:05 +01:00
|
|
|
void Icon::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
2008-01-23 00:31:57 +01:00
|
|
|
Q_UNUSED(event)
|
|
|
|
|
2008-01-11 00:29:05 +01:00
|
|
|
emit doubleClicked();
|
2008-01-11 21:30:25 +01:00
|
|
|
if (!KGlobalSettings::singleClick()) {
|
|
|
|
emit activated();
|
|
|
|
}
|
2008-01-11 00:29:05 +01: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
|
|
|
}
|
2008-02-27 01:50:07 +01:00
|
|
|
hoverEffect(true);
|
2007-12-16 11:09:07 +01:00
|
|
|
update();
|
|
|
|
|
2008-04-13 23:47:47 +02:00
|
|
|
QGraphicsWidget::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
|
|
|
}
|
2008-10-12 03:31:54 +02:00
|
|
|
// d->states &= ~IconPrivate::HoverState; // Will be set once progress is zero again ...
|
2008-02-27 01:50:07 +01:00
|
|
|
hoverEffect(false);
|
2007-12-16 11:09:07 +01:00
|
|
|
update();
|
|
|
|
|
2008-04-13 23:47:47 +02:00
|
|
|
QGraphicsWidget::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) {
|
2008-07-01 20:56:43 +02:00
|
|
|
d->states |= IconPrivate::ManualPressedState;
|
|
|
|
d->states |= IconPrivate::PressedState;
|
2007-12-16 11:09:07 +01:00
|
|
|
} else {
|
2008-07-01 20:56:43 +02:00
|
|
|
d->states &= ~IconPrivate::ManualPressedState;
|
|
|
|
d->states &= ~IconPrivate::PressedState;
|
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
|
|
|
}
|
|
|
|
|
2008-07-01 20:56:43 +02:00
|
|
|
void IconPrivate::syncToAction()
|
2008-05-24 03:48:36 +02:00
|
|
|
{
|
|
|
|
if (!action) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
//we don't get told *what* changed, just that something changed
|
|
|
|
//so we update everything we care about
|
|
|
|
q->setIcon(action->icon());
|
|
|
|
q->setText(action->iconText());
|
|
|
|
q->setEnabled(action->isEnabled());
|
|
|
|
//TODO use action's tooltip too
|
|
|
|
|
|
|
|
emit q->changed();
|
|
|
|
}
|
|
|
|
|
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;
|
2008-03-03 20:35:24 +01:00
|
|
|
resize(sizeFromIconSize(d->iconSize.width()));
|
2007-11-10 16:16:33 +01:00
|
|
|
}
|
|
|
|
|
2008-01-01 21:58:36 +01:00
|
|
|
void Icon::invertLayout(bool invert)
|
|
|
|
{
|
|
|
|
d->invertLayout = invert;
|
|
|
|
}
|
|
|
|
|
2008-01-02 11:39:29 +01:00
|
|
|
bool Icon::invertedLayout() const
|
|
|
|
{
|
|
|
|
return d->invertLayout;
|
|
|
|
}
|
|
|
|
|
2007-12-23 11:51:00 +01:00
|
|
|
QSizeF Icon::sizeFromIconSize(const qreal iconWidth) const
|
|
|
|
{
|
|
|
|
if (d->text.isEmpty() && d->infoText.isEmpty()) {
|
2008-03-03 20:35:24 +01:00
|
|
|
//no text, less calculations
|
2008-07-01 20:56:43 +02:00
|
|
|
return d->addMargin(d->addMargin(QSizeF(iconWidth, iconWidth), IconPrivate::IconMargin),
|
|
|
|
IconPrivate::ItemMargin);
|
2007-12-23 11:51:00 +01:00
|
|
|
}
|
|
|
|
|
2008-04-22 23:10:14 +02:00
|
|
|
QFontMetricsF fm = Plasma::Theme::defaultTheme()->fontMetrics();
|
2008-03-03 20:35:24 +01:00
|
|
|
qreal width = 0;
|
2007-12-23 11:51:00 +01:00
|
|
|
|
2007-12-28 12:29:56 +01:00
|
|
|
if (d->orientation == Qt::Vertical) {
|
2008-03-03 20:35:24 +01:00
|
|
|
// make room for at most 14 characters
|
|
|
|
width = qMax(fm.width(d->text.left(12)),
|
|
|
|
fm.width(d->infoText.left(12))) +
|
|
|
|
fm.width("xx") +
|
2008-07-01 20:56:43 +02:00
|
|
|
d->horizontalMargin[IconPrivate::TextMargin].left +
|
|
|
|
d->horizontalMargin[IconPrivate::TextMargin].right;
|
2008-03-03 20:35:24 +01:00
|
|
|
|
2007-12-28 12:29:56 +01:00
|
|
|
width = qMax(width,
|
|
|
|
iconWidth +
|
2008-07-01 20:56:43 +02:00
|
|
|
d->horizontalMargin[IconPrivate::IconMargin].left +
|
|
|
|
d->horizontalMargin[IconPrivate::IconMargin].right);
|
2008-03-03 20:35:24 +01:00
|
|
|
} else {
|
|
|
|
width = iconWidth +
|
2008-07-01 20:56:43 +02:00
|
|
|
d->horizontalMargin[IconPrivate::IconMargin].left +
|
|
|
|
d->horizontalMargin[IconPrivate::IconMargin].right +
|
2008-03-03 20:35:24 +01:00
|
|
|
qMax(fm.width(d->text), fm.width(d->infoText)) + fm.width("xx") +
|
2008-07-01 20:56:43 +02:00
|
|
|
d->horizontalMargin[IconPrivate::TextMargin].left +
|
|
|
|
d->horizontalMargin[IconPrivate::TextMargin].right;
|
2007-12-28 12:29:56 +01:00
|
|
|
}
|
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 +
|
2008-07-01 20:56:43 +02:00
|
|
|
d->verticalMargin[IconPrivate::TextMargin].top +
|
|
|
|
d->verticalMargin[IconPrivate::TextMargin].bottom +
|
|
|
|
d->verticalMargin[IconPrivate::IconMargin].top +
|
|
|
|
d->verticalMargin[IconPrivate::IconMargin].bottom;
|
2008-03-03 20:35:24 +01:00
|
|
|
} else {
|
|
|
|
//Horizontal
|
2007-12-28 12:29:56 +01:00
|
|
|
height = qMax(iconWidth +
|
2008-07-01 20:56:43 +02:00
|
|
|
d->verticalMargin[IconPrivate::IconMargin].top +
|
|
|
|
d->verticalMargin[IconPrivate::IconMargin].bottom,
|
2007-12-28 12:29:56 +01:00
|
|
|
textHeight +
|
2008-07-01 20:56:43 +02:00
|
|
|
d->verticalMargin[IconPrivate::TextMargin].top +
|
|
|
|
d->verticalMargin[IconPrivate::TextMargin].bottom);
|
2007-12-28 12:29:56 +01:00
|
|
|
}
|
2007-12-23 11:51:00 +01:00
|
|
|
|
2008-07-01 20:56:43 +02:00
|
|
|
return d->addMargin(QSizeF(width, height), IconPrivate::ItemMargin);
|
2007-12-23 11:51:00 +01:00
|
|
|
}
|
|
|
|
|
2007-05-29 09:59:50 +02:00
|
|
|
} // namespace Plasma
|
|
|
|
|
|
|
|
#include "icon.moc"
|