ctkConsole_p.h 5.3 KB

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