MIRA
QConsole.h
Go to the documentation of this file.
1 /* -*- mode: c++ -*- */
2 /***************************************************************************
3 qconsole.h - description
4  -------------------
5 begin : mar mar 15 2005
6 copyright : (C) 2005 by Houssem BDIOUI
7 email : houssem.bdioui@gmail.com
8  ***************************************************************************/
9 
10 // Source: http://sourceforge.net/projects/qconsole/
11 // migrated to Qt4 by YoungTaek Oh. date: Nov 29, 2010
12 // adapted to MIRA and removed unnecessary bloat (e.g. saved script, etc)
13 
14 /***************************************************************************
15  * *
16  * This program is free software; you can redistribute it and/or modify *
17  * it under the terms of the GNU General Public License as published by *
18  * the Free Software Foundation; either version 2 of the License, or *
19  * (at your option) any later version. *
20  * *
21  ***************************************************************************/
22 
23 #ifndef QCONSOLE_H
24 #define QCONSOLE_H
25 
26 #include <QStringList>
27 #include <QTextEdit>
28 #include <QMouseEvent>
29 #include <QKeyEvent>
30 #include <QMenu>
31 
32 #include <QDialog>
33 #include <QListWidget>
34 #include <QDebug>
35 
36 #include <utils/ExtensibleEnum.h>
38 
39 namespace mira {
40 
42 
53 {
54  Q_OBJECT
55 
56 public:
57  QConsolePopupCompleter(const QStringList&, QWidget *parent = 0);
58  virtual ~QConsolePopupCompleter();
59 
60 public:
61  QString selected(void) { return selected_; }
62  int exec(QTextEdit*);
63 
64 protected:
65  virtual void showEvent(QShowEvent*);
66 
67 private Q_SLOTS:
68  void onItemActivated(QListWidgetItem*);
69 
70 public:
71  QListWidget *listWidget_;
72  QString selected_;
73 };
74 
79 class MIRA_GUI_WIDGETS_EXPORT QConsole : public QTextEdit
80 {
81  Q_OBJECT
82 public:
83  //constructor
84  QConsole(QWidget *parent = NULL, const QString &welcomeText = "");
85  //set the prompt of the console
86  void setPrompt(const QString &prompt, bool display = true);
87  //execCommand(QString) executes the command and displays back its result
88  bool execCommand(const QString &command, bool writeCommand = true,
89  bool showPrompt = true, QString *result = NULL);
90  void clear();
91  void reset(const QString &welcomeText = "");
92 
93  //cosmetic methods !
94 
95  // @{
97  QColor cmdColor() const { return cmdColor_; }
98  void setCmdColor(QColor c) {cmdColor_ = c;}
99  // @}
100 
101  // @{
103  QColor errColor() const { return errColor_; }
104  void setErrColor(QColor c) {errColor_ = c;}
105  // @}
106 
107  // @{
109  QColor outColor() const { return outColor_; }
110  void setOutColor(QColor c) {outColor_ = c;}
111  // @}
112  void setCompletionColor(QColor c) {completionColor = c;}
113 
114  // @{
116  void setFont(const QFont& f);
117  QFont font() const { return currentFont(); }
118  // @}
119 
120 protected:
121 
122  // interface for derived console classes to write some output
123  void setFormat(QColor color, bool bold=false, bool italic=false);
124  void setFormat(bool bold=false, bool italic=false);
125  void println(const QString& s);
126  void println(const std::string& s);
127  void println(const char* s);
128 
129 
130 protected:
131  void keyPressEvent(QKeyEvent * e);
132  void contextMenuEvent( QContextMenuEvent * event);
133 
134  //Return false if the command is incomplete (e.g. unmatched braces)
135  virtual bool isCommandComplete(const QString &command);
136  //Get the command to validate
137  QString getCurrentCommand();
138 
139  //Replace current command with a new one
140  void replaceCurrentCommand(const QString &newCommand);
141 
142  //Test whether the cursor is in the edition zone
143  bool isInEditionZone();
144  bool isInEditionZone(int pos);
145 
146  //Test whether the selection is in the edition zone
147  bool isSelectionInEditionZone();
148  //Change paste behaviour
149  void insertFromMimeData(const QMimeData *);
150 
151 
152  //protected attributes
153 protected:
154  //colors
155  QColor cmdColor_, errColor_, outColor_, completionColor;
156 
158  // cached prompt length
160  // The prompt string
161  QString prompt;
162  // The commands history
163  QStringList history;
164  // Current history index (needed because afaik QStringList does not have such an index)
166  //Holds position of the prompt
168 
169 protected:
170  virtual void dragEnterEvent(QDragEnterEvent *e);
171  virtual void dragLeaveEvent(QDragLeaveEvent *e);
172  virtual void dragMoveEvent(QDragMoveEvent *e);
173  virtual void dropEvent(QDropEvent *e);
174 
175  void mousePressEvent(QMouseEvent*);
176  void mouseReleaseEvent(QMouseEvent*);
177 
178  //execute a validated command (should be reimplemented and called at the end)
179  //the return value of the function is the string result
180  //res must hold back the return value of the command (0: passed; else: error)
181  virtual QString interpretCommand(const QString &command, int *res);
182 
183  //give suggestions to autocomplete a command (should be reimplemented)
184  //the return value of the function is the string list of all suggestions
185  //the returned prefix is useful to complete "sub-commands"
186  virtual QStringList suggestCommand(const QString &cmd, QString &prefix);
187 
188 
189 public slots:
190  //Contextual menu slots
191  void cut();
192 
193  //displays the prompt
194  void displayPrompt();
195 
196  void onCursorPositionChanged();
197 
198 Q_SIGNALS:
199  //Signal emitted after that a command is executed
200  void commandExecuted(const QString &command);
201 
202 private:
203  void handleTabKeyPress();
204  void handleReturnKeyPress();
205  void handleUpKeyPress();
206  void handleDownKeyPress();
207  void setHome();
208 
209 protected:
210 
212  SEARCH_NONE, SEARCH_HISTORY)
213 
214 protected: // Specific search stuff, to be reimplemented in subclasses for own search capabilities
215  virtual const SearchMode& searchModeCommand(QKeyEvent* event);
216  virtual void enterSearchMode(const SearchMode& mode);
217  virtual QString getSearchModeDescriptor();
218  virtual QStringList getSearchList();
219  // distinguish between string used for comparison (=first) and the respective command (=second)
220  virtual std::multimap<QString, QString> getExtendedSearchList();
221 
222 protected: // (General) Search stuff
223  SearchMode currentSearchMode;
224 
225 private:
226  bool handleSearchKeyEvent(QKeyEvent* event); // return true if event was handled
227 
228  void overwriteCommand(QString command);
229  void clearCurrentLine();
230  void setCursorToEditZone();
231 
232  void resetSearch();
233  void updateSearchMatches();
234  void updateSearchCursor(QString query, QString result, int index, int numResults);
235 
236  void handleSearch(const SearchMode& mode);
237  void handleSearchInput(QKeyEvent* event);
238  void handleSearchReturnKey();
239  void handleSearchEscapeKey();
240 
241  QString searchInput;
242 
243  QStringList searchMatches;
244  int searchMatchIndex;
245 
246  // Store the previous prompt as we will have to restore it
247  QString previousPrompt;
248 };
249 
251 }
252 #endif
#define MIRA_GUI_WIDGETS_EXPORT
Definition: GuiWidgetsExports.h:61
QListWidget * listWidget_
Definition: QConsole.h:71
A replacement for enum that can be extended in subclasses.
int historyIndex
Definition: QConsole.h:165
void setCmdColor(QColor c)
Definition: QConsole.h:98
An abstract Qt console.
Definition: QConsole.h:79
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
QStringList history
Definition: QConsole.h:163
int promptPosition
Definition: QConsole.h:167
QString selected_
Definition: QConsole.h:72
Popup Completer class.
Definition: QConsole.h:52
GUI export macro declaration.
QColor cmdColor() const
get/set command color
Definition: QConsole.h:97
#define MIRA_EXTENSIBLE_ENUM_DECLARE(NAME, BASE, VALUES...)
Definition: ExtensibleEnum.h:331
SearchMode currentSearchMode
Definition: QConsole.h:223
int oldPosition
Definition: QConsole.h:157
QColor errColor() const
get/set error color
Definition: QConsole.h:103
QColor outColor_
Definition: QConsole.h:155
void setOutColor(QColor c)
Definition: QConsole.h:110
ExtensibleEnum is a base for derived classes that can be extensible &#39;replacements&#39; for enum types...
Definition: ExtensibleEnum.h:285
QString prompt
Definition: QConsole.h:161
QString selected(void)
Definition: QConsole.h:61
void setCompletionColor(QColor c)
Definition: QConsole.h:112
QFont font() const
Definition: QConsole.h:117
QColor outColor() const
get/set output color
Definition: QConsole.h:109
int promptLength
Definition: QConsole.h:159
void setErrColor(QColor c)
Definition: QConsole.h:104