AppFwk: Clean-up of table view after refactoring

This commit is contained in:
Jacob Støren 2018-06-27 17:19:00 +02:00
parent 88297bdb9f
commit ba9f7720e6
4 changed files with 47 additions and 180 deletions

View File

@ -42,6 +42,8 @@
#include "cafPdmUiTableViewEditor.h" #include "cafPdmUiTableViewEditor.h"
#include <QVBoxLayout> #include <QVBoxLayout>
#include "cafPdmUiCommandSystemProxy.h"
#include <QTableView>
namespace caf namespace caf
@ -62,13 +64,15 @@ PdmUiTableView::PdmUiTableView(QWidget* parent, Qt::WindowFlags f)
m_listViewEditor = new PdmUiTableViewEditor(); m_listViewEditor = new PdmUiTableViewEditor();
m_listViewEditor->createWidgets(this);
{ {
QWidget* widget = m_listViewEditor->createLabelWidget(this); QWidget* widget = m_listViewEditor->labelWidget();
layout->addWidget(widget); layout->addWidget(widget);
} }
{ {
QWidget* widget = m_listViewEditor->createEditorWidget(this); QWidget* widget = m_listViewEditor->editorWidget();
layout->addWidget(widget); layout->addWidget(widget);
} }
} }
@ -90,7 +94,18 @@ void PdmUiTableView::setChildArrayField(PdmChildArrayFieldHandle* childArrayFiel
if (childArrayField) if (childArrayField)
{ {
// Keep the possible custom context menu setting from the user of the table view.
// setUIField will set it based on the PdmUIItem settings, but turning the custom menu off should not
// be respected when using the field in a separate view.
auto orgContextPolicy = m_listViewEditor->tableView()->contextMenuPolicy();
m_listViewEditor->setUiField(childArrayField->uiCapability()); m_listViewEditor->setUiField(childArrayField->uiCapability());
auto newContextPolicy = m_listViewEditor->tableView()->contextMenuPolicy();
if (newContextPolicy == Qt::DefaultContextMenu)
{
m_listViewEditor->tableView()->setContextMenuPolicy(orgContextPolicy);
}
} }
else else
{ {
@ -142,22 +157,6 @@ void PdmUiTableView::setSelectionRole(SelectionManager::SelectionRole role)
m_listViewEditor->setSelectionRole(role); m_listViewEditor->setSelectionRole(role);
} }
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void PdmUiTableView::handleModelNotification(caf::PdmObjectHandle* itemThatChanged)
{
// Nothing to do for now
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void PdmUiTableView::handleModelSelectionChange()
{
m_listViewEditor->handleModelSelectionChange();
}
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
/// ///
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
@ -166,6 +165,18 @@ PdmObjectHandle* PdmUiTableView::pdmObjectFromModelIndex(const QModelIndex& mi)
return m_listViewEditor->pdmObjectFromModelIndex(mi); return m_listViewEditor->pdmObjectFromModelIndex(mi);
} }
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void PdmUiTableView::addActionsToMenu(QMenu* menu, PdmChildArrayFieldHandle* childArrayField)
{
// This is function is required to execute before populating the menu
// Several commands rely on the activeChildArrayFieldHandle in the selection manager
SelectionManager::instance()->setActiveChildArrayFieldHandle(childArrayField);
caf::PdmUiCommandSystemProxy::instance()->populateMenuWithDefaultCommands("PdmUiTreeViewEditor", menu);
}
} //End of namespace caf } //End of namespace caf

View File

