ctkConsole.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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.commontk.org/LICENSE
  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. Module: $RCSfile$
  17. Copyright (c) 2005-2008 Sandia Corporation, Kitware Inc.
  18. All rights reserved.
  19. ParaView is a free software; you can redistribute it and/or modify it
  20. under the terms of the ParaView license version 1.2.
  21. See License_v1.2.txt for the full ParaView license.
  22. A copy of this license can be obtained by contacting
  23. Kitware Inc.
  24. 28 Corporate Drive
  25. Clifton Park, NY 12065
  26. USA
  27. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  28. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  29. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  30. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR
  31. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  32. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  33. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  34. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  35. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  36. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  37. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. =========================================================================*/
  39. // Qt includes
  40. #include <QAbstractItemView>
  41. #include <QApplication>
  42. #include <QClipboard>
  43. #include <QCompleter>
  44. #include <QKeyEvent>
  45. #include <QPointer>
  46. #include <QTextCursor>
  47. #include <QVBoxLayout>
  48. #include <QScrollBar>
  49. #include <QDebug>
  50. // CTK includes
  51. #include "ctkConsole.h"
  52. #include "ctkConsole_p.h"
  53. #include "ctkPimpl.h"
  54. //-----------------------------------------------------------------------------
  55. // ctkConsolePrivate methods
  56. //-----------------------------------------------------------------------------
  57. ctkConsolePrivate::ctkConsolePrivate(ctkConsole& object) :
  58. QTextEdit(0),
  59. q_ptr(&object),
  60. InteractivePosition(documentEnd())
  61. {
  62. }
  63. //-----------------------------------------------------------------------------
  64. void ctkConsolePrivate::init()
  65. {
  66. Q_Q(ctkConsole);
  67. this->setParent(q);
  68. this->setTabChangesFocus(false);
  69. this->setAcceptDrops(false);
  70. this->setAcceptRichText(false);
  71. this->setUndoRedoEnabled(false);
  72. this->PromptColor = QColor(0, 0, 0); // Black
  73. this->OutputTextColor = QColor(0, 150, 0); // Green
  74. this->ErrorTextColor = QColor(255, 0, 0); // Red
  75. this->CommandTextColor = QColor(0, 0, 150); // Blue
  76. this->WelcomeTextColor = QColor(0, 0, 255); // Dark Blue
  77. QFont f;
  78. f.setFamily("Courier");
  79. f.setStyleHint(QFont::TypeWriter);
  80. f.setFixedPitch(true);
  81. QTextCharFormat format;
  82. format.setFont(f);
  83. format.setForeground(this->OutputTextColor);
  84. this->setCurrentCharFormat(format);
  85. this->CommandHistory.append("");
  86. this->CommandPosition = 0;
  87. }
  88. //-----------------------------------------------------------------------------
  89. void ctkConsolePrivate::keyPressEvent(QKeyEvent* e)
  90. {
  91. if (this->Completer && this->Completer->popup()->isVisible())
  92. {
  93. // The following keys are forwarded by the completer to the widget
  94. switch (e->key())
  95. {
  96. case Qt::Key_Enter:
  97. case Qt::Key_Return:
  98. case Qt::Key_Escape:
  99. case Qt::Key_Tab:
  100. case Qt::Key_Backtab:
  101. e->ignore();
  102. return; // let the completer do default behavior
  103. default:
  104. break;
  105. }
  106. }
  107. QTextCursor text_cursor = this->textCursor();
  108. // Set to true if there's a current selection
  109. const bool selection = text_cursor.anchor() != text_cursor.position();
  110. // Set to true if the cursor overlaps the history area
  111. const bool history_area =
  112. text_cursor.anchor() < this->InteractivePosition
  113. || text_cursor.position() < this->InteractivePosition;
  114. // Allow copying anywhere in the console ...
  115. if(e->key() == Qt::Key_C && e->modifiers() == Qt::ControlModifier)
  116. {
  117. if(selection)
  118. {
  119. this->copy();
  120. }
  121. e->accept();
  122. return;
  123. }
  124. // Allow cut only if the selection is limited to the interactive area ...
  125. if(e->key() == Qt::Key_X && e->modifiers() == Qt::ControlModifier)
  126. {
  127. if(selection && !history_area)
  128. {
  129. this->cut();
  130. }
  131. e->accept();
  132. return;
  133. }
  134. // Allow paste only if the selection is in the interactive area ...
  135. if(e->key() == Qt::Key_V && e->modifiers() == Qt::ControlModifier)
  136. {
  137. if(!history_area)
  138. {
  139. const QMimeData* const clipboard = QApplication::clipboard()->mimeData();
  140. const QString text = clipboard->text();
  141. if(!text.isNull())
  142. {
  143. text_cursor.insertText(text);
  144. this->updateCommandBuffer();
  145. }
  146. }
  147. e->accept();
  148. return;
  149. }
  150. // Force the cursor back to the interactive area
  151. if(history_area && e->key() != Qt::Key_Control)
  152. {
  153. text_cursor.setPosition(this->documentEnd());
  154. this->setTextCursor(text_cursor);
  155. }
  156. switch(e->key())
  157. {
  158. case Qt::Key_Up:
  159. e->accept();
  160. if (this->CommandPosition > 0)
  161. {
  162. this->replaceCommandBuffer(this->CommandHistory[--this->CommandPosition]);
  163. }
  164. break;
  165. case Qt::Key_Down:
  166. e->accept();
  167. if (this->CommandPosition < this->CommandHistory.size() - 2)
  168. {
  169. this->replaceCommandBuffer(this->CommandHistory[++this->CommandPosition]);
  170. }
  171. else
  172. {
  173. this->CommandPosition = this->CommandHistory.size()-1;
  174. this->replaceCommandBuffer("");
  175. }
  176. break;
  177. case Qt::Key_Left:
  178. if (text_cursor.position() > this->InteractivePosition)
  179. {
  180. QTextEdit::keyPressEvent(e);
  181. }
  182. else
  183. {
  184. e->accept();
  185. }
  186. break;
  187. case Qt::Key_Delete:
  188. e->accept();
  189. QTextEdit::keyPressEvent(e);
  190. this->updateCommandBuffer();
  191. break;
  192. case Qt::Key_Backspace:
  193. e->accept();
  194. if(text_cursor.position() > this->InteractivePosition)
  195. {
  196. QTextEdit::keyPressEvent(e);
  197. this->updateCommandBuffer();
  198. this->updateCompleterIfVisible();
  199. }
  200. break;
  201. case Qt::Key_Tab:
  202. e->accept();
  203. this->updateCompleter();
  204. this->selectCompletion();
  205. break;
  206. case Qt::Key_Home:
  207. e->accept();
  208. text_cursor.setPosition(this->InteractivePosition);
  209. this->setTextCursor(text_cursor);
  210. break;
  211. case Qt::Key_Return:
  212. case Qt::Key_Enter:
  213. e->accept();
  214. text_cursor.setPosition(this->documentEnd());
  215. this->setTextCursor(text_cursor);
  216. this->internalExecuteCommand();
  217. break;
  218. default:
  219. e->accept();
  220. QTextEdit::keyPressEvent(e);
  221. this->updateCommandBuffer();
  222. this->updateCompleterIfVisible();
  223. break;
  224. }
  225. }
  226. //-----------------------------------------------------------------------------
  227. int ctkConsolePrivate::documentEnd() const
  228. {
  229. QTextCursor c(this->document());
  230. c.movePosition(QTextCursor::End);
  231. return c.position();
  232. }
  233. //-----------------------------------------------------------------------------
  234. void ctkConsolePrivate::focusOutEvent(QFocusEvent *e)
  235. {
  236. QTextEdit::focusOutEvent(e);
  237. // For some reason the QCompleter tries to set the focus policy to
  238. // NoFocus, set let's make sure we set it back to the default WheelFocus.
  239. this->setFocusPolicy(Qt::WheelFocus);
  240. }
  241. //-----------------------------------------------------------------------------
  242. void ctkConsolePrivate::updateCompleterIfVisible()
  243. {
  244. if (this->Completer && this->Completer->popup()->isVisible())
  245. {
  246. this->updateCompleter();
  247. }
  248. }
  249. //-----------------------------------------------------------------------------
  250. void ctkConsolePrivate::selectCompletion()
  251. {
  252. if (this->Completer && this->Completer->completionCount() == 1)
  253. {
  254. this->insertCompletion(this->Completer->currentCompletion());
  255. this->Completer->popup()->hide();
  256. }
  257. }
  258. //-----------------------------------------------------------------------------
  259. void ctkConsolePrivate::updateCompleter()
  260. {
  261. if (this->Completer)
  262. {
  263. // Get the text between the current cursor position
  264. // and the start of the line
  265. QTextCursor text_cursor = this->textCursor();
  266. text_cursor.setPosition(this->InteractivePosition, QTextCursor::KeepAnchor);
  267. QString commandText = text_cursor.selectedText();
  268. // Call the completer to update the completion model
  269. this->Completer->updateCompletionModel(commandText);
  270. // Place and show the completer if there are available completions
  271. if (this->Completer->completionCount())
  272. {
  273. // Get a QRect for the cursor at the start of the
  274. // current word and then translate it down 8 pixels.
  275. text_cursor = this->textCursor();
  276. text_cursor.movePosition(QTextCursor::StartOfWord);
  277. QRect cr = this->cursorRect(text_cursor);
  278. cr.translate(0,8);
  279. cr.setWidth(this->Completer->popup()->sizeHintForColumn(0)
  280. + this->Completer->popup()->verticalScrollBar()->sizeHint().width());
  281. this->Completer->complete(cr);
  282. }
  283. else
  284. {
  285. this->Completer->popup()->hide();
  286. }
  287. }
  288. }
  289. //-----------------------------------------------------------------------------c
  290. void ctkConsolePrivate::updateCommandBuffer()
  291. {
  292. this->commandBuffer() = this->toPlainText().mid(this->InteractivePosition);
  293. }
  294. //-----------------------------------------------------------------------------
  295. void ctkConsolePrivate::replaceCommandBuffer(const QString& Text)
  296. {
  297. this->commandBuffer() = Text;
  298. QTextCursor c(this->document());
  299. c.setPosition(this->InteractivePosition);
  300. c.movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
  301. c.removeSelectedText();
  302. c.insertText(Text);
  303. }
  304. //-----------------------------------------------------------------------------
  305. QString& ctkConsolePrivate::commandBuffer()
  306. {
  307. return this->CommandHistory.back();
  308. }
  309. //-----------------------------------------------------------------------------
  310. void ctkConsolePrivate::internalExecuteCommand()
  311. {
  312. Q_Q(ctkConsole);
  313. // First update the history cache. It's essential to update the
  314. // this->CommandPosition before calling internalExecuteCommand() since that
  315. // can result in a clearing of the current command (BUG #8765).
  316. QString command = this->commandBuffer();
  317. if (!command.isEmpty()) // Don't store empty commands in the history
  318. {
  319. this->CommandHistory.push_back("");
  320. this->CommandPosition = this->CommandHistory.size() - 1;
  321. }
  322. QTextCursor c(this->document());
  323. c.movePosition(QTextCursor::End);
  324. c.insertText("\n");
  325. this->InteractivePosition = this->documentEnd();
  326. emit q->executing(true);
  327. q->executeCommand(command);
  328. emit q->executing(false);
  329. }
  330. //-----------------------------------------------------------------------------
  331. void ctkConsolePrivate::setCompleter(ctkConsoleCompleter* completer)
  332. {
  333. if (this->Completer)
  334. {
  335. this->Completer->setWidget(0);
  336. disconnect(this->Completer, SIGNAL(activated(const QString&)),
  337. this, SLOT(insertCompletion(const QString&)));
  338. }
  339. this->Completer = completer;
  340. if (this->Completer)
  341. {
  342. this->Completer->setWidget(this);
  343. connect(this->Completer, SIGNAL(activated(const QString&)),
  344. this, SLOT(insertCompletion(const QString&)));
  345. }
  346. }
  347. //-----------------------------------------------------------------------------
  348. void ctkConsolePrivate::printString(const QString& text)
  349. {
  350. this->textCursor().movePosition(QTextCursor::End);
  351. this->textCursor().insertText(text);
  352. this->InteractivePosition = this->documentEnd();
  353. this->ensureCursorVisible();
  354. }
  355. //----------------------------------------------------------------------------
  356. void ctkConsolePrivate::printOutputMessage(const QString& text)
  357. {
  358. Q_Q(ctkConsole);
  359. q->printMessage(text, q->outputTextColor());
  360. QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
  361. }
  362. //----------------------------------------------------------------------------
  363. void ctkConsolePrivate::printErrorMessage(const QString& text)
  364. {
  365. Q_Q(ctkConsole);
  366. q->printMessage(text, q->errorTextColor());
  367. QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
  368. }
  369. //-----------------------------------------------------------------------------
  370. void ctkConsolePrivate::printCommand(const QString& cmd)
  371. {
  372. this->textCursor().insertText(cmd);
  373. this->updateCommandBuffer();
  374. }
  375. //-----------------------------------------------------------------------------
  376. void ctkConsolePrivate::prompt(const QString& text)
  377. {
  378. QTextCursor text_cursor = this->textCursor();
  379. // If the cursor is currently on a clean line, do nothing, otherwise we move
  380. // the cursor to a new line before showing the prompt.
  381. text_cursor.movePosition(QTextCursor::StartOfLine);
  382. int startpos = text_cursor.position();
  383. text_cursor.movePosition(QTextCursor::EndOfLine);
  384. int endpos = text_cursor.position();
  385. if (endpos != startpos)
  386. {
  387. this->textCursor().insertText("\n");
  388. }
  389. this->textCursor().insertText(text);
  390. this->InteractivePosition = this->documentEnd();
  391. this->ensureCursorVisible();
  392. }
  393. //-----------------------------------------------------------------------------
  394. void ctkConsolePrivate::insertCompletion(const QString& completion)
  395. {
  396. QTextCursor tc = this->textCursor();
  397. tc.movePosition(QTextCursor::Left, QTextCursor::KeepAnchor);
  398. if (tc.selectedText()==".")
  399. {
  400. tc.insertText(QString(".") + completion);
  401. }
  402. else
  403. {
  404. tc = this->textCursor();
  405. tc.movePosition(QTextCursor::StartOfWord, QTextCursor::MoveAnchor);
  406. tc.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
  407. tc.insertText(completion);
  408. this->setTextCursor(tc);
  409. }
  410. this->updateCommandBuffer();
  411. }
  412. //-----------------------------------------------------------------------------
  413. // ctkConsole methods
  414. //-----------------------------------------------------------------------------
  415. ctkConsole::ctkConsole(QWidget* parentObject) :
  416. QWidget(parentObject),
  417. d_ptr(new ctkConsolePrivate(*this))
  418. {
  419. Q_D(ctkConsole);
  420. QVBoxLayout* layout = new QVBoxLayout(this);
  421. layout->setMargin(0);
  422. layout->addWidget(d);
  423. }
  424. //-----------------------------------------------------------------------------
  425. ctkConsole::ctkConsole(ctkConsolePrivate * pimpl, QWidget* parentObject) :
  426. QWidget(parentObject), d_ptr(pimpl)
  427. {
  428. Q_D(ctkConsole);
  429. d->init();
  430. QVBoxLayout* layout = new QVBoxLayout(this);
  431. layout->setMargin(0);
  432. layout->addWidget(d);
  433. }
  434. //-----------------------------------------------------------------------------
  435. ctkConsole::~ctkConsole()
  436. {
  437. }
  438. //-----------------------------------------------------------------------------
  439. QTextCharFormat ctkConsole::getFormat() const
  440. {
  441. Q_D(const ctkConsole);
  442. return d->currentCharFormat();
  443. }
  444. //-----------------------------------------------------------------------------
  445. void ctkConsole::setFormat(const QTextCharFormat& Format)
  446. {
  447. Q_D(ctkConsole);
  448. d->setCurrentCharFormat(Format);
  449. }
  450. //-----------------------------------------------------------------------------
  451. void ctkConsole::setCompleter(ctkConsoleCompleter* completer)
  452. {
  453. Q_D(ctkConsole);
  454. d->setCompleter(completer);
  455. }
  456. //-----------------------------------------------------------------------------
  457. CTK_GET_CPP(ctkConsole, QColor, promptColor, PromptColor);
  458. CTK_SET_CPP(ctkConsole, const QColor&, setPromptColor, PromptColor);
  459. //-----------------------------------------------------------------------------
  460. CTK_GET_CPP(ctkConsole, QColor, outputTextColor, OutputTextColor);
  461. CTK_SET_CPP(ctkConsole, const QColor&, setOutputTextColor, OutputTextColor);
  462. //-----------------------------------------------------------------------------
  463. CTK_GET_CPP(ctkConsole, QColor, errorTextColor, ErrorTextColor);
  464. CTK_SET_CPP(ctkConsole, const QColor&, setErrorTextColor, ErrorTextColor);
  465. //-----------------------------------------------------------------------------
  466. CTK_GET_CPP(ctkConsole, QColor, commandTextColor, CommandTextColor);
  467. CTK_SET_CPP(ctkConsole, const QColor&, setCommandTextColor, CommandTextColor);
  468. //-----------------------------------------------------------------------------
  469. CTK_GET_CPP(ctkConsole, QColor, welcomeTextColor, WelcomeTextColor);
  470. CTK_SET_CPP(ctkConsole, const QColor&, setWelcomeTextColor, WelcomeTextColor);
  471. //-----------------------------------------------------------------------------
  472. void ctkConsole::executeCommand(const QString& command)
  473. {
  474. qWarning() << "ctkConsole::executeCommand not implemented !";
  475. qWarning() << "command:" << command;
  476. }
  477. //----------------------------------------------------------------------------
  478. void ctkConsole::printMessage(const QString& message, const QColor& color)
  479. {
  480. Q_D(ctkConsole);
  481. QTextCharFormat format = this->getFormat();
  482. format.setForeground(color);
  483. this->setFormat(format);
  484. d->printString(message);
  485. }
  486. //-----------------------------------------------------------------------------
  487. void ctkConsole::clear()
  488. {
  489. Q_D(ctkConsole);
  490. d->clear();
  491. // For some reason the QCompleter tries to set the focus policy to
  492. // NoFocus, set let's make sure we set it back to the default WheelFocus.
  493. d->setFocusPolicy(Qt::WheelFocus);
  494. }