some OpenMP testing
[engrid.git] / src / main.cpp
blob5aae5a5505ef53526c0b366e1ff9561294e5aedd
1 //
2 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 // + +
4 // + This file is part of enGrid. +
5 // + +
6 // + Copyright 2008-2013 enGits GmbH +
7 // + +
8 // + enGrid is free software: you can redistribute it and/or modify +
9 // + it under the terms of the GNU General Public License as published by +
10 // + the Free Software Foundation, either version 3 of the License, or +
11 // + (at your option) any later version. +
12 // + +
13 // + enGrid is distributed in the hope that it will be useful, +
14 // + but WITHOUT ANY WARRANTY; without even the implied warranty of +
15 // + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +
16 // + GNU General Public License for more details. +
17 // + +
18 // + You should have received a copy of the GNU General Public License +
19 // + along with enGrid. If not, see <http://www.gnu.org/licenses/>. +
20 // + +
21 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
22 //
23 #include <QtCore>
24 #include <QtGui>
25 #include <QApplication>
26 #include <QFile>
27 #include <QTextStream>
28 #include <QDate>
30 #include "guimainwindow.h"
31 #include "filetemplate.h"
33 #include "geometrytools.h"
34 using namespace GeometryTools;
36 ///\todo replace with shellscript?
37 void appendLicense(int argc, char ** argv)
39 int first_year = 2008;
40 QString first_year_text;
41 first_year_text.setNum(first_year);
42 int year = QDate::currentDate().year();
43 QString year_text;
44 year_text.setNum(year);
45 if (year-first_year == 1) {
46 year_text = first_year_text + "," + year_text;
48 if (year-first_year > 1) {
49 year_text = first_year_text + "-" + year_text;
51 QString year_end_text = " +\n";
52 if (year == first_year) {
53 year_end_text = " " + year_end_text;
55 for (int i = 2; i < argc; ++i) {
56 QString filename(argv[i]);
57 QString comment = "// ";
58 QString buffer = "";
59 bool script = false;
60 if (filename.right(3) == ".sh") script = true;
61 if (filename.right(3) == ".py") script = true;
62 if (filename.right(5) == ".bash") script = true;
63 QFile file(filename);
64 file.open(QIODevice::ReadOnly);
65 QTextStream f(&file);
66 if (script) {
67 comment = "# ";
68 QString line = f.readLine();
69 buffer += line + "\n";
71 buffer += comment + "\n";
72 buffer += comment + "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n";
73 buffer += comment + "+ +\n";
74 buffer += comment + "+ This file is part of enGrid. +\n";
75 buffer += comment + "+ +\n";
76 buffer += comment + "+ Copyright " + year_text + " enGits GmbH" + year_end_text;
77 buffer += comment + "+ +\n";
78 buffer += comment + "+ enGrid is free software: you can redistribute it and/or modify +\n";
79 buffer += comment + "+ it under the terms of the GNU General Public License as published by +\n";
80 buffer += comment + "+ the Free Software Foundation, either version 3 of the License, or +\n";
81 buffer += comment + "+ (at your option) any later version. +\n";
82 buffer += comment + "+ +\n";
83 buffer += comment + "+ enGrid is distributed in the hope that it will be useful, +\n";
84 buffer += comment + "+ but WITHOUT ANY WARRANTY; without even the implied warranty of +\n";
85 buffer += comment + "+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +\n";
86 buffer += comment + "+ GNU General Public License for more details. +\n";
87 buffer += comment + "+ +\n";
88 buffer += comment + "+ You should have received a copy of the GNU General Public License +\n";
89 buffer += comment + "+ along with enGrid. If not, see <http://www.gnu.org/licenses/>. +\n";
90 buffer += comment + "+ +\n";
91 buffer += comment + "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n";
92 buffer += comment + "\n";
94 bool src_code = false;
95 while (!f.atEnd()) {
96 QString line = f.readLine();
97 if (!src_code) {
98 if (line.size() >= comment.length()-1) {
99 if (line.left(comment.length()-1) != comment.left(comment.length()-1)) {
100 src_code = true;
102 } else {
103 src_code = true;
106 if (src_code) {
107 buffer += line + "\n";
112 QFile file(filename);
113 file.open(QIODevice::WriteOnly);
114 QTextStream f(&file);
115 f << buffer;
120 ///\todo replace with shellscript?
121 void makeDistribution()
123 system ("ldd ./engrid > ldd.out");
124 system ("mkdir enGrid");
125 system ("cp engrid enGrid");
127 QFile file("ldd.out");
128 file.open(QIODevice::ReadOnly);
129 QTextStream f(&file);
130 while (!f.atEnd()) {
131 QString line = f.readLine();
132 QTextStream l(&line, QIODevice::ReadOnly);
133 QString word;
134 l >> word;
135 if (word.left(1) != "/") {
136 l >> word;
137 l >> word;
139 QString cmd = "cp " + word + " enGrid";
140 system(qPrintable(cmd));
141 cout << qPrintable(cmd) << endl;
144 system ("tar czf enGrid_bin.tar.gz enGrid/*");
145 system ("rm -rf enGrid");
146 system ("rm ldd.out");
149 /** Message handler to allow output of Qt objects in the terminal (stderr) or the logfile/output window of engrid (stdout)
150 qDebug() is used for writing custom debug output.
151 qWarning() is used to report warnings and recoverable errors in your application.
152 qCritical() is used for writing critical error mesages and reporting system errors.
153 qFatal() is used for writing fatal error messages shortly before exiting.
155 void engridMessageHandler(QtMsgType type, const char *msg)
157 switch (type) {
158 case QtDebugMsg:
159 // fprintf(stdout, "%s", msg);
160 cout<<msg<<endl;
161 break;
162 case QtWarningMsg:
163 // fprintf(stderr, "%s", msg);
164 cerr<<msg<<endl;
165 break;
166 case QtCriticalMsg:
167 fprintf(stderr, "Critical: %s\n", msg);
168 break;
169 case QtFatalMsg:
170 fprintf(stderr, "Fatal: %s\n", msg);
171 abort();
175 double getNumber(QString text)
177 double myNumber = 0;
178 while (true) {
179 qDebug() << text;
180 string input = "";
181 getline(cin, input);
183 // This code converts from string to number safely.
184 stringstream myStream(input);
185 if (myStream >> myNumber)
186 break;
187 qDebug() << "Invalid number, please try again" << endl;
189 return myNumber;
192 int main( int argc, char ** argv )
195 double x, y, z, a, b, r;
196 cout << "x = "; cin >> x;
197 cout << "y = "; cin >> y;
198 cout << "z = "; cin >> z;
199 GeometryTools::cart2spherical(vec3_t(x,y,z), a, b, r);
200 cout << endl;
201 cout << "r = " << r << endl;
202 cout << "alpha = " << GeometryTools::rad2deg(a) << endl;
203 cout << "beta = " << GeometryTools::rad2deg(b) << endl;
204 vec3_t X = spherical2cart(a, b, r);
205 cout << "\nX[0] = " << X[0] << endl;
206 cout << "X[1] = " << X[1] << endl;
207 cout << "X[2] = " << X[2] << endl;
208 exit(0);
211 #ifdef QT_DEBUG
212 //omp_set_num_threads(1);
213 #endif
214 qInstallMsgHandler(engridMessageHandler);
215 Q_INIT_RESOURCE(engrid);
216 int app_result=0;
218 ///\todo use gnu getopt ? Check windows/mac compatibility.
219 if (argc > 1) {
220 if (QString(argv[1]) == QString("-h")) {
221 QFileInfo file_info(argv[0]);
222 cout<<"Usage:"<<endl;
223 cout<<qPrintable(file_info.fileName())<<" : start engrid"<<endl;
224 cout<<qPrintable(file_info.fileName())<<" -f FILE : start engrid and open FILE"<<endl;
225 cout<<qPrintable(file_info.fileName())<<" -h : Display usage instructions"<<endl;
226 cout<<qPrintable(file_info.fileName())<<" -appendlic FILE1 FILE2 ...: Append license to files"<<endl;
227 cout<<qPrintable(file_info.fileName())<<" -distbin : Create binary distribution"<<endl;
228 exit(0);
230 if (QString(argv[1]) == QString("-appendlic")) {
231 appendLicense(argc, argv);
233 if (QString(argv[1]) == QString("-distbin")) {
234 makeDistribution();
236 if (QString(argv[1]) == QString("-f") && argc == 3) {
237 QApplication a( argc, argv );
238 QString filename = QString(argv[2]);
239 GuiMainWindow w(filename);
240 w.show();
241 a.connect( &a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) );
242 app_result = a.exec();
244 } else {
245 QApplication a( argc, argv );
246 GuiMainWindow w;
247 w.show();
248 a.connect( &a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) );
249 app_result = a.exec();
252 return app_result;