Wt examples  3.7.1
Classes | Public Member Functions | Static Public Attributes | Private Types | Private Member Functions | Private Attributes | List of all members
GitModel Class Reference

A model that retrieves revision trees from a git repository. More...

#include <GitModel.h>

Inheritance diagram for GitModel:
Inheritance graph
[legend]

Classes

class  ChildIndex
 Index usable as a key to a map, that identifies a child/row within a tree. More...
 
class  Tree
 Used to uniquely locate a folder within the folder hierarchy. More...
 

Public Member Functions

 GitModel (Wt::WObject *parent=0)
 Constructor. More...
 
void setRepositoryPath (const std::string &repositoryPath)
 Set the repository and load its 'master' revision. More...
 
void loadRevision (const std::string &revName)
 Load a particular revision. More...
 
virtual Wt::WModelIndex parent (const Wt::WModelIndex &index) const
 Returns the parent index. More...
 
virtual int columnCount (const Wt::WModelIndex &parent=Wt::WModelIndex()) const
 Returns the column count. More...
 
virtual int rowCount (const Wt::WModelIndex &parent=Wt::WModelIndex()) const
 Returns the row count. More...
 
virtual Wt::WModelIndex index (int row, int column, const Wt::WModelIndex &parent=Wt::WModelIndex()) const
 Returns a child index. More...
 
virtual boost::any data (const Wt::WModelIndex &index, int role=Wt::DisplayRole) const
 Returns data. More...
 
virtual boost::any headerData (int section, Wt::Orientation orientation=Wt::Horizontal, int role=Wt::DisplayRole) const
 Returns header data. More...
 
- Public Member Functions inherited from Wt::WAbstractItemModel
 WAbstractItemModel ()
 
virtual WFlags< ItemFlagflags (const WModelIndex &index) const
 
virtual WFlags< HeaderFlagheaderFlags (int section, Orientation orientation=Orientation::Horizontal) const
 
virtual bool hasChildren (const WModelIndex &index) const
 
virtual cpp17::any data (const WModelIndex &index, ItemDataRole role=ItemDataRole::Display) const=0
 
virtual DataMap itemData (const WModelIndex &index) const
 
virtual cpp17::any headerData (int section, Orientation orientation=Orientation::Horizontal, ItemDataRole role=ItemDataRole::Display) const
 
virtual WModelIndexList match (const WModelIndex &start, ItemDataRole role, const cpp17::any &value, int hits=-1, WFlags< MatchFlag > flags=WFlags< MatchFlag >(MatchFlag::StartsWith|MatchFlag::Wrap)) const
 
cpp17::any data (int row, int column, ItemDataRole role=ItemDataRole::Display, const WModelIndex &parent=WModelIndex()) const
 
virtual bool hasIndex (int row, int column, const WModelIndex &parent=WModelIndex()) const
 
virtual bool insertColumns (int column, int count, const WModelIndex &parent=WModelIndex())
 
virtual bool insertRows (int row, int count, const WModelIndex &parent=WModelIndex())
 
virtual bool removeColumns (int column, int count, const WModelIndex &parent=WModelIndex())
 
virtual bool removeRows (int row, int count, const WModelIndex &parent=WModelIndex())
 
virtual bool setData (const WModelIndex &index, const cpp17::any &value, ItemDataRole role=ItemDataRole::Edit)
 
virtual bool setItemData (const WModelIndex &index, const DataMap &values)
 
virtual bool setHeaderData (int section, Orientation orientation, const cpp17::any &value, ItemDataRole role=ItemDataRole::Edit)
 
bool setHeaderData (int section, const cpp17::any &value)
 
virtual void sort (int column, SortOrder order=SortOrder::Ascending)
 
virtual void expandColumn (int column)
 
virtual void collapseColumn (int column)
 
virtual void * toRawIndex (const WModelIndex &index) const
 
virtual WModelIndex fromRawIndex (void *rawIndex) const
 
virtual std::string mimeType () const
 
virtual std::vector< std::string > acceptDropMimeTypes () const
 
virtual void dropEvent (const WDropEvent &e, DropAction action, int row, int column, const WModelIndex &parent)
 
bool insertColumn (int column, const WModelIndex &parent=WModelIndex())
 
bool insertRow (int row, const WModelIndex &parent=WModelIndex())
 
bool removeColumn (int column, const WModelIndex &parent=WModelIndex())
 
