Wt examples 4.5.2
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
14using namespace Wt;
15
20
38class GitModel : public Wt::WAbstractItemModel
39{
40public:
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
104 using WAbstractItemModel::data;
105
106private:
109
114 struct ChildIndex {
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)
140 treeObject_(object),
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
const Git::ObjectId & treeObject() const
Returns the SHA1 id for the git tree object.
Definition: GitModel.h:158
ChildIndex index_
Definition: GitModel.h:165
Git::ObjectId treeObject_
Definition: GitModel.h:166
A model that retrieves revision trees from a git repository.
Definition: GitModel.h:39
virtual WModelIndex parent(const WModelIndex &index) const
Returns the parent index.
Definition: GitModel.C:41
virtual WModelIndex index(int row, int column, const WModelIndex &parent=WModelIndex()) const
Returns a child index.
Definition: GitModel.C:57
ChildPointerMap childPointer_
Maps child indexes to tree indexes.
Definition: GitModel.h:194
int getTreeId(int parentId, int childIndex) const
Get or allocate an id for a folder.
Definition: GitModel.C:77
std::vector< Tree > treeData_
List of folder objects.
Definition: GitModel.h:184
std::map< ChildIndex, int > ChildPointerMap
Definition: GitModel.h:170
Git::Object getObject(const WModelIndex &index) const
Get the Git::Object that corresponds to an index.
Definition: GitModel.C:178
virtual cpp17::any headerData(int section, Orientation orientation=Orientation::Horizontal, ItemDataRole role=ItemDataRole::Display) const
Returns header data.
Definition: GitModel.C:169
virtual int columnCount(const WModelIndex &parent=WModelIndex()) const
Returns the column count.
Definition: GitModel.C:98
virtual cpp17::any data(const WModelIndex &index, ItemDataRole role=ItemDataRole::Display) const
Returns data.
Definition: GitModel.C:136
GitModel()
Constructor.
Definition: GitModel.C:12
static const ItemDataRole ContentsRole
The role which may be used on a file to retrieve its contents.
Definition: GitModel.h:43
void setRepositoryPath(const std::string &repositoryPath)
Set the repository and load its 'master' revision.
Definition: GitModel.C:16
static const ItemDataRole FilePathRole
Definition: GitModel.h:44
void loadRevision(const std::string &revName)
Load a particular revision.
Definition: GitModel.C:22
Git git_
The git repository.
Definition: GitModel.h:108
virtual int rowCount(const WModelIndex &parent=WModelIndex()) const
Returns the row count.
Definition: GitModel.C:104
Git object Id.
Definition: Git.h:39
Git utility class for browsing git archives.
Definition: Git.h:24
Index usable as a key to a map, that identifies a child/row within a tree.
Definition: GitModel.h:114
bool operator<(const ChildIndex &other) const
Definition: GitModel.h:121
ChildIndex(int aParent, int anIndex)
Definition: GitModel.h:118
Git object.
Definition: Git.h:63

Generated on Wed Jan 12 2022 for the C++ Web Toolkit (Wt) by doxygen 1.9.3