ctkConsole.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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. // CTK includes
  50. #include "ctkConsole.h"
  51. #include "ctkConsole_p.h"
  52. //-----------------------------------------------------------------------------
  53. // ctkConsolePrivate methods
  54. //-----------------------------------------------------------------------------
  55. ctkConsolePrivate::ctkConsolePrivate(ctkConsole& object) :
  56. QTextEdit(&object),
  57. q_ptr(&object),
  58. InteractivePosition(documentEnd())
  59. {
  60. this->setTabChangesFocus(false);
  61. this->setAcceptDrops(false);
  62. this->setAcceptRichText(false);
  63. this->setUndoRedoEnabled(false);
  64. QFont f;
  65. f.setFamily("Courier");
  66. f.setStyleHint(QFont::TypeWriter);
  67. f.setFixedPitch(true);
  68. QTextCharFormat format;
  69. format.setFont(f);
  70. format.setForeground(QColor(0, 0, 0));
  71. this->setCurrentCharFormat(format);
  72. this->CommandHistory.append("");
  73. this->CommandPosition = 0;
  74. }
  75. //-----------------------------------------------------------------------------
  76. void ctkConsolePrivate::keyPressEvent(QKeyEvent* e)
  77. {
  78. if (this->Completer && this->Completer->popup()->isVisible())
  79. {
  80. // The following keys are forwarded by the completer to the widget
  81. switch (e->key())
  82. {
  83. case Qt::Key_Enter:
  84. case Qt::Key_Return:
  85. case Qt::Key_Escape:
  86. case Qt::Key_Tab:
  87. case Qt::Key_Backtab:
  88. e->ignore();
  89. return; // let the completer do default behavior
  90. default:
  91. break;
  92. }
  93. }
  94. QTextCursor text_cursor = this->textCursor();
  95. // Set to true if there's a current selection
  96. const bool selection = text_cursor.anchor() != text_cursor.position();
  97. // Set to true if the cursor overlaps the history area
  98. const bool history_area =
  99. text_cursor.anchor() < this->InteractivePosition
  100. || text_cursor.position() < this->InteractivePosition;
  101. // Allow copying anywhere in the console ...
  102. if(e->key() == Qt::Key_C && e->modifiers() == Qt::ControlModifier)
  103. {
  104. if(selection)
  105. {
  106. this->copy();
  107. }
  108. e->accept();
  109. return;
  110. }
  111. // Allow cut only if the selection is limited to the interactive area ...
  112. if(e->key() == Qt::Key_X && e->modifiers() == Qt::ControlModifier)
  113. {
  114. if(selection && !history_area)
  115. {
  116. this->cut();
  117. }
  118. e->accept();
  119. return;
  120. }
  121. // Allow paste only if the selection is in the interactive area ...
  122. if(e->key() == Qt::Key_V && e->modifiers() == Qt::ControlModifier)
  123. {
  124. if(!history_area)
  125. {
  126. const QMimeData* const clipboard = QApplication::clipboard()->mimeData();
  127. const QString text = clipboard->text();
  128. if(!text.isNull())
  129. {
  130. text_cursor.insertText(text);
  131. this->updateCommandBuffer();
  132. }
  133. }
  134. e->accept();
  135. return;
  136. }
  137. // Force the cursor back to the interactive area
  138. if(history_area && e->key() != Qt::Key_Control)
  139. {
  140. text_cursor.setPosition(this->documentEnd());
  141. this->setTextCursor(text_cursor);
  142. }
  143. switch(e->key())
  144. {
  145. case Qt::Key_Up:
  146. e->accept();
  147. if (this->CommandPosition > 0)
  148. {
  149. this->replaceCommandBuffer(this->CommandHistory[--this->CommandPosition]);
  150. }
  151. break;
  152. case Qt::Key_Down:
  153. e->accept();
  154. if (this->CommandPosition < this->CommandHistory.size() - 2)
  155. {
  156. this->replaceCommandBuffer(this->CommandHistory[++this->CommandPosition]);
  157. }
  158. else
  159. {
  160. this->CommandPosition = this->CommandHistory.size()-1;
  161. this->replaceCommandBuffer("");
  162. }
  163. break;
  164. case Qt::Key_Left:
  165. if (text_cursor.position() > this->InteractivePosition)
  166. {
  167. QTextEdit::keyPressEvent(e);
  168. }
  169. else
  170. {
  171. e->accept();
  172. }
  173. break;
  174. case Qt::Key_Delete:
  175. e->accept();
  176. QTextEdit::keyPressEvent(e);
  177. this->updateCommandBuffer();
  178. break;
  179. case Qt::Key_Backspace:
  180. e->accept();
  181. if(text_cursor.position() > this->InteractivePosition)
  182. {
  183. QTextEdit::keyPressEvent(e);
  184. this->updateCommandBuffer();
  185. this->updateCompleterIfVisible();
  186. }
  187. break;
  188. case Qt::Key_Tab:
  189. e->accept();
  190. this->updateCompleter();
  191. this->selectCompletion();
  192. break;
  193. case Qt::Key_Home:
  194. e->accept();
  195. text_cursor.setPosition(this->InteractivePosition);
  196. this->setTextCursor(text_cursor);
  197. break;
  198. case Qt::Key_Return:
  199. case Qt::Key_Enter:
  200. e->accept();
  201. text_cursor.setPosition(this->documentEnd());
  202. this->setTextCursor(text_cursor);
  203. this->internalExecuteCommand();
  204. break;
  205. default:
  206. e->accept();
  207. QTextEdit::keyPressEvent(e);
  208. this->updateCommandBuffer();
  209. this->updateCompleterIfVisible();
  210. break;
  211. }
  212. }
  213. //-----------------------------------------------------------------------------
  214. int ctkConsolePrivate::documentEnd() const
  215. {
  216. QTextCursor c(this->document());
  217. c.movePosition(QTextCursor::End);
  218. return c.position();
  219. }
  220. //-----------------------------------------------------------------------------
  221. void ctkConsolePrivate::focusOutEvent(QFocusEvent *e)
  222. {
  223. QTextEdit::focusOutEvent(e);
  224. // For some reason the QCompleter tries to set the focus policy to
  225. // NoFocus, set let's make sure we set it back to the default WheelFocus.
  226. this->setFocusPolicy(Qt::WheelFocus);
  227. }
  228. //-----------------------------------------------------------------------------
  229. void ctkConsolePrivate::updateCompleterIfVisible()
  230. {
  231. if (this->Completer && this->Completer->popup()->isVisible())
  232. {
  233. this->updateCompleter();
  234. }
  235. }
  236. //-----------------------------------------------------------------------------
  237. void ctkConsolePrivate::selectCompletion()
  238. {
  239. Q_Q(ctkConsole);
  240. if (this->Completer && this->Completer->completionCount() == 1)
  241. {
  242. q->insertCompletion(this->Completer->currentCompletion());
  243. this->Completer->popup()->hide();
  244. }
  245. }
  246. //-----------------------------------------------------------------------------
  247. void ctkConsolePrivate::updateCompleter()
  248. {
  249. if (this->Completer)
  250. {
  251. // Get the text between the current cursor position
  252. // and the start of the line
  253. QTextCursor text_cursor = this->textCursor();
  254. text_cursor.setPosition(this->InteractivePosition, QTextCursor::KeepAnchor);
  255. QString commandText = text_cursor.selectedText();
  256. // Call the completer to update the completion model
  257. this->Completer->updateCompletionModel(commandText);
  258. // Place and show the completer if there are available completions
  259. if (this->Completer->completionCount())
  260. {
  261. // Get a QRect for the cursor at the start of the
  262. // current word and then translate it down 8 pixels.
  263. text_cursor = this->textCursor();
  264. text_cursor.movePosition(QTextCursor::StartOfWord);
  265. QRect cr = this->cursorRect(text_cursor);
  266. cr.translate(0,8);
  267. cr.setWidth(this->Completer->popup()->sizeHintForColumn(0)
  268. + this->Completer->popup()->verticalScrollBar()->sizeHint().width());
  269. this->Completer->complete(cr);
  270. }
  271. else
  272. {
  273. this->Completer->popup()->hide();
  274. }
  275. }
  276. }
  277. //-----------------------------------------------------------------------------c
  278. void ctkConsolePrivate::updateCommandBuffer()
  279. {
  280. this->commandBuffer() = this->toPlainText().mid(this->InteractivePosition);
  281. }
  282. //-----------------------------------------------------------------------------
  283. void ctkConsolePrivate::replaceCommandBuffer(const QString& Text)
  284. {
  285. this->commandBuffer() = Text;
  286. QTextCursor c(this->document());
  287. c.setPosition(this->InteractivePosition);
  288. c.movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
  289. c.removeSelectedText();
  290. c.insertText(Text);
  291. }
  292. //-----------------------------------------------------------------------------
  293. QString& ctkConsolePrivate::commandBuffer()
  294. {
  295. return this->CommandHistory.back();
  296. }
  297. //-----------------------------------------------------------------------------
  298. void ctkConsolePrivate::internalExecuteCommand()
  299. {
  300. Q_Q(ctkConsole);
  301. // First update the history cache. It's essential to update the
  302. // this->CommandPosition before calling internalExecuteCommand() since that
  303. // can result in a clearing of the current command (BUG #8765).
  304. QString command = this->commandBuffer();
  305. if (!command.isEmpty()) // Don't store empty commands in the history
  306. {
  307. this->CommandHistory.push_back("");
  308. this->CommandPosition = this->CommandHistory.size() - 1;
  309. }
  310. QTextCursor c(this->document());
  311. c.movePosition(QTextCursor::End);
  312. c.insertText("\n");
  313. this->InteractivePosition = this->documentEnd();
  314. q->internalExecuteCommand(command);
  315. }
  316. //-----------------------------------------------------------------------------
  317. void ctkConsolePrivate::setCompleter(ctkConsoleCompleter* completer)
  318. {
  319. Q_Q(ctkConsole);
  320. if (this->Completer)
  321. {
  322. this->Completer->setWidget(0);
  323. QObject::disconnect(this->Completer, SIGNAL(activated(const QString&)),
  324. q, SLOT(insertCompletion(const QString&)));
  325. }
  326. this->Completer = completer;
  327. if (this->Completer)
  328. {
  329. this->Completer->setWidget(this);
  330. QObject::connect(this->Completer, SIGNAL(activated(const QString&)),
  331. q, SLOT(insertCompletion(const QString&)));
  332. }
  333. }
  334. //-----------------------------------------------------------------------------
  335. // ctkConsole methods
  336. //-----------------------------------------------------------------------------
  337. ctkConsole::ctkConsole(QWidget* parentObject) :
  338. QWidget(parentObject),
  339. d_ptr(new ctkConsolePrivate(*this))
  340. {
  341. Q_D(ctkConsole);
  342. QVBoxLayout* layout = new QVBoxLayout(this);
  343. layout->setMargin(0);
  344. layout->addWidget(d);
  345. }
  346. //-----------------------------------------------------------------------------
  347. ctkConsole::~ctkConsole()
  348. {
  349. }
  350. //-----------------------------------------------------------------------------
  351. QTextCharFormat ctkConsole::getFormat() const
  352. {
  353. Q_D(const ctkConsole);
  354. return d->currentCharFormat();
  355. }
  356. //-----------------------------------------------------------------------------
  357. void ctkConsole::setFormat(const QTextCharFormat& Format)
  358. {
  359. Q_D(ctkConsole);
  360. d->setCurrentCharFormat(Format);
  361. }
  362. //-----------------------------------------------------------------------------
  363. void ctkConsole::setCompleter(ctkConsoleCompleter* completer)
  364. {
  365. Q_D(ctkConsole);
  366. d->setCompleter(completer);
  367. }
  368. //-----------------------------------------------------------------------------
  369. void ctkConsole::insertCompletion(const QString& completion)
  370. {
  371. Q_D(ctkConsole);
  372. QTextCursor tc = d->textCursor();
  373. tc.movePosition(QTextCursor::Left, QTextCursor::KeepAnchor);
  374. if (tc.selectedText()==".")
  375. {
  376. tc.insertText(QString(".") + completion);
  377. }
  378. else
  379. {
  380. tc = d->textCursor();
  381. tc.movePosition(QTextCursor::StartOfWord, QTextCursor::MoveAnchor);
  382. tc.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
  383. tc.insertText(completion);
  384. d->setTextCursor(tc);
  385. }
  386. d->updateCommandBuffer();
  387. }
  388. //-----------------------------------------------------------------------------
  389. void ctkConsole::printString(const QString& Text)
  390. {
  391. Q_D(ctkConsole);
  392. d->textCursor().movePosition(QTextCursor::End);
  393. d->textCursor().insertText(Text);
  394. d->InteractivePosition = d->documentEnd();
  395. d->ensureCursorVisible();
  396. }
  397. //-----------------------------------------------------------------------------
  398. void ctkConsole::printCommand(const QString& cmd)
  399. {
  400. Q_D(ctkConsole);
  401. d->textCursor().insertText(cmd);
  402. d->updateCommandBuffer();
  403. }
  404. //-----------------------------------------------------------------------------
  405. void ctkConsole::prompt(const QString& text)
  406. {
  407. Q_D(ctkConsole);
  408. QTextCursor text_cursor = d->textCursor();
  409. // If the cursor is currently on a clean line, do nothing, otherwise we move
  410. // the cursor to a new line before showing the prompt.
  411. text_cursor.movePosition(QTextCursor::StartOfLine);
  412. int startpos = text_cursor.position();
  413. text_cursor.movePosition(QTextCursor::EndOfLine);
  414. int endpos = text_cursor.position();
  415. if (endpos != startpos)
  416. {
  417. d->textCursor().insertText("\n");
  418. }
  419. d->textCursor().insertText(text);
  420. d->InteractivePosition = d->documentEnd();
  421. d->ensureCursorVisible();
  422. }
  423. //-----------------------------------------------------------------------------
  424. void ctkConsole::clear()
  425. {
  426. Q_D(ctkConsole);
  427. d->clear();
  428. // For some reason the QCompleter tries to set the focus policy to
  429. // NoFocus, set let's make sure we set it back to the default WheelFocus.
  430. d->setFocusPolicy(Qt::WheelFocus);
  431. }
  432. //-----------------------------------------------------------------------------
  433. void ctkConsole::internalExecuteCommand(const QString& Command)
  434. {
  435. emit this->executeCommand(Command);
  436. }