2
* Copyright (C) 2009 by Ana Cecília Martins <anaceciliamb@gmail.com>
3
* Copyright (C) 2010 by Chani Armitage <chani@kde.org>
4
* Copyright (C) 2010 by Ivan Cukic <ivan.cukic@kde.org>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU Library/Lesser General Public License
8
* version 2, or (at your option) any later version, as published by the
9
* Free Software Foundation
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details
16
* You should have received a copy of the GNU Library/Lesser General Public
17
* License along with this program; if not, write to the
18
* Free Software Foundation, Inc.,
19
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22
#include "abstracticon.h"
24
#include <QApplication>
27
#include <QFontMetrics>
28
#include <QGraphicsSceneMouseEvent>
29
#include <QPropertyAnimation>
30
#include <QStyleOptionGraphicsItem>
33
#include <KIconLoader>
35
#include <KGlobalSettings>
37
#include <Plasma/FrameSvg>
38
#include <Plasma/Theme>
39
#include <Plasma/PaintUtils>
45
AbstractIcon::AbstractIcon(QGraphicsItem *parent)
46
: QGraphicsWidget(parent),
47
m_background(new Plasma::FrameSvg(this)),
48
m_backgroundFadeAnim(0),
49
m_backgroundPrefix("normal"),
50
m_iconHeight(DEFAULT_ICON_SIZE),
51
m_maxSize(maximumSize()),
56
setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
57
setCacheMode(DeviceCoordinateCache);
58
setAcceptHoverEvents(true);
59
m_background->setCacheAllRenderedFrames(true);
60
m_background->setImagePath("widgets/tasks");
61
connect(m_background, SIGNAL(repaintNeeded()), this, SLOT(syncTheme()));
65
AbstractIcon::~AbstractIcon()
69
void AbstractIcon::syncTheme()
71
m_background->setElementPrefix("normal");
72
m_background->resizeFrame(size());
74
m_background->getMargins(ml, mt, mr, mb);
76
setContentsMargins(ml, mt, mr, mb);
81
void AbstractIcon::resizeEvent(QGraphicsSceneResizeEvent *event)
83
m_background->resizeFrame(event->newSize());
86
QSizeF AbstractIcon::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
88
if (which == Qt::MinimumSize || which == Qt::PreferredSize) {
90
getContentsMargins(&l, &t, &r, &b);
91
QFontMetrics fm(font());
92
const int minHeight = m_iconHeight + 2 + fm.height();
93
QSizeF s(qMax(m_iconHeight, fm.width(m_name)) + l + r, minHeight + t + b);
97
return QGraphicsWidget::sizeHint(which, constraint);
100
void AbstractIcon::setIconSize(int height)
102
m_iconHeight = height;
107
int AbstractIcon::iconSize() const
112
void AbstractIcon::setName(const QString &name)
119
QString AbstractIcon::name() const
124
void AbstractIcon::collapse()
128
m_maxSize = maximumSize();
129
//kDebug() << m_maxSize;
130
setMaximumSize(0, 0);
134
void AbstractIcon::expand()
138
setMaximumSize(m_maxSize);
142
void AbstractIcon::hoverEnterEvent(QGraphicsSceneHoverEvent *)
146
m_backgroundPrefix = "hover";
147
m_oldBackgroundPrefix = m_selected ? "focus" : "normal";
148
m_background->setElementPrefix(m_backgroundPrefix);
149
m_background->resizeFrame(size());
151
emit hoverEnter(this);
156
void AbstractIcon::hoverLeaveEvent(QGraphicsSceneHoverEvent *)
159
emit hoverLeave(this);
161
m_backgroundPrefix = m_selected ? "focus" : "normal";
162
m_oldBackgroundPrefix = "hover";
163
m_background->setElementPrefix(m_backgroundPrefix);
164
m_background->resizeFrame(size());
169
void AbstractIcon::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
171
if (event->button() != Qt::LeftButton &&
172
(event->pos() - event->buttonDownPos(Qt::LeftButton)).toPoint().manhattanLength() > QApplication::startDragDistance()) {
174
setCursor(Qt::OpenHandCursor);
175
QMimeData *data = mimeData();
176
if (data && !data->formats().isEmpty()) {
177
qDebug() << "Start Dragging";
179
QDrag *drag = new QDrag(event->widget());
180
QPixmap p = pixmap(QSize(KIconLoader::SizeLarge, KIconLoader::SizeLarge));
183
drag->setMimeData(mimeData());
191
void AbstractIcon::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
193
QGraphicsWidget::mouseReleaseEvent(event);
195
setCursor(Qt::OpenHandCursor);
198
if (boundingRect().contains(event->pos())) {
203
void AbstractIcon::mousePressEvent(QGraphicsSceneMouseEvent *event)
207
setCursor(Qt::ClosedHandCursor);
211
void AbstractIcon::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
214
emit doubleClicked(this);
217
void AbstractIcon::setSelected(bool selected)
219
if (m_selected != selected) {
220
m_selected = selected;
223
m_backgroundPrefix = "focus";
224
m_oldBackgroundPrefix = m_hovered ? "hover" : "normal";
226
m_backgroundPrefix = m_hovered ? "hover" : "normal";
227
m_oldBackgroundPrefix = "focus";
234
bool AbstractIcon::isSelected() const
239
bool AbstractIcon::isDraggable() const
241
return cursor().shape() == Qt::OpenHandCursor ||
242
cursor().shape() == Qt::ClosedHandCursor;
245
void AbstractIcon::setDraggable(bool draggable)
248
setCursor(Qt::OpenHandCursor);
254
void AbstractIcon::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
256
paintBackground(painter, option, widget);
257
paintForeground(painter, option, widget);
260
void AbstractIcon::paintBackground(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
262
if (!option->rect.isValid()) {
266
if (!m_backgroundFadeAnim || m_backgroundFadeAnim->state() != QAbstractAnimation::Running) {
267
m_background->setElementPrefix(m_backgroundPrefix);
268
m_background->paintFrame(painter);
272
m_background->setElementPrefix(m_oldBackgroundPrefix);
273
QPixmap bg = m_background->framePixmap();
275
m_background->setElementPrefix(m_backgroundPrefix);
276
bg = Plasma::PaintUtils::transition(bg, m_background->framePixmap(), m_backgroundAlpha);
277
painter->drawPixmap(option->exposedRect, bg, option->exposedRect);
280
void AbstractIcon::paintForeground(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
282
const QRectF rect = contentsRect();
283
const int width = rect.width();
284
const int height = rect.height();
286
QRectF textRect(rect.x(), rect.y(), width, height - m_iconHeight - 2);
287
QRect iconRect(rect.x() + qMax(0, (width / 2) - (m_iconHeight / 2)), textRect.bottom() + 2, m_iconHeight, m_iconHeight);
289
painter->setPen(Plasma::Theme::defaultTheme()->color(Plasma::Theme::TextColor));
290
painter->setFont(font());
292
int flags = Qt::AlignTop;// | Qt::TextWordWrap;
293
QFontMetrics fm(font());
294
if (fm.width(m_name) < textRect.width()) {
295
flags |= Qt::AlignCenter;
298
if (!m_name.isEmpty() && qGray(painter->pen().color().rgb()) < 192) {
299
const QRectF haloRect = fm.boundingRect(textRect.toAlignedRect(), flags, m_name);
300
PaintUtils::drawHalo(painter, haloRect);
303
painter->drawPixmap(iconRect, pixmap(QSize(m_iconHeight, m_iconHeight)));
304
painter->drawText(textRect, flags, m_name);
307
qreal AbstractIcon::backgroundFadeAlpha() const
309
return m_backgroundAlpha;
312
void AbstractIcon::setBackgroundFadeAlpha(qreal progress)
314
m_backgroundAlpha = progress;
318
void AbstractIcon::fadeBackground(int duration)
320
if (m_oldBackgroundPrefix.isEmpty()) {
323
if (!m_backgroundFadeAnim) {
324
m_backgroundFadeAnim = new QPropertyAnimation(this);
325
m_backgroundFadeAnim->setEasingCurve(QEasingCurve::InQuad);
326
m_backgroundFadeAnim->setPropertyName("backgroundFadeAlpha");
327
m_backgroundFadeAnim->setTargetObject(this);
328
m_backgroundFadeAnim->setStartValue(0);
329
m_backgroundFadeAnim->setEndValue(1);
332
m_backgroundFadeAnim->setDuration(duration);
333
m_backgroundFadeAnim->start();
337
} // namespace Plasma