ctkConsole_p.h 4.5 KB

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