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 "servicemodel.h"
24 #include <QAbstractItemView>
27 #include <QPushButton>
29 ServiceItemDelegate::ServiceItemDelegate(QAbstractItemView
* itemView
, QObject
* parent
) :
30 KWidgetItemDelegate(itemView
, parent
)
34 ServiceItemDelegate::~ServiceItemDelegate()
38 QSize
ServiceItemDelegate::sizeHint(const QStyleOptionViewItem
&option
,
39 const QModelIndex
&index
) const
43 const QStyle
*style
= itemView()->style();
44 const int buttonHeight
= style
->pixelMetric(QStyle::PM_ButtonMargin
) * 2 +
45 style
->pixelMetric(QStyle::PM_ButtonIconSize
);
46 const int fontHeight
= option
.fontMetrics
.height();
47 return QSize(100, qMax(buttonHeight
, fontHeight
));
50 void ServiceItemDelegate::paint(QPainter
* painter
, const QStyleOptionViewItem
& option
,
51 const QModelIndex
& index
) const
56 itemView()->style()->drawPrimitive(QStyle::PE_PanelItemViewItem
, &option
, painter
);
58 if (option
.state
& QStyle::State_Selected
) {
59 painter
->setPen(option
.palette
.highlightedText().color());
65 QList
<QWidget
*> ServiceItemDelegate::createItemWidgets(const QModelIndex
&) const
67 QCheckBox
* checkBox
= new QCheckBox();
68 QPalette palette
= checkBox
->palette();
69 palette
.setColor(QPalette::WindowText
, palette
.color(QPalette::Text
));
70 checkBox
->setPalette(palette
);
71 connect(checkBox
, &QCheckBox::clicked
, this, &ServiceItemDelegate::slotCheckBoxClicked
);
73 QPushButton
* configureButton
= new QPushButton();
74 connect(configureButton
, &QPushButton::clicked
, this, &ServiceItemDelegate::slotConfigureButtonClicked
);
76 return {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 QPushButton
*configureButton
= static_cast<QPushButton
*>(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(QIcon::fromTheme(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(QIcon::fromTheme(QStringLiteral("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());