@ -46,6 +46,7 @@
#include <QWidget> #include <QWidget>
class QTableView; class QTableView;
class QMenu;
namespace caf namespace caf
{ {
@ -58,27 +59,23 @@ class PdmChildArrayFieldHandle;
//================================================================================================== //==================================================================================================
/// ///
//================================================================================================== //==================================================================================================
class PdmUiTableView : public QWidget, public DataModelObserver class PdmUiTableView : public QWidget
{ {
Q_OBJECT Q_OBJECT
public: public:
PdmUiTableView(QWidget* parent = nullptr, Qt::WindowFlags f = nullptr); PdmUiTableView(QWidget* parent = nullptr, Qt::WindowFlags f = nullptr);
~PdmUiTableView(); ~PdmUiTableView();
PdmObjectHandle* pdmObjectFromModelIndex(const QModelIndex& mi);
// SIG_CAF_HACK
void setUiConfigurationName(QString uiConfigName);
void setChildArrayField(PdmChildArrayFieldHandle* childArrayField); void setChildArrayField(PdmChildArrayFieldHandle* childArrayField);
void setUiConfigurationName(QString uiConfigName);
void enableHeaderText(bool enable); void enableHeaderText(bool enable);
void setSelectionRole(SelectionManager::SelectionRole role); void setSelectionRole(SelectionManager::SelectionRole role);
PdmObjectHandle* pdmObjectFromModelIndex(const QModelIndex& mi);
QTableView* tableView(); QTableView* tableView();
void handleModelNotification(caf::PdmObjectHandle* itemThatChanged) override; static void addActionsToMenu(QMenu* menu, PdmChildArrayFieldHandle* childArrayField);
void handleModelSelectionChange() override;
private: private:
PdmUiTableViewEditor* m_listViewEditor; PdmUiTableViewEditor* m_listViewEditor;

View File

@ -33,15 +33,12 @@
// for more details. // for more details.
// //
//################################################################################################## //##################################################################################################
#include "cafPdmUiTableViewEditor.h" #include "cafPdmUiTableViewEditor.h"
#include "cafPdmChildArrayField.h" #include "cafPdmChildArrayField.h"
#include "cafPdmField.h" #include "cafPdmField.h"
#include "cafPdmObject.h" #include "cafPdmObject.h"
#include "cafPdmUiCheckBoxDelegate.h" #include "cafPdmUiCheckBoxDelegate.h"
#include "cafPdmUiCommandSystemProxy.h"
#include "cafPdmUiEditorHandle.h" #include "cafPdmUiEditorHandle.h"
#include "cafPdmUiTableViewDelegate.h" #include "cafPdmUiTableViewDelegate.h"
#include "cafPdmUiTableViewQModel.h" #include "cafPdmUiTableViewQModel.h"
@ -54,41 +51,11 @@
#include <QTableView> #include <QTableView>
#include <QWidget> #include <QWidget>
namespace caf namespace caf
{ {
class FocusEventHandler : public QObject
{
public:
explicit FocusEventHandler(PdmUiTableViewEditor* tableViewEditor)
: QObject(tableViewEditor)
{
m_tableViewEditor = tableViewEditor;
}
protected:
bool eventFilter(QObject *obj, QEvent *event) override
{
if (event->type() == QEvent::FocusIn ||
event->type() == QEvent::FocusOut)
{
m_tableViewEditor->tableViewWidgetFocusChanged(event);
}
// standard event processing
return QObject::eventFilter(obj, event);
}
private:
PdmUiTableViewEditor* m_tableViewEditor;
};
CAF_PDM_UI_FIELD_EDITOR_SOURCE_INIT(PdmUiTableViewEditor); CAF_PDM_UI_FIELD_EDITOR_SOURCE_INIT(PdmUiTableViewEditor);
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
/// ///
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
@ -108,7 +75,6 @@ PdmUiTableViewEditor::PdmUiTableViewEditor()
m_isBlockingSelectionManagerChanged = false; m_isBlockingSelectionManagerChanged = false;
} }
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
/// ///
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
@ -130,10 +96,6 @@ QWidget* PdmUiTableViewEditor::createEditorWidget(QWidget* parent)
m_tableView->setModel(m_tableModelPdm); m_tableView->setModel(m_tableModelPdm);
connect(m_tableView->selectionModel(), SIGNAL(selectionChanged( const QItemSelection & , const QItemSelection & )), SLOT(slotSelectionChanged( const QItemSelection & , const QItemSelection & ))); connect(m_tableView->selectionModel(), SIGNAL(selectionChanged( const QItemSelection & , const QItemSelection & )), SLOT(slotSelectionChanged( const QItemSelection & , const QItemSelection & )));
connect(m_tableView->selectionModel(), SIGNAL(currentChanged( const QModelIndex & , const QModelIndex & )), SLOT(slotCurrentChanged( const QModelIndex& , const QModelIndex& )));
FocusEventHandler* tableViewWidgetFocusEventHandler = new FocusEventHandler(this);
m_tableView->installEventFilter(tableViewWidgetFocusEventHandler);
return m_tableView; return m_tableView;
} }
@ -214,16 +176,6 @@ void PdmUiTableViewEditor::configureAndUpdateUi(const QString& uiConfigName)
} }
} }
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void PdmUiTableViewEditor::onSelectionManagerSelectionChanged()
{
if (m_isBlockingSelectionManagerChanged) return;
handleModelSelectionChange();
}
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
/// ///
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
@ -269,29 +221,15 @@ void PdmUiTableViewEditor::setSelectionRole(SelectionManager::SelectionRole role
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
/// ///
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
void PdmUiTableViewEditor::slotCurrentChanged(const QModelIndex & current, const QModelIndex & previous) void PdmUiTableViewEditor::onSelectionManagerSelectionChanged()
{ {
//if (isSelectionRoleDefined()) if (m_isBlockingSelectionManagerChanged) return;
//{
// std::vector<PdmUiItem*> items;
// QModelIndexList list;
// list.append(current);
// selectedUiItems(list, items);
//
// SelectionManager::instance()->setSelectedItems(items, m_selectionRole);
//}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void PdmUiTableViewEditor::handleModelSelectionChange()
{
if (isSelectionRoleDefined()) if (isSelectionRoleDefined())
{ {
std::vector<PdmUiItem*> items; std::vector<PdmUiItem*> items;
SelectionManager::instance()->selectedItems(items, m_selectionRole); SelectionManager::instance()->selectedItems(items, m_selectionRole);
#if 1
QItemSelection totalSelection; QItemSelection totalSelection;
for (auto item: items) for (auto item: items)
{ {
@ -299,33 +237,11 @@ void PdmUiTableViewEditor::handleModelSelectionChange()
QItemSelection itemSelection = m_tableModelPdm->modelIndexFromPdmObject(pdmObj); QItemSelection itemSelection = m_tableModelPdm->modelIndexFromPdmObject(pdmObj);
totalSelection.merge(itemSelection, QItemSelectionModel::Select); totalSelection.merge(itemSelection, QItemSelectionModel::Select);
} }
m_tableView->selectionModel()->select(totalSelection, QItemSelectionModel::SelectCurrent); m_tableView->selectionModel()->select(totalSelection, QItemSelectionModel::SelectCurrent);
#else
// TODO: Handle multiple selection
if (items.size() == 1)
{
PdmObject* pdmObj = dynamic_cast<PdmObject*>(items[0]);
QItemSelection itemSelection = m_tableModelPdm->modelIndexFromPdmObject(pdmObj);
if (!itemSelection.empty())
{
m_tableView->selectionModel()->select(itemSelection, QItemSelectionModel::SelectCurrent);
}
}
#endif
} }
} }
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void PdmUiTableViewEditor::addActionsToMenu(QMenu* menu, PdmChildArrayFieldHandle* childArrayField)
{
// This is function is required to execute before populating the menu
// Several commands rely on the activeChildArrayFieldHandle in the selection manager
SelectionManager::instance()->setActiveChildArrayFieldHandle(childArrayField);
caf::PdmUiCommandSystemProxy::instance()->populateMenuWithDefaultCommands("PdmUiTreeViewEditor", menu);
}
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
/// NOTE: If no selection role is defined, the selection manager is not changed, the selection in the /// NOTE: If no selection role is defined, the selection manager is not changed, the selection in the
@ -357,33 +273,6 @@ PdmObjectHandle* PdmUiTableViewEditor::pdmObjectFromModelIndex(const QModelIndex
return nullptr; return nullptr;
} }
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void PdmUiTableViewEditor::tableViewWidgetFocusChanged(QEvent* focusEvent)
{
if (m_delegate->isEditorOpen())
{
// The table view emits focus out when a table cell editor is active
// Do not update the selection when this state occurs
return;
}
if (isSelectionRoleDefined())
{
if (focusEvent->type() == QEvent::FocusIn)
{
//updateSelectionManagerFromTableSelection();
}
else if (focusEvent->type() == QEvent::FocusOut)
{
// Clearing the selection here causes the Menu to not display all items
// Not sure how this can be handled correctly
// SelectionManager::instance()->clear(m_selectionRole);
}
}
}
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
/// ///
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
@ -391,27 +280,6 @@ void PdmUiTableViewEditor::updateSelectionManagerFromTableSelection()
{ {
if (isSelectionRoleDefined()) if (isSelectionRoleDefined())
{ {
#if 0
std::vector<PdmUiItem*> items;
QModelIndexList modelIndexList = m_tableView->selectionModel()->selectedIndexes();
for (const QModelIndex& mi : modelIndexList)
{
PdmFieldHandle* pdmFieldHandle = m_tableModelPdm->getField(mi);
if (pdmFieldHandle && pdmFieldHandle->uiCapability())
{
items.push_back(pdmFieldHandle->uiCapability());
}
}
if (items.size() > 1)
{
// Selection of a single row is handled by slotCurrentChanged()
// Multiple selection of fields is handled here
SelectionManager::instance()->setSelectedItems(items, m_selectionRole);
}
#else
std::set<PdmUiItem*> selectedRowObjects; std::set<PdmUiItem*> selectedRowObjects;
QModelIndexList modelIndexList = m_tableView->selectionModel()->selectedIndexes(); QModelIndexList modelIndexList = m_tableView->selectionModel()->selectedIndexes();
for (const QModelIndex& mi : modelIndexList) for (const QModelIndex& mi : modelIndexList)
@ -425,12 +293,10 @@ void PdmUiTableViewEditor::updateSelectionManagerFromTableSelection()
} }
std::vector<PdmUiItem*> items { selectedRowObjects.begin(), selectedRowObjects.end() }; std::vector<PdmUiItem*> items { selectedRowObjects.begin(), selectedRowObjects.end() };
m_isBlockingSelectionManagerChanged = true; m_isBlockingSelectionManagerChanged = true;
SelectionManager::instance()->setSelectedItems(items, m_selectionRole); SelectionManager::instance()->setSelectedItems(items, m_selectionRole);
m_isBlockingSelectionManagerChanged = false; m_isBlockingSelectionManagerChanged = false;
#endif
} }
} }

