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