ctkConsole.cpp 27 KB

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