- fix #277 : History not correctly loaded with up/down arrow key in the debug console.

This commit is contained in:
Roy Qu 2024-03-22 15:43:03 +08:00
parent 72cd79eef5
commit cef356a184
4 changed files with 33 additions and 31 deletions

View File

@ -76,6 +76,7 @@ Red Panda C++ Version 2.27
- enhancement: Syntax highlighting for c++ attributes. - enhancement: Syntax highlighting for c++ attributes.
- enhancement: Show "std::function" in the completion list. - enhancement: Show "std::function" in the completion list.
- enhancement: Improvement in italic font support. - enhancement: Improvement in italic font support.
- fix: History not correctly loaded with up/down arrow key in the debug console.
Red Panda C++ Version 2.26 Red Panda C++ Version 2.26

View File

@ -22,8 +22,8 @@
#include <QDebug> #include <QDebug>
AboutDialog::AboutDialog(QWidget *parent) : AboutDialog::AboutDialog(QWidget *parent) :
QDialog(parent), QDialog{parent},
ui(new Ui::AboutDialog) ui{new Ui::AboutDialog}
{ {
setWindowFlag(Qt::WindowContextHelpButtonHint,false); setWindowFlag(Qt::WindowContextHelpButtonHint,false);
ui->setupUi(this); ui->setupUi(this);

View File

@ -31,11 +31,11 @@
#include "../utils.h" #include "../utils.h"
QConsole::QConsole(QWidget *parent): QConsole::QConsole(QWidget *parent):
QAbstractScrollArea(parent), QAbstractScrollArea{parent},
mContents(this), mContents{this},
mContentImage() mContentImage{}
{ {
mHistorySize = 500; mMaxHistory = 500;
mHistoryIndex = -1; mHistoryIndex = -1;
mCommand = ""; mCommand = "";
mCurrentEditableLine = ""; mCurrentEditableLine = "";
@ -73,14 +73,14 @@ QConsole::QConsole(QWidget *parent):
} }
int QConsole::historySize() const int QConsole::maxHistory() const
{ {
return mHistorySize; return mMaxHistory;
} }
void QConsole::setHistorySize(int historySize) void QConsole::setMaxHistory(int historySize)
{ {
mHistorySize = historySize; mMaxHistory = historySize;
} }
int QConsole::tabSize() const int QConsole::tabSize() const
@ -570,32 +570,28 @@ void QConsole::keyPressEvent(QKeyEvent *event)
if (mReadonly) if (mReadonly)
return; return;
emit commandInput(mCommand); emit commandInput(mCommand);
if (mHistorySize>0 && !mCommand.trimmed().isEmpty()) { if (mMaxHistory>0 && !mCommand.trimmed().isEmpty()) {
if (mCommandHistory.size()==mHistorySize) { if (mCommandHistory.isEmpty()
mCommandHistory.pop_front(); || mCommandHistory.last()!=mCommand) {
mHistoryIndex--; if (mCommandHistory.length()==mMaxHistory) {
mCommandHistory.pop_front();
}
mCommandHistory.append(mCommand);
} }
if (mCommandHistory.size()==0 || mHistoryIndex<0 || mHistoryIndex == mCommandHistory.size()-1) { mHistoryIndex = mCommandHistory.length();
mHistoryIndex=mCommandHistory.size();
}
mCommandHistory.append(mCommand);
} }
mCommand=""; mCommand="";
addLine(""); addLine("");
return; return;
case Qt::Key_Up: case Qt::Key_Up:
event->accept(); event->accept();
if (mHistorySize>0 && mHistoryIndex>0) { mHistoryIndex--;
mHistoryIndex--; loadCommandFromHistory();
loadCommandFromHistory();
}
return; return;
case Qt::Key_Down: case Qt::Key_Down:
event->accept(); event->accept();
if (mHistorySize>0 && mHistoryIndex<mCommandHistory.size()-1) { mHistoryIndex++;
mHistoryIndex++; loadCommandFromHistory();
loadCommandFromHistory();
}
return; return;
case Qt::Key_Left: case Qt::Key_Left:
event->accept(); event->accept();
@ -791,9 +787,14 @@ void QConsole::textInputed(const QString &text)
void QConsole::loadCommandFromHistory() void QConsole::loadCommandFromHistory()
{ {
if (mHistorySize<=0) if (mMaxHistory<=0)
return; return;
mCommand = mCommandHistory[mHistoryIndex]; if (mHistoryIndex<0)
mHistoryIndex=0;
if (mHistoryIndex<mCommandHistory.length())
mCommand = mCommandHistory[mHistoryIndex];
else
mCommand = "";
QString lastLine = mContents.getLastLine(); QString lastLine = mContents.getLastLine();
int len=mCurrentEditableLine.length(); int len=mCurrentEditableLine.length();
lastLine.remove(lastLine.length()-len,INT_MAX); lastLine.remove(lastLine.length()-len,INT_MAX);

View File

@ -112,8 +112,8 @@ class QConsole : public QAbstractScrollArea
Q_OBJECT Q_OBJECT
public: public:
explicit QConsole(QWidget* parent = nullptr); explicit QConsole(QWidget* parent = nullptr);
int historySize() const; int maxHistory() const;
void setHistorySize(int historySize); void setMaxHistory(int historySize);
int tabSize() const; int tabSize() const;
@ -142,7 +142,7 @@ signals:
private: private:
ConsoleLines mContents; ConsoleLines mContents;
QStringList mCommandHistory; QStringList mCommandHistory;
int mHistorySize; int mMaxHistory;
int mHistoryIndex; int mHistoryIndex;
QString mCommand; QString mCommand;
QString mCurrentEditableLine; QString mCurrentEditableLine;