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