main.cpp
author insilmaril
Tue, 23 Mar 2010 11:54:30 +0000
branchrelease-1-12-maintained
changeset 81 876eed30ba3b
parent 79 32f499cbe874
permissions -rw-r--r--
Patch from Xavier Oswald to compile with older compilers
     1 #include <QApplication>
     2 #include <cstdlib>
     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 	iconPath=vymBaseDir.path()+"/icons/";
   142 	flagsPath=vymBaseDir.path()+"/flags/";
   143 
   144 	// Some directories
   145 	lastImageDir=QDir().current();
   146 	lastFileDir=QDir().current();
   147 
   148 	if (options.isOn ("help"))
   149 	{
   150 		cout << qPrintable (options.getHelpText())<<endl;
   151 		return 0;	
   152 	}	
   153 
   154 	// Initialize translations
   155 	QTranslator translator (0);
   156 	translator.load( QString("vym_")+QTextCodec::locale(), vymBaseDir.path() + "/lang");
   157     app.installTranslator( &translator );
   158 
   159 	// Initializing the row of system flags
   160 	// is done in first call to MapEditor(),
   161 	// because we need at least one canvas first
   162 	systemFlagsDefault=NULL;
   163 	standardFlagsDefault=NULL;
   164 
   165 	// Initialize window of TextEditor
   166 	textEditor = new TextEditor();
   167 	textEditor->setIcon (QPixmap (iconPath+"vym-editor.png"));
   168 
   169 	// Initialize mainwindow 
   170 #if defined(Q_OS_WIN32)
   171     Main m(0, 0, (Qt::Window | Qt::MSWindowsOwnDC));
   172 #else
   173     Main m;
   174 #endif
   175 
   176 	//m.resize(m.sizeHint());
   177 	m.setIcon (QPixmap (iconPath+"vym.png"));
   178 	m.show();
   179 	m.fileNew();
   180 	m.loadCmdLine();
   181 
   182 	// Run script
   183 	if (options.isOn ("run"))
   184 	{
   185 		QString script;
   186 		QString fn=options.getArg ("run");
   187 		if ( !fn.isEmpty() )
   188 		{
   189 			QFile f( fn );
   190 			if ( !f.open( QIODevice::ReadOnly ) )
   191 			{
   192 				QMessageBox::warning(0, 
   193 					QObject::tr("Error"),
   194 					QObject::tr("Couldn't open %1.\n").arg(fn));
   195 				return 0;
   196 			}	
   197 
   198 			QTextStream ts( &f );
   199 			script= ts.read();
   200 			f.close();
   201 			m.setScript (script);
   202 			m.runScriptEverywhere (script);
   203 		}
   204 	}		
   205 	
   206 	// For benchmarking we may want to quit instead of entering event loop
   207 	if (options.isOn ("quit"))
   208 	{
   209 		return 0;
   210 	}	
   211 
   212     QObject::connect( &app, SIGNAL(lastWindowClosed()), &app, SLOT(quit()) );
   213 
   214     return app.exec();
   215 }