compiles on openSUSE 15.4 part 2
[engrid-github.git] / src / main.cpp
blob538be3547e33c8e6b3903a988003dd3f2b9eb61a
1 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 // + +
3 // + This file is part of enGrid. +
4 // + +
5 // + Copyright 2008-2014 enGits GmbH +
6 // + +
7 // + enGrid is free software: you can redistribute it and/or modify +
8 // + it under the terms of the GNU General Public License as published by +
9 // + the Free Software Foundation, either version 3 of the License, or +
10 // + (at your option) any later version. +
11 // + +
12 // + enGrid 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 enGrid. If not, see <http://www.gnu.org/licenses/>. +
19 // + +
20 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
21 #include <QtCore>
22 #include <QtGui>
23 #include <QApplication>
24 #include <QFile>
25 #include <QTextStream>
26 #include <QDate>
27 #include <qglobal.h>
29 #include "guimainwindow.h"
30 #include "filetemplate.h"
32 #include "geometrytools.h"
33 using namespace GeometryTools;
35 ///\todo replace with shellscript?
36 void appendLicense(int argc, char ** argv)
38 int first_year = 2008;
39 QString first_year_text;
40 first_year_text.setNum(first_year);
41 int year = QDate::currentDate().year();
42 QString year_text;
43 year_text.setNum(year);
44 if (year-first_year == 1) {
45 year_text = first_year_text + "," + year_text;
47 if (year-first_year > 1) {
48 year_text = first_year_text + "-" + year_text;
50 QString year_end_text = " +\n";
51 if (year == first_year) {
52 year_end_text = " " + year_end_text;
54 for (int i = 2; i < argc; ++i) {
55 QString filename(argv[i]);
56 QString comment = "// ";
57 QString buffer = "";
58 bool script = false;
59 if (filename.right(3) == ".sh") script = true;
60 if (filename.right(3) == ".py") script = true;
61 if (filename.right(5) == ".bash") script = true;
62 QFile file(filename);
63 file.open(QIODevice::ReadOnly);
64 QTextStream f(&file);
65 if (script) {
66 comment = "# ";
67 QString line = f.readLine();
68 buffer += line + "\n";
70 buffer += comment + "\n";
71 buffer += comment + "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n";
72 buffer += comment + "+ +\n";
73 buffer += comment + "+ This file is part of enGrid. +\n";
74 buffer += comment + "+ +\n";
75 buffer += comment + "+ Copyright " + year_text + " enGits GmbH" + year_end_text;
76 buffer += comment + "+ +\n";
77 buffer += comment + "+ enGrid is free software: you can redistribute it and/or modify +\n";
78 buffer += comment + "+ it under the terms of the GNU General Public License as published by +\n";
79 buffer += comment + "+ the Free Software Foundation, either version 3 of the License, or +\n";
80 buffer += comment + "+ (at your option) any later version. +\n";
81 buffer += comment + "+ +\n";
82 buffer += comment + "+ enGrid is distributed in the hope that it will be useful, +\n";
83 buffer += comment + "+ but WITHOUT ANY WARRANTY; without even the implied warranty of +\n";
84 buffer += comment + "+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +\n";
85 buffer += comment + "+ GNU General Public License for more details. +\n";
86 buffer += comment + "+ +\n";
87 buffer += comment + "+ You should have received a copy of the GNU General Public License +\n";
88 buffer += comment + "+ along with enGrid. If not, see <http://www.gnu.org/licenses/>. +\n";
89 buffer += comment + "+ +\n";
90 buffer += comment + "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n";
91 buffer += comment + "\n";
93 bool src_code = false;
94 while (!f.atEnd()) {
95 QString line = f.readLine();
96 if (!src_code) {
97 if (line.size() >= comment.length()-1) {
98 if (line.left(comment.length()-1) != comment.left(comment.length()-1)) {
99 src_code = true;
101 } else {
102 src_code = true;
105 if (src_code) {
106 buffer += line + "\n";
111 QFile file(filename);
112 file.open(QIODevice::WriteOnly);
113 QTextStream f(&file);
114 f << buffer;
119 ///\todo replace with shellscript?
120 void makeDistribution()
122 system ("ldd ./engrid > ldd.out");
123 system ("mkdir enGrid");
124 system ("cp engrid enGrid");
126 QFile file("ldd.out");
127 file.open(QIODevice::ReadOnly);
128 QTextStream f(&file);
129 while (!f.atEnd()) {
130 QString line = f.readLine();
131 QTextStream l(&line, QIODevice::ReadOnly);
132 QString word;
133 l >> word;
134 if (word.left(1) != "/") {
135 l >> word;
136 l >> word;
138 QString cmd = "cp " + word + " enGrid";
139 system(qPrintable(cmd));
140 cout << qPrintable(cmd) << endl;
143 system ("tar czf enGrid_bin.tar.gz enGrid/*");
144 system ("rm -rf enGrid");
145 system ("rm ldd.out");
148 /** Message handler to allow output of Qt objects in the terminal (stderr) or the logfile/output window of engrid (stdout)
149 qDebug() is used for writing custom debug output.
150 qWarning() is used to report warnings and recoverable errors in your application.
151 qCritical() is used for writing critical error mesages and reporting system errors.
152 qFatal() is used for writing fatal error messages shortly before exiting.
154 void engridMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
156 if (type == QtDebugMsg) {
157 fprintf(stdout, "%s", qPrintable(msg));
159 if (type == QtWarningMsg) {
160 fprintf(stderr, "%s", qPrintable(msg));
162 if (type == QtCriticalMsg) {
163 fprintf(stderr, "Critical: %s\n", qPrintable(msg));
165 if (type == QtFatalMsg) {
166 fprintf(stderr, "Fatal: %s\n", qPrintable(msg));
167 abort();
171 double getNumber(QString text)
173 double myNumber = 0;
174 while (true) {
175 qDebug() << text;
176 string input = "";
177 getline(cin, input);
179 // This code converts from string to number safely.
180 stringstream myStream(input);
181 if (myStream >> myNumber)
182 break;
183 qDebug() << "Invalid number, please try again" << endl;
185 return myNumber;
188 int main( int argc, char ** argv )
191 #ifdef QT_DEBUG
192 //omp_set_num_threads(1);
193 #endif
194 qInstallMessageHandler(engridMessageHandler);
195 Q_INIT_RESOURCE(engrid);
196 int app_result=0;
198 ///\todo use gnu getopt ? Check windows/mac compatibility.
199 if (argc > 1) {
200 if (QString(argv[1]) == QString("-h")) {
201 QFileInfo file_info(argv[0]);
202 cout<<"Usage:"<<endl;
203 cout<<qPrintable(file_info.fileName())<<" : start engrid"<<endl;
204 cout<<qPrintable(file_info.fileName())<<" -f FILE : start engrid and open FILE"<<endl;
205 cout<<qPrintable(file_info.fileName())<<" -h : Display usage instructions"<<endl;
206 cout<<qPrintable(file_info.fileName())<<" -appendlic FILE1 FILE2 ...: Append license to files"<<endl;
207 cout<<qPrintable(file_info.fileName())<<" -distbin : Create binary distribution"<<endl;
208 exit(0);
210 if (QString(argv[1]) == QString("-appendlic")) {
211 appendLicense(argc, argv);
213 if (QString(argv[1]) == QString("-distbin")) {
214 makeDistribution();
216 if (QString(argv[1]) == QString("-f") && argc == 3) {
217 QApplication a( argc, argv );
218 QString filename = QString(argv[2]);
219 GuiMainWindow w(filename);
220 w.show();
221 a.connect( &a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) );
222 app_result = a.exec();
224 } else {
225 QApplication a( argc, argv );
226 GuiMainWindow w;
227 w.show();
228 a.connect( &a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) );
229 app_result = a.exec();
232 return app_result;