Wt examples  3.3.0
/home/koen/project/wt/public-git/wt/examples/gitmodel/GitView.C
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2008 Emweb bvba, Heverlee, Belgium.
00003  *
00004  * See the LICENSE file for terms of use.
00005  */
00006 
00007 #include <iostream>
00008 #include <stdlib.h>
00009 
00010 #include <Wt/WApplication>
00011 #include <Wt/WContainerWidget>
00012 #include <Wt/WEnvironment>
00013 #include <Wt/WLineEdit>
00014 #include <Wt/WGridLayout>
00015 #include <Wt/WHBoxLayout>
00016 #include <Wt/WPushButton>
00017 #include <Wt/WTable>
00018 #include <Wt/WText>
00019 #include <Wt/WTreeView>
00020 #include <Wt/WVBoxLayout>
00021 #include <Wt/WViewWidget>
00022 
00023 #include "GitModel.h"
00024 #include "../wt-homepage/SourceView.h"
00025 
00026 using namespace Wt;
00027 
00032 
00039 class GitViewApplication : public WApplication
00040 {
00041 public:
00044   GitViewApplication(const WEnvironment& env) 
00045     : WApplication(env)
00046   {
00047     useStyleSheet("gitview.css");
00048     setTitle("Git model example");
00049 
00050     const char *gitRepo = getenv("GITVIEW_REPOSITORY_PATH");
00051 
00052     WGridLayout *grid = new WGridLayout();
00053     grid->addWidget(new WText("Git repository path:"), 0, 0);
00054     grid->addWidget(repositoryEdit_ = new WLineEdit(gitRepo ? gitRepo : "")
00055                     , 0, 1, AlignLeft);
00056     grid->addWidget(repositoryError_ = new WText(), 0, 2);
00057     grid->addWidget(new WText("Revision:"), 1, 0);
00058     grid->addWidget(revisionEdit_ = new WLineEdit("master"), 1, 1, AlignLeft);
00059     grid->addWidget(revisionError_ = new WText(), 1, 2);
00060 
00061     repositoryEdit_->setTextSize(30);
00062     revisionEdit_->setTextSize(20);
00063     repositoryError_->setStyleClass("error-msg");
00064     revisionError_->setStyleClass("error-msg");
00065 
00066     repositoryEdit_->enterPressed()
00067       .connect(this, &GitViewApplication::loadGitModel);
00068     revisionEdit_->enterPressed()
00069       .connect(this, &GitViewApplication::loadGitModel);
00070 
00071     WPushButton *b = new WPushButton("Load");
00072     b->clicked().connect(this, &GitViewApplication::loadGitModel);
00073     grid->addWidget(b, 2, 0, AlignLeft);
00074 
00075     gitView_ = new WTreeView();
00076     gitView_->resize(300, WLength::Auto);
00077     gitView_->setSortingEnabled(false);
00078     gitView_->setModel(gitModel_ = new GitModel(this));
00079     gitView_->setSelectionMode(SingleSelection);
00080     gitView_->selectionChanged().connect(this, &GitViewApplication::showFile);
00081 
00082     sourceView_ = new SourceView(DisplayRole, 
00083                                  GitModel::ContentsRole, 
00084                                  GitModel::FilePathRole);
00085     sourceView_->setStyleClass("source-view");
00086 
00087     if (environment().javaScript()) {
00088       /*
00089        * We have JavaScript: We can use layout managers so everything will
00090        * always fit nicely in the window.
00091        */
00092       WVBoxLayout *topLayout = new WVBoxLayout();
00093       topLayout->addLayout(grid, 0);
00094 
00095       WHBoxLayout *gitLayout = new WHBoxLayout();
00096       gitLayout->addWidget(gitView_, 0);
00097       gitLayout->addWidget(sourceView_, 1);
00098       topLayout->addLayout(gitLayout, 1);
00099 
00100       root()->setLayout(topLayout);
00101       root()->setStyleClass("maindiv");
00102     } else {
00103       /*
00104        * No JavaScript: let's make the best of the situation using regular
00105        * CSS-based layout
00106        */
00107       root()->setStyleClass("maindiv");
00108       WContainerWidget *top = new WContainerWidget();
00109       top->setLayout(grid);
00110       root()->addWidget(top);
00111       root()->addWidget(gitView_);
00112       gitView_->setFloatSide(Left);
00113       gitView_->setMargin(6);
00114       root()->addWidget(sourceView_);
00115       sourceView_->setMargin(6);
00116     }
00117   }
00118 
00119 private:
00120   WLineEdit  *repositoryEdit_, *revisionEdit_;
00121   WText      *repositoryError_, *revisionError_;
00122   GitModel   *gitModel_;
00123   WTreeView  *gitView_;
00124   SourceView *sourceView_;
00125 
00128   void loadGitModel() {
00129     sourceView_->setIndex(WModelIndex());
00130     repositoryError_->setText("");
00131     revisionError_->setText("");
00132     try {
00133       gitModel_->setRepositoryPath(repositoryEdit_->text().toUTF8());
00134       try {
00135         gitModel_->loadRevision(revisionEdit_->text().toUTF8());
00136       } catch (const Git::Exception& e) {
00137         revisionError_->setText(e.what());
00138       }
00139     } catch (const Git::Exception& e) {
00140       repositoryError_->setText(e.what());
00141     }
00142   }
00143 
00146   void showFile() {
00147     if (gitView_->selectedIndexes().empty())
00148       return;
00149 
00150     WModelIndex selected = *gitView_->selectedIndexes().begin();
00151     sourceView_->setIndex(selected);
00152   }
00153 };
00154 
00155 WApplication *createApplication(const WEnvironment& env)
00156 {
00157   return new GitViewApplication(env);
00158 }
00159 
00160 int main(int argc, char **argv)
00161 {
00162   return WRun(argc, argv, &createApplication);
00163 }
00164 

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