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());
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 if (!environment().agentWebKit())
00149 sourceView_->resize(WLength(100, WLength::Percentage), WLength());
00150
00151 WVBoxLayout *topLayout = new WVBoxLayout();
00152 topLayout->addLayout(grid, 0, AlignTop | AlignLeft);
00153
00154 WHBoxLayout *gitLayout = new WHBoxLayout();
00155 gitLayout->setLayoutHint("table-layout", "fixed");
00156 gitLayout->addWidget(gitView_, 0);
00157 gitLayout->addWidget(sourceView_, 1);
00158 topLayout->addLayout(gitLayout, 1);
00159
00160 root()->setLayout(topLayout);
00161 root()->setStyleClass("maindiv");
00162 } else {
00163
00164
00165
00166
00167 root()->setStyleClass("maindiv");
00168 WContainerWidget *top = new WContainerWidget();
00169 top->setLayout(grid, AlignTop | AlignLeft);
00170 root()->addWidget(top);
00171 root()->addWidget(gitView_);
00172 gitView_->setFloatSide(Left);
00173 gitView_->setMargin(6);
00174 root()->addWidget(sourceView_);
00175 sourceView_->setMargin(6);
00176 }
00177 }
00178
00179 private:
00180 WLineEdit *repositoryEdit_, *revisionEdit_;
00181 WText *repositoryError_, *revisionError_;
00182 GitModel *gitModel_;
00183 WTreeView *gitView_;
00184 SourceView *sourceView_;
00185
00188 void loadGitModel() {
00189 sourceView_->setIndex(WModelIndex());
00190 repositoryError_->setText("");
00191 revisionError_->setText("");
00192 try {
00193 gitModel_->setRepositoryPath(repositoryEdit_->text().toUTF8());
00194 try {
00195 gitModel_->loadRevision(revisionEdit_->text().toUTF8());
00196 } catch (const Git::Exception& e) {
00197 revisionError_->setText(e.what());
00198 }
00199 } catch (const Git::Exception& e) {
00200 repositoryError_->setText(e.what());
00201 }
00202 }
00203
00206 void showFile() {
00207 if (gitView_->selectedIndexes().empty())
00208 return;
00209
00210 WModelIndex selected = *gitView_->selectedIndexes().begin();
00211 sourceView_->setIndex(selected);
00212 }
00213 };
00214
00215 WApplication *createApplication(const WEnvironment& env)
00216 {
00217 return new GitViewApplication(env);
00218 }
00219
00220 int main(int argc, char **argv)
00221 {
00222 return WRun(argc, argv, &createApplication);
00223 }
00224