Skip to content

Commit 3e93437

Browse files
committed
Add Qt Creator's ConsoleLineWidget to get history for debugger commands
1 parent 43cb522 commit 3e93437

File tree

3 files changed

+137
-1
lines changed

3 files changed

+137
-1
lines changed

consolelineedit.cpp

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
}

consolelineedit.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
#pragma once
27+
28+
#include <QLineEdit>
29+
30+
class ConsoleLineEdit : public QLineEdit
31+
{
32+
public:
33+
explicit ConsoleLineEdit(QWidget *parent = nullptr);
34+
35+
void addHistoryEntry();
36+
void loadHistoryEntry(int entryIndex);
37+
38+
protected:
39+
void keyPressEvent(QKeyEvent *event) override final;
40+
41+
private:
42+
QStringList m_history;
43+
int m_maxEntries;
44+
int m_currentEntryIndex = 0;
45+
QString m_editingEntry;
46+
};

mainwindow.ui

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@
239239
</widget>
240240
</item>
241241
<item>
242-
<widget class="QLineEdit" name="lineEdit">
242+
<widget class="ConsoleLineEdit" name="lineEdit">
243243
<property name="enabled">
244244
<bool>false</bool>
245245
</property>
@@ -680,6 +680,11 @@
680680
<extends>QTreeWidget</extends>
681681
<header>usblinktreewidget.h</header>
682682
</customwidget>
683+
<customwidget>
684+
<class>ConsoleLineEdit</class>
685+
<extends>QLineEdit</extends>
686+
<header>consolelineedit.h</header>
687+
</customwidget>
683688
</customwidgets>
684689
<resources>
685690
<include location="resources.qrc"/>

0 commit comments

Comments
 (0)