ctkConsole.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0.txt
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. /*=========================================================================
  15. Program: ParaView
  16. Copyright (c) 2005-2008 Sandia Corporation, Kitware Inc.
  17. All rights reserved.
  18. ParaView is a free software; you can redistribute it and/or modify it
  19. under the terms of the ParaView license version 1.2.
  20. See http://www.paraview.org/paraview/project/license.html for the full ParaView license.
  21. A copy of this license can be obtained by contacting
  22. Kitware Inc.
  23. 28 Corporate Drive
  24. Clifton Park, NY 12065
  25. USA
  26. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  27. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  28. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  29. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR
  30. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  31. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  32. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  33. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  34. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  35. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  36. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  37. =========================================================================*/
  38. #ifndef __ctkConsole_h
  39. #define __ctkConsole_h
  40. // Qt includes
  41. #include <QWidget>
  42. #include <QTextCharFormat>
  43. #include <QCompleter>
  44. // CTK includes
  45. #include "ctkWidgetsExport.h"
  46. class ctkConsolePrivate;
  47. class ctkConsoleCompleter;
  48. /// \ingroup Widgets
  49. /// QWidget that provides an interactive console
  50. class CTK_WIDGETS_EXPORT ctkConsole : public QWidget
  51. {
  52. Q_OBJECT
  53. Q_PROPERTY(QColor promptColor READ promptColor WRITE setPromptColor)
  54. Q_PROPERTY(QColor outputTextColor READ outputTextColor WRITE setOutputTextColor)
  55. Q_PROPERTY(QColor errorTextColor READ errorTextColor WRITE setErrorTextColor)
  56. Q_PROPERTY(QColor stdinTextColor READ stdinTextColor WRITE setStdinTextColor)
  57. Q_PROPERTY(QColor commandTextColor READ commandTextColor WRITE setCommandTextColor)
  58. Q_PROPERTY(QColor welcomeTextColor READ welcomeTextColor WRITE setWelcomeTextColor)
  59. Q_PROPERTY(QColor backgroundColor READ backgroundColor WRITE setBackgroundColor)
  60. Q_PROPERTY(QString ps1 READ ps1 WRITE setPs1)
  61. Q_PROPERTY(QString ps2 READ ps2 WRITE setPs2)
  62. Q_FLAGS(EditorHint EditorHints)
  63. Q_PROPERTY(EditorHints editorHints READ editorHints WRITE setEditorHints)
  64. Q_ENUMS(Qt::ScrollBarPolicy)
  65. Q_PROPERTY(Qt::ScrollBarPolicy scrollBarPolicy READ scrollBarPolicy WRITE setScrollBarPolicy)
  66. public:
  67. enum EditorHint
  68. {
  69. NoHints = 0x00,
  70. AutomaticIndentation = 0x01, /*!< Align cursor based an indentation of the previous command */
  71. RemoveTrailingSpaces = 0x02, /*!< Remove trailing spaces of the entered command */
  72. SplitCopiedTextByLine = 0x4 /*!< Copied text is split by lines and each one is executed (expected the last one) */
  73. };
  74. Q_DECLARE_FLAGS(EditorHints, EditorHint)
  75. ctkConsole(QWidget* parentObject = 0);
  76. typedef QWidget Superclass;
  77. virtual ~ctkConsole();
  78. /// Returns the current formatting that will be used by printMessage()
  79. QTextCharFormat getFormat() const;
  80. /// Sets formatting that will be used by printMessage()
  81. void setFormat(const QTextCharFormat& Format);
  82. /// Return the completer of this console
  83. ctkConsoleCompleter* completer() const;
  84. /// Set a completer for this console
  85. void setCompleter(ctkConsoleCompleter* completer);
  86. QColor promptColor()const;
  87. /// \sa promptColor()
  88. void setPromptColor(const QColor& newColor);
  89. QColor outputTextColor()const;
  90. /// \sa outputTextColor()
  91. void setOutputTextColor(const QColor& newColor);
  92. QColor errorTextColor()const;
  93. /// \sa errorTextColor()
  94. void setErrorTextColor(const QColor& newColor);
  95. QColor stdinTextColor()const;
  96. /// \sa stdinTextColor()
  97. void setStdinTextColor(const QColor& newColor);
  98. QColor commandTextColor()const;
  99. /// \sa commandTextColor()
  100. void setCommandTextColor(const QColor& newColor);
  101. QColor welcomeTextColor()const;
  102. /// \sa welcomeTextColor()
  103. void setWelcomeTextColor(const QColor& newColor);
  104. QColor backgroundColor()const;
  105. void setBackgroundColor(const QColor& newColor);
  106. EditorHints editorHints()const;
  107. /// \sa editorHints()
  108. void setEditorHints(const EditorHints& newEditorHints);
  109. Qt::ScrollBarPolicy scrollBarPolicy()const;
  110. /// \sa scrollBarPolicy()
  111. void setScrollBarPolicy(const Qt::ScrollBarPolicy& newScrollBarPolicy);
  112. /// Prints text on the console
  113. void printMessage(const QString& message, const QColor& color);
  114. /// Returns the string used as primary prompt
  115. virtual QString ps1() const;
  116. /// Set the string used as primary prompt
  117. virtual void setPs1(const QString& newPs1);
  118. /// Returns the string used as secondary prompt
  119. virtual QString ps2() const;
  120. /// Set the string used as secondary prompt
  121. virtual void setPs2(const QString& newPs2);
  122. static QString stdInRedirectCallBack(void * callData);
  123. Q_SIGNALS:
  124. /// This signal emitted before and after a command is executed
  125. void aboutToExecute(const QString&);
  126. void executed(const QString&);
  127. public Q_SLOTS:
  128. /// Clears the contents of the console
  129. virtual void clear();
  130. /// Clears the contents of the console and display welcome message
  131. virtual void reset();
  132. /// Exec the contents of the last console line
  133. virtual void exec(const QString&);
  134. protected:
  135. /// Prompt the user for input
  136. QString readInputLine();
  137. /// Called whenever the user enters a command
  138. virtual void executeCommand(const QString& Command);
  139. protected:
  140. ctkConsole(ctkConsolePrivate * pimpl, QWidget* parentObject);
  141. QScopedPointer<ctkConsolePrivate> d_ptr;
  142. private:
  143. Q_DECLARE_PRIVATE(ctkConsole);
  144. Q_DISABLE_COPY(ctkConsole);
  145. };
  146. Q_DECLARE_OPERATORS_FOR_FLAGS(ctkConsole::EditorHints);
  147. //-----------------------------------------------------------------------------
  148. class CTK_WIDGETS_EXPORT ctkConsoleCompleter : public QCompleter
  149. {
  150. public:
  151. /// Update the completion model given a string. The given string
  152. /// is the current console text between the cursor and the start of
  153. /// the line.
  154. virtual void updateCompletionModel(const QString& str) = 0;
  155. /// Returns the autocomplete preference list
  156. QStringList autocompletePreferenceList();
  157. /// Set autocomplete preference list
  158. /// This list allows to specify options that could be
  159. /// selected by default if multiple choices are proposed to the user.
  160. void setAutocompletePreferenceList(const QStringList& list);
  161. protected:
  162. QStringList AutocompletePreferenceList;
  163. };
  164. #endif