swift
checkboxdelegate.cpp
1 // SPDX-FileCopyrightText: Copyright (C) 2013 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
5 
6 #include <QAbstractItemModel>
7 #include <QCheckBox>
8 #include <QModelIndex>
9 #include <QVariant>
10 #include <QWidget>
11 #include <Qt>
12 #include <QtGlobal>
13 
14 #include "gui/stylesheetutility.h"
15 
16 using namespace swift::gui;
17 
18 namespace swift::gui::views
19 {
20 
21  CCheckBoxDelegate::CCheckBoxDelegate(QObject *parent) : QItemDelegate(parent) {}
22 
23  CCheckBoxDelegate::CCheckBoxDelegate(const QString &iconCheckedUrl, const QString &iconUncheckedUrl,
24  QObject *parent)
25  : QItemDelegate(parent), m_iconCheckedUrl(iconCheckedUrl), m_iconUncheckedUrl(iconUncheckedUrl)
26  {}
27 
29 
30  QWidget *CCheckBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option,
31  const QModelIndex &index) const
32  {
33  Q_UNUSED(index);
34  Q_UNUSED(option);
35  QCheckBox *cb = new QCheckBox(parent);
36  if (!m_iconCheckedUrl.isEmpty() && !m_iconUncheckedUrl.isEmpty())
37  {
38  const QString style = CStyleSheetUtility::styleForIconCheckBox(m_iconCheckedUrl, m_iconUncheckedUrl);
39  cb->setStyleSheet("");
40  cb->setStyleSheet(style);
41  }
42  return cb;
43  }
44 
45  void CCheckBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
46  {
47  const bool v = index.model()->data(index, Qt::UserRole).toBool();
48  QCheckBox *cb = qobject_cast<QCheckBox *>(editor);
49  cb->setChecked(v);
50  }
51 
52  void CCheckBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
53  {
54  QCheckBox *cb = qobject_cast<QCheckBox *>(editor);
55  const bool v = cb->isChecked();
56  model->setData(index, QVariant(v), Qt::EditRole);
57  }
58 
59  void CCheckBoxDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option,
60  const QModelIndex &index) const
61  {
62  Q_UNUSED(index);
63  editor->setGeometry(option.rect);
64  }
65 
66 } // namespace swift::gui::views
static QString styleForIconCheckBox(const QString &checkedIcon, const QString &uncheckedIcon, const QString &width="16px", const QString &height="16px")
Stylesheet string for a checkbox displayed as 2 icons.
virtual QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
virtual void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
CCheckBoxDelegate(QObject *parent=nullptr)
Constructor.
virtual void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
virtual void setEditorData(QWidget *editor, const QModelIndex &index) const
Views, mainly QTableView.
GUI related classes.