00001
00002
00003
00004
00005
00006
00007 #include <iostream>
00008
00009 #include <Wt/WApplication>
00010 #include <Wt/WContainerWidget>
00011 #include <Wt/WEnvironment>
00012 #include <Wt/WLineEdit>
00013 #include <Wt/WGridLayout>
00014 #include <Wt/WHBoxLayout>
00015 #include <Wt/WPushButton>
00016 #include <Wt/WTable>
00017 #include <Wt/WText>
00018 #include <Wt/WTreeView>
00019 #include <Wt/WVBoxLayout>
00020 #include <Wt/WViewWidget>
00021
00022 #include "GitModel.h"
00023
00024 using namespace Wt;
00025
00030
00037 class SourceView : public WViewWidget
00038 {
00039 public:
00045 SourceView(int role)
00046 : role_(role)
00047 { }
00048
00053 void setIndex(const WModelIndex& index) {
00054 if (index != index_
00055 && (!index.isValid() || !index.data(role_).empty())) {
00056 index_ = index;
00057 update();
00058 }
00059 }
00060
00066 virtual WWidget *renderView() {
00067 WText *result = new WText();
00068 result->setInline(false);
00069
00070 if (!index_.isValid())
00071 return result;
00072
00073 boost::any d = index_.data(role_);
00074 const std::string& t = boost::any_cast<const std::string&>(d);
00075
00076 result->setTextFormat(PlainText);
00077 result->setText(t);
00078
00079 return result;
00080 }
00081
00082 private:
00084 WModelIndex index_;
00085
00087 int role_;
00088 };
00089
00096 class GitViewApplication : public WApplication
00097 {
00098 public:
00101 GitViewApplication(const WEnvironment& env)
00102 : WApplication(env)
00103 {
00104 useStyleSheet("gitview.css");
00105 setTitle("Git model example");
00106
00107 const char *gitRepo = getenv("GITVIEW_REPOSITORY_PATH");
00108
00109 WGridLayout *grid = new WGridLayout();
00110 grid->addWidget(new WText("Git repository path:"), 0, 0);
00111 grid->addWidget(repositoryEdit_ = new WLineEdit(gitRepo ? gitRepo : "")
00112 , 0, 1, AlignLeft);
00113 grid->addWidget(repositoryError_ = new WText(), 0, 2);
00114 grid->addWidget(new WText("Revision:"), 1, 0);
00115 grid->addWidget(revisionEdit_ = new WLineEdit("master"), 1, 1, AlignLeft);
00116 grid->addWidget(revisionError_ = new WText(), 1, 2);
00117
00118 repositoryEdit_->setTextSize(30);
00119 revisionEdit_->setTextSize(20);
00120 repositoryError_->setStyleClass("error-msg");
00121 revisionError_->setStyleClass("error-msg");
00122
00123 repositoryEdit_->enterPressed()
00124 .connect(SLOT(this, GitViewApplication::loadGitModel));
00125 revisionEdit_->enterPressed()
00126 .connect(SLOT(this, GitViewApplication::loadGitModel));
00127
00128 WPushButton *b = new WPushButton("Load");
00129 b->clicked().connect(SLOT(this, GitViewApplication::loadGitModel));
00130 grid->addWidget(b, 2, 0, AlignLeft);
00131
00132 gitView_ = new WTreeView();
00133 gitView_->resize(300, WLength::Auto);
00134 gitView_->setSortingEnabled(false);
00135 gitView_->setModel(gitModel_ = new GitModel(this));
00136 gitView_->setSelectionMode(SingleSelection);
00137 gitView_->selectionChanged().connect
00138 (SLOT(this, GitViewApplication::showFile));
00139
00140 sourceView_ = new SourceView(GitModel::ContentsRole);
00141 sourceView_->setStyleClass("source-view");
00142
00143 if (environment().javaScript()) {
00144
00145
00146
00147
00148 WVBoxLayout *topLayout = new WVBoxLayout();
00149 topLayout->addLayout(grid, 0, AlignTop | AlignLeft);
00150
00151 WHBoxLayout *gitLayout = new WHBoxLayout();
00152 gitLayout->setLayoutHint("table-layout", "fixed");
00153 gitLayout->addWidget(gitView_, 0);
00154 gitLayout->addWidget(sourceView_, 1);
00155 topLayout->addLayout(gitLayout, 1);
00156
00157 root()->setLayout(topLayout);
00158 root()->setStyleClass("maindiv");
00159 } else {
00160
00161
00162
00163
00164 root()->setStyleClass("maindiv");
00165 WContainerWidget *top = new WContainerWidget();
00166 top->setLayout(grid, AlignTop | AlignLeft);
00167 root()->addWidget(top);
00168 root()->addWidget(gitView_);
00169 gitView_->setFloatSide(Left);
00170 gitView_->setMargin(6);
00171 root()->addWidget(sourceView_);
00172 sourceView_->setMargin(6);
00173 }
00174 }
00175
00176 private:
00177 WLineEdit *repositoryEdit_, *revisionEdit_;
00178 WText *repositoryError_, *revisionError_;
00179 GitModel *gitModel_;
00180 WTreeView *gitView_;
00181 SourceView *sourceView_;
00182
00185 void loadGitModel() {
00186 sourceView_->setIndex(WModelIndex());
00187 repositoryError_->setText("");
00188 revisionError_->setText("");
00189 try {
00190 gitModel_->setRepositoryPath(repositoryEdit_->text().toUTF8());
00191 try {
00192 gitModel_->loadRevision(revisionEdit_->text().toUTF8());
00193 } catch (const Git::Exception& e) {
00194 revisionError_->setText(e.what());
00195 }
00196 } catch (const Git::Exception& e) {
00197 repositoryError_->setText(e.what());
00198 }
00199 }
00200
00203 void showFile() {
00204 if (gitView_->selectedIndexes().empty())
00205 return;
00206
00207 WModelIndex selected = *gitView_->selectedIndexes().begin();
00208 sourceView_->setIndex(selected);
00209 }
00210 };
00211
00212 WApplication *createApplication(const WEnvironment& env)
00213 {
00214 return new GitViewApplication(env);
00215 }
00216
00217 int main(int argc, char **argv)
00218 {
00219 return WRun(argc, argv, &createApplication);
00220 }
00221