+bool MultiLineEdit::addToHistory(const QString &text, bool temporary)
+{
+ if (text.isEmpty())
+ return false;
+
+ Q_ASSERT(0 <= _idx && _idx <= _history.count());
+
+ if (temporary) {
+ // if an entry of the history is changed, we remember it and show it again at this
+ // position until a line was actually sent
+ // sent lines get appended to the history
+ if (_history.isEmpty() || text != _history[_idx - (int)(_idx == _history.count())]) {
+ _tempHistory[_idx] = text;
+ return true;
+ }
+ }
+ else {
+ if (_history.isEmpty() || text != _history.last()) {
+ _history << text;
+ _tempHistory.clear();
+ return true;
+ }
+ }
+ return false;
+}
+
+
+bool MultiLineEdit::event(QEvent *e)
+{
+ // We need to make sure that global shortcuts aren't eaten
+ if (e->type() == QEvent::ShortcutOverride) {
+ QKeyEvent *event = static_cast<QKeyEvent *>(e);
+ QKeySequence key = QKeySequence(event->key() | event->modifiers());
+ foreach(QAction *action, GraphicalUi::actionCollection()->actions()) {
+ if (action->shortcuts().contains(key)) {
+ e->ignore();
+ return false;
+ }
+ }
+ }
+
+ return MultiLineEditParent::event(e);
+}
+
+
+void MultiLineEdit::keyPressEvent(QKeyEvent *event)
+{
+ if (event == QKeySequence::InsertLineSeparator) {
+ if (_mode == SingleLine) {
+ event->accept();
+ on_returnPressed();
+ return;
+ }
+ MultiLineEditParent::keyPressEvent(event);
+ return;
+ }
+
+ switch (event->key()) {
+ case Qt::Key_Up:
+ if (event->modifiers() & Qt::ShiftModifier)
+ break;
+ {
+ event->accept();
+ if (!(event->modifiers() & Qt::ControlModifier)) {
+ int pos = textCursor().position();
+ moveCursor(QTextCursor::Up);
+ if (pos == textCursor().position()) // already on top line -> history
+ historyMoveBack();
+ }
+ else
+ historyMoveBack();
+ return;
+ }
+
+ case Qt::Key_Down:
+ if (event->modifiers() & Qt::ShiftModifier)
+ break;
+ {
+ event->accept();
+ if (!(event->modifiers() & Qt::ControlModifier)) {
+ int pos = textCursor().position();
+ moveCursor(QTextCursor::Down);
+ if (pos == textCursor().position()) // already on bottom line -> history
+ historyMoveForward();
+ }
+ else
+ historyMoveForward();
+ return;
+ }
+
+ case Qt::Key_Return:
+ case Qt::Key_Enter:
+ case Qt::Key_Select:
+ event->accept();
+ on_returnPressed();
+ return;
+
+ // We don't want to have the tab key react even if no completer is installed
+ case Qt::Key_Tab:
+ event->accept();
+ return;
+
+ default:
+ ;
+ }
+
+ if (_emacsMode) {
+ if (event->modifiers() & Qt::ControlModifier) {
+ switch (event->key()) {
+ // move
+ case Qt::Key_A:
+ moveCursor(QTextCursor::StartOfLine);
+ return;
+ case Qt::Key_E:
+ moveCursor(QTextCursor::EndOfLine);
+ return;
+ case Qt::Key_F:
+ moveCursor(QTextCursor::Right);
+ return;
+ case Qt::Key_B:
+ moveCursor(QTextCursor::Left);
+ return;
+
+ // modify
+ case Qt::Key_Y:
+ paste();
+ return;
+ case Qt::Key_K:
+ moveCursor(QTextCursor::EndOfLine, QTextCursor::KeepAnchor);
+ cut();
+ return;
+
+ default:
+ break;
+ }
+ }
+ else if (event->modifiers() & Qt::MetaModifier ||
+ event->modifiers() & Qt::AltModifier)
+ {
+ switch (event->key()) {
+ case Qt::Key_Right:
+ moveCursor(QTextCursor::WordRight);
+ return;
+ case Qt::Key_Left:
+ moveCursor(QTextCursor::WordLeft);
+ return;
+ case Qt::Key_F:
+ moveCursor(QTextCursor::WordRight);
+ return;
+ case Qt::Key_B:
+ moveCursor(QTextCursor::WordLeft);
+ return;
+ case Qt::Key_Less:
+ moveCursor(QTextCursor::Start);
+ return;
+ case Qt::Key_Greater:
+ moveCursor(QTextCursor::End);
+ return;
+
+ // modify
+ case Qt::Key_D:
+ moveCursor(QTextCursor::WordRight, QTextCursor::KeepAnchor);
+ cut();
+ return;
+
+ case Qt::Key_U: // uppercase word
+ moveCursor(QTextCursor::WordRight, QTextCursor::KeepAnchor);
+ textCursor().insertText(textCursor().selectedText().toUpper());
+ return;
+
+ case Qt::Key_L: // lowercase word
+ moveCursor(QTextCursor::WordRight, QTextCursor::KeepAnchor);
+ textCursor().insertText(textCursor().selectedText().toLower());
+ return;
+
+ case Qt::Key_C:
+ { // capitalize word
+ moveCursor(QTextCursor::WordRight, QTextCursor::KeepAnchor);
+ QString const text = textCursor().selectedText();
+ textCursor().insertText(text.left(1).toUpper() + text.mid(1).toLower());
+ return;
+ }
+
+ case Qt::Key_T:
+ { // transpose words
+ moveCursor(QTextCursor::StartOfWord);
+ moveCursor(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
+ QString const word1 = textCursor().selectedText();
+ textCursor().clearSelection();
+ moveCursor(QTextCursor::WordRight);
+ moveCursor(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
+ QString const word2 = textCursor().selectedText();
+ if (!word2.isEmpty() && !word1.isEmpty()) {
+ textCursor().insertText(word1);
+ moveCursor(QTextCursor::WordLeft);
+ moveCursor(QTextCursor::WordLeft);
+ moveCursor(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
+ textCursor().insertText(word2);
+ moveCursor(QTextCursor::WordRight);
+ moveCursor(QTextCursor::EndOfWord);
+ }
+ return;
+ }
+
+ default:
+ break;
+ }
+ }
+ }