Wt examples  3.3.5
Public Member Functions | Private Member Functions | Private Attributes
ExampleSourceViewer Class Reference

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

#include <ExampleSourceViewer.h>

List of all members.

Public Member Functions

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

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.
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 ( const std::string &  deployPath,
const std::string &  examplesRoot,
const std::string &  examplesType 
)

Constructor.

Definition at line 71 of file ExampleSourceViewer.C.

  : deployPath_(deployPath),
    examplesRoot_(examplesRoot),
    examplesType_(examplesType)
{
  wApp->internalPathChanged().connect
    (this, &ExampleSourceViewer::handlePathChange);

  handlePathChange();
}

Member Function Documentation

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

Definition at line 194 of file ExampleSourceViewer.C.

{
  static const char *supportedFiles[] = {
    ".C", ".cpp", ".h", ".css", ".xml", ".png", ".gif", ".csv", ".ico", 0
  };

  FileItem* dir = new FileItem("/icons/yellow-folder-open.png", filename(path),
                               "");
  parent->appendRow(dir);
  parent = dir;
  try {
    std::set<fs::path> paths;

    fs::directory_iterator end_itr;
    for (fs::directory_iterator i(path); i != end_itr; ++i) 
      paths.insert(*i);

    std::vector<FileItem*> classes, files;
    std::vector<fs::path> dirs;

    while (!paths.empty()) {
      fs::path p = *paths.begin();
      paths.erase(p);

      // skip symbolic links and other files
      if (fs::is_symlink(p))
        continue;

      // skip files with an extension we do not want to handle
      if (fs::is_regular(p)) {
        std::string ext = fs::extension(p);
        bool supported = false;
        for (const char **s = supportedFiles; *s != 0; ++s)
          if (*s == ext) {
            supported = true;
            break;
          }
        
        if (!supported)
          continue;
      }

      // see if we have one file of a class (.C, .h)
      fs::path companion = getCompanion(p);
      if (!companion.empty()) {
        std::set<fs::path>::iterator it_companion = paths.find(companion);
 
          if (it_companion != paths.end()) {
            std::string className = stem(p);
            escapeText(className);
            std::string label = "<i>class</i> " + className;

            FileItem *classItem = 
              new FileItem("/icons/cppclass.png", label, std::string());
            classItem->setFlags(classItem->flags() | ItemIsXHTMLText);

            FileItem *header = new FileItem("/icons/document.png", filename(p),
                                            p.string());
            FileItem *cpp = new FileItem("/icons/document.png",
                                         filename(*it_companion),
                                         (*it_companion).string());
            classItem->appendRow(header);
            classItem->appendRow(cpp);
          
            classes.push_back(classItem);
            paths.erase(it_companion);
          } else {
            FileItem *file = new FileItem("/icons/document.png", filename(p),
                                          p.string());
            files.push_back(file);
          }
      } else if (fs::is_directory(p)) {
        dirs.push_back(p);
      } else {
        FileItem *file = new FileItem("/icons/document.png", filename(p),
                                      p.string());
        files.push_back(file);
      }
    }

    std::sort(dirs.begin(), dirs.end(), comparePaths);

    for (unsigned int i = 0; i < classes.size(); i++)
      parent->appendRow(classes[i]);

    for (unsigned int i = 0; i < files.size(); i++)
      parent->appendRow(files[i]);

    for (unsigned int i = 0; i < dirs.size(); i++)
      cppTraverseDir(parent, dirs[i]);
  } catch (fs::filesystem_error& e) {
    std::cerr << e.what() << std::endl;
  }
}

Definition at line 84 of file ExampleSourceViewer.C.

{
  WApplication *app = wApp;

  if (app->internalPathMatches(deployPath_)) {
    std::string example = app->internalPathNextPart(deployPath_);

    if (example.find("..") != std::string::npos
        || example.find('/') != std::string::npos
        || example.find('\\') != std::string::npos) {
      app->setInternalPathValid(false);
      setExample("INVALID_DIR", "INVALID");
    } else
      setExample(examplesRoot_ + example, example);
  }
}
void ExampleSourceViewer::javaTraverseDir ( Wt::WStandardItem *  parent,
const boost::filesystem::path &  path 
) [private]

Definition at line 324 of file ExampleSourceViewer.C.

{
  FileItem* dir = new FileItem("/icons/yellow-folder-open.png", filename(path),
                               "");
  parent->appendRow(dir);
  parent = dir;

  std::vector<fs::path> files, dirs;

  fs::directory_iterator end_itr;
  for (fs::directory_iterator i(path); i != end_itr; ++i) {
    fs::path p = *i;
    if (fs::is_directory(p)) {
      if (filename(p) == "src") {
        FileItem* dir = new FileItem("/icons/package-folder-open.png",
                                     filename(p), "");
        parent->appendRow(dir);
        javaTraversePackages(dir, p, "");
      } else
        dirs.push_back(p);
    } else {
      files.push_back(p);
    }
  }

  std::sort(dirs.begin(), dirs.end(), comparePaths);
  std::sort(files.begin(), files.end(), comparePaths);

  for (unsigned int i = 0; i < dirs.size(); i++)
    javaTraverseDir(parent, dirs[i]);

  for (unsigned int i = 0; i < files.size(); i++) {
    FileItem *file = new FileItem("/icons/document.png", filename(files[i]),
                                  files[i].string());
    parent->appendRow(file);
  }
}
void ExampleSourceViewer::javaTraversePackages ( Wt::WStandardItem *  parent,
const boost::filesystem::path &  srcPath,
const std::string  packageName 
) [private]

