SMTK  @SMTK_VERSION@
Simulation Modeling Tool Kit
PhraseModel.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_PhraseModel_h
11 #define smtk_view_PhraseModel_h
12 
13 #include "smtk/CoreExports.h"
14 #include "smtk/PublicPointerDefs.h"
15 #include "smtk/SharedFromThis.h"
16 
17 #include "smtk/common/Deprecation.h"
18 #include "smtk/common/ThreadPool.h"
19 #include "smtk/common/TypeContainer.h"
20 #include "smtk/common/Visit.h"
21 
22 #include "smtk/view/BadgeSet.h"
23 #include "smtk/view/PhraseContent.h"
24 #include "smtk/view/PhraseModelObserver.h"
25 #include "smtk/view/Selection.h"
26 
27 #include "smtk/resource/Manager.h"
28 #include "smtk/resource/Resource.h"
29 
30 #include "smtk/operation/Manager.h"
31 #include "smtk/operation/Operation.h"
32 
33 #include <functional>
34 #include <list>
35 #include <set>
36 #include <unordered_map>
37 
38 namespace smtk
39 {
40 namespace view
41 {
42 
43 // Typedef representing a map of UUID to a set of weak pointers to Descriptive Phrases that refer to
44 // that UUId's PersistentObject
45 typedef std::unordered_map<
47  std::set<
48  std::weak_ptr<smtk::view::DescriptivePhrase>,
49  std::owner_less<std::weak_ptr<smtk::view::DescriptivePhrase>>>>
50  UUIDsToPhrasesMap;
51 
52 // Sort paths from deepest to shallowest, then rear-most to front-most.
53 // Doing these things keeps us from invalidating paths when items are removed.
54 struct PathComp
55 {
56  bool operator()(const std::vector<int>& a, const std::vector<int>& b) const
57  {
58  if (a.size() < b.size())
59  {
60  return false;
61  }
62  else if (a.size() > b.size())
63  {
64  return true;
65  }
66  std::size_t ii = 0;
67  for (auto ai : a)
68  {
69  if (ai < b[ii])
70  {
71  return false;
72  }
73  else if (ai > b[ii])
74  {
75  return true;
76  }
77  ++ii;
78  }
79  return false; // a == b... neither is less than other.
80  }
81 };
82 
83 class PhraseDeltas : public std::set<std::vector<int>, PathComp>
84 {
85 };
86 
101 class SMTKCORE_EXPORT PhraseModel : smtkEnableSharedPtr(PhraseModel)
102 {
103 public:
109  using PhraseDecorator = std::function<void(smtk::view::DescriptivePhrasePtr)>;
112  using SourceVisitor = std::function<bool(
115  const smtk::view::ManagerPtr&,
117 
120  using SourceVisitorFunction = std::function<smtk::common::Visit(
123  const smtk::view::ManagerPtr&,
125 
127  using OperationPtr = smtk::operation::Operation::Ptr;
128  using ComponentItemPtr = smtk::attribute::ComponentItemPtr;
129 
131 
134  virtual ~PhraseModel();
135 
141  static SubphraseGeneratorPtr configureSubphraseGenerator(const Configuration*, Manager*);
142 
144  static std::multimap<std::string, std::string> configureFilterStrings(
145  const Configuration* config,
146  Manager* manager);
147 
157  virtual bool addSource(const smtk::common::TypeContainer& managers);
161  virtual bool removeSource(const smtk::common::TypeContainer& managers);
163  virtual bool resetSources();
165  SMTK_DEPRECATED_IN_21_12("This is the older style visitor which returns true to continue and "
166  "false to halt instead of using the smtk::common::Visit enums")
167  virtual void visitSources(SourceVisitor visitor);
169  virtual void visitSources(SourceVisitorFunction visitor);
178  static smtk::operation::Observers::Priority operationObserverPriority();
180 
182  virtual DescriptivePhrasePtr root() const;
183 
186  virtual void updateChildren(
187  smtk::view::DescriptivePhrasePtr plist,
188  DescriptivePhrases& next,
189  const std::vector<int>& idx);
190 
192  virtual void triggerDataChanged();
193 
195  virtual void triggerDataChangedFor(smtk::resource::ComponentPtr comp);
196 
198  Observers& observers() { return m_observers; }
199  const Observers& observers() const { return m_observers; }
200 
202  smtk::operation::ManagerPtr operationManager() const;
203 
214  void setMutableAspects(int mutableAspects) { m_mutableAspects = mutableAspects; }
215 
220  int mutableAspects() const { return m_mutableAspects; }
221 
223  ManagerPtr manager() const { return m_manager.lock(); }
224  friend Manager;
225 
227  const BadgeSet& badges() const { return m_badges; }
228  BadgeSet& badges() { return m_badges; }
229 
231  const UUIDsToPhrasesMap uuidPhraseMap() const { return m_objectMap; }
232 
233  smtk::common::ThreadPool<DescriptivePhrases>& threadPool() { return m_pool; }
234 
235 protected:
236  PhraseModel();
237  PhraseModel(const Configuration* config, Manager* manager);
238 
240  virtual void handleSelectionEvent(const std::string& src, smtk::view::SelectionPtr seln);
241 
243  virtual void handleResourceEvent(const Resource& rsrc, smtk::resource::EventType event);
244 
252  virtual int
253  handleOperationEvent(const Operation& op, operation::EventType e, const Operation::Result& res);
254 
260  void removeChildren(const std::vector<int>& parentIdx, int childRange[2]);
261 
263  void setPhraseParent(const DescriptivePhrasePtr& phrase, const DescriptivePhrasePtr& parent)
264  const;
265 
267  virtual void handleExpunged(const smtk::resource::PersistentObjectSet& expungedObjects);
269  virtual void handleModified(const smtk::resource::PersistentObjectSet& modifiedObjects);
271  virtual void handleCreated(const smtk::resource::PersistentObjectSet& createdObjects);
272 
285  virtual void redecorate();
286 
292 
297  virtual void trigger(
300  const std::vector<int>& src,
301  const std::vector<int>& dst,
302  const std::vector<int>& refs);
303 
306  void removeFromMap(const DescriptivePhrasePtr& phr);
307 
310  void insertIntoMap(const DescriptivePhrasePtr& phrase);
311 
312  struct Source
313  {
314  smtk::common::TypeContainer m_managers;
315  smtk::resource::Observers::Key m_rsrcHandle;
316  smtk::operation::Observers::Key m_operHandle;
318  Source() = default;
319 
320  [[deprecated("PhraseModel::Source::Source now accepts managers held in a const "
322 
323  Source(
324  const smtk::common::TypeContainer& managers,
325  smtk::resource::Observers::Key&& rh,
328  };
329  std::list<Source> m_sources;
330 
331  UUIDsToPhrasesMap m_objectMap;
332  Observers m_observers;
333 
334  PhraseDecorator m_decorator;
335 
336  BadgeSet m_badges;
337 
338  int m_mutableAspects{ PhraseContent::EVERYTHING };
339 
340  WeakManagerPtr m_manager;
341 
342  // Indicates we are in the process of updating children phrases
343  bool m_updatingChildren = false;
344 
346 };
347 } // namespace view
348 } // namespace smtk
349 
350 #endif
smtk
The main namespace for the Simulation Modeling Tool Kit (SMTK).
Definition: doc.h:33
smtk::view::PhraseModel::badges
const BadgeSet & badges() const
Return the badges that may apply to phrases in this model.
Definition: PhraseModel.h:227
PublicPointerDefs.h
smtk::common::Observers::Key
Definition: Observers.h:99
smtk::view::PhraseModel::SourceVisitor
std::function< bool(const smtk::resource::ManagerPtr &, const smtk::operation::ManagerPtr &, const smtk::view::ManagerPtr &, const smtk::view::SelectionPtr &)> SourceVisitor
Subclasses (and others) may wish to invoke functions on the sources of data for the phrases.
Definition: PhraseModel.h:116
smtk::view::BadgeSet
A container for the badges that apply to a view.
Definition: BadgeSet.h:30
smtk::view::SelectionPtr
smtk::shared_ptr< smtk::view::Selection > SelectionPtr
Definition: PublicPointerDefs.h:634
smtk::view::Manager
A view Manager is responsible for creating new views (eventually) as well as view components such as ...
Definition: Manager.h:40
smtk::view::PhraseModel::uuidPhraseMap
const UUIDsToPhrasesMap uuidPhraseMap() const
Return the map between persistent object IDs and Descriptive Phrases.
Definition: PhraseModel.h:231
smtk::view::PhraseModel::Source
Definition: PhraseModel.h:312
smtk::attribute::ComponentItemPtr
smtk::shared_ptr< smtk::attribute::ComponentItem > ComponentItemPtr
Definition: PublicPointerDefs.h:539
smtk::view::PhraseModelEvent
PhraseModelEvent
Events that can be observed on an smtk::view::PhraseModel.
Definition: PhraseModelObserver.h:26
smtk::view::PhraseModel::setMutableAspects
void setMutableAspects(int mutableAspects)
Set what aspects of top-level phrases should be user-editable.
Definition: PhraseModel.h:214
smtk::common::UUID
Definition: UUID.h:38
smtk::view::PhraseModel::manager
ManagerPtr manager() const
PhraseModels that are managed have a non-null pointer to their manager.
Definition: PhraseModel.h:223
smtk::common::Observers
An Observer is a functor that is called when certain actions are performed.
Definition: Observers.h:66
smtk::view::DescriptivePhrases
std::vector< smtk::view::DescriptivePhrasePtr > DescriptivePhrases
Definition: PublicPointerDefs.h:624
smtk::view::PhraseModelObservers
smtk::common::Observers< PhraseModelObserver > PhraseModelObservers
A class for holding PhraseModelObserver functors that observe phrase model events.
Definition: PhraseModelObserver.h:47
smtk::view::PhraseModel::mutableAspects
int mutableAspects() const
Get what aspects of top-level phrases should be user-editable.
Definition: PhraseModel.h:220
smtk::resource::EventType
EventType
Enumerate events that the resource manager may encounter.
Definition: Observer.h:32
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::common::Visit
Visit
Return values common to most visitor methods.
Definition: Visit.h:23
smtk::operation::ManagerPtr
smtk::shared_ptr< smtk::operation::Manager > ManagerPtr
Definition: PublicPointerDefs.h:348
smtk::view::ManagerPtr
smtk::shared_ptr< smtk::view::Manager > ManagerPtr
Definition: PublicPointerDefs.h:626
smtk::view::PhraseDeltas
Definition: PhraseModel.h:83
smtk::view::PhraseModelObserver
std::function< void(DescriptivePhrasePtr, PhraseModelEvent, const std::vector< int > &, const std::vector< int > &, const std::vector< int > &)> PhraseModelObserver
Events that alter the phrase model trigger callbacks of this type.
Definition: PhraseModelObserver.h:44
smtk::view::PhraseModel::Observer
PhraseModelObserver Observer
Events that alter the phrase model trigger callbacks of this type.
Definition: PhraseModel.h:105
smtk::view::PhraseModel
Hold and maintain a descriptive phrase hierarchy.
Definition: PhraseModel.h:101
smtk::operation::Operation
Operation is a base class for all SMTK operations.
Definition: Operation.h:51
smtk::view::PhraseContent::EVERYTHING
@ EVERYTHING
Every aspect of the phrase content.
Definition: PhraseContent.h:54
smtk::view::Configuration
Configure a view, specifying types and attributes, without specifying a UI library.
Definition: Configuration.h:28
smtk::view::WeakManagerPtr
smtk::weak_ptr< smtk::view::Manager > WeakManagerPtr
Definition: PublicPointerDefs.h:628
smtk::view::SubphraseGeneratorPtr
smtk::shared_ptr< smtk::view::SubphraseGenerator > SubphraseGeneratorPtr
Definition: PublicPointerDefs.h:638
smtk::view::PhraseModel::SourceVisitorFunction
std::function< smtk::common::Visit(const smtk::resource::ManagerPtr &, const smtk::operation::ManagerPtr &, const smtk::view::ManagerPtr &, const smtk::view::SelectionPtr &)> SourceVisitorFunction
Subclasses (and others) may wish to invoke functions on the sources of data for the phrases.
Definition: PhraseModel.h:124
smtk::resource::Resource
An abstract base class for SMTK resources.
Definition: Resource.h:60
smtk::resource::PersistentObjectSet
std::set< smtk::resource::PersistentObjectPtr > PersistentObjectSet
Definition: PublicPointerDefs.h:291
smtk::view::DescriptivePhrasePtr
smtk::shared_ptr< smtk::view::DescriptivePhrase > DescriptivePhrasePtr
Definition: PublicPointerDefs.h:620
Visit.h
SharedFromThis.h
Macros for dealing with shared-pointer classes.
smtkTypeMacroBase
#define smtkTypeMacroBase(...)
Add typedefs to a class for identifcation.
Definition: SharedFromThis.h:55
smtk::operation::EventType
EventType
Enumerate events that an operation may encounter.
Definition: Observer.h:30
smtk::view::PathComp
Definition: PhraseModel.h:54
smtk::common::ThreadPool< DescriptivePhrases >
smtk::resource::ManagerPtr
smtk::shared_ptr< smtk::resource::Manager > ManagerPtr
Definition: PublicPointerDefs.h:281
smtk::resource::ComponentPtr
smtk::shared_ptr< smtk::resource::Component > ComponentPtr
Definition: PublicPointerDefs.h:297
smtk::view::PhraseModel::PhraseDecorator
std::function< void(smtk::view::DescriptivePhrasePtr)> PhraseDecorator
Applications may have a model decorate its phrases by providing a method with this signature.
Definition: PhraseModel.h:109
smtk::common::TypeContainer
A container for caching and retrieving instances of types.
Definition: TypeContainer.h:37