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