Wt examples  3.7.1
GitView.C
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008 Emweb bv, Herent, Belgium.
3  *
4  * See the LICENSE file for terms of use.
5  */
6 
7 #include <iostream>
8 #include <stdlib.h>
9 
10 #include <Wt/WApplication>
11 #include <Wt/WContainerWidget>
12 #include <Wt/WEnvironment>
13 #include <Wt/WLineEdit>
14 #include <Wt/WGridLayout>
15 #include <Wt/WHBoxLayout>
16 #include <Wt/WPushButton>
17 #include <Wt/WTable>
18 #include <Wt/WText>
19 #include <Wt/WTreeView>
20 #include <Wt/WVBoxLayout>
21 #include <Wt/WViewWidget>
22 
23 #include "GitModel.h"
24 #include "../wt-homepage/SourceView.h"
25 
26 using namespace Wt;
27 
32 
40 {
41 public:
45  : WApplication(env)
46  {
47  useStyleSheet("gitview.css");
48  setTitle("Git model example");
49 
50  const char *gitRepo = getenv("GITVIEW_REPOSITORY_PATH");
51 
52  WGridLayout *grid = new WGridLayout();
53  grid->addWidget(new WText("Git repository path:"), 0, 0);
54  grid->addWidget(repositoryEdit_ = new WLineEdit(gitRepo ? gitRepo : "")
55  , 0, 1, AlignLeft);
56  grid->addWidget(repositoryError_ = new WText(), 0, 2);
57  grid->addWidget(new WText("Revision:"), 1, 0);
58  grid->addWidget(revisionEdit_ = new WLineEdit("master"), 1, 1, AlignLeft);
59  grid->addWidget(revisionError_ = new WText(), 1, 2);
60 
61  repositoryEdit_->setTextSize(30);
62  revisionEdit_->setTextSize(20);
63  repositoryError_->setStyleClass("error-msg");
64  revisionError_->setStyleClass("error-msg");
65 
66  repositoryEdit_->enterPressed()
67  .connect(this, &GitViewApplication::loadGitModel);
68  revisionEdit_->enterPressed()
69  .connect(this, &GitViewApplication::loadGitModel);
70 
71  WPushButton *b = new WPushButton("Load");
72  b->clicked().connect(this, &GitViewApplication::loadGitModel);
73  grid->addWidget(b, 2, 0, AlignLeft);
74 
75  gitView_ = new WTreeView();
76  gitView_->resize(300, WLength::Auto);
77  gitView_->setSortingEnabled(false);
78  gitView_->setModel(gitModel_ = new GitModel(this));
79  gitView_->setSelectionMode(SingleSelection);
80  gitView_->selectionChanged().connect(this, &GitViewApplication::showFile);
81 
82  sourceView_ = new SourceView(DisplayRole,
85  sourceView_->setStyleClass("source-view");
86 
87  if (environment().javaScript()) {
88  /*
89  * We have JavaScript: We can use layout managers so everything will
90  * always fit nicely in the window.
91  */
92  WVBoxLayout *topLayout = new WVBoxLayout();
93  topLayout->addLayout(grid, 0);
94 
95  WHBoxLayout *gitLayout = new WHBoxLayout();
96  gitLayout->addWidget(gitView_, 0);
97  gitLayout->addWidget(sourceView_, 1);
98  topLayout->addLayout(gitLayout, 1);
99 
100  root()->setLayout(topLayout);
101  root()->setStyleClass("maindiv");
102  } else {
103  /*
104  * No JavaScript: let's make the best of the situation using regular
105  * CSS-based layout
106  */
107  root()->setStyleClass("maindiv");
108  WContainerWidget *top = new WContainerWidget();
109  top->setLayout(grid);
110  root()->addWidget(top);
111  root()->addWidget(gitView_);
112  gitView_->setFloatSide(Left);
113  gitView_->setMargin(6);
114  root()->addWidget(sourceView_);
115  sourceView_->setMargin(6);
116  }
117  }
118 
119 private:
120  WLineEdit *repositoryEdit_, *revisionEdit_;
121  WText *repositoryError_, *revisionError_;
125 
128  void loadGitModel() {
129  sourceView_->setIndex(WModelIndex());
130  repositoryError_->setText("");
131  revisionError_->setText("");
132  try {
133  gitModel_->setRepositoryPath(repositoryEdit_->text().toUTF8());
134  try {
135  gitModel_->loadRevision(revisionEdit_->text().toUTF8());
136  } catch (const Git::Exception& e) {
137  revisionError_->setText(e.what());
138  }
139  } catch (const Git::Exception& e) {
140  repositoryError_->setText(e.what());
141  }
142  }
143 
146  void showFile() {
147  if (gitView_->selectedIndexes().empty())
148  return;
149 
150  WModelIndex selected = *gitView_->selectedIndexes().begin();
151  sourceView_->setIndex(selected);
152  }
153 };
154 
156 {
157  return new GitViewApplication(env);
158 }
159 
160 int main(int argc, char **argv)
161 {
162  return WRun(argc, argv, &createApplication);
163 }
164 
void setLayout(std::unique_ptr< WLayout > layout)
void setRepositoryPath(const std::string &repositoryPath)
Set the repository and load its &#39;master&#39; revision.
Definition: GitModel.C:15
int main(int argc, char **argv)
Definition: GitView.C:160
const WString & text() const
void addWidget(std::unique_ptr< WWidget > widget, int row, int column, WFlags< AlignmentFlag > alignment)
void loadGitModel()
Change repository and/or revision.
Definition: GitView.C:128
EventSignal< WMouseEvent > & clicked()
static WLength Auto
void addLayout(std::unique_ptr< WLayout > layout, int stretch, WFlags< AlignmentFlag > alignment)
void addWidget(std::unique_ptr< WWidget > widget, int stretch, WFlags< AlignmentFlag > alignment)
WApplication * createApplication(const WEnvironment &env)
Definition: GitView.C:155
static const int ContentsRole
The role which may be used on a file to retrieve its contents.
Definition: GitModel.h:41
A simple application to navigate a git repository.
Definition: GitView.C:39
WTreeView * gitView_
Definition: GitView.C:123
bool setText(const WString &text)
void showFile()
Displayed the currently selected file.
Definition: GitView.C:146
GitViewApplication(const WEnvironment &env)
Constructor.
Definition: GitView.C:44
std::string toUTF8() const
WLineEdit * revisionEdit_
Definition: GitView.C:120
GitModel * gitModel_
Definition: GitView.C:122
WText * revisionError_
Definition: GitView.C:121
SourceView * sourceView_
Definition: GitView.C:124
WModelIndexSet selectedIndexes() const
View class for source code.
Definition: SourceView.h:26
bool setIndex(const Wt::WModelIndex &index)
Sets the model index.
Definition: SourceView.C:30
static const int FilePathRole
Definition: GitModel.h:42
A model that retrieves revision trees from a git repository.
Definition: GitModel.h:36
void loadRevision(const std::string &revName)
Load a particular revision.
Definition: GitModel.C:21
Exception class.
Definition: Git.h:28

Generated on Tue Dec 15 2020 for the C++ Web Toolkit (Wt) by doxygen 1.8.13