ctkConsole.cpp 28 KB

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