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