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