Wt examples
3.2.3
|
View class for source code. More...
#include <SourceView.h>
Public Member Functions | |
SourceView (int fileNameRole, int contentRole, int filePathRole) | |
Constructor. | |
virtual | ~SourceView () |
Destructor. | |
bool | setIndex (const Wt::WModelIndex &index) |
Sets the model index. | |
virtual Wt::WWidget * | renderView () |
Returns the widget that renders the view. | |
Private Member Functions | |
std::string | imageExtension (const std::string &fileName) |
Private Attributes | |
Wt::WModelIndex | index_ |
The index that is currently displayed. | |
int | fileNameRole_ |
The role that is currently displayed. | |
int | contentRole_ |
int | filePathRole_ |
Wt::WMemoryResource * | imageResource_ |
View class for source code.
A view class is used so that no server-side memory is used while displaying a potentially large file.
Definition at line 26 of file SourceView.h.
SourceView::SourceView | ( | int | fileNameRole, |
int | contentRole, | ||
int | filePathRole | ||
) |
Constructor.
The fileNameRole will be used to retrieve data from a file to be displayed. If no data is set for this role, then contentRole should hold the data as a string.
Definition at line 20 of file SourceView.C.
: fileNameRole_(fileNameRole), contentRole_(contentRole), filePathRole_(filePathRole), imageResource_(0) {}
SourceView::~SourceView | ( | ) | [virtual] |
std::string SourceView::imageExtension | ( | const std::string & | fileName | ) | [private] |
Definition at line 204 of file SourceView.C.
{ static const char *imageExtensions[] = { ".png", ".gif", ".jpg", "jpeg", ".ico", 0 }; fs::path p(fileName); std::string extension = fs::extension(p); for (const char **s = imageExtensions; *s != 0; ++s) if (*s == extension) return extension.substr(1); return std::string(); }
WWidget * SourceView::renderView | ( | ) | [virtual] |
Returns the widget that renders the view.
Returns he view contents: renders the file to a WText widget. WViewWidget deletes this widget after every rendering step.
Implements Wt::WViewWidget.
Definition at line 95 of file SourceView.C.
{ if (!index_.isValid()) { // no content WText *result = new WText(); result->setInline(false); return result; } /* * read the contents, from string or file name */ boost::any contentsData = index_.data(contentRole_); std::string content; if (!contentsData.empty()) content = boost::any_cast<const std::string&>(contentsData); boost::any fileNameData = index_.data(fileNameRole_); std::string fileName = boost::any_cast<const std::string&>(fileNameData); boost::any filePathData = index_.data(filePathRole_); std::string filePath; if (!filePathData.empty()) filePath = boost::any_cast<const std::string&>(filePathData); /* * determine source language, for source highlight */ std::string lang = getLanguageFromFileExtension(fileName); if (content != "" && content.substr(0, 100).find("-*- C++ -*-") != std::string::npos) lang = "cpp"; std::string outputFileName; if (lang != "") { std::string inputFileName; if (!filePathData.empty()) inputFileName = filePath; else { inputFileName = tempFileName(); std::ofstream out(inputFileName.c_str(), std::ios::out | std::ios::binary); out.write(content.c_str(), (std::streamsize)content.length()); out.close(); } outputFileName = tempFileName(); std::string sourceHighlightCommand = "source-highlight "; sourceHighlightCommand += "--src-lang=" + lang + " "; sourceHighlightCommand += "--out-format=xhtml "; sourceHighlightCommand += "--input=" + inputFileName + " "; sourceHighlightCommand += "--output=" + outputFileName + " "; std::cerr << sourceHighlightCommand << std::endl; bool sourceHighlightOk = system(sourceHighlightCommand.c_str()) == 0; if (sourceHighlightOk) content = readFileToString(outputFileName); else { content = readFileToString(inputFileName); lang = ""; } unlink(outputFileName.c_str()); if (filePathData.empty()) unlink(inputFileName.c_str()); } if (content == "") // do not load binary files, we would need to perform proper UTF-8 // transcoding to display them if (!boost::iends_with(fileName, ".jar") && !boost::iends_with(fileName, ".war") && !boost::iends_with(fileName, ".class")) content = readFileToString(fileName); delete imageResource_; imageResource_ = 0; WWidget *result = 0; if (!imageExtension(fileName).empty()) { WImage *image = new WImage(); imageResource_ = new WMemoryResource(this); imageResource_->setMimeType("mime/" + imageExtension(fileName)); imageResource_->setData((const unsigned char*)content.data(), (int)content.length()); image->setImageLink(imageResource_); result = image; } else if (lang != "") { WText *text = new WText(); text->setTextFormat(XHTMLUnsafeText); text->setText(WString::fromUTF8(content)); result = text; } else { WText *text = new WText(); text->setTextFormat(PlainText); text->setText(WString::fromUTF8(content)); result = text; } result->setInline(false); WApplication::instance() ->doJavaScript(result->jsRef() + ".parentNode.scrollTop = 0;"); return result; }
bool SourceView::setIndex | ( | const Wt::WModelIndex & | index | ) |
Sets the model index.
Returns true whether the view will be rerendered. The view will only be rerendered if the index contains new data.
Definition at line 30 of file SourceView.C.
{ if (index != index_ && index.isValid()) { std::string fp = index.data(filePathRole_).empty() ? std::string() : boost::any_cast<std::string>(index.data(filePathRole_)); if (!index.data(contentRole_).empty() || (!fp.empty() && !fs::is_directory(fp))) { index_ = index; update(); return true; } } return false; }
int SourceView::contentRole_ [private] |
Definition at line 61 of file SourceView.h.
int SourceView::fileNameRole_ [private] |
The role that is currently displayed.
Definition at line 60 of file SourceView.h.
int SourceView::filePathRole_ [private] |
Definition at line 62 of file SourceView.h.
Wt::WMemoryResource* SourceView::imageResource_ [private] |
Definition at line 64 of file SourceView.h.
Wt::WModelIndex SourceView::index_ [private] |
The index that is currently displayed.
Definition at line 57 of file SourceView.h.