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