SGL
gconsolewindow.h
1 /*
2  * File: gconsolewindow.h
3  * ----------------------
4  * This file describes the GConsoleWindow class, which is the class used to
5  * represent the graphical console.
6  * The class is implemented as a singleton which can be accessed using the
7  * static method GConsoleWindow::instance().
8  *
9  * @author Marty Stepp
10  * @version 2021/04/03
11  * - removed dependency on custom collections
12  * @version 2019/04/25
13  * - added hasInputScript
14  * @version 2019/04/10
15  * - toolbar support with icons from icon strip image
16  * @version 2018/09/23
17  * - added getFont
18  * @version 2018/09/07
19  * - added doc comments for new documentation generation
20  * @version 2018/08/23
21  * - initial version, separated out from console .cpp/h
22  */
23 
24 
25 #ifndef _gconsolewindow_h
26 #define _gconsolewindow_h
27 
28 #include <iostream>
29 #include <list>
30 #include <sstream>
31 #include <string>
32 #include <vector>
33 #include <Qt>
34 #include <QMutex>
35 #include <QReadWriteLock>
36 #include <QTextFrame>
37 
38 #include "gevent.h"
39 #include "gtextarea.h"
40 #include "gtypes.h"
41 #include "gthread.h"
42 #include "gwindow.h"
43 #include "consolestreambuf.h"
44 
57 class GConsoleWindow : public GWindow {
58 public:
59  static bool consoleEnabled();
60  static string getDefaultFont();
61  static GConsoleWindow* instance();
62  static bool isInitialized();
63  static void setConsoleEnabled(bool enabled);
64 
65  virtual void clearConsole();
66  virtual void clipboardCopy();
67  virtual void clipboardCut();
68  virtual void clipboardPaste();
69  void close() override;
70  virtual void compareOutput(const string& filename);
71  virtual string getAllOutput() const;
72  string getBackground() const override;
73  int getBackgroundInt() const override;
74  string getColor() const override;
75  int getColorInt() const override;
76  virtual string getErrorColor() const;
77  string getFont() const override;
78  string getForeground() const override;
79  int getForegroundInt() const override;
80  virtual string getOutputColor() const;
81  virtual string getUserInputColor() const;
82  virtual bool hasInputScript() const;
83  virtual bool isClearEnabled() const;
84  virtual bool isEcho() const;
85  virtual bool isLocationSaved() const;
86  virtual bool isLocked() const;
87  virtual void loadConfiguration();
88  virtual void loadInputScript(int number);
89  virtual void loadInputScript(const string& filename);
90  virtual void print(const string& str, bool isStdErr = false);
91  virtual void println(bool isStdErr = false);
92  virtual void println(const string& str, bool isStdErr = false);
93  virtual string readLine();
94  virtual void save();
95  virtual void saveAs(const string& filename = "");
96  virtual void saveConfiguration(bool prompt = true);
97  virtual void selectAll();
98  void setBackground(int color) override;
99  void setBackground(const string& color) override;
100  virtual void setClearEnabled(bool clearEnabled);
101  virtual void setConsoleSize(double width, double height);
102  void setColor(int color) override;
103  void setColor(const string& color) override;
104  virtual void setEcho(bool echo);
105  virtual void setErrorColor(const string& errorColor);
106  void setFont(const QFont& font) override;
107  void setFont(const string& font) override;
108  void setForeground(int color) override;
109  void setForeground(const string& color) override;
110  virtual void setLocationSaved(bool locationSaved);
111  virtual void setLocked(bool locked);
112  virtual void setOutputColor(int rgb);
113  virtual void setOutputColor(const string& outputColor);
114  void setSize(double width, double height) override;
115  virtual void setUserInputColor(const string& userInputColor);
116  virtual void showAboutDialog();
117  virtual void showColorDialog(bool background = false);
118  virtual void showCompareOutputDialog();
119  virtual void showFontDialog();
120  virtual void showInputScriptDialog();
121  virtual void showPrintDialog();
122  virtual void shutdown(const string& reason);
123 
124 private:
125  static const bool ALLOW_RICH_INPUT_EDITING;
126  static const double DEFAULT_WIDTH;
127  static const double DEFAULT_HEIGHT;
128  static const double DEFAULT_X;
129  static const double DEFAULT_Y;
130  static const string CONFIG_FILE_NAME;
131  static const string DEFAULT_FONT_FAMILY;
132  static const string DEFAULT_FONT_WEIGHT;
133  static const int DEFAULT_FONT_SIZE;
134  static const int MIN_FONT_SIZE;
135  static const int MAX_FONT_SIZE;
136  static const string DEFAULT_ERROR_COLOR;
137  static const string DEFAULT_ERROR_COLOR_DARK_MODE;
138  static const string DEFAULT_USER_INPUT_COLOR;
139  static const string DEFAULT_USER_INPUT_COLOR_DARK_MODE;
140  static GConsoleWindow* _instance;
141  static bool _consoleEnabled;
142 
143  Q_DISABLE_COPY(GConsoleWindow)
144 
145  GConsoleWindow();
146  ~GConsoleWindow() override;
147  void _initMenuBar();
148  void _initWidgets();
149  void _initStreams();
150  QTextFragment getUserInputFragment() const;
151  int getUserInputStart() const;
152  int getUserInputEnd() const;
153  bool isCursorInUserInputArea() const;
154  bool isSelectionInUserInputArea() const;
155  void processBackspace(int key);
156  void processCommandHistory(int delta);
157  void processEof();
158  void processKeyPress(GEvent event);
159  void processUserInputEnterKey();
160  void processUserInputKey(int key);
161  void setUserInput(const string& userInput);
162 
163  GTextArea* _textArea;
164  bool _clearEnabled;
165  bool _echo;
166  bool _locationSaved;
167  bool _locked;
168  bool _promptActive;
169  bool _shutdown;
170  int _commandHistoryIndex;
171  string _errorColor;
172  string _outputColor;
173  string _userInputColor;
174  string _inputBuffer;
175  string _lastSaveFileName;
176  std::queue<string> _inputLines;
177  std::queue<string> _inputScript;
178  std::vector<string> _inputCommandHistory;
179  sgl::qtgui::ConsoleStreambufQt* _cinout_new_buf;
180  sgl::qtgui::ConsoleStreambufQt* _cerr_new_buf;
181  std::streambuf* _cin_old_buf;
182  std::streambuf* _cout_old_buf;
183  std::streambuf* _cerr_old_buf;
184  std::ostringstream _allOutputBuffer;
185  QReadWriteLock _cinMutex;
186  QReadWriteLock _cinQueueMutex;
187  QMutex _coutMutex;
188 };
189 
190 #endif // _gconsolewindow_h
virtual void setSize(double width, double height)
Sets the window&#39;s total width and height in pixels.
Definition: gwindow.cpp:1196
virtual int getColorInt() const
Returns the current foreground outline color of the interactor as an RGB integer. ...
Definition: gdrawingsurface.cpp:246
virtual string getBackground() const
Returns the current background color of the interactor as a string.
Definition: gdrawingsurface.cpp:222
virtual string getForeground() const
Returns the current foreground outline color of the interactor as a string.
Definition: gdrawingsurface.cpp:278
virtual string getColor() const
Returns the current foreground outline color of the interactor as a string.
Definition: gdrawingsurface.cpp:238
A GEvent represents a user action that has occurred on a graphical interactor.
Definition: gevent.h:153
virtual void setForeground(int color)
Sets the current foreground outline color of the interactor as an RGB integer.
Definition: gdrawingsurface.cpp:439
static const int DEFAULT_WIDTH
The default width of a newly created window in pixels if its width is not explicitly specified...
Definition: gwindow.h:125
virtual int getBackgroundInt() const
Returns the current background color of the interactor as an RGB integer.
Definition: gdrawingsurface.cpp:230
static const int DEFAULT_HEIGHT
The default height of a newly created window in pixels if its height is not explicitly specified...
Definition: gwindow.h:131
virtual string getFont() const
Returns the current text font of the interactor as a font string.
Definition: gdrawingsurface.cpp:270
virtual int getForegroundInt() const
Returns the current foreground outline color of the interactor as an RGB integer. ...
Definition: gdrawingsurface.cpp:282
A GTextArea is a multi-line editable text box.
Definition: gtextarea.h:33
void setBackground(int color) override
Sets the current background color of the interactor as an RGB integer.
Definition: gwindow.cpp:1024
This class represents a graphics window that supports simple graphics.
Definition: gwindow.h:98
virtual void close()
Closes the window.
Definition: gwindow.cpp:595