2007-08-05 19:11:46 +02:00
|
|
|
/*
|
2007-08-08 07:29:50 +02: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>
|
2007-08-05 19:11:46 +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-08-05 19:11:46 +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.
|
|
|
|
*/
|
|
|
|
#ifndef ICON_P_H
|
|
|
|
#define ICON_P_H
|
|
|
|
|
|
|
|
#include <QtCore/QEvent>
|
|
|
|
#include <QtGui/QApplication>
|
2007-08-11 00:24:49 +02:00
|
|
|
#include <QtGui/QIcon>
|
2007-08-05 19:11:46 +02:00
|
|
|
#include <QtGui/QStyleOptionGraphicsItem>
|
|
|
|
#include <QtGui/QTextLayout>
|
|
|
|
#include <QtGui/QTextOption>
|
2007-08-05 20:03:26 +02:00
|
|
|
|
2007-08-05 19:11:46 +02:00
|
|
|
#include <plasma/plasma_export.h>
|
|
|
|
#include <plasma/svg.h>
|
|
|
|
|
|
|
|
#include "icon.h"
|
2008-04-25 05:23:31 +02:00
|
|
|
#include "animator.h"
|
2007-08-05 19:11:46 +02:00
|
|
|
|
|
|
|
class QAction;
|
|
|
|
class QPainter;
|
|
|
|
class QTextLayout;
|
|
|
|
|
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
|
|
|
class PLASMA_EXPORT IconAction
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
IconAction(Icon* icon, QAction* action);
|
|
|
|
|
|
|
|
void show();
|
|
|
|
void hide();
|
|
|
|
bool isVisible() const;
|
|
|
|
|
2008-04-24 23:26:36 +02:00
|
|
|
int animationId() const;
|
2007-08-05 19:11:46 +02: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();
|
|
|
|
|
|
|
|
Icon *m_icon;
|
|
|
|
QAction *m_action;
|
|
|
|
QPixmap m_pixmap;
|
|
|
|
QRectF m_rect;
|
|
|
|
|
|
|
|
bool m_hovered;
|
|
|
|
bool m_pressed;
|
|
|
|
bool m_selected;
|
|
|
|
bool m_visible;
|
|
|
|
|
2008-04-24 23:26:36 +02:00
|
|
|
int m_animationId;
|
2007-08-05 19:11:46 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct Margin
|
|
|
|
{
|
|
|
|
qreal left, right, top, bottom;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Icon::Private
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum MarginType { ItemMargin = 0, TextMargin, IconMargin, NMargins };
|
|
|
|
|
2007-12-16 11:09:07 +01:00
|
|
|
enum IconState
|
|
|
|
{
|
|
|
|
NoState = 0,
|
|
|
|
HoverState = 1,
|
|
|
|
PressedState = 2,
|
2008-01-16 21:21:47 +01:00
|
|
|
ManualPressedState = 4
|
2007-12-16 11:09:07 +01:00
|
|
|
};
|
2007-12-16 14:50:18 +01:00
|
|
|
Q_DECLARE_FLAGS(IconStates, IconState)
|
2007-12-16 11:09:07 +01:00
|
|
|
|
2007-08-05 19:11:46 +02:00
|
|
|
public:
|
|
|
|
Private();
|
|
|
|
~Private();
|
|
|
|
|
2007-12-16 11:09:07 +01:00
|
|
|
void drawBackground(QPainter *painter, IconState state);
|
2007-08-05 19:11:46 +02:00
|
|
|
void drawText(QPainter *painter);
|
|
|
|
void drawTextItems(QPainter *painter, const QStyleOptionGraphicsItem *option,
|
|
|
|
const QTextLayout &labelLayout, const QTextLayout &infoLayout) const;
|
|
|
|
|
2007-12-20 17:28:47 +01:00
|
|
|
QPixmap decoration(const QStyleOptionGraphicsItem *option, bool useHoverEffect);
|
2007-08-05 19:11:46 +02:00
|
|
|
QPointF iconPosition(const QStyleOptionGraphicsItem *option, const QPixmap &pixmap) const;
|
|
|
|
|
2007-12-23 11:51:00 +01:00
|
|
|
QSizeF displaySizeHint(const QStyleOptionGraphicsItem *option, const qreal width) const;
|
2007-08-05 19:11:46 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2008-02-27 01:50:07 +01:00
|
|
|
|
2007-08-05 19:11:46 +02:00
|
|
|
inline void setLayoutOptions(QTextLayout &layout, const QStyleOptionGraphicsItem *options) const;
|
|
|
|
|
2008-01-01 22:00:37 +01:00
|
|
|
inline Qt::LayoutDirection iconDirection(const QStyleOptionGraphicsItem *option) const;
|
|
|
|
|
2007-08-05 19:11:46 +02:00
|
|
|
|
|
|
|
enum {
|
2008-01-03 19:38:11 +01:00
|
|
|
Minibutton = 64,
|
|
|
|
MinibuttonHover = 128,
|
|
|
|
MinibuttonPressed = 256
|
2007-08-05 19:11:46 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
enum ActionPosition {
|
|
|
|
TopLeft = 0,
|
|
|
|
TopRight,
|
|
|
|
BottomLeft,
|
|
|
|
BottomRight,
|
|
|
|
LastIconPosition
|
|
|
|
};
|
|
|
|
|
2008-01-20 21:07:22 +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;
|
|
|
|
|
|
|
|
|
2007-08-05 19:11:46 +02:00
|
|
|
QString text;
|
|
|
|
QString infoText;
|
2007-12-20 17:28:47 +01:00
|
|
|
Svg *iconSvg;
|
2007-12-22 06:31:57 +01:00
|
|
|
QString iconSvgElement;
|
2007-12-20 17:28:47 +01:00
|
|
|
QPixmap iconSvgPixmap;
|
2007-12-23 11:51:00 +01:00
|
|
|
QColor textColor;
|
|
|
|
QColor shadowColor;
|
2008-02-27 01:50:07 +01:00
|
|
|
bool m_fadeIn;
|
2008-04-24 23:26:36 +02:00
|
|
|
int m_hoverAnimId;
|
2008-02-27 01:50:07 +01:00
|
|
|
qreal m_hoverAlpha;
|
2007-08-05 19:11:46 +02:00
|
|
|
QSizeF iconSize;
|
2007-08-11 00:24:49 +02:00
|
|
|
QIcon icon;
|
2007-12-16 11:09:07 +01:00
|
|
|
IconStates states;
|
2007-08-05 19:11:46 +02:00
|
|
|
Qt::Orientation orientation;
|
2007-12-26 14:44:57 +01:00
|
|
|
int numDisplayLines;
|
2008-01-01 22:00:37 +01:00
|
|
|
bool invertLayout;
|
2008-01-03 22:22:24 +01:00
|
|
|
bool drawBg;
|
2008-01-01 20:21:33 +01:00
|
|
|
QSizeF currentSize;
|
2008-01-13 06:33:05 +01:00
|
|
|
QPointF clickStartPos;
|
2007-08-05 19:11:46 +02:00
|
|
|
|
|
|
|
QList<IconAction*> cornerActions;
|
|
|
|
|
|
|
|
Margin verticalMargin[NMargins];
|
|
|
|
Margin horizontalMargin[NMargins];
|
|
|
|
Margin *activeMargins;
|
2007-12-26 14:44:57 +01:00
|
|
|
|
|
|
|
static const int maxDisplayLines = 5;
|
2008-01-20 21:07:22 +01:00
|
|
|
static const int iconActionSize = 26;
|
|
|
|
static const int iconActionMargin = 4;
|
2007-08-05 19:11:46 +02:00
|
|
|
};
|
|
|
|
|
2007-12-16 14:50:18 +01:00
|
|
|
Q_DECLARE_OPERATORS_FOR_FLAGS(Icon::Private::IconStates)
|
2007-12-16 11:09:07 +01:00
|
|
|
|
2007-08-05 19:11:46 +02:00
|
|
|
// Inline methods
|
|
|
|
void Icon::Private::setLayoutOptions(QTextLayout &layout, const QStyleOptionGraphicsItem *option) const
|
|
|
|
{
|
|
|
|
QTextOption textoption;
|
|
|
|
textoption.setTextDirection(option->direction);
|
|
|
|
textoption.setAlignment(Qt::AlignCenter); // NOTE: assumption
|
|
|
|
textoption.setWrapMode(QTextOption::WordWrap); // NOTE: assumption as well
|
|
|
|
|
|
|
|
layout.setFont(QApplication::font()); // NOTE: find better ways to get the font
|
|
|
|
layout.setTextOption(textoption);
|
|
|
|
}
|
|
|
|
|
2008-01-01 22:00:37 +01:00
|
|
|
Qt::LayoutDirection Icon::Private::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;
|
|
|
|
}
|
2008-02-27 01:50:07 +01:00
|
|
|
} else {
|
2008-01-01 22:00:37 +01:00
|
|
|
direction = option->direction;
|
|
|
|
}
|
|
|
|
|
|
|
|
return direction;
|
|
|
|
}
|
|
|
|
|
2007-08-05 19:11:46 +02:00
|
|
|
void Icon::Private::setActiveMargins()
|
|
|
|
{
|
|
|
|
activeMargins = (orientation == Qt::Horizontal ?
|
|
|
|
horizontalMargin : verticalMargin);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Icon::Private::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 Icon::Private::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 Icon::Private::setVerticalMargin(MarginType type, qreal horizontal, qreal vertical)
|
|
|
|
{
|
|
|
|
setVerticalMargin(type, horizontal, vertical, horizontal, vertical);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Icon::Private::setHorizontalMargin(MarginType type, qreal horizontal, qreal vertical)
|
|
|
|
{
|
|
|
|
setHorizontalMargin(type, horizontal, vertical, horizontal, vertical);
|
|
|
|
}
|
|
|
|
|
|
|
|
QRectF Icon::Private::addMargin(const QRectF &rect, MarginType type) const
|
|
|
|
{
|
|
|
|
const Margin &m = activeMargins[type];
|
|
|
|
return rect.adjusted(-m.left, -m.top, m.right, m.bottom);
|
|
|
|
}
|
|
|
|
|
|
|
|
QRectF Icon::Private::subtractMargin(const QRectF &rect, MarginType type) const
|
|
|
|
{
|
|
|
|
const Margin &m = activeMargins[type];
|
|
|
|
return rect.adjusted(m.left, m.top, -m.right, -m.bottom);
|
|
|
|
}
|
|
|
|
|
|
|
|
QSizeF Icon::Private::addMargin(const QSizeF &size, MarginType type) const
|
|
|
|
{
|
|
|
|
const Margin &m = activeMargins[type];
|
|
|
|
return QSizeF(size.width() + m.left + m.right, size.height() + m.top + m.bottom);
|
|
|
|
}
|
|
|
|
|
|
|
|
QSizeF Icon::Private::subtractMargin(const QSizeF &size, MarginType type) const
|
|
|
|
{
|
|
|
|
const Margin &m = activeMargins[type];
|
|
|
|
return QSizeF(size.width() - m.left - m.right, size.height() - m.top - m.bottom);
|
|
|
|
}
|
|
|
|
|
2008-01-20 21:07:22 +01:00
|
|
|
QRectF Icon::Private::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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-05 19:11:46 +02:00
|
|
|
} // Namespace
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|