Isis 3 Programmer Reference
SubTreeProxyModel.cpp
1#include "SubTreeProxyModel.h"
2
3#include <QAbstractItemModel>
4#include <QIdentityProxyModel>
5#include <QModelIndex>
6#include <QObject>
7#include <QPersistentModelIndex>
8#include <QSortFilterProxyModel>
9#include <QStandardItem>
10#include <QVariant>
11
12#include "ProjectItem.h"
13
14namespace Isis {
15
16
32
33
34 // Returns the model index in the proxy model corresponding to the sourceIndex from the
35 // source model
36 QModelIndex SubTreeProxyModel::mapFromSource(const QModelIndex &sourceIndex) const {
37 // check if the model index corresponds to the invisible root item in source model
38 if (sourceIndex ==
39 static_cast<QStandardItemModel *>(sourceModel())->invisibleRootItem()->index()) {
40 qDebug() << "creating index for invisible root item "
41 << static_cast<QStandardItemModel *>(sourceModel())->invisibleRootItem()->index();
42 return createIndex(sourceIndex.row(), 0, sourceIndex.internalId());
43 }
44
45 // First check to see if the source index is the proxy root
46 if (sourceIndex == m_root) {
47 return createIndex(sourceIndex.row(), 0, sourceIndex.internalId());
48 }
49
50 // If the source index is a child of the proxy root, one if its ancestors IS proxy root
51 QModelIndex ancestorIndex = sourceIndex.parent();
52 while (ancestorIndex.isValid() && ancestorIndex != m_root) {
53 ancestorIndex = ancestorIndex.parent();
54 }
55 if (ancestorIndex.isValid()) {
56 return createIndex(sourceIndex.row(), 0, sourceIndex.internalId());
57 }
58 else {
59 return QModelIndex();
60 }
61 }
62
63
64 //Returns the model index in the source that corresponds to the proxy index
65 //in the proxy model
66 QModelIndex SubTreeProxyModel::mapToSource(const QModelIndex &proxyIndex) const {
67 return QIdentityProxyModel::mapToSource(proxyIndex);
68 }
69
70
71 void SubTreeProxyModel::setSourceModel(QAbstractItemModel *newSourceModel) {
72 QPersistentModelIndex persistentIndex(newSourceModel->index(0,0,QModelIndex()));
73
74 if (persistentIndex.isValid()) {
75 m_root = persistentIndex;
76 }
77 else {
78 m_root = QPersistentModelIndex(QModelIndex());
79 }
80
81 QIdentityProxyModel::setSourceModel(newSourceModel);
82 }
83
84
85 bool SubTreeProxyModel::setRoot(const QStandardItem *item) {
86 QAbstractItemModel::removeRows(1,2,item->index());
87
88 if (m_root.isValid()) {
89 return true;
90 }
91 else {
92 return false;
93 }
94 }
95
96
97}
SubTreeProxyModel(QObject *parent=0)
Constructs a SubTreeProxyModel.
This is free and unencumbered software released into the public domain.
Definition Apollo.h:16