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
, const QModelIndex
&index
) const
29 const QStyle
*style
= itemView()->style();
30 const int buttonHeight
= style
->pixelMetric(QStyle::PM_ButtonMargin
) * 2 + style
->pixelMetric(QStyle::PM_ButtonIconSize
);
31 const int fontHeight
= option
.fontMetrics
.height();
32 return QSize(100, qMax(buttonHeight
, fontHeight
));
35 void ServiceItemDelegate::paint(QPainter
*painter
, const QStyleOptionViewItem
&option
, const QModelIndex
&index
) const
40 itemView()->style()->drawPrimitive(QStyle::PE_PanelItemViewItem
, &option
, painter
);
42 if (option
.state
& QStyle::State_Selected
) {
43 painter
->setPen(option
.palette
.highlightedText().color());
49 QList
<QWidget
*> ServiceItemDelegate::createItemWidgets(const QModelIndex
&) const
51 QCheckBox
*checkBox
= new QCheckBox();
52 QPalette palette
= checkBox
->palette();
53 palette
.setColor(QPalette::WindowText
, palette
.color(QPalette::Text
));
54 checkBox
->setPalette(palette
);
55 connect(checkBox
, &QCheckBox::clicked
, this, &ServiceItemDelegate::slotCheckBoxClicked
);
57 QPushButton
*configureButton
= new QPushButton();
58 connect(configureButton
, &QPushButton::clicked
, this, &ServiceItemDelegate::slotConfigureButtonClicked
);
60 return {checkBox
, configureButton
};
63 void ServiceItemDelegate::updateItemWidgets(const QList
<QWidget
*> &widgets
, const QStyleOptionViewItem
&option
, const QPersistentModelIndex
&index
) const
65 QCheckBox
*checkBox
= static_cast<QCheckBox
*>(widgets
[0]);
66 QPushButton
*configureButton
= static_cast<QPushButton
*>(widgets
[1]);
68 const int itemHeight
= sizeHint(option
, index
).height();
70 // Update the checkbox showing the service name and icon
71 const QAbstractItemModel
*model
= index
.model();
72 checkBox
->setText(model
->data(index
).toString());
73 const QString iconName
= model
->data(index
, Qt::DecorationRole
).toString();
74 if (!iconName
.isEmpty()) {
75 checkBox
->setIcon(QIcon::fromTheme(iconName
));
77 checkBox
->setChecked(model
->data(index
, Qt::CheckStateRole
).toBool());
79 const bool configurable
= model
->data(index
, ServiceModel::ConfigurableRole
).toBool();
81 int checkBoxWidth
= option
.rect
.width();
83 checkBoxWidth
-= configureButton
->sizeHint().width();
85 checkBox
->resize(checkBoxWidth
, checkBox
->sizeHint().height());
86 checkBox
->move(0, (itemHeight
- checkBox
->height()) / 2);
88 // Update the configuration button
90 configureButton
->setEnabled(checkBox
->isChecked());
91 configureButton
->setIcon(QIcon::fromTheme(QStringLiteral("configure")));
92 configureButton
->resize(configureButton
->sizeHint());
93 configureButton
->move(option
.rect
.right() - configureButton
->width(), (itemHeight
- configureButton
->height()) / 2);
95 configureButton
->setVisible(configurable
);
98 void ServiceItemDelegate::slotCheckBoxClicked(bool checked
)
100 QAbstractItemModel
*model
= const_cast<QAbstractItemModel
*>(focusedIndex().model());
101 model
->setData(focusedIndex(), checked
, Qt::CheckStateRole
);
104 void ServiceItemDelegate::slotConfigureButtonClicked()
106 Q_EMIT
requestServiceConfiguration(focusedIndex());