Wt examples  4.10.4
Public Member Functions | Private Member Functions | Private Attributes | List of all members
SourceView Class Reference

View class for source code. More...

#include <SourceView.h>

Inheritance diagram for SourceView:
[legend]

Public Member Functions

 SourceView (ItemDataRole fileNameRole, ItemDataRole contentRole, ItemDataRole filePathRole)
 Constructor. More...
 
virtual ~SourceView ()
 Destructor. More...
 
bool setIndex (const WModelIndex &index)
 Sets the model index. More...
 
virtual std::unique_ptr< WWidgetrenderView ()
 Returns the widget that renders the view. More...
 
- Public Member Functions inherited from Wt::WViewWidget
 WViewWidget ()
 
void update ()
 
virtual void load () override
 
virtual void render (WFlags< RenderFlag > flags) override
 
virtual void refresh () override
 
- Public Member Functions inherited from Wt::WWebWidget
 WWebWidget ()
 
virtual std::vector< WWidget * > children () const override
 
SignalchildrenChanged ()
 
virtual void setPositionScheme (PositionScheme scheme) override
 
virtual PositionScheme positionScheme () const override
 
virtual void setOffsets (const WLength &offset, WFlags< Side > sides=AllSides) override
 
virtual WLength offset (Side s) const override
 
virtual void resize (const WLength &width, const WLength &height) override
 
virtual WLength width () const override
 
virtual WLength height () const override
 
virtual void setMinimumSize (const WLength &width, const WLength &height) override
 
virtual WLength minimumWidth () const override
 
virtual WLength minimumHeight () const override
 
virtual void setMaximumSize (const WLength &width, const WLength &height) override
 
virtual WLength maximumWidth () const override
 
virtual WLength maximumHeight () const override
 
virtual void setLineHeight (const WLength &height) override
 
virtual WLength lineHeight () const override
 
virtual void setFloatSide (Side s) override
 
virtual Side floatSide () const override
 
virtual void setClearSides (WFlags< Side > sides) override
 
virtual WFlags< SideclearSides () const override
 
virtual void setMargin (const WLength &margin, WFlags< Side > sides=AllSides) override
 
virtual WLength margin (Side side) const override
 
virtual void setHiddenKeepsGeometry (bool enabled) override
 
virtual bool hiddenKeepsGeometry () const override
 
virtual void setHidden (bool hidden, const WAnimation &animation=WAnimation()) override
 
virtual bool isHidden () const override
 
virtual bool isVisible () const override
 
virtual void setDisabled (bool disabled) override
 
virtual bool isDisabled () const override
 
virtual bool isEnabled () const override
 
virtual void setPopup (bool popup) override
 
virtual bool isPopup () const override
 
virtual void setInline (bool isInline) override
 
virtual bool isInline () const override
 
virtual void setDecorationStyle (const WCssDecorationStyle &style) override
 
virtual WCssDecorationStyledecorationStyle () override
 
virtual void setStyleClass (const WString &styleClass) override
 
virtual WString styleClass () const override
 
virtual void addStyleClass (const WString &styleClass, bool force=false) override
 
virtual void removeStyleClass (const WString &styleClass, bool force=false) override
 
virtual bool hasStyleClass (const WString &styleClass) const override
 
virtual void setVerticalAlignment (AlignmentFlag alignment, const WLength &length=WLength()) override
 
virtual AlignmentFlag verticalAlignment () const override
 
virtual WLength verticalAlignmentLength () const override
 
virtual void setToolTip (const WString &text, TextFormat textFormat=TextFormat::Plain) override
 
virtual void setDeferredToolTip (bool enable, TextFormat textFormat=TextFormat::Plain) override
 
virtual WString toolTip () const override
 
virtual void setAttributeValue (const std::string &name, const WString &value) override
 
virtual WString attributeValue (const std::string &name) const override
 
virtual void setJavaScriptMember (const std::string &name, const std::string &value) override
 
virtual std::string javaScriptMember (const std::string &name) const override
 
virtual void callJavaScriptMember (const std::string &name, const std::string &args) override
 
