Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ElmerCSC
GitHub Repository: ElmerCSC/elmerfem
Path: blob/devel/ElmerGUIlogger/src/mainwindow.cpp
3196 views
1
/*****************************************************************************
2
* *
3
* Elmer, A Finite Element Software for Multiphysical Problems *
4
* *
5
* Copyright 1st April 1995 - , CSC - IT Center for Science Ltd., Finland *
6
* *
7
* This program is free software; you can redistribute it and/or *
8
* modify it under the terms of the GNU General Public License *
9
* as published by the Free Software Foundation; either version 2 *
10
* of the License, or (at your option) any later version. *
11
* *
12
* This program is distributed in the hope that it will be useful, *
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15
* GNU General Public License for more details. *
16
* *
17
* You should have received a copy of the GNU General Public License *
18
* along with this program (in file fem/GPL-2); if not, write to the *
19
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, *
20
* Boston, MA 02110-1301, USA. *
21
* *
22
*****************************************************************************/
23
24
/*****************************************************************************
25
* *
26
* ElmerGUIlogger *
27
* *
28
*****************************************************************************
29
* *
30
* Authors: Mikko Lyly, Juha Ruokolainen and Peter RÃ¥back *
31
* Small fixes: Boris Pek, Juhani Kataja *
32
* Email: [email protected] *
33
* Web: http://www.csc.fi/elmer *
34
* Address: CSC - IT Center for Science Ltd. *
35
* Keilaranta 14 *
36
* 02101 Espoo, Finland *
37
* *
38
* Original Date: 24 Aug 2009 *
39
* *
40
*****************************************************************************/
41
42
#include <QAction>
43
#include <QMenu>
44
#include <QMenuBar>
45
#include <QTextEdit>
46
#include <QIcon>
47
#include <QColor>
48
#include <QFileDialog>
49
#include <QFile>
50
#include <QTextStream>
51
#include <QPrintDialog>
52
#include <QPrinter>
53
#include <QByteArray>
54
#include <QCoreApplication>
55
#include "mainwindow.h"
56
57
MainWindow::MainWindow()
58
{
59
setWindowTitle("ElmerGUI log window");
60
setWindowIcon(QIcon(":/icons/ElmerGUI.png"));
61
resize(600, 300);
62
63
textEdit = new QTextEdit(this);
64
setCentralWidget(textEdit);
65
textEdit->setTextColor(Qt::darkGreen);
66
67
createActions();
68
createMenus();
69
70
QString ELMER_HOME = getenv("ELMER_HOME");
71
QString ELMERGUI_HOME = getenv("ELMERGUI_HOME");
72
QString ELMER_POST_HOME = getenv("ELMER_POST_HOME");
73
74
if(ELMER_HOME.isEmpty())
75
ELMER_HOME = QCoreApplication::applicationDirPath() + "/..";
76
77
if(ELMERGUI_HOME.isEmpty())
78
ELMERGUI_HOME = QCoreApplication::applicationDirPath();
79
80
if(ELMER_POST_HOME.isEmpty())
81
ELMER_POST_HOME = QCoreApplication::applicationDirPath() + "/../share/elmerpost";
82
83
textEdit->append("ELMER_HOME=" + ELMER_HOME);
84
textEdit->append("ELMERGUI_HOME=" + ELMERGUI_HOME);
85
textEdit->append("ELMER_POST_HOME=" + ELMER_POST_HOME);
86
87
elmerGUI = new QProcess(this);
88
89
connect(elmerGUI, SIGNAL(error(QProcess::ProcessError)), this, SLOT(errorSlot(QProcess::ProcessError)));
90
connect(elmerGUI, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(finishedSlot(int, QProcess::ExitStatus)));
91
connect(elmerGUI, SIGNAL(readyReadStandardOutput()), this, SLOT(stdoutSlot()));
92
connect(elmerGUI, SIGNAL(readyReadStandardError()), this, SLOT(stderrSlot()));
93
connect(elmerGUI, SIGNAL(started()), this, SLOT(startedSlot()));
94
connect(elmerGUI, SIGNAL(stateChanged(QProcess::ProcessState)), this, SLOT(stateChangedSlot(QProcess::ProcessState)));
95
96
startElmerGUISlot();
97
}
98
99
MainWindow::~MainWindow()
100
{
101
}
102
103
void MainWindow::createActions()
104
{
105
startElmerGUIAct = new QAction(QIcon(":/icons/ElmerGUI.png"), tr("Start ElmerGUI"), this);
106
startElmerGUIAct->setShortcut(tr("Ctrl+R"));
107
connect(startElmerGUIAct, SIGNAL(triggered()), this, SLOT(startElmerGUISlot()));
108
109
saveAsAct = new QAction(QIcon(":/icons/document-save-as.png"), tr("Save as..."), this);
110
saveAsAct->setShortcut(tr("Ctrl+S"));
111
connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAsSlot()));
112
113
printAct = new QAction(QIcon(":/icons/document-print.png"), tr("Print..."), this);
114
printAct->setShortcut(tr("Ctrl+P"));
115
connect(printAct, SIGNAL(triggered()), this, SLOT(printSlot()));
116
117
exitAct = new QAction(QIcon(":/icons/application-exit.png"), tr("Exit"), this);
118
exitAct->setShortcut(tr("Ctrl+Q"));
119
connect(exitAct, SIGNAL(triggered()), this, SLOT(exitSlot()));
120
}
121
122
void MainWindow::createMenus()
123
{
124
fileMenu = menuBar()->addMenu(tr("&File"));
125
fileMenu->addAction(startElmerGUIAct);
126
fileMenu->addSeparator();
127
fileMenu->addAction(saveAsAct);
128
fileMenu->addSeparator();
129
fileMenu->addAction(printAct);
130
fileMenu->addSeparator();
131
fileMenu->addAction(exitAct);
132
}
133
134
void MainWindow::startElmerGUISlot()
135
{
136
textEdit->setTextColor(Qt::darkGreen);
137
138
textEdit->append("Starting ElmerGUI:");
139
QString ELMERGUI_BIN = QCoreApplication::applicationDirPath() + "/ElmerGUI";
140
textEdit->append(ELMERGUI_BIN);
141
142
elmerGUI->start(ELMERGUI_BIN, QStringList());
143
144
if(!elmerGUI->waitForStarted()) {
145
textEdit->setTextColor(Qt::darkGreen);
146
textEdit->append("The executable ElmerGUI(.exe) is either"
147
"missing, or then there is no path to it");
148
}
149
else {
150
startElmerGUIAct->setEnabled(false);
151
}
152
}
153
154
void MainWindow::saveAsSlot()
155
{
156
textEdit->setTextColor(Qt::darkGreen);
157
158
QString fileName = QFileDialog::getSaveFileName(this, tr("Save text file"));
159
160
if(fileName.isEmpty()) {
161
textEdit->append("File name is empty");
162
return;
163
}
164
165
QFile file;
166
167
file.setFileName(fileName);
168
169
if(!file.open(QIODevice::WriteOnly)) {
170
textEdit->append("Unable to open file");
171
return;
172
}
173
174
QTextStream outputStream(&file);
175
176
outputStream << textEdit->toPlainText();
177
178
file.close();
179
180
textEdit->append("File saved");
181
}
182
183
void MainWindow::printSlot()
184
{
185
textEdit->setTextColor(Qt::darkGreen);
186
187
QTextDocument* document = textEdit->document();
188
189
QPrinter printer;
190
191
QPrintDialog *printDialog = new QPrintDialog(&printer, this);
192
193
if(printDialog->exec() != QDialog::Accepted)
194
return;
195
196
document->print(&printer);
197
198
textEdit->append("Printed");
199
}
200
201
void MainWindow::exitSlot()
202
{
203
if(elmerGUI->state() == QProcess::Running) {
204
205
elmerGUI->kill();
206
207
if(!elmerGUI->waitForFinished())
208
textEdit->append("Failed killing process - closing anyways");
209
}
210
211
close();
212
}
213
214
void MainWindow::errorSlot(QProcess::ProcessError error)
215
{
216
textEdit->setTextColor(Qt::blue);
217
textEdit->append("Process error");
218
219
switch(error) {
220
case(QProcess::FailedToStart):
221
textEdit->append("Error: Failed to start");
222
break;
223
case(QProcess::Crashed):
224
textEdit->append("Error: Crashed");
225
break;
226
case(QProcess::Timedout):
227
textEdit->append("Error: Timedout");
228
break;
229
case(QProcess::WriteError):
230
textEdit->append("Error: WriteError");
231
break;
232
case(QProcess::ReadError):
233
textEdit->append("Error: ReadError");
234
break;
235
case(QProcess::UnknownError):
236
textEdit->append("Error: UnknownError");
237
break;
238
default:
239
textEdit->append("Error type unknown");
240
}
241
}
242
243
void MainWindow::finishedSlot(int exitCode, QProcess::ExitStatus status)
244
{
245
textEdit->setTextColor(Qt::blue);
246
textEdit->append("Finished");
247
248
textEdit->append("Exit code: " + QString::number(exitCode));
249
250
switch(status) {
251
case(QProcess::NormalExit):
252
textEdit->append("Status: NormalExit");
253
break;
254
case(QProcess::CrashExit):
255
textEdit->append("Status: CrashExit");
256
break;
257
default:
258
textEdit->append("Exit status unknown");
259
}
260
261
startElmerGUIAct->setEnabled(true);
262
}
263
264
void MainWindow::stdoutSlot()
265
{
266
static QString qs_save = "";
267
268
QString out = elmerGUI->readAllStandardOutput().replace("\r", "");
269
QString qs = qs_save + out;
270
271
int n = qs.lastIndexOf('\n');
272
273
if((n > 0) && (n < qs.size()-1)) {
274
qs_save = qs.mid(n+1);
275
qs = qs.mid(0, n);
276
277
} else if(n == 0) {
278
if(qs.size() == 1) {
279
qs_save = "";
280
return;
281
}
282
qs_save = qs.mid(1);
283
return;
284
285
} else if(n < 0) {
286
qs_save = qs;
287
return;
288
289
} else qs_save = "";
290
291
while(qs.at(qs.size()-1).unicode() == '\n')
292
qs.chop(1);
293
294
if(qs.isEmpty())
295
return;
296
297
textEdit->setTextColor(Qt::black);
298
textEdit->append(qs);
299
}
300
301
void MainWindow::stderrSlot()
302
{
303
static QString qs_save = "";
304
305
QString err = elmerGUI->readAllStandardError().replace("\r", "");
306
QString qs = qs_save + err;
307
308
int n = qs.lastIndexOf('\n');
309
310
if((n > 0) && (n < qs.size()-1)) {
311
qs_save = qs.mid(n+1);
312
qs = qs.mid(0, n);
313
314
} else if(n == 0) {
315
if(qs.size() == 1) {
316
qs_save = "";
317
return;
318
}
319
qs_save = qs.mid(1);
320
return;
321
322
} else if(n < 0) {
323
qs_save = qs;
324
return;
325
326
} else qs_save = "";
327
328
while(qs.at(qs.size()-1).unicode() == '\n')
329
qs.chop(1);
330
331
if(qs.isEmpty())
332
return;
333
334
textEdit->setTextColor(Qt::red);
335
textEdit->append(qs);
336
}
337
338
void MainWindow::startedSlot()
339
{
340
textEdit->setTextColor(Qt::blue);
341
textEdit->append("Started");
342
}
343
344
void MainWindow::stateChangedSlot(QProcess::ProcessState state)
345
{
346
textEdit->setTextColor(Qt::blue);
347
textEdit->append("Process state changed");
348
349
switch(state) {
350
case(QProcess::NotRunning):
351
textEdit->append("State: NotRunning");
352
break;
353
case(QProcess::Starting):
354
textEdit->append("State: Starting");
355
break;
356
case(QProcess::Running):
357
textEdit->append("State: Running");
358
break;
359
default:
360
textEdit->append("Process state unknown");
361
}
362
}
363
364