Wt examples  3.3.0
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.

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.

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.

  : WAbstractItemModel(parent)
{ }

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.
  layoutAboutToBeChanged().emit();

  treeData_.clear();
  childPointer_.clear();

  // Store the tree root as treeData_[0]
  treeData_.push_back(Tree(-1, -1, treeRoot, git_.treeSize(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;
  int treeId;

  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;
      treeId = getTreeId(index.internalId(), index.row());
    } 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

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_ [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 182 of file GitModel.h.


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

Generated on Mon Apr 8 2013 for the C++ Web Toolkit (Wt) by doxygen 1.7.5.1