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