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