mainwindow.cpp
author insilmaril
Thu Aug 07 10:56:52 2008 +0000 (2008-08-07)
branchrelease-1-12-maintained
changeset 47 326e3336e9b0
child 2 608f976aa7bb
child 59 1c550f80c43b
child 89 9db3eaa21237
permissions -rw-r--r--
Changed speed of animation a bit
     1 #include "mainwindow.h"
     2 
     3 #include <qstatusbar.h>
     4 #include <qmessagebox.h>
     5 #include <qmenubar.h>
     6 #include <qapplication.h>
     7 #include <qpainter.h>
     8 #include <qprinter.h>
     9 #include <qfile.h>
    10 #include <qfiledialog.h>
    11 #include <qcolor.h>
    12 #include <qcolordialog.h>
    13 #include <qbitmap.h>
    14 #include <qinputdialog.h>
    15 
    16 
    17 #include <iostream>
    18 #include <stdlib.h>
    19 #include <typeinfo>
    20 
    21 #include "version.h"
    22 
    23 #include "icons/filenew.xpm"
    24 #include "icons/fileopen.xpm"
    25 #include "icons/filesave.xpm"
    26 #include "icons/fileprint.xpm"
    27 #include "icons/editundo.xpm"
    28 //#include "icons/editredo.xpm"	// TODO
    29 #include "icons/editcopy.xpm"
    30 #include "icons/editcut.xpm"
    31 #include "icons/editpaste.xpm"
    32 #include "icons/editmoveup.xpm"
    33 #include "icons/editmovedown.xpm"
    34 #include "icons/formatcoloritem.xpm"
    35 #include "icons/formatcolorbranch.xpm"
    36 #include "icons/formatcolorpicker.xpm"
    37 #include "icons/viewzoomreset.xpm"
    38 #include "icons/viewzoomin.xpm"
    39 #include "icons/viewzoomout.xpm"
    40 #include "icons/vym-48x48.xpm"
    41 #include "icons/flag-note.xpm"
    42 #include "icons/flag-url.xpm"
    43 #include "icons/flag-vymlink.xpm"	
    44 #include "icons/flag-scrolled-right.xpm"
    45 
    46 #include "flagrowobj.h"
    47 #include "texteditor.h"
    48 #include "mapeditor.h"
    49 #include "exporthtmldialog.h"
    50 #include "exportxhtmldialog.h"
    51 #include "showtextdialog.h"
    52 #include "process.h"
    53 #include "settings.h"
    54 #include "options.h"
    55 
    56 extern TextEditor *textEditor;
    57 extern Main *mainWindow;
    58 extern int statusbarTime;
    59 extern MapEditor *clipboardME;
    60 extern FlagRowObj* standardFlagsDefault;
    61 
    62 extern QAction* actionFileSave;
    63 extern QAction* actionFilePrint;
    64 extern QAction* actionEditUndo;
    65 extern QAction *actionEditCopy;
    66 extern QAction *actionEditCut;
    67 extern QAction *actionEditPaste;
    68 extern QAction *actionEditMoveUp;
    69 extern QAction *actionEditMoveDown;
    70 extern QAction *actionEditToggleScroll;
    71 extern QAction* actionEditOpenURL;
    72 extern QAction* actionEditURL;
    73 extern QAction* actionEditHeading2URL;
    74 extern QAction* actionEditBugzilla2URL;
    75 extern QAction *actionEditOpenVymLink;
    76 extern QAction *actionEditVymLink;
    77 extern QAction *actionEditDeleteVymLink;
    78 extern QAction *actionEditMapInfo;
    79 extern QAction *actionEditHeading;
    80 extern QAction *actionEditDelete;
    81 extern QAction *actionEditAddBranch;
    82 extern QAction *actionEditAddBranchAbove;
    83 extern QAction *actionEditAddBranchBelow;
    84 extern QAction *actionEditImportAdd;
    85 extern QAction *actionEditImportReplace;
    86 extern QAction *actionEditSaveBranch;
    87 extern QAction *actionEditSelectFirst;
    88 extern QAction *actionEditSelectLast;
    89 extern QAction *actionEditLoadImage;
    90 extern QAction *actionEditToggleFloatExport;
    91 
    92 extern QAction* actionFormatColor;
    93 extern QAction* actionFormatPickColor;
    94 extern QAction* actionFormatColorBranch;
    95 extern QAction* actionFormatColorSubtree;
    96 extern QAction* actionFormatLinkColorHint;
    97 extern QAction* actionFormatBackColor;
    98 extern QAction* actionFormatLinkColor;
    99 
   100 extern QActionGroup *actionGroupFormatFrameTypes;
   101 extern QAction *actionFormatFrameNone;
   102 extern QAction *actionFormatFrameRectangle;
   103 
   104 extern QActionGroup *actionGroupFormatLinkStyles;
   105 extern QAction *actionFormatLinkStyleLine;
   106 extern QAction *actionFormatLinkStyleParabel;
   107 extern QAction *actionFormatLinkStylePolyLine;
   108 extern QAction *actionFormatLinkStylePolyParabel;
   109 
   110 extern QAction *actionViewToggleNoteEditor;
   111 
   112 extern QAction* actionSettingsAutoedit;
   113 extern QAction* actionSettingsAutoselectHeading;
   114 extern QAction* actionSettingsAutoselectHeading;
   115 extern QAction* actionSettingsAutoselectText;
   116 extern QAction* actionSettingsPasteNewHeading;
   117 extern QAction* actionSettingsUseDelKey;
   118 
   119 extern QPopupMenu* branchContextMenu;
   120 extern QPopupMenu* floatimageContextMenu;
   121 extern QPopupMenu* saveImageFormatMenu;
   122 extern QPopupMenu* canvasContextMenu;
   123 extern QPopupMenu* lastMapsMenu;
   124 extern QPopupMenu* exportMenu;
   125 extern QPopupMenu* exportImageFormatMenu;
   126 
   127 
   128 extern Settings settings;
   129 extern Options options;
   130 
   131 #if defined(Q_OS_LINUX)
   132 extern void qt_wait_for_window_manager( QWidget* w );
   133 #endif
   134 
   135 Main::Main(QWidget* parent, const char* name, WFlags f) :
   136     QMainWindow(parent,name,f)
   137 {
   138 	mainWindow=this;
   139 
   140 	setCaption ("VYM - View Your Mind");
   141 
   142 	// Load window settings
   143 	resize (settings.readNumEntry( "/vym/mainwindow/geometry/width", 800),
   144 	        settings.readNumEntry( "/vym/mainwindow/geometry/height",600));
   145 	move   (settings.readNumEntry( "/vym/mainwindow/geometry/posX", 100),
   146 	        settings.readNumEntry( "/vym/mainwindow/geometry/posY", 100));
   147 
   148 
   149 	// Initialize some settings, which are platform dependant
   150 	QString p,s;
   151 
   152 		// application to open URLs
   153 		p="/vym/mainwindow/readerURL";
   154 		#if defined(Q_OS_LINUX)
   155 			s=settings.readEntry (p,"konqueror");
   156 		#else
   157 			#if defined(Q_OS_MACX)
   158 				s=settings.readEntry (p,"/Applications/Safari.app/Contents/MacOS/Safari");
   159 			#else
   160 				s=settings.readEntry (p,"mozilla");
   161 			#endif
   162 		#endif
   163 		settings.writeEntry( p,s);
   164 
   165 		// application to open PDFs
   166 		p="/vym/mainwindow/readerPDF";
   167 		#if defined(Q_OS_LINUX)
   168 			s=settings.readEntry (p,"acroread");
   169 		#else
   170 			#if defined(Q_OS_MACX)
   171 				s=settings.readEntry (p,"/Applications/Safari.app/Contents/MacOS/Safari");
   172 			#else
   173 				s=settings.readEntry (p,"acroread");
   174 			#endif
   175 		#endif
   176 		settings.writeEntry( p,s);
   177 
   178 	
   179 	maxLastMaps=9;
   180 
   181 	// Create tab widget which holds the maps
   182 	tabWidget= new QTabWidget (this);
   183 	connect( tabWidget, SIGNAL( currentChanged( QWidget * ) ), 
   184 		this, SLOT( editorChanged( QWidget * ) ) );
   185 
   186 	setCentralWidget(tabWidget);	
   187 
   188     setupFileActions();
   189     setupEditActions();
   190     setupFormatActions();
   191     setupViewActions();
   192 	setupFlagActions();
   193     setupSettingsActions();
   194 	setupContextMenus();
   195     if (settings.readBoolEntry( "/vym/mainwindow/showTestMenu",false)) setupTestActions();
   196     setupHelpActions();
   197     
   198 	// After menu is created, we can enable some actions
   199 	actionFilePrint->setEnabled (true);
   200 
   201     statusBar();
   202 
   203 	// Create the default map into first tab
   204 //	fileNew();
   205 //	tabWidget->addTab (new MapEditor(tabWidget,true), "unnamed");
   206 //	currentMapEditor()->show();
   207 
   208 	// Initialize Find window
   209 	findWindow=new FindWindow(NULL,"findwindow");
   210 	connect (findWindow, SIGNAL( findButton(QString) ), 
   211 		this, SLOT(editFind(QString) ) );	
   212 	connect (findWindow, SIGNAL( somethingChanged() ), 
   213 		this, SLOT(editFindChanged() ) );	
   214 
   215 	updateGeometry();
   216 }
   217 
   218 Main::~Main()
   219 {
   220 	// Save Settings
   221 	settings.writeEntry( "/vym/mainwindow/geometry/width", width() );
   222 	settings.writeEntry( "/vym/mainwindow/geometry/height", height() );
   223 	settings.writeEntry( "/vym/mainwindow/geometry/posX", pos().x() );
   224 	settings.writeEntry( "/vym/mainwindow/geometry/posY", pos().y() );
   225 
   226 	settings.writeEntry( "/vym/version/version", __VYM_VERSION__ );
   227 	settings.writeEntry( "/vym/version/builddate", __BUILD_DATE__ );
   228 
   229 	settings.writeEntry( "/vym/mapeditor/editmode/autoselectheading",actionSettingsAutoselectHeading->isOn() );
   230 	settings.writeEntry( "/vym/mapeditor/editmode/autoselecttext",actionSettingsAutoselectText->isOn() );
   231 	settings.writeEntry( "/vym/mapeditor/editmode/pastenewheading",actionSettingsPasteNewHeading->isOn() );
   232 	settings.writeEntry( "/vym/mapeditor/editmode/autoedit",actionSettingsAutoedit->isOn() );
   233 	settings.writeEntry( "/vym/mapeditor/editmode/useDelKey",actionSettingsUseDelKey->isOn() );
   234 
   235 	QString s;
   236 	int maps=lastMaps.count();
   237 	settings.writeEntry( "/vym/lastMaps/number",maps );
   238 	for (int i=1;i<=maps;i++)
   239 	{
   240 		QStringList::Iterator it = lastMaps.at(i-1);
   241 		s=QString("/vym/lastMaps/map-%1").arg(i);
   242 		if (!s.isEmpty() && i<=maxLastMaps) 
   243 			settings.writeEntry (s, *it);
   244 	}
   245 
   246 
   247 	// To make the texteditor save its settings, call the destructor
   248 	delete (textEditor);
   249 }
   250 
   251 void Main::loadCmdLine()
   252 {
   253 	/* TODO draw some kind of splashscreen while loading...
   254 	if (qApp->argc()>1)
   255 	{
   256 	}
   257 	*/
   258 	
   259 	QStringList flist=options.getFileList();
   260 	QStringList::Iterator it=flist.begin();
   261 
   262 	while (it !=flist.end() )
   263 	{
   264 		fileLoad (*it, NewMap);
   265 		*it++;
   266 	}	
   267 }
   268 
   269 
   270 void Main::statusMessage(const QString &s)
   271 {
   272 	statusBar()->message (s);
   273 }
   274 
   275 void Main::closeEvent (QCloseEvent* )
   276 {
   277 	fileExitVYM();
   278 }
   279 
   280 // File Actions
   281 void Main::setupFileActions()
   282 {
   283     QToolBar *tb = new QToolBar( this );
   284     tb->setLabel( "File Actions" );
   285     QPopupMenu *menu = new QPopupMenu( this );
   286     menuBar()->insertItem( tr( "&File" ), menu );
   287 
   288 	// Keycodes:  /usr/lib64/qt3/include/qnamespace.h
   289 
   290     QAction *a;
   291     a = new QAction( tr( "New map" ), QPixmap( filenew_xpm ), tr( "&New..." ), CTRL + Key_N, this, "fileNew" );
   292     connect( a, SIGNAL( activated() ), this, SLOT( fileNew() ) );
   293     a->addTo( tb );
   294     a->addTo( menu );
   295 	
   296     a = new QAction( tr( "Open" ), QPixmap( fileopen_xpm), tr( "&Open..." ), CTRL + Key_O, this, "fileOpen" );
   297     connect( a, SIGNAL( activated() ), this, SLOT( fileLoad() ) );
   298     a->addTo( tb );
   299     a->addTo( menu );
   300 	
   301 	lastMapsMenu = new QPopupMenu (this);
   302 
   303     menu->insertItem (tr("Open Recent"),lastMapsMenu );
   304     menu->insertSeparator();
   305 	
   306     a = new QAction( tr( "Save" ), QPixmap( filesave_xpm ), tr( "&Save..." ), CTRL + Key_S, this, "fileSave" );
   307     connect( a, SIGNAL( activated() ), this, SLOT( fileSave() ) );
   308     a->addTo( tb );
   309     a->addTo( menu );
   310 	actionFileSave=a;
   311 	
   312     a = new QAction( tr( "Save &As" ), QPixmap(), tr( "Save &As..." ), 0, this, "fileSaveAs" );
   313     connect( a, SIGNAL( activated() ), this, SLOT( fileSaveAs() ) );
   314     a->addTo( menu );
   315 
   316     menu->insertSeparator();
   317 
   318     a = new QAction( tr( "Import directory structure (experimental)" ), QPixmap(), tr( "Import Dir" ), 0, this, "export" );
   319     connect( a, SIGNAL( activated() ), this, SLOT( fileImportDir() ) );
   320     a->addTo( menu );
   321 
   322 	exportMenu = new QPopupMenu (this);
   323     menu->insertItem (tr("Export"),exportMenu );
   324 
   325     menu->insertSeparator();
   326 
   327 
   328     a = new QAction( tr( "Print" ), QPixmap( fileprint_xpm ), tr( "&Print..." ), CTRL + Key_P, this, "filePrint" );
   329     connect( a, SIGNAL( activated() ), this, SLOT( filePrint() ) );
   330     a->addTo( tb );
   331     a->addTo( menu );
   332 	actionFilePrint=a;
   333 
   334     a = new QAction( tr( "Close Map" ), QPixmap(), tr( "&Close Map" ), ALT + Key_C, this, "fileCloseMap" );
   335     connect( a, SIGNAL( activated() ), this, SLOT( fileCloseMap() ) );
   336     a->addTo( menu );
   337 
   338     a = new QAction( tr( "Exit VYM" ), QPixmap(), tr( "E&xit VYM" ), CTRL + Key_Q, this, "fileExitVYM" );
   339     connect( a, SIGNAL( activated() ), this, SLOT( fileExitVYM() ) );
   340     a->addTo( menu );
   341 }
   342 
   343 
   344 //Edit Actions
   345 void Main::setupEditActions()
   346 {
   347     QToolBar *tb = new QToolBar( this );
   348     tb->setLabel( "Edit Actions" );
   349     QPopupMenu *menu = new QPopupMenu( this );
   350     menuBar()->insertItem( tr( "&Edit" ), menu );
   351 
   352     QAction *a;
   353     a = new QAction( tr( "Undo" ), QPixmap( editundo_xpm ), tr( "&Undo" ), CTRL + Key_Z, this, "editUndo" );
   354     connect( a, SIGNAL( activated() ), this, SLOT( editUndo() ) );
   355 	a->setEnabled (false);
   356     a->addTo( tb );
   357     a->addTo( menu );
   358 	actionEditUndo=a;
   359     /*
   360     a = new QAction( tr( "Redo" ), QPixmap( editredo_xpm ), tr( "&Redo" ), CTRL + Key_Y, this, "editRedo" ); 
   361     connect( a, SIGNAL( activated() ), this, SLOT( editRedo() ) );
   362     a->addTo( tb );
   363     a->addTo( menu );
   364     */
   365     menu->insertSeparator();
   366     a = new QAction( tr( "Copy" ), QPixmap( editcopy_xpm ), tr( "&Copy" ), CTRL + Key_C, this, "editCopy" );
   367     connect( a, SIGNAL( activated() ), this, SLOT( editCopy() ) );
   368 	a->setEnabled (false);
   369     a->addTo( tb );
   370     a->addTo( menu );
   371 	actionEditCopy=a;
   372     a = new QAction( tr( "Cut" ), QPixmap( editcut_xpm ), tr( "Cu&t" ), CTRL + Key_X, this, "editCut" );
   373     connect( a, SIGNAL( activated() ), this, SLOT( editCut() ) );
   374 	a->setEnabled (false);
   375     a->addTo( tb );
   376     a->addTo( menu );
   377 	actionEditCut=a;
   378     a = new QAction( tr( "Paste" ), QPixmap( editpaste_xpm ), tr( "&Paste" ), CTRL + Key_V, this, "editPaste" );
   379     connect( a, SIGNAL( activated() ), this, SLOT( editPaste() ) );
   380 	a->setEnabled (false);
   381     a->addTo( tb );
   382     a->addTo( menu );
   383 	actionEditPaste=a;
   384     a = new QAction( tr( "Move branch up" ), QPixmap( editmoveup_xpm ), tr( "Move up" ), Key_PageUp, this, "editMoveUp" );
   385     connect( a, SIGNAL( activated() ), this, SLOT( editMoveUp() ) );
   386 	a->setEnabled (false);
   387     a->addTo( tb );
   388     a->addTo( menu );
   389 	actionEditMoveUp=a;
   390     a = new QAction( tr( "Move branch down" ), QPixmap( editmovedown_xpm ), tr( "Move down" ), Key_PageDown, this, "editMoveDown" );
   391     connect( a, SIGNAL( activated() ), this, SLOT( editMoveDown() ) );
   392 	a->setEnabled (false);
   393     a->addTo( tb );
   394     a->addTo( menu );
   395 	actionEditMoveDown=a;
   396 
   397     a = new QAction( tr( "Scroll branch" ), QPixmap(flag_scrolled_right_xpm), tr( "Scroll branch" ), Key_ScrollLock, this, "scroll" );
   398     connect( a, SIGNAL( activated() ), this, SLOT( editToggleScroll() ) );
   399 	a->setEnabled (false);
   400     a->addTo( tb );
   401     a->addTo( menu );
   402 	actionEditToggleScroll=a;
   403 	
   404     a = new QAction( tr( "Unscroll all" ), QPixmap(), tr( "Unscroll all scrolled branches" ), 0, this, "scroll" );
   405     connect( a, SIGNAL( activated() ), this, SLOT( editUnScrollAll() ) );
   406     a->addTo( menu );
   407 	
   408     menu->insertSeparator();
   409 
   410 	a = new QAction( tr( "Find" ), QPixmap(), tr( "Find" ), CTRL + Key_F, this, "find" );
   411     connect( a, SIGNAL( activated() ), this, SLOT( editOpenFindWindow() ) );
   412     a->addTo( menu );
   413     
   414 	menu->insertSeparator();
   415 
   416 	a = new QAction( tr( "Open URL" ), QPixmap(flag_url_xpm), tr( "Open URL" ), CTRL + Key_U, this, "url" );
   417     connect( a, SIGNAL( activated() ), this, SLOT( editOpenURL() ) );
   418     a->addTo( menu );
   419     a->addTo( tb );
   420 	a->setEnabled (false);
   421 	actionEditOpenURL=a;
   422 
   423 	a = new QAction( tr( "Edit URL" ), QPixmap(), tr( "Edit URL" ), SHIFT + CTRL + Key_U, this, "url" );
   424     connect( a, SIGNAL( activated() ), this, SLOT( editURL() ) );
   425     a->addTo( menu );
   426 	a->setEnabled (false);
   427 	actionEditURL=a;
   428 	
   429 	a = new QAction( tr( "Use heading of selected branch as URL" ), QPixmap(), tr( "Use heading for URL" ), 0, this, "heading2url" );
   430     connect( a, SIGNAL( activated() ), this, SLOT( editHeading2URL() ) );
   431     a->addTo( menu );
   432 	a->setEnabled (false);
   433 	actionEditHeading2URL=a;
   434     
   435 	a = new QAction( tr( "Create URL to Bugzilla" ), QPixmap(), tr( "Create URL to Bugzilla" ), 0, this, "bugzilla2url" );
   436     connect( a, SIGNAL( activated() ), this, SLOT( editBugzilla2URL() ) );
   437 	a->setEnabled (false);
   438 	actionEditBugzilla2URL=a;
   439     
   440 	menu->insertSeparator();
   441 	
   442     a = new QAction( tr( "Jump to another vym map, if needed load it first" ), QPixmap(flag_vymlink_xpm), tr( "Jump to map" ), 0, this, "jumpMap" );
   443     connect( a, SIGNAL( activated() ), this, SLOT( editOpenVymLink() ) );
   444     a->addTo( menu );
   445     a->addTo( tb );
   446 	a->setEnabled (false);
   447 	actionEditOpenVymLink=a;
   448 	
   449     a = new QAction( tr( "Edit link to another vym map" ), QPixmap(), tr( "Edit vym link" ), 0, this, "editLinkMap" );
   450     connect( a, SIGNAL( activated() ), this, SLOT( editVymLink() ) );
   451     a->addTo( menu );
   452 	a->setEnabled (false);
   453 	actionEditVymLink=a;
   454 
   455     a = new QAction( tr( "Delete link to another vym map" ), QPixmap(), tr( "Delete vym link" ), 0, this, "deleteLinkMap" );
   456     connect( a, SIGNAL( activated() ), this, SLOT( editDeleteVymLink() ) );
   457     a->addTo( menu );
   458 	a->setEnabled (false);
   459 	actionEditDeleteVymLink=a;
   460 
   461 	menu->insertSeparator();
   462 
   463     a = new QAction( tr( "Edit Map Info" ), QPixmap(), tr( "Edit Map Info" ), 0, this, "editMapInfo" );
   464     connect( a, SIGNAL( activated() ), this, SLOT( editMapInfo() ) );
   465     a->addTo( menu );
   466 	a->setEnabled (true);
   467 	actionEditMapInfo=a;
   468 
   469 	menu->insertSeparator();
   470 
   471     // Shortcuts to modify heading:
   472     a = new QAction( tr( "edit Heading" ),tr( "Edit heading" ), Key_Enter, this, "editHeading" );
   473     connect( a, SIGNAL( activated() ), this, SLOT( editHeading() ) );
   474 	a->setEnabled (false);
   475     a->addTo ( menu );
   476 	actionEditHeading=a;
   477     a = new QAction( tr( "edit Heading" ),tr( "Edit heading" ), Key_Return, this, "editHeading" );
   478     connect( a, SIGNAL( activated() ), this, SLOT( editHeading() ) );
   479     //a->addTo ( menu );
   480     
   481     // Shortcut to delete selection
   482     a = new QAction( tr( "Delete Selection" ),tr( "Delete Selection" ), Key_Delete, this, "deleteBranch" );
   483     connect( a, SIGNAL( activated() ), this, SLOT( editDeleteSelection() ) );
   484 	a->setEnabled (false);
   485     a->addTo ( menu );
   486 	actionEditDelete=a;
   487     
   488     // Shortcut to add branch
   489 	#if defined (Q_OS_MACX)
   490 		a = new QAction( tr( "Add a branch as child of selection" ),tr( "Add branch as child" ), CTRL + Key_I, this, "newBranch" );
   491 	#else
   492 		a = new QAction( tr( "Add a branch as child of selection" ),tr( "Add branch as child" ), Key_Insert, this, "newBranch" );
   493 	#endif
   494     connect( a, SIGNAL( activated() ), this, SLOT( editNewBranch() ) );
   495 	a->setEnabled (false);
   496     a->addTo ( menu );
   497 	actionEditAddBranch=a;
   498 
   499 	// Add branch above
   500     a = new QAction( tr( "Add a branch above selection" ),tr( "Add branch above" ), SHIFT+Key_Insert, this, "newBranch" );
   501     connect( a, SIGNAL( activated() ), this, SLOT( editNewBranchAbove() ) );
   502 	a->setEnabled (false);
   503     a->addTo ( menu );
   504 	actionEditAddBranchAbove=a;
   505 
   506 	// Add branch below 
   507     a = new QAction( tr( "Add a branch below selection" ),tr( "Add branch below" ), CTRL +Key_Insert, this, "newBranch" );
   508     connect( a, SIGNAL( activated() ), this, SLOT( editNewBranchBelow() ) );
   509 	a->setEnabled (false);
   510     a->addTo ( menu );
   511 	actionEditAddBranchBelow=a;
   512 
   513 	// Import at selection (adding to selection)
   514     a = new QAction( tr( "Add map at selection" ),tr( "Import (add)" ), 0, this, "importAdd" );
   515     connect( a, SIGNAL( activated() ), this, SLOT( editImportAdd() ) );
   516 	a->setEnabled (false);
   517     a->addTo ( menu );
   518 	actionEditImportAdd=a;
   519 
   520 	// Import at selection (replacing selection)
   521     a = new QAction( tr( "Replace selection with map" ),tr( "Import (replace)" ), 0, this, "importReplace" );
   522     connect( a, SIGNAL( activated() ), this, SLOT( editImportReplace() ) );
   523 	a->setEnabled (false);
   524     a->addTo ( menu );
   525 	actionEditImportReplace=a;
   526 
   527 	// Save selection 
   528     a = new QAction( tr( "Save selction" ),tr( "Save selection" ), 0, this, "saveSelection" );
   529     connect( a, SIGNAL( activated() ), this, SLOT( editSaveBranch() ) );
   530 	a->setEnabled (false);
   531     a->addTo ( menu );
   532 	actionEditSaveBranch=a;
   533 
   534     // Shortcuts for navigating with cursor:
   535     a = new QAction( tr( "Select upper branch" ),tr( "Select upper branch" ), Key_Up, this, "upperBranch" );
   536     connect( a, SIGNAL( activated() ), this, SLOT( editUpperBranch() ) );
   537     a = new QAction( tr( "Select lower branch" ),tr( "Select lower branch" ), Key_Down, this, "lowerBranch" );
   538     connect( a, SIGNAL( activated() ), this, SLOT( editLowerBranch() ) );
   539     a = new QAction( tr( "Select left branch" ),tr( "Select left branch" ), Key_Left, this, "upperBranch" );
   540     connect( a, SIGNAL( activated() ), this, SLOT( editLeftBranch() ) );
   541     a = new QAction( tr( "Select right branch" ),tr( "Select child branch" ), Key_Right, this, "rightBranch" );
   542     connect( a, SIGNAL( activated() ), this, SLOT( editRightBranch() ) );
   543     a = new QAction( tr( "Select first branch" ),tr( "Select first branch" ), Key_Home, this, "firstBranch" );
   544 	a->setEnabled (false);
   545     a->addTo ( menu );
   546 	actionEditSelectFirst=a;
   547     connect( a, SIGNAL( activated() ), this, SLOT( editFirstBranch() ) );
   548     a = new QAction( tr( "Select last branch" ),tr( "Select last branch" ), Key_End, this, "lastBranch" );
   549     connect( a, SIGNAL( activated() ), this, SLOT( editLastBranch() ) );
   550 	a->setEnabled (false);
   551     a->addTo ( menu );
   552 	actionEditSelectLast=a;
   553 
   554     a = new QAction( tr( "Add Image" ),tr( "Add Image" ), 0, this, "loadImage" );
   555     connect( a, SIGNAL( activated() ), this, SLOT( editLoadImage() ) );
   556 	actionEditLoadImage=a;
   557 }
   558 
   559 // Format Actions
   560 void Main::setupFormatActions()
   561 {
   562     QPopupMenu *menu = new QPopupMenu( this );
   563     menuBar()->insertItem( tr( "&Format" ), menu );
   564 
   565     QToolBar *tb = new QToolBar( this );
   566     QAction *a;
   567     QPixmap pix( 16,16);
   568     pix.fill (black);
   569     actionFormatColor= new QAction( tr( "Set Color" ), pix, tr( "Set &Color" ), 0, this, "formatColor" );
   570     connect( actionFormatColor, SIGNAL( activated() ), this, SLOT( formatSelectColor() ) );
   571     actionFormatColor->addTo( tb );
   572     actionFormatColor->addTo( menu );
   573     a= new QAction( tr( "Pick color\nHint: You can pick a color from another branch and color using CTRL+Left Button" ), QPixmap(formatcolorpicker_xpm), tr( "Pic&k color" ), CTRL + Key_K, this, "pickColor" );
   574     connect( a, SIGNAL( activated() ), this, SLOT( formatPickColor() ) );
   575 	a->setEnabled (false);
   576     a->addTo( tb );
   577     a->addTo( menu );
   578 	actionFormatPickColor=a;
   579     a= new QAction( tr( "Color branch" ), QPixmap(formatcoloritem_xpm), tr( "Color &branch" ), CTRL + Key_I, this, "colorItem" );
   580     connect( a, SIGNAL( activated() ), this, SLOT( formatColorItem() ) );
   581 	a->setEnabled (false);
   582     a->addTo( tb );
   583     a->addTo( menu );
   584 	actionFormatColorBranch=a;
   585     a= new QAction( tr( "Color Subtree" ), QPixmap(formatcolorbranch_xpm), tr( "Color sub&tree" ), CTRL + Key_T, this, "colorBranch" );
   586     connect( a, SIGNAL( activated() ), this, SLOT( formatColorBranch() ) );
   587 	a->setEnabled (false);
   588     a->addTo( menu );
   589     a->addTo( tb );
   590 	actionFormatColorSubtree=a;
   591 
   592     menu->insertSeparator();
   593 	actionGroupFormatLinkStyles=new QActionGroup ( this, "formatLinkStyles");
   594 	actionGroupFormatLinkStyles->setExclusive (true);
   595     a= new QAction( tr( "Line" ), QPixmap(), tr( "Linkstyle Line" ), 0, actionGroupFormatLinkStyles, "formatLinkStyleLine" );
   596 	a->setToggleAction(true);
   597     connect( a, SIGNAL( activated() ), this, SLOT( formatLinkStyleLine() ) );
   598 	actionFormatLinkStyleLine=a;
   599     a= new QAction( tr( "Line" ), QPixmap(), tr( "Linkstyle Parabel" ), 0, actionGroupFormatLinkStyles, "formatLinkStyleLine" );
   600 	a->setToggleAction(true);
   601     connect( a, SIGNAL( activated() ), this, SLOT( formatLinkStyleParabel() ) );
   602 	actionFormatLinkStyleParabel=a;
   603     a= new QAction( tr( "PolyLine" ), QPixmap(), tr( "Linkstyle Thick Line" ), 0, actionGroupFormatLinkStyles, "formatLinkStyleLine" );
   604 	a->setToggleAction(true);
   605     connect( a, SIGNAL( activated() ), this, SLOT( formatLinkStylePolyLine() ) );
   606 	actionFormatLinkStylePolyLine=a;
   607     a= new QAction( tr( "PolyParabel" ), QPixmap(), tr( "Linkstyle Thick Parabel" ), 0, actionGroupFormatLinkStyles, "formatLinkStylePolyParabel" );
   608 	a->setToggleAction(true);
   609     connect( a, SIGNAL( activated() ), this, SLOT( formatLinkStylePolyParabel() ) );
   610 	actionFormatLinkStylePolyParabel=a;
   611 	actionGroupFormatLinkStyles->addTo (menu);
   612 	
   613 	actionGroupFormatFrameTypes=new QActionGroup ( this, "formatFrameTypes");
   614 	actionGroupFormatFrameTypes->setExclusive (true);
   615     a = new QAction( tr( "No Frame" ),tr( "No Frame" ), 0, actionGroupFormatFrameTypes, "frameNone" );
   616 	a->setToggleAction(true);
   617     connect( a, SIGNAL( activated() ), this, SLOT( formatFrameNone() ) );
   618 	actionFormatFrameNone=a;
   619     a = new QAction( tr( "Rectangle" ),tr( "Rectangle" ), 0, actionGroupFormatFrameTypes, "frameRectangle" );
   620 	a->setToggleAction(true);
   621     connect( a, SIGNAL( activated() ), this, SLOT( formatFrameRectangle() ) );
   622 	actionFormatFrameRectangle=a;
   623 
   624     menu->insertSeparator();
   625     a= new QAction( tr( "Use same color for links and headings" ), QPixmap(), tr( "&Use color of heading for link" ), 0, this, "formatLinkColorHint" );
   626 	a->setToggleAction(true);
   627     connect( a, SIGNAL( activated() ), this, SLOT( formatToggleLinkColorHint() ) );
   628 	a->addTo( menu );
   629 	actionFormatLinkColorHint=a;
   630     pix.fill (white);
   631     actionFormatLinkColor= new QAction( tr( "Set Link Color" ), pix, tr( "Set &Link Color" ), 0, this, "formatLinkColor" );
   632     connect( actionFormatLinkColor, SIGNAL( activated() ), this, SLOT( formatSelectLinkColor() ) );
   633     actionFormatLinkColor->addTo( menu );
   634     actionFormatBackColor= new QAction( tr( "Set Background Color" ), pix, tr( "Set &Background Color" ), 0, this, "formatBackColor" );
   635     connect( actionFormatBackColor, SIGNAL( activated() ), this, SLOT( formatSelectBackColor() ) );
   636     actionFormatBackColor->addTo( menu );
   637 }
   638 
   639 // View Actions
   640 void Main::setupViewActions()
   641 {
   642     QToolBar *tb = new QToolBar( this );
   643     tb->setLabel( "View Actions" );
   644     QPopupMenu *menu = new QPopupMenu( this );
   645     menuBar()->insertItem( tr( "&View" ), menu );
   646 
   647     QAction *a;
   648     a = new QAction( tr( "Zoom reset" ), QPixmap(viewzoomreset_xpm), tr( "reset Zoom" ), 0, this, "zoomReset" );
   649     connect( a, SIGNAL( activated() ), this, SLOT(viewZoomReset() ) );
   650     a->addTo( tb );
   651     a->addTo( menu );
   652     a = new QAction( tr( "Zoom in" ), QPixmap(viewzoomin_xpm), tr( "Zoom in" ), CTRL + Key_Plus, this, "zoomIn" );
   653     connect( a, SIGNAL( activated() ), this, SLOT(viewZoomIn() ) );
   654     a->addTo( tb );
   655     a->addTo( menu );
   656     a = new QAction( tr( "Zoom out" ), QPixmap(viewzoomout_xpm), tr( "Zoom out" ), CTRL + Key_Minus, this, "zoomOut" );
   657     connect( a, SIGNAL( activated() ), this, SLOT( viewZoomOut() ) );
   658     a->addTo( tb );
   659     a->addTo( menu );
   660     a = new QAction( tr( "Toggle Note Editor" ), QPixmap(flag_note_xpm), tr( "Toggle Note Editor" ), CTRL + Key_E , this, "noteEditor" );
   661     connect( a, SIGNAL( activated() ), this, SLOT(windowToggleNoteEditor() ) );
   662 	a->setToggleAction(true);
   663 	if (textEditor->showWithMain())
   664 		a->setOn(true);
   665 	else	
   666 		a->setOn(false);
   667     a->addTo( tb );
   668     a->addTo( menu );
   669 	actionViewToggleNoteEditor=a;
   670     a = new QAction( tr( "&Next Window" ), QPixmap(), tr( "Next Window" ), ALT + Key_N , this, "nextWindow" );
   671     connect( a, SIGNAL( activated() ), this, SLOT(windowNextEditor() ) );
   672     a->addTo( menu );
   673     a = new QAction( tr( "&Previous Window" ), QPixmap(), tr( "Previous Window" ), ALT + Key_P , this, "previousWindow" );
   674     connect( a, SIGNAL( activated() ), this, SLOT(windowPreviousEditor() ) );
   675     a->addTo( menu );
   676 }
   677 
   678 // Flag Actions
   679 void Main::setupFlagActions()
   680 {
   681 	standardFlagsDefault->makeToolbar(this, "Standard Flags");
   682 }
   683 
   684 // Settings Actions
   685 void Main::setupSettingsActions()
   686 {
   687     QPopupMenu *menu = new QPopupMenu( this );
   688     menuBar()->insertItem( tr( "&Settings" ), menu );
   689 
   690 	QAction *a;
   691 
   692 
   693     a = new QAction( tr( "Set application to open pdf files" ), QPixmap(), tr( "Set application to open pdf files" ), 0, this, "setPDF" );
   694     connect( a, SIGNAL( activated() ), this, SLOT( settingsPDF() ) );
   695     a->addTo( menu );
   696 
   697     a = new QAction( tr( "Set application to open an URL" ), QPixmap(), tr( "Set application to open an URL" ), 0, this, "setURL" );
   698     connect( a, SIGNAL( activated() ), this, SLOT( settingsURL() ) );
   699     a->addTo( menu );
   700 
   701     menu->insertSeparator();
   702     a = new QAction( tr( "Edit branch after adding it" ), QPixmap(), tr( "Edit branch after adding it" ), 0, this, "autoedit" );
   703 	a->setToggleAction(true);
   704 	a->setOn ( settings.readBoolEntry ("/vym/mapeditor/editmode/autoedit",true) );
   705     a->addTo( menu );
   706 	actionSettingsAutoedit=a;
   707 
   708     a= new QAction( tr( "Select branch after adding it" ), QPixmap(), tr( "Select branch after adding it" ), 0, this, "autoselectheading" );
   709 	a->setToggleAction(true);
   710 	a->setOn ( settings.readBoolEntry ("/vym/mapeditor/editmode/autoselect",false) );
   711     a->addTo( menu );
   712 	actionSettingsAutoselectHeading=a;
   713 	
   714     a= new QAction( tr( "Select heading before editing" ), QPixmap(), tr( "Select existing heading" ), 0, this, "autoselectexistingtext" );
   715 	a->setToggleAction(true);
   716 	a->setOn ( settings.readBoolEntry ("/vym/mapeditor/editmode/autoselectexistingtext",true) );
   717     a->addTo( menu );
   718 	actionSettingsAutoselectText=a;
   719 	
   720     a= new QAction( tr( "Enable pasting into new branch" ), QPixmap(), tr( "Enable pasting into new branch" ), 0, this, "pastenewheading" );
   721 	a->setToggleAction(true);
   722 	a->setOn ( settings.readBoolEntry ("/vym/mapeditor/editmode/newheadingisempty",true) );
   723     a->addTo( menu );
   724 	actionSettingsPasteNewHeading=a;
   725 	
   726     a= new QAction( tr( "Enable Delete key for deleting branches" ), QPixmap(), tr( "Enable Delete key" ), 0, this, "delkey" );
   727 	a->setToggleAction(true);
   728 	a->setOn ( settings.readBoolEntry ("/vym/mapeditor/editmode/useDelKey",false) );
   729     a->addTo( menu );
   730 	actionSettingsUseDelKey=a;
   731 }
   732 
   733 // Test Actions
   734 void Main::setupTestActions()
   735 {
   736     QPopupMenu *menu = new QPopupMenu( this );
   737     menuBar()->insertItem( tr( "&Test" ), menu );
   738 
   739     QAction *a;
   740     a = new QAction( tr( "Test Flag" ), QPixmap(), tr( "test flag" ), 0, this, "flag" );
   741     connect( a, SIGNAL( activated() ), this, SLOT( testFunction() ) );
   742     a->addTo( menu );
   743 
   744 	a = new QAction( tr( "Show Clipboard" ), QPixmap(), tr( "Show clipboard" ), 0, this, "clipboard" );
   745     connect( a, SIGNAL( activated() ), this, SLOT( testShowClipboard() ) );
   746     a->addTo( menu );
   747 }
   748 
   749 // Help Actions
   750 void Main::setupHelpActions()
   751 {
   752     QPopupMenu *menu = new QPopupMenu( this );
   753     menuBar()->insertItem( tr( "&Help" ), menu );
   754 
   755     QAction *a;
   756     a = new QAction( tr( "Open VYM Documentation (pdf)" ), QPixmap(), tr( "Open VYM Documentation (pdf) " ), 0, this, "about" );
   757     connect( a, SIGNAL( activated() ), this, SLOT( helpDoc() ) );
   758     a->addTo( menu );
   759 
   760     a = new QAction( tr( "Information about VYM" ), QPixmap(), tr( "About VYM" ), 0, this, "about" );
   761     connect( a, SIGNAL( activated() ), this, SLOT( helpAbout() ) );
   762     a->addTo( menu );
   763 
   764     a = new QAction( tr( "Information about QT toolkit" ), QPixmap(), tr( "About QT" ), 0, this, "about" );
   765     connect( a, SIGNAL( activated() ), this, SLOT( helpAboutQT() ) );
   766     a->addTo( menu );
   767 }
   768 
   769 // Context Menus
   770 void Main::setupContextMenus()
   771 {
   772 	QAction*a;
   773 
   774 	// Context Menu for branch or mapcenter
   775 	branchContextMenu =new QPopupMenu (this);
   776 	actionEditAddBranch->addTo ( branchContextMenu );
   777 	actionEditAddBranchAbove->addTo ( branchContextMenu );
   778 	actionEditAddBranchBelow->addTo ( branchContextMenu );
   779 	actionEditSaveBranch->addTo( branchContextMenu );
   780 	branchContextMenu->insertSeparator();	
   781 	actionEditImportAdd->addTo ( branchContextMenu );
   782 	actionEditImportReplace->addTo ( branchContextMenu );
   783 	branchContextMenu->insertSeparator();	
   784 	actionEditOpenURL->addTo ( branchContextMenu );
   785 	actionEditURL->addTo ( branchContextMenu );
   786 	actionEditHeading2URL->addTo ( branchContextMenu );
   787     if (settings.readBoolEntry( "/vym/mainwindow/showTestMenu",false)) 
   788 		actionEditBugzilla2URL->addTo( branchContextMenu );
   789 	branchContextMenu->insertSeparator();	
   790 	actionEditOpenVymLink->addTo ( branchContextMenu );
   791 	actionEditVymLink->addTo ( branchContextMenu );
   792 	actionEditDeleteVymLink->addTo ( branchContextMenu );
   793 	branchContextMenu->insertSeparator();	
   794 	actionEditLoadImage->addTo( branchContextMenu );
   795 	branchContextMenu->insertSeparator();	
   796 	actionEditCopy->addTo( branchContextMenu );
   797 	actionEditCut->addTo( branchContextMenu );
   798 	actionEditPaste->addTo( branchContextMenu );
   799 	branchContextMenu->insertSeparator();	
   800 	actionGroupFormatFrameTypes->addTo( branchContextMenu );
   801 
   802 	// Context menu for floatimage
   803 	floatimageContextMenu =new QPopupMenu (this);
   804 	saveImageFormatMenu=new QPopupMenu (this);
   805 	exportImageFormatMenu=new QPopupMenu (this);
   806 
   807 	QStrList fmt = QImage::outputFormats();
   808 	for (const char* f = fmt.first(); f; f = fmt.next()) 
   809 	{
   810 		saveImageFormatMenu->insertItem( f );
   811 		exportImageFormatMenu->insertItem( f );
   812 	}	
   813 	connect( saveImageFormatMenu, SIGNAL( activated(int) ), this, SLOT( editSaveImage(int ) ) );
   814 	connect( exportImageFormatMenu, SIGNAL( activated(int) ), this, SLOT( fileExportImage(int ) ) );
   815 	floatimageContextMenu->insertItem( tr("Save image"),saveImageFormatMenu );
   816 	
   817 	floatimageContextMenu->insertSeparator();	
   818 	actionEditCopy->addTo( floatimageContextMenu );
   819 	actionEditCut->addTo( floatimageContextMenu );
   820 
   821 	floatimageContextMenu->insertSeparator();	
   822     a = new QAction( tr( "Use for Export" ), QPixmap(), tr( "Use for Export"), 0, this, "useForExport" );
   823 	a->setToggleAction(true);
   824     connect( a, SIGNAL( activated() ), this, SLOT( editToggleFloatExport() ) );
   825     a->addTo( floatimageContextMenu);
   826 	actionEditToggleFloatExport=a;
   827 
   828 
   829 
   830 	// Context menu for exports
   831 	exportMenu->insertItem ( tr("Export map as image"),exportImageFormatMenu);
   832 
   833     a = new QAction( tr( "Export as ASCII (still experimental)" ), QPixmap(), tr( "Export (ASCII)" ), 0, this, "exportASCII" );
   834     connect( a, SIGNAL( activated() ), this, SLOT( fileExportASCII() ) );
   835     a->addTo( exportMenu );
   836 
   837 	a = new QAction( tr( "Export XML" ), QPixmap(), tr( "Export XML" ),  0, this, "exportXML" );
   838     connect( a, SIGNAL( activated() ), this, SLOT( fileExportXML() ) );
   839     a->addTo( exportMenu );
   840 	
   841     if (settings.readBoolEntry( "/vym/mainwindow/showTestMenu",false)) 
   842 	{
   843 		a = new QAction( tr( "Export HTML" ), QPixmap(), tr( "Export HTML" ), 0, this, "exportHTML" );
   844 		connect( a, SIGNAL( activated() ), this, SLOT( fileExportHTML() ) );
   845 		a->addTo( exportMenu );
   846 	}
   847 
   848 	a = new QAction( tr( "Export XHTML" ), QPixmap(), tr( "Export XHTML" ), ALT + Key_X, this, "exportXHTML" );
   849     connect( a, SIGNAL( activated() ), this, SLOT( fileExportXHTML() ) );
   850     a->addTo( exportMenu );
   851 
   852 	
   853 	// Context menu for canvas
   854 	canvasContextMenu =new QPopupMenu (this);
   855 	actionEditMapInfo->addTo( canvasContextMenu );
   856 	canvasContextMenu->insertSeparator();	
   857 	actionGroupFormatLinkStyles->addTo( canvasContextMenu );
   858 	canvasContextMenu->insertSeparator();	
   859 	actionFormatLinkColorHint->addTo( canvasContextMenu );
   860 	actionFormatLinkColor->addTo( canvasContextMenu );
   861 	actionFormatBackColor->addTo( canvasContextMenu );
   862 
   863 	// Menu for last opened files
   864 	// Read settings initially
   865 	QString s;
   866 	int j=settings.readNumEntry( "/vym/lastMaps/number",0);
   867 	for (int i=1;i<=j;i++)
   868 	{
   869 		s=settings.readEntry(QString("/vym/lastMaps/map-%1").arg(i),"");
   870 		if (!s.isEmpty() && j<=maxLastMaps) 
   871 			lastMaps.append(s);
   872 	}
   873 	setupLastMapsMenu();
   874 	connect( lastMapsMenu, SIGNAL( activated(int) ), this, SLOT( fileLoadLast(int ) ) );
   875 }
   876 
   877 void Main::setupLastMapsMenu()
   878 {
   879 	// Remove double entries
   880 	QStringList::Iterator it=lastMaps.begin();
   881 	QStringList::Iterator jt;
   882 	while (it!=lastMaps.end() )
   883 	{
   884 		jt=it;
   885 		++jt;
   886 		while (jt!=lastMaps.end() )
   887 		{
   888 			if (*it == *jt)		
   889 				jt=lastMaps.remove(jt);
   890 			else	
   891 				jt++;
   892 		}
   893 		it++;
   894 	}	
   895 
   896 	// Limit length of list to maxLastMaps
   897 	while ((int)(lastMaps.count()) > maxLastMaps) lastMaps.pop_back();
   898 	
   899 	// build Menu from lastMaps string list
   900 	lastMapsMenu->clear();
   901 	for (it = lastMaps.begin(); it != lastMaps.end(); ++it ) 
   902 		lastMapsMenu->insertItem (*it );
   903 		
   904 }
   905 
   906 void Main::hideEvent (QHideEvent * )
   907 {
   908 	if (!textEditor->isMinimized() ) textEditor->hide();
   909 }
   910 
   911 void Main::showEvent (QShowEvent * )
   912 {
   913 	if (textEditor->showWithMain()) textEditor->show();
   914 }
   915 
   916 bool Main::reallyWriteDirectory(const QString &dir)
   917 {
   918 	QStringList eList = QDir(dir).entryList();
   919 	if (eList.first() ==".")  eList.pop_front();	// remove "."
   920 	if (eList.first() =="..") eList.pop_front();	// remove "."
   921 	if (!eList.isEmpty())
   922 	{
   923 		QMessageBox mb( "VYM",
   924 			tr("The directory ") + dir + 
   925 			tr(" is not empty. Do you risk to overwrite its contents?"),
   926 		QMessageBox::Warning,
   927 		QMessageBox::Yes ,
   928 		QMessageBox::Cancel | QMessageBox::Default,
   929 		QMessageBox::QMessageBox::NoButton );
   930 
   931 		mb.setButtonText( QMessageBox::Yes, tr("Overwrite") );
   932 		mb.setButtonText( QMessageBox::No, tr("Cancel"));
   933 		switch( mb.exec() ) 
   934 		{
   935 			case QMessageBox::Yes:
   936 				// save 
   937 				return true;
   938 			case QMessageBox::Cancel:
   939 				// do nothing
   940 				return false;
   941 		}
   942 	}
   943 	return true;
   944 }
   945 
   946 QString Main::browseDirectory (const QString &caption)
   947 {
   948 	QFileDialog fd(this,caption);
   949 	fd.setMode (QFileDialog::DirectoryOnly);
   950 	fd.setCaption("VYM - "+caption);
   951 	fd.show();
   952 	
   953 	if ( fd.exec() == QDialog::Accepted )
   954 		return fd.selectedFile();
   955 	else
   956 		return "";
   957 }
   958 
   959 MapEditor* Main::currentMapEditor() const
   960 {
   961     if ( tabWidget->currentPage() &&
   962 	 tabWidget->currentPage()->inherits( "MapEditor" ) )
   963 		return (MapEditor*)tabWidget->currentPage();
   964     return NULL;	
   965 }
   966 
   967 //TODO not used now, maybe use this for overview window later
   968 void Main::newView() 
   969 {
   970     // Open a new view... have it delete when closed.
   971     Main *m = new Main(0, 0, WDestructiveClose);
   972     qApp->setMainWidget(m);
   973     m->show();
   974     qApp->setMainWidget(0);
   975 }
   976 
   977 void Main::editorChanged(QWidget *)
   978 {
   979 	// Unselect all possibly selected objects
   980 	// (Important to update note editor)
   981 	int i;
   982 	MapEditor *me;
   983 	for (i=0;i<=tabWidget->count() -1;i++)
   984 	{
   985 		
   986 		me=(MapEditor*)tabWidget->page(i);
   987 		me->unselect();
   988 	}	
   989 	currentMapEditor()->reselect();
   990 
   991 	// Update actions to in menus and toolbars according to editor
   992 	currentMapEditor()->updateActions();
   993 }
   994 
   995 void Main::fileNew()
   996 {
   997 	QString fn="unnamed";
   998 	MapEditor* medit = new MapEditor (tabWidget, true);
   999 	tabWidget->addTab (medit,fn);
  1000 	tabWidget->showPage(medit);
  1001 	medit->viewport()->setFocus();
  1002 	medit->select("mc:");
  1003 }
  1004 
  1005 void Main::fileLoad(QString fn, const LoadMode &lmode)
  1006 {
  1007 	// Error codes
  1008 	enum errorCode {success,aborted};
  1009 	errorCode err=success;
  1010 	
  1011 	// fn is usually the archive, mapfile the file after uncompressing
  1012 	QString mapfile;
  1013 
  1014 	// Make fn absolute (needed for unzip)
  1015 	fn=QDir (fn).absPath();
  1016 
  1017 	MapEditor *me;
  1018 
  1019 	if (lmode==NewMap)
  1020 	{
  1021 		// Check, if map is already loaded
  1022 		int i=0;
  1023 		while (i<=tabWidget->count() -1)
  1024 		{
  1025 			me=(MapEditor*)tabWidget->page(i);
  1026 			if (me->getFilePath() == fn)
  1027 			{
  1028 				// Already there, ask for confirmation
  1029 				QMessageBox mb( "VYM",
  1030 					tr("The map ") + fn + tr ("\n is already opened."
  1031 					"Opening the same map in multiple editors may lead \n"
  1032 					"to confusion when finishing working with vym."
  1033 					"Do you want to"),
  1034 					QMessageBox::Warning,
  1035 					QMessageBox::Yes | QMessageBox::Default,
  1036 					QMessageBox::Cancel | QMessageBox::Escape,
  1037 					QMessageBox::QMessageBox::NoButton);
  1038 				mb.setButtonText( QMessageBox::Yes, tr("Open anyway") );
  1039 				mb.setButtonText( QMessageBox::Cancel, tr("Cancel"));
  1040 				switch( mb.exec() ) 
  1041 				{
  1042 					case QMessageBox::Yes:
  1043 						// load anyway
  1044 						i=tabWidget->count();
  1045 						break;
  1046 					case QMessageBox::Cancel:
  1047 						// do nothing
  1048 						return;
  1049 						break;
  1050 				}
  1051 			}
  1052 			i++;
  1053 		}
  1054 	}
  1055 
  1056 
  1057 	// Try to load map
  1058     if ( !fn.isEmpty() )
  1059 	{
  1060 		me = currentMapEditor();
  1061 		int tabIndex=tabWidget->currentPageIndex();
  1062 		// Check first, if mapeditor exists
  1063 		// If it is not default AND we want a new map, 
  1064 		// create a new mapeditor in a new tab
  1065 		if ( lmode==NewMap && (!me || !me->isDefault() ) )
  1066 		{
  1067 			me = new MapEditor (tabWidget,true);
  1068 			tabWidget->addTab (me,fn);
  1069 			tabIndex=tabWidget->indexOf (me);
  1070 			tabWidget->setCurrentPage (tabIndex);
  1071 		}
  1072 		
  1073 		// Check, if file exists (important for creating new files
  1074 		// from command line
  1075 		if (!QFile(fn).exists() )
  1076 		{
  1077 			QMessageBox mb( "VYM",
  1078 				tr("The map") + fn + 
  1079 				tr(" does not exist.\n Do you want to create a new one?"),
  1080 				QMessageBox::Question,
  1081 				QMessageBox::Yes ,
  1082 				QMessageBox::Cancel | QMessageBox::Default,
  1083 				QMessageBox::QMessageBox::NoButton );
  1084 
  1085 			mb.setButtonText( QMessageBox::Yes, tr("Create"));
  1086 			mb.setButtonText( QMessageBox::No, tr("Cancel"));
  1087 			switch( mb.exec() ) 
  1088 			{
  1089 				case QMessageBox::Yes:
  1090 					// Create new map
  1091 					currentMapEditor()->setFilePath(fn);
  1092 					tabWidget->setTabLabel (currentMapEditor(),
  1093 						currentMapEditor()->getFileName() );
  1094 					statusBar()->message( "Created " + fn , statusbarTime );
  1095 					return;
  1096 						
  1097 				case QMessageBox::Cancel:
  1098 					// don't create new map
  1099 					statusBar()->message( "Loading " + fn + " failed!", statusbarTime );
  1100 					fileCloseMap();
  1101 					return;
  1102 			}
  1103 		}	
  1104 
  1105 
  1106 		//tabWidget->currentPage() won't be NULL here, because of above...
  1107 		tabWidget->showPage(me);
  1108 		me->viewport()->setFocus();
  1109 
  1110 		// Create temporary directory for packing
  1111 		char tmpdir1[]="/tmp/vym-XXXXXX";	
  1112 		QString tmpMapDir=mkdtemp(tmpdir1);
  1113 
  1114 		// Try to unzip file
  1115 		Process *zipProc=new Process ();
  1116 		zipProc->clearArguments();
  1117 		zipProc->setWorkingDirectory (QDir(tmpMapDir));
  1118 		zipProc->addArgument ("unzip");
  1119 		zipProc->addArgument (fn );
  1120 		zipProc->addArgument ("-d");
  1121 		zipProc->addArgument (tmpMapDir);
  1122 
  1123 		if (!zipProc->start() )
  1124 		{
  1125 			QMessageBox::critical( 0, tr( "Critical Load Error" ),
  1126 						   tr("Couldn't start unzip to decompress data."));
  1127 			err=aborted;
  1128 			
  1129 		} else
  1130 		{
  1131 			zipProc->waitFinished();
  1132 			if (!zipProc->normalExit() )
  1133 			{
  1134 				QMessageBox::critical( 0, tr( "Critical Load Error" ),
  1135 							   tr("unzip didn't exit normally") +
  1136 							   zipProc->getErrout() );
  1137 				err=aborted;
  1138 			} else
  1139 			{
  1140 				if (zipProc->exitStatus()>0)
  1141 				{
  1142 					if (zipProc->exitStatus()==9)
  1143 					{
  1144 						// no zipped file, but maybe .xml or old version? Try again.
  1145 						mapfile=fn;
  1146 						me->setZipped(false);
  1147 					}	
  1148 					else	
  1149 					{
  1150 						QMessageBox::critical( 0, tr( "Critical Load Error" ),
  1151 									   QString("unzip exit code:  %1").arg(zipProc->exitStatus() ) +
  1152 									   zipProc->getErrout() );
  1153 						err=aborted;
  1154 					}
  1155 				} else
  1156 				{	// Uncompressing was successfull,
  1157 					// load from uncompressed temporary directory
  1158 
  1159 					me->setZipped(true);
  1160 
  1161 					
  1162 					// Look for mapname.xml
  1163 					mapfile= fn.left(fn.findRev(".",-1,true));
  1164 					mapfile=mapfile.section( '/', -1 );
  1165 					QFile file( tmpMapDir + "/" + mapfile + ".xml");
  1166 					if (!file.exists() )
  1167 					{
  1168 						// mapname.xml does not exist, well, 
  1169 						// maybe some renamed the mapname.vym file...
  1170 						// Try to find any .xml in the toplevel 
  1171 						// directory of the .vym file
  1172 						QStringList flist=QDir (tmpMapDir).entryList("*.xml");
  1173 						if (flist.count()==1) 
  1174 						{
  1175 							// Only one entry, take this one
  1176 							mapfile=tmpMapDir + "/"+flist.first();
  1177 						} else
  1178 						{
  1179 							for ( QStringList::Iterator it = flist.begin(); it != flist.end(); ++it ) 
  1180 								*it=tmpMapDir + "/" + *it;
  1181 							// FIXME	
  1182 							// Multiple entries, load all (but only the first one into this ME)
  1183 							//mainWindow->fileLoadFromTmp (flist);
  1184 							//returnCode=1;	// Silently forget this attempt to load
  1185 							qWarning ("MainWindow::load (fn)  multimap found...");
  1186 						}	
  1187 							
  1188 						if (flist.isEmpty() )
  1189 						{
  1190 							QMessageBox::critical( 0, tr( "Critical Load Error" ),
  1191 									   tr("Couldn't find a map (*.xml) in .vym archive.\n"));
  1192 							err=aborted;				   
  1193 						}	
  1194 					} //file doesn't exist	
  1195 					else
  1196 						mapfile=file.name();
  1197 				} // Uncompressing successfull
  1198 			}
  1199 		}
  1200 
  1201 		// Finally load map into mapEditor
  1202 		me->setFilePath (mapfile,fn);
  1203 		if (me->load(mapfile,lmode))
  1204 			err=aborted;
  1205 
  1206 		// Delete tmpDir
  1207 		system ( "rm -rf "+tmpMapDir);
  1208 
  1209 		// Check for errors
  1210 		if (err==success) 
  1211 		{
  1212 			if (lmode==NewMap)
  1213 			{
  1214 				me->setFilePath (fn);
  1215 				tabWidget->changeTab(tabWidget->page(tabIndex), me->getFileName());
  1216 				lastMaps.prepend(me->getFilePath() );
  1217 				setupLastMapsMenu();
  1218 				actionFilePrint->setEnabled (true);
  1219 			}	
  1220 			statusBar()->message( "Loaded " + fn, statusbarTime );
  1221 			return;
  1222 		} else 
  1223 		{
  1224 			if (lmode==NewMap)
  1225 				fileCloseMap();
  1226 		}	
  1227 
  1228 
  1229 	}
  1230 	statusBar()->message( "Could not load " + fn, statusbarTime );
  1231 }
  1232 
  1233 
  1234 void Main::fileLoad(const LoadMode &lmode)
  1235 {
  1236 	QFileDialog *fd=new QFileDialog( this);
  1237 	if (!lastFileDir.isEmpty()) 
  1238 		fd->setDir (lastFileDir);
  1239 	fd->addFilter ("XML (*.xml)");
  1240 	fd->addFilter ("VYM map (*.vym *.vyp)");
  1241 	switch (lmode)
  1242 	{
  1243 		case NewMap:
  1244 			fd->setCaption(tr("Load vym map"));
  1245 			break;
  1246 		case ImportAdd:
  1247 			fd->setCaption(tr("Import: Add vym map to selection"));
  1248 			break;
  1249 		case ImportReplace:
  1250 			fd->setCaption(tr("Import: Replace selection with vym map"));
  1251 			break;
  1252 	}
  1253 	fd->show();
  1254 
  1255 	QString fn;
  1256 	if ( fd->exec() == QDialog::Accepted )
  1257 	{
  1258 		lastFileDir=fd->dirPath();
  1259         fn = fd->selectedFile();
  1260 		fileLoad(fn, lmode);				   
  1261 	}
  1262 }
  1263 
  1264 void Main::fileLoad()
  1265 {
  1266 	fileLoad (NewMap);
  1267 }
  1268 
  1269 void Main::fileLoadLast(int i)
  1270 {
  1271 	fileLoad(*lastMaps.at(lastMapsMenu->indexOf (i) ),NewMap);			   
  1272 }
  1273 
  1274 void Main::fileSave(const SaveMode &savemode)
  1275 {
  1276 	// tmp dir for zipping 
  1277 	QString tmpMapDir;
  1278 	
  1279 	// Error codes
  1280 	enum errorCode {success,aborted};
  1281 	errorCode err=success;
  1282 	
  1283 	QString safeFilePath;
  1284 
  1285 	bool saveZipped=currentMapEditor()->saveZipped();
  1286 
  1287 	if (currentMapEditor())
  1288 	{
  1289 		QString fn=currentMapEditor()->getFilePath();
  1290 		// filename=unnamed, filepath="" in constructor...
  1291 		if ( !fn.isEmpty() ) 
  1292 		{	
  1293 			// We have a filepath, go on saving			
  1294 			// First remove existing file, we 
  1295 			// don't want to add to old zip archives
  1296 			QFile f(fn);
  1297 			if (f.exists() ) 
  1298 				if (!f.remove())
  1299 					QMessageBox::warning( 0, tr( "Save Error" ),
  1300 						fn+   tr("\ncould not be removed before saving"));
  1301 
  1302 			// Look, if we should zip the data:
  1303 			if (!saveZipped)
  1304 			{
  1305 				QMessageBox mb( "VYM",
  1306 					tr("The map ") + fn + 
  1307 					tr ("\ndid not use the compressed "
  1308 					"vym file format.\nWriting it uncompressed will also write images \n"
  1309 					"and flags and thus may overwrite files in the "
  1310 					"given directory\n\nDo you want to write the map"),
  1311 					QMessageBox::Warning,
  1312 					QMessageBox::Yes | QMessageBox::Default,
  1313 					QMessageBox::No ,
  1314 					QMessageBox::Cancel | QMessageBox::Escape);
  1315 				mb.setButtonText( QMessageBox::Yes, tr("compressed (vym default)") );
  1316 				mb.setButtonText( QMessageBox::No, tr("uncompressed") );
  1317 				mb.setButtonText( QMessageBox::Cancel, tr("Cancel"));
  1318 				switch( mb.exec() ) 
  1319 				{
  1320 					case QMessageBox::Yes:
  1321 						// save compressed (default file format)
  1322 						saveZipped=true;
  1323 						break;
  1324 					case QMessageBox::No:
  1325 						// save uncompressed
  1326 						saveZipped=false;
  1327 						break;
  1328 					case QMessageBox::Cancel:
  1329 						// do nothing
  1330 						return;
  1331 						break;
  1332 				}
  1333 			}
  1334 
  1335 			if (saveZipped)
  1336 			{
  1337 				char tmpdir1[]="/tmp/vym-XXXXXX";	
  1338 				tmpMapDir=mkdtemp(tmpdir1);
  1339 			
  1340 				safeFilePath=currentMapEditor()->getFilePath();
  1341 				currentMapEditor()->setFilePath (tmpMapDir+"/"+
  1342 					currentMapEditor()->getMapName()+ ".xml",
  1343 					safeFilePath);
  1344 				currentMapEditor()->save (savemode);
  1345 				currentMapEditor()->setFilePath (safeFilePath);
  1346 				
  1347 				// zip the temporary directory
  1348 				Process *zipProc=new Process ();
  1349 				zipProc->clearArguments();
  1350 				zipProc->setWorkingDirectory (QDir(tmpMapDir));
  1351 				zipProc->addArgument ("zip");
  1352 				zipProc->addArgument ("-r");
  1353 				zipProc->addArgument (fn);
  1354 				zipProc->addArgument (".");
  1355 
  1356 				if (!zipProc->start() )
  1357 				{	
  1358 					// zip could not be started
  1359 					QMessageBox::critical( 0, tr( "Critical Save Error" ),
  1360 								   tr("Couldn't start zip to compress data."));
  1361 					err=aborted;
  1362 				} else
  1363 				{
  1364 					// zip could be started
  1365 					zipProc->waitFinished();
  1366 					if (!zipProc->normalExit() )
  1367 					{
  1368 						QMessageBox::critical( 0, tr( "Critical Save Error" ),
  1369 									   tr("zip didn't exit normally")+
  1370 									   "\n" + zipProc->getErrout());
  1371 						err=aborted;
  1372 					} else
  1373 					{
  1374 						if (zipProc->exitStatus()>0)
  1375 						{
  1376 							QMessageBox::critical( 0, tr( "Critical Save Error" ),
  1377 									   QString("zip exit code:  %1").arg(zipProc->exitStatus() )+
  1378 									   "\n" + zipProc->getErrout() );
  1379 							err=aborted;
  1380 						}
  1381 					}
  1382 				}	// zip could be started
  1383 			} // save zipped
  1384 			else
  1385 			{
  1386 				// Save unzipped. 
  1387 				safeFilePath=currentMapEditor()->getFilePath();
  1388 				currentMapEditor()->setFilePath (fn, safeFilePath);
  1389 				currentMapEditor()->save (savemode);
  1390 				currentMapEditor()->setFilePath (safeFilePath);
  1391 			} // save zipped 	
  1392 		} // filepath available
  1393 		else
  1394 		{
  1395 			// We have  no filepath yet,
  1396 			// call fileSaveAs() now, this will call fileSave() 
  1397 			// again.
  1398 			fileSaveAs(savemode);
  1399 		}
  1400     }
  1401 
  1402 	if (currentMapEditor()->saveZipped())
  1403 	{
  1404 		// Delete tmpDir
  1405 		system ( "rm -rf "+ tmpMapDir );
  1406 	} 
  1407 
  1408 	if (err==success)
  1409 		statusBar()->message( 
  1410 			tr("Saved")+" " + currentMapEditor()->getFilePath(), 
  1411 			statusbarTime );
  1412 	else		
  1413 		statusBar()->message( 
  1414 			tr("Couldn't save")+" " + currentMapEditor()->getFilePath(), 
  1415 			statusbarTime );
  1416 }
  1417 
  1418 void Main::fileSave()
  1419 {
  1420 	fileSave (CompleteMap);
  1421 }
  1422 
  1423 void Main::fileSaveAs(const SaveMode& savemode)
  1424 {
  1425 	QString tmpMapDir;
  1426 	QString fn;
  1427 
  1428 	if (currentMapEditor())
  1429 	{
  1430 		if (savemode==CompleteMap)
  1431 			fn = QFileDialog::getSaveFileName( QString::null, "VYM map (*.vym)", this );
  1432 		else		
  1433 			fn = QFileDialog::getSaveFileName( QString::null, "VYM part of map (*.vyp)", this );
  1434 		if ( !fn.isEmpty() ) 
  1435 		{
  1436 			// Check for existing file
  1437 			if (QFile (fn).exists())
  1438 			{
  1439 				QMessageBox mb( "VYM",
  1440 					tr("The file ") + fn + tr ("\nexists already. Do you want to"),
  1441 					QMessageBox::Warning,
  1442 					QMessageBox::Yes | QMessageBox::Default,
  1443 					QMessageBox::Cancel | QMessageBox::Escape,
  1444 					QMessageBox::QMessageBox::NoButton);
  1445 				mb.setButtonText( QMessageBox::Yes, tr("Overwrite") );
  1446 				mb.setButtonText( QMessageBox::Cancel, tr("Cancel"));
  1447 				switch( mb.exec() ) 
  1448 				{
  1449 					case QMessageBox::Yes:
  1450 						// save 
  1451 						break;
  1452 					case QMessageBox::Cancel:
  1453 						// do nothing
  1454 						return;
  1455 						break;
  1456 				}
  1457 			} else
  1458 			{
  1459 				// New file, add extension to filename, if missing
  1460 				// This is always .vym or .vyp, depending on savemode
  1461 				if (savemode==CompleteMap)
  1462 				{
  1463 					if (!fn.contains (".vym") && !fn.contains (".xml"))
  1464 						fn +=".vym";
  1465 				} else		
  1466 				{
  1467 					if (!fn.contains (".vyp") && !fn.contains (".xml"))
  1468 						fn +=".vyp";
  1469 				}
  1470 			}
  1471 	
  1472 
  1473 
  1474 
  1475 			// Save now
  1476 			currentMapEditor()->setFilePath(fn);
  1477 			fileSave(savemode);
  1478 
  1479 			// Set name of tab
  1480 			tabWidget->setTabLabel (currentMapEditor(),
  1481 				currentMapEditor()->getFileName() );
  1482 			return;
  1483 		} 
  1484 	}
  1485 }
  1486 
  1487 void Main::fileSaveAs()
  1488 {
  1489 	fileSaveAs (CompleteMap);
  1490 }
  1491 
  1492 void Main::fileImportDir()
  1493 {
  1494 	if (currentMapEditor())
  1495 		currentMapEditor()->importDir();	
  1496 }
  1497 
  1498 void Main::fileExportXML()
  1499 {
  1500 	if (currentMapEditor())
  1501 	{
  1502 		QString dir=browseDirectory(tr("Export XML to directory"));
  1503 		if (dir !="" && reallyWriteDirectory(dir) )
  1504 			currentMapEditor()->exportXML(dir);
  1505 	}	
  1506 }
  1507 
  1508 void Main::fileExportHTML()
  1509 {
  1510 	if (currentMapEditor())
  1511 	{
  1512 		ExportHTMLDialog dia(this);
  1513 		
  1514 		if (dia.exec()==QDialog::Accepted)
  1515 		{
  1516 			QString dir=dia.getDir();
  1517 			if (reallyWriteDirectory(dir) )
  1518 			{
  1519 				currentMapEditor()->exportXML (dia.getDir() );
  1520 				dia.doExport(currentMapEditor()->getMapName() );
  1521 			}	
  1522 		}
  1523 	}	
  1524 }
  1525 
  1526 void Main::fileExportXHTML()
  1527 {
  1528 	QString dir;
  1529 	if (currentMapEditor())
  1530 	{
  1531 		ExportXHTMLDialog dia(this);
  1532 		dia.setFilePath (currentMapEditor()->getFilePath() );
  1533 		dia.setMapName (currentMapEditor()->getMapName() );
  1534 		dia.readSettings();
  1535 		
  1536 		if (dia.exec()==QDialog::Accepted)
  1537 		{
  1538 			QString dir=dia.getDir();
  1539 			// Check, if warnings should be used before overwriting
  1540 			// the output directory
  1541 			bool ok;
  1542 			if (dia.warnings()) 
  1543 				ok=reallyWriteDirectory(dir);
  1544 			else
  1545 				ok=true;
  1546 
  1547 			if (ok)
  1548 			{
  1549 				currentMapEditor()->exportXML (dia.getDir() );
  1550 				dia.doExport(currentMapEditor()->getMapName() );
  1551 				if (dia.hasChanged())
  1552 					currentMapEditor()->setChanged();
  1553 			}	
  1554 		}
  1555 	}	
  1556 }
  1557 
  1558 void Main::fileExportImage(int item)
  1559 {
  1560 	if (currentMapEditor())
  1561 	{
  1562 		QString fn = QFileDialog::getSaveFileName( QString::null, "Image  (*.bmp *.jpg *.pbm *.pgm *.png *.ppm *xbm *.xpm)",
  1563 							   this );
  1564 		if ( !fn.isEmpty() ) 
  1565 		{
  1566 			currentMapEditor()->exportImage(fn,item);
  1567 		} else {
  1568 			statusBar()->message( tr("Couldn't save") + fn, statusbarTime );
  1569 		}
  1570 	}
  1571 }
  1572 
  1573 void Main::fileExportASCII()
  1574 {
  1575 	if (currentMapEditor())
  1576 		currentMapEditor()->exportASCII();	
  1577 }
  1578 
  1579 void Main::fileCloseMap()
  1580 {
  1581 	if (currentMapEditor())
  1582 	{
  1583 		if (currentMapEditor()->hasChanged())
  1584 		{
  1585 			QMessageBox mb( "VYM",
  1586 				tr("The map ") + currentMapEditor()->getFileName() +
  1587 				tr(" has been modified but not saved yet. Do you want to"),
  1588 				QMessageBox::Warning,
  1589 				QMessageBox::Yes | QMessageBox::Default,
  1590 				QMessageBox::No,
  1591 				QMessageBox::Cancel | QMessageBox::Escape );
  1592 			mb.setButtonText( QMessageBox::Yes, tr("Save modified map before closing it") );
  1593 			mb.setButtonText( QMessageBox::No, tr("Discard changes"));
  1594 			switch( mb.exec() ) 
  1595 			{
  1596 				case QMessageBox::Yes:
  1597 					// save and close
  1598 					fileSave(CompleteMap);
  1599 					break;
  1600 				case QMessageBox::No:
  1601 				// close  without saving
  1602 					break;
  1603 				case QMessageBox::Cancel:
  1604 					// do nothing
  1605 				return;
  1606 			}
  1607 		} 
  1608 		currentMapEditor()->closeMap();
  1609 		tabWidget->removePage(currentMapEditor());
  1610 		if (tabWidget->count()==0)
  1611 			actionFilePrint->setEnabled (false);
  1612 	}	
  1613 }
  1614 
  1615 void Main::filePrint()
  1616 {
  1617 	if (currentMapEditor())
  1618 		currentMapEditor()->print();
  1619 }
  1620 
  1621 void Main::fileExitVYM()
  1622 {
  1623 	// Check if one or more editors have changed
  1624 	MapEditor *me;
  1625 	int i;
  1626 	for (i=0;i<=tabWidget->count() -1;i++)
  1627 	{
  1628 		
  1629 		me=(MapEditor*)tabWidget->page(i);
  1630 
  1631 		// If something changed, ask what to do
  1632 		if (me->isUnsaved())
  1633 		{
  1634 			tabWidget->setCurrentPage(i);
  1635 			QMessageBox mb( "VYM",
  1636 				tr("This map is not saved yet. Do you want to"),
  1637 				QMessageBox::Warning,
  1638 				QMessageBox::Yes | QMessageBox::Default,
  1639 				QMessageBox::No,
  1640 				QMessageBox::Cancel | QMessageBox::Escape );
  1641 			mb.setButtonText( QMessageBox::Yes, tr("Save map") );
  1642 			mb.setButtonText( QMessageBox::No, tr("Discard changes") );
  1643 			mb.show();
  1644 			// Call undocumented function: setActiveWindow is only
  1645 			// possible, if widget is visible. This depends on
  1646 			// windowmanager...
  1647 #if defined(Q_OS_LINUX)
  1648 			qt_wait_for_window_manager( this);
  1649 #endif			
  1650 			mb.setActiveWindow();
  1651 			switch( mb.exec() ) {
  1652 				case QMessageBox::Yes:
  1653 					// save (the changed editors) and exit
  1654 					fileSave(CompleteMap);
  1655 					break;
  1656 				case QMessageBox::No:
  1657 					// exit without saving
  1658 					break;
  1659 				case QMessageBox::Cancel:
  1660 					// don't save and don't exit
  1661 				return;
  1662 			}
  1663 		}
  1664 	} // loop over all MEs	
  1665     qApp->quit();
  1666 }
  1667 
  1668 void Main::editUndo()
  1669 {
  1670 	if (currentMapEditor())
  1671 		currentMapEditor()->undo();
  1672 }
  1673 
  1674 void Main::editRedo()	    // TODO
  1675 {
  1676 }
  1677 
  1678 void Main::editCopy()
  1679 {
  1680 	if (currentMapEditor())
  1681 		currentMapEditor()->copy();
  1682 }
  1683 
  1684 void Main::editPaste()
  1685 {
  1686 	if (currentMapEditor())
  1687 		currentMapEditor()->paste();
  1688 }
  1689 
  1690 void Main::editCut()
  1691 {
  1692 	if (currentMapEditor())
  1693 		currentMapEditor()->cut();
  1694 }
  1695 
  1696 void Main::editOpenFindWindow()
  1697 {
  1698 	findWindow->popup();
  1699 	findWindow->raise();
  1700 
  1701 	// Call undocumented function: setActiveWindow is only
  1702 	// possible, if widget is visible. This depends on
  1703 	// windowmanager...
  1704 #if defined(Q_OS_LINUX)
  1705 	qt_wait_for_window_manager( this);
  1706 #endif	
  1707 	findWindow->setActiveWindow();
  1708 }
  1709 
  1710 void Main::editFind(QString s)
  1711 {
  1712 	bool cs=false;
  1713 	BranchObj *bo=currentMapEditor()->findText(s, cs);
  1714 	if (bo)
  1715 	{	
  1716 		statusBar()->message( "Found: " + bo->getHeading(), statusbarTime );
  1717 	} else
  1718 	{
  1719 		QMessageBox::information( findWindow, tr( "VYM -Information:" ),
  1720 							   tr("No matches found for ")+
  1721 							   "'<b>"+s+"</b>'");
  1722 	}	
  1723 }
  1724 
  1725 void Main::editFindChanged()
  1726 {	// Notify editor, to abort the current find process
  1727 	currentMapEditor()->findReset();
  1728 }
  1729 
  1730 void Main::editOpenURL()
  1731 {
  1732 	if (currentMapEditor())
  1733 	    currentMapEditor()->openURL();
  1734 }
  1735 
  1736 void Main::editURL()
  1737 {
  1738 	if (currentMapEditor())
  1739 	    currentMapEditor()->editURL();
  1740 }
  1741 
  1742 void Main::editHeading2URL()
  1743 {
  1744 	if (currentMapEditor())
  1745 	    currentMapEditor()->editHeading2URL();
  1746 }
  1747 
  1748 void Main::editBugzilla2URL()
  1749 {
  1750 	if (currentMapEditor())
  1751 	    currentMapEditor()->editBugzilla2URL();
  1752 }
  1753 
  1754 void Main::editOpenVymLink()
  1755 {
  1756 	// Get current path to map
  1757 	QString currentVymLink;
  1758 	if (currentMapEditor())
  1759 	{
  1760 		currentVymLink=currentMapEditor()->getVymLink();	
  1761 		// compare path with already loaded maps
  1762 		int index=-1;
  1763 		int i;
  1764 		MapEditor *me;
  1765 		for (i=0;i<=tabWidget->count() -1;i++)
  1766 		{
  1767 			me=(MapEditor*)tabWidget->page(i);
  1768 			if (currentVymLink==me->getFilePath() )
  1769 			{
  1770 				index=i;
  1771 				break;
  1772 			}
  1773 		}	
  1774 		if (index<0)
  1775 		// Load map
  1776 		{
  1777 			if (!QFile(currentVymLink).exists() )
  1778 				QMessageBox::critical( 0, tr( "Critical Error" ),
  1779 				   tr("Couldn't open map " +currentVymLink)+".");
  1780 			else
  1781 			{
  1782 				fileLoad (currentVymLink, NewMap);
  1783 				tabWidget->setCurrentPage (tabWidget->count()-1);	
  1784 			}
  1785 		} else
  1786 			// Go to tab containing the map
  1787 			tabWidget->setCurrentPage (index);	
  1788 	}
  1789 }
  1790 
  1791 void Main::editVymLink()
  1792 {
  1793 	if (currentMapEditor())
  1794 		currentMapEditor()->editVymLink();	
  1795 }
  1796 
  1797 void Main::editDeleteVymLink()
  1798 {
  1799 	if (currentMapEditor())
  1800 		currentMapEditor()->deleteVymLink();	
  1801 }
  1802 
  1803 void Main::editMapInfo()
  1804 {
  1805 	if (currentMapEditor())
  1806 		currentMapEditor()->editMapInfo();	
  1807 }
  1808 
  1809 void Main::editMoveUp()
  1810 {
  1811 	if (currentMapEditor())
  1812 	    currentMapEditor()->moveBranchUp();
  1813 }
  1814 
  1815 void Main::editMoveDown()
  1816 {
  1817 	if (currentMapEditor())
  1818 		currentMapEditor()->moveBranchDown();
  1819 }
  1820 
  1821 void Main::editToggleScroll()
  1822 {
  1823 	if (currentMapEditor())
  1824 	{
  1825 		currentMapEditor()->toggleScroll();	
  1826 	}	
  1827 }
  1828 
  1829 void Main::editUnScrollAll()
  1830 {
  1831 	if (currentMapEditor())
  1832 	{
  1833 		currentMapEditor()->unScrollAll();	
  1834 	}	
  1835 }
  1836 
  1837 void Main::editHeading()
  1838 {
  1839 	if (currentMapEditor())
  1840 		currentMapEditor()->editHeading();
  1841 }
  1842 
  1843 void Main::editNewBranch()
  1844 {
  1845 	if (currentMapEditor())
  1846 		currentMapEditor()->addNewBranch(0);
  1847 }
  1848 
  1849 void Main::editNewBranchAbove()
  1850 {
  1851 	if (currentMapEditor())
  1852 		currentMapEditor()->addNewBranch(-1);
  1853 }
  1854 
  1855 void Main::editNewBranchBelow()
  1856 {
  1857 	if (currentMapEditor())
  1858 		currentMapEditor()->addNewBranch(1);
  1859 }
  1860 
  1861 void Main::editImportAdd()
  1862 {
  1863 	fileLoad (ImportAdd);
  1864 }
  1865 
  1866 void Main::editImportReplace()
  1867 {
  1868 	fileLoad (ImportReplace);
  1869 }
  1870 
  1871 void Main::editSaveBranch()
  1872 {
  1873 	fileSaveAs (PartOfMap);
  1874 }
  1875 
  1876 void Main::editDeleteSelection()
  1877 {
  1878 	if (currentMapEditor())
  1879 		currentMapEditor()->deleteSelection();
  1880 }
  1881 
  1882 void Main::editUpperBranch()
  1883 {
  1884 	if (currentMapEditor())
  1885 		currentMapEditor()->selectUpperBranch();
  1886 }
  1887 
  1888 void Main::editLowerBranch()
  1889 {
  1890 	if (currentMapEditor())
  1891 		currentMapEditor()->selectLowerBranch();
  1892 }
  1893 
  1894 void Main::editLeftBranch()
  1895 {
  1896 	if (currentMapEditor())
  1897 		currentMapEditor()->selectLeftBranch();
  1898 }
  1899 
  1900 void Main::editRightBranch()
  1901 {
  1902 	if (currentMapEditor())
  1903 		currentMapEditor()->selectRightBranch();
  1904 }
  1905 
  1906 void Main::editFirstBranch()
  1907 {
  1908 	if (currentMapEditor())
  1909 		currentMapEditor()->selectFirstBranch();
  1910 }
  1911 
  1912 void Main::editLastBranch()
  1913 {
  1914 	if (currentMapEditor())
  1915 		currentMapEditor()->selectLastBranch();
  1916 }
  1917 
  1918 void Main::editLoadImage()
  1919 {
  1920 	if (currentMapEditor())
  1921 		currentMapEditor()->loadFloatImage();
  1922 }
  1923 
  1924 void Main::editSaveImage(int item)
  1925 {
  1926 	if (currentMapEditor())
  1927 		currentMapEditor()->saveFloatImage(item);
  1928 }
  1929 
  1930 void Main::editToggleFloatExport()
  1931 {
  1932 	if (currentMapEditor())
  1933 		currentMapEditor()->toggleFloatExport();
  1934 }
  1935 
  1936 void Main::formatSelectColor()
  1937 {
  1938 	if (currentMapEditor())
  1939 	{
  1940 		QColor col = QColorDialog::getColor( currentMapEditor()->color(), this );
  1941 		if ( !col.isValid() ) return;
  1942 		currentMapEditor()->setColor( col );
  1943 		colorChanged( col );
  1944 	}	
  1945 }
  1946 
  1947 void Main::formatPickColor()
  1948 {
  1949 	if (currentMapEditor())
  1950 		colorChanged( currentMapEditor()->pickColor() );
  1951 }
  1952 
  1953 void Main::colorChanged(QColor c)
  1954 {
  1955     QPixmap pix( 16, 16 );
  1956     pix.fill( c );
  1957     actionFormatColor->setIconSet( pix );
  1958 }
  1959 
  1960 void Main::formatColorItem()
  1961 {
  1962 	if (currentMapEditor())
  1963 		currentMapEditor()->colorItem();
  1964 }
  1965 
  1966 void Main::formatColorBranch()
  1967 {
  1968 	if (currentMapEditor())
  1969 		currentMapEditor()->colorBranch();
  1970 }
  1971 
  1972 void Main::formatLinkStyleLine()
  1973 {
  1974 	if (currentMapEditor())
  1975 		currentMapEditor()->setLinkStyle(StyleLine);
  1976 }
  1977 
  1978 void Main::formatLinkStyleParabel()
  1979 {
  1980 	if (currentMapEditor())
  1981 		currentMapEditor()->setLinkStyle(StyleParabel);
  1982 }
  1983 
  1984 void Main::formatLinkStylePolyLine()
  1985 {
  1986 	if (currentMapEditor())
  1987 		currentMapEditor()->setLinkStyle(StylePolyLine);
  1988 }
  1989 
  1990 void Main::formatLinkStylePolyParabel()
  1991 {
  1992 	if (currentMapEditor())
  1993 		currentMapEditor()->setLinkStyle(StylePolyParabel);
  1994 }
  1995 
  1996 void Main::formatSelectBackColor()
  1997 {
  1998 	if (currentMapEditor())
  1999 		currentMapEditor()->selectBackgroundColor();
  2000 }
  2001 
  2002 void Main::formatSelectLinkColor()
  2003 {
  2004 	if (currentMapEditor())
  2005 		currentMapEditor()->selectLinkColor();
  2006 }
  2007 
  2008 void Main::formatToggleLinkColorHint()
  2009 {
  2010 	currentMapEditor()->toggleLinkColorHint();
  2011 }
  2012 
  2013 void Main::formatFrameNone()
  2014 {
  2015 	if (currentMapEditor())
  2016 		currentMapEditor()->setFrame(NoFrame);
  2017 }
  2018 
  2019 void Main::formatFrameRectangle()
  2020 {
  2021 	if (currentMapEditor())
  2022 		currentMapEditor()->setFrame(Rectangle);
  2023 }
  2024 
  2025 void Main::viewZoomReset()
  2026 {
  2027 	if (currentMapEditor())
  2028 	{
  2029 		QWMatrix m;
  2030 		m.reset();
  2031 		currentMapEditor()->setWorldMatrix( m );
  2032 		currentMapEditor()->setViewCenter();
  2033 		currentMapEditor()->adjustCanvasSize();
  2034 	}	
  2035 }
  2036 
  2037 void Main::viewZoomIn()
  2038 {
  2039 	if (currentMapEditor())
  2040 	{
  2041 		QWMatrix m = currentMapEditor()->worldMatrix();
  2042 		m.scale( 1.25, 1.25 );
  2043 		currentMapEditor()->setWorldMatrix( m );
  2044 		currentMapEditor()->setViewCenter();
  2045 		currentMapEditor()->adjustCanvasSize();
  2046 	}	
  2047 }
  2048 
  2049 void Main::viewZoomOut()
  2050 {
  2051 	if (currentMapEditor())
  2052 	{
  2053 		QWMatrix m = currentMapEditor()->worldMatrix();
  2054 		m.scale( 0.8, 0.8 );
  2055 		currentMapEditor()->setWorldMatrix( m );
  2056 		currentMapEditor()->setViewCenter();
  2057 		currentMapEditor()->adjustCanvasSize();
  2058 	}	
  2059 }
  2060 bool Main::settingsPDF()
  2061 {
  2062 	// Default browser is set in constructor
  2063 	bool ok;
  2064 	QString text = QInputDialog::getText(
  2065 		"VYM", tr("Enter path for pdf reader:"), QLineEdit::Normal,
  2066 		settings.readEntry("/vym/mainwindow/readerPDF"), &ok, this );
  2067 	if (ok)
  2068 		settings.writeEntry ("/vym/mainwindow/readerPDF",text);
  2069 	return ok;
  2070 }
  2071 
  2072 
  2073 bool Main::settingsURL()
  2074 {
  2075 	// Default browser is set in constructor
  2076 	bool ok;
  2077 	QString text = QInputDialog::getText(
  2078 		"VYM", tr("Enter path for application to open an URL:"), QLineEdit::Normal,
  2079 		settings.readEntry("/vym/mainwindow/readerURL")
  2080 		, &ok, this );
  2081 	if (ok)
  2082 		settings.writeEntry ("/vym/mainwindow/readerURL",text);
  2083 	return ok;
  2084 }
  2085 
  2086 void Main::windowToggleNoteEditor()
  2087 {
  2088 	if (textEditor->showWithMain() )
  2089 		windowHideNoteEditor();
  2090 	else	
  2091 		windowShowNoteEditor();
  2092 }
  2093 
  2094 void Main::windowShowNoteEditor()
  2095 {
  2096 	textEditor->setShowWithMain(true);
  2097 	textEditor->show();
  2098 	actionViewToggleNoteEditor->setOn (true);
  2099 }
  2100 
  2101 void Main::windowHideNoteEditor()
  2102 {
  2103 	textEditor->setShowWithMain(false);
  2104 	textEditor->hide();
  2105 	actionViewToggleNoteEditor->setOn (false);
  2106 }
  2107 
  2108 void Main::windowNextEditor()
  2109 {
  2110 	if (tabWidget->currentPageIndex() < tabWidget->count())
  2111 		tabWidget->setCurrentPage (tabWidget->currentPageIndex() +1);
  2112 }
  2113 
  2114 void Main::windowPreviousEditor()
  2115 {
  2116 	if (tabWidget->currentPageIndex() >0)
  2117 		tabWidget->setCurrentPage (tabWidget->currentPageIndex() -1);
  2118 }
  2119 
  2120 void Main::standardFlagChanged()
  2121 {
  2122 	currentMapEditor()->toggleStandardFlag(sender()->name());
  2123 }
  2124 
  2125 void Main::testFunction()
  2126 {
  2127 	//textEditor->stackUnder(this);
  2128 	currentMapEditor()->testFunction();
  2129 }
  2130 
  2131 void Main::testShowClipboard()
  2132 {
  2133 	clipboardME->show();
  2134 }
  2135 
  2136 void Main::helpDoc()
  2137 {
  2138 	QString docpath;
  2139 	// default path in SUSE LINUX
  2140 	docpath="/usr/share/doc/packages/vym/doc/vym.pdf";
  2141 
  2142 	if (!QFile (docpath).exists() )
  2143 	{
  2144 		// relative path for easy testing in tarball
  2145 		docpath="doc/vym.pdf";
  2146 		if (!QFile (docpath).exists() )
  2147 		{
  2148 			// relative path for testing while still writing vym.tex
  2149 			docpath="doc/tex/vym.pdf";
  2150 			if (!QFile (docpath).exists() )
  2151 			{
  2152 				QMessageBox::critical(0, 
  2153 				tr("Critcal error"),
  2154 				tr("Couldn't find the documentation\n"
  2155 				"vym.pdf in various places."));
  2156 				return;
  2157 			}	
  2158 		}
  2159 	}
  2160 	
  2161 	Process *pdfProc = new Process();
  2162 	pdfProc->clearArguments();
  2163 	pdfProc->addArgument( settings.readEntry("/vym/mainwindow/readerPDF"));
  2164 	pdfProc->addArgument( docpath);
  2165 
  2166 	if ( !pdfProc->start() ) 
  2167 	{
  2168 		// error handling
  2169 		QMessageBox::critical(0, 
  2170 		tr("Critcal error"),
  2171 		tr("Couldn't find a viewer to read vym.pdf.\n"
  2172 		"Please use Settings->")+tr("Set application to open pdf files"));
  2173 		return;
  2174 	}
  2175 }
  2176 
  2177 
  2178 void Main::helpAbout()
  2179 {
  2180     static QMessageBox* about = new QMessageBox( "VYM",
  2181 			"<h3>VYM - View Your Mind </h3>"
  2182 			"<p> A tool to put the things you have got in your mind into a map.</p>"
  2183 			"<ul>"
  2184 				"<li> (c) by Uwe Drechsel (vym@InSilmaril.de)</li>"
  2185 				"<li> vym is released under the GPL (Gnu General Public License)"
  2186 				", with one exception (see the file \"LICENSE\"which "
  2187 				"comes with vym). This exception is needed to build vym with QT libraries for proprietary operating systems.</li>"
  2188 				"<li> Project homepage <a href=\"http:/www.InSilmaril.de/vym\">"
  2189     					"http:/www.InSilmaril.de/vym</a></li>"
  2190 			"<li> Credits " 
  2191 				"<ul>"
  2192 					"<li>Thomas Schraitle for the stylesheet  used for XHTML-export </li>"
  2193 					"<li>Clemens Kraus for stylesheets and script used for HTML-export "
  2194 					"<a href=\"http://www.clemens-kraus.de\">(www.clemens-kraus.de)</a></li>"
  2195 					"<li>Alexander Johannesen for providing stylesheets from his xsiteable project " 
  2196 					"<a href=\"http://www.shelter.nu/xsiteable/xsiteable.html\">(www.shelter.nu/xsiteable/xsiteable.html)</a>. </li>"
  2197 					"<li>Ken Wimer and Olaf Hering for Mac support</li>"
  2198 				"</ul>"
  2199 			"</li>"
  2200 			"<li> Version "  __VYM_VERSION__  "</li>"
  2201 				"<li> Build date "  __BUILD_DATE__"</li>"
  2202 			"</ul>", QMessageBox::Information, 1, 0, 0, this, 0, FALSE );
  2203     about->setButtonText( 1, "Dismiss" );
  2204     about->setMinimumSize(QSize(300,300));
  2205 	about->setIconPixmap (QPixmap(vym_logo_xpm));
  2206     about->show();
  2207 }
  2208 
  2209 void Main::helpAboutQT()
  2210 {
  2211 	QMessageBox::aboutQt( this, "Qt Application Example" );
  2212 }
  2213