LeechCraft  0.6.70-18450-gabe19ee3b0
Modular cross-platform feature rich live environment.
checkableproxymodel.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * LeechCraft - modular cross-platform feature rich internet client.
3  * Copyright (C) 2006-2014 Georg Rudoy
4  *
5  * Distributed under the Boost Software License, Version 1.0.
6  * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
7  **********************************************************************/
8 
9 #include "checkableproxymodel.h"
10 
11 namespace LC::Util
12 {
14  : QIdentityProxyModel { parent }
15  , IdRole_ { idRole }
16  {
17  }
18 
19  Qt::ItemFlags CheckableProxyModelBase::flags (const QModelIndex& index) const
20  {
21  auto flags = QIdentityProxyModel::flags (index);
22  if (index.column () == 0)
23  flags |= Qt::ItemIsUserCheckable;
24  return flags;
25  }
26 
27  QVariant CheckableProxyModelBase::data (const QModelIndex& index, int role) const
28  {
29  if (role == Qt::CheckStateRole && index.column () == 0)
30  return IsChecked (index.data (IdRole_)) ? Qt::Checked : Qt::Unchecked;
31 
32  return QIdentityProxyModel::data (index, role);
33  }
34 
35  bool CheckableProxyModelBase::setData (const QModelIndex& index, const QVariant& value, int role)
36  {
37  if (role == Qt::CheckStateRole && index.column () == 0)
38  {
39  SetChecked (index.data (IdRole_), value.value<Qt::CheckState> () == Qt::Checked);
40  emit selectionChanged ();
41  emit dataChanged (index, index, { Qt::CheckStateRole });
42  return true;
43  }
44 
45  return QIdentityProxyModel::setData (index, value, role);
46  }
47 
49  {
50  const auto rc = sourceModel ()->rowCount ();
51  for (int i = 0; i < rc; ++i)
52  {
53  const auto srcIndex = sourceModel ()->index (i, 0);
54  const auto& idVar = srcIndex.data (IdRole_);
55  if (!IsChecked (idVar))
56  {
57  SetChecked (idVar, true);
58  const auto& index = mapFromSource (srcIndex);
59  emit dataChanged (index, index, { Qt::CheckStateRole });
60  }
61  }
62 
63  emit selectionChanged ();
64  }
65 
67  {
68  const auto rc = sourceModel ()->rowCount ();
69  for (int i = 0; i < rc; ++i)
70  {
71  const auto srcIndex = sourceModel ()->index (i, 0);
72  const auto& idVar = srcIndex.data (IdRole_);
73  if (IsChecked (idVar))
74  {
75  SetChecked (idVar, false);
76  const auto& index = mapFromSource (srcIndex);
77  emit dataChanged (index, index, { Qt::CheckStateRole });
78  }
79  }
80 
81  emit selectionChanged ();
82  }
83 }
bool setData(const QModelIndex &index, const QVariant &value, int role) override
virtual void SetChecked(const QVariant &idVar, bool checked)=0
QVariant data(const QModelIndex &index, int role) const override
Qt::ItemFlags flags(const QModelIndex &index) const override
virtual bool IsChecked(const QVariant &idVar) const =0
CheckableProxyModelBase(int idRole, QObject *parent=nullptr)