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