Wt examples  4.10.4
GitModel.h
Go to the documentation of this file.
1 // This may look like C code, but it's really -*- C++ -*-
2 /*
3  * Copyright (C) 2008 Emweb bv, Herent, Belgium.
4  *
5  * See the LICENSE file for terms of use.
6  */
7 #ifndef GIT_MODEL_H_
8 #define GIT_MODEL_H_
9 
10 #include <Wt/WAbstractItemModel.h>
11 
12 #include "Git.h"
13 
14 using namespace Wt;
15 
20 
39 {
40 public:
43  static const ItemDataRole ContentsRole;
44  static const ItemDataRole FilePathRole;
45 
48  GitModel();
49 
52  void setRepositoryPath(const std::string& repositoryPath);
53 
59  void loadRevision(const std::string& revName);
60 
65  virtual WModelIndex parent(const WModelIndex& index) const;
66 
71  virtual int columnCount(const WModelIndex& parent = WModelIndex())
72  const;
73 
79  virtual int rowCount(const WModelIndex& parent = WModelIndex()) const;
80 
87  virtual WModelIndex
88  index(int row, int column, const WModelIndex& parent = WModelIndex())
89  const;
90 
95  virtual cpp17::any
96  data(const WModelIndex& index, ItemDataRole role = ItemDataRole::Display) const;
97 
100  virtual cpp17::any
101  headerData(int section, Orientation orientation = Orientation::Horizontal,
102  ItemDataRole role = ItemDataRole::Display) const;
103 
105 
106 private:
109 
114  struct ChildIndex {
115  int parentId;
116  int index;
117 
118  ChildIndex(int aParent, int anIndex)
119  : parentId(aParent), index(anIndex) { }
120 
121  bool operator< (const ChildIndex& other) const {
122  if (parentId < other.parentId)
123  return true;
124  else if (parentId > other.parentId)
125  return false;
126  else return index < other.index;
127  }
128  };
129 
133  class Tree {
134  public:
137  Tree(int parentId, int index, const Git::ObjectId& object,
138  int rowCount)
139  : index_(parentId, index),
140  treeObject_(object),
141  rowCount_(rowCount)
142  { }
143 
148  int parentId() const { return index_.parentId; }
149 
154  int index() const { return index_.index; }
155 
158  const Git::ObjectId& treeObject() const { return treeObject_; }
159 
162  int rowCount() const { return rowCount_; }
163 
164  private:
168  };
169 
170  typedef std::map<ChildIndex, int> ChildPointerMap;
171 
184  mutable std::vector<Tree> treeData_;
185 
195 
202  int getTreeId(int parentId, int childIndex) const;
203 
206  Git::Object getObject(const WModelIndex& index) const;
207 };
208 
211 #endif // GIT_MODEL_H_
Used to uniquely locate a folder within the folder hierarchy.
Definition: GitModel.h:133
Tree(int parentId, int index, const Git::ObjectId &object, int rowCount)
Constructor.
Definition: GitModel.h:137
int parentId() const
Returns the parent id.
Definition: GitModel.h:148
int index() const
Returns the child index within the parent folder.
Definition: GitModel.h:154
int rowCount() const
Returns the (cached) row count.
Definition: GitModel.h:162
ChildIndex index_
Definition: GitModel.h:165
const Git::ObjectId & treeObject() const
Returns the SHA1 id for the git tree object.
Definition: GitModel.h:158
Git::ObjectId treeObject_
Definition: GitModel.h:166
A model that retrieves revision trees from a git repository.
Definition: GitModel.h:39
ChildPointerMap childPointer_
Maps child indexes to tree indexes.
Definition: GitModel.h:194
std::vector< Tree > treeData_
List of folder objects.
Definition: GitModel.h:184
std::map< ChildIndex, int > ChildPointerMap
Definition: GitModel.h:170
static const ItemDataRole ContentsRole
The role which may be used on a file to retrieve its contents.
Definition: GitModel.h:43
static const ItemDataRole FilePathRole
Definition: GitModel.h:44
Git git_
The git repository.
Definition: GitModel.h:108
Git object Id.
Definition: Git.h:39
Git utility class for browsing git archives.
Definition: Git.h:24
static constexpr const int Display
virtual cpp17::any data(const WModelIndex &index, ItemDataRole role=ItemDataRole::Display) const=0
Orientation
Index usable as a key to a map, that identifies a child/row within a tree.
Definition: GitModel.h:114
ChildIndex(int aParent, int anIndex)
Definition: GitModel.h:118
Git object.
Definition: Git.h:63