]> cloud.milkyroute.net Git - dolphin.git/blob - src/settings/serviceitemdelegate.cpp
Fix default sort-role
[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 #include <KIcon>
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
70 {
71 QCheckBox* checkBox = new QCheckBox();
72 connect(checkBox, SIGNAL(clicked(bool)), this, SLOT(slotCheckBoxClicked(bool)));
73
74 KPushButton* configureButton = new KPushButton();
75 connect(configureButton, SIGNAL(clicked()), this, SLOT(slotConfigureButtonClicked()));
76
77 return QList<QWidget*>() << 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 KPushButton *configureButton = static_cast<KPushButton*>(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(KIcon(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(KIcon("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
129 #include "serviceitemdelegate.moc"