virtual bool loaded () const override
 
virtual void setId (const std::string &id) override
 
virtual WWidgetfind (const std::string &name) override
 
virtual WWidgetfindById (const std::string &id) override
 
virtual void setSelectable (bool selectable) override
 
virtual void doJavaScript (const std::string &javascript) override
 
virtual const std::string id () const override
 
void setLoadLaterWhenInvisible (bool)
 
std::string htmlTagName () const
 
void setHtmlTagName (const std::string &tag)
 
virtual void setCanReceiveFocus (bool enabled) override
 
virtual bool canReceiveFocus () const override
 
virtual bool setFirstFocus () override
 
virtual void setFocus (bool focus) override
 
virtual bool hasFocus () const override
 
virtual void setTabIndex (int index) override
 
virtual int tabIndex () const override
 
EventSignalblurred ()
 
EventSignalfocussed ()
 
virtual bool scrollVisibilityEnabled () const final override
 
virtual void setScrollVisibilityEnabled (bool enabled) final override
 
virtual int scrollVisibilityMargin () const final override
 
virtual void setScrollVisibilityMargin (int margin) final override
 
virtual Signal< bool > & scrollVisibilityChanged () final override
 
virtual bool isScrollVisible () const final override
 
virtual void setThemeStyleEnabled (bool enabled) final override
 
virtual bool isThemeStyleEnabled () const final override
 
virtual void setObjectName (const std::string &name) override
 
void setFocus ()
 
virtual void setFocus (bool focus)=0
 
- Public Member Functions inherited from Wt::WWidget
virtual ~WWidget ()
 
WWidgetparent () const
 
virtual std::unique_ptr< WWidgetremoveWidget (WWidget *widget)
 
std::unique_ptr< Widget > removeWidget (Widget *widget)
 
std::unique_ptr< WWidgetremoveFromParent ()
 
void setWidth (const WLength &width)
 
void setHeight (const WLength &height)
 
virtual void positionAt (const WWidget *widget, Orientation orientation=Orientation::Vertical)
 
virtual void toggleStyleClass (const WString &styleClass, bool add, bool force=false)
 
std::string jsRef () const
 
void setFocus ()
 
virtual void acceptDrops (const std::string &mimeType, const WString &hoverStyleClass=WString())
 
virtual void stopAcceptDrops (const std::string &mimeType)
 
virtual void htmlText (std::ostream &out)
 
bool isRendered () const
 
void hide ()
 
void animateHide (const WAnimation &animation)
 
void show ()
 
void animateShow (const WAnimation &animation)
 
void enable ()
 
void disable ()
 
bool layoutSizeAware () const
 
- Public Member Functions inherited from Wt::WObject
void addChild (std::unique_ptr< WObject > child)
 
Child * addChild (std::unique_ptr< Child > child)
 
std::unique_ptr< WObjectremoveChild (WObject *child)
 
std::unique_ptr< Child > removeChild (Child *child)
 
virtual std::string objectName () const
 
void resetLearnedSlots ()
 
void resetLearnedSlot (void(T::*method)())
 
WStatelessSlot * implementStateless (void(T::*method)())
 
WStatelessSlot * implementStateless (void(T::*method)(), void(T::*undoMethod)())
 
void isNotStateless ()
 
WStatelessSlot * implementJavaScript (void(T::*method)(), const std::string &jsCode)
 
- Public Member Functions inherited from Wt::Core::observable
 observable () noexcept
 
virtual ~observable ()
 
auto bindSafe (void(C::*method)(Args...)) noexcept
 
auto bindSafe (void(C::*method)(Args...) const) const noexcept
 
auto bindSafe (const Function &function) noexcept
 

Private Member Functions

std::string imageExtension (const std::string &fileName)
 

Private Attributes

WModelIndex index_
 The index that is currently displayed. More...
 
Wt::ItemDataRole fileNameRole_
 The role that is currently displayed. More...
 
Wt::ItemDataRole contentRole_
 
Wt::ItemDataRole filePathRole_
 
std::shared_ptr< WMemoryResourceimageResource_
 

Additional Inherited Members

