Wt examples  3.2.1
GitView.C
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008 Emweb bvba, Heverlee, 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, AlignTop | AlignLeft);
94 
95  WHBoxLayout *gitLayout = new WHBoxLayout();
96  gitLayout->setLayoutHint("table-layout", "fixed");
97  gitLayout->addWidget(gitView_, 0);
98  gitLayout->addWidget(sourceView_, 1);
99  topLayout->addLayout(gitLayout, 1);
100 
101  root()->setLayout(topLayout);
102  root()->setStyleClass("maindiv");
103  } else {
104  /*
105  * No JavaScript: let's make the best of the situation using regular
106  * CSS-based layout
107  */
108  root()->setStyleClass("maindiv");
109  WContainerWidget *top = new WContainerWidget();
110  top->setLayout(grid, AlignTop | AlignLeft);
111  root()->addWidget(top);
112  root()->addWidget(gitView_);
113  gitView_->setFloatSide(Left);
114  gitView_->setMargin(6);
115  root()->addWidget(sourceView_);
116  sourceView_->setMargin(6);
117  }
118  }
119 
120 private:
121  WLineEdit *repositoryEdit_, *revisionEdit_;
122  WText *repositoryError_, *revisionError_;
126 
129  void loadGitModel() {
130  sourceView_->setIndex(WModelIndex());
131  repositoryError_->setText("");
132  revisionError_->setText("");
133  try {
134  gitModel_->setRepositoryPath(repositoryEdit_->text().toUTF8());
135  try {
136  gitModel_->loadRevision(revisionEdit_->text().toUTF8());
137  } catch (const Git::Exception& e) {
138  revisionError_->setText(e.what());
139  }
140  } catch (const Git::Exception& e) {
141  repositoryError_->setText(e.what());
142  }
143  }
144 
147  void showFile() {
148  if (gitView_->selectedIndexes().empty())
149  return;
150 
151  WModelIndex selected = *gitView_->selectedIndexes().begin();
152  sourceView_->setIndex(selected);
153  }
154 };
155 
157 {
158  return new GitViewApplication(env);
159 }
160 
161 int main(int argc, char **argv)
162 {
163  return WRun(argc, argv, &createApplication);
164 }
165 

Generated on Sat Oct 18 2014 for the C++ Web Toolkit (Wt) by doxygen 1.8.1.2