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

View File

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