bool removeRow (int row, const WModelIndex &parent=WModelIndex())
 
bool setData (int row, int column, const cpp17::any &value, ItemDataRole role=ItemDataRole::Edit, const WModelIndex &parent=WModelIndex())
 
virtual Signal< WModelIndex, int, int > & columnsAboutToBeInserted ()
 
virtual Signal< WModelIndex, int, int > & columnsAboutToBeRemoved ()
 
virtual Signal< WModelIndex, int, int > & columnsInserted ()
 
virtual Signal< WModelIndex, int, int > & columnsRemoved ()
 
virtual Signal< WModelIndex, int, int > & rowsAboutToBeInserted ()
 
virtual Signal< WModelIndex, int, int > & rowsAboutToBeRemoved ()
 
virtual Signal< WModelIndex, int, int > & rowsInserted ()
 
virtual Signal< WModelIndex, int, int > & rowsRemoved ()
 
virtual Signal< WModelIndex, WModelIndex > & dataChanged ()
 
virtual Signal< Orientation, int, int > & headerDataChanged ()
 
virtual SignallayoutAboutToBeChanged ()
 
virtual SignallayoutChanged ()
 
virtual SignalmodelReset ()
 
- Public Member Functions inherited from Wt::WObject
void addChild (std::unique_ptr< WObject > child)
 
Child * addChild (std::unique_ptr< Child > child)
 
std::unique_ptr< WObjectremoveChild (WObject *child)
 
std::unique_ptr< Child > removeChild (Child *child)
 
virtual const std::string id () const
 
virtual void setObjectName (const std::string &name)
 
virtual std::string objectName () const
 
void resetLearnedSlots ()
 
void resetLearnedSlot (void(T::*method)())
 
WStatelessSlot * implementStateless (void(T::*method)())
 
WStatelessSlot * implementStateless (void(T::*method)(), void(T::*undoMethod)())
 
void isNotStateless ()
 
WStatelessSlot * implementJavaScript (void(T::*method)(), const std::string &jsCode)
 
- Public Member Functions inherited from Wt::Core::observable
 observable () noexcept
 
virtual ~observable ()
 
auto bindSafe (void(C::*method)(Args...)) noexcept
 
auto bindSafe (void(C::*method)(Args...) const) const noexcept
 
auto bindSafe (const Function &function) noexcept
 

Static Public Attributes

static const int ContentsRole = Wt::UserRole
 The role which may be used on a file to retrieve its contents. More...
 
static const int FilePathRole = Wt::UserRole + 1
 

Private Types

typedef std::map< ChildIndex, int > ChildPointerMap
 

Private Member Functions

int getTreeId (int parentId, int childIndex) const
 Get or allocate an id for a folder. More...
 
Git::Object getObject (const Wt::WModelIndex &index) const
 Get the Git::Object that corresponds to an index. More...
 

Private Attributes

Git git_
 The git repository. More...
 
std::vector< TreetreeData_
 List of folder objects. More...
 
ChildPointerMap childPointer_
 Maps child indexes to tree indexes. More...
 

Additional Inherited Members

- Public Types inherited from Wt::WAbstractItemModel
typedef std::map< ItemDataRole, cpp17::any > DataMap
 
- Public Types inherited from Wt::WObject
typedef void(WObject::* Method) ()
 
- Protected Member Functions inherited from Wt::WAbstractItemModel
void reset ()
 
WModelIndex createIndex (int row, int column, void *ptr) const
 
WModelIndex createIndex (int row, int column, ::uint64_t id) const
 
void beginInsertColumns (const WModelIndex &parent, int first, int last)
 
void beginInsertRows (const WModelIndex &parent, int first, int last)
 
void beginRemoveColumns (const WModelIndex &parent, int first, int last)
 
void beginRemoveRows (const WModelIndex &parent, int first, int last)
 
void endInsertColumns ()
 
void endInsertRows ()
 
void endRemoveColumns ()
 
void endRemoveRows ()
 
- Protected Member Functions inherited from Wt::WObject
virtual WStatelessSlot * getStateless (Method method)
 

Detailed Description

A model that retrieves revision trees from a git repository.

In its present form, it presents only a single column of data: the file names. Additional data could be easily added. Git "tree" objects correspond to folders, and "blob" objects to files.

The model is read-only, does not support sorting (that could be provided by using a WSortFilterProxyModel).

