]> Shamusworld >> Repos - architektonas/blob - src/mainapp/main.cpp
Removed more QC_ madness...
[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 "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         QPixmap * pixmap = new QPixmap(":/res/splash.png");
150 //# endif
151
152 /*
153         splash = new QSplashScreen(*pixmap);
154         splash->show();
155         splash->message(app.tr("Loading.."),
156                 Qt::AlignRight|Qt::AlignBottom, QC_SPLASH_TXTCOL);
157         RS_DEBUG->print("main: splashscreen: OK");
158 */
159 #endif
160
161         RS_DEBUG->print("main: init fontlist..");
162     RS_FONTLIST->init();
163         RS_DEBUG->print("main: init fontlist: OK");
164
165         RS_DEBUG->print("main: init patternlist..");
166     RS_PATTERNLIST->init();
167         RS_DEBUG->print("main: init patternlist: OK");
168
169         RS_DEBUG->print("main: init scriptlist..");
170     RS_SCRIPTLIST->init();
171         RS_DEBUG->print("main: init scriptlist: OK");
172
173         RS_DEBUG->print("main: loading translation..");
174         settings.beginGroup("Appearance");
175 #ifdef QC_PREDEFINED_LOCALE
176         lang = settings.value("Language", "").toString();
177
178         if (lang.isEmpty())
179         {
180                 lang = QC_PREDEFINED_LOCALE;
181                 settings.setValue("Language", lang);
182         }
183
184         langCmd = settings.value("LanguageCmd", "").toString();
185
186         if (langCmd.isEmpty())
187         {
188                 langCmd = QC_PREDEFINED_LOCALE;
189                 settings.setValue("LanguageCmd", langCmd);
190         }
191 #else
192     lang = settings.value("Language", "en").toString();
193     langCmd = settings.value("LanguageCmd", "en").toString();
194 #endif
195         settings.endGroup();
196
197         RS_SYSTEM->loadTranslation(lang, langCmd);
198         RS_DEBUG->print("main: loading translation: OK");
199
200 #ifdef QSPLASHSCREEN_H
201         splash = new QSplashScreen(*pixmap);
202         splash->show();
203 //      splash->message(QObject::tr("Loading.."), Qt::AlignRight | Qt::AlignBottom, QC_SPLASH_TXTCOL);
204         splash->showMessage(QObject::tr("Loading..."), Qt::AlignRight | Qt::AlignBottom, QC_SPLASH_TXTCOL);
205         RS_DEBUG->print("main: splashscreen: OK");
206 #endif
207
208         //QApplication::setStyle(new QWindowsStyle());
209         //QApplication::setStyle(new QPlatinumStyle());
210 #ifdef __APPLE__
211         QApplication::setStyle(new QMacStyle());
212 #endif
213
214 #ifdef QC_BUILTIN_STYLE //js:
215         RS_DEBUG->print("main: applying built in style..");
216         applyBuiltinStyle();
217 #endif
218
219         RS_DEBUG->print("main: creating main window..");
220     ApplicationWindow * appWin = new ApplicationWindow();
221         RS_DEBUG->print("main: setting caption");
222     appWin->setWindowTitle("Architektonas");
223         RS_DEBUG->print("main: show main window");
224     appWin->show();
225         RS_DEBUG->print("main: set focus");
226         appWin->setFocus();
227         RS_DEBUG->print("main: creating main window: OK");
228
229 #ifdef QSPLASHSCREEN_H
230         if (splash)
231         {
232                 RS_DEBUG->print("main: updating splash..");
233 //              splash->message(QObject::tr("Loading Library Paths.."),
234                 splash->showMessage(QObject::tr("Loading Library Paths.."),
235                         Qt::AlignRight | Qt::AlignBottom, QC_SPLASH_TXTCOL);
236                 RS_DEBUG->print("main: processing events");
237                 qApp->processEvents();
238                 RS_DEBUG->print("main: updating splash: OK");
239         }
240 #endif
241
242         // loading files:
243         RS_DEBUG->print("main: loading files..");
244         bool files_loaded = false;
245
246         for(QStringList::Iterator it=fileList.begin(); it!=fileList.end(); ++it)
247         {
248 #ifdef QSPLASHSCREEN_H
249                 if (splash)
250                 {
251 //                      splash->message(QObject::tr("Loading File %1..").arg(QDir::convertSeparators(*it)),
252                         splash->showMessage(QObject::tr("Loading File %1..").arg(QDir::convertSeparators(*it)),
253                                 Qt::AlignRight|Qt::AlignBottom, QC_SPLASH_TXTCOL);
254                         qApp->processEvents();
255                 }
256 #endif
257                         appWin->slotFileOpen(*it, RS2::FormatUnknown);
258                         files_loaded = true;
259         }
260
261         RS_DEBUG->print("main: loading files: OK");
262
263 //none of this demo shit
264 //#ifdef RS_DEMO
265 //      QTimer::singleShot(10 * 60 * 1000, appWin, SLOT(slotFileDemoQuit()));
266 //#endif
267
268 #ifdef QSPLASHSCREEN_H
269 # ifndef QC_DELAYED_SPLASH_SCREEN
270         if (splash)
271         {
272                 splash->finish(appWin);
273                 delete splash;
274                 splash = 0;
275         }
276 # endif
277         delete pixmap;
278 #endif
279
280     //app.connect(&app, SIGNAL(lastWindowClosed()), &app, SLOT(quit()));
281
282         // renew: avoid . / , confusion on German environments
283         //setlocale(LC_NUMERIC, "C");
284         setlocale(LC_ALL, "C");
285
286         RS_DEBUG->print("main: app.exec()");
287
288         if (!files_loaded)
289                 appWin->slotFileNew();
290
291         appWin->slotRunStartScript();
292
293         int r = app.exec();
294
295         delete appWin;
296
297         RS_DEBUG->print("main: finished");
298
299         return r;
300 }
301
302 /**
303  * Handles command line arguments that might not require a GUI.
304  *
305  * @return list of files to load on startup.
306  */
307 QStringList handleArgs(int argc, char * argv[])
308 {
309         RS_DEBUG->print("main: handling args..");
310         QStringList ret;
311
312         bool doexit = false;
313         QString machine;
314         QString input;
315         QString output;
316
317         for(int i=1; i<argc; i++)
318         {
319                 if (QString(argv[i]).startsWith("-") == false)
320                 {
321 //                      QString fname = QDir::convertSeparators(QFileInfo(QFile::decodeName(argv[i])).absFilePath());
322                         QString fname = QDir::convertSeparators(QFileInfo(QFile::decodeName(argv[i])).absoluteFilePath());
323                         ret.append(fname);
324                 }
325                 else if (QString(argv[i]) == "--exit")
326                 {
327                         doexit = true;
328                 }
329 #ifdef RS_CAM
330                 else if (QString(argv[i]) == "--convert")
331                 {
332                         ++i;
333
334                         if (i < argc)
335                                 machine = QString(argv[i]);
336                         else
337                         {
338                                 RS_DEBUG->print(RS_Debug::D_WARNING, "No machine configuration given after --convert. Aborting..");
339                                 exit(1);
340                         }
341
342                         ++i;
343
344                         if (i < argc)
345                                 input = QString(argv[i]);
346                         else
347                         {
348                                 RS_DEBUG->print(RS_Debug::D_WARNING, "No input given after --convert. Aborting..");
349                                 exit(1);
350                         }
351
352                         ++i;
353
354                         if (i < argc)
355                                 output = QString(argv[i]);
356                         else
357                         {
358                                 RS_DEBUG->print(RS_Debug::D_WARNING, "No output given after --convert. Aborting..");
359                                 exit(1);
360                         }
361                 }
362 #endif
363         }
364
365 #ifdef RS_CAM
366         // auto cam convert
367         if (machine.isEmpty() == false && input.isEmpty() == false && output.isEmpty() == false)
368         {
369                 RS_FilterCAM fc;
370                 Drawing gr;
371                 RS_FILEIO->fileImport(gr, input, RS2::FormatUnknown);
372                 RS_CamDialog dlg(gr, NULL);
373                 dlg.activateMachineGenerator(machine);
374                 dlg.externalOK();
375                 fc.sort(gr);
376                 fc.fileExport(gr, output, RS2::FormatCAM);
377         }
378 #endif
379
380         if (doexit)
381                 exit(0);
382
383         RS_DEBUG->print("main: handling args: OK");
384         return ret;
385 }