iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ModuleHierarchyModel.H
Go to the documentation of this file.
1 /*! @file
2  @author Unknown
3  @copyright GNU Public License (GPL v3)
4  @section License
5  @verbatim
6  // ////////////////////////////////////////////////////////////////////////
7  // The iLab Neuromorphic Robotics Toolkit (NRT) //
8  // Copyright 2010-2012 by the University of Southern California (USC) //
9  // and the iLab at USC. //
10  // //
11  // iLab - University of Southern California //
12  // Hedco Neurociences Building, Room HNB-10 //
13  // Los Angeles, Ca 90089-2520 - USA //
14  // //
15  // See http://ilab.usc.edu for information about this project. //
16  // ////////////////////////////////////////////////////////////////////////
17  // This file is part of The iLab Neuromorphic Robotics Toolkit. //
18  // //
19  // The iLab Neuromorphic Robotics Toolkit is free software: you can //
20  // redistribute it and/or modify it under the terms of the GNU General //
21  // Public License as published by the Free Software Foundation, either //
22  // version 3 of the License, or (at your option) any later version. //
23  // //
24  // The iLab Neuromorphic Robotics Toolkit is distributed in the hope //
25  // that it will be useful, but WITHOUT ANY WARRANTY; without even the //
26  // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
27  // PURPOSE. See the GNU General Public License for more details. //
28  // //
29  // You should have received a copy of the GNU General Public License //
30  // along with The iLab Neuromorphic Robotics Toolkit. If not, see //
31  // <http://www.gnu.org/licenses/>. //
32  // ////////////////////////////////////////////////////////////////////////
33  @endverbatim */
34 #ifndef SRC_APPS_NRTDESIGNER_MODULEHIERARCHYMODEL_H
35 #define SRC_APPS_NRTDESIGNER_MODULEHIERARCHYMODEL_H
36 
37 #include "Common.H"
40 NRT_BEGIN_UNCHECKED_INCLUDES
41 #include <QSortFilterProxyModel>
42 #include <QAbstractItemModel>
43 #include <QStringList>
44 #include <QSet>
45 #include <QMimeData>
46 #include <QMap>
47 #include <QRegExp>
48 NRT_END_UNCHECKED_INCLUDES
49 
50 #include <vector>
51 
52 class ModuleHierarchyItem;
53 
54 // ######################################################################
55 // A Model (as in Model-View-Controller) to represent a tree of module descriptions
56 // for use in the tree-view module library.
57 class ModuleHierarchyModel : public QAbstractItemModel
58 {
59  Q_OBJECT
60 
61  public:
62  ModuleHierarchyModel(std::vector<nrt::ModuleDescription> const & modules, QObject * parent = 0);
63 
64  QModelIndex index (int row, int column, const QModelIndex & parent = QModelIndex()) const;
65 
66  QModelIndex parent(const QModelIndex & index) const;
67 
68  int rowCount(const QModelIndex & parent = QModelIndex()) const;
69 
70  int columnCount(const QModelIndex & parent = QModelIndex()) const;
71 
72  QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;
73 
74  ModuleHierarchyItem* findItem(QStringList path);
75 
76  ModuleHierarchyItem* findItem(QString path);
77 
78  QList<ModuleHierarchyItem*> findItemsByClassName(QString classname);
79 
80  ModuleHierarchyItem* rootItem();
81 
82  QStringList mimeTypes() const;
83 
84  QMimeData* mimeData(QModelIndexList const & indexes) const;
85 
86  Qt::ItemFlags flags(QModelIndex const& index) const;
87 
88  private:
89  ModuleHierarchyItem* itsHierarchyRoot;
90 };
91 
92 // ######################################################################
93 class ModuleHierarchyItem
94 {
95  public:
96  enum itemtype_t { Module, Directory, MacroModule };
97 
98  itemtype_t itemType() const;
99 
100  static ModuleHierarchyItem *
101  createDirectoryItem(QString dirName, ModuleHierarchyItem* parent);
102 
103  static ModuleHierarchyItem *
104  createModuleItem(nrt::ModuleDescription const & moduledescr, ModuleHierarchyItem* parent);
105 
106  ModuleHierarchyItem(ModuleHierarchyItem const& other) = default;
107 
108  void appendChild(ModuleHierarchyItem* child);
109  ModuleHierarchyItem* child(int row);
110  int childCount() const;
111  int columnCount() const;
112  int row() const;
113  QVariant data(int column) const;
114  ModuleHierarchyItem* parent();
115  void setParent(ModuleHierarchyItem* parent);
116 
117  QString name() const;
118 
119  // This will throw an assert if you call on a itemtype() != Module
120  ModuleDescr const & moduleDescr() const;
121 
122  // This will throw an assert if you call on a itemtype() != Directory
123  QString dirPath() const;
124 
125  private:
126  ModuleHierarchyItem() = default;
127 
128  ModuleHierarchyItem* itsParentItem;
129  QList<ModuleHierarchyItem*> itsChildItems;
130  itemtype_t itsType;
131 
132  // Will not be used if itsType != Module
133  ModuleDescr itsModuleDescr;
134 
135  // Will not be used if itsType != Directory
136  QString itsDirPath;
137 
138  QString itsName;
139 };
140 
141 // ######################################################################
142 class ModuleHierarchyFilter : public QSortFilterProxyModel
143 {
144  Q_OBJECT
145 
146  public:
147  ModuleHierarchyFilter(QObject *parent = 0);
148  ModuleHierarchyItem* item(QModelIndex const& index);
149 
150  public slots:
151  void textChangedSlot(QString const& text);
152  void searchItemToggledSlot(bool toggled);
153 
154  protected:
155  bool filterAcceptsRow(int sourceRow, QModelIndex const& sourceParent) const;
156  void invalidateFilter();
157  bool itemMatchesFilter(ModuleHierarchyItem* item);
158 
159  private:
160  QSet<QString /*logical path*/> itsShowableItems;
161  QRegExp itsFilter;
162  QMap<QString /*search type*/, bool> itsSearchTypes;
163 };
164 
165 
166 #endif // SRC_APPS_NRTDESIGNER_MODULEHIERARCHYMODEL_H