-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmdwidget.cpp
123 lines (100 loc) · 2.91 KB
/
cmdwidget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include "cmdwidget.hpp"
#include "debuggermanager.h"
#include "LtDbg/LtDbg/Exceptions.hpp"
#include <QKeyEvent>
CmdWidget::CmdWidget(QWidget * parent) : QWidget(parent)
{
_mainLayout = new QVBoxLayout;
_cmdTextEdit = new QTextEdit;
_cmdTextEdit->setReadOnly(true);
_cmdLineEdit = new QLineEdit(this);
_cmdLineEdit->setFocus();
_mainLayout->addWidget(_cmdTextEdit);
_mainLayout->addWidget(_cmdLineEdit);
SetEnabled(false);
setLayout(_mainLayout);
}
void CmdWidget::AddInfo(const QString message)
{
_cmdTextEdit->append("[Info] " + message);
}
void CmdWidget::AddError(const QString message)
{
_cmdTextEdit->append("[Error] " + message);
}
void CmdWidget::AddText(const QString message)
{
_cmdTextEdit->append(message);
}
void CmdWidget::SetEnabled(const bool enabled)
{
_cmdLineEdit->setEnabled(enabled);
}
void CmdWidget::ExecuteCommand()
{
QString cmdStr = _cmdLineEdit->text();
SetEnabled(false);
try {
if (cmdStr.size() == 0 && _commandsHistory.size() == 0)
return;
if (cmdStr.size() == 0)
{
DebuggerManager::Instance()->ExecuteCommand(_commandsHistory[_commandIndex - 1].toStdString(), DebuggerManager::Instance()->lastResponse->context);
}
else
{
DebuggerManager::Instance()->ExecuteCommand(cmdStr.toStdString());
}
} catch (const DbgException & exc) {
AddError("An error occured");
AddError(QString(exc.ToString().c_str()));
}
if (_commandsHistory.size() == 0 && cmdStr.size() > 0)
{
_commandsHistory.push_back(cmdStr);
_commandIndex = _commandsHistory.size();
}
else if (_commandsHistory.back() != cmdStr && cmdStr.size() > 0)
{
_commandsHistory.push_back(cmdStr);
_commandIndex = _commandsHistory.size();
}
_cmdLineEdit->setText("");
SetEnabled(true);
_cmdLineEdit->setFocus();
}
void CmdWidget::keyPressEvent(QKeyEvent * event)
{
switch(event->key())
{
case Qt::Key_Return:
ExecuteCommand();
break;
case Qt::Key_Up:
_cmdLineEdit->setText(GetPreviousCommand());
break;
case Qt::Key_Down:
_cmdLineEdit->setText(GetNextCommand());
break;
}
}
QString CmdWidget::GetPreviousCommand()
{
if (_commandsHistory.size() == 0)
return _cmdLineEdit->text();
if ((_commandIndex - 1) < 0)
return _commandsHistory[_commandIndex];
return _commandsHistory[--_commandIndex];
}
QString CmdWidget::GetNextCommand()
{
if (_commandsHistory.size() == 0)
return "";
if ((_commandIndex + 1) >= _commandsHistory.size())
{
if ((_commandIndex + 1) == _commandsHistory.size())
_commandIndex++;
return "";
}
return _commandsHistory[++_commandIndex];
}