]> cloud.milkyroute.net Git - dolphin.git/blob - src/settings/serviceitemdelegate.cpp
3dc314631a1ac4845e735fa52714d113c1412387
[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 <KDebug>
23 #include <KPushButton>
24
25 #include "servicemodel.h"
26
27 #include <QAbstractItemView>
28 #include <QCheckBox>
29 #include <QModelIndex>
30 #include <QPainter>
31
32 ServiceItemDelegate::ServiceItemDelegate(QAbstractItemView* itemView, QObject* parent) :
33 KWidgetItemDelegate(itemView, parent)
34 {
35 }
36
37 ServiceItemDelegate::~ServiceItemDelegate()
38 {
39 }
40
41 QSize ServiceItemDelegate::sizeHint(const QStyleOptionViewItem &option,
42 const QModelIndex &index) const
43 {
44 Q_UNUSED(index);
45
46 const QStyle *style = itemView()->style();
47 const int buttonHeight = style->pixelMetric(QStyle::PM_ButtonMargin) * 2 +
48 style->pixelMetric(QStyle::PM_ButtonIconSize);
49 const int fontHeight = option.fontMetrics.height();
50 return QSize(100, qMax(buttonHeight, fontHeight));
51 }
52
53 void ServiceItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option,
54 const QModelIndex& index) const
55 {
56 Q_UNUSED(index);
57 painter->save();
58
59 itemView()->style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter);
60
61 if (option.state & QStyle::State_Selected) {
62 painter->setPen(option.palette.highlightedText().color());
63 }
64
65 painter->restore();
66 }
67
68 QList<QWidget*> ServiceItemDelegate::createItemWidgets() const
69 {
70 QCheckBox* checkBox = new QCheckBox();
71 connect(checkBox, SIGNAL(clicked(bool)), this, SLOT(slotCheckBoxClicked(bool)));
72
73 KPushButton* configureButton = new KPushButton();
74 connect(configureButton, SIGNAL(clicked()), this, SLOT(slotConfigureButtonClicked()));
75
76 return QList<QWidget*>() << 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 KPushButton *configureButton = static_cast<KPushButton*>(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(KIcon(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(KIcon("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
128 #include "serviceitemdelegate.moc"