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