Isis 3 Programmer Reference
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  if (!sourceItem) {
183  return 0;
184  }
185 
186  ProjectItem *proxyItem;
187 
188  if (ProjectItem *parentItem = mapItemFromSource( sourceItem->parent() ) ) {
189  proxyItem = addChild(sourceItem, parentItem);
190  }
191  else {
192  proxyItem = addChild(sourceItem, 0);
193  }
194 
195  for (int i=0; i < sourceItem->rowCount(); i++) {
196  addItem( sourceItem->child(i) );
197  }
198 
199  return proxyItem;
200  }
201 
202 
210  foreach (ProjectItem *item, sourceItems) {
211  addItem(item);
212  }
213  emit itemsAdded();
214  }
215 
216 
223  if (item) {
224  m_sourceProxyMap.remove( mapItemToSource(item) );
225  }
226 // qDebug()<<"ProjectItemProxyModel::removeItem item= "<<item;
228  }
229 
230 
239  if (m_sourceModel == sourceModel) {
240  return;
241  }
242 
243  if (m_sourceModel) {
244  clear();
245  m_sourceModel->disconnect(this);
246  }
247 
249 
250  // If current item changes on the Source Model, update this proxy model's current item
251  connect(sourceModel->selectionModel(),
252  SIGNAL( currentChanged(const QModelIndex &, const QModelIndex &) ),
253  this, SLOT( updateProxyCurrent() ) );
254  // If current item changes on this proxy model, update the source model's current item
255  connect(selectionModel(),
256  SIGNAL( currentChanged(const QModelIndex &, const QModelIndex &) ),
257  this, SLOT( updateSourceCurrent() ) );
258  // If selection changes on the Source Model, update this proxy model's selection
259  connect(sourceModel->selectionModel(),
260  SIGNAL( selectionChanged(const QItemSelection &, const QItemSelection &) ),
261  this, SLOT( updateProxySelection() ) );
262  // If the selection changes on this proxy model, update the source model's selection
263  connect(selectionModel(),
264  SIGNAL( selectionChanged(const QItemSelection &, const QItemSelection &) ),
265  this, SLOT( updateSourceSelection() ) );
266 
267  connect(sourceModel, SIGNAL( itemChanged(QStandardItem *) ),
268  this, SLOT( onItemChanged(QStandardItem *) ) );
269 
270  connect(sourceModel, SIGNAL(itemRemoved(ProjectItem *)),
271  this, SIGNAL(itemRemoved(ProjectItem *)));
272  }
273 
274 
281  return m_sourceModel;
282  }
283 
284 
293  if ( ProjectItem *proxyItem = mapItemFromSource(sourceItem) ) {
294  proxyItem->setProjectItem(sourceItem);
295  }
296  }
297 
298 
304  QModelIndex newProxyCurrent = mapIndexFromSource(
305  sourceModel()->selectionModel()->currentIndex() );
306  if ( newProxyCurrent != selectionModel()->currentIndex() ) {
307  selectionModel()->setCurrentIndex(newProxyCurrent, QItemSelectionModel::Current);
308  }
309  }
310 
311 
317  QModelIndex newSourceCurrent = mapIndexToSource( selectionModel()->currentIndex() );
318  if ( newSourceCurrent != sourceModel()->selectionModel()->currentIndex() ) {
319  sourceModel()->selectionModel()->setCurrentIndex(newSourceCurrent,
320  QItemSelectionModel::Current);
321  }
322  }
323 
324 
330  QItemSelection newProxySelection = mapSelectionFromSource(
331  sourceModel()->selectionModel()->selection() );
332  if ( newProxySelection != selectionModel()->selection() ) {
333  selectionModel()->select(newProxySelection, QItemSelectionModel::ClearAndSelect);
334  }
335  }
336 
337 
343  QItemSelection newSourceSelection = mapSelectionToSource( selectionModel()->selection() );
344  if ( mapSelectionFromSource(newSourceSelection) !=
345  mapSelectionFromSource( sourceModel()->selectionModel()->selection() ) ) {
346  sourceModel()->selectionModel()->select(newSourceSelection,
347  QItemSelectionModel::ClearAndSelect);
348  }
349  }
350 
351 
366  if (!sourceItem) {
367  return 0;
368  }
369 
370  if ( !sourceModel() ) {
371  setSourceModel( sourceItem->model() );
372  }
373 
374  if ( sourceItem->model() != sourceModel() ) {
375  return 0;
376  }
377 
378  if ( parentItem && (parentItem->model() != this) ) {
379  return 0;
380  }
381 
382  ProjectItem *proxyItem = mapItemFromSource(sourceItem);
383 
384  if (!proxyItem) {
385  proxyItem = new ProjectItem();
386  proxyItem->setProjectItem(sourceItem);
387  m_sourceProxyMap.insert(sourceItem, proxyItem);
388  }
389  else {
390  if ( ProjectItem *oldParent = proxyItem->parent() ) {
391  oldParent->takeRow( proxyItem->row() );
392  }
393  else if ( ProjectItemModel *model = proxyItem->model() ) {
394  model->takeRow( proxyItem->row() );
395  }
396  }
397 
398  if (parentItem) {
399  parentItem->appendRow(proxyItem);
400  }
401  else {
402  appendRow(proxyItem);
403  }
404 
405  return proxyItem;
406  }
407 
408 
417  updateItem( static_cast<ProjectItem *>(item) );
418  }
419 
420 
432  bool ProjectItemProxyModel::canDropMimeData(const QMimeData *data,
433  Qt::DropAction action,
434  int row, int column,
435  const QModelIndex &parent) const {
436  return true;
437  }
438 
439 
451  bool ProjectItemProxyModel::dropMimeData(const QMimeData *data,
452  Qt::DropAction action,
453  int row, int column,
454  const QModelIndex &parent) {
455  if ( data->hasFormat("application/x-qabstractitemmodeldatalist") ) {
457  return true;
458  }
459  return false;
460  }
461 }
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$
void addItems(QList< ProjectItem *> sourceItems)
Adds a list of items to the proxy model.
$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...
virtual bool canDropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) const
Returns true.
ProjectItemModel * model() const
Returns the ProjectItemModel associated with this item.
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.
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.
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 model.
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.
Unless noted otherwise, the portions of Isis written by the USGS are public domain.
ProjectItem * child(int row) const
Returns the child item at a given row.
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:146
Namespace for ISIS/Bullet specific routines.
Definition: Apollo.h:31
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...
void setSourceModel(ProjectItemModel *sourceModel)
Sets the source model.