Wt examples  4.11.3
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 26 of file SourceView.C.

29  : fileNameRole_(fileNameRole),
30  contentRole_(contentRole),
31  filePathRole_(filePathRole),
33 {}
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 35 of file SourceView.C.

36 { }

Member Function Documentation

◆ imageExtension()

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

Definition at line 209 of file SourceView.C.

210 {
211  static const char *imageExtensions[] = {
212  ".png", ".gif", ".jpg", "jpeg", ".ico", 0
213  };
214 
215  fs::path p(fileName);
216  std::string extension = p.extension().string();
217 
218  for (const char **s = imageExtensions; *s != 0; ++s)
219  if (*s == extension)
220  return extension.substr(1);
221 
222  return std::string();
223 }

◆ 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 103 of file SourceView.C.

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

39 {
40  if (index != index_ && index.isValid()) {
41  std::string fp = !cpp17::any_has_value(index.data(filePathRole_)) ? std::string()
42  : asString(index.data(filePathRole_)).toUTF8();
43 
44  if (cpp17::any_has_value(index.data(contentRole_))
45  || (!fp.empty() && !fs::is_directory(fp))) {
46  index_ = index;
47  update();
48 
49  return true;
50  }
51  }
52 
53  return false;
54 }

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: