Isis 3.0 Programmer Reference
Back | Home
ProjectItemProxyModel.cpp
Go to the documentation of this file.
1 
23 #include "IsisDebug.h"
24 
25 #include "ProjectItemProxyModel.h"
26 
27 #include <QList>
28 #include <QItemSelection>
29 #include <QMap>
30 #include <QMimeData>
31 #include <QModelIndex>
32 #include <QObject>
33 #include <QtDebug>
34 
35 #include "ProjectItem.h"
36 #include "ProjectItemModel.h"
37 
38 namespace Isis {
45  m_sourceModel = 0;
46  m_sourceProxyMap = QMap<ProjectItem *, ProjectItem *>();
47  }
48 
49 
59  QModelIndex ProjectItemProxyModel::mapIndexFromSource(const QModelIndex &sourceIndex) {
60  ProjectItem *proxyItem = mapItemFromSource( sourceModel()->itemFromIndex(sourceIndex) );
61 
62  if (proxyItem) {
63  return proxyItem->index();
64  }
65  else {
66  return QModelIndex();
67  }
68 
69  }
70 
71 
80  QModelIndex ProjectItemProxyModel::mapIndexToSource(const QModelIndex &proxyIndex) {
81  ProjectItem *sourceItem = mapItemToSource( itemFromIndex(proxyIndex) );
82 
83  if (sourceItem) {
84  return sourceItem->index();
85  }
86  else {
87  return QModelIndex();
88  }
89  }
90 
91 
102  const QItemSelection &sourceSelection) {
103 
104  QItemSelection proxySelection = QItemSelection();
105 
106  foreach ( QModelIndex sourceIndex, sourceSelection.indexes() ) {
107  QModelIndex proxyIndex = mapIndexFromSource(sourceIndex);
108  if ( proxyIndex.isValid() ) {
109  proxySelection.select(proxyIndex, proxyIndex);
110  }
111  }
112 
113  return proxySelection;
114  }
115 
116 
126  QItemSelection ProjectItemProxyModel::mapSelectionToSource(const QItemSelection &proxySelection) {
127  QItemSelection sourceSelection = QItemSelection();
128 
129  foreach ( QModelIndex proxyIndex, proxySelection.indexes() ) {
130  QModelIndex sourceIndex = mapIndexToSource(proxyIndex);
131  if ( sourceIndex.isValid() ) {
132  sourceSelection.select(sourceIndex, sourceIndex);
133  }
134  }
135 
136  return sourceSelection;
137  }
138 
139 
149  return m_sourceProxyMap.value(sourceItem, 0);
150  }
151 
152 
162  return m_sourceProxyMap.key(proxyItem, 0);
163  }
164 
165 
182 // qDebug()<<"ProjectItemProxyModel::addItem";
183  if (!sourceItem) {
184  return 0;
185  }
186 
187  ProjectItem *proxyItem;
188 
189  if (ProjectItem *parentItem = mapItemFromSource( sourceItem->parent() ) ) {
190  proxyItem = addChild(sourceItem, parentItem);
191  }
192  else {
193  proxyItem = addChild(sourceItem, 0);
194  }
195 
196  for (int i=0; i < sourceItem->rowCount(); i++) {
197  addItem( sourceItem->child(i) );
198  }
199 
200  return proxyItem;
201  }
202 
203 
211 // qDebug()<<"ProjectItemProxyModel::addItem";
212  foreach (ProjectItem *item, sourceItems) {
213  addItem(item);
214  }
215  }
216 
217 
224  if (item) {
225  m_sourceProxyMap.remove( mapItemToSource(item) );
226  }
227 // qDebug()<<"ProjectItemProxyModel::removeItem item= "<<item;
229  }
230 
231 
240  if (m_sourceModel == sourceModel) {
241  return;
242  }
243 
244  if (m_sourceModel) {
245  clear();
246  m_sourceModel->disconnect(this);
247  }
248 
250 
251  // If current item changes on the Source Model, update this proxy model's current item
252  connect(sourceModel->selectionModel(),
253  SIGNAL( currentChanged(const QModelIndex &, const QModelIndex &) ),
254  this, SLOT( updateProxyCurrent() ) );
255  // If current item changes on this proxy model, update the source model's current item
256  connect(selectionModel(),
257  SIGNAL( currentChanged(const QModelIndex &, const QModelIndex &) ),
258  this, SLOT( updateSourceCurrent() ) );
259  // If selection changes on the Source Model, update this proxy model's selection
260  connect(sourceModel->selectionModel(),
261  SIGNAL( selectionChanged(const QItemSelection &, const QItemSelection &) ),
262  this, SLOT( updateProxySelection() ) );
263  // If the selection changes on this proxy model, update the source model's selection
264  connect(selectionModel(),
265  SIGNAL( selectionChanged(const QItemSelection &, const QItemSelection &) ),
266  this, SLOT( updateSourceSelection() ) );
267 
268  connect(sourceModel, SIGNAL( itemChanged(QStandardItem *) ),
269  this, SLOT( onItemChanged(QStandardItem *) ) );
270 
271  connect(sourceModel, SIGNAL(itemRemoved(ProjectItem *)),
272  this, SIGNAL(itemRemoved(ProjectItem *)));
273  }
274 
275 
282  return m_sourceModel;
283  }
284 
285 
294  if ( ProjectItem *proxyItem = mapItemFromSource(sourceItem) ) {
295  proxyItem->setProjectItem(sourceItem);
296  }
297  }
298 
299 
305  QModelIndex newProxyCurrent = mapIndexFromSource(
306  sourceModel()->selectionModel()->currentIndex() );
307  if ( newProxyCurrent != selectionModel()->currentIndex() ) {
308  selectionModel()->setCurrentIndex(newProxyCurrent, QItemSelectionModel::Current);
309  }
310  }
311 
312 
318  QModelIndex newSourceCurrent = mapIndexToSource( selectionModel()->currentIndex() );
319  if ( newSourceCurrent != sourceModel()->selectionModel()->currentIndex() ) {
320  sourceModel()->selectionModel()->setCurrentIndex(newSourceCurrent,
321  QItemSelectionModel::Current);
322  }
323  }
324 
325 
331  QItemSelection newProxySelection = mapSelectionFromSource(
332  sourceModel()->selectionModel()->selection() );
333  if ( newProxySelection != selectionModel()->selection() ) {
334  selectionModel()->select(newProxySelection, QItemSelectionModel::ClearAndSelect);
335  }
336  }
337 
338 
344  QItemSelection newSourceSelection = mapSelectionToSource( selectionModel()->selection() );
345  if ( mapSelectionFromSource(newSourceSelection) !=
346  mapSelectionFromSource( sourceModel()->selectionModel()->selection() ) ) {
347  sourceModel()->selectionModel()->select(newSourceSelection,
348  QItemSelectionModel::ClearAndSelect);
349  }
350  }
351 
352 
367  if (!sourceItem) {
368  return 0;
369  }
370 
371  if ( !sourceModel() ) {
372  setSourceModel( sourceItem->model() );
373  }
374 
375  if ( sourceItem->model() != sourceModel() ) {
376  return 0;
377  }
378 
379  if ( parentItem && (parentItem->model() != this) ) {
380  return 0;
381  }
382 
383  ProjectItem *proxyItem = mapItemFromSource(sourceItem);
384 
385  if (!proxyItem) {
386  proxyItem = new ProjectItem();
387  proxyItem->setProjectItem(sourceItem);
388  m_sourceProxyMap.insert(sourceItem, proxyItem);
389  }
390  else {
391  if ( ProjectItem *oldParent = proxyItem->parent() ) {
392  oldParent->takeRow( proxyItem->row() );
393  }
394  else if ( ProjectItemModel *model = proxyItem->model() ) {
395  model->takeRow( proxyItem->row() );
396  }
397  }
398 
399  if (parentItem) {
400  parentItem->appendRow(proxyItem);
401  }
402  else {
403  appendRow(proxyItem);
404  }
405 
406  return proxyItem;
407  }
408 
409 
418  updateItem( static_cast<ProjectItem *>(item) );
419  }
420 
421 
433  bool ProjectItemProxyModel::canDropMimeData(const QMimeData *data,
434  Qt::DropAction action,
435  int row, int column,
436  const QModelIndex &parent) const {
437  return true;
438  }
439 
440 
452  bool ProjectItemProxyModel::dropMimeData(const QMimeData *data,
453  Qt::DropAction action,
454  int row, int column,
455  const QModelIndex &parent) {
456  if ( data->hasFormat("application/x-qabstractitemmodeldatalist") ) {
458  return true;
459  }
460  return false;
461  }
462 }
QModelIndex mapIndexFromSource(const QModelIndex &sourceIndex)
Returns the QModelIndex of an item in the proxy model that corresponds with the QModelIndex of an ite...
$Revision$ $Date$
$Date$ $Revision$
void updateProxySelection()
Slot that updates the selection in the proxy model only if it is different than the corresponding sel...
void appendRow(ProjectItem *item)
Appends an item to the children of this item.
void updateSourceSelection()
Slot that updates the selection in the source model only if it is different than the corresponding se...
ProjectItem * itemFromIndex(const QModelIndex &index)
Returns the ProjectItem corresponding to a given QModelIndex.
void updateItem(ProjectItem *sourceItem)
Given an item in the source model, this method changes the data of the corresponding item in the prox...
ProjectItem * parent() const
Returns the parent item of this item.
ProjectItemProxyModel(QObject *parent=0)
Constructs the proxy model.
Provides access to data stored in a Project through Qt&#39;s model-view framework.
ProjectItem * addItem(ProjectItem *sourceItem)
Adds an item and its children to the proxy model.
void setProjectItem(ProjectItem *item)
Sets the text, icon, and data to those of another item.
void updateProxyCurrent()
Slot that updates the current item in the proxy model only if it is different than the corresponding ...
QModelIndex mapIndexToSource(const QModelIndex &proxyIndex)
Returns the QModelIndex of an item in the souce model that corresponds with the QModelIndex of an ite...
void appendRow(ProjectItem *item)
Appends a top-level item to the model.
ProjectItemModel * model() const
Returns the ProjectItemModel associated with this item.
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...
QItemSelectionModel * selectionModel()
Returns the internal selection model.
void updateSourceCurrent()
Slot that updates the current item in the proxy model only if it is different than the corresponding ...
virtual void removeItem(ProjectItem *item)
Removes an item and its children from the model.
ProjectItem * item(int row)
Returns the top-level item at the given row.
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.
ProjectItemModel * sourceModel()
Returns the source model.
virtual bool canDropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) const
Returns true.
void addItems(QList< ProjectItem * > sourceItems)
Adds a list of items to the proxy model.
ProjectItem * mapItemToSource(ProjectItem *proxyItem)
Returns the ProjectItem in the source model that corresponds with a ProjectItem in the source model...
ProjectItemModel * m_sourceModel
The source model. Map of items from the source model to the proxy mo...
QItemSelection mapSelectionFromSource(const QItemSelection &sourceSelection)
Returns a QItemSelection of items in the proxy model that corresponds with a QItemSelection of items ...
void onItemChanged(QStandardItem *item)
Signal to connect to the itemChanged() signal from a ProjectItemModel.
$Date$ $Revision$
QList< ProjectItem * > selectedItems()
Returns a list of the selected items of the internal selection model.
Represents an item of a ProjectItemModel in Qt&#39;s model-view framework.
Definition: ProjectItem.h:113
void removeItem(ProjectItem *item)
Removes an item and its children from the proxy model.
ProjectItem * mapItemFromSource(ProjectItem *sourceItem)
Returns the ProjectItem in the proxy model that corresponds with a ProjectItem in the source model...
QItemSelection mapSelectionToSource(const QItemSelection &proxySelection)
Returns a QItemSelection of items in the source model that corresponds with a QItemSelection of itesm...
ProjectItem * child(int row) const
Returns the child item at a given row.
void setSourceModel(ProjectItemModel *sourceModel)
Sets the source model.

U.S. Department of the Interior | U.S. Geological Survey
ISIS | Privacy & Disclaimers | Astrogeology Research Program
To contact us, please post comments and questions on the ISIS Support Center
File Modified: 07/12/2023 23:26:48