Wt examples  3.2.1
Classes | Public Member Functions | Static Public Attributes | Private Types | Private Member Functions | Private Attributes
GitModel Class Reference

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

#include <GitModel.h>

Inheritance diagram for GitModel:
Inheritance graph
[legend]

List of all members.

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.
- Public Member Functions inherited from Wt::WAbstractItemModel
 WAbstractItemModel (WObject *parent=0)
virtual WFlags< ItemFlagflags (const WModelIndex &index) const
virtual WFlags< HeaderFlagheaderFlags (int section, Orientation orientation=Horizontal) const
virtual bool hasChildren (const WModelIndex &index) const
virtual DataMap itemData (const WModelIndex &index) const
virtual WModelIndexList match (const WModelIndex &start, int role, const boost::any &value, int hits=-1, WFlags< MatchFlag > flags=WFlags< MatchFlag >(MatchStartsWith|MatchWrap)) const
boost::any data (int row, int column, int role=DisplayRole, 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 boost::any &value, int role=EditRole)
virtual bool setItemData (const WModelIndex &index, const DataMap &values)
virtual bool setHeaderData (int section, Orientation orientation, const boost::any &value, int role=EditRole)
bool setHeaderData (int section, const boost::any &value)
virtual void sort (int column, SortOrder order=AscendingOrder)
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 boost::any &value, int role=EditRole, 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
 WObject (WObject *parent=0)
virtual ~WObject ()
virtual const std::string id () const
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)())
WStatelessSlot * implementJavaScript (void(T::*method)(), const std::string &jsCode)
void addChild (WObject *child)
virtual void removeChild (WObject *child)
const std::vector< WObject * > & children () const
WObjectparent () const

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< TreetreeData_
 List of folder objects.
ChildPointerMap childPointer_
 Maps child indexes to tree indexes.

Additional Inherited Members

- Public Types inherited from Wt::WAbstractItemModel
typedef std::map< int, boost::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

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

Definition at line 168 of file GitModel.h.


Constructor & Destructor Documentation

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

Constructor.

Definition at line 11 of file GitModel.C.

{ }

Member Function Documentation

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 135 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 static_cast<const char*>("icons/git-blob.png");
else if (object.type == Git::Tree)
return static_cast<const char*>("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 177 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, git_.treeSize(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 168 of file GitModel.C.

{
if (orientation == Horizontal && role == DisplayRole)
return static_cast<const char*>("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.
treeData_.clear();
childPointer_.clear();
// Store the tree root as treeData_[0]
treeData_.push_back(Tree(-1, -1, treeRoot, git_.treeSize(treeRoot)));
}
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;
int treeId;
if (index.isValid()) {
// only column 0 items may contain children
if (index.column() != 0)
return 0;
if (o.type == Git::Tree) {
objectId = o.id;
} else
// not a folder: no children
return 0;
} else {
treeId = 0;
// the index corresponds to the root object
if (treeData_.empty())
// model not yet loaded !
return 0;
else
objectId = treeData_[0].treeObject();
}
return treeData_[treeId].rowCount();
}
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");
}

Member Data Documentation

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.

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_
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 Wed Jun 13 2012 for the C++ Web Toolkit (Wt) by doxygen 1.8.1