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"
22 #include "dolphindebug.h"
23 #include <QPushButton>
26 #include "servicemodel.h"
28 #include <QAbstractItemView>
30 #include <QModelIndex>
33 ServiceItemDelegate::ServiceItemDelegate(QAbstractItemView
* itemView
, QObject
* parent
) :
34 KWidgetItemDelegate(itemView
, parent
)
38 ServiceItemDelegate::~ServiceItemDelegate()
42 QSize
ServiceItemDelegate::sizeHint(const QStyleOptionViewItem
&option
,
43 const QModelIndex
&index
) const
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
));
54 void ServiceItemDelegate::paint(QPainter
* painter
, const QStyleOptionViewItem
& option
,
55 const QModelIndex
& index
) const
60 itemView()->style()->drawPrimitive(QStyle::PE_PanelItemViewItem
, &option
, painter
);
62 if (option
.state
& QStyle::State_Selected
) {
63 painter
->setPen(option
.palette
.highlightedText().color());
69 QList
<QWidget
*> ServiceItemDelegate::createItemWidgets(const QModelIndex
&) const
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
);
77 QPushButton
* configureButton
= new QPushButton();
78 connect(configureButton
, &QPushButton::clicked
, this, &ServiceItemDelegate::slotConfigureButtonClicked
);
80 return {checkBox
, configureButton
};
83 void ServiceItemDelegate::updateItemWidgets(const QList
<QWidget
*> widgets
,
84 const QStyleOptionViewItem
& option
,
85 const QPersistentModelIndex
& index
) const
87 QCheckBox
* checkBox
= static_cast<QCheckBox
*>(widgets
[0]);
88 QPushButton
*configureButton
= static_cast<QPushButton
*>(widgets
[1]);
90 const int itemHeight
= sizeHint(option
, index
).height();
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
));
99 checkBox
->setChecked(model
->data(index
, Qt::CheckStateRole
).toBool());
101 const bool configurable
= model
->data(index
, ServiceModel::ConfigurableRole
).toBool();
103 int checkBoxWidth
= option
.rect
.width();
105 checkBoxWidth
-= configureButton
->sizeHint().width();
107 checkBox
->resize(checkBoxWidth
, checkBox
->sizeHint().height());
108 checkBox
->move(0, (itemHeight
- checkBox
->height()) / 2);
110 // Update the configuration button
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);
118 configureButton
->setVisible(configurable
);
121 void ServiceItemDelegate::slotCheckBoxClicked(bool checked
)
123 QAbstractItemModel
* model
= const_cast<QAbstractItemModel
*>(focusedIndex().model());
124 model
->setData(focusedIndex(), checked
, Qt::CheckStateRole
);
127 void ServiceItemDelegate::slotConfigureButtonClicked()
129 emit
requestServiceConfiguration(focusedIndex());