ctkConsole_p.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. #ifndef __ctkConsole_p_h
  15. #define __ctkConsole_p_h
  16. // Qt includes
  17. #include <QTextEdit>
  18. #include <QPointer>
  19. #include <QEventLoop>
  20. // CTK includes
  21. #include "ctkConsole.h"
  22. #include "ctkWidgetsExport.h"
  23. class QPushButton;
  24. /// \ingroup Widgets
  25. class CTK_WIDGETS_EXPORT ctkConsolePrivate : public QTextEdit
  26. {
  27. Q_OBJECT
  28. Q_DECLARE_PUBLIC(ctkConsole);
  29. protected:
  30. ctkConsole* const q_ptr;
  31. public:
  32. ctkConsolePrivate(ctkConsole& object);
  33. typedef QTextEdit Superclass;
  34. void init();
  35. static bool isMoveLeftWithinLine(QKeyEvent* e, QTextCursor::MoveOperation &moveOperation, QTextCursor::MoveMode &moveMode);
  36. static bool isMoveRighttWithinLine(QKeyEvent* e, QTextCursor::MoveOperation &moveOperation, QTextCursor::MoveMode &moveMode);
  37. virtual void keyPressEvent(QKeyEvent* e);
  38. void switchToUserInputTextColor(QTextCursor* textCursorToUpdate = 0);
  39. /// Returns the end of the document
  40. int documentEnd() const;
  41. /// Returns the end of the commandLine
  42. int commandEnd() const;
  43. virtual void focusOutEvent(QFocusEvent *e);
  44. virtual void resizeEvent(QResizeEvent * e);
  45. /// Force the scrollbar to be all the way down
  46. void scrollToBottom();
  47. void updateCompleterIfVisible();
  48. /// If there is exactly 1 completion, insert it and hide the completer,
  49. /// else do nothing.
  50. void selectCompletion();
  51. void setCompleter(ctkConsoleCompleter* completer);
  52. void updateCompleter();
  53. /// Update the contents of the command buffer from the contents of the widget.
  54. /// If \a commandLength is specified, buffer is updated limiting the content
  55. /// of the widget.
  56. void updateCommandBuffer(int commandLength = -1);
  57. /// Replace the contents of the command buffer, updating the display
  58. void replaceCommandBuffer(const QString& text);
  59. /// References the buffer where the current un-executed command is stored
  60. QString& commandBuffer();
  61. /// Implements command-execution
  62. void internalExecuteCommand();
  63. void processInput();
  64. /// Writes the supplied text to the console
  65. void printString(const QString& text);
  66. /// Updates the current command.
  67. /// Unlike printMessage(), this will affect the current command being typed.
  68. void printCommand(const QString& cmd);
  69. /// Puts out an input accepting promp using either ps1 or ps2.
  70. /// ps2 will be used if MultilineStatement is True
  71. /// \sa ctkConsole::ps1(), ctkConsole::ps2()
  72. void promptForInput(const QString& indent = QString());
  73. /// Puts out an input accepting prompt.
  74. /// It is recommended that one uses prompt instead of printMessage() to print
  75. /// an input prompt since this call ensures that the prompt is shown on a new
  76. /// line.
  77. void prompt(const QString& text);
  78. /// Print welcome message
  79. virtual void printWelcomeMessage();
  80. public Q_SLOTS:
  81. /// Inserts the given completion string at the cursor.
  82. /// 2 Different ways of completion are established by \sa ctkConsolePrivate::insertCompletionMethod:
  83. /// TRUE - Replace the current word that the cursor is touching with the given text.
  84. /// Determines the word using QTextCursor::StartOfWord, EndOfWord.
  85. /// FALSE - Just insert the word replacing only the text from the current position until StartOfWord
  86. void insertCompletion(const QString& text);
  87. /// Print a message
  88. /// \sa ctkConsole::outputTextColor
  89. void printOutputMessage(const QString& text);
  90. /// Print a message
  91. /// \sa ctkConsole::errorTextColor
  92. void printErrorMessage(const QString& text);
  93. /// Update the value of ScrollbarAtBottom given the current position of the scollbar
  94. void onScrollBarValueChanged(int value);
  95. /// Ensure the interactive line is visible (even when it is split on 2 lines)
  96. /// \sa onScrollBarValueChanged()
  97. void onTextChanged();
  98. protected:
  99. /// Return true if the cursor position is in the history area
  100. /// false if it is after the InteractivePosition.
  101. bool isCursorInHistoryArea()const;
  102. /// Return true if the cursor position is in the message output area
  103. /// false if it is before the end of the commandLine.
  104. bool isCursorInMessageOutputArea()const;
  105. /// Reimplemented to make sure there is no text added into the
  106. /// history logs.
  107. virtual void insertFromMimeData(const QMimeData* source);
  108. /// Paste text at the current text cursor position.
  109. void pasteText(const QString& text);
  110. public:
  111. /// A custom completer
  112. QPointer<ctkConsoleCompleter> Completer;
  113. /// Stores the beginning of the area of interactive input, outside which
  114. /// changes can't be made to the text edit contents
  115. int InteractivePosition;
  116. /// Stores the size of the message output area from the end of document
  117. /// until the end of the command
  118. int MessageOutputSize;
  119. /// Indicates if the last statement processes was incomplete.
  120. bool MultilineStatement;
  121. /// Stores command-history, plus the current command buffer
  122. QStringList CommandHistory;
  123. /// Stores the current position in the command-history
  124. int CommandPosition;
  125. /// Prompt color
  126. QColor PromptColor;
  127. /// Output text color
  128. QColor OutputTextColor;
  129. /// Message Output text color (every message displayed during autocompletion)
  130. QColor MessageOutputColor;
  131. /// Error text color
  132. QColor ErrorTextColor;
  133. /// Standard input text color.
  134. QColor StdinTextColor;
  135. /// Command text color
  136. QColor CommandTextColor;
  137. /// Welcome text color
  138. QColor WelcomeTextColor;
  139. /// Primary prompt
  140. QString Ps1;
  141. /// Secondary prompt
  142. QString Ps2;
  143. /// Method to insert the completion word:
  144. /// TRUE - Replace the whole word under the cursor
  145. /// FALSE - Insert the word and replace only from the cursor until the StartOfWord
  146. bool insertCompletionMethod;
  147. ctkConsole::EditorHints EditorHints;
  148. bool ScrollbarAtBottom;
  149. QPointer<QEventLoop> InputEventLoop;
  150. QList<QKeySequence> CompleterShortcuts;
  151. ctkConsole::RunFileOptions RunFileOptions;
  152. QPushButton* RunFileButton;
  153. QAction* RunFileAction;
  154. };
  155. #endif