]> Shamusworld >> Repos - architektonas/blob - src/mainapp/main.cpp
3c0a154addb8199b70feae5b6beafe8c445d8580
[architektonas] / src / mainapp / main.cpp
1 // main.cpp
2 //
3 // Part of the Architektonas Project
4 // Originally part of QCad Community Edition by Andrew Mustun
5 // Extensively rewritten and refactored by James L. Hammons
6 // (C) 2010 Underground Software
7 //
8 // JLH = James L. Hammons <jlhamm@acm.org>
9 //
10 // Who  When        What
11 // ---  ----------  -----------------------------------------------------------
12 // JLH  05/17/2010  Added this text. :-)
13 //
14
15 #include "main.h"
16
17 #include <locale.h>
18
19 #ifdef __APPLE__
20 #include <qmacstyle_mac.h>
21 #endif
22
23 QSplashScreen * splash = 0;
24
25 #ifdef RS_SCRIPTING
26 #include <qsproject.h>
27 #endif
28
29 #include "rs_fontlist.h"
30 #include "rs_patternlist.h"
31 #include "rs_scriptlist.h"
32 #include "settings.h"
33 #include "rs_system.h"
34 #include "rs_fileio.h"
35 #include "rs_filtercxf.h"
36 #include "rs_filterdxf.h"
37 #include "rs_filterdxf1.h"
38
39 #include "dlginitial.h"
40
41 #include "qc_applicationwindow.h"
42
43 #ifdef RS_CAM
44 # include "rs_filtercam.h"
45 # include "rs_camdialog.h"
46 #endif
47
48 //#ifdef RS_CAM
49 //# include "xpm/intro_camexpert.xpm"
50 //# include "xpm/splash_camexpert.xpm"
51 //#else
52 //# include "xpm/intro_qcad.xpm"
53 //# ifndef QC_CUSTOM_SPLASH
54 //#  include "xpm/splash_qcad.xpm"
55 //# endif
56 //#endif
57
58 #ifndef QC_SPLASH_TXTCOL
59 # define QC_SPLASH_TXTCOL Qt::black
60 #endif
61
62 #ifdef QC_BUILTIN_STYLE
63 extern void applyBuiltinStyle();
64 #endif
65
66 /**
67  * Main. Creates Application window.
68  *
69  * @todo Switch back to xpm instead of png. (No, we ain't doin' dat. >:-)
70  * Cleaning up #defines.
71  */
72 int main(int argc, char * argv[])
73 {
74         // Init compiled-in resources
75         Q_INIT_RESOURCE(architektonas);
76
77         RS_DEBUG->setLevel(RS_Debug::D_WARNING);
78         RS_DEBUG->print("param 0: %s", argv[0]);
79
80         // avoid . / , confusion on German environments
81         setlocale(LC_ALL, "C");
82
83         QApplication app(argc, argv);
84
85         for(int i=0; i<app.argc(); i++)
86                 if (QString("--debug") == app.argv()[i])
87                         RS_DEBUG->setLevel(RS_Debug::D_DEBUGGING);
88
89         QFileInfo prgInfo(QFile::decodeName(argv[0]));
90         QString prgDir(prgInfo.absolutePath());
91 //fail  RS_SYSTEM->init(QC_APPNAME, QC_VERSION, QC_APPDIR, prgDir);
92 //      RS_SYSTEM->init("Architektonas", QC_VERSION, "qcad", prgDir);
93         RS_SYSTEM->init("Architektonas", QC_VERSION, "architektonas", prgDir);
94
95         RS_FILEIO->registerFilter(new RS_FilterCXF());
96         RS_FILEIO->registerFilter(new RS_FilterDXF());
97         RS_FILEIO->registerFilter(new RS_FilterDXF1());
98 #ifdef RS_OPT_CAM
99         RS_FILEIO->registerFilter(new RS_FilterCAM());
100 #endif
101
102         // parse command line arguments that might not need a launched program:
103         QStringList fileList = handleArgs(argc, argv);
104
105         QString lang;
106         QString langCmd;
107         QString unit;
108
109         settings.beginGroup("Defaults");
110 #ifndef QC_PREDEFINED_UNIT
111         unit = settings.value("Unit", "Invalid").toString();
112 #else
113         unit = settings.value("Unit", QC_PREDEFINED_UNIT).toString();
114 #endif
115     settings.endGroup();
116
117         // show initial config dialog:
118         if (unit == "Invalid")
119         {
120                 RS_DEBUG->print("main: show initial config dialog..");
121                 DlgInitial di(NULL);
122                 di.setText("<font size=\"+1\"><b>Welcome to Architektonas</b></font>"
123                 "<br>"
124                 "Please choose the unit you want to use for new drawings and your "
125                 "preferred language.<br>"
126                 "You can changes these settings later in the Options Dialog of Architektonas.");
127 //              QPixmap pxm(":/res/intro.xpm");
128 //              di.setPixmap(pxm);
129                 di.setPixmap(QPixmap(":/res/intro.xpm"));
130
131                 if (di.exec())
132                 {
133                         settings.beginGroup("Defaults");
134                         unit = settings.value("Unit", "None").toString();
135                 settings.endGroup();
136                 }
137
138                 RS_DEBUG->print("main: show initial config dialog: OK");
139         }
140
141 #ifdef QSPLASHSCREEN_H
142         RS_DEBUG->print("main: splashscreen..");
143
144 //# ifdef QC_CUSTOM_SPLASH
145 //      QPixmap * pixmap = new QPixmap(qPixmapFromMimeSource(QC_CUSTOM_SPLASH));
146 //# else
147 ////    QPixmap * pixmap = new QPixmap(":/res/splash.xpm");
148         QPixmap * pixmap = new QPixmap(":/res/splash_qcad.xpm");
149 //# endif
150
151 /*
152         splash = new QSplashScreen(*pixmap);
153         splash->show();
154         splash->message(app.tr("Loading.."),
155                 Qt::AlignRight|Qt::AlignBottom, QC_SPLASH_TXTCOL);
156         RS_DEBUG->print("main: splashscreen: OK");
157 */
158 #endif
159
160         RS_DEBUG->print("main: init fontlist..");
161     RS_FONTLIST->init();
162         RS_DEBUG->print("main: init fontlist: OK");
163
164         RS_DEBUG->print("main: init patternlist..");
165     RS_PATTERNLIST->init();
166         RS_DEBUG->print("main: init patternlist: OK");
167
168         RS_DEBUG->print("main: init scriptlist..");
169     RS_SCRIPTLIST->init();
170         RS_DEBUG->print("main: init scriptlist: OK");
171
172         RS_DEBUG->print("main: loading translation..");
173         settings.beginGroup("Appearance");
174 #ifdef QC_PREDEFINED_LOCALE
175         lang = settings.value("Language", "").toString();
176
177         if (lang.isEmpty())
178         {
179                 lang = QC_PREDEFINED_LOCALE;
180                 settings.setValue("Language", lang);
181         }
182
183         langCmd = settings.value("LanguageCmd", "").toString();
184
185         if (langCmd.isEmpty())
186         {
187                 langCmd = QC_PREDEFINED_LOCALE;
188                 settings.setValue("LanguageCmd", langCmd);
189         }
190 #else
191     lang = settings.value("Language", "en").toString();
192     langCmd = settings.value("LanguageCmd", "en").toString();
193 #endif
194         settings.endGroup();
195
196         RS_SYSTEM->loadTranslation(lang, langCmd);
197         RS_DEBUG->print("main: loading translation: OK");
198
199 #ifdef QSPLASHSCREEN_H
200         splash = new QSplashScreen(*pixmap);
201         splash->show();
202 //      splash->message(QObject::tr("Loading.."), Qt::AlignRight | Qt::AlignBottom, QC_SPLASH_TXTCOL);
203         splash->showMessage(QObject::tr("Loading..."), Qt::AlignRight | Qt::AlignBottom, QC_SPLASH_TXTCOL);
204         RS_DEBUG->print("main: splashscreen: OK");
205 #endif
206
207         //QApplication::setStyle(new QWindowsStyle());
208         //QApplication::setStyle(new QPlatinumStyle());
209 #ifdef __APPLE__
210         QApplication::setStyle(new QMacStyle());
211 #endif
212
213 #ifdef QC_BUILTIN_STYLE //js:
214         RS_DEBUG->print("main: applying built in style..");
215         applyBuiltinStyle();
216 #endif
217
218         RS_DEBUG->print("main: creating main window..");
219     QC_ApplicationWindow * appWin = new QC_ApplicationWindow();
220         RS_DEBUG->print("main: setting caption");
221     appWin->setWindowTitle("Architektonas");
222         RS_DEBUG->print("main: show main window");
223     appWin->show();
224         RS_DEBUG->print("main: set focus");
225         appWin->setFocus();
226         RS_DEBUG->print("main: creating main window: OK");
227
228 #ifdef QSPLASHSCREEN_H
229         if (splash)
230         {
231                 RS_DEBUG->print("main: updating splash..");
232 //              splash->message(QObject::tr("Loading Library Paths.."),
233                 splash->showMessage(QObject::tr("Loading Library Paths.."),
234                         Qt::AlignRight | Qt::AlignBottom, QC_SPLASH_TXTCOL);
235                 RS_DEBUG->print("main: processing events");
236                 qApp->processEvents();
237                 RS_DEBUG->print("main: updating splash: OK");
238         }
239 #endif
240
241         // loading files:
242         RS_DEBUG->print("main: loading files..");
243         bool files_loaded = false;
244
245         for(QStringList::Iterator it=fileList.begin(); it!=fileList.end(); ++it)
246         {
247 #ifdef QSPLASHSCREEN_H
248                 if (splash)
249                 {
250 //                      splash->message(QObject::tr("Loading File %1..").arg(QDir::convertSeparators(*it)),
251                         splash->showMessage(QObject::tr("Loading File %1..").arg(QDir::convertSeparators(*it)),
252                                 Qt::AlignRight|Qt::AlignBottom, QC_SPLASH_TXTCOL);
253                         qApp->processEvents();
254                 }
255 #endif
256                         appWin->slotFileOpen(*it, RS2::FormatUnknown);
257                         files_loaded = true;
258         }
259
260         RS_DEBUG->print("main: loading files: OK");
261
262 //none of this demo shit
263 //#ifdef RS_DEMO
264 //      QTimer::singleShot(10 * 60 * 1000, appWin, SLOT(slotFileDemoQuit()));
265 //#endif
266
267 #ifdef QSPLASHSCREEN_H
268 # ifndef QC_DELAYED_SPLASH_SCREEN
269         if (splash)
270         {
271                 splash->finish(appWin);
272                 delete splash;
273                 splash = 0;
274         }
275 # endif
276         delete pixmap;
277 #endif
278
279     //app.connect(&app, SIGNAL(lastWindowClosed()), &app, SLOT(quit()));
280
281         // renew: avoid . / , confusion on German environments
282         //setlocale(LC_NUMERIC, "C");
283         setlocale(LC_ALL, "C");
284
285         RS_DEBUG->print("main: app.exec()");
286
287         if (!files_loaded)
288                 appWin->slotFileNew();
289
290         appWin->slotRunStartScript();
291
292         int r = app.exec();
293
294         delete appWin;
295
296         RS_DEBUG->print("main: finished");
297
298         return r;
299 }
300
301 /**
302  * Handles command line arguments that might not require a GUI.
303  *
304  * @return list of files to load on startup.
305  */
306 QStringList handleArgs(int argc, char * argv[])
307 {
308         RS_DEBUG->print("main: handling args..");
309         QStringList ret;
310
311         bool doexit = false;
312         QString machine;
313         QString input;
314         QString output;
315
316         for(int i=1; i<argc; i++)
317         {
318                 if (QString(argv[i]).startsWith("-") == false)
319                 {
320 //                      QString fname = QDir::convertSeparators(QFileInfo(QFile::decodeName(argv[i])).absFilePath());
321                         QString fname = QDir::convertSeparators(QFileInfo(QFile::decodeName(argv[i])).absoluteFilePath());
322                         ret.append(fname);
323                 }
324                 else if (QString(argv[i]) == "--exit")
325                 {
326                         doexit = true;
327                 }
328 #ifdef RS_CAM
329                 else if (QString(argv[i]) == "--convert")
330                 {
331                         ++i;
332
333                         if (i < argc)
334                                 machine = QString(argv[i]);
335                         else
336                         {
337                                 RS_DEBUG->print(RS_Debug::D_WARNING, "No machine configuration given after --convert. Aborting..");
338                                 exit(1);
339                         }
340
341                         ++i;
342
343                         if (i < argc)
344                                 input = QString(argv[i]);
345                         else
346                         {
347                                 RS_DEBUG->print(RS_Debug::D_WARNING, "No input given after --convert. Aborting..");
348                                 exit(1);
349                         }
350
351                         ++i;
352
353                         if (i < argc)
354                                 output = QString(argv[i]);
355                         else
356                         {
357                                 RS_DEBUG->print(RS_Debug::D_WARNING, "No output given after --convert. Aborting..");
358                                 exit(1);
359                         }
360                 }
361 #endif
362         }
363
364 #ifdef RS_CAM
365         // auto cam convert
366         if (machine.isEmpty() == false && input.isEmpty() == false && output.isEmpty() == false)
367         {
368                 RS_FilterCAM fc;
369                 RS_Graphic gr;
370                 RS_FILEIO->fileImport(gr, input, RS2::FormatUnknown);
371                 RS_CamDialog dlg(gr, NULL);
372                 dlg.activateMachineGenerator(machine);
373                 dlg.externalOK();
374                 fc.sort(gr);
375                 fc.fileExport(gr, output, RS2::FormatCAM);
376         }
377 #endif
378
379         if (doexit)
380                 exit(0);
381
382         RS_DEBUG->print("main: handling args: OK");
383         return ret;
384 }