The model loads only minimal information in memory: to create model indexes for folders. These cannot be uniquely identified by their SHA1 id, since two identical folders at different locations would have the same SHA1 id.

The internal id of model indexes created by the model uniquely identify a containing folder for a particular file.

Definition at line 36 of file GitModel.h.

Member Typedef Documentation

◆ ChildPointerMap

typedef std::map<ChildIndex, int> GitModel::ChildPointerMap
private

Definition at line 168 of file GitModel.h.

Constructor & Destructor Documentation

◆ GitModel()

GitModel::GitModel ( Wt::WObject parent = 0)

Constructor.

Definition at line 11 of file GitModel.C.

12  : WAbstractItemModel(parent)
13 { }

Member Function Documentation

◆ columnCount()

int GitModel::columnCount ( const Wt::WModelIndex parent = Wt::WModelIndex()) const
virtual

Returns the column count.

Returns 1.

Implements Wt::WAbstractItemModel.

Definition at line 97 of file GitModel.C.

98 {
99  // currently only one column
100  return 1;
101 }

◆ data()

boost::any GitModel::data ( const Wt::WModelIndex index,
int  role = Wt::DisplayRole 
) const
virtual

Returns data.

Returns only data corresponding to DisplayRole and ContentsRole.

Definition at line 135 of file GitModel.C.

136 {
137  if (!index.isValid())
138  return boost::any();
139 
140  /* Only 3 data roles on column 0 data are supported:
141  * - DisplayRole: the file name
142  * - DecorationRole: an icon (folder or file)
143  * - ContentsRole: the file contents
144  */
145  if (index.column() == 0) {
146  Git::Object object = getObject(index);
147  if (role == DisplayRole) {
148  if (object.type == Git::Tree)
149  return object.name + '/';
150  else
151  return object.name;
152  } else if (role == DecorationRole) {
153  if (object.type == Git::Blob)
154  return static_cast<const char*>("icons/git-blob.png");
155  else if (object.type == Git::Tree)
156  return static_cast<const char*>("icons/git-tree.png");
157  } else if (role == ContentsRole) {
158  if (object.type == Git::Blob)
159  return git_.catFile(object.id);
160  } else if (role == FilePathRole) {
161  return boost::any();
162  }
163  }
164 
165  return boost::any();
166 }
Definition: Git.h:59
bool isValid() const
Git git_
The git repository.
Definition: GitModel.h:106
Definition: Git.h:59
Git object.
Definition: Git.h:63
static const int ContentsRole
The role which may be used on a file to retrieve its contents.
Definition: GitModel.h:41
Git::Object getObject(const Wt::WModelIndex &index) const
Get the Git::Object that corresponds to an index.
Definition: GitModel.C:177
std::string catFile(const ObjectId &id) const
Return the raw contents of a git object.
Definition: Git.C:213
static const int FilePathRole
Definition: GitModel.h:42
int column() const

◆ getObject()

Git::Object GitModel::getObject ( const Wt::WModelIndex index) const
private

Get the Git::Object that corresponds to an index.

Definition at line 177 of file GitModel.C.

178 {
179  int parentId = index.internalId();
180  const Tree& parentItem = treeData_[parentId];
181  return git_.treeGetObject(parentItem.treeObject(), index.row());
182 }
::uint64_t internalId() const
Git git_
The git repository.
Definition: GitModel.h:106
int row() const
Object treeGetObject(const ObjectId &tree, int index) const
Get some info on a tree object.
Definition: Git.C:245
std::vector< Tree > treeData_
List of folder objects.
Definition: GitModel.h:182

◆ getTreeId()

int GitModel::getTreeId ( int  parentId,
int  childIndex 
) const
private

Get or allocate an id for a folder.

The folder is identified by a given childIndex within a parent folder. This method adds data to the treeData_ (and childPointer_) data structures.

Definition at line 76 of file GitModel.C.

