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 
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  const QModelIndex &index) const
30  {
31  Q_UNUSED(index);
32  Q_UNUSED(option);
33  auto *cb = new QCheckBox(parent);
34  if (!m_iconCheckedUrl.isEmpty() && !m_iconUncheckedUrl.isEmpty())
35  {
36  const QString style = CStyleSheetUtility::styleForIconCheckBox(m_iconCheckedUrl, m_iconUncheckedUrl);
37  cb->setStyleSheet("");
38  cb->setStyleSheet(style);
39  }
40  return cb;
41  }
42 
43  void CCheckBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
44  {
45  const bool v = index.model()->data(index, Qt::UserRole).toBool();
46  auto *cb = qobject_cast<QCheckBox *>(editor);
47  cb->setChecked(v);
48  }
49 
50  void CCheckBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
51  {
52  auto *cb = qobject_cast<QCheckBox *>(editor);
53  const bool v = cb->isChecked();
54  model->setData(index, QVariant(v), Qt::EditRole);
55  }
56 
58  const QModelIndex &index) const
59  {
60  Q_UNUSED(index);
61  editor->setGeometry(option.rect);
62  }
63 
64 } // 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.
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
CCheckBoxDelegate(QObject *parent=nullptr)
Constructor.
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
void setEditorData(QWidget *editor, const QModelIndex &index) const
Views, mainly QTableView.
GUI related classes.
virtual QVariant data(const QModelIndex &index, int role) const const=0
virtual bool setData(const QModelIndex &index, const QVariant &value, int role)
const QAbstractItemModel * model() const const
QObject * parent() const const
bool isEmpty() const const
UserRole
bool toBool() const const
void setGeometry(const QRect &)