]> cloud.milkyroute.net Git - dolphin.git/blob - src/settings/serviceitemdelegate.cpp
Merge branch 'release/21.12'
[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,
26 const QModelIndex &index) const
27 {
28 Q_UNUSED(index)
29
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));
35 }
36
37 void ServiceItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option,
38 const QModelIndex& index) const
39 {
40 Q_UNUSED(index)
41 painter->save();
42
43 itemView()->style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter);
44
45 if (option.state & QStyle::State_Selected) {
46 painter->setPen(option.palette.highlightedText().color());
47 }
48
49 painter->restore();
50 }
51
52 QList<QWidget*> ServiceItemDelegate::createItemWidgets(const QModelIndex&) const
53 {
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);
59
60 QPushButton* configureButton = new QPushButton();
61 connect(configureButton, &QPushButton::clicked, this, &ServiceItemDelegate::slotConfigureButtonClicked);
62
63 return {checkBox, configureButton};
64 }
65
66 void ServiceItemDelegate::updateItemWidgets(const QList<QWidget*> widgets,
67 const QStyleOptionViewItem& option,
68 const QPersistentModelIndex& index) const
69 {
70 QCheckBox* checkBox = static_cast<QCheckBox*>(widgets[0]);
71 QPushButton *configureButton = static_cast<QPushButton*>(widgets[1]);
72
73 const int itemHeight = sizeHint(option, index).height();
74
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));
81 }
82 checkBox->setChecked(model->data(index, Qt::CheckStateRole).toBool());
83
84 const bool configurable = model->data(index, ServiceModel::ConfigurableRole).toBool();
85
86 int checkBoxWidth = option.rect.width();
87 if (configurable) {
88 checkBoxWidth -= configureButton->sizeHint().width();
89 }
90 checkBox->resize(checkBoxWidth, checkBox->sizeHint().height());
91 checkBox->move(0, (itemHeight - checkBox->height()) / 2);
92
93 // Update the configuration button
94 if (configurable) {
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);
100 }
101 configureButton->setVisible(configurable);
102 }
103
104 void ServiceItemDelegate::slotCheckBoxClicked(bool checked)
105 {
106 QAbstractItemModel* model = const_cast<QAbstractItemModel*>(focusedIndex().model());
107 model->setData(focusedIndex(), checked, Qt::CheckStateRole);
108 }
109
110 void ServiceItemDelegate::slotConfigureButtonClicked()
111 {
112 Q_EMIT requestServiceConfiguration(focusedIndex());
113 }
114