ctkConsole.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  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. /*=========================================================================
  15. Program: ParaView
  16. Module: $RCSfile$
  17. Copyright (c) 2005-2008 Sandia Corporation, Kitware Inc.
  18. All rights reserved.
  19. ParaView is a free software; you can redistribute it and/or modify it
  20. under the terms of the ParaView license version 1.2.
  21. See License_v1.2.txt for the full ParaView license.
  22. A copy of this license can be obtained by contacting
  23. Kitware Inc.
  24. 28 Corporate Drive
  25. Clifton Park, NY 12065
  26. USA
  27. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  28. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  29. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  30. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR
  31. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  32. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  33. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  34. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  35. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  36. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  37. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. =========================================================================*/
  39. // Qt includes
  40. #include <QAbstractItemView>
  41. #include <QApplication>
  42. #include <QClipboard>
  43. #include <QCompleter>
  44. #include <QKeyEvent>
  45. #include <QPointer>
  46. #include <QTextCursor>
  47. #include <QVBoxLayout>
  48. #include <QScrollBar>
  49. #include <QDebug>
  50. // CTK includes
  51. #include "ctkConsole.h"
  52. #include "ctkConsole_p.h"
  53. #include "ctkPimpl.h"
  54. //-----------------------------------------------------------------------------
  55. // ctkConsolePrivate methods
  56. //-----------------------------------------------------------------------------
  57. ctkConsolePrivate::ctkConsolePrivate(ctkConsole& object) :
  58. QTextEdit(0),
  59. q_ptr(&object),
  60. InteractivePosition(documentEnd()),
  61. MultilineStatement(false), Ps1("$ "), Ps2("> "),
  62. EditorHints(ctkConsole::AutomaticIndentation | ctkConsole::RemoveTrailingSpaces)
  63. {
  64. }
  65. //-----------------------------------------------------------------------------
  66. void ctkConsolePrivate::init()
  67. {
  68. Q_Q(ctkConsole);
  69. this->setParent(q);
  70. this->setTabChangesFocus(false);
  71. this->setAcceptDrops(false);
  72. this->setAcceptRichText(false);
  73. this->setUndoRedoEnabled(false);
  74. this->PromptColor = QColor(0, 0, 0); // Black
  75. this->OutputTextColor = QColor(0, 150, 0); // Green
  76. this->ErrorTextColor = QColor(255, 0, 0); // Red
  77. this->CommandTextColor = QColor(0, 0, 150); // Blue
  78. this->WelcomeTextColor = QColor(0, 0, 255); // Dark Blue
  79. QFont f;
  80. f.setFamily("Courier");
  81. f.setStyleHint(QFont::TypeWriter);
  82. f.setFixedPitch(true);
  83. QTextCharFormat format;
  84. format.setFont(f);
  85. format.setForeground(this->OutputTextColor);
  86. this->setCurrentCharFormat(format);
  87. this->CommandHistory.append("");
  88. this->CommandPosition = 0;
  89. QVBoxLayout * layout = new QVBoxLayout(q);
  90. layout->setMargin(0);
  91. layout->addWidget(this);
  92. }
  93. //-----------------------------------------------------------------------------
  94. void ctkConsolePrivate::keyPressEvent(QKeyEvent* e)
  95. {
  96. if (this->Completer && this->Completer->popup()->isVisible())
  97. {
  98. // The following keys are forwarded by the completer to the widget
  99. switch (e->key())
  100. {
  101. case Qt::Key_Enter:
  102. case Qt::Key_Return:
  103. case Qt::Key_Escape:
  104. case Qt::Key_Tab:
  105. case Qt::Key_Backtab:
  106. e->ignore();
  107. return; // let the completer do default behavior
  108. default:
  109. break;
  110. }
  111. }
  112. QTextCursor text_cursor = this->textCursor();
  113. // Set to true if there's a current selection
  114. const bool selection = text_cursor.anchor() != text_cursor.position();
  115. // Set to true if the cursor overlaps the history area
  116. const bool history_area =
  117. text_cursor.anchor() < this->InteractivePosition
  118. || text_cursor.position() < this->InteractivePosition;
  119. // Allow copying anywhere in the console ...
  120. if(e->key() == Qt::Key_C && e->modifiers() == Qt::ControlModifier)
  121. {
  122. if(selection)
  123. {
  124. this->copy();
  125. }
  126. e->accept();
  127. return;
  128. }
  129. // Allow cut only if the selection is limited to the interactive area ...
  130. if(e->key() == Qt::Key_X && e->modifiers() == Qt::ControlModifier)
  131. {
  132. if(selection && !history_area)
  133. {
  134. this->cut();
  135. }
  136. e->accept();
  137. return;
  138. }
  139. // Allow paste only if the selection is in the interactive area ...
  140. if(e->key() == Qt::Key_V && e->modifiers() == Qt::ControlModifier)
  141. {
  142. if(!history_area)
  143. {
  144. const QMimeData* const clipboard = QApplication::clipboard()->mimeData();
  145. const QString text = clipboard->text();
  146. if(!text.isNull())
  147. {
  148. if (this->EditorHints & ctkConsole::SplitCopiedTextByLine)
  149. {
  150. QStringList lines = text.split(QRegExp("(?:\r\n|\r|\n)"));
  151. for(int i=0; i < lines.count(); ++i)
  152. {
  153. this->switchToUserInputTextColor(&text_cursor);
  154. text_cursor.insertText(lines.at(i));
  155. this->updateCommandBuffer();
  156. if (i < lines.count() - 1)
  157. {
  158. this->internalExecuteCommand();
  159. }
  160. }
  161. }
  162. else
  163. {
  164. this->switchToUserInputTextColor(&text_cursor);
  165. text_cursor.insertText(text);
  166. this->updateCommandBuffer();
  167. }
  168. }
  169. }
  170. e->accept();
  171. return;
  172. }
  173. // Force the cursor back to the interactive area
  174. if(history_area && e->key() != Qt::Key_Control)
  175. {
  176. text_cursor.setPosition(this->documentEnd());
  177. this->setTextCursor(text_cursor);
  178. }
  179. switch(e->key())
  180. {
  181. case Qt::Key_Up:
  182. e->accept();
  183. if (this->CommandPosition > 0)
  184. {
  185. this->replaceCommandBuffer(this->CommandHistory[--this->CommandPosition]);
  186. }
  187. break;
  188. case Qt::Key_Down:
  189. e->accept();
  190. if (this->CommandPosition < this->CommandHistory.size() - 2)
  191. {
  192. this->replaceCommandBuffer(this->CommandHistory[++this->CommandPosition]);
  193. }
  194. else
  195. {
  196. this->CommandPosition = this->CommandHistory.size()-1;
  197. this->replaceCommandBuffer("");
  198. }
  199. break;
  200. case Qt::Key_Left:
  201. if (text_cursor.position() > this->InteractivePosition)
  202. {
  203. QTextEdit::keyPressEvent(e);
  204. }
  205. else
  206. {
  207. e->accept();
  208. }
  209. break;
  210. case Qt::Key_Delete:
  211. e->accept();
  212. QTextEdit::keyPressEvent(e);
  213. this->updateCommandBuffer();
  214. break;
  215. case Qt::Key_Backspace:
  216. e->accept();
  217. if(text_cursor.position() > this->InteractivePosition)
  218. {
  219. QTextEdit::keyPressEvent(e);
  220. this->updateCommandBuffer();
  221. this->updateCompleterIfVisible();
  222. }
  223. break;
  224. case Qt::Key_Tab:
  225. e->accept();
  226. this->updateCompleter();
  227. this->selectCompletion();
  228. break;
  229. case Qt::Key_Home:
  230. e->accept();
  231. text_cursor.setPosition(this->InteractivePosition);
  232. this->setTextCursor(text_cursor);
  233. break;
  234. case Qt::Key_Return:
  235. case Qt::Key_Enter:
  236. e->accept();
  237. text_cursor.setPosition(this->documentEnd());
  238. this->setTextCursor(text_cursor);
  239. this->internalExecuteCommand();
  240. break;
  241. default:
  242. e->accept();
  243. this->switchToUserInputTextColor();
  244. QTextEdit::keyPressEvent(e);
  245. this->updateCommandBuffer();
  246. this->updateCompleterIfVisible();
  247. break;
  248. }
  249. }
  250. //-----------------------------------------------------------------------------
  251. void ctkConsolePrivate::switchToUserInputTextColor(QTextCursor* textCursorToUpdate)
  252. {
  253. QTextCharFormat currentFormat = this->currentCharFormat();
  254. currentFormat.setForeground(this->CommandTextColor);
  255. this->setCurrentCharFormat(currentFormat);
  256. if (textCursorToUpdate)
  257. {
  258. QTextCharFormat textCursorFormat = textCursorToUpdate->charFormat();
  259. textCursorFormat.setForeground(this->CommandTextColor);
  260. textCursorToUpdate->setCharFormat(textCursorFormat);
  261. }
  262. }
  263. //-----------------------------------------------------------------------------
  264. int ctkConsolePrivate::documentEnd() const
  265. {
  266. QTextCursor c(this->document());
  267. c.movePosition(QTextCursor::End);
  268. return c.position();
  269. }
  270. //-----------------------------------------------------------------------------
  271. void ctkConsolePrivate::focusOutEvent(QFocusEvent *e)
  272. {
  273. QTextEdit::focusOutEvent(e);
  274. // For some reason the QCompleter tries to set the focus policy to
  275. // NoFocus, set let's make sure we set it back to the default WheelFocus.
  276. this->setFocusPolicy(Qt::WheelFocus);
  277. }
  278. //-----------------------------------------------------------------------------
  279. void ctkConsolePrivate::updateCompleterIfVisible()
  280. {
  281. if (this->Completer && this->Completer->popup()->isVisible())
  282. {
  283. this->updateCompleter();
  284. }
  285. }
  286. //-----------------------------------------------------------------------------
  287. void ctkConsolePrivate::selectCompletion()
  288. {
  289. if (this->Completer && this->Completer->completionCount() == 1)
  290. {
  291. this->insertCompletion(this->Completer->currentCompletion());
  292. this->Completer->popup()->hide();
  293. }
  294. }
  295. //-----------------------------------------------------------------------------
  296. void ctkConsolePrivate::setCompleter(ctkConsoleCompleter* completer)
  297. {
  298. if (this->Completer)
  299. {
  300. this->Completer->setWidget(0);
  301. disconnect(this->Completer, SIGNAL(activated(const QString&)),
  302. this, SLOT(insertCompletion(const QString&)));
  303. }
  304. this->Completer = completer;
  305. if (this->Completer)
  306. {
  307. this->Completer->setWidget(this);
  308. connect(this->Completer, SIGNAL(activated(const QString&)),
  309. this, SLOT(insertCompletion(const QString&)));
  310. }
  311. }
  312. //-----------------------------------------------------------------------------
  313. void ctkConsolePrivate::updateCompleter()
  314. {
  315. if (this->Completer)
  316. {
  317. // Get the text between the current cursor position
  318. // and the start of the line
  319. QTextCursor text_cursor = this->textCursor();
  320. text_cursor.setPosition(this->InteractivePosition, QTextCursor::KeepAnchor);
  321. QString commandText = text_cursor.selectedText();
  322. // Call the completer to update the completion model
  323. this->Completer->updateCompletionModel(commandText);
  324. // Place and show the completer if there are available completions
  325. if (this->Completer->completionCount())
  326. {
  327. // Get a QRect for the cursor at the start of the
  328. // current word and then translate it down 8 pixels.
  329. text_cursor = this->textCursor();
  330. text_cursor.movePosition(QTextCursor::StartOfWord);
  331. QRect cr = this->cursorRect(text_cursor);
  332. cr.translate(0,8);
  333. cr.setWidth(this->Completer->popup()->sizeHintForColumn(0)
  334. + this->Completer->popup()->verticalScrollBar()->sizeHint().width());
  335. this->Completer->complete(cr);
  336. }
  337. else
  338. {
  339. this->Completer->popup()->hide();
  340. }
  341. }
  342. }
  343. //-----------------------------------------------------------------------------c
  344. void ctkConsolePrivate::updateCommandBuffer()
  345. {
  346. this->commandBuffer() = this->toPlainText().mid(this->InteractivePosition);
  347. }
  348. //-----------------------------------------------------------------------------
  349. void ctkConsolePrivate::replaceCommandBuffer(const QString& text)
  350. {
  351. this->commandBuffer() = text;
  352. QTextCursor c(this->document());
  353. c.setPosition(this->InteractivePosition);
  354. c.movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
  355. c.removeSelectedText();
  356. this->switchToUserInputTextColor(&c);
  357. c.insertText(text);
  358. }
  359. //-----------------------------------------------------------------------------
  360. QString& ctkConsolePrivate::commandBuffer()
  361. {
  362. return this->CommandHistory.back();
  363. }
  364. //-----------------------------------------------------------------------------
  365. void ctkConsolePrivate::internalExecuteCommand()
  366. {
  367. Q_Q(ctkConsole);
  368. QString command = this->commandBuffer();
  369. if (this->EditorHints & ctkConsole::RemoveTrailingSpaces)
  370. {
  371. command.replace(QRegExp("\\s*$"), ""); // Remove trailing spaces
  372. this->commandBuffer() = command; // Update buffer
  373. }
  374. // First update the history cache. It's essential to update the
  375. // this->CommandPosition before calling internalExecuteCommand() since that
  376. // can result in a clearing of the current command (BUG #8765).
  377. if (!command.isEmpty()) // Don't store empty commands in the history
  378. {
  379. this->CommandHistory.push_back("");
  380. this->CommandPosition = this->CommandHistory.size() - 1;
  381. }
  382. QTextCursor c(this->document());
  383. c.movePosition(QTextCursor::End);
  384. c.insertText("\n");
  385. this->InteractivePosition = this->documentEnd();
  386. emit q->executing(true);
  387. q->executeCommand(command);
  388. emit q->executing(false);
  389. // Find the indent for the command.
  390. QString indent;
  391. if (this->EditorHints & ctkConsole::AutomaticIndentation)
  392. {
  393. QRegExp regExp("^(\\s+)");
  394. if (regExp.indexIn(command) != -1)
  395. {
  396. indent = regExp.cap(1);
  397. }
  398. }
  399. this->promptForInput(indent);
  400. }
  401. //-----------------------------------------------------------------------------
  402. void ctkConsolePrivate::printString(const QString& text)
  403. {
  404. this->textCursor().movePosition(QTextCursor::End);
  405. this->textCursor().insertText(text);
  406. this->InteractivePosition = this->documentEnd();
  407. this->ensureCursorVisible();
  408. }
  409. //----------------------------------------------------------------------------
  410. void ctkConsolePrivate::printOutputMessage(const QString& text)
  411. {
  412. Q_Q(ctkConsole);
  413. q->printMessage(text, q->outputTextColor());
  414. QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
  415. }
  416. //----------------------------------------------------------------------------
  417. void ctkConsolePrivate::printErrorMessage(const QString& text)
  418. {
  419. Q_Q(ctkConsole);
  420. q->printMessage(text, q->errorTextColor());
  421. QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
  422. }
  423. //-----------------------------------------------------------------------------
  424. void ctkConsolePrivate::printCommand(const QString& cmd)
  425. {
  426. this->textCursor().insertText(cmd);
  427. this->updateCommandBuffer();
  428. }
  429. //----------------------------------------------------------------------------
  430. void ctkConsolePrivate::promptForInput(const QString& indent)
  431. {
  432. Q_Q(ctkConsole);
  433. QTextCharFormat format = q->getFormat();
  434. format.setForeground(q->promptColor());
  435. q->setFormat(format);
  436. if(!this->MultilineStatement)
  437. {
  438. this->prompt(q->ps1());
  439. }
  440. else
  441. {
  442. this->prompt(q->ps2());
  443. }
  444. this->printCommand(indent);
  445. }
  446. //-----------------------------------------------------------------------------
  447. void ctkConsolePrivate::prompt(const QString& text)
  448. {
  449. QTextCursor text_cursor = this->textCursor();
  450. // If the cursor is currently on a clean line, do nothing, otherwise we move
  451. // the cursor to a new line before showing the prompt.
  452. text_cursor.movePosition(QTextCursor::StartOfLine);
  453. int startpos = text_cursor.position();
  454. text_cursor.movePosition(QTextCursor::EndOfLine);
  455. int endpos = text_cursor.position();
  456. if (endpos != startpos)
  457. {
  458. this->textCursor().insertText("\n");
  459. }
  460. this->textCursor().insertText(text);
  461. this->InteractivePosition = this->documentEnd();
  462. this->ensureCursorVisible();
  463. }
  464. //----------------------------------------------------------------------------
  465. void ctkConsolePrivate::printWelcomeMessage()
  466. {
  467. Q_Q(ctkConsole);
  468. q->printMessage(
  469. QLatin1String("CTK Console"),
  470. q->welcomeTextColor());
  471. }
  472. //-----------------------------------------------------------------------------
  473. void ctkConsolePrivate::insertCompletion(const QString& completion)
  474. {
  475. QTextCursor tc = this->textCursor();
  476. tc.movePosition(QTextCursor::Left, QTextCursor::KeepAnchor);
  477. if (tc.selectedText()==".")
  478. {
  479. tc.insertText(QString(".") + completion);
  480. }
  481. else
  482. {
  483. tc = this->textCursor();
  484. tc.movePosition(QTextCursor::StartOfWord, QTextCursor::MoveAnchor);
  485. tc.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
  486. tc.insertText(completion);
  487. this->setTextCursor(tc);
  488. }
  489. this->updateCommandBuffer();
  490. }
  491. //-----------------------------------------------------------------------------
  492. // ctkConsole methods
  493. //-----------------------------------------------------------------------------
  494. ctkConsole::ctkConsole(QWidget* parentObject) :
  495. QWidget(parentObject),
  496. d_ptr(new ctkConsolePrivate(*this))
  497. {
  498. Q_D(ctkConsole);
  499. d->init();
  500. }
  501. //-----------------------------------------------------------------------------
  502. ctkConsole::ctkConsole(ctkConsolePrivate * pimpl, QWidget* parentObject) :
  503. QWidget(parentObject), d_ptr(pimpl)
  504. {
  505. Q_D(ctkConsole);
  506. d->init();
  507. }
  508. //-----------------------------------------------------------------------------
  509. ctkConsole::~ctkConsole()
  510. {
  511. }
  512. //-----------------------------------------------------------------------------
  513. QTextCharFormat ctkConsole::getFormat() const
  514. {
  515. Q_D(const ctkConsole);
  516. return d->currentCharFormat();
  517. }
  518. //-----------------------------------------------------------------------------
  519. void ctkConsole::setFormat(const QTextCharFormat& Format)
  520. {
  521. Q_D(ctkConsole);
  522. d->setCurrentCharFormat(Format);
  523. }
  524. //-----------------------------------------------------------------------------
  525. void ctkConsole::setCompleter(ctkConsoleCompleter* completer)
  526. {
  527. Q_D(ctkConsole);
  528. d->setCompleter(completer);
  529. }
  530. //-----------------------------------------------------------------------------
  531. CTK_GET_CPP(ctkConsole, QColor, promptColor, PromptColor);
  532. CTK_SET_CPP(ctkConsole, const QColor&, setPromptColor, PromptColor);
  533. //-----------------------------------------------------------------------------
  534. CTK_GET_CPP(ctkConsole, QColor, outputTextColor, OutputTextColor);
  535. CTK_SET_CPP(ctkConsole, const QColor&, setOutputTextColor, OutputTextColor);
  536. //-----------------------------------------------------------------------------
  537. CTK_GET_CPP(ctkConsole, QColor, errorTextColor, ErrorTextColor);
  538. CTK_SET_CPP(ctkConsole, const QColor&, setErrorTextColor, ErrorTextColor);
  539. //-----------------------------------------------------------------------------
  540. CTK_GET_CPP(ctkConsole, QColor, commandTextColor, CommandTextColor);
  541. CTK_SET_CPP(ctkConsole, const QColor&, setCommandTextColor, CommandTextColor);
  542. //-----------------------------------------------------------------------------
  543. CTK_GET_CPP(ctkConsole, QColor, welcomeTextColor, WelcomeTextColor);
  544. CTK_SET_CPP(ctkConsole, const QColor&, setWelcomeTextColor, WelcomeTextColor);
  545. //-----------------------------------------------------------------------------
  546. QColor ctkConsole::textBackgroundColor()const
  547. {
  548. Q_D(const ctkConsole);
  549. return d->textBackgroundColor();
  550. }
  551. //-----------------------------------------------------------------------------
  552. void ctkConsole::setTextBackgroundColor(const QColor& newColor)
  553. {
  554. Q_D(ctkConsole);
  555. return d->setTextBackgroundColor(newColor);
  556. }
  557. //-----------------------------------------------------------------------------
  558. CTK_GET_CPP(ctkConsole, QString, ps1, Ps1);
  559. CTK_SET_CPP(ctkConsole, const QString&, setPs1, Ps1);
  560. //-----------------------------------------------------------------------------
  561. CTK_GET_CPP(ctkConsole, QString, ps2, Ps2);
  562. CTK_SET_CPP(ctkConsole, const QString&, setPs2, Ps2);
  563. //-----------------------------------------------------------------------------
  564. CTK_GET_CPP(ctkConsole, ctkConsole::EditorHints, editorHints, EditorHints);
  565. CTK_SET_CPP(ctkConsole, const ctkConsole::EditorHints&, setEditorHints, EditorHints);
  566. //-----------------------------------------------------------------------------
  567. Qt::ScrollBarPolicy ctkConsole::scrollBarPolicy()const
  568. {
  569. Q_D(const ctkConsole);
  570. return d->verticalScrollBarPolicy();
  571. }
  572. //-----------------------------------------------------------------------------
  573. void ctkConsole::setScrollBarPolicy(const Qt::ScrollBarPolicy& newScrollBarPolicy)
  574. {
  575. Q_D(ctkConsole);
  576. d->setVerticalScrollBarPolicy(newScrollBarPolicy);
  577. }
  578. //-----------------------------------------------------------------------------
  579. void ctkConsole::executeCommand(const QString& command)
  580. {
  581. qWarning() << "ctkConsole::executeCommand not implemented !";
  582. qWarning() << "command:" << command;
  583. }
  584. //----------------------------------------------------------------------------
  585. void ctkConsole::printMessage(const QString& message, const QColor& color)
  586. {
  587. Q_D(ctkConsole);
  588. QTextCharFormat format = this->getFormat();
  589. format.setForeground(color);
  590. this->setFormat(format);
  591. d->printString(message);
  592. }
  593. //-----------------------------------------------------------------------------
  594. void ctkConsole::clear()
  595. {
  596. Q_D(ctkConsole);
  597. d->clear();
  598. // For some reason the QCompleter tries to set the focus policy to
  599. // NoFocus, set let's make sure we set it back to the default WheelFocus.
  600. d->setFocusPolicy(Qt::WheelFocus);
  601. d->promptForInput();
  602. }
  603. //-----------------------------------------------------------------------------
  604. void ctkConsole::reset()
  605. {
  606. Q_D(ctkConsole);
  607. d->clear();
  608. // For some reason the QCompleter tries to set the focus policy to
  609. // NoFocus, set let's make sure we set it back to the default WheelFocus.
  610. d->setFocusPolicy(Qt::WheelFocus);
  611. d->printWelcomeMessage();
  612. d->promptForInput();
  613. }