View File

@ -48,7 +48,6 @@
class QItemSelection; class QItemSelection;
class QLabel; class QLabel;
class QMenu;
class QTableView; class QTableView;
namespace caf namespace caf
@ -109,30 +108,24 @@ public:
void setSelectionRole(SelectionManager::SelectionRole role); void setSelectionRole(SelectionManager::SelectionRole role);
PdmObjectHandle* pdmObjectFromModelIndex(const QModelIndex& mi); PdmObjectHandle* pdmObjectFromModelIndex(const QModelIndex& mi);
QTableView* tableView();
QWidget* createEditorWidget(QWidget * parent) override; QWidget* createEditorWidget(QWidget * parent) override;
QWidget* createLabelWidget(QWidget * parent) override; QWidget* createLabelWidget(QWidget * parent) override;
QTableView* tableView();
void handleModelSelectionChange();
static void addActionsToMenu(QMenu* menu, PdmChildArrayFieldHandle* childArrayField);
protected: protected:
virtual void configureAndUpdateUi(const QString& uiConfigName) override; virtual void configureAndUpdateUi(const QString& uiConfigName) override;
virtual void onSelectionManagerSelectionChanged() override; virtual void onSelectionManagerSelectionChanged() override;
private: private:
void selectedUiItems(const QModelIndexList& modelIndexList, std::vector<PdmUiItem*>& objects); void selectedUiItems(const QModelIndexList& modelIndexList, std::vector<PdmUiItem*>& objects);
bool isSelectionRoleDefined() const; bool isSelectionRoleDefined() const;
void tableViewWidgetFocusChanged(QEvent* focusEvent);
void updateSelectionManagerFromTableSelection(); void updateSelectionManagerFromTableSelection();
PdmChildArrayFieldHandle* childArrayFieldHandle(); PdmChildArrayFieldHandle* childArrayFieldHandle();
private slots: private slots:
void slotCurrentChanged(const QModelIndex & current, const QModelIndex & previous);
void slotSelectionChanged(const QItemSelection & selected, const QItemSelection & deselected); void slotSelectionChanged(const QItemSelection & selected, const QItemSelection & deselected);
private: private: