ctkConsole.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  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. // ctkConsoleCompleter methods
  56. //-----------------------------------------------------------------------------
  57. QStringList ctkConsoleCompleter::autocompletePreferenceList()
  58. {
  59. return this->AutocompletePreferenceList;
  60. }
  61. //-----------------------------------------------------------------------------
  62. void ctkConsoleCompleter::setAutocompletePreferenceList(const QStringList& list)
  63. {
  64. this->AutocompletePreferenceList = list;
  65. }
  66. //-----------------------------------------------------------------------------
  67. // ctkConsolePrivate methods
  68. //-----------------------------------------------------------------------------
  69. ctkConsolePrivate::ctkConsolePrivate(ctkConsole& object) :
  70. Superclass(0),
  71. q_ptr(&object),
  72. InteractivePosition(documentEnd()),
  73. MultilineStatement(false), Ps1("$ "), Ps2("> "),
  74. EditorHints(ctkConsole::AutomaticIndentation | ctkConsole::RemoveTrailingSpaces),
  75. ScrollbarAtBottom(false)
  76. {
  77. }
  78. //-----------------------------------------------------------------------------
  79. void ctkConsolePrivate::init()
  80. {
  81. Q_Q(ctkConsole);
  82. this->setParent(q);
  83. this->setTabChangesFocus(false);
  84. this->setAcceptDrops(false);
  85. this->setAcceptRichText(false);
  86. this->setUndoRedoEnabled(false);
  87. this->PromptColor = QColor(0, 0, 0); // Black
  88. this->OutputTextColor = QColor(0, 150, 0); // Green
  89. this->ErrorTextColor = QColor(255, 0, 0); // Red
  90. this->StdinTextColor = QColor(Qt::darkGray);
  91. this->CommandTextColor = QColor(0, 0, 150); // Blue
  92. this->WelcomeTextColor = QColor(0, 0, 255); // Dark Blue
  93. QFont f;
  94. f.setFamily("Courier");
  95. f.setStyleHint(QFont::TypeWriter);
  96. f.setFixedPitch(true);
  97. QTextCharFormat format;
  98. format.setFont(f);
  99. format.setForeground(this->OutputTextColor);
  100. this->setCurrentCharFormat(format);
  101. this->CommandHistory.append("");
  102. this->CommandPosition = 0;
  103. QVBoxLayout * layout = new QVBoxLayout(q);
  104. layout->setMargin(0);
  105. layout->addWidget(this);
  106. connect(this->verticalScrollBar(), SIGNAL(valueChanged(int)),
  107. SLOT(onScrollBarValueChanged(int)));
  108. }
  109. //-----------------------------------------------------------------------------
  110. void ctkConsolePrivate::keyPressEvent(QKeyEvent* e)
  111. {
  112. if (this->Completer && this->Completer->popup()->isVisible())
  113. {
  114. // The following keys are forwarded by the completer to the widget
  115. switch (e->key())
  116. {
  117. case Qt::Key_Enter:
  118. case Qt::Key_Return:
  119. case Qt::Key_Escape:
  120. case Qt::Key_Tab:
  121. case Qt::Key_Backtab:
  122. e->ignore();
  123. return; // let the completer do default behavior
  124. default:
  125. break;
  126. }
  127. }
  128. QTextCursor text_cursor = this->textCursor();
  129. // Set to true if there's a current selection
  130. const bool selection = text_cursor.anchor() != text_cursor.position();
  131. // Set to true if the cursor overlaps the history area
  132. const bool history_area =
  133. text_cursor.anchor() < this->InteractivePosition
  134. || text_cursor.position() < this->InteractivePosition;
  135. // Allow copying anywhere in the console ...
  136. if(e->key() == Qt::Key_C && e->modifiers() == Qt::ControlModifier)
  137. {
  138. if(selection)
  139. {
  140. this->copy();
  141. }
  142. e->accept();
  143. return;
  144. }
  145. // Allow cut only if the selection is limited to the interactive area ...
  146. if(e->key() == Qt::Key_X && e->modifiers() == Qt::ControlModifier)
  147. {
  148. if(selection && !history_area)
  149. {
  150. this->cut();
  151. }
  152. e->accept();
  153. return;
  154. }
  155. // Allow paste only if the selection is in the interactive area ...
  156. if(e->key() == Qt::Key_V && e->modifiers() == Qt::ControlModifier)
  157. {
  158. if(!history_area)
  159. {
  160. const QMimeData* const clipboard = QApplication::clipboard()->mimeData();
  161. const QString text = clipboard->text();
  162. if(!text.isNull())
  163. {
  164. if (this->EditorHints & ctkConsole::SplitCopiedTextByLine)
  165. {
  166. QStringList lines = text.split(QRegExp("(?:\r\n|\r|\n)"));
  167. for(int i=0; i < lines.count(); ++i)
  168. {
  169. this->switchToUserInputTextColor(&text_cursor);
  170. text_cursor.insertText(lines.at(i));
  171. this->updateCommandBuffer();
  172. if (i < lines.count() - 1)
  173. {
  174. this->internalExecuteCommand();
  175. }
  176. }
  177. }
  178. else
  179. {
  180. this->switchToUserInputTextColor(&text_cursor);
  181. text_cursor.insertText(text);
  182. this->updateCommandBuffer();
  183. }
  184. }
  185. }
  186. e->accept();
  187. return;
  188. }
  189. // Force the cursor back to the interactive area
  190. if(history_area && e->key() != Qt::Key_Control)
  191. {
  192. text_cursor.setPosition(this->documentEnd());
  193. this->setTextCursor(text_cursor);
  194. }
  195. switch(e->key())
  196. {
  197. case Qt::Key_Up:
  198. e->accept();
  199. if (this->CommandPosition > 0)
  200. {
  201. this->replaceCommandBuffer(this->CommandHistory[--this->CommandPosition]);
  202. }
  203. break;
  204. case Qt::Key_Down:
  205. e->accept();
  206. if (this->CommandPosition < this->CommandHistory.size() - 2)
  207. {
  208. this->replaceCommandBuffer(this->CommandHistory[++this->CommandPosition]);
  209. }
  210. else
  211. {
  212. this->CommandPosition = this->CommandHistory.size()-1;
  213. this->replaceCommandBuffer("");
  214. }
  215. break;
  216. case Qt::Key_Left:
  217. if (text_cursor.position() > this->InteractivePosition)
  218. {
  219. this->Superclass::keyPressEvent(e);
  220. }
  221. else
  222. {
  223. e->accept();
  224. }
  225. break;
  226. case Qt::Key_Delete:
  227. e->accept();
  228. this->Superclass::keyPressEvent(e);
  229. this->updateCommandBuffer();
  230. break;
  231. case Qt::Key_Backspace:
  232. e->accept();
  233. if(text_cursor.position() > this->InteractivePosition)
  234. {
  235. this->Superclass::keyPressEvent(e);
  236. this->updateCommandBuffer();
  237. this->updateCompleterIfVisible();
  238. }
  239. break;
  240. case Qt::Key_Tab:
  241. e->accept();
  242. this->updateCompleter();
  243. this->selectCompletion();
  244. break;
  245. case Qt::Key_Home:
  246. e->accept();
  247. text_cursor.setPosition(this->InteractivePosition);
  248. this->setTextCursor(text_cursor);
  249. break;
  250. case Qt::Key_Return:
  251. case Qt::Key_Enter:
  252. e->accept();
  253. text_cursor.setPosition(this->documentEnd());
  254. this->setTextCursor(text_cursor);
  255. if (this->InputEventLoop.isNull())
  256. {
  257. this->internalExecuteCommand();
  258. }
  259. else
  260. {
  261. this->processInput();
  262. }
  263. break;
  264. default:
  265. e->accept();
  266. this->switchToUserInputTextColor();
  267. this->Superclass::keyPressEvent(e);
  268. this->updateCommandBuffer();
  269. this->updateCompleterIfVisible();
  270. break;
  271. }
  272. }
  273. //-----------------------------------------------------------------------------
  274. void ctkConsolePrivate::switchToUserInputTextColor(QTextCursor* textCursorToUpdate)
  275. {
  276. QColor color = this->CommandTextColor;
  277. if (!this->InputEventLoop.isNull())
  278. {
  279. color = this->StdinTextColor;
  280. }
  281. QTextCharFormat currentFormat = this->currentCharFormat();
  282. currentFormat.setForeground(color);
  283. this->setCurrentCharFormat(currentFormat);
  284. if (textCursorToUpdate)
  285. {
  286. QTextCharFormat textCursorFormat = textCursorToUpdate->charFormat();
  287. textCursorFormat.setForeground(color);
  288. textCursorToUpdate->setCharFormat(textCursorFormat);
  289. }
  290. }
  291. //-----------------------------------------------------------------------------
  292. int ctkConsolePrivate::documentEnd() const
  293. {
  294. QTextCursor c(this->document());
  295. c.movePosition(QTextCursor::End);
  296. return c.position();
  297. }
  298. //-----------------------------------------------------------------------------
  299. void ctkConsolePrivate::focusOutEvent(QFocusEvent *e)
  300. {
  301. this->Superclass::focusOutEvent(e);
  302. // For some reason the QCompleter tries to set the focus policy to
  303. // NoFocus, set let's make sure we set it back to the default WheelFocus.
  304. this->setFocusPolicy(Qt::WheelFocus);
  305. }
  306. //-----------------------------------------------------------------------------
  307. void ctkConsolePrivate::resizeEvent(QResizeEvent * e)
  308. {
  309. this->Superclass::resizeEvent(e);
  310. if (this->ScrollbarAtBottom)
  311. {
  312. this->moveCursor(QTextCursor::End);
  313. this->scrollToBottom();
  314. }
  315. }
  316. //-----------------------------------------------------------------------------
  317. void ctkConsolePrivate::scrollToBottom()
  318. {
  319. this->verticalScrollBar()->setValue(this->verticalScrollBar()->maximum());
  320. }
  321. //-----------------------------------------------------------------------------
  322. void ctkConsolePrivate::updateCompleterIfVisible()
  323. {
  324. if (this->Completer && this->Completer->popup()->isVisible())
  325. {
  326. this->updateCompleter();
  327. }
  328. }
  329. //-----------------------------------------------------------------------------
  330. void ctkConsolePrivate::selectCompletion()
  331. {
  332. if (this->Completer && this->Completer->completionCount() == 1)
  333. {
  334. this->insertCompletion(this->Completer->currentCompletion());
  335. this->Completer->popup()->hide();
  336. }
  337. }
  338. //-----------------------------------------------------------------------------
  339. void ctkConsolePrivate::setCompleter(ctkConsoleCompleter* completer)
  340. {
  341. if (this->Completer)
  342. {
  343. this->Completer->setWidget(0);
  344. disconnect(this->Completer, SIGNAL(activated(const QString&)),
  345. this, SLOT(insertCompletion(const QString&)));
  346. }
  347. this->Completer = completer;
  348. if (this->Completer)
  349. {
  350. this->Completer->setWidget(this);
  351. connect(this->Completer, SIGNAL(activated(const QString&)),
  352. this, SLOT(insertCompletion(const QString&)));
  353. }
  354. }
  355. //-----------------------------------------------------------------------------
  356. void ctkConsolePrivate::updateCompleter()
  357. {
  358. if (this->Completer)
  359. {
  360. // Get the text between the current cursor position
  361. // and the start of the line
  362. QTextCursor text_cursor = this->textCursor();
  363. text_cursor.setPosition(this->InteractivePosition, QTextCursor::KeepAnchor);
  364. QString commandText = text_cursor.selectedText();
  365. // Call the completer to update the completion model
  366. this->Completer->updateCompletionModel(commandText);
  367. // Place and show the completer if there are available completions
  368. if (this->Completer->completionCount())
  369. {
  370. // Get a QRect for the cursor at the start of the
  371. // current word and then translate it down 8 pixels.
  372. text_cursor = this->textCursor();
  373. text_cursor.movePosition(QTextCursor::StartOfWord);
  374. QRect cr = this->cursorRect(text_cursor);
  375. cr.translate(0,8);
  376. cr.setWidth(this->Completer->popup()->sizeHintForColumn(0)
  377. + this->Completer->popup()->verticalScrollBar()->sizeHint().width());
  378. this->Completer->complete(cr);
  379. }
  380. else
  381. {
  382. this->Completer->popup()->hide();
  383. }
  384. }
  385. }
  386. //-----------------------------------------------------------------------------
  387. void ctkConsolePrivate::updateCommandBuffer()
  388. {
  389. this->commandBuffer() = this->toPlainText().mid(this->InteractivePosition);
  390. }
  391. //-----------------------------------------------------------------------------
  392. void ctkConsolePrivate::replaceCommandBuffer(const QString& text)
  393. {
  394. this->commandBuffer() = text;
  395. QTextCursor c(this->document());
  396. c.setPosition(this->InteractivePosition);
  397. c.movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
  398. c.removeSelectedText();
  399. this->switchToUserInputTextColor(&c);
  400. c.insertText(text);
  401. }
  402. //-----------------------------------------------------------------------------
  403. QString& ctkConsolePrivate::commandBuffer()
  404. {
  405. return this->CommandHistory.back();
  406. }
  407. //-----------------------------------------------------------------------------
  408. void ctkConsolePrivate::internalExecuteCommand()
  409. {
  410. Q_Q(ctkConsole);
  411. QString command = this->commandBuffer();
  412. if (this->EditorHints & ctkConsole::RemoveTrailingSpaces)
  413. {
  414. command.replace(QRegExp("\\s*$"), ""); // Remove trailing spaces
  415. this->commandBuffer() = command; // Update buffer
  416. }
  417. // First update the history cache. It's essential to update the
  418. // this->CommandPosition before calling internalExecuteCommand() since that
  419. // can result in a clearing of the current command (BUG #8765).
  420. if (!command.isEmpty()) // Don't store empty commands in the history
  421. {
  422. this->CommandHistory.push_back("");
  423. this->CommandPosition = this->CommandHistory.size() - 1;
  424. }
  425. QTextCursor c(this->document());
  426. c.movePosition(QTextCursor::End);
  427. c.insertText("\n");
  428. this->InteractivePosition = this->documentEnd();
  429. emit q->executing(true);
  430. q->executeCommand(command);
  431. emit q->executing(false);
  432. // Find the indent for the command.
  433. QString indent;
  434. if (this->EditorHints & ctkConsole::AutomaticIndentation)
  435. {
  436. QRegExp regExp("^(\\s+)");
  437. if (regExp.indexIn(command) != -1)
  438. {
  439. indent = regExp.cap(1);
  440. }
  441. }
  442. this->promptForInput(indent);
  443. }
  444. //-----------------------------------------------------------------------------
  445. void ctkConsolePrivate::processInput()
  446. {
  447. QString command = this->commandBuffer();
  448. if (this->EditorHints & ctkConsole::RemoveTrailingSpaces)
  449. {
  450. command.replace(QRegExp("\\s*$"), ""); // Remove trailing spaces
  451. this->commandBuffer() = command; // Update buffer
  452. }
  453. QTextCursor c(this->document());
  454. c.movePosition(QTextCursor::End);
  455. c.insertText("\n");
  456. this->InteractivePosition = this->documentEnd();
  457. this->InputEventLoop->exit();
  458. }
  459. //-----------------------------------------------------------------------------
  460. void ctkConsolePrivate::printString(const QString& text)
  461. {
  462. this->textCursor().movePosition(QTextCursor::End);
  463. this->textCursor().insertText(text);
  464. this->InteractivePosition = this->documentEnd();
  465. this->ensureCursorVisible();
  466. this->scrollToBottom();
  467. }
  468. //----------------------------------------------------------------------------
  469. void ctkConsolePrivate::printOutputMessage(const QString& text)
  470. {
  471. Q_Q(ctkConsole);
  472. q->printMessage(text, q->outputTextColor());
  473. }
  474. //----------------------------------------------------------------------------
  475. void ctkConsolePrivate::printErrorMessage(const QString& text)
  476. {
  477. Q_Q(ctkConsole);
  478. q->printMessage(text, q->errorTextColor());
  479. }
  480. //-----------------------------------------------------------------------------
  481. void ctkConsolePrivate::printCommand(const QString& cmd)
  482. {
  483. this->textCursor().insertText(cmd);
  484. this->updateCommandBuffer();
  485. }
  486. //----------------------------------------------------------------------------
  487. void ctkConsolePrivate::promptForInput(const QString& indent)
  488. {
  489. Q_Q(ctkConsole);
  490. QTextCharFormat format = q->getFormat();
  491. format.setForeground(q->promptColor());
  492. q->setFormat(format);
  493. if(!this->MultilineStatement)
  494. {
  495. this->prompt(q->ps1());
  496. }
  497. else
  498. {
  499. this->prompt(q->ps2());
  500. }
  501. this->printCommand(indent);
  502. }
  503. //-----------------------------------------------------------------------------
  504. void ctkConsolePrivate::prompt(const QString& text)
  505. {
  506. QTextCursor text_cursor = this->textCursor();
  507. // If the cursor is currently on a clean line, do nothing, otherwise we move
  508. // the cursor to a new line before showing the prompt.
  509. text_cursor.movePosition(QTextCursor::StartOfLine);
  510. int startpos = text_cursor.position();
  511. text_cursor.movePosition(QTextCursor::EndOfLine);
  512. int endpos = text_cursor.position();
  513. if (endpos != startpos)
  514. {
  515. this->textCursor().insertText("\n");
  516. }
  517. this->textCursor().insertText(text);
  518. this->InteractivePosition = this->documentEnd();
  519. this->ensureCursorVisible();
  520. this->scrollToBottom();
  521. }
  522. //----------------------------------------------------------------------------
  523. void ctkConsolePrivate::printWelcomeMessage()
  524. {
  525. Q_Q(ctkConsole);
  526. q->printMessage(
  527. QLatin1String("CTK Console"),
  528. q->welcomeTextColor());
  529. }
  530. //-----------------------------------------------------------------------------
  531. void ctkConsolePrivate::insertCompletion(const QString& completion)
  532. {
  533. QTextCursor tc = this->textCursor();
  534. tc.movePosition(QTextCursor::Left, QTextCursor::KeepAnchor);
  535. if (tc.selectedText()==".")
  536. {
  537. tc.insertText(QString(".") + completion);
  538. }
  539. else
  540. {
  541. tc = this->textCursor();
  542. tc.movePosition(QTextCursor::StartOfWord, QTextCursor::MoveAnchor);
  543. tc.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
  544. tc.insertText(completion);
  545. this->setTextCursor(tc);
  546. }
  547. this->updateCommandBuffer();
  548. }
  549. //-----------------------------------------------------------------------------
  550. void ctkConsolePrivate::onScrollBarValueChanged(int value)
  551. {
  552. this->ScrollbarAtBottom = (this->verticalScrollBar()->maximum() == value);
  553. }
  554. //-----------------------------------------------------------------------------
  555. // ctkConsole methods
  556. //-----------------------------------------------------------------------------
  557. ctkConsole::ctkConsole(QWidget* parentObject) :
  558. Superclass(parentObject),
  559. d_ptr(new ctkConsolePrivate(*this))
  560. {
  561. Q_D(ctkConsole);
  562. d->init();
  563. }
  564. //-----------------------------------------------------------------------------
  565. ctkConsole::ctkConsole(ctkConsolePrivate * pimpl, QWidget* parentObject) :
  566. QWidget(parentObject), d_ptr(pimpl)
  567. {
  568. Q_D(ctkConsole);
  569. d->init();
  570. }
  571. //-----------------------------------------------------------------------------
  572. ctkConsole::~ctkConsole()
  573. {
  574. }
  575. //-----------------------------------------------------------------------------
  576. QTextCharFormat ctkConsole::getFormat() const
  577. {
  578. Q_D(const ctkConsole);
  579. return d->currentCharFormat();
  580. }
  581. //-----------------------------------------------------------------------------
  582. void ctkConsole::setFormat(const QTextCharFormat& Format)
  583. {
  584. Q_D(ctkConsole);
  585. d->setCurrentCharFormat(Format);
  586. }
  587. //-----------------------------------------------------------------------------
  588. CTK_GET_CPP(ctkConsole, ctkConsoleCompleter*, completer, Completer);
  589. //-----------------------------------------------------------------------------
  590. void ctkConsole::setCompleter(ctkConsoleCompleter* completer)
  591. {
  592. Q_D(ctkConsole);
  593. d->setCompleter(completer);
  594. }
  595. //-----------------------------------------------------------------------------
  596. CTK_GET_CPP(ctkConsole, QColor, promptColor, PromptColor);
  597. CTK_SET_CPP(ctkConsole, const QColor&, setPromptColor, PromptColor);
  598. //-----------------------------------------------------------------------------
  599. CTK_GET_CPP(ctkConsole, QColor, outputTextColor, OutputTextColor);
  600. CTK_SET_CPP(ctkConsole, const QColor&, setOutputTextColor, OutputTextColor);
  601. //-----------------------------------------------------------------------------
  602. CTK_GET_CPP(ctkConsole, QColor, errorTextColor, ErrorTextColor);
  603. CTK_SET_CPP(ctkConsole, const QColor&, setErrorTextColor, ErrorTextColor);
  604. //-----------------------------------------------------------------------------
  605. CTK_GET_CPP(ctkConsole, QColor, stdinTextColor, StdinTextColor);
  606. CTK_SET_CPP(ctkConsole, const QColor&, setStdinTextColor, StdinTextColor);
  607. //-----------------------------------------------------------------------------
  608. CTK_GET_CPP(ctkConsole, QColor, commandTextColor, CommandTextColor);
  609. CTK_SET_CPP(ctkConsole, const QColor&, setCommandTextColor, CommandTextColor);
  610. //-----------------------------------------------------------------------------
  611. CTK_GET_CPP(ctkConsole, QColor, welcomeTextColor, WelcomeTextColor);
  612. CTK_SET_CPP(ctkConsole, const QColor&, setWelcomeTextColor, WelcomeTextColor);
  613. //-----------------------------------------------------------------------------
  614. QColor ctkConsole::backgroundColor()const
  615. {
  616. Q_D(const ctkConsole);
  617. QPalette pal = d->palette();
  618. return pal.color(QPalette::Base);
  619. }
  620. //-----------------------------------------------------------------------------
  621. void ctkConsole::setBackgroundColor(const QColor& newColor)
  622. {
  623. Q_D(ctkConsole);
  624. if (this->backgroundColor() == newColor)
  625. {
  626. return;
  627. }
  628. QPalette pal = this->palette();
  629. pal.setColor(QPalette::Base, newColor);
  630. d->setPalette(pal);
  631. }
  632. //-----------------------------------------------------------------------------
  633. CTK_GET_CPP(ctkConsole, QString, ps1, Ps1);
  634. CTK_SET_CPP(ctkConsole, const QString&, setPs1, Ps1);
  635. //-----------------------------------------------------------------------------
  636. CTK_GET_CPP(ctkConsole, QString, ps2, Ps2);
  637. CTK_SET_CPP(ctkConsole, const QString&, setPs2, Ps2);
  638. //-----------------------------------------------------------------------------
  639. CTK_GET_CPP(ctkConsole, ctkConsole::EditorHints, editorHints, EditorHints);
  640. CTK_SET_CPP(ctkConsole, const ctkConsole::EditorHints&, setEditorHints, EditorHints);
  641. //-----------------------------------------------------------------------------
  642. Qt::ScrollBarPolicy ctkConsole::scrollBarPolicy()const
  643. {
  644. Q_D(const ctkConsole);
  645. return d->verticalScrollBarPolicy();
  646. }
  647. //-----------------------------------------------------------------------------
  648. void ctkConsole::setScrollBarPolicy(const Qt::ScrollBarPolicy& newScrollBarPolicy)
  649. {
  650. Q_D(ctkConsole);
  651. d->setVerticalScrollBarPolicy(newScrollBarPolicy);
  652. }
  653. //-----------------------------------------------------------------------------
  654. void ctkConsole::executeCommand(const QString& command)
  655. {
  656. qWarning() << "ctkConsole::executeCommand not implemented !";
  657. qWarning() << "command:" << command;
  658. }
  659. //----------------------------------------------------------------------------
  660. void ctkConsole::printMessage(const QString& message, const QColor& color)
  661. {
  662. Q_D(ctkConsole);
  663. QTextCharFormat format = this->getFormat();
  664. format.setForeground(color);
  665. this->setFormat(format);
  666. d->printString(message);
  667. }
  668. //-----------------------------------------------------------------------------
  669. void ctkConsole::clear()
  670. {
  671. Q_D(ctkConsole);
  672. d->clear();
  673. // For some reason the QCompleter tries to set the focus policy to
  674. // NoFocus, set let's make sure we set it back to the default WheelFocus.
  675. d->setFocusPolicy(Qt::WheelFocus);
  676. d->promptForInput();
  677. }
  678. //-----------------------------------------------------------------------------
  679. void ctkConsole::reset()
  680. {
  681. Q_D(ctkConsole);
  682. d->clear();
  683. // For some reason the QCompleter tries to set the focus policy to
  684. // NoFocus, set let's make sure we set it back to the default WheelFocus.
  685. d->setFocusPolicy(Qt::WheelFocus);
  686. d->printWelcomeMessage();
  687. d->promptForInput();
  688. }
  689. //-----------------------------------------------------------------------------
  690. QString ctkConsole::stdInRedirectCallBack(void * callData)
  691. {
  692. ctkConsole * self = reinterpret_cast<ctkConsole*>(callData);
  693. Q_ASSERT(self);
  694. if (!self)
  695. {
  696. return QLatin1String("");
  697. }
  698. return self->readInputLine();
  699. }
  700. namespace
  701. {
  702. class InputEventLoop : public QEventLoop
  703. {
  704. public:
  705. InputEventLoop(QApplication * app, QObject * parentObject = 0) :
  706. QEventLoop(parentObject), App(app){}
  707. virtual bool processEvents(ProcessEventsFlags flags = AllEvents)
  708. {
  709. this->App->processEvents(flags);
  710. return true;
  711. }
  712. QApplication * App;
  713. };
  714. }
  715. //-----------------------------------------------------------------------------
  716. QString ctkConsole::readInputLine()
  717. {
  718. Q_D(ctkConsole);
  719. d->moveCursor(QTextCursor::End);
  720. QScopedPointer<InputEventLoop> eventLoop(new InputEventLoop(qApp));
  721. d->InputEventLoop = QPointer<QEventLoop>(eventLoop.data());
  722. eventLoop->exec();
  723. return d->commandBuffer();
  724. }