|
| 1 | +/**************************************************************************** |
| 2 | +** |
| 3 | +** Copyright (C) 2018 Benjamin Balga |
| 4 | +** Contact: https://www.qt.io/licensing/ |
| 5 | +** |
| 6 | +** This file is part of Qt Creator. |
| 7 | +** |
| 8 | +** Commercial License Usage |
| 9 | +** Licensees holding valid commercial Qt licenses may use this file in |
| 10 | +** accordance with the commercial license agreement provided with the |
| 11 | +** Software or, alternatively, in accordance with the terms contained in |
| 12 | +** a written agreement between you and The Qt Company. For licensing terms |
| 13 | +** and conditions see https://www.qt.io/terms-conditions. For further |
| 14 | +** information use the contact form at https://www.qt.io/contact-us. |
| 15 | +** |
| 16 | +** GNU General Public License Usage |
| 17 | +** Alternatively, this file may be used under the terms of the GNU |
| 18 | +** General Public License version 3 as published by the Free Software |
| 19 | +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
| 20 | +** included in the packaging of this file. Please review the following |
| 21 | +** information to ensure the GNU General Public License requirements will |
| 22 | +** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 23 | +** |
| 24 | +****************************************************************************/ |
| 25 | + |
| 26 | +#include "consolelineedit.h" |
| 27 | + |
| 28 | +#include <QKeyEvent> |
| 29 | + |
| 30 | +ConsoleLineEdit::ConsoleLineEdit(QWidget *parent) : |
| 31 | + QLineEdit(parent), |
| 32 | + m_maxEntries(20) |
| 33 | +{ |
| 34 | + connect(this, &QLineEdit::returnPressed, this, &ConsoleLineEdit::addHistoryEntry); |
| 35 | +} |
| 36 | + |
| 37 | +// Add current text to history entries, if not empty and different from last entry. |
| 38 | +// Called when return key is pressed. |
| 39 | +void ConsoleLineEdit::addHistoryEntry() |
| 40 | +{ |
| 41 | + m_currentEntryIndex = 0; |
| 42 | + const QString currentText = text(); |
| 43 | + |
| 44 | + if (currentText.isEmpty()) |
| 45 | + return; |
| 46 | + |
| 47 | + if (!m_history.isEmpty() && m_history.first() == currentText) |
| 48 | + return; |
| 49 | + |
| 50 | + m_history.prepend(currentText); |
| 51 | + if (m_history.size() > m_maxEntries) |
| 52 | + m_history.removeLast(); |
| 53 | +} |
| 54 | + |
| 55 | +// Load a specific history entry: 0 = current, n = n-most last entry |
| 56 | +void ConsoleLineEdit::loadHistoryEntry(int entryIndex) |
| 57 | +{ |
| 58 | + if (entryIndex < 0 || entryIndex > m_history.size()) |
| 59 | + return; |
| 60 | + |
| 61 | + if (m_currentEntryIndex == 0) |
| 62 | + m_editingEntry = text(); |
| 63 | + |
| 64 | + if (entryIndex <= 0 && m_currentEntryIndex > 0) { |
| 65 | + m_currentEntryIndex = 0; |
| 66 | + setText(m_editingEntry); |
| 67 | + } else if (entryIndex > 0) { |
| 68 | + m_currentEntryIndex = entryIndex; |
| 69 | + setText(m_history.at(entryIndex-1)); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +void ConsoleLineEdit::keyPressEvent(QKeyEvent *event) |
| 74 | +{ |
| 75 | + // Navigate history with up/down keys |
| 76 | + if (event->key() == Qt::Key_Up) { |
| 77 | + loadHistoryEntry(m_currentEntryIndex+1); |
| 78 | + event->accept(); |
| 79 | + } else if (event->key() == Qt::Key_Down) { |
| 80 | + loadHistoryEntry(m_currentEntryIndex-1); |
| 81 | + event->accept(); |
| 82 | + } else { |
| 83 | + QLineEdit::keyPressEvent(event); |
| 84 | + } |
| 85 | +} |
0 commit comments