ctkConsole_p.h 5.1 KB

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