ctkConsole.cpp 26 KB

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