Wt examples  3.3.6
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:
Inheritance graph
[legend]

Public Member Functions

 ExampleSourceViewer (const std::string &deployPath, const std::string &examplesRoot, const std::string &examplesType)
 Constructor. More...
 

Private Member Functions

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

Private Attributes

Wt::WTreeView * exampleView_
 
SourceViewsourceView_
 
std::string deployPath_
 
std::string examplesRoot_
 
std::string examplesType_
 
Wt::WStandardItemModel * model_
 

Detailed Description

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

Definition at line 21 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 71 of file ExampleSourceViewer.C.

74  : deployPath_(deployPath),
75  examplesRoot_(examplesRoot),
76  examplesType_(examplesType)
77 {
78  wApp->internalPathChanged().connect
80 
82 }

Member Function Documentation

◆ cppTraverseDir()

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

Definition at line 194 of file ExampleSourceViewer.C.

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

◆ handlePathChange()

void ExampleSourceViewer::handlePathChange ( )
private

Definition at line 84 of file ExampleSourceViewer.C.

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

◆ javaTraverseDir()

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

Definition at line 324 of file ExampleSourceViewer.C.

326 {
327  FileItem* dir = new FileItem("/icons/yellow-folder-open.png", filename(path),
328  "");
329  parent->appendRow(dir);
330  parent = dir;
331 
332  std::vector<fs::path> files, dirs;
333 
334  fs::directory_iterator end_itr;
335  for (fs::directory_iterator i(path); i != end_itr; ++i) {
336  fs::path p = *i;
337  if (fs::is_directory(p)) {
338  if (filename(p) == "src") {
339  FileItem* dir = new FileItem("/icons/package-folder-open.png",
340  filename(p), "");
341  parent->appendRow(dir);
342  javaTraversePackages(dir, p, "");
343  } else
344  dirs.push_back(p);
345  } else {
346  files.push_back(p);
347  }
348  }
349 
350  std::sort(dirs.begin(), dirs.end(), comparePaths);
351  std::sort(files.begin(), files.end(), comparePaths);
352 
353  for (unsigned int i = 0; i < dirs.size(); i++)
354  javaTraverseDir(parent, dirs[i]);
355 
356  for (unsigned int i = 0; i < files.size(); i++) {
357  FileItem *file = new FileItem("/icons/document.png", filename(files[i]),
358  files[i].string());
359  parent->appendRow(file);
360  }
361 }
WStandardItem which stores a file.
Definition: FileItem.h:28
void javaTraverseDir(Wt::WStandardItem *parent, const boost::filesystem::path &path)
void javaTraversePackages(Wt::WStandardItem *parent, const boost::filesystem::path &srcPath, const std::string packageName)
static std::string filename(const fs::path &p)
static bool comparePaths(const fs::path &p1, const fs::path &p2)

◆ javaTraversePackages()

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

Definition at line 290 of file ExampleSourceViewer.C.

293 {
294  fs::directory_iterator end_itr;
295 
296  FileItem *packageItem = 0;
297  for (fs::directory_iterator i(srcPath); i != end_itr; ++i) {
298  fs::path p = *i;
299  if (fs::is_regular(p)) {
300  if (!packageItem) {
301  packageItem = new FileItem("/icons/package.png", packageName, "");
302  parent->appendRow(packageItem);
303  }
304 
305  FileItem *file = new FileItem("/icons/javaclass.png", filename(p),
306  p.string());
307  packageItem->appendRow(file);
308  }
309  }
310 
311  for (fs::directory_iterator i(srcPath); i != end_itr; ++i) {
312  fs::path p = *i;
313  if (fs::is_directory(p)) {
314  std::string pn = packageName;
315  if (!pn.empty())
316  pn += ".";
317  pn += filename(p);
318 
319  javaTraversePackages(parent, p, pn);
320  }
321  }
322 }
WStandardItem which stores a file.
Definition: FileItem.h:28
void javaTraversePackages(Wt::WStandardItem *parent, const boost::filesystem::path &srcPath, const std::string packageName)
static std::string filename(const fs::path &p)

◆ setExample()

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

Definition at line 101 of file ExampleSourceViewer.C.

