]>
cloud.milkyroute.net Git - dolphin.git/blob - src/settings/serviceitemdelegate.cpp
3dc314631a1ac4845e735fa52714d113c1412387
1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
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. *
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. *
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 ***************************************************************************/
20 #include "serviceitemdelegate.h"
23 #include <KPushButton>
25 #include "servicemodel.h"
27 #include <QAbstractItemView>
29 #include <QModelIndex>
32 ServiceItemDelegate::ServiceItemDelegate(QAbstractItemView
* itemView
, QObject
* parent
) :
33 KWidgetItemDelegate(itemView
, parent
)
37 ServiceItemDelegate::~ServiceItemDelegate()
41 QSize
ServiceItemDelegate::sizeHint(const QStyleOptionViewItem
&option
,
42 const QModelIndex
&index
) const
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
));
53 void ServiceItemDelegate::paint(QPainter
* painter
, const QStyleOptionViewItem
& option
,
54 const QModelIndex
& index
) const
59 itemView()->style()->drawPrimitive(QStyle::PE_PanelItemViewItem
, &option
, painter
);
61 if (option
.state
& QStyle::State_Selected
) {
62 painter
->setPen(option
.palette
.highlightedText().color());
68 QList
<QWidget
*> ServiceItemDelegate::createItemWidgets() const
70 QCheckBox
* checkBox
= new QCheckBox();
71 connect(checkBox
, SIGNAL(clicked(bool)), this, SLOT(slotCheckBoxClicked(bool)));
73 KPushButton
* configureButton
= new KPushButton();
74 connect(configureButton
, SIGNAL(clicked()), this, SLOT(slotConfigureButtonClicked()));
76 return QList
<QWidget
*>() << checkBox
<< configureButton
;
79 void ServiceItemDelegate::updateItemWidgets(const QList
<QWidget
*> widgets
,
80 const QStyleOptionViewItem
& option
,
81 const QPersistentModelIndex
& index
) const
83 QCheckBox
* checkBox
= static_cast<QCheckBox
*>(widgets
[0]);
84 KPushButton
*configureButton
= static_cast<KPushButton
*>(widgets
[1]);
86 const int itemHeight
= sizeHint(option
, index
).height();
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
));
95 checkBox
->setChecked(model
->data(index
, Qt::CheckStateRole
).toBool());
97 const bool configurable
= model
->data(index
, ServiceModel::ConfigurableRole
).toBool();
99 int checkBoxWidth
= option
.rect
.width();
101 checkBoxWidth
-= configureButton
->sizeHint().width();
103 checkBox
->resize(checkBoxWidth
, checkBox
->sizeHint().height());
104 checkBox
->move(0, (itemHeight
- checkBox
->height()) / 2);
106 // Update the configuration button
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);
114 configureButton
->setVisible(configurable
);
117 void ServiceItemDelegate::slotCheckBoxClicked(bool checked
)
119 QAbstractItemModel
* model
= const_cast<QAbstractItemModel
*>(focusedIndex().model());
120 model
->setData(focusedIndex(), checked
, Qt::CheckStateRole
);
123 void ServiceItemDelegate::slotConfigureButtonClicked()
125 emit
requestServiceConfiguration(focusedIndex());
128 #include "serviceitemdelegate.moc"