A model that retrieves revision trees from a git repository. More...
#include <GitModel.h>
Inherits Wt::WAbstractItemModel.
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. | |
void | setRepositoryPath (const std::string &repositoryPath) |
Set the repository and load its 'master' revision. | |
void | loadRevision (const std::string &revName) |
Load a particular revision. | |
virtual Wt::WModelIndex | parent (const Wt::WModelIndex &index) const |
Returns the parent index. | |
virtual int | columnCount (const Wt::WModelIndex &parent=Wt::WModelIndex()) const |
Returns the column count. | |
virtual int | rowCount (const Wt::WModelIndex &parent=Wt::WModelIndex()) const |
Returns the row count. | |
virtual Wt::WModelIndex | index (int row, int column, const Wt::WModelIndex &parent=Wt::WModelIndex()) const |
Returns a child index. | |
virtual boost::any | data (const Wt::WModelIndex &index, int role=Wt::DisplayRole) const |
Returns data. | |
virtual boost::any | headerData (int section, Wt::Orientation orientation=Wt::Horizontal, int role=Wt::DisplayRole) const |
Returns header data. | |
Static Public Attributes | |
static const int | ContentsRole = Wt::UserRole |
The role which may be used on a file to retrieve its contents. | |
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. | |
Git::Object | getObject (const Wt::WModelIndex &index) const |
Get the Git::Object that corresponds to an index. | |
Private Attributes | |
Git | git_ |
The git repository. | |
std::vector< Tree > | treeData_ |
List of folder objects. | |
ChildPointerMap | childPointer_ |
Maps child indexes to tree indexes. |
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.
typedef std::map<ChildIndex, int> GitModel::ChildPointerMap [private] |
Definition at line 161 of file GitModel.h.
GitModel::GitModel | ( | Wt::WObject * | parent = 0 |
) |
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.
{ // currently only one column return 1; }
boost::any GitModel::data | ( | const Wt::WModelIndex & | index, | |
int | role = Wt::DisplayRole | |||
) | const [virtual] |
Returns data.
Returns only data corresponding to DisplayRole and ContentsRole.
Implements Wt::WAbstractItemModel.
Definition at line 131 of file GitModel.C.
{ if (!index.isValid()) return boost::any(); /* Only 3 data roles on column 0 data are supported: * - DisplayRole: the file name * - DecorationRole: an icon (folder or file) * - ContentsRole: the file contents */ if (index.column() == 0) { Git::Object object = getObject(index); if (role == DisplayRole) { if (object.type == Git::Tree) return object.name + '/'; else return object.name; } else if (role == DecorationRole) { if (object.type == Git::Blob) return "icons/git-blob.png"; else if (object.type == Git::Tree) return "icons/git-tree.png"; } else if (role == ContentsRole) { if (object.type == Git::Blob) return git_.catFile(object.id); } else if (role == FilePathRole) { return boost::any(); } } return boost::any(); }
Git::Object GitModel::getObject | ( | const Wt::WModelIndex & | index | ) | const [private] |
Get the Git::Object that corresponds to an index.
Definition at line 173 of file GitModel.C.
{ int parentId = index.internalId(); const Tree& parentItem = treeData_[parentId]; return git_.treeGetObject(parentItem.treeObject(), index.row()); }
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.
{ ChildIndex index(parentId, childIndex); ChildPointerMap::const_iterator i = childPointer_.find(index); if (i == childPointer_.end()) { // no tree object was already allocated, so do that now. // lookup the git SHA1 object Id (within the parent) const Tree& parentItem = treeData_[parentId]; Git::Object o = git_.treeGetObject(parentItem.treeObject(), childIndex); // and add to treeData_ and childPointer_ data structures treeData_.push_back(Tree(parentId, childIndex, o.id)); int result = treeData_.size() - 1; childPointer_[index] = result; return result; } else return i->second; }
boost::any GitModel::headerData | ( | int | section, | |
Wt::Orientation | orientation = Wt::Horizontal , |
|||
int | role = Wt::DisplayRole | |||
) | const [virtual] |
Returns header data.
Reimplemented from Wt::WAbstractItemModel.
Definition at line 164 of file GitModel.C.
{ if (orientation == Horizontal && role == DisplayRole) return "File"; else return boost::any(); }
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.
{ int parentId; // the top-level parent has id=0. if (!parent.isValid()) parentId = 0; else { // the internal id of the parent identifies the grand parent int grandParentId = parent.internalId(); // lookup the parent id for the parent himself, based on grand parent // and child-index (=row) within the grand parent parentId = getTreeId(grandParentId, parent.row()); } return createIndex(row, column, parentId); }
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.
{ Git::ObjectId treeRoot = git_.getCommitTree(revName); // You need to call this method before invalidating all existing // model indexes. Anyone listening for this event could temporarily // convert some model indexes to a raw index pointer, but this model // does not reimplement these methods. layoutAboutToBeChanged().emit(); treeData_.clear(); childPointer_.clear(); // Store the tree root as treeData_[0] treeData_.push_back(Tree(-1, -1, treeRoot)); layoutChanged().emit(); }
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.
{ // treeData_[0] indicates the top-level parent. if (!index.isValid() || index.internalId() == 0) return WModelIndex(); else { // get the item that corresponds to the parent ... const Tree& item = treeData_[index.internalId()]; // ... and construct that identifies the parent: // row = child index in the grand parent // internalId = id of the grand parent return createIndex(item.index(), 0, item.parentId()); } }
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.
{ // we are looking for the git SHA1 id of a tree object (since only folders // may contain children). Git::ObjectId objectId; if (index.isValid()) { // only column 0 items may contain children if (index.column() != 0) return 0; Git::Object o = getObject(index); if (o.type == Git::Tree) { objectId = o.id; } else // not a folder: no children return 0; } else // the index corresponds to the root object if (treeData_.empty()) // model not yet loaded ! return 0; else objectId = treeData_[0].treeObject(); return git_.treeSize(objectId); }
void GitModel::setRepositoryPath | ( | const std::string & | repositoryPath | ) |
Set the repository and load its 'master' revision.
Definition at line 15 of file GitModel.C.
{ git_.setRepositoryPath(gitRepositoryPath); loadRevision("master"); }
ChildPointerMap GitModel::childPointer_ [mutable, private] |
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 185 of file GitModel.h.
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.
const int GitModel::FilePathRole = Wt::UserRole + 1 [static] |
Definition at line 42 of file GitModel.h.
Git GitModel::git_ [private] |
The git repository.
Definition at line 106 of file GitModel.h.
std::vector<Tree> GitModel::treeData_ [mutable, private] |
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 175 of file GitModel.h.