LeechCraft  0.6.70-18450-gabe19ee3b0
Modular cross-platform feature rich live environment.
checkableproxymodel.h
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 #pragma once
10 
11 #include <QIdentityProxyModel>
12 #include <QSet>
13 #include "modelsconfig.h"
14 
15 namespace LC::Util
16 {
17  class UTIL_MODELS_API CheckableProxyModelBase : public QIdentityProxyModel
18  {
19  Q_OBJECT
20  protected:
21  const int IdRole_;
22  public:
23  explicit CheckableProxyModelBase (int idRole, QObject *parent = nullptr);
24 
25  Qt::ItemFlags flags (const QModelIndex& index) const override;
26  QVariant data (const QModelIndex& index, int role) const override;
27  bool setData (const QModelIndex& index, const QVariant& value, int role) override;
28 
29  void CheckAll ();
30  void CheckNone ();
31  protected:
32  virtual bool IsChecked (const QVariant& idVar) const = 0;
33  virtual void SetChecked (const QVariant& idVar, bool checked) = 0;
34  signals:
35  void selectionChanged ();
36  };
37 
38  template<typename IdType>
40  {
41  QSet<IdType> Unchecked_;
42  public:
44 
45  QSet<IdType> GetUnchecked () const
46  {
47  return Unchecked_;
48  }
49 
50  QSet<IdType> GetChecked () const
51  {
52  const auto rc = sourceModel ()->rowCount ();
53 
54  QSet<IdType> result;
55  result.reserve (rc - Unchecked_.size ());
56 
57  for (int i = 0; i < rc; ++i)
58  {
59  const auto rowId = sourceModel ()->index (i, 0).data (IdRole_).template value<IdType> ();
60  if (!Unchecked_.contains (rowId))
61  result << rowId;
62  }
63 
64  return result;
65  }
66  protected:
67  bool IsChecked (const QVariant& idVar) const override
68  {
69  return !Unchecked_.contains (idVar.value<IdType> ());
70  }
71 
72  void SetChecked (const QVariant& idVar, bool checked) override
73  {
74  const auto id = idVar.value<IdType> ();
75  if (checked)
76  Unchecked_.remove (id);
77  else
78  Unchecked_ << id;
79  }
80  };
81 }
void SetChecked(const QVariant &idVar, bool checked) override
QSet< IdType > GetChecked() const
#define UTIL_MODELS_API
Definition: modelsconfig.h:16
CheckableProxyModelBase(int idRole, QObject *parent=nullptr)
bool IsChecked(const QVariant &idVar) const override
QSet< IdType > GetUnchecked() const