77 {
78  ChildIndex index(parentId, childIndex);
79 
80  ChildPointerMap::const_iterator i = childPointer_.find(index);
81  if (i == childPointer_.end()) {
82  // no tree object was already allocated, so do that now.
83 
84  // lookup the git SHA1 object Id (within the parent)
85  const Tree& parentItem = treeData_[parentId];
86  Git::Object o = git_.treeGetObject(parentItem.treeObject(), childIndex);
87 
88  // and add to treeData_ and childPointer_ data structures
89  treeData_.push_back(Tree(parentId, childIndex, o.id, git_.treeSize(o.id)));
90  int result = treeData_.size() - 1;
91  childPointer_[index] = result;
92  return result;
93  } else
94  return i->second;
95 }
Git git_
The git repository.
Definition: GitModel.h:106
Git object.
Definition: Git.h:63
ObjectId id
Definition: Git.h:64
ChildPointerMap childPointer_
Maps child indexes to tree indexes.
Definition: GitModel.h:192
Object treeGetObject(const ObjectId &tree, int index) const
Get some info on a tree object.
Definition: Git.C:245
std::vector< Tree > treeData_
List of folder objects.
Definition: GitModel.h:182
virtual Wt::WModelIndex index(int row, int column, const Wt::WModelIndex &parent=Wt::WModelIndex()) const
Returns a child index.
Definition: GitModel.C:56
int treeSize(const ObjectId &tree) const
Return the number of objects inside a tree object.
Definition: Git.C:279

◆ headerData()

boost::any GitModel::headerData ( int  section,
Wt::Orientation  orientation = Wt::Horizontal,
int  role = Wt::DisplayRole 
) const
virtual

Returns header data.

Definition at line 168 of file GitModel.C.

170 {
171  if (orientation == Horizontal && role == DisplayRole)
172  return static_cast<const char*>("File");
173  else
174  return boost::any();
175 }

◆ index()

WModelIndex GitModel::index ( int  row,
int  column,
const Wt::WModelIndex parent = Wt::WModelIndex() 
) const
virtual

Returns a child index.

Consults the internal data structure to create a child index. If necessary, the internal data structure is expanded by adding an entry for using the parent index as a parent index.

Implements Wt::WAbstractItemModel.

Definition at line 56 of file GitModel.C.

58 {
59  int parentId;
60 
61  // the top-level parent has id=0.
62  if (!parent.isValid())
63  parentId = 0;
64  else {
65  // the internal id of the parent identifies the grand parent
66  int grandParentId = parent.internalId();
67 
68  // lookup the parent id for the parent himself, based on grand parent
69  // and child-index (=row) within the grand parent
70  parentId = getTreeId(grandParentId, parent.row());
71  }
72 
73  return createIndex(row, column, parentId);
74 }
bool isValid() const
WModelIndex createIndex(int row, int column, void *ptr) const
::uint64_t internalId() const
int getTreeId(int parentId, int childIndex) const
Get or allocate an id for a folder.
Definition: GitModel.C:76
int row() const

◆ loadRevision()

void GitModel::loadRevision ( const std::string &  revName)

Load a particular revision.

The revision name may be any revision accepted by git, by git-rev-parse(1).

Definition at line 21 of file GitModel.C.

22 {
23  Git::ObjectId treeRoot = git_.getCommitTree(revName);
24 
25  // You need to call this method before invalidating all existing
26  // model indexes. Anyone listening for this event could temporarily
27  // convert some model indexes to a raw index pointer, but this model
28  // does not reimplement these methods.
30 
31  treeData_.clear();
32  childPointer_.clear();
33 
34  // Store the tree root as treeData_[0]
35  treeData_.push_back(Tree(-1, -1, treeRoot, git_.treeSize(treeRoot)));
36 
37  layoutChanged().emit();
38 }
ObjectId getCommitTree(const std::string &revision) const
Get the tree for a particular revision.
Definition: Git.C:207
Git git_
The git repository.
Definition: GitModel.h:106
virtual Signal & layoutAboutToBeChanged()
Git object Id.
Definition: Git.h:39
void emit(A... args) const
virtual Signal & layoutChanged()
ChildPointerMap childPointer_
Maps child indexes to tree indexes.
Definition: GitModel.h:192
std::vector< Tree > treeData_
List of folder objects.
Definition: GitModel.h:182
int treeSize(const ObjectId &tree) const
Return the number of objects inside a tree object.
Definition: Git.C:279

◆ parent()

WModelIndex GitModel::parent ( const Wt::WModelIndex index) const
virtual

Returns the parent index.

Consults the internal data structure to find the parent index.

Implements Wt::WAbstractItemModel.

Definition at line 40 of file GitModel.C.

