cmake: Add missing Boost dependency
[quassel.git] / src / uisupport / flatproxymodel.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) version 3.                                           *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #pragma once
22
23 #include "uisupport-export.h"
24
25 #include <QAbstractProxyModel>
26
27 class UISUPPORT_EXPORT FlatProxyModel : public QAbstractProxyModel
28 {
29     Q_OBJECT
30
31 public:
32     FlatProxyModel(QObject* parent = nullptr);
33
34     QModelIndex mapFromSource(const QModelIndex& sourceIndex) const override;
35     QModelIndex mapToSource(const QModelIndex& proxyIndex) const override;
36
37     QItemSelection mapSelectionFromSource(const QItemSelection& sourceSelection) const override;
38     QItemSelection mapSelectionToSource(const QItemSelection& proxySelection) const override;
39
40     void setSourceModel(QAbstractItemModel* sourceModel) override;
41
42     QModelIndex index(int row, int column, const QModelIndex& parent) const override;
43     QModelIndex parent(const QModelIndex& index) const override;
44
45     int rowCount(const QModelIndex& index) const override;
46     int columnCount(const QModelIndex& index) const override;
47
48 public slots:
49     void linkTest() const;
50     void completenessTest() const;
51
52 private slots:
53     void on_columnsAboutToBeInserted(const QModelIndex& parent, int start, int end);
54     void on_columnsAboutToBeRemoved(const QModelIndex& parent, int start, int end);
55     void on_columnsInserted(const QModelIndex& parent, int start, int end);
56     void on_columnsRemoved(const QModelIndex& parent, int start, int end);
57
58     void on_dataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight);
59     //   void on_headerDataChanged(Qt::Orientation orientation, int first, int last);
60
61     void on_layoutAboutToBeChanged();
62     void on_layoutChanged();
63
64     inline void on_modelAboutToBeReset()
65     {
66         beginResetModel();
67         endResetModel();
68     }
69     // void on_modelReset();
70
71     void on_rowsAboutToBeInserted(const QModelIndex& parent, int start, int end);
72     void on_rowsAboutToBeRemoved(const QModelIndex& parent, int start, int end);
73     void on_rowsInserted(const QModelIndex& parent, int start, int end);
74     void on_rowsRemoved(const QModelIndex& parent, int start, int end);
75
76 private:
77     QList<int> _childCount;
78
79     class SourceItem;
80     SourceItem* _rootSourceItem{nullptr};
81
82     void insertSubTree(const QModelIndex& source_idx, bool emitInsert = true);
83     SourceItem* insertSubTreeHelper(SourceItem* parentItem, SourceItem* lastItem_, const QModelIndex& source_idx);
84
85     void removeSubTree(const QModelIndex& source_idx, bool emitRemove = true);
86
87     SourceItem* sourceToInternal(const QModelIndex& sourceIndex) const;
88
89     void checkChildCount(const QModelIndex& index, const SourceItem* item, int& pos) const;
90
91     class _RangeRect
92     {
93     public:
94         int left, right, top, bottom;
95         SourceItem *topItem, *bottomItem;
96         bool operator<(const _RangeRect& other) const;
97     };
98 };
99
100 class FlatProxyModel::SourceItem
101 {
102 public:
103     SourceItem(int row = 0, SourceItem* parent = nullptr);
104     ~SourceItem();
105
106     inline SourceItem* parent() const { return _parent; }
107     inline SourceItem* child(int i) const { return _childs[i]; }
108     inline int childCount() const { return _childs.count(); }
109
110     inline int pos() const { return _pos; }
111     inline SourceItem* next() const { return _next; }
112
113     int sourceRow() const;
114     SourceItem* findChild(int proxyPos) const;
115
116 private:
117     inline void removeChild(SourceItem* item) { _childs.removeAt(_childs.indexOf(item)); }
118     inline void setPos(int i) { _pos = i; }
119     inline void setNext(SourceItem* next) { _next = next; }
120
121     SourceItem* _parent;
122     QList<SourceItem*> _childs;
123     int _pos{-1};
124     SourceItem* _next{nullptr};
125
126     friend class FlatProxyModel;
127 };