initial import
[vym] / main.cpp
1 #include <QApplication>
2 #include <QtGlobal>
3
4 #include "flagrowobj.h"
5 #include "mainwindow.h"
6 #include "options.h"
7 #include "settings.h"
8 #include "version.h"
9
10 #if defined(Q_OS_WIN32)
11 #define WIN32_LEAN_AND_MEAN
12 #include <windows.h>
13 #endif
14
15 // Global variables
16 TextEditor *textEditor;                 // used in Constr. of LinkableMapObj
17                                                                 // initialized in mainwindow
18 QString vymName;
19 QString vymVersion;
20 QString vymBuildDate;
21 QString vymCodeName;
22
23 Main *mainWindow;                               // used in BranchObj::select()                                                          
24 QString tmpVymDir;                              // All temp files go there, created in mainwindow
25 QString clipboardDir;                   // Clipboard used in all mapEditors
26 QString clipboardFile;                  // Clipboard used in all mapEditors
27 QDir vymBaseDir;                                // Containing all styles, scripts, images, ...
28 QDir lastImageDir;
29 QDir lastFileDir;
30 #if defined(Q_OS_WIN32)
31 QDir vymInstallDir;
32 #endif
33 QString iconPath;                               // Pointing to icons used for toolbars
34 QString flagsPath;                              // Pointing to flags
35 bool clipboardEmpty;                    
36 bool debug;                                             // global debugging flag
37 FlagRowObj *systemFlagsDefault; // used to copy from in LinkableMapObj
38 FlagRowObj *standardFlagsDefault;
39
40
41 Settings settings ("InSilmaril","vym"); // Organization, Application name
42
43 Options options;
44 ImageIO imageIO;
45
46 int statusbarTime=3500;
47
48 int main(int argc, char* argv[])
49 {
50         //Q_INIT_RESOURCE (application);
51
52     QApplication app(argc,argv);
53
54         vymName=__VYM_NAME;
55         vymVersion=__VYM_VERSION;
56         vymBuildDate=__VYM_BUILD_DATE;
57         vymCodeName=__VYM_CODENAME;
58
59
60         // Reading and initializing options commandline options
61         options.add ("debug", Option::Switch, "d", "debug");
62         options.add ("version", Option::Switch, "v","version");
63         options.add ("local", Option::Switch, "l", "local");
64         options.add ("help", Option::Switch, "h", "help");
65         options.add ("quit", Option::Switch, "q", "quit");
66         options.add ("run", Option::String, "r", "run");
67         options.add ("test", Option::String, "t", "test");
68         options.setHelpText (
69                 "VYM - View Your Mind\n"
70                 "--------------------\n\n"
71                 "Information about vym can be found in vym.pdf,\n"
72                 "which should be part of the vym package.\n"
73                 "It is also available at the project homepage:\n\n"
74                 "http://www.InSilmaril.de/vym\n");
75         if (options.parse())
76         {
77                 cout << endl << qPrintable( options.getHelpText())<<endl;
78                 return 1;
79         }
80
81         debug=options.isOn ("debug");
82
83         if (options.isOn ("version"))
84         {
85                 cout << "VYM - View Your Mind (c) 2004-2007 Uwe Drechsel "  << endl
86                         <<"   Version: "<<__VYM_VERSION <<endl
87                         <<"Build date: "<<__VYM_BUILD_DATE << endl
88                         <<"  "<<__VYM_CODENAME<<endl;
89                         
90                 return 0;       
91         }               
92         
93         // Use /usr/share/vym or /usr/local/share/vym or . ?
94         // First try options
95         if (options.isOn ("local"))
96         {
97                 vymBaseDir.setPath (vymBaseDir.currentDirPath());
98         } else
99         // then look for environment variable
100         if (getenv("VYMHOME")!=0)
101         {
102                 vymBaseDir.setPath (getenv("VYMHOME"));
103         } else
104         // ok, let's find my way on my own
105         {
106                 #if defined (Q_OS_MACX)
107                         vymBaseDir.setPath(vymBaseDir.currentDirPath() +"/vym.app/Contents/Resources");
108
109         #elif defined (Q_OS_WIN32)
110             QString basePath;
111
112             wchar_t wbuf[512];
113             if (GetModuleFileName(NULL, wbuf, 512))
114             {
115                 QString mfn(QString::fromWCharArray(wbuf));
116                 mfn.replace('\\', '/');
117                 if (mfn.endsWith("/bin/vym.exe", Qt::CaseInsensitive))
118                 {
119                     mfn.chop(12);
120                     basePath = mfn;
121                 }
122             }
123
124             if (basePath.isEmpty())
125                 basePath = vymBaseDir.currentDirPath();
126
127             vymInstallDir.setPath(basePath);
128             vymBaseDir.setPath(basePath + "/share/vym");
129
130                 #else
131                         vymBaseDir.setPath ("/usr/share/vym");
132                         if (!vymBaseDir.exists())
133                         {
134                                 vymBaseDir.setPath ("/usr/local/share/vym");
135                                 if (!vymBaseDir.exists())
136                                         vymBaseDir.setPath(vymBaseDir.currentDirPath() );
137                         }               
138                 #endif
139         }
140
141 #ifdef Q_OS_WIN32
142         iconPath=vymBaseDir.path()+"\\icons\\";
143         flagsPath=vymBaseDir.path()+"\\flags\\";
144 #else
145         iconPath=vymBaseDir.path()+"/icons/";
146         flagsPath=vymBaseDir.path()+"/flags/";
147 #endif
148         // Some directories
149         lastImageDir=QDir().current();
150         lastFileDir=QDir().current();
151
152         if (options.isOn ("help"))
153         {
154                 cout << qPrintable (options.getHelpText())<<endl;
155                 return 0;       
156         }       
157
158         // Initialize translations
159         QTranslator translator (0);
160         translator.load( QString("vym_")+QTextCodec::locale(), vymBaseDir.path() + "/lang");
161     app.installTranslator( &translator );
162
163         // Initializing the row of system flags
164         // is done in first call to MapEditor(),
165         // because we need at least one canvas first
166         systemFlagsDefault=NULL;
167         standardFlagsDefault=NULL;
168
169         // Initialize window of TextEditor
170         textEditor = new TextEditor();
171         textEditor->setIcon (QPixmap (iconPath+"vym-editor.png"));
172
173         // Initialize mainwindow 
174 #if defined(Q_OS_WIN32)
175     Main m(0, 0, (Qt::Window | Qt::MSWindowsOwnDC));
176 #else
177     Main m;
178 #endif
179
180         //m.resize(m.sizeHint());
181         m.setIcon (QPixmap (iconPath+"vym-48x48.png"));
182         m.show();
183         m.fileNew();
184         m.loadCmdLine();
185
186         // Run script
187         if (options.isOn ("run"))
188         {
189                 QString script;
190                 QString fn=options.getArg ("run");
191                 if ( !fn.isEmpty() )
192                 {
193                         QFile f( fn );
194                         if ( !f.open( QIODevice::ReadOnly ) )
195                         {
196                                 QMessageBox::warning(0, 
197                                         QObject::tr("Error"),
198                                         QObject::tr("Couldn't open %1.\n").arg(fn));
199                                 return 0;
200                         }       
201
202                         QTextStream ts( &f );
203                         script= ts.read();
204                         f.close();
205                         m.setScript (script);
206                         m.runScriptEverywhere (script);
207                 }
208         }               
209         
210         // For benchmarking we may want to quit instead of entering event loop
211         if (options.isOn ("quit"))
212         {
213                 return 0;
214         }       
215
216     QObject::connect( &app, SIGNAL(lastWindowClosed()), &app, SLOT(quit()) );
217
218     return app.exec();
219 }