-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathcontexthelp.cpp
More file actions
78 lines (73 loc) · 1.61 KB
/
contexthelp.cpp
File metadata and controls
78 lines (73 loc) · 1.61 KB
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
#include "contexthelp.h"
ContextHelp::ContextHelp(QStatusBar *statusBar, QObject *parent) :
QObject(parent)
{
this->statusBar = statusBar;
loadCommands();
showPrompt();
}
void ContextHelp::print(QString s)
{
statusBar->showMessage(s);
}
void ContextHelp::onUserInput(QString input)
{
if(input.isEmpty())
{
showPrompt();
return;
}
if(commands.isEmpty())
{
print("Commands are not loaded");
return;
}
int pos = input.indexOf(' ');
QString cmd = input.toUpper().left(pos);
if(pos > 0 && lastCmd == cmd)
{
print(lastPrint);
return;
}
QString help = "Unknown command";
QStringList found;
QHashIterator<QString, QString> i(commands);
while (i.hasNext()) {
i.next();
if((pos == -1 && i.key().startsWith(cmd)) || i.key() == cmd)
found << i.key();
}
found.sort();
if(found.length() > 1)
help = found.join(" ");
if(found.length() == 1)
help = commands[found.at(0)];
if(pos != -1)
lastCmd = cmd;
else
lastCmd = "";
lastPrint = help;
print(help);
}
void ContextHelp::showPrompt()
{
print("Start typing in console to get context help");
}
void ContextHelp::loadCommands()
{
QFile file(":/commands.txt");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
QMessageBox::critical(0, "Context help", "Can\'t open commands resource");
return;
}
QTextStream in(&file);
while (!in.atEnd()) {
QString line = in.readLine().trimmed();
if(!line.isEmpty() && !in.atEnd())
{
QString nextLine = in.readLine().trimmed();
commands[line.left(line.indexOf(' '))] = line + " - " + nextLine;
}
}
}