1/* This file is part of the KDE project
2 Copyright (C) 2010 Dag Andersen <danders@get2net.dk>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
18*/
19
20#ifndef KPTFLATPROXYMODEL_H
21#define KPTFLATPROXYMODEL_H
22
23#include "kplatomodels_export.h"
24
25#include <QAbstractProxyModel>
26#include <QStandardItemModel>
27
28class PersistentModelIndex;
29class QModelIndex;
30class QItemSelection;
31
32/// The main namespace
33namespace KPlato
34{
35
36/**
37 FlatProxyModel is a proxy model that makes a tree source model flat.
38
39 This might be useful to present data from a tree model in e.g. a table view or a report.
40
41 Note that the source model should have the same number of columns for all parent indices,
42 since a flat model obviously have the same number of columns for all indices.
43 If this is not the case, the behavior is undefined.
44
45 The row sequence of the flat model is the same as if the source model was fully expanded.
46
47 The flat model adds a Parent column at the end of the source model columns,
48 to make it possible to access the parent index's data at column 0.
49*/
50class KPLATOMODELS_EXPORT FlatProxyModel : public QAbstractProxyModel
51{
52 Q_OBJECT
53public:
54 explicit FlatProxyModel(QObject *parent = 0);
55
56 virtual QModelIndex mapFromSource ( const QModelIndex & sourceIndex ) const;
57 virtual QItemSelection mapSelectionFromSource ( const QItemSelection & sourceSelection ) const;
58 virtual QItemSelection mapSelectionToSource ( const QItemSelection & proxySelection ) const;
59 virtual QModelIndex mapToSource ( const QModelIndex & proxyIndex ) const;
60 virtual void setSourceModel ( QAbstractItemModel * sourceModel );
61
62 QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const;
63 QModelIndex parent(const QModelIndex &child) const;
64 int rowCount( const QModelIndex &parent = QModelIndex() ) const;
65 int columnCount(const QModelIndex &parent = QModelIndex() ) const;
66 bool hasChildren(const QModelIndex &parent = QModelIndex() ) const;
67 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole ) const;
68 bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole );
69
70 QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
71 bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role = Qt::EditRole);
72
73 QMimeData *mimeData(const QModelIndexList &indexes) const;
74 QStringList mimeTypes() const;
75 Qt::DropActions supportedDropActions() const;
76 bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent = QModelIndex());
77
78 bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex());
79 bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex());
80
81
82public slots:
83 void sourceDataChanged(const QModelIndex &source_top_left,
84 const QModelIndex &source_bottom_right);
85 void sourceHeaderDataChanged(Qt::Orientation orientation, int start, int end);
86
87 void sourceReset();
88
89 void sourceLayoutAboutToBeChanged();
90 void sourceLayoutChanged();
91
92 void sourceRowsAboutToBeInserted(const QModelIndex &source_parent,
93 int start, int end);
94 void sourceRowsInserted(const QModelIndex &source_parent,
95 int start, int end);
96 void sourceRowsAboutToBeRemoved(const QModelIndex &source_parent,
97 int start, int end);
98 void sourceRowsRemoved(const QModelIndex &source_parent,
99 int start, int end);
100
101 void sourceRowsAboutToBeMoved( const QModelIndex &source_parent,
102 int start, int end, const QModelIndex &destParent, int destStart );
103 void sourceRowsMoved( const QModelIndex &source_parent,
104 int start, int end, const QModelIndex &destParent, int destStart );
105
106protected:
107 int mapFromSourceRow( const QModelIndex & sourceIndex ) const;
108 int mapToSourceRow( const QModelIndex & sourceIndex ) const;
109
110private slots:
111 void initiateMaps( const QModelIndex &sourceParent = QModelIndex() );
112 void sourceModelDestroyed();
113
114private:
115 /// List of sourceIndexes
116 QList<QPersistentModelIndex> m_sourceIndexList;
117 /// Map of sourceIndexes (parent, index)
118 QMultiMap<QPersistentModelIndex, QPersistentModelIndex> m_sourceIndexMap;
119};
120
121} //namespace KPlato
122
123
124#endif
125