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