Repository navigation
Expand file tree
/
Copy pathanalysis.cpp
More file actions
290 lines (268 loc) · 9.26 KB
/
Copy pathanalysis.cpp
File metadata and controls
290 lines (268 loc) · 9.26 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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#include <QApplication>
#include <QScrollBar>
#include <QLabel>
#include <QPushButton>
#include <QStyleOptionButton>
#include <QContextMenuEvent>
#include <QMenu>
#include <QClipboard>
#include "analysis.hpp"
#include "messagebox.hpp"
#include "io.hpp"
Analysis::Analysis(Globals& globals_,NodePtr node,const std::pair<Placements,ExtendedSteps>& partialMove,const CommandLine& commandLine,std::set<std::string>& passSynonyms_,QWidget* parent) :
QWidget(parent,Qt::Window),
globals(globals_),
passSynonyms(passSynonyms_),
process(this),
scrolledDown(true),
state(ROW_START),
startPosition{node,partialMove.first,partialMove.second},
currentPosition(startPosition),
layout(this),
vBoxLayout(&scrollAreaChild),
lastHBoxLayout(nullptr),
lastLabel(nullptr)
{
layout.addWidget(&scrollArea);
scrollArea.setWidget(&scrollAreaChild);
scrollArea.setWidgetResizable(true);
vBoxLayout.addStretch();
connect(&process,&QProcess::readyReadStandardOutput,this,&Analysis::processStandardOutput);
connect(&process,&QProcess::errorOccurred,this,[this](const QProcess::ProcessError error) {
if (isVisible())
setWindowTitle();
else
close();
QString message;
switch (error) {
case QProcess::FailedToStart: message=tr("Failed to start."); break;
case QProcess::Crashed: message=tr("Crashed."); break;
case QProcess::Timedout: message=tr("Timed out."); break;
case QProcess::ReadError: message=tr("Read error."); break;
case QProcess::WriteError: message=tr("Write error."); break;
case QProcess::UnknownError: message=tr("Unknown error."); break;
};
MessageBox(QMessageBox::Critical,tr("Error analyzing"),message,QMessageBox::NoButton,this).exec();
});
connect(&process,static_cast<void (QProcess::*)(int,QProcess::ExitStatus)>(&QProcess::finished),this,[this] {
if (isVisible()) {
QApplication::alert(this);
setWindowTitle();
}
});
const auto verticalScrollBar=scrollArea.verticalScrollBar();
connect(verticalScrollBar,&QScrollBar::rangeChanged,this,[=] {
if (scrolledDown)
verticalScrollBar->setValue(verticalScrollBar->maximum());
});
moveFile.open();
QTextStream moveFileStream(&moveFile);
moveFileStream<<commandLine.moves;
QStringList processStrings=commandLine.beforeMoves;
processStrings.append(moveFile.fileName());
processStrings.append(commandLine.afterMoves);
process.start(commandLine.executable,processStrings);
globals.settings.beginGroup("Analysis");
const auto size=globals.settings.value("size").toSize();
globals.settings.endGroup();
if (size.isValid())
resize(size);
setWindowTitle();
setAttribute(Qt::WA_DeleteOnClose);
}
Analysis::~Analysis()
{
disconnect(&process,&QProcess::errorOccurred,this,nullptr);
process.close();
}
void Analysis::setWindowTitle()
{
const auto& startingNode=std::get<0>(currentPosition);
auto position=startingNode->nextPlyString();
if (startingNode->inSetup()) {
const auto& startingSetup=std::get<1>(currentPosition);
if (!startingSetup.empty())
position+=' '+toString(startingSetup);
}
else {
const auto& startingMove=std::get<2>(currentPosition);
if (!startingMove.empty())
position+=' '+toString(startingMove);
}
QString windowTitle;
if (process.state()==QProcess::NotRunning) {
if (process.exitStatus()==QProcess::NormalExit)
windowTitle=tr("Finished analyzing %1").arg(QString::fromStdString(position));
else
windowTitle=tr("Stopped analyzing %1").arg(QString::fromStdString(position));
}
else
windowTitle=tr("Analyzing %1").arg(QString::fromStdString(position));
QWidget::setWindowTitle(windowTitle);
}
void Analysis::processStandardOutput()
{
QApplication::alert(this);
const auto verticalScrollBar=scrollArea.verticalScrollBar();
scrolledDown=(verticalScrollBar->value()==verticalScrollBar->maximum());
const auto additionalOutput=process.readAllStandardOutput();
output+=additionalOutput;
ss.clear();
ss<<additionalOutput.toStdString();
std::string unprocessed;
while (true) {
auto c=ss.peek();
if (c==EOF) {
processPlainText(unprocessed);
break;
}
else if (isspace(c)) {
ss.get();
if (c=='\n' && state==PROCESSING_MOVES) {
state=ROW_START;
currentPosition=startPosition;
}
else
unprocessed+=c;
}
else {
auto& currentNode=std::get<0>(currentPosition);
const auto& currentSetup=std::get<1>(currentPosition);
auto& currentMove=std::get<2>(currentPosition);
const auto posBefore=ss.tellg();
const auto result=parseChunk(ss,currentPosition,true);
const auto& newPosition=std::get<0>(result);
auto chunk=std::get<1>(result);
bool action=(std::get<0>(newPosition)!=nullptr);
if (action)
currentPosition=newPosition;
else {
ss.clear();
ss.seekg(posBefore);
ss>>chunk;
if (passSynonyms.find(chunk)!=passSynonyms.end() && !currentNode->inSetup() && currentNode->legalMove(currentMove)==MoveLegality::LEGAL) {
currentNode=Node::makeMove(currentNode,currentMove,true);
assert(currentSetup.empty());
currentMove.clear();
action=true;
}
}
if (action) {
if (state==PROCESSING_MOVES) {
if (unprocessed!=" ")
lastHBoxLayout->insertSpacing(lastHBoxLayout->count()-1,10);
unprocessed.clear();
}
else {
processPlainText(unprocessed);
lastHBoxLayout=new QHBoxLayout();
vBoxLayout.insertLayout(vBoxLayout.count()-1,lastHBoxLayout);
assert(lastHBoxLayout->parent()!=nullptr);
lastHBoxLayout->setSpacing(0);
lastHBoxLayout->addStretch();
}
const auto pushButton=new QPushButton(QString::fromStdString(chunk));
lastHBoxLayout->insertWidget(lastHBoxLayout->count()-1,pushButton);
pushButton->installEventFilter(this);
pushButton->setFocusPolicy(Qt::TabFocus);
connect(pushButton,&QPushButton::pressed,pushButton,[pushButton]{pushButton->setFocus(Qt::MouseFocusReason);});
connect(pushButton,&QPushButton::clicked,this,[=] {
emit sendPosition(currentNode,{currentSetup,currentMove});
});
pushButton->setFont(monospace());
const auto textSize=pushButton->fontMetrics().size(Qt::TextShowMnemonic,pushButton->text());
QStyleOptionButton styleOptionButton;
styleOptionButton.initFrom(pushButton);
styleOptionButton.rect.setSize(textSize);
pushButton->setFixedSize(pushButton->style()->sizeFromContents(QStyle::CT_PushButton,&styleOptionButton,textSize,pushButton));
state=PROCESSING_MOVES;
}
else {
unprocessed+=chunk;
if (state==PROCESSING_MOVES) {
std::string line;
getline(ss,line);
unprocessed+=line;
processPlainText(unprocessed);
}
}
}
}
}
void Analysis::processPlainText(std::string& text)
{
if (!text.empty()) {
if (state==APPENDING_TEXT) {
assert(lastLabel!=nullptr);
lastLabel->setText(lastLabel->text()+QString::fromStdString(text));
}
else {
lastLabel=new QLabel(QString::fromStdString(text));
if (state==ROW_START) {
vBoxLayout.insertWidget(vBoxLayout.count()-1,lastLabel);
state=APPENDING_TEXT;
}
else {
assert(state==PROCESSING_MOVES);
assert(lastHBoxLayout!=nullptr);
lastHBoxLayout->insertWidget(lastHBoxLayout->count()-1,lastLabel);
state=ROW_START;
currentPosition=startPosition;
}
lastLabel->installEventFilter(this);
assert(lastLabel->parent()!=nullptr);
lastLabel->setFont(monospace());
lastLabel->setTextInteractionFlags(Qt::TextSelectableByMouse);
}
text.clear();
}
}
bool Analysis::event(QEvent* event)
{
const auto type=event->type();
if (type==QEvent::Close || type==QEvent::WindowActivate) {
globals.settings.beginGroup("Analysis");
globals.settings.setValue("size",normalGeometry().size());
globals.settings.endGroup();
}
return QWidget::event(event);
}
bool Analysis::eventFilter(QObject* watched,QEvent* event)
{
switch (event->type()) {
case QEvent::FocusIn:
if (const auto& pushButton=dynamic_cast<QPushButton*>(watched))
pushButton->click();
break;
case QEvent::ContextMenu: {
const auto& label=dynamic_cast<QLabel*>(watched);
if (label!=nullptr && !label->hasSelectedText()) {
contextMenuEvent(static_cast<QContextMenuEvent*>(event));
return true;
}
}
break;
default: break;
}
return QWidget::eventFilter(watched,event);
}
void Analysis::contextMenuEvent(QContextMenuEvent*)
{
auto menu=new QMenu(this);
menu->setAttribute(Qt::WA_DeleteOnClose);
const auto copyAll=new QAction(tr("Copy all to clipboard"),menu);
connect(copyAll,&QAction::triggered,this,[this] {
QGuiApplication::clipboard()->setText(output);
});
menu->addAction(copyAll);
const auto stop=new QAction(tr("Stop analysis"),menu);
if (process.state()==QProcess::NotRunning)
stop->setEnabled(false);
else
connect(stop,&QAction::triggered,this,[this] {
disconnect(&process,&QProcess::errorOccurred,this,nullptr);
process.close();
});
menu->addAction(stop);
menu->popup(QCursor::pos());
}