41 {
42  // treeData_[0] indicates the top-level parent.
43  if (!index.isValid() || index.internalId() == 0)
44  return WModelIndex();
45  else {
46  // get the item that corresponds to the parent ...
47  const Tree& item = treeData_[index.internalId()];
48 
49  // ... and construct that identifies the parent:
50  // row = child index in the grand parent
51  // internalId = id of the grand parent
52  return createIndex(item.index(), 0, item.parentId());
53  }
54 }
bool isValid() const
WModelIndex createIndex(int row, int column, void *ptr) const
::uint64_t internalId() const
std::vector< Tree > treeData_
List of folder objects.
Definition: GitModel.h:182

◆ rowCount()

int GitModel::rowCount ( const Wt::WModelIndex parent = Wt::WModelIndex()) const
virtual

Returns the row count.

Returns 0 unless the item represents a folder, in which case it returns the number of items in the tree object that corresponds to the folder.

Implements Wt::WAbstractItemModel.

Definition at line 103 of file GitModel.C.

104 {
105  // we are looking for the git SHA1 id of a tree object (since only folders
106  // may contain children).
107  Git::ObjectId objectId;
108  int treeId;
109 
110  if (index.isValid()) {
111  // only column 0 items may contain children
112  if (index.column() != 0)
113  return 0;
114 
116  if (o.type == Git::Tree) {
117  objectId = o.id;
118  treeId = getTreeId(index.internalId(), index.row());
119  } else
120  // not a folder: no children
121  return 0;
122  } else {
123  treeId = 0;
124  // the index corresponds to the root object
125  if (treeData_.empty())
126  // model not yet loaded !
127  return 0;
128  else
129  objectId = treeData_[0].treeObject();
130  }
131 
132  return treeData_[treeId].rowCount();
133 }
Definition: Git.h:59
bool isValid() const
::uint64_t internalId() const
int getTreeId(int parentId, int childIndex) const
Get or allocate an id for a folder.
Definition: GitModel.C:76
Git object Id.
Definition: Git.h:39
Git object.
Definition: Git.h:63
int row() const
ObjectId id
Definition: Git.h:64
Git::Object getObject(const Wt::WModelIndex &index) const
Get the Git::Object that corresponds to an index.
Definition: GitModel.C:177
int column() const
std::vector< Tree > treeData_
List of folder objects.
Definition: GitModel.h:182
virtual Wt::WModelIndex index(int row, int column, const Wt::WModelIndex &parent=Wt::WModelIndex()) const
Returns a child index.
Definition: GitModel.C:56
ObjectType type
Definition: Git.h:65

◆ setRepositoryPath()

void GitModel::setRepositoryPath ( const std::string &  repositoryPath)

Set the repository and load its 'master' revision.

Definition at line 15 of file GitModel.C.

16 {
17  git_.setRepositoryPath(gitRepositoryPath);
18  loadRevision("master");
19 }
Git git_
The git repository.
Definition: GitModel.h:106
void loadRevision(const std::string &revName)
Load a particular revision.
Definition: GitModel.C:21
void setRepositoryPath(const std::string &repository)
Set the git repository path.
Definition: Git.C:201

Member Data Documentation

◆ childPointer_

ChildPointerMap GitModel::childPointer_
mutableprivate

Maps child indexes to tree indexes.

This map provides a way to lookup data in treeData_. It has an entry corresponding to every entry in treeData_: it maps child indexes for folders to indexes in the treeData_ vector.

It is populated on-the-fly, as the user navigates the model.

Definition at line 192 of file GitModel.h.

◆ ContentsRole

const int GitModel::ContentsRole = Wt::UserRole
static

The role which may be used on a file to retrieve its contents.

Definition at line 41 of file GitModel.h.

◆ FilePathRole

const int GitModel::FilePathRole = Wt::UserRole + 1
static

Definition at line 42 of file GitModel.h.

◆ git_

Git GitModel::git_
private

The git repository.

Definition at line 106 of file GitModel.h.

◆ treeData_

std::vector<Tree> GitModel::treeData_
mutableprivate

List of folder objects.

This list contains folders for which a model index has been allocated.

Model indexes have an internal id that are indexes into this vector, identifying the folder that contains a particular file.

Note: only for folders this is needed, since files will never be used as a 'parent' index.

It is populated on-the-fly, as the user navigates the model.

Definition at line 182 of file GitModel.h.


The documentation for this class was generated from the following files:

Generated on Tue Dec 15 2020 for the C++ Web Toolkit (Wt) by doxygen 1.8.13