(#369) Added selectAsCurrentItem to the PdmUiTreeView

Cleaned up the responsibilities between Editor, model and View
Now model exposes translation between QModelIndex and PdmUiItem
Selection inteface implementation is handled by editor, and forwarded to View
Also started renaming of SlectionManager update enabeling
This commit is contained in:
Jacob Støren 2015-08-13 23:45:34 +02:00
parent 235e024533
commit 8052a1c672
6 changed files with 80 additions and 70 deletions

View File

@ -65,7 +65,7 @@ PdmUiTreeView::PdmUiTreeView(QWidget* parent, Qt::WindowFlags f)
this->m_layout->insertWidget(0, widget);
connect(treeView()->selectionModel(), SIGNAL(selectionChanged( const QItemSelection & , const QItemSelection & )), SLOT(slotOnSelectionChanged( const QItemSelection & , const QItemSelection & )));
connect(m_treeViewEditor, SIGNAL(selectionChanged()), SLOT(slotOnSelectionChanged()));
}
//--------------------------------------------------------------------------------------------------
@ -118,7 +118,7 @@ void PdmUiTreeView::selectedObjects(std::vector<PdmUiItem*>& objects)
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void PdmUiTreeView::slotOnSelectionChanged(const QItemSelection & selected, const QItemSelection & deselected)
void PdmUiTreeView::slotOnSelectionChanged()
{
emit selectionChanged();
}
@ -136,7 +136,15 @@ void PdmUiTreeView::enableDefaultContextMenu(bool enable)
//--------------------------------------------------------------------------------------------------
void PdmUiTreeView::setCurrentSelectionToCurrentEditorSelection(bool enable)
{
m_treeViewEditor->setCurrentSelectionToCurrentEditorSelection(enable);
m_treeViewEditor->enableSelectionManagerUpdating(enable);
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void PdmUiTreeView::selectAsCurrentItem(PdmUiItem* uiItem)
{
m_treeViewEditor->selectAsCurrentItem(uiItem);
}
} //End of namespace caf

View File

@ -64,19 +64,19 @@ public:
void enableDefaultContextMenu(bool enable);
void setCurrentSelectionToCurrentEditorSelection(bool enable);
void setUiConfigurationName(QString uiConfigName);
void setPdmItem(caf::PdmUiItem* object);
QTreeView* treeView();
void selectedObjects(std::vector<PdmUiItem*>& objects);
void selectAsCurrentItem(PdmUiItem* uiItem);
signals:
void selectionChanged();
private slots:
void slotOnSelectionChanged( const QItemSelection & selected, const QItemSelection & deselected );
void slotOnSelectionChanged();
private:
PdmUiTreeViewEditor* m_treeViewEditor;

View File

