1 /************************************************************************** 2 ** This file is part of LiteIDE 3 ** 4 ** Copyright (c) 2011-2017 LiteIDE. All rights reserved. 5 ** 6 ** This library is free software; you can redistribute it and/or 7 ** modify it under the terms of the GNU Lesser General Public 8 ** License as published by the Free Software Foundation; either 9 ** version 2.1 of the License, or (at your option) any later version. 10 ** 11 ** This library is distributed in the hope that it will be useful, 12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of 13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 ** Lesser General Public License for more details. 15 ** 16 ** In addition, as a special exception, that plugins developed for LiteIDE, 17 ** are allowed to remain closed sourced and can be distributed under any license . 18 ** These rights are included in the file LGPL_EXCEPTION.txt in this package. 19 ** 20 **************************************************************************/ 21 // Module: multiindexmodel_p.h 22 // Creator: visualfc <visualfc@gmail.com> 23 24 #ifndef MULTIINDEXMODEL_P_H 25 #define MULTIINDEXMODEL_P_H 26 27 #include <QList> 28 #include <QVector> 29 #include <QPair> 30 #include <QModelIndex> 31 #include "abstractmultiproxymodel_p.h" 32 #include "multiindexmodel.h" 33 34 typedef QList<QPair<QModelIndex, QPersistentModelIndex> > QModelIndexPairList; 35 36 struct Mapping 37 { 38 int rowCount; 39 QVector<int> source_rows; //source rows 40 QVector<int> proxy_rows; //proxy rows 41 QModelIndex sourceParent; //source parent 42 QAbstractItemModel *sourceModel; 43 }; 44 45 class MultiIndexModel; 46 class MultiIndexModelPrivate : public AbstractMultiProxyModelPrivate 47 { 48 Q_OBJECT 49 public: MultiIndexModelPrivate()50 MultiIndexModelPrivate() 51 { 52 source_sort_column = -1; 53 sort_order = Qt::AscendingOrder; 54 sort_casesensitivity = Qt::CaseSensitive; 55 sort_role = Qt::DisplayRole; 56 sort_localeaware = false; 57 } 58 59 Q_DECLARE_PUBLIC(MultiIndexModel) 60 61 mutable QMap<QAbstractItemModel*, QMap<QModelIndex,Mapping*> > modelMapping; 62 63 int sort_order; 64 int source_sort_column; 65 Qt::CaseSensitivity sort_casesensitivity; 66 int sort_role; 67 bool sort_localeaware; 68 QModelIndexPairList saved_persistent_indexes; 69 QList<Mapping*> removeMappingList; 70 71 public slots: 72 void _q_sourceRowsAboutToBeInserted(const QModelIndex &parent, int start, int end); 73 void _q_sourceRowsInserted(const QModelIndex &parent, int start, int end); 74 void _q_sourceRowsAboutToBeRemoved(const QModelIndex &parent, int start, int end); 75 void _q_sourceRowsRemoved(const QModelIndex &parent, int start, int end); 76 void _q_sourceRowsAboutToBeMoved(const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destParent, int dest); 77 void _q_sourceRowsMoved(const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destParent, int dest); 78 79 void _q_sourceColumnsAboutToBeInserted(const QModelIndex &parent, int start, int end); 80 void _q_sourceColumnsInserted(const QModelIndex &parent, int start, int end); 81 void _q_sourceColumnsAboutToBeRemoved(const QModelIndex &parent, int start, int end); 82 void _q_sourceColumnsRemoved(const QModelIndex &parent, int start, int end); 83 void _q_sourceColumnsAboutToBeMoved(const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destParent, int dest); 84 void _q_sourceColumnsMoved(const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destParent, int dest); 85 86 void _q_sourceDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight); 87 void _q_sourceHeaderDataChanged(Qt::Orientation orientation, int first, int last); 88 89 void _q_sourceLayoutAboutToBeChanged(); 90 void _q_sourceLayoutChanged(); 91 void _q_sourceModelAboutToBeReset(); 92 void _q_sourceModelReset(); 93 protected: 94 QMap<QModelIndex,Mapping*>::iterator createMapping(QAbstractItemModel *model, const QModelIndex &parent, bool forceUpdate, const QString &context = "update") const; 95 QMap<QModelIndex,Mapping*>::iterator findOrCreateMapping(QAbstractItemModel *model, const QModelIndex &parent) const; 96 void build_source_to_proxy_mapping(const QVector<int> &proxy_to_source, QVector<int> &source_to_proxy) const; 97 void clearMapping(); 98 void deleteMapping(QAbstractItemModel *model); 99 isRootIndexRow(QAbstractItemModel * model,const QModelIndex & index)100 inline int isRootIndexRow(QAbstractItemModel *model, const QModelIndex &index) const 101 { 102 int row = 0; 103 foreach (SourceModelIndex i, indexList) { 104 if (i.model == model && i.index.internalId() == index.internalId()) { 105 return row; 106 } 107 row++; 108 } 109 return -1; 110 } 111 112 void sort_source_rows(QAbstractItemModel *model, QVector<int> &source_rows, const QModelIndex &source_parent) const; 113 QModelIndexPairList store_persistent_indexes(const QModelIndexList &persistentList); 114 void update_persistent_indexes(const QModelIndexPairList &source_indexes); 115 116 public: 117 void sort(QAbstractItemModel *sourceModel); 118 }; 119 120 #endif // MULTIINDEXMODEL_P_H 121 122