]> cloud.milkyroute.net Git - dolphin.git/blob - src/settings/serviceitemdelegate.cpp
Remove unused #include
[dolphin.git] / src / settings / serviceitemdelegate.cpp
1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
19
20 #include "serviceitemdelegate.h"
21
22 #include <QPushButton>
23
24 #include "servicemodel.h"
25
26 #include <QAbstractItemView>
27 #include <QCheckBox>
28 #include <QPainter>
29
30 ServiceItemDelegate::ServiceItemDelegate(QAbstractItemView* itemView, QObject* parent) :
31 KWidgetItemDelegate(itemView, parent)
32 {
33 }
34
35 ServiceItemDelegate::~ServiceItemDelegate()
36 {
37 }
38
39 QSize ServiceItemDelegate::sizeHint(const QStyleOptionViewItem &option,
40 const QModelIndex &index) const
41 {
42 Q_UNUSED(index);
43
44 const QStyle *style = itemView()->style();
45 const int buttonHeight = style->pixelMetric(QStyle::PM_ButtonMargin) * 2 +
46 style->pixelMetric(QStyle::PM_ButtonIconSize);
47 const int fontHeight = option.fontMetrics.height();
48 return QSize(100, qMax(buttonHeight, fontHeight));
49 }
50
51 void ServiceItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option,
52 const QModelIndex& index) const
53 {
54 Q_UNUSED(index);
55 painter->save();
56
57 itemView()->style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter);
58
59 if (option.state & QStyle::State_Selected) {
60 painter->setPen(option.palette.highlightedText().color());
61 }
62
63 painter->restore();
64 }
65
66 QList<QWidget*> ServiceItemDelegate::createItemWidgets(const QModelIndex&) const
67 {
68 QCheckBox* checkBox = new QCheckBox();
69 QPalette palette = checkBox->palette();
70 palette.setColor(QPalette::WindowText, palette.color(QPalette::Text));
71 checkBox->setPalette(palette);
72 connect(checkBox, &QCheckBox::clicked, this, &ServiceItemDelegate::slotCheckBoxClicked);
73
74 QPushButton* configureButton = new QPushButton();
75 connect(configureButton, &QPushButton::clicked, this, &ServiceItemDelegate::slotConfigureButtonClicked);
76
77 return {checkBox, configureButton};
78 }
79
80 void ServiceItemDelegate::updateItemWidgets(const QList<QWidget*> widgets,
81 const QStyleOptionViewItem& option,
82 const QPersistentModelIndex& index) const
83 {
84 QCheckBox* checkBox = static_cast<QCheckBox*>(widgets[0]);
85 QPushButton *configureButton = static_cast<QPushButton*>(widgets[1]);
86
87 const int itemHeight = sizeHint(option, index).height();
88
89 // Update the checkbox showing the service name and icon
90 const QAbstractItemModel* model = index.model();
91 checkBox->setText(model->data(index).toString());
92 const QString iconName = model->data(index, Qt::DecorationRole).toString();
93 if (!iconName.isEmpty()) {
94 checkBox->setIcon(QIcon::fromTheme(iconName));
95 }
96 checkBox->setChecked(model->data(index, Qt::CheckStateRole).toBool());
97
98 const bool configurable = model->data(index, ServiceModel::ConfigurableRole).toBool();
99
100 int checkBoxWidth = option.rect.width();
101 if (configurable) {
102 checkBoxWidth -= configureButton->sizeHint().width();
103 }
104 checkBox->resize(checkBoxWidth, checkBox->sizeHint().height());
105 checkBox->move(0, (itemHeight - checkBox->height()) / 2);
106
107 // Update the configuration button
108 if (configurable) {
109 configureButton->setEnabled(checkBox->isChecked());
110 configureButton->setIcon(QIcon::fromTheme(QStringLiteral("configure")));
111 configureButton->resize(configureButton->sizeHint());
112 configureButton->move(option.rect.right() - configureButton->width(),
113 (itemHeight - configureButton->height()) / 2);
114 }
115 configureButton->setVisible(configurable);
116 }
117
118 void ServiceItemDelegate::slotCheckBoxClicked(bool checked)
119 {
120 QAbstractItemModel* model = const_cast<QAbstractItemModel*>(focusedIndex().model());
121 model->setData(focusedIndex(), checked, Qt::CheckStateRole);
122 }
123
124 void ServiceItemDelegate::slotConfigureButtonClicked()
125 {
126 emit requestServiceConfiguration(focusedIndex());
127 }
128