CuteLogger
Fast and simple logging solution for Qt based applications
qconsole.h
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 // migrated to Qt4 by YoungTaek Oh. date: Nov 29, 2010
11 
12 /***************************************************************************
13  * *
14  * This program is free software; you can redistribute it and/or modify *
15  * it under the terms of the GNU General Public License as published by *
16  * the Free Software Foundation; either version 2 of the License, or *
17  * (at your option) any later version. *
18  * *
19  ***************************************************************************/
20 
21 #ifndef QCONSOLE_H
22 #define QCONSOLE_H
23 
24 #include <QStringList>
25 #include <QTextEdit>
26 #include <QMouseEvent>
27 #include <QKeyEvent>
28 #include <QMenu>
29 
30 #include <QDialog>
31 #include <QListWidget>
32 #include <Logger.h>
33 
34 #if QT_VERSION < 0x040000
35 #error "supports only Qt 4.0 or greater"
36 #endif
37 
43 class PopupListWidget : public QListWidget
44 {
45  Q_OBJECT
46 
47 public:
48  PopupListWidget(QWidget *parent = 0): QListWidget(parent) {
49  setUniformItemSizes(true);
50  setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
51  }
52  virtual ~PopupListWidget() { }
53 
54 protected:
55  virtual QSize sizeHint() const;
56  virtual void keyPressEvent(QKeyEvent *e) {
57  if (e->key() == Qt::Key_Tab ||
58  e->key() == Qt::Key_Return)
59  Q_EMIT itemActivated(currentItem());
60  else
61  QListWidget::keyPressEvent(e);
62  }
63 };
64 
74 class PopupCompleter : public QDialog
75 {
76  Q_OBJECT
77 
78 public:
79  PopupCompleter(const QStringList&, QWidget *parent = 0);
80  virtual ~PopupCompleter();
81 
82 public:
83  QString selected(void) { return selected_; }
84  int exec(QTextEdit*);
85 
86 protected:
87  virtual void showEvent(QShowEvent*);
88 
89 private Q_SLOTS:
90  void onItemActivated(QListWidgetItem*);
91 
92 public:
93  QListWidget *listWidget_;
94  QString selected_;
95 };
96 
101 class QConsole : public QTextEdit
102 {
103  Q_OBJECT
104 public:
105  //constructor
106  QConsole(QWidget *parent = NULL, const QString &welcomeText = "");
107  //set the prompt of the console
108  void setPrompt(const QString &prompt, bool display = true);
109  //execCommand(QString) executes the command and displays back its result
110  bool execCommand(const QString &command, bool writeCommand = true,
111  bool showPrompt = true, QString *result = NULL);
112  //saves a file script
113  int saveScript(const QString &fileName);
114  //loads a file script
115  int loadScript(const QString &fileName);
116  //clear & reset the console (useful sometimes)
117  void clear();
118  void reset(const QString &welcomeText = "");
119 
120  //cosmetic methods !
121 
122  // @{
124  QColor cmdColor() const { return cmdColor_; }
125  void setCmdColor(QColor c) {cmdColor_ = c;}
126  // @}
127 
128  // @{
130  QColor errColor() const { return errColor_; }
131  void setErrColor(QColor c) {errColor_ = c;}
132  // @}
133 
134  // @{
136  QColor outColor() const { return outColor_; }
137  void setOutColor(QColor c) {outColor_ = c;}
138  // @}
139  void setCompletionColor(QColor c) {completionColor = c;}
140 
141  // @{
143  void setFont(const QFont& f);
144  QFont font() const { return currentFont(); }
145  // @}
146 
147  void correctPathName(QString& pathName);
148 
149 private:
150  void dropEvent( QDropEvent * event);
151  void dragMoveEvent( QDragMoveEvent * event);
152 
153  void keyPressEvent(QKeyEvent * e);
154  void contextMenuEvent( QContextMenuEvent * event);
155 
156  //Return false if the command is incomplete (e.g. unmatched braces)
157  virtual bool isCommandComplete(const QString &command);
158  //Get the command to validate
159  QString getCurrentCommand();
160 
161  //Replace current command with a new one
162  void replaceCurrentCommand(const QString &newCommand);
163 
164  //Test whether the cursor is in the edition zone
165  bool isInEditionZone();
166  bool isInEditionZone(const int& pos);
167 
168  //Test whether the selection is in the edition zone
169  bool isSelectionInEditionZone();
170  //Change paste behaviour
171  void insertFromMimeData(const QMimeData *);
172 
173 
174 //protected attributes
175 protected:
176  //colors
177  QColor cmdColor_, errColor_, outColor_, completionColor;
178 
179  int oldPosition;
180  // cached prompt length
181  int promptLength;
182  // The prompt string
183  QString prompt;
184  // The commands history
185  QStringList history;
186  //Contains the commands that has succeeded
187  QStringList recordedScript;
188  // Current history index (needed because afaik QStringList does not have such an index)
189  int historyIndex;
190  //Holds the paragraph number of the prompt (useful for multi-line command handling)
191  int promptParagraph;
192 
193 protected:
194  //Implement paste with middle mouse button
195  void mousePressEvent(QMouseEvent*);
196 
197  //execute a validated command (should be reimplemented and called at the end)
198  //the return value of the function is the string result
199  //res must hold back the return value of the command (0: passed; else: error)
200  virtual QString interpretCommand(const QString &command, int *res);
201  //give suggestions to autocomplete a command (should be reimplemented)
202  //the return value of the function is the string list of all suggestions
203  //the returned prefix is useful to complete "sub-commands"
204  virtual QStringList suggestCommand(const QString &cmd, QString &prefix);
205 
206 
207 public Q_SLOTS:
208  //Contextual menu slots
209  void cut();
210  //void paste();
211  void del();
212  //displays the prompt
213  void displayPrompt();
214 
215 Q_SIGNALS:
216  //Signal emitted after that a command is executed
217  void commandExecuted(const QString &command);
218 
219 private:
220  void handleTabKeyPress();
221  void handleReturnKeyPress();
222  bool handleBackspaceKeyPress();
223  void handleUpKeyPress();
224  void handleDownKeyPress();
225  void setHome(bool);
226 };
227 
228 #endif
QColor outColor() const
get/set output color
Definition: qconsole.h:136
int exec(QTextEdit *)
execute PopupCompleter at appropriate position.
Definition: qconsole.cpp:133
Definition: qconsole.h:74
QColor errColor() const
get/set error color
Definition: qconsole.h:130
Definition: qconsole.h:43
A file containing the description of Logger class and and additional useful macros for logging...
Definition: qconsole.h:101
void setFont(const QFont &f)
get set font
Definition: qconsole.cpp:274
QColor cmdColor() const
get/set command color
Definition: qconsole.h:124