- Public Types inherited from Wt::WObject
typedef void(WObject::* Method) ()
 
- Static Public Member Functions inherited from Wt::WWebWidget
static std::string jsStringLiteral (const std::string &v, char delimiter='\'')
 
- Static Public Member Functions inherited from Wt::WWidget
static WString tr (const char *key)
 
- Protected Member Functions inherited from Wt::WWebWidget
virtual void propagateSetEnabled (bool enabled) override
 
virtual void enableAjax () override
 
virtual WStatelessSlot * getStateless (Method method) override
 
- Protected Member Functions inherited from Wt::WWidget
void setLayoutSizeAware (bool sizeAware)
 
virtual void layoutSizeChanged (int width, int height)
 
 WWidget ()
 
virtual void dropEvent (WDropEvent dropEvent)
 
virtual int boxPadding (Orientation orientation) const
 
virtual int boxBorder (Orientation orientation) const
 
void scheduleRender (WFlags< RepaintFlag > flags=None)
 

Detailed Description

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 28 of file SourceView.h.

Constructor & Destructor Documentation

◆ SourceView()

SourceView::SourceView ( ItemDataRole  fileNameRole,
ItemDataRole  contentRole,
ItemDataRole  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.

23  : fileNameRole_(fileNameRole),
24  contentRole_(contentRole),
25  filePathRole_(filePathRole),
27 {}
Wt::ItemDataRole filePathRole_
Definition: SourceView.h:66
Wt::ItemDataRole fileNameRole_
The role that is currently displayed.
Definition: SourceView.h:64
std::shared_ptr< WMemoryResource > imageResource_
Definition: SourceView.h:68
Wt::ItemDataRole contentRole_
Definition: SourceView.h:65

◆ ~SourceView()

SourceView::~SourceView ( )
virtual

Destructor.

Definition at line 29 of file SourceView.C.

30 { }

Member Function Documentation

◆ imageExtension()

std::string SourceView::imageExtension ( const std::string &  fileName)
private

Definition at line 203 of file SourceView.C.

204 {
205  static const char *imageExtensions[] = {
206  ".png", ".gif", ".jpg", "jpeg", ".ico", 0
207  };
208 
209  fs::path p(fileName);
210  std::string extension = fs::extension(p);
211 
212  for (const char **s = imageExtensions; *s != 0; ++s)
213  if (*s == extension)
214  return extension.substr(1);
215 
216  return std::string();
217 }

◆ renderView()

std::unique_ptr< 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 97 of file SourceView.C.

98 {
99  if (!index_.isValid()) {
100  // no content
101  auto result = std::make_unique<WText>();
102  result->setInline(false);
103  return std::move(result);
104  }
105 
106  /*
107  * read the contents, from string or file name
108  */
109  cpp17::any contentsData = index_.data(contentRole_);
110  std::string content;
111  if (cpp17::any_has_value(contentsData))
112  content = asString(contentsData).toUTF8();
113  cpp17::any fileNameData = index_.data(fileNameRole_);
114  std::string fileName =
115  asString(fileNameData).toUTF8();
116  cpp17::any filePathData = index_.data(filePathRole_);
117  std::string filePath;
118  if (cpp17::any_has_value(filePathData))
119  filePath = asString(filePathData).toUTF8();
120 
121  /*
122  * determine source language, for source highlight
123  */
124  std::string lang = getLanguageFromFileExtension(fileName);
125  if (content != "" && content.substr(0, 100).find("-*- C++ -*-")
126  != std::string::npos)
127  lang = "cpp";
128 
129  std::string outputFileName;
130 
131  if (lang != "") {
132  std::string inputFileName;
133 
134  if (cpp17::any_has_value(filePathData))
135  inputFileName = filePath;
136  else {
137  inputFileName = tempFileName();
138  std::ofstream out(inputFileName.c_str(),
139  std::ios::out | std::ios::binary);
140  out.write(content.c_str(), (std::streamsize)content.length());
141  out.close();
142  }
143 
144  outputFileName = tempFileName();
145 
146  std::string sourceHighlightCommand = "source-highlight ";
147  sourceHighlightCommand += "--src-lang=" + lang + " ";
148  sourceHighlightCommand += "--out-format=xhtml ";
149  sourceHighlightCommand += "--input=" + inputFileName + " ";
150  sourceHighlightCommand += "--output=" + outputFileName + " ";
151 
152  std::cerr << sourceHighlightCommand << std::endl;
153  bool sourceHighlightOk = system(sourceHighlightCommand.c_str()) == 0;
154 
155  if (sourceHighlightOk)
156  content = readFileToString(outputFileName);
157  else {
158  content = readFileToString(inputFileName);
159  lang = "";
160  }
161  unlink(outputFileName.c_str());
162 
163  if (!cpp17::any_has_value(filePathData))
164  unlink(inputFileName.c_str());
165  }
166 
167  if (content == "")
168  // do not load binary files, we would need to perform proper UTF-8
169  // transcoding to display them
170  if (!boost::iends_with(fileName, ".jar")
171  && !boost::iends_with(fileName, ".war")
172  && !boost::iends_with(fileName, ".class"))
173  content = readFileToString(fileName);
174 
175  std::unique_ptr<WWidget> result;
176 
177  if (!imageExtension(fileName).empty()) {
178  std::unique_ptr<WImage> image(std::make_unique<WImage>());
179  imageResource_ = std::make_shared<WMemoryResource>();
180  imageResource_->setMimeType("mime/" + imageExtension(fileName));
181  imageResource_->setData((const unsigned char*)content.data(),
182  (int)content.length());
183  image->setImageLink(WLink(imageResource_));
184  result = std::move(image);
185  } else if (lang != "") {
186  auto text = std::make_unique<WText>();
187  text->setTextFormat(TextFormat::UnsafeXHTML);
188  text->setText(content);
189  result = std::move(text);
190  } else {
191  auto text = std::make_unique<WText>();
192  text->setTextFormat(TextFormat::Plain);
193  text->setText(content);
194  result = std::move(text);
195  }
196 
197  result->setInline(false);
198  WApplication::instance()
199  ->doJavaScript(result->jsRef() + ".parentNode.scrollTop = 0;");
200  return std::move(result);
201 }
std::string readFileToString(const std::string &fileName)
Definition: SourceView.C:85
std::string getLanguageFromFileExtension(const std::string &fileName)
Definition: SourceView.C:65
std::string tempFileName()
Definition: SourceView.C:50
WModelIndex index_
The index that is currently displayed.
Definition: SourceView.h:61
std::string imageExtension(const std::string &fileName)
Definition: SourceView.C:203
bool isValid() const
cpp17::any data(ItemDataRole role=ItemDataRole::Display) const
std::string toUTF8() const
WString asString(const cpp17::any &v, const WString &formatString=WString())

◆ setIndex()

bool SourceView::setIndex ( const 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 32 of file SourceView.C.

33 {
34  if (index != index_ && index.isValid()) {
35  std::string fp = !cpp17::any_has_value(index.data(filePathRole_)) ? std::string()
36  : asString(index.data(filePathRole_)).toUTF8();
37 
38  if (cpp17::any_has_value(index.data(contentRole_))
39  || (!fp.empty() && !fs::is_directory(fp))) {
40  index_ = index;
41  update();
42 
43  return true;
44  }
45  }
46 
47  return false;
48 }

Member Data Documentation

◆ contentRole_

Wt::ItemDataRole SourceView::contentRole_
private

Definition at line 65 of file SourceView.h.

◆ fileNameRole_

Wt::ItemDataRole SourceView::fileNameRole_
private

The role that is currently displayed.

Definition at line 64 of file SourceView.h.

◆ filePathRole_

Wt::ItemDataRole SourceView::filePathRole_
private

Definition at line 66 of file SourceView.h.

◆ imageResource_

std::shared_ptr<WMemoryResource> SourceView::imageResource_
private

Definition at line 68 of file SourceView.h.

◆ index_

WModelIndex SourceView::index_
private

The index that is currently displayed.

Definition at line 61 of file SourceView.h.


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