Wt examples: ExampleSourceViewer Class Reference

Wt examples 4.10.4

Loading...
Searching...
No Matches
Public Member Functions | Private Member Functions | Private Attributes | List of all members
ExampleSourceViewer Class Reference

A simple widget to visualise a set of example source files. More...

`#include <ExampleSourceViewer.h>`

Inheritance diagram for ExampleSourceViewer:
[legend]

## Public Member Functions
| ExampleSourceViewer (const std::string &deployPath, const std::string &examplesRoot, const std::string &examplesType)
| Constructor.

## Private Member Functions
void | cppTraverseDir (WStandardItem *parent, const boost::filesystem::path &path)
void | javaTraverseDir (WStandardItem *parent, const boost::filesystem::path &path)
void | javaTraversePackages (WStandardItem *parent, const boost::filesystem::path &srcPath, const std::string packageName)
void | showFile ()
| Displayed the currently selected file.
void | handlePathChange ()
void | setExample (const std::string &exampleDir, const std::string &example)

## Private Attributes
WTreeView * | exampleView_
SourceView * | sourceView_
std::string | deployPath_
std::string | examplesRoot_
std::string | examplesType_
std::shared_ptr< WStandardItemModel > | model_

## Detailed Description

A simple widget to visualise a set of example source files.

Definition at line 23 of file ExampleSourceViewer.h.

## Constructor & Destructor Documentation

## ◆ ExampleSourceViewer()

ExampleSourceViewer::ExampleSourceViewer | ( | const std::string & | deployPath,
|  | const std::string & | examplesRoot,
|  | const std::string & | examplesType )

Constructor.

Definition at line 70 of file ExampleSourceViewer.C.

73 : deployPath_(deployPath),
74 examplesRoot_(examplesRoot),
75 examplesType_(examplesType)
76{
77 wApp->internalPathChanged().connect
78 (this, &ExampleSourceViewer::handlePathChange);
79
80 handlePathChange();
81}
ExampleSourceViewer::deployPath_
std::string deployPath_
Definition ExampleSourceViewer.h:36
ExampleSourceViewer::examplesType_
std::string examplesType_
Definition ExampleSourceViewer.h:38
ExampleSourceViewer::examplesRoot_
std::string examplesRoot_
Definition ExampleSourceViewer.h:37
ExampleSourceViewer::handlePathChange
void handlePathChange()
Definition ExampleSourceViewer.C:83

## Member Function Documentation

## ◆ cppTraverseDir()

void ExampleSourceViewer::cppTraverseDir | ( | WStandardItem * | parent,
|  | const boost::filesystem::path & | path )
| private

Definition at line 197 of file ExampleSourceViewer.C.

