ctkConsole.cpp 27 KB

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