2008-11-04 00:08:39 +01: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>
|
|
|
|
* Copyright 2007 by Marco Martin <notmart@gmail.com>
|
|
|
|
* Copyright 2008 by Alexis Ménard <darktears31@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Library General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "iconwidget.h"
|
|
|
|
#include "iconwidget_p.h"
|
|
|
|
|
|
|
|
#include <QAction>
|
|
|
|
#include <QApplication>
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QGraphicsSceneMouseEvent>
|
|
|
|
#include <QGraphicsView>
|
|
|
|
#include <QStyleOptionGraphicsItem>
|
|
|
|
#include <QTextLayout>
|
|
|
|
|
2008-11-04 03:55:37 +01:00
|
|
|
#include <kglobalsettings.h>
|
|
|
|
#include <kiconeffect.h>
|
|
|
|
#include <kiconloader.h>
|
|
|
|
#include <kicon.h>
|
|
|
|
#include <kurl.h>
|
|
|
|
#include <krun.h>
|
|
|
|
#include <kmimetype.h>
|
|
|
|
#include <kdebug.h>
|
|
|
|
#include <kcolorscheme.h>
|
2008-11-04 00:08:39 +01:00
|
|
|
|
2010-02-05 22:46:51 +01:00
|
|
|
#include <plasma/animator.h>
|
|
|
|
#include <plasma/animations/animation.h>
|
2008-11-04 00:08:39 +01:00
|
|
|
#include <plasma/paintutils.h>
|
|
|
|
#include <plasma/theme.h>
|
|
|
|
|
|
|
|
#include "svg.h"
|
|
|
|
|
2009-01-06 22:11:54 +01:00
|
|
|
/*
|
|
|
|
TODO:
|
|
|
|
Add these to a UrlIcon class
|
|
|
|
void setUrl(const KUrl& url);
|
|
|
|
KUrl url() const;
|
|
|
|
*/
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
2010-02-05 22:46:51 +01:00
|
|
|
IconHoverAnimation::IconHoverAnimation(QObject *parent)
|
|
|
|
: QObject(parent), m_value(0), m_fadeIn(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
qreal IconHoverAnimation::value() const
|
|
|
|
{
|
|
|
|
return m_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IconHoverAnimation::fadeIn() const
|
|
|
|
{
|
|
|
|
return m_fadeIn;
|
|
|
|
}
|
|
|
|
|
|
|
|
QPropertyAnimation *IconHoverAnimation::animation() const
|
|
|
|
{
|
|
|
|
return m_animation.data();
|
|
|
|
}
|
|
|
|
|
|
|
|
void IconHoverAnimation::setValue(qreal value)
|
|
|
|
{
|
|
|
|
m_value = value;
|
|
|
|
|
|
|
|
QGraphicsItem *item = qobject_cast<QGraphicsItem*>(parent());
|
|
|
|
item->update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void IconHoverAnimation::setFadeIn(bool fadeIn)
|
|
|
|
{
|
|
|
|
m_fadeIn = fadeIn;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IconHoverAnimation::setAnimation(QPropertyAnimation *animation)
|
|
|
|
{
|
|
|
|
m_animation = animation;
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
IconWidgetPrivate::IconWidgetPrivate(IconWidget *i)
|
2009-04-24 09:40:52 +02:00
|
|
|
: ActionWidgetInterface<IconWidget>(i),
|
|
|
|
q(i),
|
2008-11-04 00:08:39 +01:00
|
|
|
iconSvg(0),
|
2010-02-05 22:46:51 +01:00
|
|
|
hoverAnimation(new IconHoverAnimation(q)),
|
2008-11-04 00:08:39 +01:00
|
|
|
iconSize(48, 48),
|
|
|
|
states(IconWidgetPrivate::NoState),
|
|
|
|
orientation(Qt::Vertical),
|
|
|
|
numDisplayLines(2),
|
2009-04-10 23:16:14 +02:00
|
|
|
activeMargins(0),
|
|
|
|
iconSvgElementChanged(false),
|
2008-11-04 00:08:39 +01:00
|
|
|
invertLayout(false),
|
|
|
|
drawBg(false),
|
2009-09-13 15:13:55 +02:00
|
|
|
textBgCustomized(false),
|
|
|
|
customFont(false)
|
2008-11-04 00:08:39 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
IconWidgetPrivate::~IconWidgetPrivate()
|
|
|
|
{
|
|
|
|
qDeleteAll(cornerActions);
|
2010-02-05 22:46:51 +01:00
|
|
|
delete hoverAnimation;
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
2009-01-06 22:11:54 +01:00
|
|
|
void IconWidgetPrivate::readColors()
|
2008-11-04 00:08:39 +01:00
|
|
|
{
|
2009-01-06 22:11:54 +01:00
|
|
|
textColor = Plasma::Theme::defaultTheme()->color(Theme::TextColor);
|
2009-04-10 23:16:14 +02:00
|
|
|
|
2009-03-05 17:24:12 +01:00
|
|
|
if (qGray(textColor.rgb()) > 192) {
|
|
|
|
shadowColor = Qt::black;
|
|
|
|
} else {
|
|
|
|
shadowColor = Qt::white;
|
|
|
|
}
|
2009-04-10 23:16:14 +02:00
|
|
|
|
|
|
|
if (!textBgCustomized) {
|
2009-11-26 22:57:36 +01:00
|
|
|
textBgColor = QColor();
|
2009-04-10 23:16:14 +02:00
|
|
|
}
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
2009-01-20 03:37:52 +01:00
|
|
|
void IconWidgetPrivate::colorConfigChanged()
|
|
|
|
{
|
|
|
|
readColors();
|
2009-09-09 19:03:22 +02:00
|
|
|
if (drawBg) {
|
|
|
|
qreal left, top, right, bottom;
|
|
|
|
background->getMargins(left, top, right, bottom);
|
|
|
|
setVerticalMargin(IconWidgetPrivate::ItemMargin, left, top, right, bottom);
|
|
|
|
setHorizontalMargin(IconWidgetPrivate::ItemMargin, left, top, right, bottom);
|
|
|
|
}
|
2009-01-20 03:37:52 +01:00
|
|
|
q->update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void IconWidgetPrivate::iconConfigChanged()
|
|
|
|
{
|
|
|
|
if (!icon.isNull()) {
|
|
|
|
q->update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-10 10:46:14 +01:00
|
|
|
QFont IconWidgetPrivate::widgetFont() const
|
|
|
|
{
|
|
|
|
if (customFont) {
|
|
|
|
return q->font();
|
|
|
|
} else {
|
|
|
|
return Plasma::Theme::defaultTheme()->font(Plasma::Theme::DefaultFont);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
IconAction::IconAction(IconWidget *icon, QAction *action)
|
|
|
|
: m_icon(icon),
|
|
|
|
m_action(action),
|
|
|
|
m_hovered(false),
|
|
|
|
m_pressed(false),
|
|
|
|
m_selected(false),
|
2010-02-05 22:46:51 +01:00
|
|
|
m_visible(false)
|
2008-11-04 00:08:39 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void IconAction::show()
|
|
|
|
{
|
2010-02-05 22:46:51 +01:00
|
|
|
Animation *animation = m_animation.data();
|
|
|
|
if (!animation) {
|
2010-02-08 20:27:12 +01:00
|
|
|
animation = Plasma::Animator::create(Plasma::Animator::PixmapTransitionAnimation, m_icon);
|
2010-02-05 22:46:51 +01:00
|
|
|
animation->setTargetWidget(m_icon);
|
2010-02-08 20:27:12 +01:00
|
|
|
m_animation = animation;
|
2010-02-05 22:46:51 +01:00
|
|
|
} else if (animation->state() == QAbstractAnimation::Running) {
|
|
|
|
animation->pause();
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
rebuildPixmap();
|
2010-02-08 20:27:12 +01:00
|
|
|
m_visible = true;
|
2008-11-04 00:08:39 +01:00
|
|
|
|
2010-02-08 20:27:12 +01:00
|
|
|
animation->setProperty("targetPixmap", m_pixmap);
|
2010-02-05 22:46:51 +01:00
|
|
|
animation->setDirection(QAbstractAnimation::Forward);
|
2010-02-08 20:27:12 +01:00
|
|
|
animation->start();
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void IconAction::hide()
|
|
|
|
{
|
2010-02-08 21:47:29 +01:00
|
|
|
if (!m_animation) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-02-05 22:46:51 +01:00
|
|
|
Animation *animation = m_animation.data();
|
2010-02-08 20:27:12 +01:00
|
|
|
if (animation->state() == QAbstractAnimation::Running) {
|
2010-02-05 22:46:51 +01:00
|
|
|
animation->pause();
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
2010-02-08 20:27:12 +01:00
|
|
|
m_visible = false;
|
2008-11-04 00:08:39 +01:00
|
|
|
|
2010-02-05 22:46:51 +01:00
|
|
|
animation->setDirection(QAbstractAnimation::Backward);
|
|
|
|
animation->start(QAbstractAnimation::DeleteWhenStopped);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IconAction::isVisible() const
|
|
|
|
{
|
|
|
|
return m_visible;
|
|
|
|
}
|
|
|
|
|
2010-02-05 22:46:51 +01:00
|
|
|
bool IconAction::isAnimating() const
|
|
|
|
{
|
2010-02-08 20:27:12 +01:00
|
|
|
return !m_animation.isNull();
|
2010-02-05 22:46:51 +01:00
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
bool IconAction::isPressed() const
|
|
|
|
{
|
|
|
|
return m_pressed;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IconAction::isHovered() const
|
|
|
|
{
|
|
|
|
return m_hovered;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IconAction::setSelected(bool selected)
|
|
|
|
{
|
|
|
|
m_selected = selected;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IconAction::isSelected() const
|
|
|
|
{
|
|
|
|
return m_selected;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IconAction::setRect(const QRectF &rect)
|
|
|
|
{
|
|
|
|
m_rect = rect;
|
|
|
|
}
|
|
|
|
|
|
|
|
QRectF IconAction::rect() const
|
|
|
|
{
|
|
|
|
return m_rect;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IconAction::rebuildPixmap()
|
|
|
|
{
|
|
|
|
// Determine proper QIcon mode based on selection status
|
2010-02-08 20:27:12 +01:00
|
|
|
QIcon::Mode mode = m_selected ? QIcon::Selected : QIcon::Normal;
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
// Draw everything
|
|
|
|
m_pixmap = QPixmap(26, 26);
|
|
|
|
m_pixmap.fill(Qt::transparent);
|
|
|
|
|
|
|
|
int element = IconWidgetPrivate::Minibutton;
|
|
|
|
if (m_pressed) {
|
|
|
|
element = IconWidgetPrivate::MinibuttonPressed;
|
|
|
|
} else if (m_hovered) {
|
|
|
|
element = IconWidgetPrivate::MinibuttonHover;
|
|
|
|
}
|
|
|
|
|
|
|
|
QPainter painter(&m_pixmap);
|
|
|
|
m_icon->drawActionButtonBase(&painter, m_pixmap.size(), element);
|
|
|
|
m_action->icon().paint(&painter, 2, 2, 22, 22, Qt::AlignCenter, mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IconAction::event(QEvent::Type type, const QPointF &pos)
|
|
|
|
{
|
2010-01-10 20:34:33 +01:00
|
|
|
if (!m_action->isVisible() || !m_action->isEnabled()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
if (m_icon->size().width() < m_rect.width() * 2.0 ||
|
|
|
|
m_icon->size().height() < m_rect.height() * 2.0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case QEvent::GraphicsSceneMousePress:
|
|
|
|
{
|
|
|
|
setSelected(m_rect.contains(pos));
|
|
|
|
return isSelected();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QEvent::GraphicsSceneMouseMove:
|
|
|
|
{
|
|
|
|
bool wasSelected = isSelected();
|
|
|
|
bool active = m_rect.contains(pos);
|
|
|
|
setSelected(wasSelected && active);
|
|
|
|
return (wasSelected != isSelected()) || active;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QEvent::GraphicsSceneMouseRelease:
|
|
|
|
{
|
|
|
|
// kDebug() << "IconAction::event got a QEvent::MouseButtonRelease, " << isSelected();
|
|
|
|
bool wasSelected = isSelected();
|
|
|
|
setSelected(false);
|
|
|
|
if (wasSelected) {
|
|
|
|
m_action->trigger();
|
|
|
|
}
|
|
|
|
|
|
|
|
return wasSelected;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QEvent::GraphicsSceneHoverEnter:
|
|
|
|
m_pressed = false;
|
|
|
|
m_hovered = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QEvent::GraphicsSceneHoverLeave:
|
|
|
|
m_pressed = false;
|
|
|
|
m_hovered = false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QAction *IconAction::action() const
|
|
|
|
{
|
|
|
|
return m_action;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IconAction::paint(QPainter *painter) const
|
|
|
|
{
|
2010-01-10 20:34:33 +01:00
|
|
|
if (!m_action->isVisible() || !m_action->isEnabled()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
if (m_icon->size().width() < m_rect.width() * 2.0 ||
|
|
|
|
m_icon->size().height() < m_rect.height() * 2.0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-02-05 22:46:51 +01:00
|
|
|
Animation *animation = m_animation.data();
|
2010-02-08 20:27:12 +01:00
|
|
|
if (m_visible && !animation) {
|
2008-11-04 00:08:39 +01:00
|
|
|
painter->drawPixmap(m_rect.toRect(), m_pixmap);
|
|
|
|
} else {
|
2010-02-05 22:46:51 +01:00
|
|
|
painter->drawPixmap(m_rect.toRect(),
|
|
|
|
animation->property("currentPixmap").value<QPixmap>());
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
IconWidget::IconWidget(QGraphicsItem *parent)
|
|
|
|
: QGraphicsWidget(parent),
|
|
|
|
d(new IconWidgetPrivate(this))
|
|
|
|
{
|
2009-01-06 22:11:54 +01:00
|
|
|
d->init();
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
IconWidget::IconWidget(const QString &text, QGraphicsItem *parent)
|
|
|
|
: QGraphicsWidget(parent),
|
|
|
|
d(new IconWidgetPrivate(this))
|
|
|
|
{
|
2009-01-06 22:11:54 +01:00
|
|
|
d->init();
|
2008-11-04 00:08:39 +01:00
|
|
|
setText(text);
|
|
|
|
}
|
|
|
|
|
|
|
|
IconWidget::IconWidget(const QIcon &icon, const QString &text, QGraphicsItem *parent)
|
|
|
|
: QGraphicsWidget(parent),
|
|
|
|
d(new IconWidgetPrivate(this))
|
|
|
|
{
|
2009-01-06 22:11:54 +01:00
|
|
|
d->init();
|
2008-11-04 00:08:39 +01:00
|
|
|
setText(text);
|
|
|
|
setIcon(icon);
|
|
|
|
}
|
|
|
|
|
|
|
|
IconWidget::~IconWidget()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
2009-01-06 22:11:54 +01:00
|
|
|
void IconWidgetPrivate::init()
|
2008-11-04 00:08:39 +01:00
|
|
|
{
|
|
|
|
readColors();
|
2010-02-20 15:19:29 +01:00
|
|
|
|
|
|
|
iconChangeTimer = new QTimer(q);
|
|
|
|
iconChangeTimer->setSingleShot(true);
|
|
|
|
|
2009-01-20 03:37:52 +01:00
|
|
|
QObject::connect(Plasma::Theme::defaultTheme(), SIGNAL(themeChanged()), q, SLOT(colorConfigChanged()));
|
|
|
|
QObject::connect(KGlobalSettings::self(), SIGNAL(kdisplayPaletteChanged()), q, SLOT(colorConfigChanged()));
|
|
|
|
QObject::connect(KGlobalSettings::self(), SIGNAL(iconChanged(int)), q, SLOT(iconConfigChanged()));
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
// setAcceptedMouseButtons(Qt::LeftButton);
|
2009-01-06 22:11:54 +01:00
|
|
|
q->setAcceptsHoverEvents(true);
|
2009-10-01 02:58:41 +02:00
|
|
|
q->setCacheMode(QGraphicsItem::DeviceCoordinateCache);
|
2008-11-04 00:08:39 +01:00
|
|
|
|
2009-09-09 19:03:22 +02:00
|
|
|
background = new Plasma::FrameSvg(q);
|
|
|
|
background->setImagePath("widgets/viewitem");
|
|
|
|
background->setCacheAllRenderedFrames(true);
|
|
|
|
background->setElementPrefix("hover");
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
// Margins for horizontal mode (list views, tree views, table views)
|
2009-01-06 22:11:54 +01:00
|
|
|
setHorizontalMargin(IconWidgetPrivate::TextMargin, 1, 1);
|
2009-06-14 22:14:28 +02:00
|
|
|
setHorizontalMargin(IconWidgetPrivate::IconMargin, 1, 1);
|
2009-01-06 22:11:54 +01:00
|
|
|
setHorizontalMargin(IconWidgetPrivate::ItemMargin, 0, 0);
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
// Margins for vertical mode (icon views)
|
2009-01-06 22:11:54 +01:00
|
|
|
setVerticalMargin(IconWidgetPrivate::TextMargin, 6, 2);
|
2009-06-14 22:14:28 +02:00
|
|
|
setVerticalMargin(IconWidgetPrivate::IconMargin, 1, 1);
|
2009-01-06 22:11:54 +01:00
|
|
|
setVerticalMargin(IconWidgetPrivate::ItemMargin, 0, 0);
|
2008-11-04 00:08:39 +01:00
|
|
|
|
2009-01-06 22:11:54 +01:00
|
|
|
setActiveMargins();
|
|
|
|
currentSize = QSizeF(-1, -1);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void IconWidget::addIconAction(QAction *action)
|
|
|
|
{
|
|
|
|
int count = d->cornerActions.count();
|
2009-10-15 04:39:38 +02:00
|
|
|
if (count >= IconWidgetPrivate::LastIconPosition) {
|
2008-11-04 00:08:39 +01:00
|
|
|
kDebug() << "no more room for more actions!";
|
2009-10-15 04:39:38 +02:00
|
|
|
// just overlap it with the last item for now. ugly, but there you go.
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
IconAction *iconAction = new IconAction(this, action);
|
|
|
|
d->cornerActions.append(iconAction);
|
|
|
|
connect(action, SIGNAL(destroyed(QObject*)), this, SLOT(actionDestroyed(QObject*)));
|
|
|
|
|
2009-10-15 04:39:38 +02:00
|
|
|
iconAction->setRect(d->actionRect(qMin((IconWidgetPrivate::ActionPosition)count, IconWidgetPrivate::LastIconPosition)));
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
2009-01-25 10:58:02 +01:00
|
|
|
void IconWidget::removeIconAction(QAction *action)
|
|
|
|
{
|
2009-10-15 04:39:38 +02:00
|
|
|
//WARNING: do NOT access the action pointer passed in, as it may already be
|
|
|
|
//be destroyed. see IconWidgetPrivate::actionDestroyed(QObject*)
|
|
|
|
int count = 0;
|
|
|
|
bool found = false;
|
|
|
|
foreach (IconAction *iconAction, d->cornerActions) {
|
|
|
|
if (found) {
|
|
|
|
iconAction->setRect(d->actionRect((IconWidgetPrivate::ActionPosition)count));
|
2009-11-03 14:10:19 +01:00
|
|
|
} else if (!action || iconAction->action() == action) {
|
2009-10-15 04:39:38 +02:00
|
|
|
delete iconAction;
|
|
|
|
d->cornerActions.removeAll(iconAction);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count < IconWidgetPrivate::LastIconPosition) {
|
|
|
|
++count;
|
2009-01-25 10:58:02 +01:00
|
|
|
}
|
|
|
|
}
|
2009-10-15 04:39:38 +02:00
|
|
|
|
|
|
|
// redraw since an action has been deleted.
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void IconWidgetPrivate::actionDestroyed(QObject *action)
|
|
|
|
{
|
|
|
|
q->removeIconAction(static_cast<QAction*>(action));
|
2009-01-25 10:58:02 +01:00
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
void IconWidget::setAction(QAction *action)
|
|
|
|
{
|
2009-04-24 09:40:52 +02:00
|
|
|
d->setAction(action);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QAction *IconWidget::action() const
|
|
|
|
{
|
|
|
|
return d->action;
|
|
|
|
}
|
|
|
|
|
|
|
|
int IconWidget::numDisplayLines()
|
|
|
|
{
|
|
|
|
return d->numDisplayLines;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IconWidget::setNumDisplayLines(int numLines)
|
|
|
|
{
|
2008-12-16 00:42:29 +01:00
|
|
|
if (numLines > d->maxDisplayLines) {
|
2008-11-04 00:08:39 +01:00
|
|
|
d->numDisplayLines = d->maxDisplayLines;
|
|
|
|
} else {
|
|
|
|
d->numDisplayLines = numLines;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void IconWidget::setDrawBackground(bool draw)
|
|
|
|
{
|
|
|
|
if (d->drawBg != draw) {
|
|
|
|
d->drawBg = draw;
|
2008-12-16 00:42:29 +01:00
|
|
|
|
|
|
|
QStyle *style = QApplication::style();
|
|
|
|
int focusHMargin = draw ? style->pixelMetric(QStyle::PM_FocusFrameHMargin) : 1;
|
|
|
|
int focusVMargin = draw ? style->pixelMetric(QStyle::PM_FocusFrameVMargin) : 1;
|
|
|
|
d->setHorizontalMargin(IconWidgetPrivate::TextMargin, focusHMargin, focusVMargin);
|
|
|
|
d->setHorizontalMargin(IconWidgetPrivate::IconMargin, focusHMargin, focusVMargin);
|
|
|
|
d->setVerticalMargin(IconWidgetPrivate::IconMargin, focusHMargin, focusVMargin);
|
|
|
|
d->currentSize = QSizeF(-1, -1);
|
|
|
|
|
2009-09-09 19:03:22 +02:00
|
|
|
if (draw) {
|
|
|
|
qreal left, top, right, bottom;
|
|
|
|
d->background->getMargins(left, top, right, bottom);
|
|
|
|
d->setHorizontalMargin(IconWidgetPrivate::ItemMargin, left, top, right, bottom);
|
|
|
|
d->setVerticalMargin(IconWidgetPrivate::ItemMargin, left, top, right, bottom);
|
|
|
|
} else {
|
|
|
|
d->setHorizontalMargin(IconWidgetPrivate::ItemMargin, 0, 0);
|
|
|
|
d->setVerticalMargin(IconWidgetPrivate::ItemMargin, 0, 0);
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IconWidget::drawBackground() const
|
|
|
|
{
|
|
|
|
return d->drawBg;
|
|
|
|
}
|
|
|
|
|
|
|
|
QPainterPath IconWidget::shape() const
|
|
|
|
{
|
2009-06-10 04:18:41 +02:00
|
|
|
if (!d->drawBg || d->currentSize.width() < 1) {
|
2008-11-04 00:08:39 +01:00
|
|
|
return QGraphicsItem::shape();
|
|
|
|
}
|
|
|
|
|
|
|
|
return PaintUtils::roundedRectangle(
|
|
|
|
QRectF(QPointF(0.0, 0.0), d->currentSize).adjusted(-2, -2, 2, 2), 10.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
QSizeF IconWidgetPrivate::displaySizeHint(const QStyleOptionGraphicsItem *option, const qreal width) const
|
|
|
|
{
|
|
|
|
if (text.isEmpty() && infoText.isEmpty()) {
|
|
|
|
return QSizeF(.0, .0);
|
|
|
|
}
|
2009-01-06 22:11:54 +01:00
|
|
|
|
2008-11-04 00:08:39 +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 -
|
|
|
|
horizontalMargin[IconWidgetPrivate::TextMargin].left -
|
|
|
|
horizontalMargin[IconWidgetPrivate::TextMargin].right;
|
|
|
|
|
|
|
|
//allow only five lines of text
|
|
|
|
const qreal maxHeight =
|
2009-11-10 10:46:14 +01:00
|
|
|
numDisplayLines * QFontMetrics(widgetFont()).lineSpacing();
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
// To compute the nominal size for the label + info, we'll just append
|
|
|
|
// the information string to the label
|
|
|
|
if (!infoText.isEmpty()) {
|
|
|
|
label += QString(QChar::LineSeparator) + infoText;
|
|
|
|
}
|
|
|
|
|
|
|
|
QTextLayout layout;
|
2009-02-09 11:51:21 +01:00
|
|
|
setLayoutOptions(layout, option, q->orientation());
|
2009-11-10 10:46:14 +01:00
|
|
|
layout.setFont(widgetFont());
|
2008-11-04 00:08:39 +01:00
|
|
|
QSizeF size = layoutText(layout, option, label, QSizeF(textWidth, maxHeight));
|
|
|
|
|
|
|
|
return addMargin(size, TextMargin);
|
|
|
|
}
|
|
|
|
|
2009-01-06 22:11:54 +01:00
|
|
|
void IconWidgetPrivate::layoutIcons(const QStyleOptionGraphicsItem *option)
|
2008-11-04 00:08:39 +01:00
|
|
|
{
|
2009-06-24 18:54:51 +02:00
|
|
|
if (option->rect.size() == currentSize) {
|
2008-11-04 00:08:39 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-06-24 18:54:51 +02:00
|
|
|
currentSize = option->rect.size();
|
2009-01-06 22:11:54 +01:00
|
|
|
setActiveMargins();
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
//calculate icon size based on the available space
|
|
|
|
qreal iconWidth;
|
|
|
|
|
2009-01-06 22:11:54 +01:00
|
|
|
if (orientation == Qt::Vertical) {
|
2008-11-04 00:08:39 +01:00
|
|
|
qreal heightAvail;
|
|
|
|
//if there is text resize the icon in order to make room for the text
|
2009-01-06 22:11:54 +01:00
|
|
|
if (text.isEmpty() && infoText.isEmpty()) {
|
|
|
|
heightAvail = currentSize.height();
|
2008-12-16 00:42:29 +01:00
|
|
|
} else {
|
2009-01-06 22:11:54 +01:00
|
|
|
heightAvail = currentSize.height() -
|
|
|
|
displaySizeHint(option, currentSize.width()).height() -
|
|
|
|
verticalMargin[IconWidgetPrivate::TextMargin].top -
|
|
|
|
verticalMargin[IconWidgetPrivate::TextMargin].bottom;
|
2008-11-04 00:08:39 +01:00
|
|
|
//never make a label higher than half the total height
|
2009-01-06 22:11:54 +01:00
|
|
|
heightAvail = qMax(heightAvail, currentSize.height() / 2);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//aspect ratio very "tall"
|
2010-03-28 17:59:45 +02:00
|
|
|
if (!text.isEmpty() || !infoText.isEmpty()) {
|
|
|
|
if (currentSize.width() < heightAvail) {
|
|
|
|
iconWidth = currentSize.width() -
|
|
|
|
horizontalMargin[IconWidgetPrivate::IconMargin].left -
|
|
|
|
horizontalMargin[IconWidgetPrivate::IconMargin].right;
|
|
|
|
} else {
|
|
|
|
iconWidth = heightAvail -
|
|
|
|
verticalMargin[IconWidgetPrivate::IconMargin].top -
|
|
|
|
verticalMargin[IconWidgetPrivate::IconMargin].bottom;
|
|
|
|
}
|
2008-11-04 00:08:39 +01:00
|
|
|
} else {
|
2010-03-28 17:59:45 +02:00
|
|
|
iconWidth = qMin(heightAvail, currentSize.width());
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
2009-09-09 19:03:22 +02:00
|
|
|
iconWidth -= horizontalMargin[IconWidgetPrivate::ItemMargin].left + horizontalMargin[IconWidgetPrivate::ItemMargin].right;
|
2008-11-04 00:08:39 +01:00
|
|
|
} else {
|
|
|
|
//Horizontal layout
|
|
|
|
//if there is text resize the icon in order to make room for the text
|
2009-01-06 22:11:54 +01:00
|
|
|
if (text.isEmpty() && infoText.isEmpty()) {
|
2008-11-04 00:08:39 +01:00
|
|
|
// with no text, we just take up the whole geometry
|
2010-03-28 17:59:45 +02:00
|
|
|
iconWidth = qMin(currentSize.height(), currentSize.width());
|
2008-11-04 00:08:39 +01:00
|
|
|
} else {
|
2009-01-06 22:11:54 +01:00
|
|
|
iconWidth = currentSize.height() -
|
|
|
|
verticalMargin[IconWidgetPrivate::IconMargin].top -
|
|
|
|
verticalMargin[IconWidgetPrivate::IconMargin].bottom;
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
2009-09-09 19:03:22 +02:00
|
|
|
iconWidth -= verticalMargin[IconWidgetPrivate::ItemMargin].top + verticalMargin[IconWidgetPrivate::ItemMargin].bottom;
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
2009-01-06 22:11:54 +01:00
|
|
|
iconSize = QSizeF(iconWidth, iconWidth);
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
int count = 0;
|
2009-01-06 22:11:54 +01:00
|
|
|
foreach (IconAction *iconAction, cornerActions) {
|
|
|
|
iconAction->setRect(actionRect((IconWidgetPrivate::ActionPosition)count));
|
2008-11-04 00:08:39 +01:00
|
|
|
++count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void IconWidget::setSvg(const QString &svgFilePath, const QString &elementId)
|
|
|
|
{
|
|
|
|
if (!d->iconSvg) {
|
|
|
|
d->iconSvg = new Plasma::Svg(this);
|
2009-01-05 18:34:40 +01:00
|
|
|
connect(d->iconSvg, SIGNAL(repaintNeeded()), this, SLOT(svgChanged()));
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
d->iconSvg->setImagePath(svgFilePath);
|
|
|
|
d->iconSvg->setContainsMultipleImages(!elementId.isNull());
|
|
|
|
d->iconSvgElement = elementId;
|
2008-11-05 21:43:53 +01:00
|
|
|
d->iconSvgElementChanged = true;
|
2009-01-20 03:37:52 +01:00
|
|
|
d->icon = QIcon();
|
2008-11-05 21:43:53 +01:00
|
|
|
update();
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
2009-04-24 09:40:52 +02:00
|
|
|
QString IconWidget::svg() const
|
|
|
|
{
|
|
|
|
if (d->iconSvg) {
|
|
|
|
return d->iconSvg->imagePath();
|
|
|
|
}
|
|
|
|
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
2009-09-04 00:24:29 +02:00
|
|
|
QSizeF IconWidget::sizeHint(Qt::SizeHint which, const QSizeF & constraint) const
|
|
|
|
{
|
|
|
|
if (which == Qt::PreferredSize) {
|
|
|
|
return sizeFromIconSize(KIconLoader::SizeMedium);
|
2009-12-26 18:29:55 +01:00
|
|
|
} else if (which == Qt::MinimumSize) {
|
|
|
|
return sizeFromIconSize(KIconLoader::SizeSmall);
|
2009-09-04 00:24:29 +02:00
|
|
|
} else {
|
|
|
|
return QGraphicsWidget::sizeHint(which, constraint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-16 18:24:35 +01:00
|
|
|
void IconWidgetPrivate::animateMainIcon(bool show, const IconWidgetStates state)
|
2008-11-04 00:08:39 +01:00
|
|
|
{
|
|
|
|
if (show) {
|
2010-02-16 18:24:35 +01:00
|
|
|
states = state;
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
2010-02-05 22:46:51 +01:00
|
|
|
hoverAnimation->setFadeIn(show);
|
|
|
|
|
|
|
|
QPropertyAnimation *animation = hoverAnimation->animation();
|
|
|
|
if (!animation) {
|
|
|
|
animation = new QPropertyAnimation(hoverAnimation, "value");
|
|
|
|
animation->setProperty("duration", 150);
|
|
|
|
animation->setProperty("easingCurve", QEasingCurve::OutQuad);
|
|
|
|
animation->setProperty("startValue", 0.0);
|
|
|
|
animation->setProperty("endValue", 1.0);
|
|
|
|
hoverAnimation->setAnimation(animation);
|
|
|
|
q->connect(animation, SIGNAL(finished()), q, SLOT(hoverAnimationFinished()));
|
|
|
|
} else if (animation->state() == QAbstractAnimation::Running) {
|
|
|
|
animation->pause();
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
2009-01-06 22:11:54 +01:00
|
|
|
|
2010-02-05 22:46:51 +01:00
|
|
|
animation->setProperty("direction", show ?
|
|
|
|
QAbstractAnimation::Forward : QAbstractAnimation::Backward);
|
|
|
|
animation->start(show ?
|
|
|
|
QAbstractAnimation::KeepWhenStopped : QAbstractAnimation::DeleteWhenStopped);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
2010-02-05 22:46:51 +01:00
|
|
|
void IconWidgetPrivate::hoverAnimationFinished()
|
2008-11-04 00:08:39 +01:00
|
|
|
{
|
2010-02-05 22:46:51 +01:00
|
|
|
if (!hoverAnimation->fadeIn()) {
|
|
|
|
states &= ~IconWidgetPrivate::HoverState;
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void IconWidgetPrivate::drawBackground(QPainter *painter, IconWidgetState state)
|
|
|
|
{
|
|
|
|
if (!drawBg) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-01-19 21:41:45 +01:00
|
|
|
if (state == IconWidgetPrivate::PressedState) {
|
|
|
|
background->setElementPrefix("selected");
|
|
|
|
} else {
|
|
|
|
background->setElementPrefix("hover");
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
2010-02-05 22:46:51 +01:00
|
|
|
if (qFuzzyCompare(hoverAnimation->value(), 1)) {
|
2009-01-19 21:41:45 +01:00
|
|
|
background->resizeFrame(currentSize);
|
|
|
|
background->paintFrame(painter);
|
2010-02-05 22:46:51 +01:00
|
|
|
} else if (!qFuzzyCompare(hoverAnimation->value()+1, 1)) {
|
2009-01-19 21:41:45 +01:00
|
|
|
background->resizeFrame(currentSize);
|
|
|
|
QPixmap frame = background->framePixmap();
|
|
|
|
QPainter bufferPainter(&frame);
|
|
|
|
bufferPainter.setCompositionMode(QPainter::CompositionMode_DestinationIn);
|
2010-02-05 22:46:51 +01:00
|
|
|
bufferPainter.fillRect(frame.rect(), QColor(0,0,0, 255*hoverAnimation->value()));
|
2009-01-19 21:41:45 +01:00
|
|
|
bufferPainter.end();
|
|
|
|
painter->drawPixmap(QPoint(0,0), frame);
|
|
|
|
}
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
2009-03-30 08:42:19 +02:00
|
|
|
QPixmap IconWidgetPrivate::decoration(const QStyleOptionGraphicsItem *option, bool useHoverEffect, bool usePressedEffect)
|
2008-11-04 00:08:39 +01: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;
|
|
|
|
|
|
|
|
if (iconSvg) {
|
2008-11-05 21:43:53 +01:00
|
|
|
if (iconSvgElementChanged || iconSvgPixmap.size() != iconSize.toSize()) {
|
2010-02-16 15:38:58 +01:00
|
|
|
iconSvg->resize(iconSize);
|
|
|
|
iconSvgPixmap = iconSvg->pixmap(iconSvgElement);
|
2008-11-05 21:43:53 +01:00
|
|
|
iconSvgElementChanged = false;
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
result = iconSvgPixmap;
|
|
|
|
} else {
|
|
|
|
const QSize size = icon.actualSize(iconSize.toSize(), mode, state);
|
|
|
|
result = icon.pixmap(size, mode, state);
|
|
|
|
}
|
|
|
|
|
2009-06-24 18:54:51 +02:00
|
|
|
if (usePressedEffect) {
|
|
|
|
result = result.scaled(result.size() * 0.9, Qt::KeepAspectRatio);
|
|
|
|
}
|
2009-04-16 17:41:56 +02:00
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
if (!result.isNull() && useHoverEffect) {
|
|
|
|
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)) {
|
2010-02-05 22:46:51 +01:00
|
|
|
if (qFuzzyCompare(qreal(1.0), hoverAnimation->value())) {
|
2008-11-04 00:08:39 +01:00
|
|
|
result = effect->apply(result, KIconLoader::Desktop, KIconLoader::ActiveState);
|
|
|
|
} else {
|
|
|
|
result = PaintUtils::transition(
|
|
|
|
result,
|
|
|
|
effect->apply(result, KIconLoader::Desktop,
|
2010-02-05 22:46:51 +01:00
|
|
|
KIconLoader::ActiveState), hoverAnimation->value());
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
}
|
2010-02-16 18:24:35 +01:00
|
|
|
} else if (!result.isNull() && !oldIcon.isNull()) {
|
|
|
|
if (qFuzzyCompare(qreal(1.0), hoverAnimation->value())) {
|
|
|
|
oldIcon = QIcon();
|
|
|
|
} else {
|
|
|
|
result = PaintUtils::transition(
|
2010-03-28 21:55:51 +02:00
|
|
|
oldIcon.pixmap(result.size(), mode, state),
|
2010-02-16 18:24:35 +01:00
|
|
|
result, hoverAnimation->value());
|
|
|
|
}
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
QPointF IconWidgetPrivate::iconPosition(const QStyleOptionGraphicsItem *option,
|
|
|
|
const QPixmap &pixmap) const
|
|
|
|
{
|
|
|
|
const QRectF itemRect = subtractMargin(option->rect, IconWidgetPrivate::ItemMargin);
|
|
|
|
|
|
|
|
// Compute the nominal decoration rectangle
|
|
|
|
const QSizeF size = addMargin(iconSize, IconWidgetPrivate::IconMargin);
|
|
|
|
|
2009-09-09 19:03:22 +02:00
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
Qt::LayoutDirection direction = iconDirection(option);
|
|
|
|
|
|
|
|
//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(
|
|
|
|
direction, Qt::Alignment(Qt::AlignLeft | Qt::AlignVCenter));
|
|
|
|
}
|
|
|
|
|
|
|
|
const QRect iconRect =
|
|
|
|
QStyle::alignedRect(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 IconWidgetPrivate::labelRectangle(const QStyleOptionGraphicsItem *option,
|
|
|
|
const QPixmap &icon,
|
|
|
|
const QString &string) const
|
|
|
|
{
|
|
|
|
Q_UNUSED(string)
|
|
|
|
|
|
|
|
if (icon.isNull()) {
|
|
|
|
return option->rect;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QSizeF decoSize = addMargin(iconSize, IconWidgetPrivate::IconMargin);
|
|
|
|
const QRectF itemRect = subtractMargin(option->rect, IconWidgetPrivate::ItemMargin);
|
|
|
|
QRectF textArea(QPointF(0, 0), itemRect.size());
|
|
|
|
|
|
|
|
if (orientation == Qt::Vertical) {
|
|
|
|
textArea.setTop(decoSize.height() + 1);
|
|
|
|
} else {
|
|
|
|
//Horizontal
|
|
|
|
textArea.setLeft(decoSize.width() + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
textArea.translate(itemRect.topLeft());
|
|
|
|
return QRectF(QStyle::visualRect(iconDirection(option), option->rect, textArea.toRect()));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lays the text out in a rectangle no larger than constraints, eliding it as necessary
|
|
|
|
QSizeF IconWidgetPrivate::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()) {
|
2009-05-07 20:49:22 +02:00
|
|
|
if (action) {
|
2010-03-02 16:27:51 +01:00
|
|
|
q->setToolTip(action->toolTip());
|
2009-05-07 20:49:22 +02:00
|
|
|
}
|
2008-11-04 00:08:39 +01:00
|
|
|
const QString elided = elidedText(layout, option, constraints);
|
|
|
|
return layoutText(layout, elided, constraints.width());
|
|
|
|
}
|
2009-05-07 20:49:22 +02:00
|
|
|
q->setToolTip(QString());
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lays the text out in a rectangle no wider than maxWidth
|
|
|
|
QSizeF IconWidgetPrivate::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 IconWidgetPrivate::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.
|
|
|
|
if (line.naturalTextWidth() < maxWidth &&
|
|
|
|
start + length > 0 &&
|
|
|
|
text[start + length - 1] == QChar::LineSeparator) {
|
|
|
|
elided += text.mid(start, length - 1);
|
|
|
|
} else {
|
|
|
|
elided += metrics.elidedText(text.mid(start), Qt::ElideRight, maxWidth);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
} else if (line.naturalTextWidth() > maxWidth) {
|
|
|
|
elided += metrics.elidedText(text.mid(start, length), Qt::ElideRight, maxWidth);
|
|
|
|
} else {
|
|
|
|
elided += text.mid(start, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
height += line.height();
|
|
|
|
}
|
|
|
|
|
|
|
|
return elided;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IconWidgetPrivate::layoutTextItems(const QStyleOptionGraphicsItem *option,
|
|
|
|
const QPixmap &icon, QTextLayout *labelLayout,
|
|
|
|
QTextLayout *infoLayout, QRectF *textBoundingRect) const
|
|
|
|
{
|
|
|
|
bool showInformation = false;
|
|
|
|
|
2009-02-09 11:51:21 +01:00
|
|
|
setLayoutOptions(*labelLayout, option, q->orientation());
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
QFontMetricsF fm(labelLayout->font());
|
|
|
|
const QRectF textArea = labelRectangle(option, icon, text);
|
|
|
|
QRectF textRect = subtractMargin(textArea, IconWidgetPrivate::TextMargin);
|
|
|
|
|
|
|
|
//kDebug() << this << "text area" << textArea << "text rect" << textRect;
|
|
|
|
// Sizes and constraints for the different text parts
|
|
|
|
QSizeF maxLabelSize = textRect.size();
|
|
|
|
QSizeF maxInfoSize = textRect.size();
|
|
|
|
QSizeF labelSize;
|
|
|
|
QSizeF infoSize;
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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(iconDirection(option), alignment, size.toSize(), textRect.toRect());
|
|
|
|
|
|
|
|
// Compute the positions where we should draw the layouts
|
2009-11-26 22:57:36 +01:00
|
|
|
haloRects.clear();
|
2008-11-04 00:08:39 +01:00
|
|
|
labelLayout->setPosition(QPointF(textRect.x(), textBoundingRect->y()));
|
2009-11-26 22:57:36 +01:00
|
|
|
QTextLine line;
|
|
|
|
for (int i = 0; i < labelLayout->lineCount(); ++i) {
|
|
|
|
line = labelLayout->lineAt(i);
|
|
|
|
haloRects.append(line.naturalTextRect().translated(labelLayout->position().toPoint()).toRect());
|
|
|
|
}
|
2008-11-04 00:08:39 +01:00
|
|
|
infoLayout->setPosition(QPointF(textRect.x(), textBoundingRect->y() + labelSize.height()));
|
2009-11-26 22:57:36 +01:00
|
|
|
for (int i = 0; i < infoLayout->lineCount(); ++i) {
|
|
|
|
line = infoLayout->lineAt(i);
|
|
|
|
haloRects.append(line.naturalTextRect().translated(infoLayout->position().toPoint()).toRect());
|
|
|
|
}
|
2008-11-04 00:08:39 +01:00
|
|
|
//kDebug() << "final position is" << labelLayout->position();
|
|
|
|
}
|
|
|
|
|
|
|
|
QBrush IconWidgetPrivate::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 IconWidgetPrivate::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 IconWidgetPrivate::drawTextItems(QPainter *painter,
|
|
|
|
const QStyleOptionGraphicsItem *option,
|
|
|
|
const QTextLayout &labelLayout,
|
|
|
|
const QTextLayout &infoLayout) const
|
|
|
|
{
|
|
|
|
Q_UNUSED(option)
|
|
|
|
|
|
|
|
painter->save();
|
|
|
|
painter->setPen(textColor);
|
|
|
|
|
|
|
|
// the translation prevents odd rounding errors in labelLayout.position()
|
|
|
|
// when applied to the canvas
|
|
|
|
painter->translate(0.5, 0.5);
|
|
|
|
|
2009-05-23 19:16:15 +02:00
|
|
|
labelLayout.draw(painter, QPointF());
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
if (!infoLayout.text().isEmpty()) {
|
|
|
|
painter->setPen(textColor);
|
|
|
|
infoLayout.draw(painter, QPointF());
|
|
|
|
}
|
|
|
|
painter->restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
void IconWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
|
|
|
|
{
|
|
|
|
Q_UNUSED(widget);
|
|
|
|
|
|
|
|
//Lay out the main icon and action icons
|
2009-01-06 22:11:54 +01:00
|
|
|
d->layoutIcons(option);
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
// Compute the metrics, and lay out the text items
|
|
|
|
// ========================================================================
|
|
|
|
IconWidgetPrivate::IconWidgetState state = IconWidgetPrivate::NoState;
|
|
|
|
if (d->states & IconWidgetPrivate::ManualPressedState) {
|
|
|
|
state = IconWidgetPrivate::PressedState;
|
|
|
|
} else if (d->states & IconWidgetPrivate::PressedState) {
|
|
|
|
if (d->states & IconWidgetPrivate::HoverState) {
|
|
|
|
state = IconWidgetPrivate::PressedState;
|
|
|
|
}
|
|
|
|
} else if (d->states & IconWidgetPrivate::HoverState) {
|
|
|
|
state = IconWidgetPrivate::HoverState;
|
|
|
|
}
|
|
|
|
|
2009-03-30 08:42:19 +02:00
|
|
|
QPixmap icon = d->decoration(option, state != IconWidgetPrivate::NoState, state & IconWidgetPrivate::PressedState);
|
2008-11-04 00:08:39 +01:00
|
|
|
const QPointF iconPos = d->iconPosition(option, icon);
|
|
|
|
|
|
|
|
d->drawBackground(painter, state);
|
|
|
|
|
|
|
|
// draw icon
|
|
|
|
if (!icon.isNull()) {
|
|
|
|
painter->drawPixmap(iconPos, icon);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw corner actions
|
|
|
|
foreach (const IconAction *action, d->cornerActions) {
|
2010-02-05 22:46:51 +01:00
|
|
|
if (action->isAnimating()) {
|
2008-11-04 00:08:39 +01:00
|
|
|
action->paint(painter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-07 02:12:28 +01:00
|
|
|
// Draw text last because it is overlayed
|
2008-11-04 00:08:39 +01:00
|
|
|
QTextLayout labelLayout, infoLayout;
|
|
|
|
QRectF textBoundingRect;
|
2009-02-09 11:51:21 +01:00
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
d->layoutTextItems(option, icon, &labelLayout, &infoLayout, &textBoundingRect);
|
|
|
|
|
2009-12-01 21:10:49 +01:00
|
|
|
if (d->textBgColor != QColor() && d->textBgColor.alpha() > 0 &&
|
2009-08-08 15:10:37 +02:00
|
|
|
!(d->text.isEmpty() && d->infoText.isEmpty()) &&
|
2009-09-27 19:32:49 +02:00
|
|
|
!textBoundingRect.isEmpty() &&
|
2010-02-05 22:46:51 +01:00
|
|
|
!qFuzzyCompare(d->hoverAnimation->value(), (qreal)1.0)) {
|
2009-09-27 19:32:49 +02:00
|
|
|
QRectF rect = textBoundingRect.adjusted(-2, -2, 4, 4).toAlignedRect();
|
2009-04-10 23:16:14 +02:00
|
|
|
painter->setPen(Qt::transparent);
|
|
|
|
QColor color = d->textBgColor;
|
2010-02-05 22:46:51 +01:00
|
|
|
color.setAlpha(60 * (1.0 - d->hoverAnimation->value()));
|
2009-09-27 19:32:49 +02:00
|
|
|
QLinearGradient gradient(rect.topLeft(), rect.bottomLeft());
|
|
|
|
gradient.setColorAt(0, color.lighter(120));
|
|
|
|
gradient.setColorAt(1, color.darker(120));
|
|
|
|
painter->setBrush(gradient);
|
|
|
|
gradient.setColorAt(0, color.lighter(130));
|
|
|
|
gradient.setColorAt(1, color.darker(130));
|
|
|
|
painter->setPen(QPen(gradient, 0));
|
2009-04-10 23:16:14 +02:00
|
|
|
painter->setRenderHint(QPainter::Antialiasing);
|
2009-09-27 19:32:49 +02:00
|
|
|
painter->drawPath(PaintUtils::roundedRectangle(rect.translated(0.5, 0.5), 4));
|
2009-04-10 23:16:14 +02:00
|
|
|
}
|
|
|
|
|
2009-11-26 22:57:36 +01:00
|
|
|
|
|
|
|
if (d->shadowColor.value() < 128 || textBackgroundColor() != QColor()) {
|
|
|
|
QPoint shadowPos;
|
|
|
|
if (d->shadowColor.value() < 128) {
|
|
|
|
shadowPos = QPoint(1, 2);
|
|
|
|
} else {
|
|
|
|
shadowPos = QPoint(0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
QImage shadow(textBoundingRect.size().toSize() + QSize(4, 4),
|
|
|
|
QImage::Format_ARGB32_Premultiplied);
|
|
|
|
shadow.fill(Qt::transparent);
|
|
|
|
{
|
|
|
|
QPainter buffPainter(&shadow);
|
|
|
|
buffPainter.translate(-textBoundingRect.x(), -textBoundingRect.y());
|
|
|
|
d->drawTextItems(&buffPainter, option, labelLayout, infoLayout);
|
|
|
|
}
|
|
|
|
|
|
|
|
PaintUtils::shadowBlur(shadow, 2, d->shadowColor);
|
|
|
|
painter->drawImage(textBoundingRect.topLeft() + shadowPos, shadow);
|
2009-12-22 17:02:26 +01:00
|
|
|
} else if (!(d->text.isEmpty() && d->infoText.isEmpty()) &&
|
|
|
|
!textBoundingRect.isEmpty()) {
|
2009-11-26 22:57:36 +01:00
|
|
|
QRect labelRect = d->labelRectangle(option, icon, d->text).toRect();
|
|
|
|
|
2009-12-27 23:16:11 +01:00
|
|
|
foreach (const QRect &rect, d->haloRects) {
|
2009-11-26 22:57:36 +01:00
|
|
|
Plasma::PaintUtils::drawHalo(painter, rect);
|
|
|
|
}
|
|
|
|
}
|
2009-11-27 14:42:00 +01:00
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
d->drawTextItems(painter, option, labelLayout, infoLayout);
|
|
|
|
}
|
|
|
|
|
2009-04-10 23:16:14 +02:00
|
|
|
void IconWidget::setTextBackgroundColor(const QColor &color)
|
|
|
|
{
|
|
|
|
d->textBgCustomized = true;
|
|
|
|
d->textBgColor = color;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
QColor IconWidget::textBackgroundColor() const
|
|
|
|
{
|
|
|
|
return d->textBgColor;
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
void IconWidget::drawActionButtonBase(QPainter *painter, const QSize &size, int element)
|
|
|
|
{
|
|
|
|
qreal radius = size.width() / 2;
|
|
|
|
QRadialGradient gradient(radius, radius, radius, radius, radius);
|
|
|
|
int alpha;
|
|
|
|
|
|
|
|
if (element == IconWidgetPrivate::MinibuttonPressed) {
|
|
|
|
alpha = 255;
|
|
|
|
} else if (element == IconWidgetPrivate::MinibuttonHover) {
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
|
|
|
void IconWidget::setText(const QString &text)
|
|
|
|
{
|
2009-05-23 19:16:15 +02:00
|
|
|
d->text = KGlobal::locale()->removeAcceleratorMarker(text);
|
2008-11-04 00:08:39 +01:00
|
|
|
// cause a relayout
|
|
|
|
d->currentSize = QSizeF(-1, -1);
|
|
|
|
//try to relayout, needed if an icon was never shown before
|
|
|
|
if (!isVisible()) {
|
|
|
|
QStyleOptionGraphicsItem styleoption;
|
2009-01-06 22:11:54 +01:00
|
|
|
d->layoutIcons(&styleoption);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
resize(sizeFromIconSize(d->iconSize.width()));
|
2009-12-26 18:29:55 +01:00
|
|
|
updateGeometry();
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QString IconWidget::text() const
|
|
|
|
{
|
|
|
|
return d->text;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IconWidget::setInfoText(const QString &text)
|
|
|
|
{
|
|
|
|
d->infoText = text;
|
|
|
|
// cause a relayout
|
|
|
|
d->currentSize = QSizeF(-1, -1);
|
|
|
|
//try to relayout, needed if an icon was never shown before
|
|
|
|
if (!isVisible()) {
|
2009-01-06 22:11:54 +01:00
|
|
|
d->layoutIcons(new QStyleOptionGraphicsItem);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
resize(sizeFromIconSize(d->iconSize.width()));
|
2009-12-26 18:29:55 +01:00
|
|
|
updateGeometry();
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QString IconWidget::infoText() const
|
|
|
|
{
|
|
|
|
return d->infoText;
|
|
|
|
}
|
|
|
|
|
|
|
|
QIcon IconWidget::icon() const
|
|
|
|
{
|
|
|
|
return d->icon;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IconWidget::setIcon(const QString &icon)
|
|
|
|
{
|
|
|
|
if (icon.isEmpty()) {
|
|
|
|
setIcon(QIcon());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setIcon(KIcon(icon));
|
|
|
|
}
|
|
|
|
|
|
|
|
void IconWidget::setIcon(const QIcon &icon)
|
|
|
|
{
|
2010-02-16 18:24:35 +01:00
|
|
|
/*fade to the new icon, but to not bee a too big hog, not do that when:
|
|
|
|
- the fade animation is already running
|
|
|
|
- the icon is under mouse
|
|
|
|
- one betwen the old and new icon is null*/
|
2010-02-20 15:19:29 +01:00
|
|
|
if (!(d->states & IconWidgetPrivate::HoverState) && !d->iconChangeTimer->isActive() && d->oldIcon.isNull() && !d->icon.isNull() && !icon.isNull()) {
|
2010-02-16 18:24:35 +01:00
|
|
|
d->oldIcon = d->icon;
|
|
|
|
d->animateMainIcon(true, d->states);
|
2010-02-20 15:19:29 +01:00
|
|
|
} else {
|
|
|
|
d->oldIcon = QIcon();
|
2010-02-16 18:24:35 +01:00
|
|
|
}
|
2010-03-08 18:44:59 +01:00
|
|
|
d->iconChangeTimer->start(300);
|
2008-11-04 00:08:39 +01:00
|
|
|
d->icon = icon;
|
2008-11-05 21:43:53 +01:00
|
|
|
update();
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QSizeF IconWidget::iconSize() const
|
|
|
|
{
|
|
|
|
return d->iconSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IconWidget::isDown()
|
|
|
|
{
|
|
|
|
return d->states & IconWidgetPrivate::PressedState;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IconWidget::mousePressEvent(QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
|
|
|
if (event->button() != Qt::LeftButton) {
|
|
|
|
QGraphicsWidget::mousePressEvent(event);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
d->states |= IconWidgetPrivate::PressedState;
|
|
|
|
d->clickStartPos = scenePos();
|
|
|
|
|
|
|
|
bool handled = false;
|
|
|
|
foreach (IconAction *action, d->cornerActions) {
|
|
|
|
handled = action->event(event->type(), event->pos());
|
|
|
|
if (handled) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-11 11:24:06 +01:00
|
|
|
if (!handled && boundingRect().contains(event->pos())) {
|
2008-11-04 00:08:39 +01:00
|
|
|
emit pressed(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void IconWidget::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
|
|
|
if (~d->states & IconWidgetPrivate::PressedState) {
|
|
|
|
QGraphicsWidget::mouseMoveEvent(event);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (boundingRect().contains(event->pos())) {
|
|
|
|
if (~d->states & IconWidgetPrivate::HoverState) {
|
|
|
|
d->states |= IconWidgetPrivate::HoverState;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (d->states & IconWidgetPrivate::HoverState) {
|
|
|
|
d->states &= ~IconWidgetPrivate::HoverState;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void IconWidget::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
|
|
|
if (~d->states & IconWidgetPrivate::PressedState) {
|
|
|
|
QGraphicsWidget::mouseMoveEvent(event);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
d->states &= ~IconWidgetPrivate::PressedState;
|
|
|
|
|
|
|
|
//don't pass click when the mouse was moved
|
|
|
|
bool handled = d->clickStartPos != scenePos();
|
|
|
|
if (!handled) {
|
|
|
|
foreach (IconAction *action, d->cornerActions) {
|
|
|
|
if (action->event(event->type(), event->pos())) {
|
|
|
|
handled = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!handled) {
|
|
|
|
if (boundingRect().contains(event->pos())) {
|
|
|
|
emit clicked();
|
|
|
|
if (KGlobalSettings::singleClick()) {
|
|
|
|
emit activated();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
emit pressed(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void IconWidget::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
|
|
|
Q_UNUSED(event)
|
|
|
|
|
|
|
|
emit doubleClicked();
|
|
|
|
if (!KGlobalSettings::singleClick()) {
|
|
|
|
emit activated();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void IconWidget::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
|
|
|
|
{
|
2009-06-14 20:09:41 +02:00
|
|
|
//kDebug();
|
2008-11-04 00:08:39 +01:00
|
|
|
foreach (IconAction *action, d->cornerActions) {
|
|
|
|
action->show();
|
|
|
|
action->event(event->type(), event->pos());
|
|
|
|
}
|
2009-01-06 22:11:54 +01:00
|
|
|
|
2010-02-16 18:24:35 +01:00
|
|
|
d->animateMainIcon(true, d->states|IconWidgetPrivate::HoverState);
|
2008-11-04 00:08:39 +01:00
|
|
|
update();
|
|
|
|
|
|
|
|
QGraphicsWidget::hoverEnterEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void IconWidget::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
|
|
|
|
{
|
2009-06-14 20:09:41 +02:00
|
|
|
//kDebug() << d->cornerActions;
|
2008-11-04 00:08:39 +01:00
|
|
|
foreach (IconAction *action, d->cornerActions) {
|
|
|
|
action->hide();
|
|
|
|
action->event(event->type(), event->pos());
|
|
|
|
}
|
|
|
|
// d->states &= ~IconWidgetPrivate::HoverState; // Will be set once progress is zero again ...
|
2010-02-03 21:52:03 +01:00
|
|
|
//if an eventfilter stolen the mousereleaseevent remove the pressed state here
|
|
|
|
d->states &= ~IconWidgetPrivate::PressedState;
|
|
|
|
|
2010-02-16 18:24:35 +01:00
|
|
|
d->animateMainIcon(false, d->states|IconWidgetPrivate::HoverState);
|
2008-11-04 00:08:39 +01:00
|
|
|
update();
|
|
|
|
|
|
|
|
QGraphicsWidget::hoverLeaveEvent(event);
|
|
|
|
}
|
|
|
|
|
2009-04-03 21:57:13 +02:00
|
|
|
bool IconWidget::sceneEventFilter(QGraphicsItem *watched, QEvent *event)
|
2009-04-02 20:33:39 +02:00
|
|
|
{
|
2009-04-10 23:16:14 +02:00
|
|
|
Q_UNUSED(watched)
|
|
|
|
|
|
|
|
if (event->type() == QEvent::GraphicsSceneDragEnter) {
|
2010-02-16 18:24:35 +01:00
|
|
|
d->animateMainIcon(true, d->states|IconWidgetPrivate::HoverState);
|
2009-04-03 21:57:13 +02:00
|
|
|
update();
|
2009-04-10 23:16:14 +02:00
|
|
|
} else if (event->type() == QEvent::GraphicsSceneDragLeave) {
|
2010-02-16 18:24:35 +01:00
|
|
|
d->animateMainIcon(false, d->states|IconWidgetPrivate::HoverState);
|
2009-04-03 21:57:13 +02:00
|
|
|
update();
|
|
|
|
}
|
2009-04-02 20:33:39 +02:00
|
|
|
|
2009-04-03 21:57:13 +02:00
|
|
|
return false;
|
2009-04-02 20:33:39 +02:00
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
void IconWidget::setPressed(bool pressed)
|
|
|
|
{
|
|
|
|
if (pressed) {
|
|
|
|
d->states |= IconWidgetPrivate::ManualPressedState;
|
|
|
|
d->states |= IconWidgetPrivate::PressedState;
|
|
|
|
} else {
|
|
|
|
d->states &= ~IconWidgetPrivate::ManualPressedState;
|
|
|
|
d->states &= ~IconWidgetPrivate::PressedState;
|
|
|
|
}
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void IconWidget::setUnpressed()
|
|
|
|
{
|
|
|
|
setPressed(false);
|
|
|
|
}
|
|
|
|
|
2009-01-05 18:34:40 +01:00
|
|
|
void IconWidgetPrivate::svgChanged()
|
|
|
|
{
|
|
|
|
iconSvgElementChanged = true;
|
|
|
|
q->update();
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
void IconWidget::setOrientation(Qt::Orientation orientation)
|
|
|
|
{
|
|
|
|
d->orientation = orientation;
|
|
|
|
resize(sizeFromIconSize(d->iconSize.width()));
|
|
|
|
}
|
|
|
|
|
2009-01-21 06:59:33 +01:00
|
|
|
Qt::Orientation IconWidget::orientation() const
|
|
|
|
{
|
|
|
|
return d->orientation;
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
void IconWidget::invertLayout(bool invert)
|
|
|
|
{
|
|
|
|
d->invertLayout = invert;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IconWidget::invertedLayout() const
|
|
|
|
{
|
|
|
|
return d->invertLayout;
|
|
|
|
}
|
|
|
|
|
|
|
|
QSizeF IconWidget::sizeFromIconSize(const qreal iconWidth) const
|
|
|
|
{
|
2009-09-10 21:55:29 +02:00
|
|
|
d->setActiveMargins();
|
2008-11-04 00:08:39 +01:00
|
|
|
if (d->text.isEmpty() && d->infoText.isEmpty()) {
|
|
|
|
//no text, less calculations
|
|
|
|
return d->addMargin(d->addMargin(QSizeF(iconWidth, iconWidth), IconWidgetPrivate::IconMargin),
|
|
|
|
IconWidgetPrivate::ItemMargin);
|
|
|
|
}
|
|
|
|
|
2009-11-10 10:46:14 +01:00
|
|
|
QFontMetricsF fm(d->widgetFont());
|
2008-11-04 00:08:39 +01:00
|
|
|
qreal width = 0;
|
|
|
|
|
|
|
|
if (d->orientation == Qt::Vertical) {
|
|
|
|
// make room for at most 14 characters
|
2009-09-10 21:55:29 +02:00
|
|
|
width = qMax(d->maxWordWidth(d->text),
|
|
|
|
d->maxWordWidth(d->infoText)) +
|
2009-12-09 21:10:22 +01:00
|
|
|
fm.width("xxx") +
|
2008-11-04 00:08:39 +01:00
|
|
|
d->horizontalMargin[IconWidgetPrivate::TextMargin].left +
|
|
|
|
d->horizontalMargin[IconWidgetPrivate::TextMargin].right;
|
|
|
|
|
|
|
|
width = qMax(width,
|
|
|
|
iconWidth +
|
|
|
|
d->horizontalMargin[IconWidgetPrivate::IconMargin].left +
|
|
|
|
d->horizontalMargin[IconWidgetPrivate::IconMargin].right);
|
|
|
|
} else {
|
|
|
|
width = iconWidth +
|
|
|
|
d->horizontalMargin[IconWidgetPrivate::IconMargin].left +
|
|
|
|
d->horizontalMargin[IconWidgetPrivate::IconMargin].right +
|
2009-12-09 21:10:22 +01:00
|
|
|
qMax(fm.width(d->text), fm.width(d->infoText)) + fm.width("xxx") +
|
2008-11-04 00:08:39 +01:00
|
|
|
d->horizontalMargin[IconWidgetPrivate::TextMargin].left +
|
|
|
|
d->horizontalMargin[IconWidgetPrivate::TextMargin].right;
|
|
|
|
}
|
|
|
|
|
|
|
|
qreal height;
|
|
|
|
qreal textHeight;
|
|
|
|
|
|
|
|
QStyleOptionGraphicsItem option;
|
|
|
|
option.state = QStyle::State_None;
|
2009-12-09 21:10:22 +01:00
|
|
|
option.rect = QRect(0, 0, width, QWIDGETSIZE_MAX);
|
2008-11-04 00:08:39 +01:00
|
|
|
textHeight = d->displaySizeHint(&option, width).height();
|
|
|
|
|
|
|
|
if (d->orientation == Qt::Vertical) {
|
|
|
|
height = iconWidth + textHeight +
|
|
|
|
d->verticalMargin[IconWidgetPrivate::TextMargin].top +
|
|
|
|
d->verticalMargin[IconWidgetPrivate::TextMargin].bottom +
|
|
|
|
d->verticalMargin[IconWidgetPrivate::IconMargin].top +
|
|
|
|
d->verticalMargin[IconWidgetPrivate::IconMargin].bottom;
|
|
|
|
} else {
|
|
|
|
//Horizontal
|
|
|
|
height = qMax(iconWidth +
|
|
|
|
d->verticalMargin[IconWidgetPrivate::IconMargin].top +
|
|
|
|
d->verticalMargin[IconWidgetPrivate::IconMargin].bottom,
|
|
|
|
textHeight +
|
|
|
|
d->verticalMargin[IconWidgetPrivate::TextMargin].top +
|
|
|
|
d->verticalMargin[IconWidgetPrivate::TextMargin].bottom);
|
|
|
|
}
|
|
|
|
|
|
|
|
return d->addMargin(QSizeF(width, height), IconWidgetPrivate::ItemMargin);
|
|
|
|
}
|
|
|
|
|
2009-09-13 15:13:55 +02:00
|
|
|
void IconWidget::changeEvent(QEvent *event)
|
|
|
|
{
|
|
|
|
if (event->type() == QEvent::FontChange) {
|
|
|
|
d->customFont = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
QGraphicsWidget::changeEvent(event);
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
} // namespace Plasma
|
|
|
|
|
|
|
|
#include "iconwidget.moc"
|