199{
200 static const char *supportedFiles[] = {
201 ".C", ".cpp", ".h", ".css", ".xml", ".png", ".gif", ".csv", ".ico", 0
202 };
203
204 auto dir = std::make_unique<FileItem>("/icons/yellow-folder-open.png",
205 filename(path),
206 "");
207 FileItem *dirPtr = dir.get();
208 parent->appendRow(std::move(dir));
209 parent = dirPtr;
210 try {
211 std::set<fs::path> paths;
212
213 fs::directory_iterator end_itr;
214 for (fs::directory_iterator i(path); i != end_itr; ++i)
215 paths.insert(*i);
216
217 std::vector<std::unique_ptr<FileItem>> classes, files;
218 std::vector<fs::path> dirs;
219
220 while (!paths.empty()) {
221 fs::path p = *paths.begin();
222 paths.erase(p);
223
224 // skip symbolic links and other files
225 if (fs::is_symlink(p))
226 continue;
227
228 // skip files with an extension we do not want to handle
229 if (fs::is_regular(p)) {
230 std::string ext = fs::extension(p);
231 bool supported = false;
232 for (const char **s = supportedFiles; *s != 0; ++s)
233 if (*s == ext) {
234 supported = true;
235 break;
236 }
237
238 if (!supported)
239 continue;
240 }
241
242 // see if we have one file of a class (.C, .h)
243 fs::path companion = getCompanion(p);
244 if (!companion.empty()) {
245 std::set<fs::path>::iterator it_companion = paths.find(companion);
246
247 if (it_companion != paths.end()) {
248 std::string className = stem(p);
249 escapeText(className);
250 std::string label = "<i>class</i> " + className;
251
252 std::unique_ptr<FileItem> classItem =
253 std::make_unique<FileItem>("/icons/cppclass.png", label, std::string());
254 classItem->setFlags(classItem->flags() | ItemFlag::XHTMLText);
255
256 auto header
257 = std::make_unique<FileItem>("/icons/document.png", filename(p),
258 p.string());
259 auto cpp
260 = std::make_unique<FileItem>("/icons/document.png",
261 filename(*it_companion),
262 (*it_companion).string());
263 classItem->appendRow(std::move(header));
264 classItem->appendRow(std::move(cpp));
265
266 classes.push_back(std::move(classItem));
267 paths.erase(it_companion);
268 } else {
269 auto file
270 = std::make_unique<FileItem>("/icons/document.png", filename(p),
271 p.string());
272 files.push_back(std::move(file));
273 }
274 } else if (fs::is_directory(p)) {
275 dirs.push_back(p);
276 } else {
277 auto file
278 = std::make_unique<FileItem>("/icons/document.png", filename(p),
279 p.string());
280 files.push_back(std::move(file));
281 }
282 }
283
284 std::sort(dirs.begin(), dirs.end(), comparePaths);
285
286 for (unsigned int i = 0; i < classes.size(); i++)
287 parent->appendRow(std::move(classes[i]));
288
289 for (unsigned int i = 0; i < files.size(); i++)
290 parent->appendRow(std::move(files[i]));
291
292 for (unsigned int i = 0; i < dirs.size(); i++)
293 cppTraverseDir(parent, dirs[i]);
294 } catch (fs::filesystem_error& e) {
295 std::cerr << e.what() << std::endl;
296 }
297}
getCompanion
static fs::path getCompanion(const fs::path &path)
Definition ExampleSourceViewer.C:185
stem
static std::string stem(const fs::path &p)
Definition ExampleSourceViewer.C:45
filename
static std::string filename(const fs::path &p)
Definition ExampleSourceViewer.C:35
comparePaths
static bool comparePaths(const fs::path &p1, const fs::path &p2)
Definition ExampleSourceViewer.C:65
ExampleSourceViewer::cppTraverseDir
void cppTraverseDir(WStandardItem *parent, const boost::filesystem::path &path)
Definition ExampleSourceViewer.C:197
FileItem
WStandardItem which stores a file.
Definition FileItem.h:31

## ◆ handlePathChange()

void ExampleSourceViewer::handlePathChange | ( |  | ) |
| private

Definition at line 83 of file ExampleSourceViewer.C.

84{
85 WApplication *app = wApp;
86
87 if (app->internalPathMatches(deployPath_)) {
88 std::string example = app->internalPathNextPart(deployPath_);
89
90 if (example.find("..") != std::string::npos
91 || example.find('/') != std::string::npos
92 || example.find('\\') != std::string::npos) {
93 app->setInternalPathValid(false);
94 setExample("INVALID_DIR", "INVALID");
95 } else
96 setExample(examplesRoot_ + example, example);
97 }
98}
ExampleSourceViewer::setExample
void setExample(const std::string &exampleDir, const std::string &example)
Definition ExampleSourceViewer.C:100

## ◆ javaTraverseDir()

void ExampleSourceViewer::javaTraverseDir | ( | WStandardItem * | parent,
|  | const boost::filesystem::path & | path )
| private

Definition at line 335 of file ExampleSourceViewer.C.