103 {
104  clear();
105 
106  bool exists = false;
107  try {
108  exists = fs::exists(exampleDir);
109  } catch (std::exception&) {
110  }
111 
112  if (!exists) {
113  WApplication::instance()->setInternalPathValid(false);
114  addWidget(new WText("No such example: " + exampleDir));
115  return;
116  }
117 
118  model_ = new WStandardItemModel(0, 1, this);
119  if (examplesType_ == "CPP") {
120  cppTraverseDir(model_->invisibleRootItem(), exampleDir);
121  } else if (examplesType_ == "JAVA") {
122  javaTraverseDir(model_->invisibleRootItem(), exampleDir);
123  }
124 
125  WApplication::instance()->setTitle(tr("srcview.title." + example));
126  WText *title =
127  new WText(tr("srcview.title." + examplesType_ + "." + example));
128  title->setInternalPathEncoding(true);
129 
130  exampleView_ = new WTreeView();
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(SingleSelection);
137  exampleView_->setAlternatingRowColors(false);
138  exampleView_->selectionChanged().connect
140 
144  sourceView_->setStyleClass("source-view");
145 
146  /*
147  * Expand path to first file, to show something in the source viewer
148  */
149  WStandardItem *w = model_->item(0);
150  do {
151  exampleView_->setExpanded(w->index(), true);
152  if (w->rowCount() > 0)
153  w = w->child(0);
154  else {
155  exampleView_->select(w->index(), Select);
156  w = 0;
157  }
158  } while (w);
159 
160  WVBoxLayout *topLayout = new WVBoxLayout();
161  topLayout->addWidget(title);
162 
163  WHBoxLayout *gitLayout = new WHBoxLayout();
164  gitLayout->addWidget(exampleView_, 0);
165  gitLayout->addWidget(sourceView_, 1);
166  topLayout->addLayout(gitLayout, 1);
167  gitLayout->setResizable(0);
168 
169  /*
170  * FIXME, in plain HTML mode, we should set a minimum size to the source
171  * view, and remove this in enableAjax() ?
172  */
173  // sourceView_->setHeight("100%");
174 
175  setLayout(topLayout);
176  setStyleClass("maindiv");
177 }
static const int FileNameRole
Definition: FileItem.h:33
Wt::WStandardItemModel * model_
void showFile()
Displayed the currently selected file.
static const int FilePathRole
Definition: FileItem.h:32
Wt::WTreeView * exampleView_
static const int ContentsRole
Definition: FileItem.h:31
void javaTraverseDir(Wt::WStandardItem *parent, const boost::filesystem::path &path)
View class for source code.
Definition: SourceView.h:26
void cppTraverseDir(Wt::WStandardItem *parent, const boost::filesystem::path &path)

◆ showFile()

void ExampleSourceViewer::showFile ( )
private

Displayed the currently selected file.

Definition at line 365 of file ExampleSourceViewer.C.

365  {
366  if (exampleView_->selectedIndexes().empty())
367  return;
368 
369  WModelIndex selected = *exampleView_->selectedIndexes().begin();
370 
371  // expand a folder when clicked
372  if (exampleView_->model()->rowCount(selected) > 0
373  && !exampleView_->isExpanded(selected))
374  exampleView_->setExpanded(selected, true);
375 
376  // (for a file,) load data in source viewer
377  sourceView_->setIndex(selected);
378 }
Wt::WTreeView * exampleView_
bool setIndex(const Wt::WModelIndex &index)
Sets the model index.
Definition: SourceView.C:30

Member Data Documentation

◆ deployPath_

std::string ExampleSourceViewer::deployPath_
private

Definition at line 34 of file ExampleSourceViewer.h.

◆ examplesRoot_

std::string ExampleSourceViewer::examplesRoot_
private

Definition at line 35 of file ExampleSourceViewer.h.

◆ examplesType_

std::string ExampleSourceViewer::examplesType_
private

Definition at line 36 of file ExampleSourceViewer.h.

◆ exampleView_

Wt::WTreeView* ExampleSourceViewer::exampleView_
private

Definition at line 31 of file ExampleSourceViewer.h.

◆ model_

Wt::WStandardItemModel* ExampleSourceViewer::model_
private

Definition at line 38 of file ExampleSourceViewer.h.

◆ sourceView_

SourceView* ExampleSourceViewer::sourceView_
private

Definition at line 32 of file ExampleSourceViewer.h.


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

Generated on Thu Jan 12 2017 for the C++ Web Toolkit (Wt) by doxygen 1.8.13