SGL
gbrowserpane.h
1 /*
2  * File: gbrowserpane.h
3  * --------------------
4  * This file declares the <code>GBrowserPane</code> class, which is a
5  * graphical interactor that displays a web page.
6  *
7  * @version 2021/04/09
8  * - added sgl namespace
9  * @version 2019/04/23
10  * - moved some event-handling code to GInteractor superclass
11  * @version 2018/12/28
12  * - added methods for text selection, scrolling, cursor position, key/mouse listeners
13  * @version 2018/09/17
14  * - fixed thread safety bugs
15  * - added link listener events
16  * @version 2018/09/08
17  * - added doc comments for new documentation generation
18  * @version 2018/08/23
19  * - renamed to gbrowserpane.h to replace Java version
20  * @version 2018/07/15
21  * - initial version
22  */
23 
24 #ifndef _gbrowserpane_h
25 #define _gbrowserpane_h
26 
27 #include <string>
28 #include <QTextBrowser>
29 #include "ginteractor.h"
30 
31 namespace sgl {
32 
33 class _Internal_QTextBrowser;
34 
44 class GBrowserPane : public GInteractor {
45 public:
51  GBrowserPane(const string& url = "", QWidget* parent = nullptr);
52 
56  ~GBrowserPane() override;
57 
61  virtual void clearSelection();
62 
66  virtual void clearText();
67 
74  virtual string getContentType() const;
75 
80  virtual int getCursorPosition() const;
81 
82  /* @inherit */
83  _Internal_QWidget* getInternalWidget() const override;
84 
89  virtual string getPageUrl() const;
90 
95  virtual string getSelectedText() const;
96 
102  virtual int getSelectionEnd() const;
103 
108  virtual int getSelectionLength() const;
109 
115  virtual int getSelectionStart() const;
116 
122  virtual string getText() const;
123 
124  /* @inherit */
125  string getType() const override;
126 
127  /* @inherit */
128  QWidget* getWidget() const override;
129 
134  virtual bool isEditable() const;
135 
140  virtual bool isLineWrap() const;
141 
146  virtual void moveCursorToEnd();
147 
152  virtual void moveCursorToStart();
153 
161  virtual void readTextFromFile(std::istream& file);
162 
170  virtual void readTextFromFile(const string& filename);
171 
177  virtual void readTextFromUrl(const string& url);
178 
183  virtual void removeLinkListener();
184 
189  virtual void removeTextChangeListener();
190 
195  virtual void scrollToBottom();
196 
201  virtual void scrollToTop();
202 
209  virtual void select(int startIndex, int length);
210 
214  virtual void selectAll();
215 
224  virtual void setContentType(const string& contentType);
225 
231  virtual void setCursorPosition(int index, bool keepAnchor = false);
232 
237  virtual void setEditable(bool value);
238 
243  virtual void setLineWrap(bool wrap);
244 
250  virtual void setLinkListener(GEventListener func);
251 
257  virtual void setLinkListener(GEventListenerVoid func);
258 
264  void setMouseListener(GEventListener func) override;
265 
271  void setMouseListener(GEventListenerVoid func) override;
272 
278  virtual void setText(const string& text);
279 
294  virtual void setTextChangeListener(GEventListener func);
295 
310  virtual void setTextChangeListener(GEventListenerVoid func);
311 
312 private:
313  Q_DISABLE_COPY(GBrowserPane)
314 
315  string _pageUrl; // url/filename of the most recently loaded page
316  string _contentType;
317  _Internal_QTextBrowser* _iqtextbrowser;
318 
319  friend class _Internal_QTextBrowser;
320 };
321 
326 class _Internal_QTextBrowser : public QTextBrowser, public _Internal_QWidget {
327  Q_OBJECT
328 
329 public:
330  _Internal_QTextBrowser(GBrowserPane* gbrowserpane, QWidget* parent = nullptr);
331  void detach() override;
332  QVariant loadResource(int type, const QUrl &url) override;
333  void mousePressEvent(QMouseEvent* event) override;
334  void mouseReleaseEvent(QMouseEvent* event) override;
335  QSize sizeHint() const override;
336 
337 private:
338  GBrowserPane* _gbrowserpane;
339  QString _clickedLink;
340 };
341 
342 } // namespace sgl
343 
344 #endif // _gbrowserpane_h
QWidget* getWidget() const override
Returns a direct pointer to the internal Qt widget being wrapped by this interactor.
Definition: gbrowserpane.cpp:130
virtual bool isLineWrap() const
Returns whether the text pane wraps its text when a line becomes too long.
Definition: gbrowserpane.cpp:138
virtual void scrollToBottom()
Moves the visible scroll region of the text pane so that the bottom part of the text is visible...
Definition: gbrowserpane.cpp:290
virtual int getSelectionEnd() const
Returns the index just past the end of the current selection of text as a 0-based character index wit...
Definition: gbrowserpane.cpp:92
virtual void setLinkListener(GEventListener func)
Sets a link listener on this canvas so that it will be called when the user clicks on hyperlinks on t...
Definition: gbrowserpane.cpp:359
virtual string getPageUrl() const
Returns the URL of the web page or file name being currently viewed.
Definition: gbrowserpane.cpp:77
virtual void setText(string text)
Sets the pane to display to the given contents using its current content type.
Definition: gbrowserpane.cpp:367
virtual bool isEditable() const
Returns whether the text pane allows the user to modify its text.
Definition: gbrowserpane.cpp:134
string getType() const override
Returns a string representing the class name of this interactor, such as "GButton" or "GCheckBox"...
Definition: gbrowserpane.cpp:126
~GBrowserPane() override
Frees memory allocated internally by the browser pane.
Definition: gbrowserpane.cpp:45
virtual void moveCursorToEnd()
Sets the text pane&#39;s keyboard cursor position to the end of the current text.
Definition: gbrowserpane.cpp:142
virtual void setLineWrap(bool wrap)
Sets whether the text pane wraps its text when a line becomes too long.
Definition: gbrowserpane.cpp:353
std::function< void(GEvent)> GEventListener
Types for the event listener functions to be passed to various interactors.
Definition: gevent.h:38
GBrowserPane(string url="", QWidget* parent=nullptr)
Constructs a new browser pane.
Definition: gbrowserpane.cpp:35
Definition: console.h:45
_Internal_QWidget* getInternalWidget() const override
Returns a direct pointer to the internal Qt widget being wrapped by this interactor.
Definition: gbrowserpane.cpp:73
This abstract class is the superclass for all graphical interactors.
Definition: ginteractor.h:52
virtual void select(int startIndex, int length)
Sets the given range of text to be selected, beginning with the given start index as a 0-based charac...
Definition: gbrowserpane.cpp:306
void setMouseListener(GEventListener func) override
Sets a mouse listener on this text pane so that it will be called when the user moves or clicks the m...
Definition: gbrowserpane.cpp:343
virtual void setEditable(bool value)
Sets whether the text pane allows the user to modify its text.
Definition: gbrowserpane.cpp:337
virtual void readTextFromUrl(string url)
Reads text from the given web page URL and displays the entire page&#39;s text as the contents of this fo...
Definition: gbrowserpane.cpp:274
virtual void readTextFromFile(std::istream &file)
Reads text from the given file and displays the entire file&#39;s text as the contents of this formatted ...
Definition: gbrowserpane.cpp:160
std::function< void()> GEventListenerVoid
Types for the event listener functions to be passed to various interactors.
Definition: gevent.h:44
virtual void removeLinkListener()
Removes the link listener from the canvas so that it will no longer call it when hyperlink events occ...
Definition: gbrowserpane.cpp:282
virtual void clearText()
Sets the text in the pane to be empty.
Definition: gbrowserpane.cpp:59
virtual void setContentType(string contentType)
Sets the MIME content type being used to display the current/future pages.
Definition: gbrowserpane.cpp:323
virtual string getSelectedText() const
Returns the text that is currently selected in the text pane.
Definition: gbrowserpane.cpp:81
virtual void clearSelection()
Deselects any text that is currently selected in the pane.
Definition: gbrowserpane.cpp:51
virtual void setCursorPosition(int index, bool keepAnchor=false)
Moves the keyboard cursor to the given 0-based character index within the text.
Definition: gbrowserpane.cpp:327
virtual string getContentType() const
Returns the MIME content type for the current page.
Definition: gbrowserpane.cpp:65
virtual int getCursorPosition() const
Returns the keyboard cursor&#39;s current position in the text pane as a 0-based character index within t...
Definition: gbrowserpane.cpp:69
virtual void selectAll()
Selects the entire text of the text pane.
Definition: gbrowserpane.cpp:317
virtual void removeTextChangeListener()
Removes the text change listener from this text pane so that it will no longer call it when the user ...
Definition: gbrowserpane.cpp:286
virtual void scrollToTop()
Moves the visible scroll region of the text pane so that the top part of the text is visible...
Definition: gbrowserpane.cpp:298
virtual void moveCursorToStart()
Sets the text pane&#39;s keyboard cursor position to the start of the current text.
Definition: gbrowserpane.cpp:151
virtual int getSelectionStart() const
Returns the index of the start of the current selection of text as a 0-based character index within t...
Definition: gbrowserpane.cpp:111
virtual int getSelectionLength() const
Returns the number of characters that are currently selected.
Definition: gbrowserpane.cpp:104
virtual string getText() const
Returns the full text of the current page or file being displayed in the pane.
Definition: gbrowserpane.cpp:122
virtual void setTextChangeListener(GEventListener func)
Sets a text change listener on this text pane so that it will be called when the user modifies the cu...
Definition: gbrowserpane.cpp:373
A GBrowserPane is a graphical interactor that displays a web page.
Definition: gbrowserpane.h:44