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>
|
|
|
|
|
|
|
|
#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-05-24 03:48:36 +02:00
|
|
|
Icon::Private::Private(Icon *i)
|
|
|
|
: q(i),
|
|
|
|
iconSvg(0),
|
2007-08-08 07:29:50 +02:00
|
|
|
iconSize(48, 48),
|
2007-12-16 11:09:07 +01:00
|
|
|
states(Private::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),
|
|
|
|
action(0)
|
2007-05-29 09:59:50 +02:00
|
|
|
{
|
2008-02-27 01:50:07 +01:00
|
|
|
m_hoverAnimId = -1;
|
|
|
|
m_hoverAlpha = 20/255;
|
2007-08-05 19:11:46 +02:00
|
|
|
}
|
2007-07-21 06:07:41 +02:00
|
|
|
|
2007-08-05 19:11:46 +02:00
|
|
|
Icon::Private::~Private()
|
2007-07-21 06:07:41 +02:00
|
|
|
{
|
2007-08-05 19:11:46 +02:00
|
|
|
qDeleteAll(cornerActions);
|
2007-12-20 17:28:47 +01:00
|
|
|
delete iconSvg;
|
2007-08-05 19:11:46 +02:00
|
|
|
}
|
2007-07-21 06:07:41 +02:00
|
|
|
|
2008-02-27 01:50:07 +01:00
|
|
|
void Icon::readColors()
|
|
|
|
{
|
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
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-07-21 06:07:41 +02:00
|
|
|
IconAction::IconAction(Icon* icon, QAction *action)
|
|
|
|
: m_icon(icon),
|
|
|
|
m_action(action),
|
|
|
|
m_hovered(false),
|
|
|
|
m_pressed(false),
|
|
|
|
m_visible(false),
|
|
|
|
m_animationId(-1)
|
2007-07-21 00:30:33 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void IconAction::show()
|
|
|
|
{
|
2007-07-21 06:07:41 +02:00
|
|
|
if (m_animationId) {
|
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-01-03 19:38:11 +01:00
|
|
|
int element = Icon::Private::Minibutton;
|
2007-07-21 06:07:41 +02:00
|
|
|
if (m_pressed) {
|
2008-01-03 19:38:11 +01:00
|
|
|
element = Icon::Private::MinibuttonPressed;
|
2007-07-21 06:07:41 +02:00
|
|
|
} else if (m_hovered) {
|
2008-01-03 19:38:11 +01:00
|
|
|
element = Icon::Private::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-01-20 21:07:22 +01:00
|
|
|
if (m_icon->size().width() < m_rect.width()*2.0 ||
|
|
|
|
m_icon->size().height() < m_rect.height()*2.0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-07-21 06:07:41 +02:00
|
|
|
switch (type) {
|
2007-12-21 22:35:07 +01:00
|
|
|
case QEvent::GraphicsSceneMousePress: {
|
2007-07-21 06:07:41 +02:00
|
|
|
setSelected(m_rect.contains(pos));
|
|
|
|
return isSelected();
|
|
|
|
}
|
|
|
|
break;
|
2007-07-21 00:30:33 +02:00
|
|
|
|
2007-12-21 22:35:07 +01:00
|
|
|
case QEvent::GraphicsSceneMouseMove: {
|
2007-07-21 06:07:41 +02:00
|
|
|
bool wasSelected = isSelected();
|
|
|
|
bool active = m_rect.contains(pos);
|
|
|
|
setSelected(wasSelected && active);
|
|
|
|
return (wasSelected != isSelected()) || active;
|
|
|
|
}
|
|
|
|
break;
|
2007-07-21 00:30:33 +02:00
|
|
|
|
2007-12-21 22:35:07 +01:00
|
|
|
case QEvent::GraphicsSceneMouseRelease: {
|
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;
|
|
|
|
}
|
|
|
|
break;
|
2007-07-21 00:30:33 +02:00
|
|
|
|
2007-07-21 06:07:41 +02:00
|
|
|
case QEvent::GraphicsSceneHoverEnter:
|
|
|
|
m_pressed = false;
|
|
|
|
m_hovered = true;
|
|
|
|
break;
|
2007-07-21 00:30:33 +02:00
|
|
|
|
2007-07-21 06:07:41 +02:00
|
|
|
case QEvent::GraphicsSceneHoverLeave:
|
|
|
|
m_pressed = false;
|
|
|
|
m_hovered = false;
|
|
|
|
break;
|
2007-07-21 00:30:33 +02:00
|
|
|
|
2007-07-21 06:07:41 +02:00
|
|
|
default:
|
2007-07-21 00:30:33 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2007-07-21 06:07:41 +02:00
|
|
|
QAction* IconAction::action() const
|
2007-07-21 00:30:33 +02:00
|
|
|
{
|
2007-07-21 06:07:41 +02:00
|
|
|
return m_action;
|
2007-07-21 00:30:33 +02:00
|
|
|
}
|
|
|
|
|
2007-07-21 06:07:41 +02:00
|
|
|
void IconAction::paint(QPainter *painter) const
|
|
|
|
{
|
2008-01-20 21:07:22 +01:00
|
|
|
if (m_icon->size().width() < m_rect.width()*2.0 ||
|
|
|
|
m_icon->size().height() < m_rect.height()*2.0) {
|
|
|
|
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-05-24 03:48:36 +02:00
|
|
|
d(new Private(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-05-24 03:48:36 +02:00
|
|
|
d(new Private(this))
|
2007-07-20 08:28:51 +02:00
|
|
|
{
|
|
|
|
setText(text);
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
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-05-24 03:48:36 +02:00
|
|
|
d(new Private(this))
|
2007-07-20 08:28:51 +02:00
|
|
|
{
|
|
|
|
setText(text);
|
|
|
|
setIcon(icon);
|
|
|
|
init();
|
2007-05-29 09:59:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Icon::~Icon()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
2007-07-20 08:28:51 +02:00
|
|
|
void Icon::init()
|
|
|
|
{
|
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)
|
|
|
|
d->setHorizontalMargin(Private::TextMargin, focusHMargin, focusVMargin);
|
|
|
|
d->setHorizontalMargin(Private::IconMargin, focusHMargin, focusVMargin);
|
|
|
|
d->setHorizontalMargin(Private::ItemMargin, 0, 0);
|
|
|
|
|
|
|
|
// Margins for vertical mode (icon views)
|
|
|
|
d->setVerticalMargin(Private::TextMargin, 6, 2);
|
|
|
|
d->setVerticalMargin(Private::IconMargin, focusHMargin, focusVMargin);
|
|
|
|
d->setVerticalMargin(Private::ItemMargin, 0, 0);
|
2007-08-08 07:29:50 +02:00
|
|
|
|
2007-12-26 13:37:54 +01:00
|
|
|
d->setActiveMargins();
|
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
|
|
|
}
|
|
|
|
|
2007-07-21 06:07:41 +02:00
|
|
|
IconAction* iconAction = new IconAction(this, action);
|
|
|
|
d->cornerActions.append(iconAction);
|
|
|
|
connect(action, SIGNAL(destroyed(QObject*)), this, SLOT(actionDestroyed(QObject*)));
|
|
|
|
|
2008-01-20 21:07:22 +01:00
|
|
|
iconAction->setRect(d->actionRect((Private::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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QAction* Icon::action() const
|
|
|
|
{
|
|
|
|
return d->action;
|
|
|
|
}
|
|
|
|
|
2007-07-21 06:07:41 +02:00
|
|
|
void Icon::actionDestroyed(QObject* action)
|
|
|
|
{
|
|
|
|
QList<IconAction*>::iterator it = d->cornerActions.begin();
|
|
|
|
|
|
|
|
while (it != d->cornerActions.end()) {
|
|
|
|
if ((*it)->action() == action) {
|
|
|
|
d->cornerActions.erase(it);
|
2007-07-21 00:30:33 +02:00
|
|
|
break;
|
2007-07-21 06:07:41 +02:00
|
|
|
}
|
2007-07-21 00:30:33 +02:00
|
|
|
}
|
2007-07-28 21:27:56 +02:00
|
|
|
|
|
|
|
update(); // redraw since an action has been deleted.
|
2007-07-20 08:28:51 +02:00
|
|
|
}
|
|
|
|
|
2007-12-26 14:44:57 +01:00
|
|
|
int Icon::numDisplayLines()
|
|
|
|
{
|
|
|
|
return d->numDisplayLines;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Icon::setNumDisplayLines(int numLines)
|
|
|
|
{
|
|
|
|
if(numLines > d->maxDisplayLines) {
|
|
|
|
d->numDisplayLines = d->maxDisplayLines;
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
return roundedRectangle(QRectF(QPointF(0.0, 0.0), d->currentSize).adjusted(-2, -2, 2, 2), 10.0);
|
|
|
|
}
|
|
|
|
|
2007-12-23 11:51:00 +01:00
|
|
|
QSizeF Icon::Private::displaySizeHint(const QStyleOptionGraphicsItem *option, const qreal width) const
|
|
|
|
{
|
|
|
|
if (text.isEmpty() && infoText.isEmpty()) {
|
|
|
|
return QSizeF( .0, .0 );
|
|
|
|
}
|
|
|
|
QString label = text;
|
|
|
|
// const qreal maxWidth = (orientation == Qt::Vertical) ? iconSize.width() + 10 : 32757;
|
|
|
|
// NOTE: find a way to use the other layoutText, it currently returns nominal width, when
|
|
|
|
// we actually need the actual width.
|
|
|
|
|
|
|
|
|
|
|
|
qreal textWidth = width -
|
|
|
|
horizontalMargin[Private::TextMargin].left -
|
|
|
|
horizontalMargin[Private::TextMargin].right;
|
|
|
|
|
|
|
|
//allow only five lines of text
|
2008-04-22 23:10:14 +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() -
|
2007-12-28 12:29:56 +01:00
|
|
|
d->verticalMargin[Private::TextMargin].top -
|
|
|
|
d->verticalMargin[Private::TextMargin].bottom;
|
|
|
|
//never make a label higher than half the total height
|
2008-01-01 20:21:33 +01: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() -
|
2007-12-28 12:29:56 +01:00
|
|
|
d->horizontalMargin[Private::IconMargin].left -
|
|
|
|
d->horizontalMargin[Private::IconMargin].right;
|
2008-01-07 06:09:09 +01:00
|
|
|
} else {
|
2007-12-28 12:29:56 +01:00
|
|
|
iconWidth = heightAvail -
|
|
|
|
d->verticalMargin[Private::IconMargin].top -
|
|
|
|
d->verticalMargin[Private::IconMargin].bottom;
|
|
|
|
}
|
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() -
|
|
|
|
d->horizontalMargin[Private::IconMargin].left -
|
|
|
|
d->horizontalMargin[Private::IconMargin].right;
|
2008-01-07 06:09:09 +01:00
|
|
|
} else {
|
2008-01-01 20:21:33 +01:00
|
|
|
iconWidth = d->currentSize.height() -
|
2007-12-28 12:29:56 +01:00
|
|
|
d->verticalMargin[Private::IconMargin].top -
|
|
|
|
d->verticalMargin[Private::IconMargin].bottom;
|
|
|
|
}
|
2007-12-26 13:37:54 +01:00
|
|
|
}
|
2007-12-28 12:29:56 +01:00
|
|
|
|
2007-12-26 13:37:54 +01:00
|
|
|
d->iconSize = QSizeF(iconWidth, iconWidth);
|
2007-07-21 06:19:00 +02:00
|
|
|
|
|
|
|
int count = 0;
|
|
|
|
foreach (IconAction* iconAction, d->cornerActions) {
|
2008-01-20 21:07:22 +01:00
|
|
|
iconAction->setRect(d->actionRect((Private::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-04-23 15:07:41 +02:00
|
|
|
d->iconSvg = new Plasma::Svg();
|
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);
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
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-04-25 05:11:59 +02:00
|
|
|
d->m_hoverAnimId = Animator::self()->customAnimation(40 / (1000 / FadeInDuration), FadeInDuration,
|
|
|
|
Animator::EaseOutCurve, this,
|
|
|
|
"hoverAnimationUpdate");
|
2008-02-27 01:50:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Icon::hoverAnimationUpdate(qreal progress)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
qreal new_alpha = d->m_fadeIn ? progress : 1 - progress;
|
|
|
|
d->m_hoverAlpha = qMin(new_alpha, d->m_hoverAlpha);
|
|
|
|
}
|
|
|
|
if (progress == 1) {
|
|
|
|
d->m_hoverAnimId = -1;
|
|
|
|
}
|
|
|
|
if (!d->m_fadeIn && progress == 1 ) {
|
|
|
|
d->states &= ~Private::HoverState;
|
|
|
|
}
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2007-12-16 11:09:07 +01:00
|
|
|
void Icon::Private::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) {
|
2007-08-01 02:52:26 +02:00
|
|
|
case Private::HoverState:
|
2008-01-03 19:38:11 +01:00
|
|
|
shadow.setHsv(shadow.hue(),
|
|
|
|
shadow.saturation(),
|
2008-03-03 20:35:24 +01:00
|
|
|
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;
|
|
|
|
case Private::PressedState:
|
2008-01-03 19:38:11 +01:00
|
|
|
shadow.setHsv(shadow.hue(),
|
|
|
|
shadow.saturation(),
|
2008-02-27 01:50:07 +01:00
|
|
|
shadow.value() + (darkShadow?(int)(50*m_hoverAlpha):(int)(-50*m_hoverAlpha)),
|
2008-01-24 19:35:22 +01:00
|
|
|
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-03-03 20:35:24 +01:00
|
|
|
|
|
|
|
border.setAlphaF(.2);
|
|
|
|
shadow.setAlphaF(.6);
|
|
|
|
|
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-02-27 01:50:07 +01:00
|
|
|
painter->drawPath(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
|
|
|
}
|
|
|
|
|
2007-12-20 17:28:47 +01:00
|
|
|
QPixmap Icon::Private::decoration(const QStyleOptionGraphicsItem *option, bool useHoverEffect)
|
2007-08-05 19:11:46 +02:00
|
|
|
{
|
|
|
|
QPixmap result;
|
|
|
|
|
|
|
|
QIcon::Mode mode = option->state & QStyle::State_Enabled ? QIcon::Normal : QIcon::Disabled;
|
|
|
|
QIcon::State state = option->state & QStyle::State_Open ? QIcon::On : QIcon::Off;
|
|
|
|
|
2007-12-20 17:28:47 +01:00
|
|
|
if (iconSvg) {
|
|
|
|
if (iconSvgPixmap.size() != iconSize.toSize()) {
|
|
|
|
QImage img(iconSize.toSize(), QImage::Format_ARGB32_Premultiplied);
|
|
|
|
{
|
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
|
|
|
|
if (!result.isNull() && useHoverEffect && !drawBg) {
|
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)) {
|
|
|
|
result = effect->apply(result, KIconLoader::Desktop, KIconLoader::ActiveState);
|
2007-08-05 19:11:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
QPointF Icon::Private::iconPosition(const QStyleOptionGraphicsItem *option, const QPixmap &pixmap) const
|
|
|
|
{
|
|
|
|
const QRectF itemRect = subtractMargin(option->rect, Private::ItemMargin);
|
|
|
|
|
|
|
|
// Compute the nominal decoration rectangle
|
|
|
|
const QSizeF size = addMargin(iconSize, Private::IconMargin);
|
2007-12-28 12:29:56 +01:00
|
|
|
|
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;
|
|
|
|
}else if (orientation == Qt::Vertical) {
|
|
|
|
alignment = Qt::Alignment(Qt::AlignHCenter | Qt::AlignTop);
|
|
|
|
//Horizontal
|
|
|
|
}else{
|
2008-01-01 21:58:36 +01:00
|
|
|
alignment = QStyle::visualAlignment(direction, Qt::Alignment(Qt::AlignLeft | Qt::AlignVCenter));
|
2007-12-28 12:29:56 +01:00
|
|
|
}
|
|
|
|
|
2008-01-01 21:58:36 +01: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());
|
|
|
|
}
|
|
|
|
|
|
|
|
QRectF Icon::Private::labelRectangle(const QStyleOptionGraphicsItem *option, const QPixmap &icon,
|
|
|
|
const QString &string) const
|
|
|
|
{
|
|
|
|
Q_UNUSED(string)
|
|
|
|
|
2007-11-30 02:06:27 +01:00
|
|
|
if (icon.isNull()) {
|
2007-08-05 19:11:46 +02:00
|
|
|
return option->rect;
|
2007-11-30 02:06:27 +01:00
|
|
|
}
|
2007-08-05 19:11:46 +02:00
|
|
|
|
2008-03-03 20:35:24 +01:00
|
|
|
const QSizeF decoSize = addMargin(iconSize, Private::IconMargin);
|
2007-08-05 19:11:46 +02:00
|
|
|
const QRectF itemRect = subtractMargin(option->rect, Private::ItemMargin);
|
|
|
|
QRectF textArea(QPointF(0, 0), itemRect.size());
|
|
|
|
|
2007-12-28 12:29:56 +01:00
|
|
|
if (orientation == Qt::Vertical) {
|
|
|
|
textArea.setTop(decoSize.height() + 1);
|
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
|
|
|
|
QSizeF Icon::Private::layoutText(QTextLayout &layout, const QStyleOptionGraphicsItem *option,
|
|
|
|
const QString &text, const QSizeF &constraints) const
|
|
|
|
{
|
|
|
|
const QSizeF size = layoutText(layout, text, constraints.width());
|
|
|
|
|
|
|
|
if (size.width() > constraints.width() || size.height() > constraints.height())
|
|
|
|
{
|
|
|
|
const QString elided = elidedText(layout, option, constraints);
|
|
|
|
return layoutText(layout, elided, constraints.width());
|
|
|
|
}
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lays the text out in a rectangle no wider than maxWidth
|
|
|
|
QSizeF Icon::Private::layoutText(QTextLayout &layout, const QString &text, qreal maxWidth) const
|
|
|
|
{
|
|
|
|
QFontMetricsF metrics(layout.font());
|
|
|
|
qreal leading = metrics.leading();
|
|
|
|
qreal height = 0.0;
|
|
|
|
qreal widthUsed = 0.0;
|
|
|
|
QTextLine line;
|
|
|
|
|
|
|
|
layout.setText(text);
|
|
|
|
|
|
|
|
layout.beginLayout();
|
|
|
|
|
|
|
|
while ((line = layout.createLine()).isValid())
|
|
|
|
{
|
|
|
|
line.setLineWidth(maxWidth);
|
|
|
|
height += leading;
|
|
|
|
line.setPosition(QPointF(0.0, height));
|
|
|
|
height += line.height();
|
|
|
|
widthUsed = qMax(widthUsed, line.naturalTextWidth());
|
|
|
|
}
|
|
|
|
layout.endLayout();
|
|
|
|
|
|
|
|
return QSizeF(widthUsed, height);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Elides the text in the layout, by iterating over each line in the layout, eliding
|
|
|
|
// or word breaking the line if it's wider than the max width, and finally adding an
|
|
|
|
// ellipses at the end of the last line, if there are more lines than will fit within
|
|
|
|
// the vertical size constraints.
|
|
|
|
QString Icon::Private::elidedText(QTextLayout &layout, const QStyleOptionGraphicsItem *option,
|
|
|
|
const QSizeF &size) const
|
|
|
|
{
|
|
|
|
Q_UNUSED(option)
|
|
|
|
|
|
|
|
QFontMetricsF metrics(layout.font());
|
|
|
|
const QString text = layout.text();
|
|
|
|
qreal maxWidth = size.width();
|
|
|
|
qreal maxHeight = size.height();
|
|
|
|
qreal height = 0;
|
|
|
|
|
|
|
|
// Elide each line that has already been laid out in the layout.
|
|
|
|
QString elided;
|
|
|
|
elided.reserve(text.length());
|
|
|
|
|
|
|
|
for (int i = 0; i < layout.lineCount(); i++)
|
|
|
|
{
|
|
|
|
QTextLine line = layout.lineAt(i);
|
|
|
|
int start = line.textStart();
|
|
|
|
int length = line.textLength();
|
|
|
|
|
|
|
|
height += metrics.leading();
|
|
|
|
if (height + line.height() + metrics.lineSpacing() > maxHeight)
|
|
|
|
{
|
|
|
|
// Unfortunately, if the line ends because of a line separator, elidedText() will be too
|
|
|
|
// clever and keep adding lines until it finds one that's too wide.
|
2007-12-26 14:44:57 +01:00
|
|
|
if (line.naturalTextWidth() < maxWidth && start+length > 0 && text[start + length - 1] == QChar::LineSeparator)
|
2007-08-05 19:11:46 +02:00
|
|
|
elided += text.mid(start, length - 1);
|
|
|
|
else
|
|
|
|
elided += metrics.elidedText(text.mid(start), Qt::ElideRight, maxWidth);
|
|
|
|
break;
|
2007-08-01 03:25:01 +02:00
|
|
|
}
|
2007-08-05 19:11:46 +02:00
|
|
|
else if (line.naturalTextWidth() > maxWidth)
|
|
|
|
elided += metrics.elidedText(text.mid(start, length), Qt::ElideRight, maxWidth);
|
|
|
|
else
|
|
|
|
elided += text.mid(start, length);
|
|
|
|
|
|
|
|
height += line.height();
|
2007-08-01 03:25:01 +02:00
|
|
|
}
|
2007-08-05 19:11:46 +02:00
|
|
|
|
|
|
|
return elided;
|
2007-08-01 03:25:01 +02:00
|
|
|
}
|
|
|
|
|
2007-08-05 19:11:46 +02:00
|
|
|
void Icon::Private::layoutTextItems(const QStyleOptionGraphicsItem *option,
|
|
|
|
const QPixmap &icon, QTextLayout *labelLayout,
|
|
|
|
QTextLayout *infoLayout, QRectF *textBoundingRect) const
|
2007-08-01 03:25:01 +02:00
|
|
|
{
|
2007-08-05 19:11:46 +02:00
|
|
|
bool showInformation = false;
|
|
|
|
|
|
|
|
setLayoutOptions(*labelLayout, option);
|
|
|
|
|
|
|
|
QFontMetricsF fm(labelLayout->font());
|
|
|
|
const QRectF textArea = labelRectangle(option, icon, text);
|
2008-03-03 20:35:24 +01:00
|
|
|
QRectF textRect = subtractMargin(textArea, Private::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
|
|
|
|
if (!infoText.isEmpty() && textRect.height() >= fm.lineSpacing() * 2)
|
|
|
|
{
|
|
|
|
infoLayout->setFont(labelLayout->font());
|
|
|
|
infoLayout->setTextOption(labelLayout->textOption());
|
|
|
|
|
|
|
|
maxLabelSize.rheight() -= fm.lineSpacing();
|
|
|
|
showInformation = true;
|
2007-08-01 03:25:01 +02:00
|
|
|
}
|
2007-08-05 19:11:46 +02:00
|
|
|
|
|
|
|
// Lay out the label text, and adjust the max info size based on the label size
|
|
|
|
labelSize = layoutText(*labelLayout, option, text, maxLabelSize);
|
|
|
|
maxInfoSize.rheight() -= labelSize.height();
|
|
|
|
|
|
|
|
// Lay out the info text
|
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();
|
|
|
|
const QSizeF size(qMax(labelSize.width(), infoSize.width()), labelSize.height() + infoSize.height());
|
2008-01-01 21:58:36 +01:00
|
|
|
*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
|
|
|
}
|
|
|
|
|
2007-08-05 19:11:46 +02:00
|
|
|
QBrush Icon::Private::foregroundBrush(const QStyleOptionGraphicsItem *option) const
|
|
|
|
{
|
|
|
|
const QPalette::ColorGroup group = option->state & QStyle::State_Enabled ?
|
|
|
|
QPalette::Normal : QPalette::Disabled;
|
|
|
|
|
|
|
|
// Always use the highlight color for selected items
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
QBrush Icon::Private::backgroundBrush(const QStyleOptionGraphicsItem *option) const
|
|
|
|
{
|
|
|
|
const QPalette::ColorGroup group = option->state & QStyle::State_Enabled ?
|
|
|
|
QPalette::Normal : QPalette::Disabled;
|
|
|
|
|
|
|
|
QBrush background(Qt::NoBrush);
|
|
|
|
|
|
|
|
// Always use the highlight color for selected items
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Icon::Private::drawTextItems(QPainter *painter, const QStyleOptionGraphicsItem *option,
|
|
|
|
const QTextLayout &labelLayout, const QTextLayout &infoLayout) const
|
|
|
|
{
|
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
|
|
|
{
|
|
|
|
Q_UNUSED(widget)
|
|
|
|
|
2007-07-21 06:07:41 +02:00
|
|
|
#ifdef BACKINGSTORE_BLUR_HACK
|
2007-06-17 19:49:49 +02:00
|
|
|
if (d->state == Private::HoverState && scene()) {
|
|
|
|
QList<QGraphicsView*> views = scene()->views();
|
|
|
|
if (views.count() > 0) {
|
|
|
|
QPixmap* pix = static_cast<QPixmap*>(views[0]->windowSurface()->paintDevice());
|
|
|
|
QImage image(boundingRect().size().toSize(), QImage::Format_ARGB32_Premultiplied);
|
|
|
|
{
|
|
|
|
QPainter p(&image);
|
|
|
|
p.drawPixmap(image.rect(), *pix, sceneBoundingRect());
|
|
|
|
}
|
|
|
|
expblur<16,7>(image, 8);
|
|
|
|
painter->drawImage(0, 0, image);
|
|
|
|
}
|
|
|
|
}
|
2007-06-08 07:39:34 +02:00
|
|
|
#endif
|
2007-06-07 21:07:45 +02:00
|
|
|
|
2007-12-26 13:37:54 +01:00
|
|
|
//Lay out the main icon and action icons
|
|
|
|
layoutIcons(option);
|
2007-08-05 19:11:46 +02:00
|
|
|
|
|
|
|
// Compute the metrics, and lay out the text items
|
|
|
|
// ========================================================================
|
2007-12-16 11:09:07 +01:00
|
|
|
Private::IconState state = Private::NoState;
|
|
|
|
if (d->states & Private::ManualPressedState) {
|
|
|
|
state = Private::PressedState;
|
|
|
|
} else if (d->states & Private::PressedState) {
|
|
|
|
if (d->states & Private::HoverState) {
|
|
|
|
state = Private::PressedState;
|
|
|
|
}
|
|
|
|
} else if (d->states & Private::HoverState) {
|
|
|
|
state = Private::HoverState;
|
|
|
|
}
|
|
|
|
|
2008-03-03 20:35:24 +01:00
|
|
|
QPixmap icon = d->decoration(option, state != Private::NoState);
|
2007-08-05 19:11:46 +02:00
|
|
|
const QPointF iconPos = d->iconPosition(option, icon);
|
|
|
|
|
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);
|
2007-08-05 19:11:46 +02:00
|
|
|
d->drawTextItems(painter, option, labelLayout, infoLayout);
|
2007-05-29 09:59:50 +02:00
|
|
|
}
|
|
|
|
|
2007-07-21 06:07:41 +02:00
|
|
|
void Icon::drawActionButtonBase(QPainter* painter, const QSize &size, int element)
|
|
|
|
{
|
2008-01-03 19:38:11 +01:00
|
|
|
qreal radius = size.width()/2;
|
|
|
|
QRadialGradient gradient(radius, radius, radius, radius, radius);
|
|
|
|
int alpha;
|
2007-07-21 06:07:41 +02:00
|
|
|
|
2008-01-03 19:38:11 +01:00
|
|
|
if (element == Private::MinibuttonPressed) {
|
|
|
|
alpha = 255;
|
|
|
|
} else if (element == Private::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));
|
2007-07-21 06:07:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-05-29 09:59:50 +02:00
|
|
|
void Icon::setText(const QString& text)
|
|
|
|
{
|
|
|
|
d->text = text;
|
2008-01-07 06:09:09 +01:00
|
|
|
// cause a relayout
|
|
|
|
d->currentSize = QSizeF(-1, -1);
|
2007-05-29 09:59:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QString Icon::text() const
|
|
|
|
{
|
|
|
|
return d->text;
|
|
|
|
}
|
|
|
|
|
2007-07-28 21:27:56 +02:00
|
|
|
void Icon::setInfoText(const QString& text)
|
|
|
|
{
|
|
|
|
d->infoText = text;
|
2008-01-07 06:09:09 +01:00
|
|
|
// cause a relayout
|
|
|
|
d->currentSize = QSizeF(-1, -1);
|
2007-07-28 21:27:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QString Icon::infoText() const
|
|
|
|
{
|
|
|
|
return d->infoText;
|
|
|
|
}
|
|
|
|
|
2007-08-11 00:24:49 +02:00
|
|
|
QIcon Icon::icon() const
|
2007-08-05 20:03:26 +02:00
|
|
|
{
|
|
|
|
return d->icon;
|
|
|
|
}
|
|
|
|
|
2007-05-29 09:59:50 +02:00
|
|
|
void Icon::setIcon(const QString& icon)
|
|
|
|
{
|
|
|
|
if (icon.isEmpty()) {
|
2007-08-11 00:24:49 +02:00
|
|
|
setIcon(QIcon());
|
2007-05-29 09:59:50 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-08-11 05:30:54 +02:00
|
|
|
setIcon(KIcon(icon));
|
2007-05-29 09:59:50 +02:00
|
|
|
}
|
|
|
|
|
2007-08-11 00:24:49 +02:00
|
|
|
void Icon::setIcon(const QIcon& icon)
|
2007-05-29 09:59:50 +02:00
|
|
|
{
|
|
|
|
d->icon = icon;
|
|
|
|
}
|
|
|
|
|
2007-06-05 04:54:48 +02:00
|
|
|
QSizeF Icon::iconSize() const
|
|
|
|
{
|
2007-06-05 21:48:14 +02:00
|
|
|
return d->iconSize;
|
2007-06-05 04:54:48 +02:00
|
|
|
}
|
|
|
|
|
2007-05-29 09:59:50 +02:00
|
|
|
bool Icon::isDown()
|
|
|
|
{
|
2007-12-16 11:09:07 +01:00
|
|
|
return d->states & Private::PressedState;
|
2007-05-29 09:59:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Icon::mousePressEvent(QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
2007-10-30 21:59:12 +01:00
|
|
|
if (event->button() != Qt::LeftButton) {
|
2008-04-13 23:47:47 +02:00
|
|
|
QGraphicsWidget::mousePressEvent(event);
|
2007-10-30 21:59:12 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-12-16 11:09:07 +01:00
|
|
|
d->states |= Private::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
|
|
|
|
2007-12-16 11:09:07 +01:00
|
|
|
if (!handled) {
|
|
|
|
emit pressed(true);
|
|
|
|
}
|
2007-08-05 19:11:46 +02:00
|
|
|
|
2007-07-28 21:27:56 +02:00
|
|
|
update();
|
2007-05-29 09:59:50 +02:00
|
|
|
}
|
|
|
|
|
2007-12-16 11:09:07 +01:00
|
|
|
void Icon::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
|
2007-05-29 09:59:50 +02:00
|
|
|
{
|
2007-12-16 11:09:07 +01:00
|
|
|
if (~d->states & Private::PressedState) {
|
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())) {
|
|
|
|
if (~d->states & Private::HoverState) {
|
|
|
|
d->states |= Private::HoverState;
|
|
|
|
update();
|
2007-07-21 06:07:41 +02:00
|
|
|
}
|
2007-10-22 18:16:30 +02:00
|
|
|
} else {
|
2007-12-16 11:09:07 +01:00
|
|
|
if (d->states & Private::HoverState) {
|
|
|
|
d->states &= ~Private::HoverState;
|
|
|
|
update();
|
|
|
|
}
|
2007-07-21 06:07:41 +02:00
|
|
|
}
|
2007-12-16 11:09:07 +01:00
|
|
|
}
|
2007-10-22 18:16:30 +02:00
|
|
|
|
2007-12-16 11:09:07 +01:00
|
|
|
void Icon::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
|
|
|
if (~d->states & Private::PressedState) {
|
2008-04-13 23:47:47 +02:00
|
|
|
QGraphicsWidget::mouseMoveEvent(event);
|
2007-12-16 11:09:07 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
d->states &= ~Private::PressedState;
|
|
|
|
|
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
|
|
|
}
|
2007-12-16 11:09:07 +01:00
|
|
|
d->states |= Private::HoverState;
|
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-02-27 01:50:07 +01:00
|
|
|
// d->states &= ~Private::HoverState; // Will be set once progress is zero again ...
|
|
|
|
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) {
|
|
|
|
d->states |= Private::ManualPressedState;
|
2008-01-12 18:07:19 +01:00
|
|
|
d->states |= Private::PressedState;
|
2007-12-16 11:09:07 +01:00
|
|
|
} else {
|
|
|
|
d->states &= ~Private::ManualPressedState;
|
2008-01-12 18:07:19 +01:00
|
|
|
d->states &= ~Private::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-05-24 03:48:36 +02:00
|
|
|
void Icon::Private::syncToAction()
|
|
|
|
{
|
|
|
|
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
|
|
|
|
return d->addMargin(d->addMargin(QSizeF(iconWidth, iconWidth), Private::IconMargin),
|
2007-12-23 11:51:00 +01:00
|
|
|
Private::ItemMargin);
|
|
|
|
}
|
|
|
|
|
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") +
|
|
|
|
d->horizontalMargin[Private::TextMargin].left +
|
|
|
|
d->horizontalMargin[Private::TextMargin].right;
|
|
|
|
|
2007-12-28 12:29:56 +01:00
|
|
|
width = qMax(width,
|
|
|
|
iconWidth +
|
|
|
|
d->horizontalMargin[Private::IconMargin].left +
|
|
|
|
d->horizontalMargin[Private::IconMargin].right);
|
2008-03-03 20:35:24 +01:00
|
|
|
} else {
|
|
|
|
width = iconWidth +
|
|
|
|
d->horizontalMargin[Private::IconMargin].left +
|
|
|
|
d->horizontalMargin[Private::IconMargin].right +
|
|
|
|
qMax(fm.width(d->text), fm.width(d->infoText)) + fm.width("xx") +
|
|
|
|
d->horizontalMargin[Private::TextMargin].left +
|
|
|
|
d->horizontalMargin[Private::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 +
|
|
|
|
d->verticalMargin[Private::TextMargin].top +
|
|
|
|
d->verticalMargin[Private::TextMargin].bottom +
|
|
|
|
d->verticalMargin[Private::IconMargin].top +
|
|
|
|
d->verticalMargin[Private::IconMargin].bottom;
|
2008-03-03 20:35:24 +01:00
|
|
|
} else {
|
|
|
|
//Horizontal
|
2007-12-28 12:29:56 +01:00
|
|
|
height = qMax(iconWidth +
|
|
|
|
d->verticalMargin[Private::IconMargin].top +
|
|
|
|
d->verticalMargin[Private::IconMargin].bottom,
|
|
|
|
textHeight +
|
|
|
|
d->verticalMargin[Private::TextMargin].top +
|
2008-03-03 20:35:24 +01:00
|
|
|
d->verticalMargin[Private::TextMargin].bottom);
|
2007-12-28 12:29:56 +01:00
|
|
|
}
|
2007-12-23 11:51:00 +01:00
|
|
|
|
|
|
|
return d->addMargin(QSizeF(width, height), Private::ItemMargin);
|
|
|
|
}
|
|
|
|
|
2007-05-29 09:59:50 +02:00
|
|
|
} // namespace Plasma
|
|
|
|
|
|
|
|
#include "icon.moc"
|