Fix ALL the license headers!
[quassel.git] / src / uisupport / flatproxymodel.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2012 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 #ifndef FLATPROXYMODEL_H
22 #define FLATPROXYMODEL_H
23
24 #include <QAbstractProxyModel>
25
26 class FlatProxyModel : public QAbstractProxyModel
27 {
28     Q_OBJECT
29
30 public:
31     FlatProxyModel(QObject *parent = 0);
32
33     virtual QModelIndex mapFromSource(const QModelIndex &sourceIndex) const;
34     virtual QModelIndex mapToSource(const QModelIndex &proxyIndex) const;
35
36     virtual QItemSelection mapSelectionFromSource(const QItemSelection &sourceSelection) const;
37     virtual QItemSelection mapSelectionToSource(const QItemSelection &proxySelection) const;
38
39     virtual void setSourceModel(QAbstractItemModel *sourceModel);
40
41     virtual QModelIndex index(int row, int column, const QModelIndex &parent) const;
42     virtual QModelIndex parent(const QModelIndex &index) const;
43
44     virtual int rowCount(const QModelIndex &index) const;
45     virtual int columnCount(const QModelIndex &index) const;
46
47 public slots:
48     void linkTest() const;
49     void completenessTest() const;
50
51 private slots:
52     void on_columnsAboutToBeInserted(const QModelIndex &parent, int start, int end);
53     void on_columnsAboutToBeRemoved(const QModelIndex &parent, int start, int end);
54     void on_columnsInserted(const QModelIndex &parent, int start, int end);
55     void on_columnsRemoved(const QModelIndex &parent, int start, int end);
56
57     void on_dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight);
58 //   void on_headerDataChanged(Qt::Orientation orientation, int first, int last);
59
60     void on_layoutAboutToBeChanged();
61     void on_layoutChanged();
62
63     inline void on_modelAboutToBeReset() { reset(); }
64     // void on_modelReset();
65
66     void on_rowsAboutToBeInserted(const QModelIndex &parent, int start, int end);
67     void on_rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end);
68     void on_rowsInserted(const QModelIndex &parent, int start, int end);
69     void on_rowsRemoved(const QModelIndex &parent, int start, int end);
70
71 private:
72     QList<int> _childCount;
73
74     class SourceItem;
75     SourceItem *_rootSourceItem;
76
77     void insertSubTree(const QModelIndex &source_idx, bool emitInsert = true);
78     SourceItem *insertSubTreeHelper(SourceItem *parentItem, SourceItem *lastItem_, const QModelIndex &source_idx);
79
80     void removeSubTree(const QModelIndex &source_idx, bool emitRemove = true);
81
82     SourceItem *sourceToInternal(const QModelIndex &sourceIndex) const;
83
84     void checkChildCount(const QModelIndex &index, const SourceItem *item, int &pos) const;
85
86     class _RangeRect
87     {
88 public:
89         int left, right, top, bottom;
90         SourceItem *topItem, *bottomItem;
91         bool operator<(const _RangeRect &other) const;
92     };
93 };
94
95
96 class FlatProxyModel::SourceItem
97 {
98 public:
99     SourceItem(int row = 0, SourceItem *parent = 0);
100     ~SourceItem();
101
102     inline SourceItem *parent() const { return _parent; }
103     inline SourceItem *child(int i) const { return _childs[i]; }
104     inline int childCount() const { return _childs.count(); }
105
106     inline int pos() const { return _pos; }
107     inline SourceItem *next() const { return _next; }
108
109     int sourceRow() const;
110     SourceItem *findChild(int proxyPos) const;
111
112 private:
113     inline void removeChild(SourceItem *item) { _childs.removeAt(_childs.indexOf(item)); }
114     inline void setPos(int i) { _pos = i; }
115     inline void setNext(SourceItem *next) { _next = next; }
116
117     SourceItem *_parent;
118     QList<SourceItem *> _childs;
119     int _pos;
120     SourceItem *_next;
121
122     friend class FlatProxyModel;
123 };
124
125
126 #endif //FLATPROXYMODEL_H