AppFwk: Introduce SelectionChangedReceiver as a slimmer replacement for NotificationCenter

This commit is contained in:
Jacob Støren 2018-06-27 13:15:20 +02:00
parent 66cec70718
commit 08c78471c3
5 changed files with 150 additions and 15 deletions

View File

@ -53,6 +53,8 @@ set( PROJECT_FILES
cafSelectionManager.cpp cafSelectionManager.cpp
cafSelectionManager.h cafSelectionManager.h
cafSelectionChangedReceiver.h
cafSelectionChangedReceiver.cpp
cafSelectionManagerTools.h cafSelectionManagerTools.h
) )

View File

@ -0,0 +1,59 @@
//##################################################################################################
//
// Custom Visualization Core library
// Copyright (C) Ceetron Solutions AS
//
// This library may be used under the terms of either the GNU General Public License or
// the GNU Lesser General Public License as follows:
//
// GNU General Public License Usage
// This library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU General Public License at <<http://www.gnu.org/licenses/gpl.html>>
// for more details.
//
// GNU Lesser General Public License Usage
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU Lesser General Public License at <<http://www.gnu.org/licenses/lgpl-2.1.html>>
// for more details.
//
//##################################################################################################
#include "cafSelectionChangedReceiver.h"
#include "cafSelectionManager.h"
namespace caf
{
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
caf::SelectionChangedReceiver::SelectionChangedReceiver()
{
SelectionManager::instance()->registerSelectionChangedReceiver(this);
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
caf::SelectionChangedReceiver::~SelectionChangedReceiver()
{
SelectionManager::instance()->unregisterSelectionChangedReceiver(this);
}
}

View File

@ -0,0 +1,52 @@
//##################################################################################################
//
// Custom Visualization Core library
// Copyright (C) Ceetron Solutions AS
//
// This library may be used under the terms of either the GNU General Public License or
// the GNU Lesser General Public License as follows:
//
// GNU General Public License Usage
// This library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU General Public License at <<http://www.gnu.org/licenses/gpl.html>>
// for more details.
//
// GNU Lesser General Public License Usage
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU Lesser General Public License at <<http://www.gnu.org/licenses/lgpl-2.1.html>>
// for more details.
//
//##################################################################################################
#pragma once
namespace caf
{
class SelectionChangedReceiver
{
public:
SelectionChangedReceiver();
virtual ~SelectionChangedReceiver();
protected:
friend class SelectionManager;
/// Called whenever caf::SelectionManager's selection changes
virtual void onSelectionManagerSelectionChanged() = 0;
};
}

View File

@ -79,8 +79,7 @@ void SelectionManager::selectedItems(std::vector<PdmUiItem*>& items, int role /*
void SelectionManager::setSelectedItems(const std::vector<PdmUiItem*>& items, int role /*= SelectionManager::APPLICATION_GLOBAL*/) void SelectionManager::setSelectedItems(const std::vector<PdmUiItem*>& items, int role /*= SelectionManager::APPLICATION_GLOBAL*/)
{ {
std::vector< std::pair<PdmPointer<PdmObjectHandle>, PdmUiItem*> >& selection = m_selectionForRole[role]; std::vector< std::pair<PdmPointer<PdmObjectHandle>, PdmUiItem*> >& selection = m_selectionForRole[role];
std::vector< std::pair<PdmPointer<PdmObjectHandle>, PdmUiItem*> > newSelection;
selection.clear();
for (size_t i = 0; i < items.size(); i++) for (size_t i = 0; i < items.size(); i++)
{ {
@ -89,19 +88,23 @@ void SelectionManager::setSelectedItems(const std::vector<PdmUiItem*>& items, in
{ {
PdmObjectHandle* obj = fieldHandle->fieldHandle()->ownerObject(); PdmObjectHandle* obj = fieldHandle->fieldHandle()->ownerObject();
selection.push_back(std::make_pair(obj, fieldHandle)); newSelection.push_back(std::make_pair(obj, fieldHandle));
} }
else else
{ {
PdmUiObjectHandle* obj = dynamic_cast<PdmUiObjectHandle*>(items[i]); PdmUiObjectHandle* obj = dynamic_cast<PdmUiObjectHandle*>(items[i]);
if (obj) if (obj)
{ {
selection.push_back(std::make_pair(obj->objectHandle(), obj)); newSelection.push_back(std::make_pair(obj->objectHandle(), obj));
} }
} }
} }
if (newSelection != selection)
{
selection = newSelection;
notifySelectionChanged(); notifySelectionChanged();
}
} }
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
@ -197,12 +200,17 @@ void SelectionManager::setSelectionFromReferences(const std::vector<QString>& re
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
void SelectionManager::clearAll() void SelectionManager::clearAll()
{ {
bool isChanged = false;
for (size_t i = 0; i < m_selectionForRole.size(); i++) for (size_t i = 0; i < m_selectionForRole.size(); i++)
{
if ( m_selectionForRole[i].size())
{ {
m_selectionForRole[i].clear(); m_selectionForRole[i].clear();
isChanged = true;
}
} }
notifySelectionChanged(); if (isChanged) notifySelectionChanged();
} }
@ -211,9 +219,12 @@ void SelectionManager::clearAll()
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
void SelectionManager::clear(int role) void SelectionManager::clear(int role)
{ {
if ( m_selectionForRole[role].size() )
{
m_selectionForRole[role].clear(); m_selectionForRole[role].clear();
notifySelectionChanged(); notifySelectionChanged();
}
} }
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
@ -225,6 +236,11 @@ void SelectionManager::notifySelectionChanged()
{ {
m_notificationCenter->notifyObserversOfSelectionChange(); m_notificationCenter->notifyObserversOfSelectionChange();
} }
for (auto receiver: m_selectionReceivers)
{
receiver->onSelectionManagerSelectionChanged();
}
} }
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------

View File

@ -33,18 +33,16 @@
// for more details. // for more details.
// //
//################################################################################################## //##################################################################################################
#pragma once #pragma once
#include "cafPdmPointer.h" #include "cafSelectionChangedReceiver.h"
#include "cafPdmPointer.h"
#include "cafPdmField.h" #include "cafPdmField.h"
#include <vector>
#include <QString> #include <QString>
#include <vector>
#include <set>
namespace caf namespace caf
{ {
@ -103,6 +101,8 @@ public:
} }
} }
/// Returns the selected objects of the requested type if _all_ the selected objects are of the requested type
template <typename T> template <typename T>
void objectsByTypeStrict(std::vector<T*>* typedObjects, int role = SelectionManager::APPLICATION_GLOBAL) void objectsByTypeStrict(std::vector<T*>* typedObjects, int role = SelectionManager::APPLICATION_GLOBAL)
{ {
@ -124,12 +124,18 @@ private:
SelectionManager(); SelectionManager();
void notifySelectionChanged(); void notifySelectionChanged();
friend class SelectionChangedReceiver;
void registerSelectionChangedReceiver ( SelectionChangedReceiver* receiver) { m_selectionReceivers.insert(receiver);}
void unregisterSelectionChangedReceiver( SelectionChangedReceiver* receiver) { m_selectionReceivers.erase(receiver);}
private: private:
std::vector < std::vector< std::pair<PdmPointer<PdmObjectHandle>, PdmUiItem*> > > m_selectionForRole; std::vector < std::vector< std::pair<PdmPointer<PdmObjectHandle>, PdmUiItem*> > > m_selectionForRole;
NotificationCenter* m_notificationCenter; NotificationCenter* m_notificationCenter;
PdmChildArrayFieldHandle* m_activeChildArrayFieldHandle; PdmChildArrayFieldHandle* m_activeChildArrayFieldHandle;
PdmPointer<PdmObjectHandle> m_rootObject; PdmPointer<PdmObjectHandle> m_rootObject;
std::set< SelectionChangedReceiver*> m_selectionReceivers;
}; };