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 2019/04/23
8  * - moved some event-handling code to GInteractor superclass
9  * @version 2018/12/28
10  * - added methods for text selection, scrolling, cursor position, key/mouse listeners
11  * @version 2018/09/17
12  * - fixed thread safety bugs
13  * - added link listener events
14  * @version 2018/09/08
15  * - added doc comments for new documentation generation
16  * @version 2018/08/23
17  * - renamed to gbrowserpane.h to replace Java version
18  * @version 2018/07/15
19  * - initial version
20  */
21 
22 #ifndef _gbrowserpane_h
23 #define _gbrowserpane_h
24 
25 #include <string>
26 #include <QTextBrowser>
27 
28 #include "ginteractor.h"
29 
30 class _Internal_QTextBrowser;
31 
41 class GBrowserPane : public GInteractor {
42 public:
48  GBrowserPane(const string& url = "", QWidget* parent = nullptr);
49 
53  ~GBrowserPane() override;
54 
58  virtual void clearSelection();
59 
63  virtual void clearText();
64 
71  virtual string getContentType() const;
72 
77  virtual int getCursorPosition() const;
78 
79  /* @inherit */
80  _Internal_QWidget* getInternalWidget() const override;
81 
86  virtual string getPageUrl() const;
87 
92  virtual string getSelectedText() const;
93 
99  virtual int getSelectionEnd() const;
100 
105  virtual int getSelectionLength() const;
106 
112  virtual int getSelectionStart() const;
113 
119  virtual string getText() const;
120 
121  /* @inherit */
122  string getType() const override;
123 
124  /* @inherit */
125  QWidget* getWidget() const override;
126 
131  virtual bool isEditable() const;
132 
137  virtual bool isLineWrap() const;
138 
143  virtual void moveCursorToEnd();
144 
149  virtual void moveCursorToStart();
150 
158  virtual void readTextFromFile(std::istream& file);
159 
167  virtual void readTextFromFile(const string& filename);
168 
174  virtual void readTextFromUrl(const string& url);
175 
180  virtual void removeLinkListener();
181 
186  virtual void removeTextChangeListener();
187 
192  virtual void scrollToBottom();
193 
198  virtual void scrollToTop();
199 
206  virtual void select(int startIndex, int length);
207 
211  virtual void selectAll();
212 
221  virtual void setContentType(const string& contentType);
222 
228  virtual void setCursorPosition(int index, bool keepAnchor = false);
229 
234  virtual void setEditable(bool value);
235 
240  virtual void setLineWrap(bool wrap);
241 
247  virtual void setLinkListener(GEventListener func);
248 
254  virtual void setLinkListener(GEventListenerVoid func);
255 
261  void setMouseListener(GEventListener func) override;
262 
268  void setMouseListener(GEventListenerVoid func) override;
269 
275  virtual void setText(const string& text);
276 
291  virtual void setTextChangeListener(GEventListener func);
292 
307  virtual void setTextChangeListener(GEventListenerVoid func);
308 
309 private:
310  Q_DISABLE_COPY(GBrowserPane)
311 
312  string _pageUrl; // url/filename of the most recently loaded page
313  string _contentType;
314  _Internal_QTextBrowser* _iqtextbrowser;
315 
316  friend class _Internal_QTextBrowser;
317 };
318 
323 class _Internal_QTextBrowser : public QTextBrowser, public _Internal_QWidget {
324  Q_OBJECT
325 
326 public:
327  _Internal_QTextBrowser(GBrowserPane* gbrowserpane, QWidget* parent = nullptr);
328  void detach() override;
329  QVariant loadResource(int type, const QUrl &url) override;
330  void mousePressEvent(QMouseEvent* event) override;
331  void mouseReleaseEvent(QMouseEvent* event) override;
332  QSize sizeHint() const override;
333 
334 private:
335  GBrowserPane* _gbrowserpane;
336  QString _clickedLink;
337 };
338 
339 #endif // _gbrowserpane_h
virtual int getSelectionLength() const
Returns the number of characters that are currently selected.
Definition: gbrowserpane.cpp:100
virtual void setLineWrap(bool wrap)
Sets whether the text pane wraps its text when a line becomes too long.
Definition: gbrowserpane.cpp:349
QWidget* getWidget() const override
Returns a direct pointer to the internal Qt widget being wrapped by this interactor.
Definition: gbrowserpane.cpp:126
GBrowserPane(string url="", QWidget* parent=nullptr)
Constructs a new browser pane.
Definition: gbrowserpane.cpp:31
_Internal_QWidget* getInternalWidget() const override
Returns a direct pointer to the internal Qt widget being wrapped by this interactor.
Definition: gbrowserpane.cpp:69
virtual string getSelectedText() const
Returns the text that is currently selected in the text pane.
Definition: gbrowserpane.cpp:77
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:323
virtual void setEditable(bool value)
Sets whether the text pane allows the user to modify its text.
Definition: gbrowserpane.cpp:333
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:369
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:270
virtual string getText() const
Returns the full text of the current page or file being displayed in the pane.
Definition: gbrowserpane.cpp:118
virtual string getContentType() const
Returns the MIME content type for the current page.
Definition: gbrowserpane.cpp:61
virtual void setText(string text)
Sets the pane to display to the given contents using its current content type.
Definition: gbrowserpane.cpp:363
virtual bool isEditable() const
Returns whether the text pane allows the user to modify its text.
Definition: gbrowserpane.cpp:130
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:282
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:107
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:156
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:286
This abstract class is the superclass for all graphical interactors.
Definition: ginteractor.h:48
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:278
virtual void clearSelection()
Deselects any text that is currently selected in the pane.
Definition: gbrowserpane.cpp:47
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:302
virtual bool isLineWrap() const
Returns whether the text pane wraps its text when a line becomes too long.
Definition: gbrowserpane.cpp:134
virtual void moveCursorToStart()
Sets the text pane&#39;s keyboard cursor position to the start of the current text.
Definition: gbrowserpane.cpp:147
virtual void moveCursorToEnd()
Sets the text pane&#39;s keyboard cursor position to the end of the current text.
Definition: gbrowserpane.cpp:138
virtual void selectAll()
Selects the entire text of the text pane.
Definition: gbrowserpane.cpp:313
virtual void clearText()
Sets the text in the pane to be empty.
Definition: gbrowserpane.cpp:55
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:88
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:294
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:65
A GBrowserPane is a graphical interactor that displays a web page.
Definition: gbrowserpane.h:41
~GBrowserPane() override
Frees memory allocated internally by the browser pane.
Definition: gbrowserpane.cpp:41
virtual void setContentType(string contentType)
Sets the MIME content type being used to display the current/future pages.
Definition: gbrowserpane.cpp:319
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:355
virtual string getPageUrl() const
Returns the URL of the web page or file name being currently viewed.
Definition: gbrowserpane.cpp:73
string getType() const override
Returns a string representing the class name of this interactor, such as "GButton" or "GCheckBox"...
Definition: gbrowserpane.cpp:122
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:339