2 * SPDX-FileCopyrightText: 2011 Peter Penz <peter.penz19@gmail.com>
4 * SPDX-License-Identifier: GPL-2.0-or-later
7 #include "serviceitemdelegate.h"
9 #include "servicemodel.h"
11 #include <QAbstractItemView>
14 #include <QPushButton>
16 ServiceItemDelegate::ServiceItemDelegate(QAbstractItemView
* itemView
, QObject
* parent
) :
17 KWidgetItemDelegate(itemView
, parent
)
21 ServiceItemDelegate::~ServiceItemDelegate()
25 QSize
ServiceItemDelegate::sizeHint(const QStyleOptionViewItem
&option
,
26 const QModelIndex
&index
) const
30 const QStyle
*style
= itemView()->style();
31 const int buttonHeight
= style
->pixelMetric(QStyle::PM_ButtonMargin
) * 2 +
32 style
->pixelMetric(QStyle::PM_ButtonIconSize
);
33 const int fontHeight
= option
.fontMetrics
.height();
34 return QSize(100, qMax(buttonHeight
, fontHeight
));
37 void ServiceItemDelegate::paint(QPainter
* painter
, const QStyleOptionViewItem
& option
,
38 const QModelIndex
& index
) const
43 itemView()->style()->drawPrimitive(QStyle::PE_PanelItemViewItem
, &option
, painter
);
45 if (option
.state
& QStyle::State_Selected
) {
46 painter
->setPen(option
.palette
.highlightedText().color());
52 QList
<QWidget
*> ServiceItemDelegate::createItemWidgets(const QModelIndex
&) const
54 QCheckBox
* checkBox
= new QCheckBox();
55 QPalette palette
= checkBox
->palette();
56 palette
.setColor(QPalette::WindowText
, palette
.color(QPalette::Text
));
57 checkBox
->setPalette(palette
);
58 connect(checkBox
, &QCheckBox::clicked
, this, &ServiceItemDelegate::slotCheckBoxClicked
);
60 QPushButton
* configureButton
= new QPushButton();
61 connect(configureButton
, &QPushButton::clicked
, this, &ServiceItemDelegate::slotConfigureButtonClicked
);
63 return {checkBox
, configureButton
};
66 void ServiceItemDelegate::updateItemWidgets(const QList
<QWidget
*> widgets
,
67 const QStyleOptionViewItem
& option
,
68 const QPersistentModelIndex
& index
) const
70 QCheckBox
* checkBox
= static_cast<QCheckBox
*>(widgets
[0]);
71 QPushButton
*configureButton
= static_cast<QPushButton
*>(widgets
[1]);
73 const int itemHeight
= sizeHint(option
, index
).height();
75 // Update the checkbox showing the service name and icon
76 const QAbstractItemModel
* model
= index
.model();
77 checkBox
->setText(model
->data(index
).toString());
78 const QString iconName
= model
->data(index
, Qt::DecorationRole
).toString();
79 if (!iconName
.isEmpty()) {
80 checkBox
->setIcon(QIcon::fromTheme(iconName
));
82 checkBox
->setChecked(model
->data(index
, Qt::CheckStateRole
).toBool());
84 const bool configurable
= model
->data(index
, ServiceModel::ConfigurableRole
).toBool();
86 int checkBoxWidth
= option
.rect
.width();
88 checkBoxWidth
-= configureButton
->sizeHint().width();
90 checkBox
->resize(checkBoxWidth
, checkBox
->sizeHint().height());
91 checkBox
->move(0, (itemHeight
- checkBox
->height()) / 2);
93 // Update the configuration button
95 configureButton
->setEnabled(checkBox
->isChecked());
96 configureButton
->setIcon(QIcon::fromTheme(QStringLiteral("configure")));
97 configureButton
->resize(configureButton
->sizeHint());
98 configureButton
->move(option
.rect
.right() - configureButton
->width(),
99 (itemHeight
- configureButton
->height()) / 2);
101 configureButton
->setVisible(configurable
);
104 void ServiceItemDelegate::slotCheckBoxClicked(bool checked
)
106 QAbstractItemModel
* model
= const_cast<QAbstractItemModel
*>(focusedIndex().model());
107 model
->setData(focusedIndex(), checked
, Qt::CheckStateRole
);
110 void ServiceItemDelegate::slotConfigureButtonClicked()
112 Q_EMIT
requestServiceConfiguration(focusedIndex());