-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstartanalysis.cpp
167 lines (149 loc) · 6.73 KB
/
startanalysis.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
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
#include <QFileDialog>
#include "startanalysis.hpp"
#include "globals.hpp"
#include "game.hpp"
#include "io.hpp"
#include "analysis.hpp"
StartAnalysis::StartAnalysis(Globals& globals_,const NodePtr& node_,const std::pair<Placements,ExtendedSteps>& partialMove_,Game* const game) :
QDialog(game),
globals(globals_),
node(Node::reroot(node_)),
partialMove(partialMove_),
vBoxLayout(this),
executableLabel(tr("Executable:")),
executablePushButton(tr("&Locate")),
argumentGroupBox(tr("Arguments (line-separated)")),
partialMoveGroupBox(tr("Partial &move")),
dialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel)
{
executable.addWidget(&executableLabel);
executable.addWidget(&executableLineEdit);
connect(&executablePushButton,&QPushButton::clicked,this,[this] {
const auto fileName=QFileDialog::getOpenFileName(this,tr("Set analysis executable"));
if (fileName!=nullptr)
executableLineEdit.setText(fileName);
});
executable.addWidget(&executablePushButton);
vBoxLayout.addLayout(&executable);
for (auto& textEdit:arguments) {
textEdit.setAcceptRichText(false);
textEdit.setLineWrapMode(QTextEdit::NoWrap);
}
argumentLayout.addWidget(&arguments[0]);
moveFileContent.setReadOnly(true);
moveFileContent.setText(QString::fromStdString(toMoveList(node," ",false)));
moveFileLayout.addRow(tr("Move file content:"),&moveFileContent);
argumentLayout.addLayout(&moveFileLayout);
argumentLayout.addWidget(&arguments[1]);
const auto& setup=partialMove.first;
const auto& move=partialMove.second;
const bool partialMovePossible=(move.empty() ? !setup.empty() : node->legalPartialMove(move));
partialMoveGroupBox.setCheckable(true);
partialMoveGroupBox.setChecked(partialMovePossible);
partialMoveGroupBox.setEnabled(partialMovePossible);
partialMoveLine.setReadOnly(true);
if (partialMovePossible)
partialMoveLine.setText(QString::fromStdString(node->inSetup() ? toString(setup) : toString(move)));
for (auto& partialMoveArgument:partialMoveArguments) {
partialMoveArgument.setAcceptRichText(false);
partialMoveArgument.setLineWrapMode(QTextEdit::NoWrap);
partialMoveLayout.addWidget(&partialMoveArgument);
partialMoveArgument.setEnabled(partialMoveGroupBox.isChecked());
}
partialMoveLayout.insertWidget(1,&partialMoveLine);
partialMoveLine.setEnabled(partialMoveGroupBox.isChecked());
connect(&partialMoveGroupBox,&QGroupBox::toggled,this,[this](int state) {
for (auto& partialMoveArgument:partialMoveArguments)
partialMoveArgument.setEnabled(state);
partialMoveLine.setEnabled(state);
setWindowTitle();
});
partialMoveGroupBox.setLayout(&partialMoveLayout);
argumentLayout.addWidget(&partialMoveGroupBox);
argumentLayout.addWidget(&arguments[2]);
argumentGroupBox.setLayout(&argumentLayout);
vBoxLayout.addWidget(&argumentGroupBox);
passSynonymLayout.addRow(tr("&Pass synonyms (space-separated):"),&passSynonyms);
vBoxLayout.addLayout(&passSynonymLayout);
connect(&dialogButtonBox,&QDialogButtonBox::accepted,this,[=] {
setEnabled(false);
Analysis::CommandLine commandLine;
commandLine.executable=executableLineEdit.text();
commandLine.beforeMoves=split(arguments[0]);
commandLine.moves=moveFileContent.text();
commandLine.afterMoves=split(arguments[1]);
if (partialMoveGroupBox.isChecked()) {
commandLine.afterMoves.append(split(partialMoveArguments[0]));
commandLine.afterMoves.append(partialMoveLine.text());
commandLine.afterMoves.append(split(partialMoveArguments[1]));
}
commandLine.afterMoves.append(split(arguments[2]));
std::set<std::string> words;
for (const auto& word:passSynonyms.text().split(" ",Qt::SkipEmptyParts))
words.emplace(word.toStdString());
const auto analysis=new Analysis(globals,node,partialMoveGroupBox.isChecked() ? partialMove : std::pair<Placements,ExtendedSteps>(),commandLine,words,this);
if (analysis->isHidden() && analysis->process.state()==QProcess::NotRunning)
setEnabled(true);
connect(&analysis->process,&QProcess::errorOccurred,this,[this]{setEnabled(true);});
connect(&analysis->process,&QProcess::readyReadStandardOutput,this,[=] {
setEnabled(true);
writeSettings(globals.settings);
analysis->setParent(game,Qt::Window);
analysis->show();
close();
});
connect(analysis,&Analysis::sendPosition,game,&Game::setPosition);
});
connect(&dialogButtonBox,&QDialogButtonBox::rejected,this,&StartAnalysis::close);
vBoxLayout.addWidget(&dialogButtonBox);
readSettings(globals.settings);
setWindowTitle();
const auto okButton=dialogButtonBox.button(QDialogButtonBox::Ok);
okButton->setEnabled(!executableLineEdit.text().isEmpty());
connect(&executableLineEdit,&QLineEdit::textChanged,this,[okButton](const QString& text) {
okButton->setEnabled(!text.isEmpty());
});
okButton->setFocus();
}
QStringList StartAnalysis::split(const QTextEdit& textEdit)
{
return textEdit.toPlainText().split('\n',Qt::SkipEmptyParts);
}
void StartAnalysis::setWindowTitle()
{
QString position=QString::fromStdString(node->nextPlyString());
if (partialMoveGroupBox.isChecked())
position+=' '+partialMoveLine.text();
QDialog::setWindowTitle(tr("Analyze %1").arg(position));
}
void StartAnalysis::readSettings(QSettings& settings)
{
settings.beginGroup("Analysis");
executableLineEdit.setText(settings.value("executable").toString());
arguments[0].setText(settings.value("first_arguments","analyze").toString());
arguments[1].setText(settings.value("middle_arguments","-threads\n1").toString());
if (partialMoveGroupBox.isEnabled())
partialMoveGroupBox.setChecked(settings.value("partial_move",true).toBool());
partialMoveArguments[0].setText(settings.value("before_partial_move","-m").toString());
partialMoveArguments[1].setText(settings.value("after_partial_move").toString());
arguments[2].setText(settings.value("last_arguments").toString());
passSynonyms.setText(settings.value("pass_synonyms","qpss").toString());
settings.endGroup();
}
void StartAnalysis::writeSettings(QSettings& settings) const
{
settings.beginGroup("Analysis");
settings.setValue("executable",executableLineEdit.text());
settings.setValue("first_arguments",arguments[0].toPlainText());
settings.setValue("middle_arguments",arguments[1].toPlainText());
if (partialMoveGroupBox.isEnabled()) {
settings.setValue("partial_move",partialMoveGroupBox.isChecked());
if (partialMoveGroupBox.isChecked()) {
settings.setValue("before_partial_move",partialMoveArguments[0].toPlainText());
settings.setValue("after_partial_move",partialMoveArguments[1].toPlainText());
}
}
settings.setValue("last_arguments",arguments[2].toPlainText());
settings.setValue("pass_synonyms",passSynonyms.text());
settings.endGroup();
}