SMTK  @SMTK_VERSION@
Simulation Modeling Tool Kit
qtDescriptivePhraseModel.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 
11 #ifndef smtk_extension_qtDescriptivePhraseModel_h
12 #define smtk_extension_qtDescriptivePhraseModel_h
13 
14 #include "smtk/PublicPointerDefs.h"
15 #include "smtk/extension/qt/Exports.h" // For EXPORT macro.
16 #include "smtk/view/DescriptivePhrase.h"
17 #include "smtk/view/PhraseModel.h"
18 #include "smtk/view/PhraseModelObserver.h"
19 
20 #include <QAbstractItemModel>
21 #include <QIcon>
22 
23 #include <map>
24 
25 namespace smtk
26 {
27 namespace extension
28 {
29 
40 class SMTKQTEXT_EXPORT qtDescriptivePhraseModel : public QAbstractItemModel
41 {
42  Q_OBJECT
43 public:
44  qtDescriptivePhraseModel(QObject* parent = nullptr);
45  ~qtDescriptivePhraseModel() override;
46  // Set and get default entity color
47  static void setDefaultPhraseColor(const std::string& entityType, const QColor& color)
48  {
49  s_defaultColors[entityType] = color;
50  }
51  static QColor defaultPhraseColor(const std::string& entityType);
52 
54  std::string visibleIconURL() const { return m_visibleIconURL; }
55  std::string invisibleIconURL() const { return m_invisibleIconURL; }
56  void setVisibleIconURL(const std::string& url) { m_visibleIconURL = url; }
57  void setInvisibleIconURL(const std::string& url) { m_invisibleIconURL = url; }
58 
60  enum DataRoles
61  {
62  TitleTextRole = Qt::UserRole + 100,
63  EditableTitleTextRole =
64  Qt::UserRole + 101,
65  SubtitleTextRole = Qt::UserRole + 102,
66  // PhraseIconRole_LightBG = Qt::UserRole + 103, //!< Phrase type icon on light background
67  // PhraseIconRole_DarkBG = Qt::UserRole + 104, //!< Phrase type icon on dark background
68  // PhraseColorRole = Qt::UserRole + 105, //!< Phrase-specific color (e.g., component color)
69  // PhraseVisibilityRole = Qt::UserRole + 106, //!< Visibility of phrase's subject
70  PhraseCleanRole = Qt::UserRole + 107,
71  PhraseLockRole = Qt::UserRole + 108,
72  ModelActiveRole = Qt::UserRole + 109,
73  TitleTextMutableRole = Qt::UserRole + 110,
74  // ColorMutableRole = Qt::UserRole + 111, //!< Is the subject's color editable?
75  PhrasePtrRole = Qt::UserRole + 112,
76  BadgesRole = Qt::UserRole + 113
77  };
78 
79  void setPhraseModel(smtk::view::PhraseModelPtr model);
80  smtk::view::PhraseModelPtr phraseModel() const { return m_model; }
81 
82  QModelIndex index(int row, int column, const QModelIndex& parent) const override;
83  QModelIndex parent(const QModelIndex& child) const override;
84  bool hasChildren(const QModelIndex& parent) const override;
85 
86  int rowCount(const QModelIndex& parent = QModelIndex()) const override;
87  int columnCount(const QModelIndex& inParent = QModelIndex()) const override
88  {
89  (void)inParent;
90  return 1;
91  }
92 
93  QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
94  QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
95 
96  // bool insertRows(int position, int rows, const QModelIndex& parent = QModelIndex()) override;
97  bool removeRows(int position, int rows, const QModelIndex& parent = QModelIndex()) override;
98  bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole) override;
99 
100  Qt::ItemFlags flags(const QModelIndex& index) const override;
101 
102  void setDeleteOnRemoval(bool del) { m_deleteOnRemoval = del; }
103 
104  view::DescriptivePhrasePtr getItem(const QModelIndex& idx) const;
105  QModelIndex indexFromPath(const std::vector<int>& path) const;
106 
107  template<typename T, typename C>
108  bool foreach_phrase(
109  T& visitor,
110  C& collector,
111  const QModelIndex& top = QModelIndex(),
112  bool onlyBuilt = true);
113  template<typename T, typename C>
114  bool foreach_phrase(
115  T& visitor,
116  C& collector,
117  const QModelIndex& top = QModelIndex(),
118  bool onlyBuilt = true) const;
119 
120  void rebuildSubphrases(const QModelIndex& qidx);
121 
122  Qt::DropActions supportedDropActions() const override;
123 
124  void setColumnName(const std::string& name) { m_columnName = name; }
125  const std::string& columnName() const { return m_columnName; }
126 
127 Q_SIGNALS:
128  void phraseTitleChanged(const QModelIndex&);
129 
130 protected:
131  void updateObserver(
134  const std::vector<int>& src,
135  const std::vector<int>& dst,
136  const std::vector<int>& range);
137 
140  bool m_deleteOnRemoval; // remove UUIDs from mesh when they are removed from the list?
141  std::string m_visibleIconURL;
142  std::string m_invisibleIconURL;
143  static std::map<std::string, QColor> s_defaultColors;
144  std::string m_columnName;
145  class Internal;
146  Internal* P;
147 };
148 
153 template<typename T, typename C>
155  T& visitor,
156  C& collector,
157  const QModelIndex& top,
158  bool onlyBuilt)
159 {
160  // visit parent, then children if we aren't told to terminate:
161  if (!visitor(this, top, collector))
162  {
165  // Do not descend if top's corresponding phrase would have to invoke
166  // the subphrase generator to obtain the list of children... some phrases
167  // are cyclic graphs. In these cases, only descend if "onlyBuilt" is false.
168  if (phrase && (!onlyBuilt || phrase->areSubphrasesBuilt()))
169  {
170  for (int row = 0; row < this->rowCount(top); ++row)
171  {
172  if (this->foreach_phrase(visitor, collector, this->index(row, 0, top), onlyBuilt))
173  return true; // early termination;
174  }
175  }
176  }
177  return false;
178 }
179 
181 template<typename T, typename C>
183  T& visitor,
184  C& collector,
185  const QModelIndex& top,
186  bool onlyBuilt) const
187 {
188  // visit parent, then children if we aren't told to terminate:
189  if (!visitor(this, top, collector))
190  {
193  // Do not descend if top's corresponding phrase would have to invoke
194  // the subphrase generator to obtain the list of children... some models
195  // are cyclic graphs. In these cases, only descend if "onlyBuilt" is false.
196  if (phrase && (!onlyBuilt || phrase->areSubphrasesBuilt()))
197  {
198  for (int row = 0; row < this->rowCount(top); ++row)
199  {
200  if (this->foreach_phrase(visitor, collector, this->index(row, 0, top), onlyBuilt))
201  return true; // early termination;
202  }
203  }
204  }
205  return false;
206 }
207 
208 } // namespace extension
209 } // namespace smtk
210 
211 #endif // smtk_extension_qtDescriptivePhraseModel_h
smtk
The main namespace for the Simulation Modeling Tool Kit (SMTK).
Definition: doc.h:33
PublicPointerDefs.h
smtk::common::Observers::Key
Definition: Observers.h:106
smtk::view::PhraseModelEvent
PhraseModelEvent
Events that can be observed on an smtk::view::PhraseModel.
Definition: PhraseModelObserver.h:26
smtk::extension::qtDescriptivePhraseModel::DataRoles
DataRoles
Enumeration of model-specific data roles.
Definition: qtDescriptivePhraseModel.h:60
smtk::view::PhraseModelPtr
smtk::shared_ptr< smtk::view::PhraseModel > PhraseModelPtr
Definition: PublicPointerDefs.h:637
smtk::extension::qtDescriptivePhraseModel::PhrasePtrRole
@ PhrasePtrRole
Grab the whole descriptive phrase!
Definition: qtDescriptivePhraseModel.h:75
smtk::view::DescriptivePhrasePtr
smtk::shared_ptr< smtk::view::DescriptivePhrase > DescriptivePhrasePtr
Definition: PublicPointerDefs.h:627
smtk::extension::qtDescriptivePhraseModel::foreach_phrase
bool foreach_phrase(T &visitor, C &collector, const QModelIndex &top=QModelIndex(), bool onlyBuilt=true)
Iterate over all expanded entries in the tree.
Definition: qtDescriptivePhraseModel.h:154
smtk::extension::qtDescriptivePhraseModel
Adapt an smtk::view::PhraseModel instance into a hierarchical Qt model.
Definition: qtDescriptivePhraseModel.h:40
smtk::extension::qtDescriptivePhraseModel::rowCount
int rowCount(const QModelIndex &parent=QModelIndex()) const override
The number of rows in the table "underneath" owner.
Definition: qtDescriptivePhraseModel.cxx:252
smtk::extension::qtDescriptivePhraseModel::visibleIconURL
std::string visibleIconURL() const
Set and get the icons to be used when visibility is to be drawn.
Definition: qtDescriptivePhraseModel.h:54