@ -61,7 +61,7 @@ namespace caf
PdmUiTreeViewEditor::PdmUiTreeViewEditor()
{
m_useDefaultContextMenu = false;
m_currentSelectionFollowsEditorSelection = false;
m_updateSelectionManager = false;
}
//--------------------------------------------------------------------------------------------------
@ -86,8 +86,8 @@ QWidget* PdmUiTreeViewEditor::createWidget(QWidget* parent)
m_treeView = new QTreeView(m_mainWidget);
m_treeView->setModel(m_treeViewModel);
connect(m_treeView->selectionModel(), SIGNAL(currentChanged( const QModelIndex & , const QModelIndex & )), SLOT(slotCurrentChanged( const QModelIndex& , const QModelIndex& )));
connect(treeView()->selectionModel(), SIGNAL(selectionChanged( const QItemSelection & , const QItemSelection & )), SLOT(slotOnSelectionChanged( const QItemSelection & , const QItemSelection & )));
m_layout->addWidget(m_treeView);
updateContextMenuSignals();
@ -121,35 +121,27 @@ QTreeView* PdmUiTreeViewEditor::treeView()
return m_treeView;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void PdmUiTreeViewEditor::selectedUiItems(std::vector<PdmUiItem*>& objects)
{
if (m_treeViewModel) { m_treeViewModel->selectedUiItems(objects); }
}
if (!this->treeView()) return;
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void PdmUiTreeViewEditor::uiItemsFromModelIndexList(const QModelIndexList& modelIndexList, std::vector<PdmUiItem*>& objects)
{
if (m_treeViewModel)
QModelIndexList idxList = this->treeView()->selectionModel()->selectedIndexes();
for (int i = 0; i < idxList.size(); i++)
{
for (int i = 0; i < modelIndexList.size(); i++)
caf::PdmUiItem* item = this->m_treeViewModel->uiItemFromModelIndex(idxList[i]);
if (item)
{
QModelIndex mi = modelIndexList.at(i);
PdmUiTreeOrdering* treeOrdering = m_treeViewModel->treeItemFromIndex(mi);
if (treeOrdering->activeItem())
{
objects.push_back(treeOrdering->activeItem());
}
objects.push_back(item);
}
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
@ -171,9 +163,9 @@ void PdmUiTreeViewEditor::enableDefaultContextMenu(bool enable)
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void PdmUiTreeViewEditor::setCurrentSelectionToCurrentEditorSelection(bool enable)
void PdmUiTreeViewEditor::enableSelectionManagerUpdating(bool enable)
{
m_currentSelectionFollowsEditorSelection = enable;
m_updateSelectionManager = enable;
}
//--------------------------------------------------------------------------------------------------
@ -200,6 +192,7 @@ void PdmUiTreeViewEditor::updateContextMenuSignals()
//--------------------------------------------------------------------------------------------------
void PdmUiTreeViewEditor::customMenuRequested(QPoint pos)
{
// This seems a bit strange. Why ?
SelectionManager::instance()->setActiveChildArrayFieldHandle(this->currentChildArrayFieldHandle());
QMenu menu;
@ -214,22 +207,6 @@ void PdmUiTreeViewEditor::customMenuRequested(QPoint pos)
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void PdmUiTreeViewEditor::slotCurrentChanged(const QModelIndex & current, const QModelIndex & previous)
{
if (m_currentSelectionFollowsEditorSelection)
{
QModelIndexList list;
list.append(current);
std::vector<PdmUiItem*> items;
uiItemsFromModelIndexList(list, items);
SelectionManager::instance()->setSelectedItems(items, SelectionManager::CURRENT);
}
}
//--------------------------------------------------------------------------------------------------
///
@ -263,5 +240,30 @@ PdmChildArrayFieldHandle* PdmUiTreeViewEditor::currentChildArrayFieldHandle()
return NULL;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void PdmUiTreeViewEditor::selectAsCurrentItem(PdmUiItem* uiItem)
{
QModelIndex index = m_treeViewModel->findModelIndex(uiItem);
m_treeView->setCurrentIndex(index);
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void PdmUiTreeViewEditor::slotOnSelectionChanged(const QItemSelection & selected, const QItemSelection & deselected)
{
if (m_updateSelectionManager)
{
std::vector<PdmUiItem*> items;
this->selectedUiItems(items);
SelectionManager::instance()->setSelectedItems(items, SelectionManager::CURRENT);
}
emit selectionChanged();
}
} // end namespace caf

View File

@ -43,6 +43,7 @@
#include <QAbstractItemModel>
#include <QPointer>
#include <QWidget>
#include <QItemSelectionModel>
class MySortFilterProxyModel;
@ -82,13 +83,17 @@ public:
~PdmUiTreeViewEditor();
void enableDefaultContextMenu(bool enable);
void setCurrentSelectionToCurrentEditorSelection(bool enable);
void enableSelectionManagerUpdating(bool enable);
QTreeView* treeView();
void selectAsCurrentItem(PdmUiItem* uiItem);
void selectedUiItems(std::vector<PdmUiItem*>& objects);
QWidget* createWidget(QWidget* parent);
signals:
void selectionChanged();
protected:
virtual void configureAndUpdateUi(const QString& uiConfigName);
@ -99,7 +104,7 @@ protected:
private slots:
void customMenuRequested(QPoint pos);
void slotCurrentChanged(const QModelIndex & current, const QModelIndex & previous);
void slotOnSelectionChanged(const QItemSelection & selected, const QItemSelection & deselected);
private:
void uiItemsFromModelIndexList(const QModelIndexList& modelIndexList, std::vector<PdmUiItem*>& objects);
@ -117,7 +122,7 @@ private:
PdmUiTreeViewEditorAttribute m_editorAttributes;
bool m_useDefaultContextMenu;
bool m_currentSelectionFollowsEditorSelection;
bool m_updateSelectionManager;
};

View File

@ -367,26 +367,6 @@ QModelIndex caf::PdmUiTreeViewModel::findModelIndexRecursive(const QModelIndex&
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void caf::PdmUiTreeViewModel::selectedUiItems(std::vector<PdmUiItem*>& objects)
{
if (!(m_treeViewEditor && m_treeViewEditor->treeView())) return;
QModelIndexList idxList = m_treeViewEditor->treeView()->selectionModel()->selectedIndexes();
for (int i = 0; i < idxList.size(); i++)
{
PdmUiTreeOrdering* treeItem = this->treeItemFromIndex(idxList[i]);
if (treeItem)
{
caf::PdmUiItem* item = treeItem->activeItem();
objects.push_back(item);
}
}
}
//--------------------------------------------------------------------------------------------------
/// An invalid parent index is implicitly meaning the root item, and not "above" root, since
@ -698,5 +678,19 @@ QVariant PdmUiTreeViewModel::headerData(int section, Qt::Orientation orientation
return QVariant();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
PdmUiItem* PdmUiTreeViewModel::uiItemFromModelIndex(const QModelIndex& index) const
{
PdmUiTreeOrdering* treeItem = this->treeItemFromIndex(index);
if (treeItem)
{
return treeItem->activeItem();
}
return NULL;
}
} // end namespace caf

View File

@ -68,14 +68,15 @@ public:
void setColumnHeaders(const QStringList& columnHeaders);
void setUiConfigName(const QString& uiConfigName) { m_uiConfigName = uiConfigName; }
void selectedUiItems(std::vector<PdmUiItem*>& objects);
PdmUiTreeOrdering* treeItemFromIndex(const QModelIndex& index) const;
// These are supposed to be used from the Editor only, and to implement selection support.
PdmUiItem* uiItemFromModelIndex(const QModelIndex& index) const;
QModelIndex findModelIndex(const PdmUiItem* object) const;
private:
void updateSubTreeRecursive(const QModelIndex& uiSubTreeRootModelIdx, PdmUiTreeOrdering* uiModelSubTreeRoot, PdmUiTreeOrdering* updatedPdmSubTreeRoot);
QModelIndex findModelIndex(const PdmUiItem* object) const;
PdmUiTreeOrdering* treeItemFromIndex(const QModelIndex& index) const;
QModelIndex findModelIndexRecursive(const QModelIndex& currentIndex, const PdmUiItem * object) const;
void resetTree(PdmUiTreeOrdering* root);