itom  4.1.0
consoleWidget.h
1 /* ********************************************************************
2  itom software
3  URL: http://www.uni-stuttgart.de/ito
4  Copyright (C) 2020, Institut fuer Technische Optik (ITO),
5  Universitaet Stuttgart, Germany
6 
7  This file is part of itom.
8 
9  itom is free software; you can redistribute it and/or modify it
10  under the terms of the GNU Library General Public Licence as published by
11  the Free Software Foundation; either version 2 of the Licence, or (at
12  your option) any later version.
13 
14  itom is distributed in the hope that it will be useful, but
15  WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library
17  General Public Licence for more details.
18 
19  You should have received a copy of the GNU Library General Public License
20  along with itom. If not, see <http://www.gnu.org/licenses/>.
21 *********************************************************************** */
22 
23 #ifndef CONSOLEWIDGET_H
24 #define CONSOLEWIDGET_H
25 
26 #include <queue>
27 
28 #include "common/sharedStructures.h"
29 #include "common/sharedStructuresQt.h"
30 #include "../python/qDebugStream.h"
31 #include "../global.h"
32 
33 #include "abstractCodeEditorWidget.h"
34 #include "../codeEditor/modes/lineBackgroundMarker.h"
35 #include "../codeEditor/modes/pyGotoAssignment.h"
36 #include "../codeEditor/panels/lineNumber.h"
37 
38 #include <QKeyEvent>
39 #include <QDropEvent>
40 #include <qevent.h>
41 #include <qstring.h>
42 #include <qstringlist.h>
43 #include <qdebug.h>
44 #include <qsettings.h>
45 #include <qtimer.h>
46 #include <qelapsedtimer.h>
47 
48 QT_BEGIN_NAMESPACE
49 
50 QT_END_NAMESPACE
51 
52 namespace ito
53 {
54 
55 class DequeCommandList;
56 
58 {
59  Q_OBJECT
60 
61 public:
62  ConsoleWidget(QWidget* parent = NULL);
63  ~ConsoleWidget();
64 
65  static const QString lineBreak;
66  static const QString newCommandPrefix;
67 
68  virtual QString codeText(int &line, int &column) const;
69 
70 protected:
71  virtual void loadSettings();
72  virtual void contextMenuAboutToShow(int contextMenuLine);
73 
74  void initMenus();
75 
76 public slots:
77  virtual void copy();
78  virtual void paste();
79  virtual void cut();
80  void receiveStream(QString text, ito::tStreamMessageType msgType);
81  void pythonRunSelection(QString selectionText);
82  void pythonStateChanged(tPythonTransitions pyTransition);
83  void clearCommandLine();
84  void startInputCommandLine(QSharedPointer<QByteArray> buffer, ItomSharedSemaphore *inputWaitCond);
85 
86 signals:
87  void wantToCopy();
88  void sendToLastCommand(QString cmd);
89  void sendToPythonMessage(QString cmd);
90 
91 
92 protected:
93  virtual bool keyPressInternalEvent(QKeyEvent *event);
94  void dropEvent (QDropEvent *event);
95  void dragEnterEvent (QDragEnterEvent *event);
96  void dragMoveEvent (QDragMoveEvent *event);
97  void wheelEvent(QWheelEvent *event);
98  bool canInsertFromMimeData(const QMimeData *source) const;
99  void mouseDoubleClickEvent(QMouseEvent *e);
100 
101 private slots:
102  void selChanged();
103  void textDoubleClicked(int position, int line, int modifiers);
104  void clearAndStartNewCommand();
105  void toggleAutoWheel(bool enable);
106  void processStreamBuffer();
107 
108 private:
110  {
111  CmdQueueItem() :
112  m_lineBegin(-1),
113  m_nrOfLines(1)
114  {}
115 
116  CmdQueueItem(const QString &text, int lineBegin, int nrOfLines) :
117  m_singleLine(text),
118  m_lineBegin(lineBegin),
119  m_nrOfLines(nrOfLines)
120  {}
121 
122  QString m_singleLine;
123  int m_lineBegin;
124  int m_nrOfLines;
125  };
126 
128  {
129  QString text;
130  ito::tStreamMessageType msgType;
131  };
132 
134  {
135  bool inputModeEnabled;
136  bool calltipsModeCurrentState; //current enable state of calltips mode before starting input text mode (only valid if inputModeEnabled = true)
137  bool autoCompletionModeCurrentState; //current enable state of completion mode before starting input text mode (only valid if inputModeEnabled = true)
138  };
139 
140  RetVal initEditor();
141  RetVal clearEditor();
142  RetVal startNewCommand(bool clearEditorFirst = false);
143  RetVal execCommand(int lineBegin, int lineEnd);
144  RetVal useCmdListCommand(int dir);
145 
146  RetVal executeCmdQueue();
147 
148  void autoLineDelete();
149  void moveCursorToEnd();
151 
152  int checkValidDropRegion(const QPoint &pos);
153 
154  void enableInputTextMode();
155  void disableInputTextMode();
156 
158 
159  DequeCommandList *m_pCmdList;
160 
161  std::queue<CmdQueueItem> m_cmdQueue;
162 
163  bool m_canCopy;
164  bool m_canCut;
165  StreamBuffer m_receiveStreamBuffer;
166  QTimer m_receiveStreamBufferTimer;
167  bool m_splitLongLines;
168  int m_splitLongLinesMaxLength;
169  CmdQueueItem m_currentlyExecutedCommand;
170 
171  QSharedPointer<LineBackgroundMarkerMode> m_markErrorLineMode;
172  QSharedPointer<LineBackgroundMarkerMode> m_markCurrentLineMode;
173  QSharedPointer<LineBackgroundMarkerMode> m_markInputLineMode;
174  QSharedPointer<LineNumberPanel> m_lineNumberPanel;
175 
178 
180 
182  QSharedPointer<QByteArray> m_inputStreamBuffer;
185  bool m_autoWheel;
186 
187  QMap<QString, QAction*> m_contextMenuActions;
188 
189  InputTextMode m_inputTextMode;
190 
191  static const QString longLineWrapPrefix;
192 
193 };
194 
196 {
197 public:
198  DequeCommandList(int maxLength);
199  ~DequeCommandList();
200 
201  RetVal add(const QString &cmd);
202  RetVal moveLast();
203  QString getPrevious();
204  QString getNext();
205 
206 private:
207  int m_maxItems;
208  std::deque<QString> m_cmdList;
209  std::deque<QString>::reverse_iterator m_rit;
210 };
211 
212 } //end namespace ito
213 
214 #endif
bool m_pythonBusy
true: python is executing or debugging a script, a command...
Definition: consoleWidget.h:177
bool m_waitForCmdExecutionDone
true: command in this console is being executed and sends a finish-event, when done.
Definition: consoleWidget.h:176
void autoLineDelete()
delete the first N lines if the command line has more than M (M>=N) lines
Definition: consoleWidget.cpp:1924
int m_inputStartCol
if python-input command is currently used to ask for user-input, this variable holds the column in li...
Definition: consoleWidget.h:184
Definition: consoleWidget.h:133
RetVal startNewCommand(bool clearEditorFirst=false)
< new command is a new line starting with ">>" (newCommandPrefix)
Definition: consoleWidget.cpp:321
Class for managing status values (like errors or warning)
Definition: retVal.h:54
Definition: abstractCodeEditorWidget.h:45
int checkValidDropRegion(const QPoint &pos)
Definition: consoleWidget.cpp:1644
void moveCursorToValidRegion()
Definition: consoleWidget.cpp:1819
Definition: consoleWidget.h:57
Definition: apiFunctionsGraph.cpp:39
ConsoleWidget(QWidget *parent=NULL)
Definition: consoleWidget.cpp:59
virtual bool keyPressInternalEvent(QKeyEvent *event)
reimplementation to process the keyReleaseEvent
Definition: consoleWidget.cpp:405
semaphore which can be used for asychronous thread communication. By using this class it is possible ...
Definition: sharedStructuresQt.h:57
void dragMoveEvent(QDragMoveEvent *event)
Definition: consoleWidget.cpp:1589
Definition: consoleWidget.h:195
QString m_temporaryRemovedCommands
removed commands, if python busy, caused by another console instance or script (we assume that these ...
Definition: consoleWidget.h:179
ItomSharedSemaphore * m_inputStreamWaitCond
if this is != NULL, a input(...) command is currently running in Python and the command line is ready...
Definition: consoleWidget.h:181
std::queue< CmdQueueItem > m_cmdQueue
upcoming events to handle
Definition: consoleWidget.h:161
int m_startLineBeginCmd
zero-based, first-line of actual (not evaluated command), last line which starts with ">>"...
Definition: consoleWidget.h:157
tStreamMessageType
Definition: typeDefs.h:76
void processStreamBuffer()
Definition: consoleWidget.cpp:1295
static const QString lineBreak
< constants
Definition: consoleWidget.h:65
int m_inputStartLine
if python-input command is currently used to ask for user-input, this variable holds the line of the ...
Definition: consoleWidget.h:183
Definition: consoleWidget.h:127
bool m_autoWheel
true if command line should automatically move to the last line if new lines are appended, this is set to false upon a wheel event and will be reset to true if the command line is cleared (clc) or if a new input is added
Definition: consoleWidget.h:185
Definition: consoleWidget.h:109
void dropEvent(QDropEvent *event)
Definition: consoleWidget.cpp:1499