]> cloud.milkyroute.net Git - dolphin.git/blob - src/settings/serviceitemdelegate.cpp
GIT_SILENT Update Appstream for new release
[dolphin.git] / src / settings / serviceitemdelegate.cpp
1 /*
2 * SPDX-FileCopyrightText: 2011 Peter Penz <peter.penz19@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7 #include "serviceitemdelegate.h"
8
9 #include "servicemodel.h"
10
11 #include <QAbstractItemView>
12 #include <QCheckBox>
13 #include <QPainter>
14 #include <QPushButton>
15
16 ServiceItemDelegate::ServiceItemDelegate(QAbstractItemView *itemView, QObject *parent)
17 : KWidgetItemDelegate(itemView, parent)
18 {
19 }
20
21 ServiceItemDelegate::~ServiceItemDelegate()
22 {
23 }
24
25 QSize ServiceItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
26 {
27 Q_UNUSED(index)
28
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));
33 }
34
35 void ServiceItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
36 {
37 Q_UNUSED(index)
38 painter->save();
39
40 itemView()->style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter);
41
42 if (option.state & QStyle::State_Selected) {
43 painter->setPen(option.palette.highlightedText().color());
44 }
45
46 painter->restore();
47 }
48
49 QList<QWidget *> ServiceItemDelegate::createItemWidgets(const QModelIndex &) const
50 {
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);
56
57 QPushButton *configureButton = new QPushButton();
58 connect(configureButton, &QPushButton::clicked, this, &ServiceItemDelegate::slotConfigureButtonClicked);
59
60 return {checkBox, configureButton};
61 }
62
63 void ServiceItemDelegate::updateItemWidgets(const QList<QWidget *> widgets, const QStyleOptionViewItem &option, const QPersistentModelIndex &index) const
64 {
65 QCheckBox *checkBox = static_cast<QCheckBox *>(widgets[0]);
66 QPushButton *configureButton = static_cast<QPushButton *>(widgets[1]);
67
68 const int itemHeight = sizeHint(option, index).height();
69
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));
76 }
77 checkBox->setChecked(model->data(index, Qt::CheckStateRole).toBool());
78
79 const bool configurable = model->data(index, ServiceModel::ConfigurableRole).toBool();
80
81 int checkBoxWidth = option.rect.width();
82 if (configurable) {
83 checkBoxWidth -= configureButton->sizeHint().width();
84 }
85 checkBox->resize(checkBoxWidth, checkBox->sizeHint().height());
86 checkBox->move(0, (itemHeight - checkBox->height()) / 2);
87
88 // Update the configuration button
89 if (configurable) {
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);
94 }
95 configureButton->setVisible(configurable);
96 }
97
98 void ServiceItemDelegate::slotCheckBoxClicked(bool checked)
99 {
100 QAbstractItemModel *model = const_cast<QAbstractItemModel *>(focusedIndex().model());
101 model->setData(focusedIndex(), checked, Qt::CheckStateRole);
102 }
103
104 void ServiceItemDelegate::slotConfigureButtonClicked()
105 {
106 Q_EMIT requestServiceConfiguration(focusedIndex());
107 }