Wt examples  4.5.0
Public Member Functions | Private Member Functions | Private Attributes | List of all members
GitViewApplication Class Reference

A simple application to navigate a git repository. More...

Inheritance diagram for GitViewApplication:
Inheritance graph
[legend]

Public Member Functions

 GitViewApplication (const WEnvironment &env)
 Constructor. More...
 

Private Member Functions

void loadGitModel ()
 Change repository and/or revision. More...
 
void showFile ()
 Displayed the currently selected file. More...
 

Private Attributes

WLineEdit * repositoryEdit_
 
WLineEdit * revisionEdit_
 
WText * repositoryError_
 
WText * revisionError_
 
std::shared_ptr< GitModelgitModel_
 
WTreeView * gitView_
 
SourceViewsourceView_
 

Detailed Description

A simple application to navigate a git repository.

This examples demonstrates how to use the custom model use GitModel with a WTreeView.

Definition at line 39 of file GitView.C.

Constructor & Destructor Documentation

◆ GitViewApplication()

GitViewApplication::GitViewApplication ( const WEnvironment &  env)
inline

Constructor.

Definition at line 44 of file GitView.C.

45  : WApplication(env)
46  {
47  useStyleSheet("gitview.css");
48  setTitle("Git model example");
49 
50  const char *gitRepo = getenv("GITVIEW_REPOSITORY_PATH");
51 
52  auto grid
53  = std::make_unique<WGridLayout>();
54  grid->addWidget(std::make_unique<WText>("Git repository path:"), 0, 0);
55 
56  repositoryEdit_ = grid->addWidget(std::make_unique<WLineEdit>(gitRepo ? gitRepo : ""),
57  0, 1, AlignmentFlag::Left);
58  repositoryError_ = grid->addWidget(std::make_unique<WText>(), 0, 2);
59 
60  grid->addWidget(std::make_unique<WText>("Revision:"), 1, 0);
61 
62  revisionEdit_ = grid->addWidget(std::make_unique<WLineEdit>("master"),
63  1, 1, AlignmentFlag::Left);
64  revisionError_ = grid->addWidget(std::make_unique<WText>(), 1, 2);
65 
66  repositoryEdit_->setTextSize(30);
67  revisionEdit_->setTextSize(20);
68  repositoryError_->setStyleClass("error-msg");
69  revisionError_->setStyleClass("error-msg");
70 
71  repositoryEdit_->enterPressed()
72  .connect(this, &GitViewApplication::loadGitModel);
73  revisionEdit_->enterPressed()
74  .connect(this, &GitViewApplication::loadGitModel);
75 
76  auto button = grid->addWidget(std::make_unique<WPushButton>("Load"),
77  2, 0, AlignmentFlag::Left);
78  button->clicked().connect(this, &GitViewApplication::loadGitModel);
79 
80  auto gitView = std::make_unique<WTreeView>();
81  gitView_ = gitView.get();
82  gitView_->resize(300, WLength::Auto);
83  gitView_->setSortingEnabled(false);
84 
85  gitModel_
86  = std::make_shared<GitModel>();
87  gitView_->setModel(gitModel_);
88  gitView_->setSelectionMode(SelectionMode::Single);
89  gitView_->selectionChanged().connect(this, &GitViewApplication::showFile);
90 
91  auto sourceView
92  = std::make_unique<SourceView>(ItemDataRole::Display,
94  sourceView_ = sourceView.get();
95  sourceView_->setStyleClass("source-view");
96 
97  /* FIXME: adding a gridlayout to a box layout */
98  if (environment().javaScript()) {
99  /*
100  * We have JavaScript: We can use layout managers so everything will
101  * always fit nicely in the window.
102  */
103  auto topLayout = root()->setLayout(std::make_unique<WVBoxLayout>());
104  root()->setStyleClass("maindiv");
105  topLayout->addLayout(std::move(grid),0);
106 
107  auto gitLayout = std::make_unique<WHBoxLayout>();
108  gitLayout->addWidget(std::move(gitView),0);
109  gitLayout->addWidget(std::move(sourceView),1);
110  topLayout->addLayout(std::move(gitLayout),1);
111  } else {
112  /*
113  * No JavaScript: let's make the best of the situation using regular
114  * CSS-based layout
115  */
116  root()->setStyleClass("maindiv");
117  auto top
118  = std::make_unique<WContainerWidget>();
119  top->setLayout(std::move(grid));
120  root()->addWidget(std::move(top));
121  root()->addWidget(std::move(gitView));
122  gitView_->setFloatSide(Side::Left);
123  gitView_->setMargin(6);
124  root()->addWidget(std::move(sourceView));
125  sourceView_->setMargin(6);
126  }
127  }
static const ItemDataRole ContentsRole
The role which may be used on a file to retrieve its contents.
Definition: GitModel.h:43
static const ItemDataRole FilePathRole
Definition: GitModel.h:44
void loadGitModel()
Change repository and/or revision.
Definition: GitView.C:138
WText * repositoryError_
Definition: GitView.C:131
WLineEdit * repositoryEdit_
Definition: GitView.C:130
WText * revisionError_
Definition: GitView.C:131
SourceView * sourceView_
Definition: GitView.C:134
std::shared_ptr< GitModel > gitModel_
Definition: GitView.C:132
WLineEdit * revisionEdit_
Definition: GitView.C:130
WTreeView * gitView_
Definition: GitView.C:133
void showFile()
Displayed the currently selected file.
Definition: GitView.C:156

Member Function Documentation

◆ loadGitModel()

void GitViewApplication::loadGitModel ( )
inlineprivate

Change repository and/or revision.

Definition at line 138 of file GitView.C.

138  {
139  sourceView_->setIndex(WModelIndex());
140  repositoryError_->setText("");
141  revisionError_->setText("");
142  try {
143  gitModel_->setRepositoryPath(repositoryEdit_->text().toUTF8());
144  try {
145  gitModel_->loadRevision(revisionEdit_->text().toUTF8());
146  } catch (const Git::Exception& e) {
147  revisionError_->setText(e.what());
148  }
149  } catch (const Git::Exception& e) {
150  repositoryError_->setText(e.what());
151  }
152  }
Exception class.
Definition: Git.h:28
bool setIndex(const WModelIndex &index)
Sets the model index.
Definition: SourceView.C:32

◆ showFile()

void GitViewApplication::showFile ( )
inlineprivate

Displayed the currently selected file.

Definition at line 156 of file GitView.C.

156  {
157  if (gitView_->selectedIndexes().empty())
158  return;
159 
160  WModelIndex selected = *gitView_->selectedIndexes().begin();
161  sourceView_->setIndex(selected);
162  }

Member Data Documentation

◆ gitModel_

std::shared_ptr<GitModel> GitViewApplication::gitModel_
private

Definition at line 132 of file GitView.C.

◆ gitView_

WTreeView* GitViewApplication::gitView_
private

Definition at line 133 of file GitView.C.

◆ repositoryEdit_

WLineEdit* GitViewApplication::repositoryEdit_
private

Definition at line 130 of file GitView.C.

◆ repositoryError_

WText* GitViewApplication::repositoryError_
private

Definition at line 131 of file GitView.C.

◆ revisionEdit_

WLineEdit * GitViewApplication::revisionEdit_
private

Definition at line 130 of file GitView.C.

◆ revisionError_

WText * GitViewApplication::revisionError_
private

Definition at line 131 of file GitView.C.

◆ sourceView_

SourceView* GitViewApplication::sourceView_
private

Definition at line 134 of file GitView.C.


The documentation for this class was generated from the following file:

Generated on Sat Aug 14 2021 for the C++ Web Toolkit (Wt) by doxygen 1.9.1