preparing merge of master to release
[engrid.git] / src / main.cpp
blob97f90adc6e62754e498158c5c3b10fda0ab0d25e
1 //
2 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 // + +
4 // + This file is part of enGrid. +
5 // + +
6 // + Copyright 2008,2009 Oliver Gloth +
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 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
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 buffer = "//\n";
58 buffer += "// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n";
59 buffer += "// + +\n";
60 buffer += "// + This file is part of enGrid. +\n";
61 buffer += "// + +\n";
62 buffer += "// + Copyright " + year_text + " Oliver Gloth" + year_end_text;
63 buffer += "// + +\n";
64 buffer += "// + enGrid is free software: you can redistribute it and/or modify +\n";
65 buffer += "// + it under the terms of the GNU General Public License as published by +\n";
66 buffer += "// + the Free Software Foundation, either version 3 of the License, or +\n";
67 buffer += "// + (at your option) any later version. +\n";
68 buffer += "// + +\n";
69 buffer += "// + enGrid is distributed in the hope that it will be useful, +\n";
70 buffer += "// + but WITHOUT ANY WARRANTY; without even the implied warranty of +\n";
71 buffer += "// + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +\n";
72 buffer += "// + GNU General Public License for more details. +\n";
73 buffer += "// + +\n";
74 buffer += "// + You should have received a copy of the GNU General Public License +\n";
75 buffer += "// + along with enGrid. If not, see <http://www.gnu.org/licenses/>. +\n";
76 buffer += "// + +\n";
77 buffer += "// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n";
78 buffer += "//\n";
80 bool src_code = false;
81 QFile file(filename);
82 file.open(QIODevice::ReadOnly);
83 QTextStream f(&file);
84 while (!f.atEnd()) {
85 QString line = f.readLine();
86 if (!src_code) {
87 if (line.size() >= 2) {
88 if (line.left(2) != "//") {
89 src_code = true;
91 } else {
92 src_code = true;
95 if (src_code) {
96 buffer += line + "\n";
101 QFile file(filename);
102 file.open(QIODevice::WriteOnly);
103 QTextStream f(&file);
104 f << buffer;
109 ///\todo replace with shellscript?
110 void makeDistribution()
112 system ("ldd ./engrid > ldd.out");
113 system ("mkdir enGrid");
114 system ("cp engrid enGrid");
116 QFile file("ldd.out");
117 file.open(QIODevice::ReadOnly);
118 QTextStream f(&file);
119 while (!f.atEnd()) {
120 QString line = f.readLine();
121 QTextStream l(&line, QIODevice::ReadOnly);
122 QString word;
123 l >> word;
124 if (word.left(1) != "/") {
125 l >> word;
126 l >> word;
128 QString cmd = "cp " + word + " enGrid";
129 system(qPrintable(cmd));
130 cout << qPrintable(cmd) << endl;
133 system ("tar czf enGrid_bin.tar.gz enGrid/*");
134 system ("rm -rf enGrid");
135 system ("rm ldd.out");
138 /** Message handler to allow output of Qt objects in the terminal (stderr) or the logfile/output window of engrid (stdout)
139 qDebug() is used for writing custom debug output.
140 qWarning() is used to report warnings and recoverable errors in your application.
141 qCritical() is used for writing critical error mesages and reporting system errors.
142 qFatal() is used for writing fatal error messages shortly before exiting.
144 void engridMessageHandler(QtMsgType type, const char *msg)
146 switch (type) {
147 case QtDebugMsg:
148 // fprintf(stdout, "%s", msg);
149 cout<<msg<<endl;
150 break;
151 case QtWarningMsg:
152 // fprintf(stderr, "%s", msg);
153 cerr<<msg<<endl;
154 break;
155 case QtCriticalMsg:
156 fprintf(stderr, "Critical: %s\n", msg);
157 break;
158 case QtFatalMsg:
159 fprintf(stderr, "Fatal: %s\n", msg);
160 abort();
164 int main( int argc, char ** argv )
166 qInstallMsgHandler(engridMessageHandler);
167 Q_INIT_RESOURCE(engrid);
169 ///\todo use gnu getopt ? Check windows/mac compatibility.
170 if (argc > 1) {
171 if (QString(argv[1]) == QString("-h")) {
172 QFileInfo file_info(argv[0]);
173 cout<<"Usage:"<<endl;
174 cout<<qPrintable(file_info.fileName())<<" : start engrid"<<endl;
175 cout<<qPrintable(file_info.fileName())<<" -f FILE : start engrid and open FILE"<<endl;
176 cout<<qPrintable(file_info.fileName())<<" -h : Display usage instructions"<<endl;
177 cout<<qPrintable(file_info.fileName())<<" -appendlic FILE1 FILE2 ...: Append license to files"<<endl;
178 cout<<qPrintable(file_info.fileName())<<" -distbin : Create binary distribution"<<endl;
179 exit(0);
181 if (QString(argv[1]) == QString("-appendlic")) {
182 appendLicense(argc, argv);
184 if (QString(argv[1]) == QString("-distbin")) {
185 makeDistribution();
187 if (QString(argv[1]) == QString("-f") && argc == 3) {
188 QApplication a( argc, argv );
189 GuiMainWindow w;
190 w.show();
191 a.connect( &a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) );
192 GuiMainWindow::pointer()->open(QString(argv[2]));//this could aventually be done with another GuiMainWindow constructor
193 a.exec();
195 } else {
196 QApplication a( argc, argv );
197 GuiMainWindow w;
198 w.show();
199 a.connect( &a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) );
200 a.exec();