337{
338 auto dir
339 = std::make_unique<FileItem>("/icons/yellow-folder-open.png",
340 filename(path),"");
341 FileItem *dirPtr = dir.get();
342 parent->appendRow(std::move(dir));
343 parent = dirPtr;
344
345 std::vector<fs::path> files, dirs;
346
347 fs::directory_iterator end_itr;
348 for (fs::directory_iterator i(path); i != end_itr; ++i) {
349 fs::path p = *i;
350 if (fs::is_directory(p)) {
351 if (filename(p) == "src") {
352 auto dir
353 = std::make_unique<FileItem>("/icons/package-folder-open.png",
354 filename(p), "");
355 FileItem *dirPtr = dir.get();
356 parent->appendRow(std::move(dir));
357 javaTraversePackages(dirPtr, p, "");
358 } else
359 dirs.push_back(p);
360 } else {
361 files.push_back(p);
362 }
363 }
364
365 std::sort(dirs.begin(), dirs.end(), comparePaths);
366 std::sort(files.begin(), files.end(), comparePaths);
367
368 for (auto item : dirs)
369 javaTraverseDir(parent, item);
370
371 for (auto item : files) {
372 auto file
373 = std::make_unique<FileItem>("/icons/document.png", filename(item),
374 item.string());
375 parent->appendRow(std::move(file));
376 }
377}
ExampleSourceViewer::javaTraversePackages
void javaTraversePackages(WStandardItem *parent, const boost::filesystem::path &srcPath, const std::string packageName)
Definition ExampleSourceViewer.C:299
ExampleSourceViewer::javaTraverseDir
void javaTraverseDir(WStandardItem *parent, const boost::filesystem::path &path)
Definition ExampleSourceViewer.C:335

## ◆ javaTraversePackages()

void ExampleSourceViewer::javaTraversePackages | ( | WStandardItem * | parent,
|  | const boost::filesystem::path & | srcPath,
|  | const std::string | packageName )
| private

Definition at line 299 of file ExampleSourceViewer.C.

302{
303 fs::directory_iterator end_itr;
304
305 FileItem *packageItem = nullptr;
306 for (fs::directory_iterator i(srcPath); i != end_itr; ++i) {
307 fs::path p = *i;
308 if (fs::is_regular(p)) {
309 if (!packageItem) {
310 auto item = std::make_unique<FileItem>("/icons/package.png", packageName, "");
311 packageItem = item.get();
312 parent->appendRow(std::move(item));
313 }
314
315 auto file
316 = std::make_unique<FileItem>("/icons/javaclass.png", filename(p),
317 p.string());
318 packageItem->appendRow(std::move(file));
319 }
320 }
321
322 for (fs::directory_iterator i(srcPath); i != end_itr; ++i) {
323 fs::path p = *i;
324 if (fs::is_directory(p)) {
325 std::string pn = packageName;
326 if (!pn.empty())
327 pn += ".";
328 pn += filename(p);
329
330 javaTraversePackages(parent, p, pn);
331 }
332 }
333}

## ◆ setExample()

void ExampleSourceViewer::setExample | ( | const std::string & | exampleDir,
|  | const std::string & | example )
| private

Definition at line 100 of file ExampleSourceViewer.C.

