This repository has been archived by the owner on Nov 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Runner.cpp
220 lines (138 loc) · 4.8 KB
/
Runner.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
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
// Copyright (C) 2012-2015, Ali Baharev
// All rights reserved.
// This code is published under the GNU Lesser General Public License.
#include <QDebug>
#include <QDir>
#include <QFileInfo>
#include <QHBoxLayout>
#include <QPushButton>
#include <QStatusBar>
#include <QVector>
#include "Runner.hpp"
#include "ConcatPdfs.hpp"
#include "ConvertAllEps.hpp"
#include "ErrorMsg.hpp"
#include "Launcher.hpp"
#include "GlobalSettings.hpp"
#include "ProcessManager.hpp"
namespace {
const char RUN[] = "&Run";
const char RUNNING[] = "Please wait...";
}
Runner::Runner(QWidget *parent, QStatusBar* mainWindowStatusBar)
: QWidget(parent),
statusBar(mainWindowStatusBar)
{
layout = new QHBoxLayout(this);
runButton = new QPushButton(RUN);
QFontMetrics fontMetrics = runButton->fontMetrics();
runButton->setMinimumWidth(fontMetrics.width(RUNNING)*1.5);
runButton->setFocusPolicy(Qt::StrongFocus);
connect(runButton, SIGNAL(clicked()), SLOT(runButtonClicked()));
layout->addStretch(1);
layout->addWidget(runButton);
layout->addStretch(1);
processManager = new ProcessManager(this);
converter = new ConvertAllEps(this, mainWindowStatusBar);
pdf_concat = new ConcatPdfs(this);
connect(processManager, SIGNAL(runFinished(bool,QString)), SLOT(onRunFinished(bool,QString)));
connect(processManager, SIGNAL(runStarted()), SLOT(onRunStarted()));
connect(converter, SIGNAL(finished(bool)), SLOT(onConversionFinished(bool)));
connect(pdf_concat, SIGNAL(finished(bool)), SLOT(onConcatFinished(bool)));
}
void Runner::newProjectSelected(const QString& newProjectPath,
const QString& newProjectName)
{
projectPath = newProjectPath;
projectName = newProjectName;
logFile = "log.txt";
}
void Runner::runButtonClicked() {
QFileInfo rgfFile(projectPath+"/"+projectName+".rgf");
if (!rgfFile.exists() || !rgfFile.isFile()) {
showErrorMsg("the data file is not created or selected yet");
return;
}
emit generateSetFile();
qDebug() << "Runner invokes external exe";
QStringList args;
args.append(projectName);
processManager->run(projectPath, args, logFile);
}
void Runner::onRunStarted() {
runButton->setDisabled(true);
runButton->setText(RUNNING);
statusBar->showMessage("Running, please be patient...");
qDebug() << "Button disabled!";
}
void Runner::onRunFinished(bool success, const QString& errorMsg) {
if (!success) {
showErrorMsg(errorMsg);
}
showLog();
bool pointerToFolderOK = false;
finalProjectFolder = dirToShow(pointerToFolderOK); // Side effect: always deletes the pointer
if (pointerToFolderOK && opts().getConvertToPdf()) {
converter->run(finalProjectFolder); // calls onConversionFinished in the end
}
else {
onConcatFinished(success && pointerToFolderOK);
}
}
void Runner::onConversionFinished(bool ok) {
if (ok) {
statusBar->showMessage("Concatenating all PDF files"); // Hack: compare with MainWindow closeEvent()
pdf_concat->run(finalProjectFolder, projectName);
}
else {
onConcatFinished(ok);
}
}
void Runner::onConcatFinished(bool ok) {
runButton->setText(RUN);
runButton->setEnabled(true);
QString msg = ok ? QString("Done") : QString("Error occured");
statusBar->showMessage(msg, 2000);
qDebug() << "Button enabled!";
showResultDir();
showPdfIfDemo();
}
void Runner::showLog() const {
if (opts().getShowLogfile()) {
openInTextEditor(projectPath+"/"+logFile);
}
}
void Runner::showResultDir() const {
if (opts().getShowResultDirectory()) {
showInFileManager(finalProjectFolder);
}
}
void Runner::showPdfIfDemo() const {
if (projectName!="demo") {
return;
}
QVector<QString> demoPDFs = QVector<QString>();
demoPDFs.append(finalProjectFolder+"/5_PS_SEPARATED/FRACTURE/WELL-1_FRACTURE.pdf");
demoPDFs.append(finalProjectFolder+"/6_WELL_PS_SEPARATED/FRACTURE/WELL-1_FRACTURE_TILTED.pdf");
foreach (QString demoPDF, demoPDFs) {
qDebug() << "Trying to open demo pdf at" << demoPDF;
if (QFile::exists(demoPDF))
openPDF(demoPDF);
}
}
QString Runner::dirToShow(bool& pointerToFolderOK) const {
pointerToFolderOK = false;
QString dir_to_show = projectPath;
QFile file(projectPath+"/project_folder_name"); // TODO Another magic constant...
if (file.open(QIODevice::ReadOnly)) {
QString folder_name = projectPath + "/" +QTextStream(&file).readLine();
if (folder_name.endsWith(projectName, Qt::CaseInsensitive) &&
QFileInfo(folder_name).isDir())
{
dir_to_show = folder_name;
pointerToFolderOK = true;
}
}
file.remove();
return dir_to_show;
}