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