]> 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 "servicemodel.h"
23
24 #include <QAbstractItemView>
25 #include <QCheckBox>
26 #include <QPainter>
27 #include <QPushButton>
28
29 ServiceItemDelegate::ServiceItemDelegate(QAbstractItemView* itemView, QObject* parent) :
30 KWidgetItemDelegate(itemView, parent)
31 {
32 }
33
34 ServiceItemDelegate::~ServiceItemDelegate()
35 {
36 }
37
38 QSize ServiceItemDelegate::sizeHint(const QStyleOptionViewItem &option,
39 const QModelIndex &index) const
40 {
41 Q_UNUSED(index);
42
43 const QStyle *style = itemView()->style();
44 const int buttonHeight = style->pixelMetric(QStyle::PM_ButtonMargin) * 2 +
45 style->pixelMetric(QStyle::PM_ButtonIconSize);
46 const int fontHeight = option.fontMetrics.height();
47 return QSize(100, qMax(buttonHeight, fontHeight));
48 }
49
50 void ServiceItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option,
51 const QModelIndex& index) const
52 {
53 Q_UNUSED(index);
54 painter->save();
55
56 itemView()->style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter);
57
58 if (option.state & QStyle::State_Selected) {
59 painter->setPen(option.palette.highlightedText().color());
60 }
61
62 painter->restore();
63 }
64
65 QList<QWidget*> ServiceItemDelegate::createItemWidgets(const QModelIndex&) const
66 {
67 QCheckBox* checkBox = new QCheckBox();
68 QPalette palette = checkBox->palette();
69 palette.setColor(QPalette::WindowText, palette.color(QPalette::Text));
70 checkBox->setPalette(palette);
71 connect(checkBox, &QCheckBox::clicked, this, &ServiceItemDelegate::slotCheckBoxClicked);
72
73 QPushButton* configureButton = new QPushButton();
74 connect(configureButton, &QPushButton::clicked, this, &ServiceItemDelegate::slotConfigureButtonClicked);
75
76 return {checkBox, configureButton};
77 }
78
79 void ServiceItemDelegate::updateItemWidgets(const QList<QWidget*> widgets,
80 const QStyleOptionViewItem& option,
81 const QPersistentModelIndex& index) const
82 {
83 QCheckBox* checkBox = static_cast<QCheckBox*>(widgets[0]);
84 QPushButton *configureButton = static_cast<QPushButton*>(widgets[1]);
85
86 const int itemHeight = sizeHint(option, index).height();
87
88 // Update the checkbox showing the service name and icon
89 const QAbstractItemModel* model = index.model();
90 checkBox->setText(model->data(index).toString());
91 const QString iconName = model->data(index, Qt::DecorationRole).toString();
92 if (!iconName.isEmpty()) {
93 checkBox->setIcon(QIcon::fromTheme(iconName));
94 }
95 checkBox->setChecked(model->data(index, Qt::CheckStateRole).toBool());
96
97 const bool configurable = model->data(index, ServiceModel::ConfigurableRole).toBool();
98
99 int checkBoxWidth = option.rect.width();
100 if (configurable) {
101 checkBoxWidth -= configureButton->sizeHint().width();
102 }
103 checkBox->resize(checkBoxWidth, checkBox->sizeHint().height());
104 checkBox->move(0, (itemHeight - checkBox->height()) / 2);
105
106 // Update the configuration button
107 if (configurable) {
108 configureButton->setEnabled(checkBox->isChecked());
109 configureButton->setIcon(QIcon::fromTheme(QStringLiteral("configure")));
110 configureButton->resize(configureButton->sizeHint());
111 configureButton->move(option.rect.right() - configureButton->width(),
112 (itemHeight - configureButton->height()) / 2);
113 }
114 configureButton->setVisible(configurable);
115 }
116
117 void ServiceItemDelegate::slotCheckBoxClicked(bool checked)
118 {
119 QAbstractItemModel* model = const_cast<QAbstractItemModel*>(focusedIndex().model());
120 model->setData(focusedIndex(), checked, Qt::CheckStateRole);
121 }
122
123 void ServiceItemDelegate::slotConfigureButtonClicked()
124 {
125 emit requestServiceConfiguration(focusedIndex());
126 }
127