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