]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/private/kitemlistselectiontoggle.cpp
KItemViews: Internal directory restructuration
[dolphin.git] / src / kitemviews / private / kitemlistselectiontoggle.cpp
1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
19
20 #include "kitemlistselectiontoggle.h"
21
22 #include <KIconEffect>
23 #include <KIconLoader>
24 #include <QPainter>
25
26 #include <KDebug>
27
28 KItemListSelectionToggle::KItemListSelectionToggle(QGraphicsItem* parent) :
29 QGraphicsWidget(parent, 0),
30 m_checked(false),
31 m_hovered(false)
32 {
33 setAcceptHoverEvents(true);
34 }
35
36 KItemListSelectionToggle::~KItemListSelectionToggle()
37 {
38 }
39
40 void KItemListSelectionToggle::setChecked(bool checked)
41 {
42 if (m_checked != checked) {
43 m_checked = checked;
44 m_pixmap = QPixmap();
45 update();
46 }
47 }
48
49 bool KItemListSelectionToggle::isChecked() const
50 {
51 return m_checked;
52 }
53
54 void KItemListSelectionToggle::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
55 {
56 Q_UNUSED(option);
57 Q_UNUSED(widget);
58
59 if (m_pixmap.isNull()) {
60 updatePixmap();
61 }
62
63 const qreal x = (size().width() - qreal(m_pixmap.width())) / 2;
64 const qreal y = (size().height() - qreal(m_pixmap.height())) / 2;
65 painter->drawPixmap(x, y, m_pixmap);
66 }
67
68 void KItemListSelectionToggle::hoverEnterEvent(QGraphicsSceneHoverEvent* event)
69 {
70 QGraphicsWidget::hoverEnterEvent(event);
71 m_hovered = true;
72 m_pixmap = QPixmap();
73 }
74
75 void KItemListSelectionToggle::hoverLeaveEvent(QGraphicsSceneHoverEvent* event)
76 {
77 QGraphicsWidget::hoverLeaveEvent(event);
78 m_hovered = false;
79 m_pixmap = QPixmap();
80 }
81
82 void KItemListSelectionToggle::updatePixmap()
83 {
84 const char* icon = m_checked ? "list-remove" : "list-add";
85
86 int iconSize = qMin(size().width(), size().height());
87 if (iconSize < KIconLoader::SizeSmallMedium) {
88 iconSize = KIconLoader::SizeSmall;
89 } else if (iconSize < KIconLoader::SizeMedium) {
90 iconSize = KIconLoader::SizeSmallMedium;
91 } else if (iconSize < KIconLoader::SizeLarge) {
92 iconSize = KIconLoader::SizeMedium;
93 } else if (iconSize < KIconLoader::SizeHuge) {
94 iconSize = KIconLoader::SizeLarge;
95 } else if (iconSize < KIconLoader::SizeEnormous) {
96 iconSize = KIconLoader::SizeHuge;
97 }
98
99 m_pixmap = KIconLoader::global()->loadIcon(QLatin1String(icon), KIconLoader::NoGroup, iconSize);
100
101 if (m_hovered) {
102 KIconLoader::global()->iconEffect()->apply(m_pixmap, KIconLoader::Desktop, KIconLoader::ActiveState);
103 }
104 }
105
106 #include "kitemlistselectiontoggle.moc"