2008-11-04 00:08:39 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2007 by Aaron Seigo <aseigo@kde.org>
|
|
|
|
* Copyright (C) 2007 by Matt Broadstone <mbroadst@gmail.com>
|
|
|
|
* Copyright (C) 2006-2007 Fredrik Höglund <fredrik@kde.org>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
#ifndef PLASMA_ICONWIDGET_P_H
|
|
|
|
#define PLASMA_ICONWIDGET_P_H
|
|
|
|
|
|
|
|
#include <QtCore/QEvent>
|
2010-02-05 22:46:51 +01:00
|
|
|
#include <QtCore/QWeakPointer>
|
2008-11-04 00:08:39 +01:00
|
|
|
#include <QtGui/QApplication>
|
|
|
|
#include <QtGui/QIcon>
|
|
|
|
#include <QtGui/QStyleOptionGraphicsItem>
|
|
|
|
#include <QtGui/QTextLayout>
|
|
|
|
#include <QtGui/QTextOption>
|
|
|
|
|
|
|
|
#include "iconwidget.h"
|
2010-10-22 21:54:40 +02:00
|
|
|
#include "framesvg.h"
|
2009-04-24 09:40:52 +02:00
|
|
|
#include "private/actionwidgetinterface_p.h"
|
2010-10-22 21:54:40 +02:00
|
|
|
#include "theme.h"
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
class QAction;
|
|
|
|
class QPainter;
|
|
|
|
class QTextLayout;
|
2010-02-05 22:46:51 +01:00
|
|
|
class QPropertyAnimation;
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
2010-02-05 22:46:51 +01:00
|
|
|
class Animation;
|
|
|
|
class IconHoverAnimation;
|
|
|
|
|
2010-05-18 20:04:51 +02:00
|
|
|
class IconHoverAnimation : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
Q_PROPERTY(qreal value READ value WRITE setValue)
|
|
|
|
|
|
|
|
public:
|
|
|
|
IconHoverAnimation(QObject *parent = 0);
|
|
|
|
|
|
|
|
qreal value() const;
|
|
|
|
|
|
|
|
bool fadeIn() const;
|
|
|
|
void setFadeIn(bool fadeIn);
|
|
|
|
|
|
|
|
QPropertyAnimation *animation() const;
|
|
|
|
void setAnimation(QPropertyAnimation *animation);
|
|
|
|
|
|
|
|
protected slots:
|
|
|
|
void setValue(qreal value);
|
|
|
|
|
|
|
|
private:
|
|
|
|
qreal m_value;
|
|
|
|
bool m_fadeIn;
|
|
|
|
QWeakPointer<QPropertyAnimation> m_animation;
|
|
|
|
};
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
class PLASMA_EXPORT IconAction
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
IconAction(IconWidget *icon, QAction *action);
|
|
|
|
|
|
|
|
void show();
|
|
|
|
void hide();
|
|
|
|
bool isVisible() const;
|
2010-02-05 22:46:51 +01:00
|
|
|
bool isAnimating() const;
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
QAction *action() const;
|
|
|
|
|
|
|
|
void paint(QPainter *painter) const;
|
|
|
|
bool event(QEvent::Type type, const QPointF &pos);
|
|
|
|
|
|
|
|
void setSelected(bool selected);
|
|
|
|
bool isSelected() const;
|
|
|
|
|
|
|
|
bool isHovered() const;
|
|
|
|
bool isPressed() const;
|
|
|
|
|
|
|
|
void setRect(const QRectF &rect);
|
|
|
|
QRectF rect() const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
void rebuildPixmap();
|
|
|
|
|
|
|
|
IconWidget *m_icon;
|
|
|
|
QAction *m_action;
|
|
|
|
QPixmap m_pixmap;
|
|
|
|
QRectF m_rect;
|
|
|
|
|
|
|
|
bool m_hovered;
|
|
|
|
bool m_pressed;
|
|
|
|
bool m_selected;
|
|
|
|
bool m_visible;
|
|
|
|
|
2010-02-05 22:46:51 +01:00
|
|
|
QWeakPointer<Animation> m_animation;
|
2008-11-04 00:08:39 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct Margin
|
|
|
|
{
|
|
|
|
qreal left, right, top, bottom;
|
|
|
|
};
|
|
|
|
|
2009-04-24 09:40:52 +02:00
|
|
|
class IconWidgetPrivate : public ActionWidgetInterface<IconWidget>
|
2008-11-04 00:08:39 +01:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum MarginType {
|
|
|
|
ItemMargin = 0,
|
|
|
|
TextMargin,
|
|
|
|
IconMargin,
|
|
|
|
NMargins
|
|
|
|
};
|
|
|
|
|
|
|
|
enum IconWidgetState {
|
|
|
|
NoState = 0,
|
|
|
|
HoverState = 1,
|
|
|
|
PressedState = 2,
|
|
|
|
ManualPressedState = 4
|
|
|
|
};
|
|
|
|
Q_DECLARE_FLAGS(IconWidgetStates, IconWidgetState)
|
|
|
|
|
|
|
|
IconWidgetPrivate(IconWidget *i);
|
|
|
|
~IconWidgetPrivate();
|
|
|
|
|
2009-04-24 09:40:52 +02:00
|
|
|
void changed()
|
|
|
|
{
|
|
|
|
emit q->changed();
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
void drawBackground(QPainter *painter, IconWidgetState state);
|
|
|
|
void drawText(QPainter *painter);
|
|
|
|
void drawTextItems(QPainter *painter, const QStyleOptionGraphicsItem *option,
|
|
|
|
const QTextLayout &labelLayout, const QTextLayout &infoLayout) const;
|
|
|
|
|
2009-03-30 08:42:19 +02:00
|
|
|
QPixmap decoration(const QStyleOptionGraphicsItem *option, bool useHoverEffect, bool usePressedEffect);
|
2008-11-04 00:08:39 +01:00
|
|
|
QPointF iconPosition(const QStyleOptionGraphicsItem *option, const QPixmap &pixmap) const;
|
|
|
|
|
|
|
|
QSizeF displaySizeHint(const QStyleOptionGraphicsItem *option, const qreal width) const;
|
|
|
|
|
|
|
|
QBrush foregroundBrush(const QStyleOptionGraphicsItem *option) const;
|
|
|
|
QBrush backgroundBrush(const QStyleOptionGraphicsItem *option) const;
|
|
|
|
|
|
|
|
QString elidedText(QTextLayout &layout,
|
|
|
|
const QStyleOptionGraphicsItem *option,
|
|
|
|
const QSizeF &maxSize) const;
|
|
|
|
|
|
|
|
QSizeF layoutText(QTextLayout &layout,
|
|
|
|
const QStyleOptionGraphicsItem *option,
|
|
|
|
const QString &text, const QSizeF &constraints) const;
|
|
|
|
|
|
|
|
QSizeF layoutText(QTextLayout &layout, const QString &text,
|
|
|
|
qreal maxWidth) const;
|
|
|
|
|
|
|
|
QRectF labelRectangle(const QStyleOptionGraphicsItem *option,
|
|
|
|
const QPixmap &icon, const QString &string) const;
|
|
|
|
|
|
|
|
void layoutTextItems(const QStyleOptionGraphicsItem *option,
|
|
|
|
const QPixmap &icon, QTextLayout *labelLayout,
|
|
|
|
QTextLayout *infoLayout, QRectF *textBoundingRect) const;
|
|
|
|
|
2009-09-10 21:55:29 +02:00
|
|
|
int maxWordWidth(const QString text) const;
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
inline void setLayoutOptions(QTextLayout &layout,
|
2009-02-09 11:51:21 +01:00
|
|
|
const QStyleOptionGraphicsItem *options,
|
|
|
|
const Qt::Orientation orientation) const;
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
inline Qt::LayoutDirection iconDirection(const QStyleOptionGraphicsItem *option) const;
|
|
|
|
|
|
|
|
enum {
|
|
|
|
Minibutton = 64,
|
|
|
|
MinibuttonHover = 128,
|
|
|
|
MinibuttonPressed = 256
|
|
|
|
};
|
|
|
|
|
|
|
|
enum ActionPosition {
|
|
|
|
TopLeft = 0,
|
|
|
|
TopRight,
|
|
|
|
BottomLeft,
|
|
|
|
BottomRight,
|
2009-10-15 04:39:38 +02:00
|
|
|
LastIconPosition = BottomRight
|
2008-11-04 00:08:39 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Margin functions
|
|
|
|
inline void setActiveMargins();
|
|
|
|
void setVerticalMargin(MarginType type, qreal left, qreal right, qreal top, qreal bottom);
|
|
|
|
void setHorizontalMargin(MarginType type, qreal left, qreal right, qreal top, qreal bottom);
|
|
|
|
inline void setVerticalMargin(MarginType type, qreal hor, qreal ver);
|
|
|
|
inline void setHorizontalMargin(MarginType type, qreal hor, qreal ver);
|
|
|
|
inline QRectF addMargin(const QRectF &rect, MarginType type) const;
|
|
|
|
inline QRectF subtractMargin(const QRectF &rect, MarginType type) const;
|
|
|
|
inline QSizeF addMargin(const QSizeF &size, MarginType type) const;
|
|
|
|
inline QSizeF subtractMargin(const QSizeF &size, MarginType type) const;
|
|
|
|
inline QRectF actionRect(ActionPosition position) const;
|
|
|
|
|
2009-04-24 09:40:52 +02:00
|
|
|
void actionDestroyed(QObject *obj);
|
2009-01-05 18:34:40 +01:00
|
|
|
void svgChanged();
|
2008-11-04 00:08:39 +01:00
|
|
|
|
2009-01-06 22:11:54 +01:00
|
|
|
void readColors();
|
2009-01-20 03:37:52 +01:00
|
|
|
void colorConfigChanged();
|
|
|
|
void iconConfigChanged();
|
2010-02-05 22:46:51 +01:00
|
|
|
void hoverAnimationFinished();
|
2009-01-06 22:11:54 +01:00
|
|
|
void init();
|
|
|
|
void layoutIcons(const QStyleOptionGraphicsItem *option);
|
2010-02-16 18:24:35 +01:00
|
|
|
void animateMainIcon(bool, const IconWidgetStates state);
|
2009-01-06 22:11:54 +01:00
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
QString text;
|
|
|
|
QString infoText;
|
|
|
|
Svg *iconSvg;
|
2009-01-19 21:41:45 +01:00
|
|
|
FrameSvg *background;
|
2008-11-04 00:08:39 +01:00
|
|
|
QString iconSvgElement;
|
|
|
|
QPixmap iconSvgPixmap;
|
|
|
|
QColor textColor;
|
2009-04-10 23:16:14 +02:00
|
|
|
QColor textBgColor;
|
2008-11-04 00:08:39 +01:00
|
|
|
QColor shadowColor;
|
2010-02-05 22:46:51 +01:00
|
|
|
IconHoverAnimation *hoverAnimation;
|
2008-11-04 00:08:39 +01:00
|
|
|
QSizeF iconSize;
|
2010-04-24 14:08:13 +02:00
|
|
|
QSizeF preferredIconSize;
|
2010-04-26 16:17:08 +02:00
|
|
|
QSizeF minimumIconSize;
|
|
|
|
QSizeF maximumIconSize;
|
2008-11-04 00:08:39 +01:00
|
|
|
QIcon icon;
|
2010-02-16 18:24:35 +01:00
|
|
|
QIcon oldIcon;
|
2008-11-04 00:08:39 +01:00
|
|
|
IconWidgetStates states;
|
|
|
|
Qt::Orientation orientation;
|
|
|
|
int numDisplayLines;
|
|
|
|
QSizeF currentSize;
|
|
|
|
QPointF clickStartPos;
|
2009-11-26 22:57:36 +01:00
|
|
|
mutable QList<QRect> haloRects;
|
2010-02-20 15:19:29 +01:00
|
|
|
QTimer *iconChangeTimer;
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
QList<IconAction*> cornerActions;
|
|
|
|
|
|
|
|
Margin verticalMargin[NMargins];
|
|
|
|
Margin horizontalMargin[NMargins];
|
|
|
|
Margin *activeMargins;
|
|
|
|
|
2009-04-10 23:16:14 +02:00
|
|
|
bool iconSvgElementChanged : 1;
|
|
|
|
bool invertLayout : 1;
|
|
|
|
bool drawBg : 1;
|
|
|
|
bool textBgCustomized : 1;
|
2008-11-04 00:08:39 +01:00
|
|
|
static const int maxDisplayLines = 5;
|
|
|
|
static const int iconActionSize = 26;
|
|
|
|
static const int iconActionMargin = 4;
|
2009-09-13 15:13:55 +02:00
|
|
|
bool customFont;
|
2008-11-04 00:08:39 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
Q_DECLARE_OPERATORS_FOR_FLAGS(IconWidgetPrivate::IconWidgetStates)
|
|
|
|
|
|
|
|
// Inline methods
|
|
|
|
void IconWidgetPrivate::setLayoutOptions(QTextLayout &layout,
|
2009-02-09 11:51:21 +01:00
|
|
|
const QStyleOptionGraphicsItem *option,
|
|
|
|
const Qt::Orientation orientation) const
|
2008-11-04 00:08:39 +01:00
|
|
|
{
|
|
|
|
QTextOption textoption;
|
|
|
|
textoption.setTextDirection(option->direction);
|
2009-02-09 11:51:21 +01:00
|
|
|
if (orientation == Qt::Horizontal) {
|
|
|
|
textoption.setAlignment(Qt::Alignment(Qt::AlignLeft|Qt::AlignVCenter));
|
|
|
|
} else {
|
|
|
|
textoption.setAlignment(Qt::AlignCenter);
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
textoption.setWrapMode(QTextOption::WordWrap); // NOTE: assumption as well
|
|
|
|
|
2010-10-22 14:29:35 +02:00
|
|
|
layout.setFont(q->font());
|
2008-11-04 00:08:39 +01:00
|
|
|
layout.setTextOption(textoption);
|
|
|
|
}
|
|
|
|
|
|
|
|
Qt::LayoutDirection IconWidgetPrivate::iconDirection(const QStyleOptionGraphicsItem *option) const
|
|
|
|
{
|
|
|
|
Qt::LayoutDirection direction;
|
|
|
|
|
|
|
|
if (invertLayout && orientation == Qt::Horizontal) {
|
|
|
|
if (option->direction == Qt::LeftToRight) {
|
|
|
|
direction = Qt::RightToLeft;
|
|
|
|
} else {
|
|
|
|
direction = Qt::LeftToRight;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
direction = option->direction;
|
|
|
|
}
|
|
|
|
|
|
|
|
return direction;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IconWidgetPrivate::setActiveMargins()
|
|
|
|
{
|
2009-09-10 21:55:29 +02:00
|
|
|
//sync here itemmargin with contentsrect, not overly pretty but it's where it's more reliable
|
|
|
|
qreal left, top, right, bottom;
|
|
|
|
q->getContentsMargins(&left, &top, &right, &bottom);
|
|
|
|
if (left || top || right || bottom) {
|
|
|
|
verticalMargin[ItemMargin].left = horizontalMargin[ItemMargin].left = left;
|
|
|
|
verticalMargin[ItemMargin].top = horizontalMargin[ItemMargin].top = top;
|
|
|
|
verticalMargin[ItemMargin].right = horizontalMargin[ItemMargin].right = right;
|
|
|
|
verticalMargin[ItemMargin].bottom = horizontalMargin[ItemMargin].bottom = bottom;
|
|
|
|
}
|
2008-12-16 00:42:29 +01:00
|
|
|
activeMargins = (orientation == Qt::Horizontal ? horizontalMargin : verticalMargin);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void IconWidgetPrivate::setVerticalMargin(MarginType type, qreal left, qreal top,
|
|
|
|
qreal right, qreal bottom)
|
|
|
|
{
|
|
|
|
verticalMargin[type].left = left;
|
|
|
|
verticalMargin[type].right = right;
|
|
|
|
verticalMargin[type].top = top;
|
|
|
|
verticalMargin[type].bottom = bottom;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IconWidgetPrivate::setHorizontalMargin(MarginType type, qreal left, qreal top,
|
|
|
|
qreal right, qreal bottom)
|
|
|
|
{
|
|
|
|
horizontalMargin[type].left = left;
|
|
|
|
horizontalMargin[type].right = right;
|
|
|
|
horizontalMargin[type].top = top;
|
|
|
|
horizontalMargin[type].bottom = bottom;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IconWidgetPrivate::setVerticalMargin(MarginType type, qreal horizontal, qreal vertical)
|
|
|
|
{
|
|
|
|
setVerticalMargin(type, horizontal, vertical, horizontal, vertical);
|
|
|
|
}
|
|
|
|
|
|
|
|
void IconWidgetPrivate::setHorizontalMargin(MarginType type, qreal horizontal, qreal vertical)
|
|
|
|
{
|
|
|
|
setHorizontalMargin(type, horizontal, vertical, horizontal, vertical);
|
|
|
|
}
|
|
|
|
|
|
|
|
QRectF IconWidgetPrivate::addMargin(const QRectF &rect, MarginType type) const
|
|
|
|
{
|
|
|
|
Q_ASSERT(activeMargins);
|
2009-09-09 19:03:22 +02:00
|
|
|
|
2009-09-10 21:55:29 +02:00
|
|
|
const Margin &m = activeMargins[type];
|
|
|
|
return rect.adjusted(-m.left, -m.top, m.right, m.bottom);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QRectF IconWidgetPrivate::subtractMargin(const QRectF &rect, MarginType type) const
|
|
|
|
{
|
|
|
|
Q_ASSERT(activeMargins);
|
2009-09-09 19:03:22 +02:00
|
|
|
|
2009-09-10 21:55:29 +02:00
|
|
|
const Margin &m = activeMargins[type];
|
|
|
|
return rect.adjusted(m.left, m.top, -m.right, -m.bottom);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QSizeF IconWidgetPrivate::addMargin(const QSizeF &size, MarginType type) const
|
|
|
|
{
|
|
|
|
Q_ASSERT(activeMargins);
|
2009-09-09 19:03:22 +02:00
|
|
|
|
2009-09-10 21:55:29 +02:00
|
|
|
const Margin &m = activeMargins[type];
|
|
|
|
return QSizeF(size.width() + m.left + m.right, size.height() + m.top + m.bottom);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QSizeF IconWidgetPrivate::subtractMargin(const QSizeF &size, MarginType type) const
|
|
|
|
{
|
|
|
|
Q_ASSERT(activeMargins);
|
2009-09-09 19:03:22 +02:00
|
|
|
|
2009-09-10 21:55:29 +02:00
|
|
|
const Margin &m = activeMargins[type];
|
|
|
|
return QSizeF(size.width() - m.left - m.right, size.height() - m.top - m.bottom);
|
|
|
|
}
|
|
|
|
|
|
|
|
int IconWidgetPrivate::maxWordWidth(const QString text) const
|
|
|
|
{
|
|
|
|
QFontMetricsF fm = Plasma::Theme::defaultTheme()->fontMetrics();
|
|
|
|
QStringList words = text.split(' ');
|
|
|
|
|
|
|
|
qreal maxWidth = 0;
|
2009-09-24 16:24:46 +02:00
|
|
|
foreach (const QString &word, words) {
|
2009-09-10 21:55:29 +02:00
|
|
|
maxWidth = qMax(maxWidth, fm.width(word));
|
2009-09-09 19:03:22 +02:00
|
|
|
}
|
2009-09-10 21:55:29 +02:00
|
|
|
return maxWidth;
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QRectF IconWidgetPrivate::actionRect(ActionPosition position) const
|
|
|
|
{
|
|
|
|
switch (position) {
|
|
|
|
case TopLeft:
|
|
|
|
return QRectF(iconActionMargin,
|
|
|
|
iconActionMargin,
|
|
|
|
iconActionSize,
|
|
|
|
iconActionSize);
|
|
|
|
case TopRight:
|
|
|
|
return QRectF(currentSize.width() - iconActionSize - iconActionMargin,
|
|
|
|
iconActionMargin,
|
|
|
|
iconActionSize,
|
|
|
|
iconActionSize);
|
|
|
|
case BottomLeft:
|
|
|
|
return QRectF(iconActionMargin,
|
|
|
|
currentSize.height() - iconActionSize - iconActionMargin,
|
|
|
|
iconActionSize,
|
|
|
|
iconActionSize);
|
|
|
|
//BottomRight
|
|
|
|
default:
|
|
|
|
return QRectF(currentSize.width() - iconActionSize - iconActionMargin,
|
|
|
|
currentSize.height() - iconActionSize - iconActionMargin,
|
|
|
|
iconActionSize,
|
|
|
|
iconActionSize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // Namespace
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|