Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007 #include "GitModel.h"
00008
00009 using namespace Wt;
00010
00011 GitModel::GitModel(WObject *parent)
00012 : WAbstractItemModel(parent)
00013 { }
00014
00015 void GitModel::setRepositoryPath(const std::string& gitRepositoryPath)
00016 {
00017 git_.setRepositoryPath(gitRepositoryPath);
00018 loadRevision("master");
00019 }
00020
00021 void GitModel::loadRevision(const std::string& revName)
00022 {
00023 Git::ObjectId treeRoot = git_.getCommitTree(revName);
00024
00025
00026
00027
00028
00029 layoutAboutToBeChanged().emit();
00030
00031 treeData_.clear();
00032 childPointer_.clear();
00033
00034
00035 treeData_.push_back(Tree(-1, -1, treeRoot));
00036
00037 layoutChanged().emit();
00038 }
00039
00040 WModelIndex GitModel::parent(const WModelIndex& index) const
00041 {
00042
00043 if (!index.isValid() || index.internalId() == 0)
00044 return WModelIndex();
00045 else {
00046
00047 const Tree& item = treeData_[index.internalId()];
00048
00049
00050
00051
00052 return createIndex(item.index(), 0, item.parentId());
00053 }
00054 }
00055
00056 WModelIndex GitModel::index(int row, int column,
00057 const WModelIndex& parent) const
00058 {
00059 int parentId;
00060
00061
00062 if (!parent.isValid())
00063 parentId = 0;
00064 else {
00065
00066 int grandParentId = parent.internalId();
00067
00068
00069
00070 parentId = getTreeId(grandParentId, parent.row());
00071 }
00072
00073 return createIndex(row, column, parentId);
00074 }
00075
00076 int GitModel::getTreeId(int parentId, int childIndex) const
00077 {
00078 ChildIndex index(parentId, childIndex);
00079
00080 ChildPointerMap::const_iterator i = childPointer_.find(index);
00081 if (i == childPointer_.end()) {
00082
00083
00084
00085 const Tree& parentItem = treeData_[parentId];
00086 Git::Object o = git_.treeGetObject(parentItem.treeObject(), childIndex);
00087
00088
00089 treeData_.push_back(Tree(parentId, childIndex, o.id));
00090 int result = treeData_.size() - 1;
00091 childPointer_[index] = result;
00092 return result;
00093 } else
00094 return i->second;
00095 }
00096
00097 int GitModel::columnCount(const WModelIndex& index) const
00098 {
00099
00100 return 1;
00101 }
00102
00103 int GitModel::rowCount(const WModelIndex& index) const
00104 {
00105
00106
00107 Git::ObjectId objectId;
00108
00109 if (index.isValid()) {
00110
00111 if (index.column() != 0)
00112 return 0;
00113
00114 Git::Object o = getObject(index);
00115 if (o.type == Git::Tree) {
00116 objectId = o.id;
00117 } else
00118
00119 return 0;
00120 } else
00121
00122 if (treeData_.empty())
00123
00124 return 0;
00125 else
00126 objectId = treeData_[0].treeObject();
00127
00128 return git_.treeSize(objectId);
00129 }
00130
00131 boost::any GitModel::data(const WModelIndex& index, int role) const
00132 {
00133 if (!index.isValid())
00134 return boost::any();
00135
00136
00137
00138
00139
00140
00141 if (index.column() == 0) {
00142 Git::Object object = getObject(index);
00143 if (role == DisplayRole) {
00144 if (object.type == Git::Tree)
00145 return object.name + '/';
00146 else
00147 return object.name;
00148 } else if (role == DecorationRole) {
00149 if (object.type == Git::Blob)
00150 return "icons/git-blob.png";
00151 else if (object.type == Git::Tree)
00152 return "icons/git-tree.png";
00153 } else if (role == ContentsRole) {
00154 if (object.type == Git::Blob)
00155 return git_.catFile(object.id);
00156 } else if (role == FilePathRole) {
00157 return boost::any();
00158 }
00159 }
00160
00161 return boost::any();
00162 }
00163
00164 boost::any GitModel::headerData(int section, Orientation orientation,
00165 int role) const
00166 {
00167 if (orientation == Horizontal && role == DisplayRole)
00168 return "File";
00169 else
00170 return boost::any();
00171 }
00172
00173 Git::Object GitModel::getObject(const WModelIndex& index) const
00174 {
00175 int parentId = index.internalId();
00176 const Tree& parentItem = treeData_[parentId];
00177 return git_.treeGetObject(parentItem.treeObject(), index.row());
00178 }