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