Isis 3 Programmer Reference
ProjectItemProxyModel.cpp
1
7/* SPDX-License-Identifier: CC0-1.0 */
8
9#include "ProjectItemProxyModel.h"
10
11#include <QList>
12#include <QItemSelection>
13#include <QMap>
14#include <QMimeData>
15#include <QModelIndex>
16#include <QObject>
17#include <QtDebug>
18
19#include "ProjectItem.h"
20#include "ProjectItemModel.h"
21
22namespace Isis {
29 m_sourceModel = 0;
30 m_sourceProxyMap = QMap<ProjectItem *, ProjectItem *>();
31 }
32
33
43 QModelIndex ProjectItemProxyModel::mapIndexFromSource(const QModelIndex &sourceIndex) {
44 ProjectItem *proxyItem = mapItemFromSource( sourceModel()->itemFromIndex(sourceIndex) );
45
46 if (proxyItem) {
47 return proxyItem->index();
48 }
49 else {
50 return QModelIndex();
51 }
52
53 }
54
55
64 QModelIndex ProjectItemProxyModel::mapIndexToSource(const QModelIndex &proxyIndex) {
65 ProjectItem *sourceItem = mapItemToSource( itemFromIndex(proxyIndex) );
66
67 if (sourceItem) {
68 return sourceItem->index();
69 }
70 else {
71 return QModelIndex();
72 }
73 }
74
75
86 const QItemSelection &sourceSelection) {
87
88 QItemSelection proxySelection = QItemSelection();
89
90 foreach ( QModelIndex sourceIndex, sourceSelection.indexes() ) {
91 QModelIndex proxyIndex = mapIndexFromSource(sourceIndex);
92 if ( proxyIndex.isValid() ) {
93 proxySelection.select(proxyIndex, proxyIndex);
94 }
95 }
96
97 return proxySelection;
98 }
99
100
110 QItemSelection ProjectItemProxyModel::mapSelectionToSource(const QItemSelection &proxySelection) {
111 QItemSelection sourceSelection = QItemSelection();
112
113 foreach ( QModelIndex proxyIndex, proxySelection.indexes() ) {
114 QModelIndex sourceIndex = mapIndexToSource(proxyIndex);
115 if ( sourceIndex.isValid() ) {
116 sourceSelection.select(sourceIndex, sourceIndex);
117 }
118 }
119
120 return sourceSelection;
121 }
122
123
133 return m_sourceProxyMap.value(sourceItem, 0);
134 }
135
136
146 return m_sourceProxyMap.key(proxyItem, 0);
147 }
148
149
166 if (!sourceItem) {
167 return 0;
168 }
169
170 ProjectItem *proxyItem;
171
172 if (ProjectItem *parentItem = mapItemFromSource( sourceItem->parent() ) ) {
173 proxyItem = addChild(sourceItem, parentItem);
174 }
175 else {
176 proxyItem = addChild(sourceItem, 0);
177 }
178
179 for (int i=0; i < sourceItem->rowCount(); i++) {
180 addItem( sourceItem->child(i) );
181 }
182
183 return proxyItem;
184 }
185
186
193 void ProjectItemProxyModel::addItems(QList<ProjectItem *> sourceItems) {
194 foreach (ProjectItem *item, sourceItems) {
195 addItem(item);
196 }
197 emit itemsAdded();
198 }
199
200
207 if (item) {
208 m_sourceProxyMap.remove( mapItemToSource(item) );
209 }
210// qDebug()<<"ProjectItemProxyModel::removeItem item= "<<item;
212 }
213
214
223 if (m_sourceModel == sourceModel) {
224 return;
225 }
226
227 if (m_sourceModel) {
228 clear();
229 m_sourceModel->disconnect(this);
230 }
231
233
234 // If current item changes on the Source Model, update this proxy model's current item
235 connect(sourceModel->selectionModel(),
236 SIGNAL( currentChanged(const QModelIndex &, const QModelIndex &) ),
237 this, SLOT( updateProxyCurrent() ) );
238 // If current item changes on this proxy model, update the source model's current item
239 connect(selectionModel(),
240 SIGNAL( currentChanged(const QModelIndex &, const QModelIndex &) ),
241 this, SLOT( updateSourceCurrent() ) );
242 // If selection changes on the Source Model, update this proxy model's selection
243 connect(sourceModel->selectionModel(),
244 SIGNAL( selectionChanged(const QItemSelection &, const QItemSelection &) ),
245 this, SLOT( updateProxySelection() ) );
246 // If the selection changes on this proxy model, update the source model's selection
247 connect(selectionModel(),
248 SIGNAL( selectionChanged(const QItemSelection &, const QItemSelection &) ),
249 this, SLOT( updateSourceSelection() ) );
250
251 connect(sourceModel, SIGNAL( itemChanged(QStandardItem *) ),
252 this, SLOT( onItemChanged(QStandardItem *) ) );
253
254 connect(sourceModel, SIGNAL(itemRemoved(ProjectItem *)),
255 this, SIGNAL(itemRemoved(ProjectItem *)));
256 }
257
258
267
268
277 if ( ProjectItem *proxyItem = mapItemFromSource(sourceItem) ) {
278 proxyItem->setProjectItem(sourceItem);
279 }
280 }
281
282
288 QModelIndex newProxyCurrent = mapIndexFromSource(
289 sourceModel()->selectionModel()->currentIndex() );
290 if ( newProxyCurrent != selectionModel()->currentIndex() ) {
291 selectionModel()->setCurrentIndex(newProxyCurrent, QItemSelectionModel::Current);
292 }
293 }
294
295
301 QModelIndex newSourceCurrent = mapIndexToSource( selectionModel()->currentIndex() );
302 if ( newSourceCurrent != sourceModel()->selectionModel()->currentIndex() ) {
303 sourceModel()->selectionModel()->setCurrentIndex(newSourceCurrent,
304 QItemSelectionModel::Current);
305 }
306 }
307
308
314 QItemSelection newProxySelection = mapSelectionFromSource(
315 sourceModel()->selectionModel()->selection() );
316 if ( newProxySelection != selectionModel()->selection() ) {
317 selectionModel()->select(newProxySelection, QItemSelectionModel::ClearAndSelect);
318 }
319 }
320
321
327 QItemSelection newSourceSelection = mapSelectionToSource( selectionModel()->selection() );
328 if ( mapSelectionFromSource(newSourceSelection) !=
329 mapSelectionFromSource( sourceModel()->selectionModel()->selection() ) ) {
330 sourceModel()->selectionModel()->select(newSourceSelection,
331 QItemSelectionModel::ClearAndSelect);
332 }
333 }
334
335
350 if (!sourceItem) {
351 return 0;
352 }
353
354 if ( !sourceModel() ) {
355 setSourceModel( sourceItem->model() );
356 }
357
358 if ( sourceItem->model() != sourceModel() ) {
359 return 0;
360 }
361
362 if ( parentItem && (parentItem->model() != this) ) {
363 return 0;
364 }
365
366 ProjectItem *proxyItem = mapItemFromSource(sourceItem);
367
368 if (!proxyItem) {
369 proxyItem = new ProjectItem();
370 proxyItem->setProjectItem(sourceItem);
371 m_sourceProxyMap.insert(sourceItem, proxyItem);
372 }
373 else {
374 if ( ProjectItem *oldParent = proxyItem->parent() ) {
375 oldParent->takeRow( proxyItem->row() );
376 }
377 else if ( ProjectItemModel *model = proxyItem->model() ) {
378 model->takeRow( proxyItem->row() );
379 }
380 }
381
382 if (parentItem) {
383 parentItem->appendRow(proxyItem);
384 }
385 else {
386 appendRow(proxyItem);
387 }
388
389 return proxyItem;
390 }
391
392
403
404
416 bool ProjectItemProxyModel::canDropMimeData(const QMimeData *data,
417 Qt::DropAction action,
418 int row, int column,
419 const QModelIndex &parent) const {
420 return true;
421 }
422
423
435 bool ProjectItemProxyModel::dropMimeData(const QMimeData *data,
436 Qt::DropAction action,
437 int row, int column,
438 const QModelIndex &parent) {
439 if ( data->hasFormat("application/x-qabstractitemmodeldatalist") ) {
441 return true;
442 }
443 return false;
444 }
445}
Represents an item of a ProjectItemModel in Qt's model-view framework.
ProjectItem * parent() const
Returns the parent item of this item.
Provides access to data stored in a Project through Qt's model-view framework.
QList< ProjectItem * > selectedItems()
Returns a list of the selected items of the internal selection model.
void appendRow(ProjectItem *item)
Appends a top-level item to the model.
ProjectItem * itemFromIndex(const QModelIndex &index)
Returns the ProjectItem corresponding to a given QModelIndex.
ProjectItem * item(int row)
Returns the top-level item at the given row.
virtual void removeItem(ProjectItem *item)
Removes an item and its children from the model.
QItemSelectionModel * selectionModel()
Returns the internal selection model.
ProjectItemProxyModel(QObject *parent=0)
Constructs the proxy model.
void updateItem(ProjectItem *sourceItem)
Given an item in the source model, this method changes the data of the corresponding item in the prox...
ProjectItemModel * m_sourceModel
The source model. Map of items from the source model to the proxy model.
void updateProxySelection()
Slot that updates the selection in the proxy model only if it is different than the corresponding sel...
QModelIndex mapIndexFromSource(const QModelIndex &sourceIndex)
Returns the QModelIndex of an item in the proxy model that corresponds with the QModelIndex of an ite...
ProjectItemModel * sourceModel()
Returns the source model.
ProjectItem * addItem(ProjectItem *sourceItem)
Adds an item and its children to the proxy model.
ProjectItem * addChild(ProjectItem *sourceItem, ProjectItem *parentItem)
Creates an item in the proxy model corresponding to an item in the source model as a child of a paren...
QModelIndex mapIndexToSource(const QModelIndex &proxyIndex)
Returns the QModelIndex of an item in the souce model that corresponds with the QModelIndex of an ite...
void removeItem(ProjectItem *item)
Removes an item and its children from the proxy model.
void updateSourceCurrent()
Slot that updates the current item in the proxy model only if it is different than the corresponding ...
void onItemChanged(QStandardItem *item)
Signal to connect to the itemChanged() signal from a ProjectItemModel.
QItemSelection mapSelectionToSource(const QItemSelection &proxySelection)
Returns a QItemSelection of items in the source model that corresponds with a QItemSelection of itesm...
void updateProxyCurrent()
Slot that updates the current item in the proxy model only if it is different than the corresponding ...
virtual bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
Adds the data (selected items) from the source model to the proxy model.
void addItems(QList< ProjectItem * > sourceItems)
Adds a list of items to the proxy model.
QItemSelection mapSelectionFromSource(const QItemSelection &sourceSelection)
Returns a QItemSelection of items in the proxy model that corresponds with a QItemSelection of items ...
void updateSourceSelection()
Slot that updates the selection in the source model only if it is different than the corresponding se...
virtual bool canDropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) const
Returns true.
ProjectItem * mapItemToSource(ProjectItem *proxyItem)
Returns the ProjectItem in the source model that corresponds with a ProjectItem in the source model.
void setSourceModel(ProjectItemModel *sourceModel)
Sets the source model.
ProjectItem * mapItemFromSource(ProjectItem *sourceItem)
Returns the ProjectItem in the proxy model that corresponds with a ProjectItem in the source model.
This is free and unencumbered software released into the public domain.
Definition Apollo.h:16