VPTissue Reference Manual
WorkspaceQtModel.h
Go to the documentation of this file.
1 #ifndef WORKSPACEQTMODEL_H_INCLUDED
2 #define WORKSPACEQTMODEL_H_INCLUDED
3 /*
4  * Copyright 2011-2016 Universiteit Antwerpen
5  *
6  * Licensed under the EUPL, Version 1.1 or as soon they will be approved by
7  * the European Commission - subsequent versions of the EUPL (the "Licence");
8  * You may not use this work except in compliance with the Licence.
9  * You may obtain a copy of the Licence at: http://ec.europa.eu/idabc/eupl5
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the Licence is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the Licence for the specific language governing
15  * permissions and limitations under the Licence.
16  */
23 #include "workspace/IWorkspace.h"
24 #include "workspace/IProject.h"
25 
26 #include <QAbstractItemModel>
27 #include <boost/property_tree/ptree.hpp>
28 #include <memory>
29 #include <vector>
30 
31 namespace SimShell {
32 namespace Ws {
33  class IFile;
34 }
35 
36 namespace Gui {
37 
38 using namespace Ws;
39 using namespace Ws::Event;
40 
50  public std::enable_shared_from_this<WorkspaceQtModel>
51 {
52  Q_OBJECT
53 public:
54  static std::shared_ptr<WorkspaceQtModel>
55  Create(const std::shared_ptr<IWorkspace>&,
57  QObject* parent = 0);
58 
59  virtual ~WorkspaceQtModel();
60 
61  // Implementation of QAbstractItemModel methods.
62  virtual int columnCount(QModelIndex const& parent = QModelIndex()) const;
63  virtual QVariant data(QModelIndex const& index, int role = Qt::DisplayRole) const;
64  virtual Qt::ItemFlags flags(QModelIndex const& index) const;
65  virtual QVariant headerData(int section, Qt::Orientation, int role) const;
66  virtual QModelIndex index(int row, int column, QModelIndex const& parent = QModelIndex()) const;
67  virtual bool insertRow(int row, QModelIndex const& parent = QModelIndex());
68  virtual bool insertRows(int row, int count, QModelIndex const& parent = QModelIndex());
69  virtual QModelIndex parent(QModelIndex const& index) const;
70  virtual bool removeRow(int row, QModelIndex const& parent = QModelIndex());
71  virtual bool removeRows(int row, int count, QModelIndex const& parent = QModelIndex());
72  virtual int rowCount(QModelIndex const& parent = QModelIndex()) const;
73 
74  enum ItemType { RootType, ProjectType, FileType };
75 
80  bool Close();
81 
87  bool Close(const QModelIndex& index);
88 
96  std::vector<QAction*> GetContextMenuActions(const QModelIndex& index) const;
97 
106  const std::string& GetName(QModelIndex const& index) const;
107 
116  ItemType GetType(QModelIndex const& index) const;
117 
118  bool IsOpened(const QModelIndex& index) const;
119 
120  bool Open(const QModelIndex& index);
121 
122  std::shared_ptr<IWorkspace> Workspace() { return m_workspace; }
123 
124 private:
131  WorkspaceQtModel(const std::shared_ptr<IWorkspace>&,
133  QObject* parent = 0);
134 
138  struct Item
139  {
140  ItemType type;
141  int row;
142 
143  // Various constructors initialize different structs in anonymous union.
144 
145  Item()
146  : type(RootType), row(0), workspace() {}
147 
148  Item(int r, const std::string& name, const std::shared_ptr<IProject>& obj)
149  : type(ProjectType), row(r), project({name, obj, {}}) {}
150 
151  Item(int r, const std::string& name, Item* parent, const std::shared_ptr<IFile>& obj)
152  : type(FileType), row(r), file({name, parent, obj}) {}
153 
154  struct WS {
155  std::vector<Item*> children;
156  };
157  struct PR {
158  std::string name;
159  std::shared_ptr<IProject> obj;
160  std::vector<Item*> children;
161  };
162  struct FI {
163  std::string name;
164  Item* parent;
165  std::shared_ptr<IFile> obj;
166  };
167  union {
168  WS workspace;
169  PR project;
170  FI file;
171  };
172 
173  ~Item() {
174  // Destructor deletes item's children.
175  switch (type) {
176  case RootType:
177  for (auto child : workspace.children)
178  delete child;
179  break;
180  case ProjectType:
181  for (auto child : project.children)
182  delete child;
183  break;
184  default:
185  break;
186  }
187  }
188  };
189 
190 
192  void ListenWorkspaceEvent(const Ws::Event::WorkspaceChanged&);
193 
195  void ListenProjectEvent(Item* project_item, const Ws::Event::ProjectChanged&);
196 
197  int FindProjectRow(const std::string& name);
198  int FindProjectRowInsert(IWorkspace::ConstProjectIterator);
199  int FindFileRow(Item* project_item, const std::string& name);
200  int FindFileRowInsert(Item* project_item, IProject::ConstFileIterator);
201 
202  Item* m_root;
203  std::shared_ptr<IWorkspace> m_workspace;
204 
205  Controller::ProjectController* m_project_controller;
206 };
207 
208 } // namespace
209 } // namespace
210 
211 #endif // end_of_inclde_guard
see the online Qt documentation
Interface for IProject.
Interface for IWorkspace.
Abstraction of workspace on file system.
ProjectController header.
see the online Qt documentation
Namespace for generic graphical shell for simulators.
Definition: SimSession.h:32
see the online C++11 documentation
Abstraction of workspace on the filesystem.