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>
37 
38 namespace mira {
39 
41 
51 class QConsolePopupCompleter : public QDialog
52 {
53  Q_OBJECT
54 
55 public:
56  QConsolePopupCompleter(const QStringList&, QWidget *parent = 0);
57  virtual ~QConsolePopupCompleter();
58 
59 public:
60  QString selected(void) { return selected_; }
61  int exec(QTextEdit*);
62 
63 protected:
64  virtual void showEvent(QShowEvent*);
65 
66 private Q_SLOTS:
67  void onItemActivated(QListWidgetItem*);
68 
69 public:
70  QListWidget *listWidget_;
71  QString selected_;
72 };
73 
78 class QConsole : public QTextEdit
79 {
80  Q_OBJECT
81 public:
82  //constructor
83  QConsole(QWidget *parent = NULL, const QString &welcomeText = "");
84  //set the prompt of the console
85  void setPrompt(const QString &prompt, bool display = true);
86  //execCommand(QString) executes the command and displays back its result
87  bool execCommand(const QString &command, bool writeCommand = true,
88  bool showPrompt = true, QString *result = NULL);
89  void clear();
90  void reset(const QString &welcomeText = "");
91 
92  //cosmetic methods !
93 
94  // @{
96  QColor cmdColor() const { return cmdColor_; }
97  void setCmdColor(QColor c) {cmdColor_ = c;}
98  // @}
99 
100  // @{
102  QColor errColor() const { return errColor_; }
103  void setErrColor(QColor c) {errColor_ = c;}
104  // @}
105 
106  // @{
108  QColor outColor() const { return outColor_; }
109  void setOutColor(QColor c) {outColor_ = c;}
110  // @}
111  void setCompletionColor(QColor c) {completionColor = c;}
112 
113  // @{
115  void setFont(const QFont& f);
116  QFont font() const { return currentFont(); }
117  // @}
118 
119 protected:
120 
121  // interface for derived console classes to write some output
122  void setFormat(QColor color, bool bold=false, bool italic=false);
123  void setFormat(bool bold=false, bool italic=false);
124  void println(const QString& s);
125  void println(const std::string& s);
126  void println(const char* s);
127 
128 
129 protected:
130  void keyPressEvent(QKeyEvent * e);
131  void contextMenuEvent( QContextMenuEvent * event);
132 
133  //Return false if the command is incomplete (e.g. unmatched braces)
134  virtual bool isCommandComplete(const QString &command);
135  //Get the command to validate
136  QString getCurrentCommand();
137 
138  //Replace current command with a new one
139  void replaceCurrentCommand(const QString &newCommand);
140 
141  //Test whether the cursor is in the edition zone
142  bool isInEditionZone();
143  bool isInEditionZone(int pos);
144 
145  //Test whether the selection is in the edition zone
147  //Change paste behaviour
148  void insertFromMimeData(const QMimeData *);
149 
150 
151  //protected attributes
152 protected:
153  //colors
155 
157  // cached prompt length
159  // The prompt string
160  QString prompt;
161  // The commands history
162  QStringList history;
163  // Current history index (needed because afaik QStringList does not have such an index)
165  //Holds position of the prompt
167 
168 protected:
169  virtual void dragEnterEvent(QDragEnterEvent *e);
170  virtual void dragLeaveEvent(QDragLeaveEvent *e);
171  virtual void dragMoveEvent(QDragMoveEvent *e);
172  virtual void dropEvent(QDropEvent *e);
173 
174  void mousePressEvent(QMouseEvent*);
175  void mouseReleaseEvent(QMouseEvent*);
176 
177  //execute a validated command (should be reimplemented and called at the end)
178  //the return value of the function is the string result
179  //res must hold back the return value of the command (0: passed; else: error)
180  virtual QString interpretCommand(const QString &command, int *res);
181 
182  //give suggestions to autocomplete a command (should be reimplemented)
183  //the return value of the function is the string list of all suggestions
184  //the returned prefix is useful to complete "sub-commands"
185  virtual QStringList suggestCommand(const QString &cmd, QString &prefix);
186 
187 
188 public slots:
189  //Contextual menu slots
190  void cut();
191 
192  //displays the prompt
193  void displayPrompt();
194 
196 
197 Q_SIGNALS:
198  //Signal emitted after that a command is executed
199  void commandExecuted(const QString &command);
200 
201 private:
202  void handleTabKeyPress();
203  void handleReturnKeyPress();
204  void handleUpKeyPress();
205  void handleDownKeyPress();
206  void setHome();
207 
208 protected:
209 
211  SEARCH_NONE, SEARCH_HISTORY)
212 
213 protected: // Specific search stuff, to be reimplemented in subclasses for own search capabilities
214  virtual const SearchMode& searchModeCommand(QKeyEvent* event);
215  virtual void enterSearchMode(const SearchMode& mode);
216  virtual QString getSearchModeDescriptor();
217  virtual QStringList getSearchList();
218  // distinguish between string used for comparison (=first) and the respective command (=second)
219  virtual std::multimap<QString, QString> getExtendedSearchList();
220 
221 protected: // (General) Search stuff
222  SearchMode currentSearchMode;
223 
224 private:
225  bool handleSearchKeyEvent(QKeyEvent* event); // return true if event was handled
226 
227  void overwriteCommand(QString command);
228  void clearCurrentLine();
229  void setCursorToEditZone();
230 
231  void resetSearch();
232  void updateSearchMatches();
233  void updateSearchCursor(QString query, QString result, int index, int numResults);
234 
235  void handleSearch(const SearchMode& mode);
236  void handleSearchInput(QKeyEvent* event);
237  void handleSearchReturnKey();
238  void handleSearchEscapeKey();
239 
240  QString searchInput;
241 
242  QStringList searchMatches;
243  int searchMatchIndex;
244 
245  // Store the previous prompt as we will have to restore it
246  QString previousPrompt;
247 };
248 
250 }
251 #endif
bool execCommand(const QString &command, bool writeCommand=true, bool showPrompt=true, QString *result=NULL)
void displayPrompt()
QListWidget * listWidget_
Definition: QConsole.h:70
A replacement for enum that can be extended in subclasses.
int historyIndex
Definition: QConsole.h:164
void setCmdColor(QColor c)
Definition: QConsole.h:97
void commandExecuted(const QString &command)
QColor completionColor
Definition: QConsole.h:154
bool isInEditionZone()
An abstract Qt console.
Definition: QConsole.h:78
virtual QString getSearchModeDescriptor()
QConsolePopupCompleter(const QStringList &, QWidget *parent=0)
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
QStringList history
Definition: QConsole.h:162
int promptPosition
Definition: QConsole.h:166
virtual QString interpretCommand(const QString &command, int *res)
virtual QStringList getSearchList()
virtual bool isCommandComplete(const QString &command)
void setPrompt(const QString &prompt, bool display=true)
virtual MIRA_EXTENSIBLE_ENUM_DECLARE(SearchMode, ExtensibleEnum< SearchMode >, SEARCH_NONE, SEARCH_HISTORY) protected void enterSearchMode(const SearchMode &mode)
virtual void dropEvent(QDropEvent *e)
void insertFromMimeData(const QMimeData *)
void setFont(const QFont &f)
get set font
void println(const QString &s)
QString selected_
Definition: QConsole.h:71
void mouseReleaseEvent(QMouseEvent *)
virtual void dragEnterEvent(QDragEnterEvent *e)
QString getCurrentCommand()
void setFormat(QColor color, bool bold=false, bool italic=false)
virtual void dragLeaveEvent(QDragLeaveEvent *e)
virtual void showEvent(QShowEvent *)
Popup Completer class.
Definition: QConsole.h:51
virtual std::multimap< QString, QString > getExtendedSearchList()
QColor cmdColor_
Definition: QConsole.h:154
QColor cmdColor() const
get/set command color
Definition: QConsole.h:96
#define MIRA_EXTENSIBLE_ENUM_DECLARE(NAME, BASE, VALUES...)
Definition: ExtensibleEnum.h:331
QConsole(QWidget *parent=NULL, const QString &welcomeText="")
void onCursorPositionChanged()
void replaceCurrentCommand(const QString &newCommand)
SearchMode currentSearchMode
Definition: QConsole.h:222
void keyPressEvent(QKeyEvent *e)
int oldPosition
Definition: QConsole.h:156
void mousePressEvent(QMouseEvent *)
QColor errColor() const
get/set error color
Definition: QConsole.h:102
QColor outColor_
Definition: QConsole.h:154
void contextMenuEvent(QContextMenuEvent *event)
void setOutColor(QColor c)
Definition: QConsole.h:109
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:160
virtual QStringList suggestCommand(const QString &cmd, QString &prefix)
QString selected(void)
Definition: QConsole.h:60
void reset(const QString &welcomeText="")
void setCompletionColor(QColor c)
Definition: QConsole.h:111
virtual void dragMoveEvent(QDragMoveEvent *e)
bool isSelectionInEditionZone()
QFont font() const
Definition: QConsole.h:116
QColor outColor() const
get/set output color
Definition: QConsole.h:108
int promptLength
Definition: QConsole.h:158
QColor errColor_
Definition: QConsole.h:154
void setErrColor(QColor c)
Definition: QConsole.h:103