SMTK  @SMTK_VERSION@
Simulation Modeling Tool Kit
Selection.h
1 //=========================================================================
2 // Copyright (c) Kitware, Inc.
3 // All rights reserved.
4 // See LICENSE.txt for details.
5 //
6 // This software is distributed WITHOUT ANY WARRANTY; without even
7 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8 // PURPOSE. See the above copyright notice for more information.
9 //=========================================================================
10 #ifndef smtk_view_Selection_h
11 #define smtk_view_Selection_h
12 
13 #include "smtk/CoreExports.h"
14 #include "smtk/PublicPointerDefs.h"
15 #include "smtk/SharedFromThis.h"
16 
17 #include "smtk/resource/Component.h"
18 #include "smtk/view/SelectionAction.h"
19 #include "smtk/view/SelectionObserver.h"
20 
21 #include <functional>
22 #include <map>
23 #include <set>
24 
25 namespace smtk
26 {
27 namespace view
28 {
29 
116 class SMTKCORE_EXPORT Selection : smtkEnableSharedPtr(Selection)
117 {
118 public:
119  using Observer = SelectionObserver;
123 
126 
127  Selection(const Selection&) = delete;
128  Selection& operator=(const Selection&) = delete;
129 
130  static Ptr instance();
131 
133  using SelectionMap = std::map<Object::Ptr, int>;
134 
150  using SelectionFilter = std::function<bool(Object::Ptr, int, SelectionMap&)>;
151 
152  virtual ~Selection();
153 
166  bool registerSelectionSource(std::string name) { return m_selectionSources.insert(name).second; }
167 
169  bool unregisterSelectionSource(std::string name) { return (m_selectionSources.erase(name) > 0); }
170 
172  const std::set<std::string>& getSelectionSources() const { return m_selectionSources; }
173 
175  void getSelectionSources(std::set<std::string>& selectionSources) const
176  {
177  selectionSources = m_selectionSources;
178  }
180 
189  bool
191  registerSelectionValue(const std::string& valueLabel, int value, bool valueMustBeUnique = true);
193  bool unregisterSelectionValue(const std::string& valueLabel)
194  {
195  return m_selectionValueLabels.erase(valueLabel) > 0;
196  }
198  bool unregisterSelectionValue(int value);
200  const std::map<std::string, int>& selectionValueLabels() const { return m_selectionValueLabels; }
202  int selectionValueFromLabel(const std::string& label) const;
204  int findOrCreateLabeledValue(const std::string& label);
206 
240  template<typename T>
241  bool modifySelection(
242  const T& objects,
243  const std::string& source,
244  int value,
246  bool bitwise = false,
247  bool postponeNotification = false);
248 
256  bool resetSelectionBits(const std::string& source, int value);
257 
275  bool setDefaultAction(const SelectionAction& action);
277 
279  SelectionAction defaultAction() const { return m_defaultAction; }
280 
281  void setDefaultActionToReplace() { m_defaultAction = SelectionAction::FILTERED_REPLACE; }
282 
283  void setDefaultActionToAddition() { m_defaultAction = SelectionAction::FILTERED_ADD; }
284 
285  void setDefaultActionToSubtraction() { m_defaultAction = SelectionAction::FILTERED_SUBTRACT; }
287 
296  void visitSelection(std::function<void(Object::Ptr, int)> visitor)
298  {
299  for (const auto& entry : m_selection)
300  {
301  visitor(entry.first, entry.second);
302  }
303  }
305  SelectionMap& currentSelection(SelectionMap& selection) const;
306  const SelectionMap& currentSelection() const { return m_selection; }
308  template<typename T>
309  T& currentSelectionByValue(T& selection, int value, bool exactMatch = true) const;
310  template<typename T>
311  T& currentSelectionByValue(T& selection, const std::string& valueLabel, bool exactMatch = true)
312  const;
313 
314  template<typename T>
315  T currentSelectionByValueAs(int value, bool exactMatch = true) const
316  {
317  T result;
318  return this->currentSelectionByValue(result, value, exactMatch);
319  }
320  template<typename T>
321  T currentSelectionByValueAs(const std::string& valueLabel, bool exactMatch = true) const
322  {
323  T result;
324  return this->currentSelectionByValue(result, valueLabel, exactMatch);
325  }
327 
329  Observers& observers() { return m_observers; }
330  const Observers& observers() const { return m_observers; }
331 
336  void setFilter(const SelectionFilter& fn, bool refilterSelection = true);
339 
368  bool configureItem(
369  const std::shared_ptr<smtk::attribute::ReferenceItem>& item,
370  int value,
371  bool exactMatch = false,
372  bool clearItem = true) const;
374 
375 protected:
376  Selection();
377 
379  bool performAction(
381  int value,
382  SelectionAction action,
383  SelectionMap& suggested,
384  bool bitwise);
385  bool refilter(const std::string& source);
386 
388  //smtk::model::BitFlags m_modelEntityMask;
389  bool m_meshSetMask;
390  std::set<std::string> m_selectionSources;
391  std::map<std::string, int> m_selectionValueLabels;
392  SelectionMap m_selection;
393  Observers m_observers;
394  SelectionFilter m_filter;
395 };
396 
397 template<typename T>
398 T& Selection::currentSelectionByValue(T& selection, int value, bool exactMatch) const
399 {
400  if (exactMatch)
401  {
402  for (const auto& entry : m_selection)
403  {
404  if ((entry.second & value) == value)
405  {
406  auto entryT = std::dynamic_pointer_cast<typename T::value_type::element_type>(entry.first);
407  if (entryT)
408  {
409  selection.insert(selection.end(), entryT);
410  }
411  }
412  }
413  }
414  else
415  {
416  for (const auto& entry : m_selection)
417  {
418  if (entry.second & value)
419  {
420  auto entryT = std::dynamic_pointer_cast<typename T::value_type::element_type>(entry.first);
421  if (entryT)
422  {
423  selection.insert(selection.end(), entryT);
424  }
425  }
426  }
427  }
428  return selection;
429 }
430 
431 template<typename T>
432 T& Selection::currentSelectionByValue(T& selection, const std::string& valueLabel, bool exactMatch)
433  const
434 {
435  int val = this->selectionValueFromLabel(valueLabel);
436  return this->currentSelectionByValue(selection, val, exactMatch);
437 }
438 
439 template<typename T>
441  const T& objects,
442  const std::string& source,
443  int value,
444  SelectionAction action,
445  bool bitwise,
446  bool postponeNotification)
447 {
448  bool modified = false;
449  SelectionMap suggestions;
450  if (action == SelectionAction::DEFAULT)
451  {
452  action = this->defaultAction();
453  }
454  if (
455  !bitwise &&
457  {
458  modified = !m_selection.empty();
459  m_selection.clear();
460  }
461  else if (
462  bitwise &&
464  {
465  // Remove unmatched objects from existing selection
466  int mask = ~value;
468  for (auto& entry : m_selection)
469  {
470  auto it = std::find(objects.begin(), objects.end(), entry.first);
471  // std::cout << " " << entry.first->id() << " " << (it == objects.end() ? "not in obj" : "in obj") << " " << entry.second << " " << (entry.second & mask);
472  if ((it == objects.end() && ((entry.second & mask) == 0)) || value == 0)
473  {
474  willErase.insert(entry.first);
475  // std::cout << " erase obj\n";
476  }
477  else if (it == objects.end() && ((entry.second & mask) != 0))
478  {
479  entry.second &= mask;
480  modified = true;
481  }
482  else
483  {
484  // std::cout << " keep obj\n";
485  }
486  }
487  // std::cout << "---\n";
488  if (!willErase.empty())
489  {
490  modified = true;
491  for (const auto& key : willErase)
492  {
493  m_selection.erase(key);
494  }
495  }
496  }
497  for (const auto& object : objects)
498  {
499  modified |= this->performAction(object, value, action, suggestions, bitwise);
500  }
501  if (modified && !postponeNotification)
502  {
503  this->observers()(source, shared_from_this());
504  }
505  return modified;
506 }
507 
508 } // namespace view
509 } // namespace smtk
510 
511 #endif
smtk
The main namespace for the Simulation Modeling Tool Kit (SMTK).
Definition: doc.h:33
smtk::view::SelectionAction::UNFILTERED_REPLACE
@ UNFILTERED_REPLACE
Replace all the current selection and do not filter it.
smtk::view::Selection::defaultAction
SelectionAction defaultAction() const
Return the current method used to modify selections when.
Definition: Selection.h:279
PublicPointerDefs.h
smtk::view::Selection::performAction
bool performAction(smtk::resource::PersistentObjectPtr comp, int value, SelectionAction action, SelectionMap &suggested, bool bitwise)
Perform the action (ignoring m_defaultAction!!!), returning true if it had an effect.
Definition: Selection.cxx:276
smtk::view::Selection::currentSelectionByValue
T & currentSelectionByValue(T &selection, int value, bool exactMatch=true) const
Return the subset of selected elements that match the given selection value.
Definition: Selection.h:398
smtk::view::Selection::observers
Observers & observers()
Return the observers associated with this phrase model.
Definition: Selection.h:329
smtk::view::Selection::getSelectionSources
void getSelectionSources(std::set< std::string > &selectionSources) const
Populate a set with the names of all registered selection sources.
Definition: Selection.h:175
smtk::view::Selection::registerSelectionSource
bool registerSelectionSource(std::string name)
Selection sources.
Definition: Selection.h:166
smtk::view::SelectionAction
SelectionAction
Descriptors for how lists passed to the selection should modify the selection-map.
Definition: SelectionAction.h:19
smtk::common::Observers
An Observer is a functor that is called when certain actions are performed.
Definition: Observers.h:66
smtk::view::Selection::SelectionMap
std::map< Object::Ptr, int > SelectionMap
This is the underlying storage type that holds selections.
Definition: Selection.h:133
smtk::view::SelectionObservers
smtk::common::Observers< SelectionObserver > SelectionObservers
A class for holding SelectionObserver functors that observe selection events.
Definition: SelectionObserver.h:29
smtk::view::Selection::modifySelection
bool modifySelection(const T &objects, const std::string &source, int value, SelectionAction action=SelectionAction::DEFAULT, bool bitwise=false, bool postponeNotification=false)
Modify the current selection.
Definition: Selection.h:440
smtkCreateMacro
#define smtkCreateMacro(...)
Add static create() methods to a class.
Definition: SharedFromThis.h:113
smtkEnableSharedPtr
#define smtkEnableSharedPtr(...)
An abbreviation for enabling shared pointers.
Definition: SharedFromThis.h:154
smtk::view::SelectionAction::DEFAULT
@ DEFAULT
Use the default SelectionAction provided by the Selection.
smtk::view::Selection::SelectionFilter
std::function< bool(Object::Ptr, int, SelectionMap &)> SelectionFilter
Selection filters take functions of this form.
Definition: Selection.h:150
smtk::view::Selection::selectionValueFromLabel
int selectionValueFromLabel(const std::string &label) const
Return the selection value for the given label, or 0 if there is no such label registered.
Definition: Selection.cxx:115
smtk::view::Selection::unregisterSelectionSource
bool unregisterSelectionSource(std::string name)
Unregister a selection source; if false is returned, the source was not recognized.
Definition: Selection.h:169
smtk::view::Selection::unregisterSelectionValue
bool unregisterSelectionValue(const std::string &valueLabel)
Unregister a selection value. If the value was not registered, returns false.
Definition: Selection.h:193
smtk::view::SelectionAction::FILTERED_SUBTRACT
@ FILTERED_SUBTRACT
Filter the input selection, then subtract it from the current selection.
smtk::view::Selection
A class to manage linked selections in a context.
Definition: Selection.h:116
smtk::view::SelectionAction::FILTERED_REPLACE
@ FILTERED_REPLACE
Replace all the current selection and filter it.
smtk::view::Selection::getSelectionSources
const std::set< std::string > & getSelectionSources() const
Populate a set with the names of all registered selection sources.
Definition: Selection.h:172
smtk::resource::PersistentObjectSet
std::set< smtk::resource::PersistentObjectPtr > PersistentObjectSet
Definition: PublicPointerDefs.h:291
smtk::view::SelectionAction::FILTERED_ADD
@ FILTERED_ADD
Filter the input selection and add it to the current selection.
smtk::resource::Component
Component is the base class for records stored in an smtk::resource::Resource.
Definition: Component.h:43
smtk::resource::PersistentObjectPtr
smtk::shared_ptr< smtk::resource::PersistentObject > PersistentObjectPtr
Definition: PublicPointerDefs.h:285
SharedFromThis.h
Macros for dealing with shared-pointer classes.
smtkTypeMacroBase
#define smtkTypeMacroBase(...)
Add typedefs to a class for identifcation.
Definition: SharedFromThis.h:55
smtk::view::Selection::selectionValueLabels
const std::map< std::string, int > & selectionValueLabels() const
Return the map of selection values.
Definition: Selection.h:200
smtk::view::SelectionObserver
std::function< void(const std::string &, SelectionPtr)> SelectionObserver
Events that alter the selection trigger callbacks of this type.
Definition: SelectionObserver.h:26
smtk::resource::PersistentObject
An abstract base class for SMTK resources and their components.
Definition: PersistentObject.h:39