Definition at line 290 of file ExampleSourceViewer.C.

{
  fs::directory_iterator end_itr;

  FileItem *packageItem = 0;
  for (fs::directory_iterator i(srcPath); i != end_itr; ++i) {
    fs::path p = *i;
    if (fs::is_regular(p)) {
      if (!packageItem) {
        packageItem = new FileItem("/icons/package.png", packageName, "");
        parent->appendRow(packageItem);
      }

      FileItem *file = new FileItem("/icons/javaclass.png", filename(p),
                                    p.string());
      packageItem->appendRow(file);
    }
  }

  for (fs::directory_iterator i(srcPath); i != end_itr; ++i) {
    fs::path p = *i;
    if (fs::is_directory(p)) {  
      std::string pn = packageName;
      if (!pn.empty())
        pn += ".";
      pn += filename(p);

      javaTraversePackages(parent, p, pn);
    }
  }
}
void ExampleSourceViewer::setExample ( const std::string &  exampleDir,
const std::string &  example 
) [private]

Definition at line 101 of file ExampleSourceViewer.C.

{
  clear();

  bool exists = false;
  try {
    exists = fs::exists(exampleDir);
  } catch (std::exception&) {
  }

  if (!exists) {
    WApplication::instance()->setInternalPathValid(false);
    addWidget(new WText("No such example: " + exampleDir));
    return;
  }

  model_ = new WStandardItemModel(0, 1, this);
  if (examplesType_ == "CPP") {
    cppTraverseDir(model_->invisibleRootItem(), exampleDir);
  } else if (examplesType_ == "JAVA") {
    javaTraverseDir(model_->invisibleRootItem(), exampleDir);
  }

  WApplication::instance()->setTitle(tr("srcview.title." + example));
  WText *title = 
    new WText(tr("srcview.title." + examplesType_ + "." + example));
  title->setInternalPathEncoding(true);

  exampleView_ = new WTreeView();
  exampleView_->setHeaderHeight(0);
  exampleView_->resize(300, WLength::Auto);
  exampleView_->setSortingEnabled(false);
  exampleView_->setModel(model_);
  exampleView_->expandToDepth(1);
  exampleView_->setSelectionMode(SingleSelection);
  exampleView_->setAlternatingRowColors(false);
  exampleView_->selectionChanged().connect
    (this, &ExampleSourceViewer::showFile);

  sourceView_ = new SourceView(FileItem::FileNameRole, 
                               FileItem::ContentsRole,
                               FileItem::FilePathRole);
  sourceView_->setStyleClass("source-view");

  /*
   * Expand path to first file, to show something in the source viewer
   */
  WStandardItem *w = model_->item(0);
  do {
    exampleView_->setExpanded(w->index(), true);
    if (w->rowCount() > 0)
      w = w->child(0);
    else {
      exampleView_->select(w->index(), Select);
      w = 0;
    }
  } while (w);

  WVBoxLayout *topLayout = new WVBoxLayout();
  topLayout->addWidget(title);

  WHBoxLayout *gitLayout = new WHBoxLayout();
  gitLayout->addWidget(exampleView_, 0);
  gitLayout->addWidget(sourceView_, 1);
  topLayout->addLayout(gitLayout, 1);
  gitLayout->setResizable(0);

  /*
   * FIXME, in plain HTML mode, we should set a minimum size to the source
   * view, and remove this in enableAjax() ?
   */
  // sourceView_->setHeight("100%");

  setLayout(topLayout);
  setStyleClass("maindiv");
}
void ExampleSourceViewer::showFile ( ) [private]

Displayed the currently selected file.

Definition at line 365 of file ExampleSourceViewer.C.

                                   {
  if (exampleView_->selectedIndexes().empty())
    return;

  WModelIndex selected = *exampleView_->selectedIndexes().begin();

  // expand a folder when clicked
  if (exampleView_->model()->rowCount(selected) > 0
      && !exampleView_->isExpanded(selected))
    exampleView_->setExpanded(selected, true);

  // (for a file,) load data in source viewer
  sourceView_->setIndex(selected);
}

Member Data Documentation

std::string ExampleSourceViewer::deployPath_ [private]

Definition at line 34 of file ExampleSourceViewer.h.

std::string ExampleSourceViewer::examplesRoot_ [private]

Definition at line 35 of file ExampleSourceViewer.h.

std::string ExampleSourceViewer::examplesType_ [private]

Definition at line 36 of file ExampleSourceViewer.h.

Wt::WTreeView* ExampleSourceViewer::exampleView_ [private]

Definition at line 31 of file ExampleSourceViewer.h.

Wt::WStandardItemModel* ExampleSourceViewer::model_ [private]

Definition at line 38 of file ExampleSourceViewer.h.

Definition at line 32 of file ExampleSourceViewer.h.


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

Generated on Wed Mar 23 2016 for the C++ Web Toolkit (Wt) by doxygen 1.7.6.1