ctkConsole.cpp 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348
  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 <QAction>
  41. #include <QApplication>
  42. #include <QClipboard>
  43. #include <QCompleter>
  44. #include <QFile>
  45. #include <QFileDialog>
  46. #include <QKeyEvent>
  47. #include <QMimeData>
  48. #include <QPointer>
  49. #include <QPushButton>
  50. #include <QTextBlock>
  51. #include <QTextCursor>
  52. #include <QVBoxLayout>
  53. #include <QScrollBar>
  54. #include <QDebug>
  55. // CTK includes
  56. #include "ctkConsole.h"
  57. #include "ctkConsole_p.h"
  58. #include "ctkPimpl.h"
  59. //-----------------------------------------------------------------------------
  60. // ctkConsoleCompleter methods
  61. //-----------------------------------------------------------------------------
  62. QStringList ctkConsoleCompleter::autocompletePreferenceList()
  63. {
  64. return this->AutocompletePreferenceList;
  65. }
  66. //-----------------------------------------------------------------------------
  67. void ctkConsoleCompleter::setAutocompletePreferenceList(const QStringList& list)
  68. {
  69. this->AutocompletePreferenceList = list;
  70. }
  71. //-----------------------------------------------------------------------------
  72. // ctkConsolePrivate methods
  73. //-----------------------------------------------------------------------------
  74. ctkConsolePrivate::ctkConsolePrivate(ctkConsole& object) :
  75. Superclass(0),
  76. q_ptr(&object),
  77. InteractivePosition(documentEnd()),
  78. MessageOutputSize(0),
  79. MultilineStatement(false), Ps1("$ "), Ps2("> "),
  80. EditorHints(ctkConsole::AutomaticIndentation | ctkConsole::RemoveTrailingSpaces),
  81. ScrollbarAtBottom(false),
  82. CompleterShortcuts(QList<QKeySequence>() << Qt::Key_Tab),
  83. RunFileOptions(ctkConsole::RunFileShortcut),
  84. RunFileButton(NULL),
  85. RunFileAction(NULL)
  86. {
  87. }
  88. //-----------------------------------------------------------------------------
  89. void ctkConsolePrivate::init()
  90. {
  91. Q_Q(ctkConsole);
  92. this->setParent(q);
  93. this->setTabChangesFocus(false);
  94. this->setAcceptDrops(false);
  95. this->setAcceptRichText(false);
  96. this->setUndoRedoEnabled(false);
  97. this->PromptColor = QColor(0, 0, 0); // Black
  98. this->OutputTextColor = QColor(0, 150, 0); // Green
  99. this->MessageOutputColor = QColor(Qt::gray); // Gray
  100. this->ErrorTextColor = QColor(255, 0, 0); // Red
  101. this->StdinTextColor = QColor(Qt::darkGray);
  102. this->CommandTextColor = QColor(0, 0, 150); // Blue
  103. this->WelcomeTextColor = QColor(0, 0, 255); // Dark Blue
  104. QFont shellFont;
  105. shellFont.setFamily("Courier");
  106. shellFont.setStyleHint(QFont::TypeWriter);
  107. shellFont.setFixedPitch(true);
  108. QTextCharFormat format;
  109. format.setFont(shellFont);
  110. format.setForeground(this->OutputTextColor);
  111. this->setCurrentCharFormat(format);
  112. this->CommandHistory.append("");
  113. this->CommandPosition = 0;
  114. this->RunFileAction = new QAction(q->tr("&Run file"), q);
  115. this->RunFileAction->setShortcut(q->tr("Ctrl+r"));
  116. connect(this->RunFileAction, SIGNAL(triggered()), q, SLOT(runFile()));
  117. q->addAction(this->RunFileAction);
  118. QAction* printHelpAction = new QAction(q->tr("Print &help"),q);
  119. printHelpAction->setShortcut(q->tr("Ctrl+h"));
  120. connect(printHelpAction, SIGNAL(triggered()), q, SLOT(printHelp()));
  121. q->addAction(printHelpAction);
  122. this->RunFileButton = new QPushButton(q);
  123. this->RunFileButton->setText(q->tr("&Run script from file"));
  124. this->RunFileButton->setVisible(false);
  125. QVBoxLayout * layout = new QVBoxLayout(q);
  126. layout->setMargin(0);
  127. layout->setSpacing(0);
  128. layout->addWidget(this);
  129. layout->addWidget(this->RunFileButton);
  130. connect(this->verticalScrollBar(), SIGNAL(valueChanged(int)),
  131. SLOT(onScrollBarValueChanged(int)));
  132. connect(this, SIGNAL(textChanged()), SLOT(onTextChanged()));
  133. connect(this->RunFileButton, SIGNAL(clicked()), q, SLOT(runFile()));
  134. connect(this, SIGNAL(cursorPositionChanged()),
  135. q, SIGNAL(cursorPositionChanged()));
  136. }
  137. //-----------------------------------------------------------------------------
  138. bool ctkConsolePrivate::isMoveLeftWithinLine(QKeyEvent* e, QTextCursor::MoveOperation &moveOperation, QTextCursor::MoveMode &moveMode)
  139. {
  140. if (e == QKeySequence::MoveToPreviousChar)
  141. {
  142. moveOperation = QTextCursor::Left;
  143. moveMode = QTextCursor::MoveAnchor;
  144. return true;
  145. }
  146. else if (e == QKeySequence::SelectPreviousChar)
  147. {
  148. moveOperation = QTextCursor::Left;
  149. moveMode = QTextCursor::KeepAnchor;
  150. return true;
  151. }
  152. else if (e == QKeySequence::MoveToPreviousWord)
  153. {
  154. moveOperation = QTextCursor::WordLeft;
  155. moveMode = QTextCursor::MoveAnchor;
  156. return true;
  157. }
  158. else if (e == QKeySequence::SelectPreviousWord)
  159. {
  160. moveOperation = QTextCursor::WordLeft;
  161. moveMode = QTextCursor::KeepAnchor;
  162. return true;
  163. }
  164. else if (e == QKeySequence::MoveToStartOfLine)
  165. {
  166. moveOperation = QTextCursor::StartOfLine;
  167. moveMode = QTextCursor::MoveAnchor;
  168. return true;
  169. }
  170. else if (e == QKeySequence::SelectStartOfLine)
  171. {
  172. moveOperation = QTextCursor::StartOfLine;
  173. moveMode = QTextCursor::KeepAnchor;
  174. return true;
  175. }
  176. else if (e == QKeySequence::MoveToStartOfBlock)
  177. {
  178. moveOperation = QTextCursor::StartOfLine;
  179. moveMode = QTextCursor::MoveAnchor;
  180. return true;
  181. }
  182. else if (e == QKeySequence::SelectStartOfBlock)
  183. {
  184. moveOperation = QTextCursor::StartOfLine;
  185. moveMode = QTextCursor::KeepAnchor;
  186. return true;
  187. }
  188. else if (e == QKeySequence::MoveToStartOfDocument)
  189. {
  190. moveOperation = QTextCursor::StartOfLine;
  191. moveMode = QTextCursor::MoveAnchor;
  192. return true;
  193. }
  194. else if (e == QKeySequence::SelectStartOfDocument)
  195. {
  196. moveOperation = QTextCursor::StartOfLine;
  197. moveMode = QTextCursor::KeepAnchor;
  198. return true;
  199. }
  200. else
  201. {
  202. return false;
  203. }
  204. }
  205. //-----------------------------------------------------------------------------
  206. bool ctkConsolePrivate::isMoveRighttWithinLine(QKeyEvent* e, QTextCursor::MoveOperation &moveOperation, QTextCursor::MoveMode &moveMode)
  207. {
  208. if (e == QKeySequence::MoveToNextChar)
  209. {
  210. moveOperation = QTextCursor::Right;
  211. moveMode = QTextCursor::MoveAnchor;
  212. return true;
  213. }
  214. else if (e == QKeySequence::SelectNextChar)
  215. {
  216. moveOperation = QTextCursor::Right;
  217. moveMode = QTextCursor::KeepAnchor;
  218. return true;
  219. }
  220. else if (e == QKeySequence::MoveToNextWord)
  221. {
  222. moveOperation = QTextCursor::WordRight;
  223. moveMode = QTextCursor::MoveAnchor;
  224. return true;
  225. }
  226. else if (e == QKeySequence::SelectNextWord)
  227. {
  228. moveOperation = QTextCursor::WordRight;
  229. moveMode = QTextCursor::KeepAnchor;
  230. return true;
  231. }
  232. else if (e == QKeySequence::MoveToEndOfLine)
  233. {
  234. moveOperation = QTextCursor::EndOfLine;
  235. moveMode = QTextCursor::MoveAnchor;
  236. return true;
  237. }
  238. else if (e == QKeySequence::SelectEndOfLine)
  239. {
  240. moveOperation = QTextCursor::EndOfLine;
  241. moveMode = QTextCursor::KeepAnchor;
  242. return true;
  243. }
  244. else if (e == QKeySequence::MoveToEndOfBlock)
  245. {
  246. moveOperation = QTextCursor::EndOfLine;
  247. moveMode = QTextCursor::MoveAnchor;
  248. return true;
  249. }
  250. else if (e == QKeySequence::SelectEndOfBlock)
  251. {
  252. moveOperation = QTextCursor::EndOfLine;
  253. moveMode = QTextCursor::KeepAnchor;
  254. return true;
  255. }
  256. else if (e == QKeySequence::MoveToEndOfDocument)
  257. {
  258. moveOperation = QTextCursor::EndOfLine;
  259. moveMode = QTextCursor::MoveAnchor;
  260. return true;
  261. }
  262. else if (e == QKeySequence::SelectEndOfDocument)
  263. {
  264. moveOperation = QTextCursor::EndOfLine;
  265. moveMode = QTextCursor::KeepAnchor;
  266. return true;
  267. }
  268. else
  269. {
  270. return false;
  271. }
  272. }
  273. //-----------------------------------------------------------------------------
  274. void ctkConsolePrivate::keyPressEvent(QKeyEvent* e)
  275. {
  276. if (this->Completer && this->Completer->popup()->isVisible())
  277. {
  278. // The following keys are forwarded by the completer to the widget
  279. switch (e->key())
  280. {
  281. case Qt::Key_Enter:
  282. case Qt::Key_Return:
  283. case Qt::Key_Escape:
  284. case Qt::Key_Tab:
  285. case Qt::Key_Backtab:
  286. e->ignore();
  287. return; // let the completer do default behavior
  288. default:
  289. break;
  290. }
  291. }
  292. QTextCursor text_cursor = this->textCursor();
  293. // Set to true if there's a current selection
  294. const bool selection = text_cursor.anchor() != text_cursor.position();
  295. // Set to true if the cursor overlaps the history area
  296. const bool history_area = this->isCursorInHistoryArea();
  297. // The message output area is defined just under the command line
  298. // and it can display all messages catch during we autocomplete, etc.
  299. // Set to true if the cursor overlaps the message output area
  300. const bool message_output_area = this->isCursorInMessageOutputArea();
  301. // Allow copying anywhere in the console ...
  302. if(e == QKeySequence::Copy)
  303. {
  304. if(selection)
  305. {
  306. this->copy();
  307. }
  308. e->accept();
  309. return;
  310. }
  311. // Allow cut only if the selection is limited to the interactive area ...
  312. if(e == QKeySequence::Cut)
  313. {
  314. if(selection && !history_area && !message_output_area)
  315. {
  316. this->cut();
  317. }
  318. e->accept();
  319. return;
  320. }
  321. // Paste to the end of commandLine if in the history area or in message output area
  322. if(e == QKeySequence::Paste)
  323. {
  324. if(history_area || message_output_area)
  325. {
  326. text_cursor.setPosition(this->commandEnd());
  327. this->setTextCursor(text_cursor);
  328. }
  329. this->paste();
  330. e->accept();
  331. return;
  332. }
  333. // Allow vertical scrolling using page up/down
  334. if (e == QKeySequence::MoveToPreviousPage || e == QKeySequence::SelectPreviousPage
  335. || e == QKeySequence::MoveToNextPage || e == QKeySequence::SelectNextPage)
  336. {
  337. if (e == QKeySequence::SelectPreviousPage || e == QKeySequence::SelectNextPage)
  338. {
  339. // ignore
  340. e->accept();
  341. }
  342. else
  343. {
  344. this->Superclass::keyPressEvent(e);
  345. this->updateCommandBuffer();
  346. }
  347. return;
  348. }
  349. // Force the cursor back to the interactive area if anything else than copy/paste or page up/down is done
  350. // but only when a "real" key is pressed, not just a modifier (otherwise we could not press Control-c in the
  351. // history area because the cursor would jump to the interactive area immediately when Control is pressed)
  352. // Update: message_output_area is like the history_area: we can't modify it
  353. if( (history_area
  354. || message_output_area)
  355. && e->key() != Qt::Key_Control
  356. && e->key() != Qt::Key_Meta
  357. && e->key() != Qt::Key_Alt
  358. && e->key() != Qt::Key_Shift)
  359. {
  360. text_cursor.setPosition(this->commandEnd());
  361. this->setTextCursor(text_cursor);
  362. }
  363. // Start of line should be the start of interactive area
  364. QTextCursor::MoveOperation moveOperation = QTextCursor::NoMove;
  365. QTextCursor::MoveMode moveMode = QTextCursor::MoveAnchor;
  366. if(isMoveLeftWithinLine(e, moveOperation, moveMode))
  367. {
  368. text_cursor.movePosition(moveOperation, moveMode);
  369. if (text_cursor.position() >= this->InteractivePosition)
  370. {
  371. this->Superclass::keyPressEvent(e);
  372. }
  373. else
  374. {
  375. text_cursor.setPosition(this->InteractivePosition, moveMode);
  376. this->setTextCursor(text_cursor);
  377. e->accept();
  378. }
  379. this->updateCompleterIfVisible();
  380. return;
  381. }
  382. // End of line should be the end of interactive area
  383. moveOperation = QTextCursor::NoMove;
  384. moveMode = QTextCursor::MoveAnchor;
  385. if(isMoveRighttWithinLine(e, moveOperation, moveMode))
  386. {
  387. text_cursor.movePosition(moveOperation, moveMode);
  388. if (text_cursor.position() <= this->commandEnd())
  389. {
  390. this->Superclass::keyPressEvent(e);
  391. }
  392. else
  393. {
  394. text_cursor.setPosition(this->commandEnd(), moveMode);
  395. this->setTextCursor(text_cursor);
  396. e->accept();
  397. }
  398. this->updateCompleterIfVisible();
  399. return;
  400. }
  401. if (e == QKeySequence::Delete)
  402. {
  403. e->accept();
  404. // Can delete only if we are not at the end of the command line.
  405. // There is an exception if something is selected because it will erase the text selected instead.
  406. if (text_cursor.position() < this->commandEnd()
  407. || (text_cursor.position() <= this->commandEnd()
  408. && selection))
  409. {
  410. this->Superclass::keyPressEvent(e);
  411. this->updateCommandBuffer();
  412. }
  413. return;
  414. }
  415. // There seems to be no QKeySequence for backspace, therefore the key
  416. // has to be recognized directly (the same way as it is done in the parent class)
  417. if (e->key() == Qt::Key_Backspace && !(e->modifiers() & ~Qt::ShiftModifier))
  418. {
  419. e->accept();
  420. if(text_cursor.position() > this->InteractivePosition)
  421. {
  422. this->Superclass::keyPressEvent(e);
  423. this->updateCommandBuffer();
  424. this->updateCompleterIfVisible();
  425. }
  426. return;
  427. }
  428. if (e == QKeySequence::DeleteStartOfWord)
  429. {
  430. e->accept();
  431. if(text_cursor.position() > this->InteractivePosition)
  432. {
  433. this->Superclass::keyPressEvent(e);
  434. this->updateCommandBuffer();
  435. this->updateCompleterIfVisible();
  436. }
  437. return;
  438. }
  439. if (e == QKeySequence::MoveToPreviousLine || e == QKeySequence::SelectPreviousLine)
  440. {
  441. e->accept();
  442. if (this->CommandPosition > 0)
  443. {
  444. this->replaceCommandBuffer(this->CommandHistory[--this->CommandPosition]);
  445. }
  446. return;
  447. }
  448. if (e == QKeySequence::MoveToNextLine || e == QKeySequence::SelectNextLine)
  449. {
  450. e->accept();
  451. if (this->CommandPosition < this->CommandHistory.size() - 2)
  452. {
  453. this->replaceCommandBuffer(this->CommandHistory[++this->CommandPosition]);
  454. }
  455. else
  456. {
  457. this->CommandPosition = this->CommandHistory.size()-1;
  458. this->replaceCommandBuffer("");
  459. }
  460. return;
  461. }
  462. if (e == QKeySequence::InsertParagraphSeparator)
  463. {
  464. e->accept();
  465. text_cursor.setPosition(this->documentEnd());
  466. this->setTextCursor(text_cursor);
  467. if (this->InputEventLoop.isNull())
  468. {
  469. this->internalExecuteCommand();
  470. }
  471. else
  472. {
  473. this->processInput();
  474. }
  475. return;
  476. }
  477. if (this->CompleterShortcuts.contains(e->key() + e->modifiers()))
  478. {
  479. e->accept();
  480. this->updateCompleter();
  481. this->selectCompletion();
  482. return;
  483. }
  484. e->accept();
  485. this->switchToUserInputTextColor();
  486. this->Superclass::keyPressEvent(e);
  487. this->updateCommandBuffer();
  488. this->updateCompleterIfVisible();
  489. }
  490. //-----------------------------------------------------------------------------
  491. void ctkConsolePrivate::switchToUserInputTextColor(QTextCursor* textCursorToUpdate)
  492. {
  493. QColor color = this->CommandTextColor;
  494. if (!this->InputEventLoop.isNull())
  495. {
  496. color = this->StdinTextColor;
  497. }
  498. QTextCharFormat currentFormat = this->currentCharFormat();
  499. // Do not trigger a finishEdit for no reason. onTextChanged() would be called.
  500. if (currentFormat.foreground() != color)
  501. {
  502. currentFormat.setForeground(color);
  503. this->setCurrentCharFormat(currentFormat);
  504. }
  505. if (textCursorToUpdate)
  506. {
  507. QTextCharFormat textCursorFormat = textCursorToUpdate->charFormat();
  508. if (textCursorFormat.foreground() != color)
  509. {
  510. textCursorFormat.setForeground(color);
  511. textCursorToUpdate->setCharFormat(textCursorFormat);
  512. }
  513. }
  514. }
  515. //-----------------------------------------------------------------------------
  516. int ctkConsolePrivate::documentEnd() const
  517. {
  518. QTextCursor c(this->document());
  519. c.movePosition(QTextCursor::End);
  520. return c.position();
  521. }
  522. //-----------------------------------------------------------------------------
  523. int ctkConsolePrivate::commandEnd() const
  524. {
  525. QTextCursor c(this->document());
  526. c.setPosition(this->documentEnd()-this->MessageOutputSize);
  527. return c.position();
  528. }
  529. //-----------------------------------------------------------------------------
  530. void ctkConsolePrivate::focusOutEvent(QFocusEvent *e)
  531. {
  532. this->Superclass::focusOutEvent(e);
  533. // For some reason the QCompleter tries to set the focus policy to
  534. // NoFocus, set let's make sure we set it back to the default WheelFocus.
  535. this->setFocusPolicy(Qt::WheelFocus);
  536. }
  537. //-----------------------------------------------------------------------------
  538. void ctkConsolePrivate::resizeEvent(QResizeEvent * e)
  539. {
  540. this->Superclass::resizeEvent(e);
  541. if (this->ScrollbarAtBottom)
  542. {
  543. this->moveCursor(QTextCursor::End);
  544. this->scrollToBottom();
  545. }
  546. }
  547. //-----------------------------------------------------------------------------
  548. void ctkConsolePrivate::scrollToBottom()
  549. {
  550. this->verticalScrollBar()->setValue(this->verticalScrollBar()->maximum());
  551. }
  552. //-----------------------------------------------------------------------------
  553. void ctkConsolePrivate::updateCompleterIfVisible()
  554. {
  555. if (this->Completer && this->Completer->popup()->isVisible())
  556. {
  557. this->updateCompleter();
  558. }
  559. }
  560. //-----------------------------------------------------------------------------
  561. void ctkConsolePrivate::selectCompletion()
  562. {
  563. if (this->Completer && this->Completer->completionCount() == 1)
  564. {
  565. this->insertCompletion(this->Completer->currentCompletion());
  566. this->Completer->popup()->hide();
  567. }
  568. }
  569. //-----------------------------------------------------------------------------
  570. void ctkConsolePrivate::setCompleter(ctkConsoleCompleter* completer)
  571. {
  572. if (this->Completer)
  573. {
  574. this->Completer->setWidget(0);
  575. disconnect(this->Completer, SIGNAL(activated(QString)),
  576. this, SLOT(insertCompletion(QString)));
  577. }
  578. this->Completer = completer;
  579. if (this->Completer)
  580. {
  581. this->Completer->setWidget(this);
  582. connect(this->Completer, SIGNAL(activated(QString)),
  583. this, SLOT(insertCompletion(QString)));
  584. }
  585. }
  586. //-----------------------------------------------------------------------------
  587. void ctkConsolePrivate::updateCompleter()
  588. {
  589. if (this->Completer)
  590. {
  591. // Get the text between the current cursor position
  592. // and the start of the line
  593. QTextCursor text_cursor = this->textCursor();
  594. text_cursor.setPosition(this->InteractivePosition, QTextCursor::KeepAnchor);
  595. QString commandText = text_cursor.selectedText();
  596. // Save current positions: Since some implementation of
  597. // updateCompletionModel (e.g python) can display messages
  598. // while building the completion model, it is important to save
  599. // and restore the positions.
  600. int savedInteractivePosition = this->InteractivePosition;
  601. int savedCursorPosition = this->textCursor().position();
  602. //move the cursor at the end in case of a message displayed
  603. QTextCursor tc = this->textCursor();
  604. tc.setPosition(this->documentEnd());
  605. this->setTextCursor(tc);
  606. // Save color of displayed message
  607. QColor savedOutputTextColor = this->OutputTextColor;
  608. QColor savedErrorTextColor = this->ErrorTextColor;
  609. // Change color of displayed message in message_output_area
  610. this->OutputTextColor = this->MessageOutputColor;
  611. this->ErrorTextColor = this->MessageOutputColor;
  612. // Call the completer to update the completion model
  613. this->Completer->updateCompletionModel(commandText);
  614. // Restore Color
  615. this->OutputTextColor = savedOutputTextColor;
  616. this->ErrorTextColor = savedErrorTextColor;
  617. // Restore positions
  618. this->InteractivePosition = savedInteractivePosition;
  619. QTextCursor textCursor = this->textCursor();
  620. textCursor.setPosition(savedCursorPosition);
  621. this->setTextCursor(textCursor);
  622. // Place and show the completer if there are available completions
  623. if (this->Completer->completionCount())
  624. {
  625. // Get a QRect for the cursor at the start of the
  626. // current word and then translate it down 8 pixels.
  627. text_cursor = this->textCursor();
  628. text_cursor.movePosition(QTextCursor::StartOfWord);
  629. QRect cr = this->cursorRect(text_cursor);
  630. cr.translate(0,8);
  631. cr.setWidth(this->Completer->popup()->sizeHintForColumn(0)
  632. + this->Completer->popup()->verticalScrollBar()->sizeHint().width());
  633. this->Completer->complete(cr);
  634. }
  635. else
  636. {
  637. this->Completer->popup()->hide();
  638. }
  639. }
  640. }
  641. //-----------------------------------------------------------------------------
  642. void ctkConsolePrivate::updateCommandBuffer(int commandLength)
  643. {
  644. if (commandLength == -1)
  645. {
  646. commandLength =
  647. this->commandEnd() - this->InteractivePosition;
  648. }
  649. this->commandBuffer() =
  650. this->toPlainText().mid(this->InteractivePosition, commandLength);
  651. }
  652. //-----------------------------------------------------------------------------
  653. void ctkConsolePrivate::replaceCommandBuffer(const QString& text)
  654. {
  655. this->commandBuffer() = text;
  656. QTextCursor c(this->document());
  657. c.setPosition(this->InteractivePosition);
  658. c.movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
  659. c.removeSelectedText();
  660. // all text removed, we need then to re-init our tracker on the message output area
  661. this->MessageOutputSize = 0;
  662. this->switchToUserInputTextColor(&c);
  663. c.insertText(text);
  664. }
  665. //-----------------------------------------------------------------------------
  666. QString& ctkConsolePrivate::commandBuffer()
  667. {
  668. return this->CommandHistory.back();
  669. }
  670. //-----------------------------------------------------------------------------
  671. void ctkConsolePrivate::internalExecuteCommand()
  672. {
  673. Q_Q(ctkConsole);
  674. QString command = this->commandBuffer();
  675. if (this->EditorHints & ctkConsole::RemoveTrailingSpaces)
  676. {
  677. command.replace(QRegExp("\\s*$"), ""); // Remove trailing spaces
  678. this->commandBuffer() = command; // Update buffer
  679. }
  680. // First update the history cache. It's essential to update the
  681. // this->CommandPosition before calling internalExecuteCommand() since that
  682. // can result in a clearing of the current command (BUG #8765).
  683. if (!command.isEmpty()) // Don't store empty commands in the history
  684. {
  685. this->CommandHistory.push_back("");
  686. this->CommandPosition = this->CommandHistory.size() - 1;
  687. }
  688. QTextCursor c(this->document());
  689. c.movePosition(QTextCursor::End);
  690. c.insertText("\n");
  691. this->InteractivePosition = this->documentEnd();
  692. emit q->aboutToExecute(command);
  693. q->executeCommand(command);
  694. emit q->executed(command);
  695. // Find the indent for the command.
  696. QString indent;
  697. if (this->EditorHints & ctkConsole::AutomaticIndentation)
  698. {
  699. QRegExp regExp("^(\\s+)");
  700. if (regExp.indexIn(command) != -1)
  701. {
  702. indent = regExp.cap(1);
  703. }
  704. }
  705. this->promptForInput(indent);
  706. this->MessageOutputSize = 0;
  707. }
  708. //-----------------------------------------------------------------------------
  709. void ctkConsolePrivate::processInput()
  710. {
  711. QString command = this->commandBuffer();
  712. if (this->EditorHints & ctkConsole::RemoveTrailingSpaces)
  713. {
  714. command.replace(QRegExp("\\s*$"), ""); // Remove trailing spaces
  715. this->commandBuffer() = command; // Update buffer
  716. }
  717. QTextCursor c(this->document());
  718. c.movePosition(QTextCursor::End);
  719. c.insertText("\n");
  720. this->InteractivePosition = this->documentEnd();
  721. this->InputEventLoop->exit();
  722. }
  723. //-----------------------------------------------------------------------------
  724. void ctkConsolePrivate::printString(const QString& text)
  725. {
  726. this->textCursor().movePosition(QTextCursor::End);
  727. this->textCursor().insertText(text);
  728. this->InteractivePosition = this->documentEnd();
  729. }
  730. //----------------------------------------------------------------------------
  731. void ctkConsolePrivate::printOutputMessage(const QString& text)
  732. {
  733. Q_Q(ctkConsole);
  734. QString textToPrint = text;
  735. if (this->MessageOutputSize == 0)
  736. {
  737. textToPrint.prepend("\n");
  738. }
  739. this->MessageOutputSize += textToPrint.size();
  740. q->printMessage(textToPrint, q->outputTextColor());
  741. }
  742. //----------------------------------------------------------------------------
  743. void ctkConsolePrivate::printErrorMessage(const QString& text)
  744. {
  745. Q_Q(ctkConsole);
  746. QString textToPrint = text;
  747. if (this->MessageOutputSize == 0)
  748. {
  749. textToPrint.prepend("\n");
  750. }
  751. this->MessageOutputSize += textToPrint.size();
  752. q->printMessage(textToPrint, q->errorTextColor());
  753. }
  754. //-----------------------------------------------------------------------------
  755. void ctkConsolePrivate::printCommand(const QString& cmd)
  756. {
  757. this->textCursor().insertText(cmd);
  758. this->updateCommandBuffer();
  759. }
  760. //----------------------------------------------------------------------------
  761. void ctkConsolePrivate::promptForInput(const QString& indent)
  762. {
  763. Q_Q(ctkConsole);
  764. QTextCharFormat format = q->getFormat();
  765. format.setForeground(q->promptColor());
  766. q->setFormat(format);
  767. if(!this->MultilineStatement)
  768. {
  769. this->prompt(q->ps1());
  770. this->MessageOutputSize=0;
  771. }
  772. else
  773. {
  774. this->prompt(q->ps2());
  775. }
  776. this->printCommand(indent);
  777. }
  778. //-----------------------------------------------------------------------------
  779. void ctkConsolePrivate::prompt(const QString& text)
  780. {
  781. QTextCursor text_cursor = this->textCursor();
  782. // If the cursor is currently on a clean line, do nothing, otherwise we move
  783. // the cursor to a new line before showing the prompt.
  784. text_cursor.movePosition(QTextCursor::StartOfLine);
  785. int startpos = text_cursor.position();
  786. text_cursor.movePosition(QTextCursor::EndOfLine);
  787. int endpos = text_cursor.position();
  788. if (endpos != startpos)
  789. {
  790. this->textCursor().insertText("\n");
  791. }
  792. this->textCursor().insertText(text);
  793. this->InteractivePosition = this->documentEnd();
  794. }
  795. //----------------------------------------------------------------------------
  796. void ctkConsolePrivate::printWelcomeMessage()
  797. {
  798. Q_Q(ctkConsole);
  799. q->printMessage(
  800. QLatin1String("CTK Console"),
  801. q->welcomeTextColor());
  802. }
  803. //-----------------------------------------------------------------------------
  804. void ctkConsolePrivate::insertCompletion(const QString& completion)
  805. {
  806. Q_Q(ctkConsole);
  807. QTextCursor tc = this->textCursor();
  808. tc.movePosition(QTextCursor::Left, QTextCursor::KeepAnchor);
  809. if (tc.selectedText()==".")
  810. {
  811. tc.insertText(QString(".") + completion);
  812. }
  813. else
  814. {
  815. tc = this->textCursor();
  816. tc.movePosition(QTextCursor::StartOfWord, QTextCursor::MoveAnchor);
  817. tc.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
  818. tc.insertText(completion);
  819. this->setTextCursor(tc);
  820. }
  821. tc.movePosition(QTextCursor::StartOfBlock);
  822. tc.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);
  823. QString shellLine = tc.selectedText();
  824. shellLine.replace(q->ps1(), "");
  825. shellLine.replace(q->ps2(), "");
  826. tc.movePosition(QTextCursor::EndOfLine, QTextCursor::MoveAnchor);
  827. this->setTextCursor(tc);
  828. int cursorOffset = this->Completer->cursorOffset(shellLine);
  829. tc.movePosition(QTextCursor::Left, QTextCursor::MoveAnchor, cursorOffset);
  830. this->setTextCursor(tc);
  831. this->updateCommandBuffer();
  832. }
  833. //-----------------------------------------------------------------------------
  834. void ctkConsolePrivate::onScrollBarValueChanged(int value)
  835. {
  836. this->ScrollbarAtBottom = (this->verticalScrollBar()->maximum() == value);
  837. }
  838. //-----------------------------------------------------------------------------
  839. void ctkConsolePrivate::onTextChanged()
  840. {
  841. this->scrollToBottom();
  842. this->ensureCursorVisible();
  843. }
  844. //-----------------------------------------------------------------------------
  845. bool ctkConsolePrivate::isCursorInHistoryArea()const
  846. {
  847. return this->textCursor().anchor() < this->InteractivePosition
  848. || this->textCursor().position() < this->InteractivePosition;
  849. }
  850. //-----------------------------------------------------------------------------
  851. bool ctkConsolePrivate::isCursorInMessageOutputArea()const
  852. {
  853. return this->textCursor().anchor() > this->commandEnd()
  854. || this->textCursor().position() > this->commandEnd();
  855. }
  856. //-----------------------------------------------------------------------------
  857. void ctkConsolePrivate::insertFromMimeData(const QMimeData* source)
  858. {
  859. if (this->isCursorInHistoryArea())
  860. {
  861. QTextCursor textCursor = this->textCursor();
  862. textCursor.movePosition(QTextCursor::End, QTextCursor::MoveAnchor);
  863. this->setTextCursor(textCursor);
  864. }
  865. const QString text = source->text();
  866. if (!text.isEmpty())
  867. {
  868. this->pasteText(text);
  869. }
  870. else
  871. {
  872. this->Superclass::insertFromMimeData(source);
  873. }
  874. }
  875. //-----------------------------------------------------------------------------
  876. void ctkConsolePrivate::pasteText(const QString& text)
  877. {
  878. if(text.isNull())
  879. {
  880. return;
  881. }
  882. QTextCursor textCursor = this->textCursor();
  883. if (this->EditorHints & ctkConsole::SplitCopiedTextByLine)
  884. {
  885. QStringList lines = text.split(QRegExp("(?:\r\n|\r|\n)"));
  886. for(int i=0; i < lines.count(); ++i)
  887. {
  888. this->switchToUserInputTextColor(&textCursor);
  889. textCursor.insertText(lines.at(i));
  890. this->updateCommandBuffer();
  891. if (i < lines.count() - 1)
  892. {
  893. this->internalExecuteCommand();
  894. }
  895. }
  896. }
  897. else
  898. {
  899. this->switchToUserInputTextColor(&textCursor);
  900. textCursor.insertText(text);
  901. this->updateCommandBuffer();
  902. }
  903. }
  904. //-----------------------------------------------------------------------------
  905. // ctkConsole methods
  906. //-----------------------------------------------------------------------------
  907. ctkConsole::ctkConsole(QWidget* parentObject) :
  908. Superclass(parentObject),
  909. d_ptr(new ctkConsolePrivate(*this))
  910. {
  911. Q_D(ctkConsole);
  912. d->init();
  913. }
  914. //-----------------------------------------------------------------------------
  915. ctkConsole::ctkConsole(ctkConsolePrivate * pimpl, QWidget* parentObject) :
  916. QWidget(parentObject), d_ptr(pimpl)
  917. {
  918. Q_D(ctkConsole);
  919. d->init();
  920. }
  921. //-----------------------------------------------------------------------------
  922. ctkConsole::~ctkConsole()
  923. {
  924. }
  925. //-----------------------------------------------------------------------------
  926. QTextCharFormat ctkConsole::getFormat() const
  927. {
  928. Q_D(const ctkConsole);
  929. return d->currentCharFormat();
  930. }
  931. //-----------------------------------------------------------------------------
  932. void ctkConsole::setFormat(const QTextCharFormat& Format)
  933. {
  934. Q_D(ctkConsole);
  935. d->setCurrentCharFormat(Format);
  936. }
  937. //-----------------------------------------------------------------------------
  938. QFont ctkConsole::shellFont() const
  939. {
  940. Q_D(const ctkConsole);
  941. return d->currentFont();
  942. }
  943. //-----------------------------------------------------------------------------
  944. void ctkConsole::setShellFont(const QFont& font)
  945. {
  946. Q_D(ctkConsole);
  947. int savedPosition = d->textCursor().position();
  948. d->selectAll();
  949. d->setCurrentFont(font);
  950. QTextCursor tc = d->textCursor();
  951. tc.clearSelection();
  952. tc.setPosition(savedPosition);
  953. d->setTextCursor(tc);
  954. }
  955. //-----------------------------------------------------------------------------
  956. CTK_GET_CPP(ctkConsole, ctkConsoleCompleter*, completer, Completer);
  957. //-----------------------------------------------------------------------------
  958. void ctkConsole::setCompleter(ctkConsoleCompleter* completer)
  959. {
  960. Q_D(ctkConsole);
  961. d->setCompleter(completer);
  962. }
  963. //-----------------------------------------------------------------------------
  964. CTK_GET_CPP(ctkConsole, QColor, promptColor, PromptColor);
  965. CTK_SET_CPP(ctkConsole, const QColor&, setPromptColor, PromptColor);
  966. //-----------------------------------------------------------------------------
  967. CTK_GET_CPP(ctkConsole, QColor, outputTextColor, OutputTextColor);
  968. CTK_SET_CPP(ctkConsole, const QColor&, setOutputTextColor, OutputTextColor);
  969. //-----------------------------------------------------------------------------
  970. CTK_GET_CPP(ctkConsole, QColor, errorTextColor, ErrorTextColor);
  971. CTK_SET_CPP(ctkConsole, const QColor&, setErrorTextColor, ErrorTextColor);
  972. //-----------------------------------------------------------------------------
  973. CTK_GET_CPP(ctkConsole, QColor, stdinTextColor, StdinTextColor);
  974. CTK_SET_CPP(ctkConsole, const QColor&, setStdinTextColor, StdinTextColor);
  975. //-----------------------------------------------------------------------------
  976. CTK_GET_CPP(ctkConsole, QColor, commandTextColor, CommandTextColor);
  977. CTK_SET_CPP(ctkConsole, const QColor&, setCommandTextColor, CommandTextColor);
  978. //-----------------------------------------------------------------------------
  979. CTK_GET_CPP(ctkConsole, QColor, welcomeTextColor, WelcomeTextColor);
  980. CTK_SET_CPP(ctkConsole, const QColor&, setWelcomeTextColor, WelcomeTextColor);
  981. //-----------------------------------------------------------------------------
  982. QColor ctkConsole::backgroundColor()const
  983. {
  984. Q_D(const ctkConsole);
  985. QPalette pal = d->palette();
  986. return pal.color(QPalette::Base);
  987. }
  988. //-----------------------------------------------------------------------------
  989. void ctkConsole::setBackgroundColor(const QColor& newColor)
  990. {
  991. Q_D(ctkConsole);
  992. if (this->backgroundColor() == newColor)
  993. {
  994. return;
  995. }
  996. QPalette pal = this->palette();
  997. pal.setColor(QPalette::Base, newColor);
  998. d->setPalette(pal);
  999. }
  1000. //-----------------------------------------------------------------------------
  1001. CTK_GET_CPP(ctkConsole, QString, ps1, Ps1);
  1002. CTK_SET_CPP(ctkConsole, const QString&, setPs1, Ps1);
  1003. //-----------------------------------------------------------------------------
  1004. CTK_GET_CPP(ctkConsole, QString, ps2, Ps2);
  1005. CTK_SET_CPP(ctkConsole, const QString&, setPs2, Ps2);
  1006. //-----------------------------------------------------------------------------
  1007. CTK_GET_CPP(ctkConsole, ctkConsole::EditorHints, editorHints, EditorHints);
  1008. CTK_SET_CPP(ctkConsole, const ctkConsole::EditorHints&, setEditorHints, EditorHints);
  1009. //-----------------------------------------------------------------------------
  1010. int ctkConsole::cursorPosition() const
  1011. {
  1012. Q_D(const ctkConsole);
  1013. return d->textCursor().position();
  1014. }
  1015. //-----------------------------------------------------------------------------
  1016. int ctkConsole::cursorColumn() const
  1017. {
  1018. Q_D(const ctkConsole);
  1019. QTextCursor cursor = d->textCursor();
  1020. cursor.movePosition(QTextCursor::StartOfLine);
  1021. return d->textCursor().position() - cursor.position();
  1022. }
  1023. //-----------------------------------------------------------------------------
  1024. int ctkConsole::cursorLine() const
  1025. {
  1026. Q_D(const ctkConsole);
  1027. QTextCursor cursor = d->textCursor();
  1028. cursor.movePosition(QTextCursor::StartOfLine);
  1029. int lines = 1;
  1030. QTextBlock block = cursor.block().previous();
  1031. while(block.isValid())
  1032. {
  1033. lines += block.lineCount();
  1034. block = block.previous();
  1035. }
  1036. return lines;
  1037. }
  1038. //-----------------------------------------------------------------------------
  1039. Qt::ScrollBarPolicy ctkConsole::scrollBarPolicy()const
  1040. {
  1041. Q_D(const ctkConsole);
  1042. return d->verticalScrollBarPolicy();
  1043. }
  1044. //-----------------------------------------------------------------------------
  1045. void ctkConsole::setScrollBarPolicy(const Qt::ScrollBarPolicy& newScrollBarPolicy)
  1046. {
  1047. Q_D(ctkConsole);
  1048. d->setVerticalScrollBarPolicy(newScrollBarPolicy);
  1049. }
  1050. //-----------------------------------------------------------------------------
  1051. CTK_GET_CPP(ctkConsole, QList<QKeySequence>, completerShortcuts, CompleterShortcuts);
  1052. //-----------------------------------------------------------------------------
  1053. void ctkConsole::setCompleterShortcuts(const QList<QKeySequence>& keys)
  1054. {
  1055. Q_D(ctkConsole);
  1056. d->CompleterShortcuts = keys;
  1057. }
  1058. //-----------------------------------------------------------------------------
  1059. void ctkConsole::addCompleterShortcut(const QKeySequence& key)
  1060. {
  1061. Q_D(ctkConsole);
  1062. if (!d->CompleterShortcuts.contains(key))
  1063. {
  1064. d->CompleterShortcuts.append(key);
  1065. }
  1066. }
  1067. //-----------------------------------------------------------------------------
  1068. CTK_GET_CPP(ctkConsole, ctkConsole::RunFileOptions, runFileOptions, RunFileOptions);
  1069. //-----------------------------------------------------------------------------
  1070. void ctkConsole::setRunFileOptions(const RunFileOptions& newOptions)
  1071. {
  1072. Q_D(ctkConsole);
  1073. d->RunFileButton->setVisible(newOptions.testFlag(ctkConsole::RunFileButton));
  1074. d->RunFileAction->setEnabled(newOptions.testFlag(ctkConsole::RunFileShortcut));
  1075. }
  1076. //-----------------------------------------------------------------------------
  1077. void ctkConsole::exec(const QString& command)
  1078. {
  1079. Q_D(ctkConsole);
  1080. d->replaceCommandBuffer(command);
  1081. d->internalExecuteCommand();
  1082. }
  1083. //-----------------------------------------------------------------------------
  1084. void ctkConsole::runFile(const QString& filePath)
  1085. {
  1086. QFile file(filePath);
  1087. if (!file.open(QIODevice::ReadOnly))
  1088. {
  1089. qWarning() << tr( "File '%1' can't be read.").arg(filePath);
  1090. return;
  1091. }
  1092. for (QTextStream fileStream(&file); !fileStream.atEnd();)
  1093. {
  1094. QString line = fileStream.readLine();
  1095. this->exec(line);
  1096. }
  1097. }
  1098. //-----------------------------------------------------------------------------
  1099. void ctkConsole::runFile()
  1100. {
  1101. QString filePath =
  1102. QFileDialog::getOpenFileName(this, tr("Select a script file to run"));
  1103. if (!filePath.isEmpty())
  1104. {
  1105. this->runFile(filePath);
  1106. }
  1107. }
  1108. //-----------------------------------------------------------------------------
  1109. void ctkConsole::printHelp()
  1110. {
  1111. this->printMessage("\n", Qt::gray);
  1112. this->printMessage(tr("CTRL+h: Print this help message\n"), Qt::gray);
  1113. this->printMessage(tr("CTRL+r: Open a file dialog to select a file to run\n"), Qt::gray);
  1114. }
  1115. //-----------------------------------------------------------------------------
  1116. void ctkConsole::executeCommand(const QString& command)
  1117. {
  1118. qWarning() << "ctkConsole::executeCommand not implemented !";
  1119. qWarning() << "command:" << command;
  1120. }
  1121. //----------------------------------------------------------------------------
  1122. void ctkConsole::printMessage(const QString& message, const QColor& color)
  1123. {
  1124. Q_D(ctkConsole);
  1125. QTextCharFormat format = this->getFormat();
  1126. format.setForeground(color);
  1127. this->setFormat(format);
  1128. d->printString(message);
  1129. }
  1130. //-----------------------------------------------------------------------------
  1131. void ctkConsole::clear()
  1132. {
  1133. Q_D(ctkConsole);
  1134. d->clear();
  1135. // For some reason the QCompleter tries to set the focus policy to
  1136. // NoFocus, set let's make sure we set it back to the default WheelFocus.
  1137. d->setFocusPolicy(Qt::WheelFocus);
  1138. d->promptForInput();
  1139. }
  1140. //-----------------------------------------------------------------------------
  1141. void ctkConsole::reset()
  1142. {
  1143. Q_D(ctkConsole);
  1144. d->clear();
  1145. // For some reason the QCompleter tries to set the focus policy to
  1146. // NoFocus, set let's make sure we set it back to the default WheelFocus.
  1147. d->setFocusPolicy(Qt::WheelFocus);
  1148. d->printWelcomeMessage();
  1149. d->promptForInput();
  1150. }
  1151. //-----------------------------------------------------------------------------
  1152. QString ctkConsole::stdInRedirectCallBack(void * callData)
  1153. {
  1154. ctkConsole * self = reinterpret_cast<ctkConsole*>(callData);
  1155. Q_ASSERT(self);
  1156. if (!self)
  1157. {
  1158. return QLatin1String("");
  1159. }
  1160. return self->readInputLine();
  1161. }
  1162. namespace
  1163. {
  1164. class InputEventLoop : public QEventLoop
  1165. {
  1166. public:
  1167. InputEventLoop(QApplication * app, QObject * parentObject = 0) :
  1168. QEventLoop(parentObject), App(app){}
  1169. virtual bool processEvents(ProcessEventsFlags flags = AllEvents)
  1170. {
  1171. this->App->processEvents(flags);
  1172. return true;
  1173. }
  1174. QApplication * App;
  1175. };
  1176. }
  1177. //-----------------------------------------------------------------------------
  1178. QString ctkConsole::readInputLine()
  1179. {
  1180. Q_D(ctkConsole);
  1181. d->moveCursor(QTextCursor::End);
  1182. QScopedPointer<InputEventLoop> eventLoop(new InputEventLoop(qApp));
  1183. d->InputEventLoop = QPointer<QEventLoop>(eventLoop.data());
  1184. eventLoop->exec();
  1185. return d->commandBuffer();
  1186. }