ctkConsole_p.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. virtual void keyPressEvent(QKeyEvent* e);
  37. void switchToUserInputTextColor(QTextCursor* textCursorToUpdate = 0);
  38. /// Returns the end of the document
  39. int documentEnd() const;
  40. virtual void focusOutEvent(QFocusEvent *e);
  41. virtual void resizeEvent(QResizeEvent * e);
  42. /// Force the scrollbar to be all the way down
  43. void scrollToBottom();
  44. void updateCompleterIfVisible();
  45. /// If there is exactly 1 completion, insert it and hide the completer,
  46. /// else do nothing.
  47. void selectCompletion();
  48. void setCompleter(ctkConsoleCompleter* completer);
  49. void updateCompleter();
  50. /// Update the contents of the command buffer from the contents of the widget
  51. void updateCommandBuffer();
  52. /// Replace the contents of the command buffer, updating the display
  53. void replaceCommandBuffer(const QString& text);
  54. /// References the buffer where the current un-executed command is stored
  55. QString& commandBuffer();
  56. /// Implements command-execution
  57. void internalExecuteCommand();
  58. void processInput();
  59. /// Writes the supplied text to the console
  60. void printString(const QString& text);
  61. /// Updates the current command.
  62. /// Unlike printMessage(), this will affect the current command being typed.
  63. void printCommand(const QString& cmd);
  64. /// Puts out an input accepting promp using either ps1 or ps2.
  65. /// ps2 will be used if MultilineStatement is True
  66. /// \sa ctkConsole::ps1(), ctkConsole::ps2()
  67. void promptForInput(const QString& indent = QString());
  68. /// Puts out an input accepting prompt.
  69. /// It is recommended that one uses prompt instead of printMessage() to print
  70. /// an input prompt since this call ensures that the prompt is shown on a new
  71. /// line.
  72. void prompt(const QString& text);
  73. /// Print welcome message
  74. virtual void printWelcomeMessage();
  75. public Q_SLOTS:
  76. /// Inserts the given completion string at the cursor. This will replace
  77. /// the current word that the cursor is touching with the given text.
  78. /// Determines the word using QTextCursor::StartOfWord, EndOfWord.
  79. void insertCompletion(const QString& text);
  80. /// Print a message
  81. /// \sa ctkConsole::outputTextColor
  82. void printOutputMessage(const QString& text);
  83. /// Print a message
  84. /// \sa ctkConsole::errorTextColor
  85. void printErrorMessage(const QString& text);
  86. /// Update the value of ScrollbarAtBottom given the current position of the scollbar
  87. void onScrollBarValueChanged(int value);
  88. /// Ensure the interactive line is visible (even when it is split on 2 lines)
  89. /// \sa onScrollBarValueChanged()
  90. void onTextChanged();
  91. protected:
  92. /// Return true if the cursor position is in the history area
  93. /// false if it is after the InteractivePosition.
  94. bool isCursorInHistoryArea()const;
  95. /// Reimplemented to make sure there is no text added into the
  96. /// history logs.
  97. virtual void insertFromMimeData(const QMimeData* source);
  98. /// Paste text at the current text cursor position.
  99. void pasteText(const QString& text);
  100. public:
  101. /// A custom completer
  102. QPointer<ctkConsoleCompleter> Completer;
  103. /// Stores the beginning of the area of interactive input, outside which
  104. /// changes can't be made to the text edit contents
  105. int InteractivePosition;
  106. /// Indicates if the last statement processes was incomplete.
  107. bool MultilineStatement;
  108. /// Stores command-history, plus the current command buffer
  109. QStringList CommandHistory;
  110. /// Stores the current position in the command-history
  111. int CommandPosition;
  112. /// Prompt color
  113. QColor PromptColor;
  114. /// Output text color
  115. QColor OutputTextColor;
  116. /// Error text color
  117. QColor ErrorTextColor;
  118. /// Standard input text color.
  119. QColor StdinTextColor;
  120. /// Command text color
  121. QColor CommandTextColor;
  122. /// Welcome text color
  123. QColor WelcomeTextColor;
  124. /// Primary prompt
  125. QString Ps1;
  126. /// Secondary prompt
  127. QString Ps2;
  128. ctkConsole::EditorHints EditorHints;
  129. bool ScrollbarAtBottom;
  130. QPointer<QEventLoop> InputEventLoop;
  131. QList<QKeySequence> CompleterShortcuts;
  132. ctkConsole::RunFileOptions RunFileOptions;
  133. QPushButton* RunFileButton;
  134. QAction* RunFileAction;
  135. };
  136. #endif