]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/private/kitemlistselectiontoggle.cpp
Merge branch 'master' into frameworks
[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 }
34
35 KItemListSelectionToggle::~KItemListSelectionToggle()
36 {
37 }
38
39 void KItemListSelectionToggle::setChecked(bool checked)
40 {
41 if (m_checked != checked) {
42 m_checked = checked;
43 m_pixmap = QPixmap();
44 update();
45 }
46 }
47
48 bool KItemListSelectionToggle::isChecked() const
49 {
50 return m_checked;
51 }
52
53 void KItemListSelectionToggle::setHovered(bool hovered)
54 {
55 if (m_hovered != hovered) {
56 m_hovered = hovered;
57 m_pixmap = QPixmap();
58 update();
59 }
60 }
61
62 void KItemListSelectionToggle::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
63 {
64 Q_UNUSED(option);
65 Q_UNUSED(widget);
66
67 if (m_pixmap.isNull()) {
68 updatePixmap();
69 }
70
71 const qreal x = (size().width() - qreal(m_pixmap.width())) / 2;
72 const qreal y = (size().height() - qreal(m_pixmap.height())) / 2;
73 painter->drawPixmap(x, y, m_pixmap);
74 }
75
76 void KItemListSelectionToggle::resizeEvent(QGraphicsSceneResizeEvent* event)
77 {
78 QGraphicsWidget::resizeEvent(event);
79
80 if (!m_pixmap.isNull()) {
81 const int pixmapSize = m_pixmap.size().width(); // Pixmap width is always equal pixmap height
82
83 if (pixmapSize != iconSize()) {
84 // If the required icon size is different from the actual pixmap size,
85 // overwrite the m_pixmap with an empty pixmap and reload the new
86 // icon on next re-painting.
87 m_pixmap = QPixmap();
88 }
89 }
90 }
91
92 void KItemListSelectionToggle::updatePixmap()
93 {
94 const QString icon = m_checked ? "list-remove" : "list-add";
95 const KIconLoader::States state = m_hovered ? KIconLoader::ActiveState : KIconLoader::DisabledState;
96 m_pixmap = KIconLoader::global()->loadIcon(icon, KIconLoader::Desktop, iconSize(), state);
97 }
98
99 int KItemListSelectionToggle::iconSize() const
100 {
101 const int iconSize = qMin(size().width(), size().height());
102
103 if (iconSize < KIconLoader::SizeSmallMedium) {
104 return KIconLoader::SizeSmall;
105 } else if (iconSize < KIconLoader::SizeMedium) {
106 return KIconLoader::SizeSmallMedium;
107 } else if (iconSize < KIconLoader::SizeLarge) {
108 return KIconLoader::SizeMedium;
109 } else if (iconSize < KIconLoader::SizeHuge) {
110 return KIconLoader::SizeLarge;
111 } else if (iconSize < KIconLoader::SizeEnormous) {
112 return KIconLoader::SizeHuge;
113 }
114
115 return iconSize;
116 }
117