2 * SPDX-FileCopyrightText: 2011 Peter Penz <peter.penz19@gmail.com>
4 * SPDX-License-Identifier: GPL-2.0-or-later
7 #include "kitemlistselectiontoggle.h"
14 KItemListSelectionToggle::KItemListSelectionToggle(QGraphicsItem
*parent
)
15 : QGraphicsWidget(parent
)
21 KItemListSelectionToggle::~KItemListSelectionToggle()
25 void KItemListSelectionToggle::setChecked(bool checked
)
27 if (m_checked
!= checked
) {
34 bool KItemListSelectionToggle::isChecked() const
39 void KItemListSelectionToggle::setHovered(bool hovered
)
41 if (m_hovered
!= hovered
) {
48 void KItemListSelectionToggle::paint(QPainter
*painter
, const QStyleOptionGraphicsItem
*option
, QWidget
*widget
)
53 if (m_pixmap
.isNull()) {
57 const qreal x
= (size().width() - qreal(m_pixmap
.width() / m_pixmap
.devicePixelRatioF())) / 2;
58 const qreal y
= (size().height() - qreal(m_pixmap
.height() / m_pixmap
.devicePixelRatioF())) / 2;
59 painter
->drawPixmap(x
, y
, m_pixmap
);
62 void KItemListSelectionToggle::resizeEvent(QGraphicsSceneResizeEvent
*event
)
64 QGraphicsWidget::resizeEvent(event
);
66 if (!m_pixmap
.isNull()) {
67 const int pixmapSize
= m_pixmap
.size().width() / m_pixmap
.devicePixelRatioF(); // Pixmap width is always equal pixmap height
69 if (pixmapSize
!= iconSize()) {
70 // If the required icon size is different from the actual pixmap size,
71 // overwrite the m_pixmap with an empty pixmap and reload the new
72 // icon on next re-painting.
78 void KItemListSelectionToggle::updatePixmap()
80 const QString icon
= m_checked
? QStringLiteral("emblem-remove") : QStringLiteral("emblem-added");
81 m_pixmap
= QIcon::fromTheme(icon
).pixmap(iconSize(), m_hovered
? QIcon::Active
: QIcon::Disabled
);
84 int KItemListSelectionToggle::iconSize() const
86 const int iconSize
= qMin(size().width(), size().height());
88 if (iconSize
< KIconLoader::SizeSmallMedium
) {
89 return KIconLoader::SizeSmall
;
90 } else if (iconSize
< KIconLoader::SizeMedium
) {
91 return KIconLoader::SizeSmallMedium
;
92 } else if (iconSize
< KIconLoader::SizeLarge
) {
93 return KIconLoader::SizeMedium
;
94 } else if (iconSize
< KIconLoader::SizeHuge
) {
95 return KIconLoader::SizeLarge
;
96 } else if (iconSize
< KIconLoader::SizeEnormous
) {
97 return KIconLoader::SizeHuge
;