itom  4.1.0
checkableModelHelper.h
1 /* ********************************************************************
2  itom software
3  URL: http://www.uni-stuttgart.de/ito
4  Copyright (C) 2020, Institut fuer Technische Optik (ITO),
5  Universitaet Stuttgart, Germany
6 
7  This file is part of itom and its software development toolkit (SDK).
8 
9  itom is free software; you can redistribute it and/or modify it
10  under the terms of the GNU Library General Public Licence as published by
11  the Free Software Foundation; either version 2 of the Licence, or (at
12  your option) any later version.
13 
14  In addition, as a special exception, the Institut fuer Technische
15  Optik (ITO) gives you certain additional rights.
16  These rights are described in the ITO LGPL Exception version 1.0,
17  which can be found in the file LGPL_EXCEPTION.txt in this package.
18 
19  itom is distributed in the hope that it will be useful, but
20  WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library
22  General Public Licence for more details.
23 
24  You should have received a copy of the GNU Library General Public License
25  along with itom. If not, see <http://www.gnu.org/licenses/>.
26 
27  This file is a port and modified version of the
28  Common framework (http://www.commontk.org)
29 *********************************************************************** */
30 
31 #ifndef CHECKABLEMODELHELPER_H
32 #define CHECKABLEMODELHELPER_H
33 
34 // Qt includes
35 #include <QModelIndex>
36 #include <QObject>
37 class QAbstractItemModel;
38 
39 
41 
45 class CheckableModelHelper : public QObject
46 {
47  Q_OBJECT;
48  Q_PROPERTY(bool forceCheckability READ forceCheckability WRITE setForceCheckability);
49  Q_PROPERTY(int propagateDepth READ propagateDepth WRITE setPropagateDepth);
50  Q_PROPERTY(Qt::CheckState defaultCheckState READ defaultCheckState WRITE setDefaultCheckState);
51 
52 public:
53  CheckableModelHelper(Qt::Orientation orientation, QObject *parent=0);
54  virtual ~CheckableModelHelper();
55 
56  Qt::Orientation orientation()const;
57 
58 
66  QAbstractItemModel *model()const;
67  virtual void setModel(QAbstractItemModel *model);
68 
70  QModelIndex rootIndex()const;
71  virtual void setRootIndex(const QModelIndex &index);
72 
77  bool isHeaderCheckable(int section)const;
78  bool isCheckable(const QModelIndex& index)const;
79 
84  Qt::CheckState headerCheckState(int section)const;
85  Qt::CheckState checkState(const QModelIndex&)const;
86 
91  bool headerCheckState(int section, Qt::CheckState& checkState )const;
92  bool checkState(const QModelIndex&, Qt::CheckState& checkState )const;
93 
97  void setPropagateDepth(int depth);
98  int propagateDepth()const;
99 
101  void setForceCheckability(bool force);
102  bool forceCheckability()const;
103 
104  Qt::CheckState defaultCheckState()const;
105  void setDefaultCheckState(Qt::CheckState);
106 
107 public Q_SLOTS:
108  void setCheckState(const QModelIndex& modelIndex, Qt::CheckState checkState);
112  void setHeaderCheckState(int section, Qt::CheckState checkState);
113 
115  void toggleCheckState(const QModelIndex& modelIndex);
116  void toggleHeaderCheckState(int section);
117 
118 private Q_SLOTS:
119  void onHeaderDataChanged(Qt::Orientation orient, int first, int last);
120 
121  void onDataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight);
122  void updateHeadersFromItems();
123  void onColumnsInserted(const QModelIndex& parent, int start, int end);
124  void onRowsInserted(const QModelIndex& parent, int start, int end);
125 
126 protected:
127  QScopedPointer<CheckableModelHelperPrivate> d_ptr;
128 
129 private:
130  Q_DECLARE_PRIVATE(CheckableModelHelper);
131  Q_DISABLE_COPY(CheckableModelHelper);
132 };
133 
134 #endif
bool isHeaderCheckable(int section) const
Definition: checkableModelHelper.cpp:627
QAbstractItemModel * model() const
Definition: checkableModelHelper.cpp:314
Qt::CheckState headerCheckState(int section) const
Definition: checkableModelHelper.cpp:649
Definition: checkableModelHelper.cpp:41
void setPropagateDepth(int depth)
Definition: checkableModelHelper.cpp:394
QModelIndex rootIndex() const
Reimplemented for internal reasons.
Definition: checkableModelHelper.cpp:376
void setForceCheckability(bool force)
When true, the new items are automatically set to checkable.
Definition: checkableModelHelper.cpp:429
Definition: checkableModelHelper.h:45
void toggleCheckState(const QModelIndex &modelIndex)
Utility function to toggle the checkstate of an index.
Definition: checkableModelHelper.cpp:487
void setHeaderCheckState(int section, Qt::CheckState checkState)
Definition: checkableModelHelper.cpp:465