102{
103 clear();
104
105 bool exists = false;
106 try {
107 exists = fs::exists(exampleDir);
108 } catch (std::exception&) {
109 }
110
111 if (!exists) {
112 WApplication::instance()->setInternalPathValid(false);
113 addWidget(std::make_unique<WText>("No such example: " + exampleDir));
114 return;
115 }
116
117 model_ = std::make_shared<WStandardItemModel>(0, 1);
118 if (examplesType_ == "CPP") {
119 cppTraverseDir(model_->invisibleRootItem(), exampleDir);
120 } else if (examplesType_ == "JAVA") {
121 javaTraverseDir(model_->invisibleRootItem(), exampleDir);
122 }
123
124 WApplication::instance()->setTitle(tr("srcview.title." + example));
125 std::unique_ptr<WText> title(std::make_unique<WText>(
126 tr("srcview.title." + examplesType_ + "." + example)));
127 title->setInternalPathEncoding(true);
128
129 auto exampleView = std::make_unique<WTreeView>();
130 exampleView_ = exampleView.get();
131 exampleView_->setHeaderHeight(0);
132 exampleView_->resize(300, WLength::Auto);
133 exampleView_->setSortingEnabled(false);
134 exampleView_->setModel(model_);
135 exampleView_->expandToDepth(1);
136 exampleView_->setSelectionMode(SelectionMode::Single);
137 exampleView_->setAlternatingRowColors(false);
138 exampleView_->selectionChanged().connect
139 (this, &ExampleSourceViewer::showFile);
140
141 auto sourceView =
142 std::make_unique<SourceView>(FileItem::FileNameRole,
143 FileItem::ContentsRole,
144 FileItem::FilePathRole);
145 sourceView_ = sourceView.get();
146 sourceView_->setStyleClass("source-view");
147
148 /*
149 * Expand path to first file, to show something in the source viewer
150 */
151 WStandardItem *w = model_->item(0);
152 do {
153 exampleView_->setExpanded(w->index(), true);
154 if (w->rowCount() > 0)
155 w = w->child(0);
156 else {
157 exampleView_->select(w->index());
158 w = 0;
159 }
160 } while (w);
161
162 auto topLayout = std::make_unique<WVBoxLayout>();
163 topLayout->addWidget(std::move(title));
164
165 auto gitLayout = std::make_unique<WHBoxLayout>();
166 WHBoxLayout *g = gitLayout.get();
167 gitLayout->addWidget(std::move(exampleView), 0);
168 gitLayout->addWidget(std::move(sourceView), 1);
169 topLayout->addLayout(std::move(gitLayout), 1);
170 g->setResizable(0);
171
172 /*
173 * FIXME, in plain HTML mode, we should set a minimum size to the source
174 * view, and remove this in enableAjax() ?
175 */
176 // sourceView_->setHeight("100%");
177
178 setLayout(std::move(topLayout));
179 setStyleClass("maindiv");
180}
ExampleSourceViewer::model_
std::shared_ptr< WStandardItemModel > model_
Definition ExampleSourceViewer.h:40
ExampleSourceViewer::showFile
void showFile()
Displayed the currently selected file.
Definition ExampleSourceViewer.C:381
ExampleSourceViewer::exampleView_
WTreeView * exampleView_
Definition ExampleSourceViewer.h:33
ExampleSourceViewer::sourceView_
SourceView * sourceView_
Definition ExampleSourceViewer.h:34
FileItem::FileNameRole
static const Wt::ItemDataRole FileNameRole
Definition FileItem.h:35
FileItem::FilePathRole
static const Wt::ItemDataRole FilePathRole
Definition FileItem.h:34
FileItem::ContentsRole
static const Wt::ItemDataRole ContentsRole
Definition FileItem.h:33

## ◆ showFile()

void ExampleSourceViewer::showFile | ( |  | ) |
| private

Displayed the currently selected file.

Definition at line 381 of file ExampleSourceViewer.C.

381 {
382 if (exampleView_->selectedIndexes().empty())
383 return;
384
385 WModelIndex selected = *exampleView_->selectedIndexes().begin();
386
387 // expand a folder when clicked
388 if (exampleView_->model()->rowCount(selected) > 0
389 && !exampleView_->isExpanded(selected))
390 exampleView_->setExpanded(selected, true);
391
392 // (for a file,) load data in source viewer
393 sourceView_->setIndex(selected);
394}
SourceView::setIndex
bool setIndex(const WModelIndex &index)
Sets the model index.
Definition SourceView.C:32

## Member Data Documentation

## ◆ deployPath_

std::string ExampleSourceViewer::deployPath_
| private

Definition at line 36 of file ExampleSourceViewer.h.

## ◆ examplesRoot_

std::string ExampleSourceViewer::examplesRoot_
| private

Definition at line 37 of file ExampleSourceViewer.h.

## ◆ examplesType_

std::string ExampleSourceViewer::examplesType_
| private

Definition at line 38 of file ExampleSourceViewer.h.

## ◆ exampleView_

WTreeView* ExampleSourceViewer::exampleView_
| private

Definition at line 33 of file ExampleSourceViewer.h.

## ◆ model_

std::shared_ptr<WStandardItemModel> ExampleSourceViewer::model_
| private

Definition at line 40 of file ExampleSourceViewer.h.

## ◆ sourceView_

SourceView* ExampleSourceViewer::sourceView_
| private

Definition at line 34 of file ExampleSourceViewer.h.

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

- wt-homepage/ExampleSourceViewer.h
- wt-homepage/ExampleSourceViewer.C