mapeditor.cpp
author insilmaril
Sat, 09 Apr 2005 22:50:08 +0000
changeset 94 6783e13bb05d
parent 93 31c6ce8efbc7
child 95 f688a9913724
permissions -rw-r--r--
links are now partially visible, if one end is scrolled
     1 #include "mapeditor.h"
     2 
     3 #include <qstatusbar.h>
     4 #include <qmessagebox.h>
     5 #include <qapplication.h>
     6 #include <qpainter.h>
     7 #include <qpopupmenu.h>
     8 #include <qprinter.h>
     9 #include <qpaintdevicemetrics.h>
    10 #include <qfile.h>
    11 #include <qfiledialog.h>
    12 #include <qdir.h>
    13 #include <qcolor.h>
    14 #include <qcolordialog.h>
    15 #include <qbitmap.h>
    16 #include <qprocess.h>
    17 #include <qinputdialog.h>
    18 
    19 #include <iostream>
    20 #include <stdlib.h>
    21 #include <typeinfo>
    22 
    23 #include "version.h"
    24 
    25 #include "xml.h"
    26 #include "texteditor.h"
    27 #include "linkablemapobj.h"
    28 #include "exports.h"
    29 #include "misc.h"
    30 #include "mainwindow.h"
    31 #include "extrainfodialog.h"
    32 #include "editlinkdialog.h"
    33 #include "settings.h"
    34 
    35 #include "icons/flag-note.xpm"
    36 #include "icons/flag-url.xpm"
    37 #include "icons/flag-vymlink.xpm"	
    38 #include "icons/flag-scrolled-right.xpm"
    39 #include "icons/flag-tmpUnscrolled-right.xpm"
    40 #include "icons/flag-questionmark.xpm"
    41 #include "icons/flag-exclamationmark.xpm"
    42 #include "icons/flag-hook-green.xpm"
    43 #include "icons/flag-cross-red.xpm"
    44 #include "icons/flag-stopsign.xpm"
    45 #include "icons/flag-smiley-good.xpm"
    46 #include "icons/flag-smiley-sad.xpm"
    47 #include "icons/flag-clock.xpm"
    48 #include "icons/flag-lamp.xpm"
    49 #include "icons/flag-arrow-up.xpm"
    50 #include "icons/flag-arrow-down.xpm"
    51 #include "icons/flag-thumb-up.xpm"
    52 #include "icons/flag-thumb-down.xpm"
    53 #include "icons/flag-heart.xpm"
    54 #include "icons/flag-flash.xpm"
    55 #include "icons/flag-lifebelt.xpm"
    56 
    57 extern TextEditor *textEditor;
    58 extern int statusbarTime;
    59 extern Main *mainWindow;
    60 extern FlagRowObj *systemFlagsDefault;
    61 extern FlagRowObj *standardFlagsDefault;
    62 extern MapEditor *clipboardME;
    63 
    64 extern QAction *actionFileSave;
    65 extern QAction *actionEditUndo;
    66 extern QAction *actionEditCopy;
    67 extern QAction *actionEditCut;
    68 extern QAction *actionEditPaste;
    69 extern QAction *actionEditMoveUp;
    70 extern QAction *actionEditMoveDown;
    71 extern QAction *actionEditToggleScroll;
    72 extern QAction *actionEditOpenURL;
    73 extern QAction *actionEditURL;
    74 extern QAction *actionEditHeading2URL;
    75 extern QAction *actionEditBugzilla2URL;
    76 extern QAction *actionEditOpenVymLink;
    77 extern QAction *actionEditVymLink;
    78 extern QAction *actionEditDeleteVymLink;
    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* actionFormatPickColor;
    93 extern QAction* actionFormatColorBranch;
    94 extern QAction* actionFormatColorSubtree;
    95 extern QAction *actionFormatLinkColorHint;
    96 extern QAction *actionFormatBackColor;
    97 extern QAction *actionFormatLinkColor;
    98 
    99 extern QActionGroup* actionGroupModModes;
   100 extern QAction* actionModModeColor;
   101 extern QAction* actionModModeLink;
   102 extern QAction* actionModModeCopy;
   103 
   104 extern QActionGroup *actionGroupFormatFrameTypes;
   105 extern QAction *actionFormatFrameNone;
   106 extern QAction *actionFormatFrameRectangle;
   107 
   108 extern QActionGroup *actionGroupFormatLinkStyles;
   109 extern QAction *actionFormatLinkStyleLine;
   110 extern QAction *actionFormatLinkStyleParabel;
   111 extern QAction *actionFormatLinkStylePolyLine;
   112 extern QAction *actionFormatLinkStylePolyParabel;
   113 
   114 extern QAction *actionViewToggleNoteEditor;
   115 
   116 extern QAction *actionSettingsAutoedit;
   117 extern QAction *actionSettingsAutoselectHeading;
   118 extern QAction *actionSettingsAutoselectText;
   119 extern QAction *actionSettingsPasteNewHeading;
   120 
   121 extern QPopupMenu *branchContextMenu;
   122 extern QPopupMenu *branchLinksContextMenu;
   123 extern QPopupMenu *branchLinksContextMenuDup;
   124 extern QPopupMenu *floatimageContextMenu;
   125 extern QPopupMenu *saveImageFormatMenu;
   126 extern QPopupMenu *exportImageFormatMenu;
   127 extern QPopupMenu *canvasContextMenu;
   128 
   129 extern Settings settings;
   130 
   131 
   132 ///////////////////////////////////////////////////////////////////////
   133 ///////////////////////////////////////////////////////////////////////
   134 MapEditor::MapEditor(
   135 	QWidget* parent, bool interactive, const char* name, WFlags f) :
   136     QCanvasView(parent,name,f)
   137 {
   138 	//cout << "Constructor ME "<<this<<endl;
   139 
   140     mapCanvas = new QCanvas(1000,800);
   141 	mapCanvas->setAdvancePeriod(30);
   142 
   143     setCanvas (mapCanvas);
   144 	
   145 	setVScrollBarMode ( QScrollView::AlwaysOn );
   146 	setHScrollBarMode ( QScrollView::AlwaysOn );
   147 
   148 	// Now create the _global_ system flags _once_:
   149 	// (Later all OrnamentedObj copy from this 
   150 	// and set their own canvas)
   151 	if (!systemFlagsDefault)
   152 	{
   153 		systemFlagsDefault = new FlagRowObj (mapCanvas);
   154 		systemFlagsDefault->setVisibility (false);
   155 		systemFlagsDefault->setName ("systemFlagsDef");
   156 
   157 		FlagObj *fo = new FlagObj (mapCanvas);
   158 		fo->load(QPixmap(flag_note_xpm));
   159 		fo->setName("note");
   160 		fo->setToolTip(tr("Note","Systemflag"));
   161 		systemFlagsDefault->addFlag (fo);	// makes deep copy
   162 
   163 		fo->load(QPixmap(flag_url_xpm));
   164 		fo->setName("url");
   165 		fo->setToolTip(tr("WWW Document (external)","Systemflag"));
   166 		systemFlagsDefault->addFlag (fo);
   167 		
   168 		fo->load(QPixmap(flag_vymlink_xpm));
   169 		fo->setName("vymLink");
   170 		fo->setToolTip(tr("Link to another vym map","Systemflag"));
   171 		systemFlagsDefault->addFlag (fo);	
   172 
   173 		fo->load(QPixmap(flag_scrolled_right_xpm));
   174 		fo->setName("scrolledright");
   175 		fo->setToolTip(tr("subtree is scrolled","Systemflag"));
   176 		systemFlagsDefault->addFlag (fo);
   177 		
   178 		fo->load(QPixmap(flag_tmpUnscrolled_right_xpm));
   179 		fo->setName("tmpUnscrolledright");
   180 		fo->setToolTip(tr("subtree is temporary scrolled","Systemflag"));
   181 		systemFlagsDefault->addFlag (fo);
   182 		delete (fo);
   183 	}
   184 	if (!standardFlagsDefault)
   185 	{
   186 		standardFlagsDefault = new FlagRowObj (mapCanvas);
   187 		standardFlagsDefault->setVisibility (false);
   188 		standardFlagsDefault->setName ("standardFlagsDef");
   189 
   190 		FlagObj *fo = new FlagObj (mapCanvas);
   191 		fo->load(QPixmap(flag_exclamationmark_xpm));
   192 		fo->setName("exclamationmark");
   193 		fo->setToolTip(tr("Take care!","Standardflag"));
   194 		standardFlagsDefault->addFlag (fo);	// makes deep copy
   195 		
   196 		fo->load(QPixmap(flag_questionmark_xpm));
   197 		fo->setName("questionmark");
   198 		fo->setToolTip(tr("Really?","Standardflag"));
   199 		standardFlagsDefault->addFlag (fo);	
   200 
   201 		fo->load(QPixmap(flag_hook_green_xpm));
   202 		fo->setName("hook-green");
   203 		fo->setToolTip(tr("ok!","Standardflag"));
   204 		standardFlagsDefault->addFlag (fo);	
   205 
   206 		fo->load(QPixmap(flag_cross_red_xpm));
   207 		fo->setName("cross-red");
   208 		fo->setToolTip(tr("Not ok!","Standardflag"));
   209 		standardFlagsDefault->addFlag (fo);	
   210 
   211 		fo->load(QPixmap(flag_stopsign_xpm));
   212 		fo->setName("stopsign");
   213 		fo->setToolTip(tr("This won't work!","Standardflag"));
   214 		standardFlagsDefault->addFlag (fo);	
   215 
   216 		fo->load(QPixmap(flag_smiley_good_xpm));
   217 		fo->setName("smiley-good");
   218 		fo->setToolTip(tr("Good","Standardflag"));
   219 		standardFlagsDefault->addFlag (fo);	
   220 
   221 		fo->load(QPixmap(flag_smiley_sad_xpm));
   222 		fo->setName("smiley-sad");
   223 		fo->setToolTip(tr("Bad","Standardflag"));
   224 		standardFlagsDefault->addFlag (fo);	
   225 
   226 		fo->load(QPixmap(flag_clock_xpm));
   227 		fo->setName("clock");
   228 		fo->setToolTip(tr("Time critical","Standardflag"));
   229 		standardFlagsDefault->addFlag (fo);	
   230 
   231 		fo->load(QPixmap(flag_lamp_xpm));
   232 		fo->setName("lamp");
   233 		fo->setToolTip(tr("Idea!","Standardflag"));
   234 		standardFlagsDefault->addFlag (fo);	
   235 
   236 		fo->load(QPixmap(flag_arrow_up_xpm));
   237 		fo->setName("arrow-up");
   238 		fo->setToolTip(tr("Important","Standardflag"));
   239 		standardFlagsDefault->addFlag (fo);	
   240 
   241 		fo->load(QPixmap(flag_arrow_down_xpm));
   242 		fo->setName("arrow-down");
   243 		fo->setToolTip(tr("Unimportant","Standardflag"));
   244 		standardFlagsDefault->addFlag (fo);	
   245 
   246 		fo->load(QPixmap(flag_thumb_up_xpm));
   247 		fo->setName("thumb-up");
   248 		fo->setToolTip(tr("I like this","Standardflag"));
   249 		standardFlagsDefault->addFlag (fo);	
   250 
   251 		fo->load(QPixmap(flag_thumb_down_xpm));
   252 		fo->setName("thumb-down");
   253 		fo->setToolTip(tr("I do not like this","Standardflag"));
   254 		standardFlagsDefault->addFlag (fo);	
   255 		
   256 		fo->load(QPixmap(flag_heart_xpm));
   257 		fo->setName("heart");
   258 		fo->setToolTip(tr("I just love... ","Standardflag"));
   259 		standardFlagsDefault->addFlag (fo);	
   260 
   261 		fo->load(QPixmap(flag_flash_xpm));
   262 		fo->setName("flash");
   263 		fo->setToolTip(tr("Dangerous","Standardflag"));
   264 		standardFlagsDefault->addFlag (fo);	
   265 		
   266 		fo->load(QPixmap(flag_lifebelt_xpm));
   267 		fo->setName("lifebelt");
   268 		fo->setToolTip(tr("This will help","Standardflag"));
   269 		standardFlagsDefault->addFlag (fo);	
   270 		delete (fo);
   271 
   272 	}
   273 	
   274     mapCenter = new MapCenterObj(mapCanvas);
   275     mapCenter->setVisibility (true);
   276 	mapCenter->setMapEditor (this);
   277 	mapCenter->setHeading (tr("New Map","Heading of mapcenter in new map"));
   278 
   279     printer=NULL;
   280 
   281     lineedit = new QLineEdit(this, "lineedit"  );
   282     connect( lineedit, SIGNAL( returnPressed() ), SLOT( finishedLineEditNoSave() ) );
   283     lineedit->hide();
   284 
   285     actColor=black; setColor (actColor);
   286 	deflinkcolor=QColor (0,0,255);
   287 	linkcolorhint=DefaultColor;
   288 	linkstyle=StylePolyParabel;
   289 	mapCanvas->setBackgroundColor (white);
   290 
   291 	// Create bitmap cursors, patform dependant
   292 	#if defined(Q_OS_MACX)
   293 		#include "icons/cursorhandopen16.xpm"
   294 		#include "icons/cursorcolorpicker16.xpm"
   295 		QBitmap cb( 16, 16, chandopen, TRUE );
   296 		QBitmap cm( 16, 16, chandopenmask, TRUE );
   297 		handOpenCursor=QCursor ( cb, cm );		
   298 		// set hot spot to tip of picker			
   299 		pickColorCursor=QCursor ( cursorcolorpicker_xpm, 1,15 ); 
   300 	#else
   301 		#include "icons/cursorhandopen.xpm"
   302 		#include "icons/cursorcolorpicker.xpm"
   303 
   304 		QBitmap cb( 32, 32, chandopen, TRUE );
   305 		QBitmap cm( 32, 32, chandopenmask, TRUE );
   306 		handOpenCursor=QCursor ( cb, cm );		
   307 		// set hot spot to tip of picker			
   308 		pickColorCursor=QCursor ( cursorcolorpicker_xpm, 5,27 ); 
   309 	#endif
   310 
   311 	pickingColor=false;
   312 	drawingLink=false;
   313 	copyingObj=false;
   314 
   315     editingBO=NULL;
   316     selection=NULL;
   317     selectionLast=NULL;
   318     movingObj=NULL;
   319 
   320     mapChanged=false;
   321 	mapDefault=true;
   322 	mapUnsaved=false;
   323 	undoSelection=NULL;
   324 	
   325 	zipped=true;
   326 	filePath="";
   327 	fileName="unnamed";
   328 	mapName="";
   329 
   330 	// Initialize find routine
   331 	itFind=NULL;				
   332 	EOFind=false;
   333 
   334 	printFrame=true;
   335 	printFooter=true;
   336 
   337 	blockreposition=false;
   338 	isInteractive=interactive;
   339 	if (isInteractive)
   340 		// Create temporary files
   341 		makeTmpDirs();
   342 
   343 	// Initially set movingCentre
   344 	updateViewCenter();
   345 
   346 	mapCenter->reposition();	//	for positioning heading
   347 }
   348 
   349 MapEditor::~MapEditor()
   350 {
   351 	//cout <<"Destructor MapEditor\n";
   352 	if (isInteractive) delTmpDirs();
   353 
   354 	// Save Settings
   355 	//settings.writeEntry( "/vym/mapeditor/editmode/autoselect",  );
   356 
   357 }
   358 
   359 QColor MapEditor::color()
   360 {
   361     return actColor;
   362 }
   363 
   364 QColor MapEditor::backgroundColor()
   365 {
   366     return mapCanvas->backgroundColor();
   367 }
   368 
   369 MapCenterObj* MapEditor::getMapCenter()
   370 {
   371     return mapCenter;
   372 }
   373 
   374 QCanvas* MapEditor::getCanvas()
   375 {
   376     return mapCanvas;
   377 }
   378 
   379 void MapEditor::adjustCanvasSize()
   380 {
   381 	// To adjust the canvas to map, viewport size and position, we have to
   382 	// do some coordinate magic...
   383 	//
   384 	// Get rectangle of (scroll-)view. 
   385 	// We want to be in canvas coords, so
   386 	// we map. Important if view is zoomed...
   387 	QRect view = inverseWorldMatrix().mapRect( QRect( contentsX(), contentsY(),
   388 												visibleWidth(), visibleHeight()) );	
   389 												
   390 	// Now we need the bounding box of view AND map to calc the correct canvas size.
   391 	// Why? Because if the map itself is moved out of view, the view has to be enlarged
   392 	// to avoid jumping aroung...
   393 	QRect map=mapCenter->getTotalBBox();
   394 
   395 	// right edge - left edge
   396 	int cw= max(map.x() + map.width(),  view.x() + view.width())  - min(map.x(), view.x());
   397 	int ch= max(map.y() + map.height(), view.y() + view.height()) - min(map.y(), view.y());
   398 
   399 
   400 	if ( (cw!=mapCanvas->width()) || (ch!=mapCanvas->height()) ||
   401 		!mapCanvas->onCanvas (map.topLeft()) || !mapCanvas->onCanvas (map.bottomRight())
   402 	)	
   403 	{	
   404 		// move the map on canvas (in order to not move it on screen) this is neccessary
   405 		// a) if topleft corner of canvas is left or above topleft corner of view and also left of
   406 		//    above topleft corner of map. E.g. if map is completly inside view, but it would be possible 
   407 		//    to scroll to an empty area of canvas to the left.
   408 		// b) if topleft corner of map left of or above topleft of canvas
   409 		int dx=0;
   410 		int dy=0;
   411 
   412 		if (cw > mapCanvas->width() )
   413 		{
   414 			if (map.x()<0) dx=-map.x();	
   415 		}
   416 		if (cw <  mapCanvas->width() )
   417 			dx=-min (view.x(),map.x());
   418 		if (ch > mapCanvas->height() )
   419 		{
   420 			if (map.y()<0) dy=-map.y();	
   421 		}
   422 		if (ch <  mapCanvas->height() )
   423 		{
   424 			dy=-min (view.y(),map.y());
   425 		}
   426 		// We really have to resize now. Let's go...
   427 		mapCanvas->resize (cw,ch);
   428 		if ( (dx!=0) || (dy!=0) ) 
   429 		{
   430 			mapCenter->moveAllBy(dx,dy);
   431 			mapCenter->reposition();
   432 
   433 			// scroll the view (in order to not move map on screen)
   434 			scrollBy (dx,dy);
   435 		}	
   436 	}
   437 }
   438 
   439 bool MapEditor::blockReposition()
   440 {
   441 	return blockreposition;
   442 }
   443 
   444 void MapEditor::makeTmpDirs()
   445 {
   446 	// Create unique temporary directories
   447 	char tmpdir[]="/tmp/vym-XXXXXX";	
   448 	bakMapDir=mkdtemp(tmpdir);
   449 	makeSubDirs(bakMapDir);
   450 	// FIXME set permissions
   451 	// and maybe use QT method for portability
   452 }
   453 
   454 void MapEditor::delTmpDirs()
   455 {
   456 	//FIXME delete tmp directory, better use QT methods here:
   457 	system ( "rm -rf "+ bakMapDir );
   458 }
   459 
   460 
   461 void MapEditor::makeSubDirs(const QString &s)
   462 {
   463 	QDir d(s);
   464 	d.mkdir ("images");	
   465 	d.mkdir ("flags");	
   466 }
   467 
   468 
   469 QString MapEditor::saveToDir(const QString &tmpdir, const QString &prefix, bool writeflags, const QPoint &offset, SaveMode savemode)
   470 {
   471 	// tmpdir		temporary directory to which data will be writte
   472 	// prefix		mapname, which will be appended to images etc.
   473 	// writeflags	Only write flags for "real" save of map, not undo
   474 	// offset		offset of bbox of whole map in canvas. 
   475 	//				Needed for XML export
   476 	// completeMap	if false, only vympart will be written, without
   477 	//				mapcenter
   478 	
   479 	// Save Header
   480 	QString ls;
   481 	switch (linkstyle)
   482 	{
   483 		case StyleLine: 
   484 			ls="StyleLine";
   485 			break;
   486 		case StyleParabel:
   487 			ls="StyleParabel";
   488 			break;
   489 		case StylePolyLine:	
   490 			ls="StylePolyLine";
   491 			break;
   492 		default:
   493 			ls="StylePolyParabel";
   494 			break;
   495 	}	
   496 
   497 	QString s="<?xml version=\"1.0\" encoding=\"utf-8\"?><!DOCTYPE vymmap>\n";
   498 	QString colhint="";
   499 	if (linkcolorhint==HeadingColor) 
   500 		colhint=attribut("linkColorHint","HeadingColor");
   501 
   502 	QString mapAttr=attribut("version",__VYM_VERSION__);
   503 	if (savemode==CompleteMap)
   504 		mapAttr+= attribut("author",mapCenter->getAuthor()) +
   505 				  attribut("comment",mapCenter->getComment()) +
   506 			      attribut("date",mapCenter->getDate()) +
   507 		          attribut("backgroundColor", mapCanvas->backgroundColor().name() ) +
   508 		          attribut("linkStyle", ls ) +
   509 		          attribut("linkColor", deflinkcolor.name() ) +
   510 		          colhint; 
   511 	s+=beginElement("vymmap",mapAttr);
   512 	incIndent();
   513 
   514 	// Find the used flags while traversing the tree
   515 	standardFlagsDefault->resetUsedCounter();
   516 	
   517 	// Build xml recursivly
   518 	if (savemode==CompleteMap)
   519 		s+=mapCenter->saveToDir(tmpdir,prefix,writeflags,offset);
   520 	else
   521 	{
   522 		if ( undoSelection &&
   523 			typeid(*undoSelection) == typeid(BranchObj) )
   524 			s+=((BranchObj*)(undoSelection))->saveToDir(tmpdir,prefix,offset);
   525 		else	
   526 			if (selection && typeid(*selection)==typeid(BranchObj))
   527 				s+=((BranchObj*)(selection))->saveToDir(tmpdir,prefix,offset);
   528 	}
   529 
   530 	// Save local settings
   531 	s+=settings.getXMLData (destPath);
   532 
   533 	// Save selection
   534 	if (selection) 
   535 		s+=valueElement("select",selection->getSelectString());
   536 
   537 	decIndent();
   538 	s+=endElement("vymmap");
   539 
   540 	if (writeflags)
   541 		standardFlagsDefault->saveToDir (tmpdir+"/flags/","",writeflags);
   542 	
   543 	return s;
   544 }
   545 
   546 void MapEditor::saveState()
   547 {
   548 	saveState (CompleteMap,NULL);
   549 }
   550 
   551 void MapEditor::saveState(const SaveMode &mode, LinkableMapObj *part)
   552 {
   553 	// all binary data is saved in bakMapDir (created in Constructor)
   554 	// the xml data itself is kept in memory in backupXML
   555 	// 
   556 	// For faster write/read of data, a part of the map can be
   557 	// written. Then the undoSelection will mark the part of the
   558 	// map which should be replaced if an undo is wanted later.
   559 
   560 	if (mode==PartOfMap && part && (typeid(*part) == typeid (BranchObj) ) ) 
   561 	{
   562 		// Writing a vympart only is useful for BranchObj
   563 		undoSelection=part;
   564 		backupXML=saveToDir (bakMapDir,mapName+"-",false, QPoint (),PartOfMap);
   565 	} else	
   566 	{
   567 		undoSelection=NULL;
   568 		backupXML=saveToDir (bakMapDir,mapName+"-",false, QPoint (),CompleteMap);
   569 	}	
   570 }
   571 
   572 void MapEditor::finishedLineEditNoSave()
   573 {
   574 	// This is called by finishedLineEdit or any MapEditor method,
   575 	// which wants to assure, that lineedits finish, before e.g. a branch is 
   576 	// deleted
   577     
   578 	// After calling LineEdit and using the clipboard, the 
   579     // focus is not any longer on the main widget, we
   580     // have to restore it using parentWidget()->setFocus()
   581 
   582     if (editingBO!=NULL) 
   583 	{
   584 		editingBO->setHeading(lineedit->text() );
   585 		editingBO=NULL;
   586 		lineedit->releaseKeyboard();
   587 		lineedit->hide();
   588 		parentWidget()->setFocus();
   589 		mapCenter->reposition();
   590 		adjustCanvasSize();
   591 		ensureSelectionVisible();
   592     }		
   593 }
   594 
   595 
   596 bool MapEditor::isDefault()
   597 {
   598     return mapDefault;
   599 }
   600 
   601 bool MapEditor::isUnsaved()
   602 {
   603     return mapUnsaved;
   604 }
   605 
   606 bool MapEditor::hasChanged()
   607 {
   608     return mapChanged;
   609 }
   610 
   611 void MapEditor::setChanged()
   612 {
   613 	mapChanged=true;
   614 	mapDefault=false;
   615 	mapUnsaved=true;
   616 	actionEditUndo->setEnabled (true);
   617 	actionFileSave->setEnabled (true);
   618 	findReset();
   619 }
   620 
   621 void MapEditor::closeMap()
   622 {
   623 	// Finish open lineEdits
   624 	if (lineedit) finishedLineEditNoSave();
   625 
   626 	// Unselect before disabling the toolbar actions
   627 	if (selection) selection->unselect();
   628 	selection=NULL;
   629 	updateActions();
   630 
   631     clear();
   632 	close();
   633 }
   634 
   635 void MapEditor::setFilePath(QString fname)
   636 {
   637 	setFilePath (fname,fname);
   638 }
   639 
   640 void MapEditor::setFilePath(QString fname, QString destname)
   641 {
   642 	filePath=fname;
   643 	fileName=fname;
   644 	destPath=destname;
   645 
   646 	// If fname is not an absolute path, complete it
   647 	filePath=QDir(fname).absPath();
   648 	fileDir=filePath.left (1+filePath.findRev ("/"));
   649 
   650 	// Set short name, too. Search from behind:
   651 	int i=fileName.findRev("/");
   652 	if (i>=0) fileName=fileName.remove (0,i+1);
   653 
   654 	// Forget the .vym (or .xml) for name of map
   655 	mapName=fileName.left(fileName.findRev(".",-1,true) );
   656 }
   657 
   658 QString MapEditor::getFilePath()
   659 {
   660 	return filePath;
   661 }
   662 
   663 QString MapEditor::getFileName()
   664 {
   665 	return fileName;
   666 }
   667 
   668 QString MapEditor::getMapName()
   669 {
   670 	return mapName;
   671 }
   672 
   673 QString MapEditor::getDestPath()
   674 {
   675 	return destPath;
   676 }
   677 
   678 int MapEditor::load (QString &fname, const LoadMode &lmode)
   679 {
   680 	// Finish open lineEdits
   681 	if (lineedit) finishedLineEditNoSave();
   682 
   683 	int returnCode=0;
   684 
   685 	if (lmode==NewMap)
   686 	{
   687 		if (selection) selection->unselect();
   688 		selection=NULL;
   689 		mapCenter->clear();
   690 		mapCenter->setMapEditor(this);
   691 		// (map state is set later at end of load...)
   692 	} else
   693 	{
   694 		setChanged();
   695 		saveState(PartOfMap,selection);
   696 	}	
   697 	
   698     
   699     mapBuilderHandler handler;
   700 	QFile file( fname );
   701 
   702 	// I am paranoid: file should exist anyway
   703 	// according to check in mainwindow.
   704 	if (!file.exists() )
   705 	{
   706 		QMessageBox::critical( 0, tr( "Critical Parse Error" ),
   707 				   tr("Couldn't open map " +fname)+".");
   708 		returnCode=1;	
   709 	} else
   710 	{
   711 		blockreposition=true;
   712 		QXmlInputSource source( file);
   713 		QXmlSimpleReader reader;
   714 		reader.setContentHandler( &handler );
   715 		reader.setErrorHandler( &handler );
   716 		handler.setMapEditor( this );
   717 		handler.setTmpDir (filePath.left(filePath.findRev("/",-1)));	// needed to load files with rel. path
   718 		handler.setLoadMode (lmode);
   719 		bool ok = reader.parse( source );
   720 		blockreposition=false;
   721 		file.close();
   722 		if ( ok ) 
   723 		{
   724 			mapCenter->reposition();
   725 			adjustCanvasSize();
   726 			if (lmode==NewMap)
   727 			{
   728 				mapDefault=false;
   729 				mapChanged=false;
   730 				mapUnsaved=false;
   731 			}
   732 		} else 
   733 		{
   734 			QMessageBox::critical( 0, tr( "Critical Parse Error" ),
   735 					   tr( handler.errorProtocol() ) );
   736 			// returnCode=1;	
   737 			// Still return "success": the map maybe at least
   738 			// partially read by the parser
   739 		}	
   740 	}	
   741 	updateActions();
   742 	return returnCode;
   743 }
   744 
   745 int MapEditor::save (const SaveMode &savemode)
   746 {
   747 	// Finish open lineEdits
   748 	if (lineedit) finishedLineEditNoSave();
   749 
   750 	int returnCode=0;
   751 
   752 	// Create mapName and fileDir
   753 	makeSubDirs (fileDir);
   754 	QString fname;
   755 	if (saveZipped())
   756 		// save as .xml
   757 		fname=mapName+".xml";
   758 	else
   759 		// use name given by user, even if he chooses .doc
   760 		fname=fileName;
   761 
   762 
   763 	// Check if fname is writeable
   764 	QFile file( fileDir+fname);
   765 	if (!file.open( IO_WriteOnly ) ) 
   766 	{
   767 		QMessageBox::critical( 0, tr( "Critical Save Error" ),
   768 					   tr("Couldn't write to ") +fileDir+fname);
   769 		return 1;
   770 	}	
   771 	file.close();
   772 
   773 	QString saveFile;
   774 	saveFile=saveToDir (fileDir,mapName+"-",true,QPoint(),savemode);
   775 
   776 	file.setName ( fileDir  + fname);
   777 	if ( !file.open( IO_WriteOnly ) )
   778 	{
   779 		// This should neverever happen
   780 		QMessageBox::critical(0, tr("Critcal Save error"),"MapEditor::save() Couldn't open "+file.name());
   781 		return 1;
   782 	}	
   783 
   784 	// Write it finally, and write in UTF8, no matter what 
   785 	QTextStream ts( &file );
   786 	ts.setEncoding (QTextStream::UnicodeUTF8);
   787 	ts << saveFile;
   788 	file.close();
   789 
   790 	if (returnCode==0)
   791 	{
   792 		mapChanged=false;
   793 		mapUnsaved=false;
   794 		actionFileSave->setEnabled(false);
   795 	}
   796 
   797 	return returnCode;
   798 }
   799 
   800 void MapEditor::setZipped (bool z)
   801 {
   802 	zipped=z;
   803 }
   804 
   805 bool MapEditor::saveZipped ()
   806 {
   807 	return zipped;
   808 }
   809 
   810 void MapEditor::print()
   811 {
   812 	// Finish open lineEdits
   813 	if (lineedit) finishedLineEditNoSave();
   814 
   815 	if ( !printer ) 
   816 	{
   817 		printer = new QPrinter;
   818 		printer->setColorMode (QPrinter::Color);
   819 	}
   820 
   821 	QRect totalBBox=mapCenter->getTotalBBox();
   822 
   823 	// Try to set orientation automagically
   824 	// Note: Interpretation of generated postscript is amibiguous, if 
   825 	// there are problems with landscape mode, see
   826 	// http://sdb.suse.de/de/sdb/html/jsmeix_print-cups-landscape-81.html
   827 
   828 	if (totalBBox.width()>totalBBox.height())
   829 		// recommend landscape
   830 		printer->setOrientation (QPrinter::Landscape);
   831 	else	
   832 		// recommend portrait
   833 		printer->setOrientation (QPrinter::Portrait);
   834 
   835 	if ( printer->setup(this) ) 
   836 	// returns false, if printing is canceled
   837 	{
   838 		QPainter pp(printer);
   839 
   840 		// Don't print the visualisation of selection
   841 		LinkableMapObj *oldselection=NULL;
   842 		if (selection) 
   843 		{
   844 			oldselection=selection;
   845 			selection->unselect();
   846 		}
   847 
   848 		// Handle sizes of map and paper:
   849 		//
   850 		// setWindow defines which part of the canvas will be transformed 
   851 		// setViewport defines area on paper in device coordinates (dpi)
   852 		// e.g. (0,50,700,700) is upper part on A4
   853 		// see also /usr/lib/qt3/doc/html/coordsys.html
   854 
   855 		QPaintDeviceMetrics metrics (printer);
   856 
   857 		double paperAspect = (double)metrics.width()   / (double)metrics.height();
   858 		double   mapAspect = (double)totalBBox.width() / (double)totalBBox.height();
   859 
   860 		QRect mapRect=mapCenter->getTotalBBox();
   861 		QCanvasRectangle *frame=NULL;
   862 		QCanvasText *footerFN=NULL;
   863 		QCanvasText *footerDate=NULL;
   864 		if (printFrame || printFooter)
   865 		{
   866 			
   867 			if (printFrame) 
   868 			{
   869 				// Print frame around map
   870 				mapRect.setRect (mapRect.x()-10, mapRect.y()-10, 
   871 					mapRect.width()+20, mapRect.height()+20);
   872 				frame=new QCanvasRectangle (mapRect,mapCanvas);
   873 				frame->setBrush (QColor(white));
   874 				frame->setPen (QColor(black));
   875 				frame->setZ(0);
   876 				frame->show();    
   877 			}		
   878 			if (printFooter) 
   879 			{
   880 				// Print footer below map
   881 				QFont font;		
   882 				font.setPointSize(10);
   883 				footerFN=new QCanvasText (mapCanvas);
   884 				footerFN->setText ("VYM - " + fileName);
   885 				footerFN->setFont(font);
   886 				footerFN->move (mapRect.x(), mapRect.y() + mapRect.height() );
   887 				footerFN->setZ(Z_TEXT);
   888 				footerFN->show();    
   889 				footerDate=new QCanvasText (mapCanvas);
   890 				footerDate->setText (QDate::currentDate().toString(Qt::TextDate));
   891 				footerDate->setFont(font);
   892 				footerDate->move (mapRect.x()+mapRect.width()-footerDate->boundingRect().width(), mapRect.y() + mapRect.height() );
   893 				footerDate->setZ(Z_TEXT);
   894 				footerDate->show();    
   895 				mapRect.setRect (mapRect.x(), mapRect.y(), 
   896 					mapRect.width(), mapRect.height()+20);
   897 			}
   898 			pp.setWindow (mapRect.x(), mapRect.y(), mapRect.width(), mapRect.height());
   899 		}	else	
   900 		{
   901 			pp.setWindow (mapRect);
   902 		}	
   903 
   904 		if (mapAspect>=paperAspect)
   905 		{
   906 			// Fit horizontally to paper width
   907 			pp.setViewport(0,0, metrics.width(),(int)(metrics.width()/mapAspect) );	
   908 		}	else
   909 		{
   910 			// Fit vertically to paper height
   911 			pp.setViewport(0,0,(int)(metrics.height()*mapAspect),metrics.height());	
   912 		}	
   913 
   914 		mapCanvas->drawArea(mapRect, &pp);	// draw Canvas to printer
   915 
   916 		// Delete Frame and footer
   917 		if (footerFN) 
   918 		{
   919 			delete (footerFN);
   920 			delete (footerDate);
   921 		}	
   922 		if (frame)  delete (frame);
   923 
   924 		// Restore selection
   925 		if (oldselection) 
   926 		{
   927 			selection=oldselection;
   928 			selection->select();
   929 		}	
   930 	}
   931 }
   932 
   933 QPixmap MapEditor::getPixmap()
   934 {
   935 	QRect mapRect=mapCenter->getTotalBBox();
   936 	QPixmap pix (mapRect.size());
   937 	QPainter pp (&pix);
   938 
   939 	// Don't print the visualisation of selection
   940 	LinkableMapObj *oldselection=NULL;
   941 	if (selection) 
   942 	{
   943 		oldselection=selection;
   944 		selection->unselect();
   945 	}
   946 
   947 	pp.setWindow (mapRect);
   948 
   949 	mapCanvas->drawArea(mapRect, &pp);	// draw Canvas to painter
   950 
   951 
   952 	// Restore selection
   953 	if (oldselection) 
   954 	{
   955 		selection=oldselection;
   956 		selection->select();
   957 	}	
   958 	
   959 	return pix;
   960 }
   961 
   962 void MapEditor::exportImage(QString fn)
   963 {
   964 	// Finish open lineEdits
   965 	if (lineedit) finishedLineEditNoSave();
   966 
   967 	QPixmap pix (getPixmap());
   968 	pix.save(fn, "PNG");
   969 }
   970 
   971 void MapEditor::exportImage(QString fn, int item)
   972 {
   973 	// Finish open lineEdits
   974 	if (lineedit) finishedLineEditNoSave();
   975 
   976 	QPixmap pix (getPixmap());
   977 	pix.save(fn, exportImageFormatMenu->text(item) );
   978 }
   979 
   980 void MapEditor::exportASCII()
   981 {
   982 	// FIXME still experimental
   983 	QFileDialog *fd=new QFileDialog( this, tr("VYM - Export (ASCII)"));
   984 	fd->addFilter ("TXT (*.txt)");
   985 	fd->setCaption("VYM - Export (ASCII) (still experimental)");
   986 	fd->setMode( QFileDialog::AnyFile );
   987 	fd->show();
   988 
   989 	if ( fd->exec() == QDialog::Accepted )
   990 	{
   991 		if (QFile (fd->selectedFile()).exists() )
   992 		{
   993 			QMessageBox mb( "VYM",
   994 				tr("The file ") + fd->selectedFile() + 
   995 				tr(" exists already. Do you want to overwrite it?"),
   996 			QMessageBox::Warning,
   997 			QMessageBox::Yes | QMessageBox::Default,
   998 			QMessageBox::Cancel | QMessageBox::Escape,
   999 			QMessageBox::NoButton );
  1000 
  1001 			mb.setButtonText( QMessageBox::Yes, tr("Overwrite") );
  1002 			mb.setButtonText( QMessageBox::No, tr("Cancel"));
  1003 			Export ex;
  1004 			switch( mb.exec() ) 
  1005 			{
  1006 				case QMessageBox::Yes:
  1007 					// save 
  1008 					if (!ex.setOutputDir ("out"))
  1009 					{
  1010 						QMessageBox::critical (0,tr("Critical Export Error "),tr("Couldn't create directory ") + "out");
  1011 						return;
  1012 					}
  1013 					break;;
  1014 				case QMessageBox::Cancel:
  1015 					// do nothing
  1016 					return;
  1017 					break;
  1018 			}
  1019 		}
  1020 		Export ex;
  1021 		ex.setPath (fd->selectedFile() );
  1022 		ex.setMapCenter(mapCenter);
  1023 		ex.exportMap();
  1024 	}
  1025 }
  1026 
  1027 
  1028 void MapEditor::exportXML(const QString &dir)
  1029 {
  1030 	// Create subdirectories
  1031 	makeSubDirs (dir);
  1032 
  1033 	// write to directory
  1034 	QString saveFile=saveToDir (dir,mapName+"-",true,mapCenter->getTotalBBox().topLeft() ,CompleteMap);
  1035 	QFile file;
  1036 
  1037 	file.setName ( dir + "/"+mapName+".xml");
  1038 	if ( !file.open( IO_WriteOnly ) )
  1039 	{
  1040 		// This should neverever happen
  1041 		QMessageBox::critical (0,tr("Critical Export Error"),tr("MapEditor::exportXML couldn't open ")+file.name());
  1042 		return;
  1043 	}	
  1044 
  1045 	// Write it finally, and write in UTF8, no matter what 
  1046 	QTextStream ts( &file );
  1047 	ts.setEncoding (QTextStream::UnicodeUTF8);
  1048 	ts << saveFile;
  1049 	file.close();
  1050 
  1051 	// Now write image, too
  1052 	exportImage (dir+"/images/"+mapName+".png");
  1053 }
  1054 
  1055 void MapEditor::clear()
  1056 {
  1057 	if (selection)
  1058 	{
  1059 		selection->unselect();
  1060 		selection=NULL;
  1061 	}	
  1062 
  1063 	mapCenter->clear();
  1064 }
  1065 
  1066 void MapEditor::undo()
  1067 {
  1068 	QDir d;
  1069 	d.setPath(bakMapDir);
  1070 	if (d.exists() )
  1071 	{
  1072 		// Finish open lineEdits
  1073 		if (lineedit) finishedLineEditNoSave();
  1074 
  1075 		if (selection) 
  1076 		{
  1077 			selection->unselect();
  1078 			selection=NULL;
  1079 		}	
  1080 		
  1081 		mapBuilderHandler handler;
  1082 		QXmlInputSource source;
  1083 		source.setData(backupXML);
  1084 		QXmlSimpleReader reader;
  1085 		reader.setContentHandler( &handler );
  1086 		reader.setErrorHandler( &handler );
  1087 		handler.setMapEditor( this );
  1088 		handler.setTmpDir ( bakMapDir );	// needed to load files with rel. path
  1089 		if (undoSelection)
  1090 		{
  1091 			selection=undoSelection;
  1092 			selection->select();
  1093 			handler.setLoadMode (ImportReplace);
  1094 
  1095 		} else	
  1096 		{
  1097 			mapCenter->clear();
  1098 			handler.setLoadMode (NewMap);
  1099 		}	
  1100 		blockreposition=true;
  1101 		bool ok = reader.parse( source );
  1102 		blockreposition=false;
  1103 		if ( ok ) 
  1104 			mapCenter->reposition();
  1105 			
  1106 		else 
  1107 		{	
  1108 			// This should never ever happen
  1109 			QMessageBox::critical( 0, tr( "Critical Parse Error by reading backupFile" ),
  1110 								   tr( handler.errorProtocol() )+" in "+backupXML );
  1111 		}
  1112 		// Undo not longer available now
  1113 		actionEditUndo->setEnabled (false);
  1114 		undoSelection=false;
  1115 		mapChanged=false;
  1116 		return;
  1117 	} else	
  1118 	{
  1119 		QMessageBox::critical( 0, tr( "Critical Error" ),
  1120 			"Temporary directory " +bakMapDir + 
  1121 			tr (" used for undo is gone. \n"
  1122 			"I will create a new one, but at the moment no undo is available.\n"
  1123 			"Maybe you want to reload your original data.\n\n"
  1124 			"Sorry for any inconveniences.") );
  1125 		makeTmpDirs();	
  1126 	}							
  1127 }
  1128 
  1129 void MapEditor::copy()
  1130 {
  1131 	// Finish open lineEdits
  1132 	if (lineedit) finishedLineEditNoSave();
  1133 
  1134 	if (selection) 
  1135 	{
  1136 		if (typeid(*selection) == typeid(BranchObj) ) 
  1137 		{
  1138 			BranchObj* to;
  1139 			BranchObj* from;
  1140 			clipboardME->clear();
  1141 			clipboardME->getMapCenter()->addBranch();
  1142 			to=clipboardME->getMapCenter()->getLastBranch();
  1143 			if (to) 
  1144 			{
  1145 				from=(BranchObj*)(selection);
  1146 				to->copy(from);
  1147 
  1148 				// keep position relative to parent
  1149 				to->move2RelPos ( from->getRelPos());
  1150 
  1151 				// select data in clipboard
  1152 				clipboardME->select ("bo:0");
  1153 
  1154 				// repositioning makes testing nicer,
  1155 				// but is not needed usually:
  1156 				if (clipboardME->isVisible())
  1157 				{
  1158 					clipboardME->getMapCenter()->reposition();  
  1159 				}	
  1160 				else
  1161 					clipboardME->hide();
  1162 			}
  1163 		}    
  1164 		if (typeid(*selection) == typeid(FloatImageObj) ) 
  1165 		{
  1166 			FloatImageObj* to;
  1167 			FloatImageObj* from;
  1168 			clipboardME->clear();
  1169 			clipboardME->getMapCenter()->addFloatImage();
  1170 			to=clipboardME->getMapCenter()->getLastFloatImage();
  1171 			if (to) 
  1172 			{
  1173 				from=(FloatImageObj*)(selection);
  1174 				to->copy(from);
  1175 
  1176 				// select data in clipboard
  1177 				clipboardME->select ("fi:0");
  1178 
  1179 				// repositioning makes testing nicer,
  1180 				// but is not needed usually:
  1181 				if (clipboardME->isVisible())
  1182 				{
  1183 					clipboardME->getMapCenter()->reposition();  
  1184 				}	
  1185 				else
  1186 					clipboardME->hide();
  1187 			}
  1188 		}    
  1189 	}	    
  1190 }
  1191 
  1192 LinkableMapObj* MapEditor::pasteNoSave()
  1193 {
  1194 	// Finish open lineEdits
  1195 	if (lineedit) finishedLineEditNoSave();
  1196 
  1197 	LinkableMapObj *fromLMO=clipboardME->getSelection();
  1198 	LinkableMapObj *returnLMO=NULL;
  1199 
  1200 	if (selection && fromLMO) 
  1201 	{
  1202 
  1203 		if (typeid(*fromLMO) == typeid(BranchObj) ) 
  1204 		{
  1205 			if (typeid(*selection) == typeid(MapCenterObj)) 
  1206 			{
  1207 				returnLMO=mapCenter->addBranch( (BranchObj*)(fromLMO) );
  1208 				((BranchObj*)(returnLMO))->move2RelPos(normalise(fromLMO->getRelPos() ) );
  1209 			}     
  1210 			if (typeid(*selection) == typeid(BranchObj)) 
  1211 					returnLMO=((BranchObj*)(selection))->addBranch((BranchObj*)(fromLMO) );
  1212 		}
  1213 		
  1214 		if (typeid(*fromLMO) == typeid(FloatImageObj) &&
  1215 			(typeid(*selection) == typeid (BranchObj) || 
  1216 			 typeid(*selection)==typeid(MapCenterObj)) )
  1217 			returnLMO=((BranchObj*) (selection))->addFloatImage ((FloatImageObj*)(fromLMO));
  1218 		
  1219 	}	    
  1220 	return returnLMO;
  1221 }
  1222 
  1223 void MapEditor::cutNoSave()
  1224 {
  1225 	// Finish open lineEdits
  1226 	if (lineedit) finishedLineEditNoSave();
  1227 
  1228 	BranchObj* bo;
  1229 	BranchObj* par; 
  1230 	if (selection != NULL) {
  1231 		if (typeid(*selection) == typeid(BranchObj) ) 
  1232 		{
  1233 			bo=(BranchObj*)(selection);
  1234 			par=(BranchObj*)(bo->getParObj());
  1235 			bo->unselect();
  1236 			selection=NULL;
  1237 			par->removeBranch(bo);
  1238 			selection=par;
  1239 			selection->select();
  1240 		}
  1241 		if (typeid(*selection) == typeid(FloatImageObj) ) 
  1242 		{
  1243 			FloatImageObj* fio=(FloatImageObj*)(selection);
  1244 			par=(BranchObj*)(fio->getParObj());
  1245 			fio->unselect();
  1246 			selection=NULL;
  1247 			par->removeFloatImage(fio);
  1248 			selection=par;
  1249 			selection->select();
  1250 		}
  1251 	}	    
  1252 }
  1253 
  1254 void MapEditor::paste()
  1255 {   
  1256 	setChanged();
  1257 	saveState(PartOfMap,selection);
  1258 	pasteNoSave();
  1259 	mapCenter->reposition();
  1260 	adjustCanvasSize();
  1261 }
  1262 
  1263 void MapEditor::cut()
  1264 {
  1265 	setChanged();
  1266 	saveState(PartOfMap,selection->getParObj());
  1267 	copy();
  1268 	cutNoSave();
  1269 	mapCenter->reposition();
  1270 	adjustCanvasSize();
  1271 }
  1272 
  1273 void MapEditor::moveBranchUp()
  1274 {
  1275 	// Finish open lineEdits
  1276 	if (lineedit) finishedLineEditNoSave();
  1277 
  1278 	BranchObj* bo;
  1279 	BranchObj* par;
  1280 	if (typeid(*selection) == typeid(BranchObj)  ) 
  1281 	{
  1282 		setChanged();
  1283 		saveState(PartOfMap,selection->getParObj());
  1284 		bo=(BranchObj*)(selection);
  1285 		par=(BranchObj*)(bo->getParObj());
  1286 		selection->unselect();
  1287 		selection=par->moveBranchUp (bo);
  1288 		selection->select();
  1289 		mapCenter->reposition();
  1290 		ensureSelectionVisible();
  1291 	}
  1292 }
  1293 
  1294 void MapEditor::moveBranchDown()
  1295 {
  1296 	// Finish open lineEdits
  1297 	if (lineedit) finishedLineEditNoSave();
  1298 
  1299 	BranchObj* bo;
  1300 	BranchObj* par;
  1301 	if (typeid(*selection) == typeid(BranchObj)  ) 
  1302 	{
  1303 		setChanged();
  1304 		saveState(PartOfMap,selection->getParObj());
  1305 		bo=(BranchObj*)(selection);
  1306 		par=(BranchObj*)(bo->getParObj());
  1307 		selection->unselect(); 
  1308 		selection=par->moveBranchDown(bo);
  1309 		selection->select();
  1310 		mapCenter->reposition();
  1311 		ensureSelectionVisible();
  1312 	}	
  1313 }
  1314 
  1315 void MapEditor::editHeading()
  1316 {
  1317 	// Finish open lineEdits
  1318 	if (lineedit) finishedLineEditNoSave();
  1319 
  1320 	if (selection  &&  
  1321 		 (typeid(*selection) == typeid(BranchObj) || 
  1322 		  typeid(*selection) == typeid(MapCenterObj) ) ) 
  1323 	{
  1324 		setChanged();
  1325 		saveState(PartOfMap,selection);
  1326 
  1327 		ensureSelectionVisible();
  1328 		editingBO=(BranchObj*)(selection);
  1329 		QPoint p = worldMatrix().map(QPoint (editingBO->x(),editingBO->y()));
  1330 		lineedit->setGeometry(p.x()-contentsX(),p.y()-contentsY(),200,25);
  1331 		QString s=editingBO->getHeading();
  1332 		lineedit->setText(s);
  1333 		lineedit->setCursorPosition(1);
  1334 		if (actionSettingsAutoselectText->isOn() && !s.isEmpty() && actionSettingsPasteNewHeading->isOn() )
  1335 			lineedit->selectAll();
  1336 		lineedit->show();
  1337 
  1338 		lineedit->grabKeyboard();
  1339 		lineedit->setFocus();
  1340 	}				
  1341 }
  1342 
  1343 
  1344 void MapEditor::addNewBranch(int pos)
  1345 {
  1346 	// Finish open lineEdits
  1347 	if (lineedit) finishedLineEditNoSave();
  1348 
  1349 	if (selection  &&  
  1350 		 (typeid(*selection) == typeid(BranchObj) || 
  1351 		  typeid(*selection) == typeid(MapCenterObj) ) ) 
  1352 	{
  1353 		setChanged();
  1354 		saveState(PartOfMap,selection);
  1355 
  1356 		BranchObj* bo1 = (BranchObj*) (selection);
  1357 		bool wasScrolled=false;
  1358 		BranchObj *newbo=NULL;
  1359 		if (pos==0)
  1360 		{
  1361 			// save scroll state. If scrolled, automatically select
  1362 			// new branch in order to tmp unscroll parent...
  1363 			wasScrolled=bo1->isScrolled();
  1364 			newbo=bo1->addBranch();
  1365 		}	else 
  1366 		{
  1367 			BranchObj *parbo=(BranchObj*)(selection->getParObj());
  1368 			if (parbo)
  1369 			{
  1370 				if (pos<0)
  1371 					// add above selection
  1372 					newbo=parbo->insertBranch(bo1->getNum());
  1373 				else
  1374 					// add below selection
  1375 					newbo=parbo->insertBranch(bo1->getNum()+1);
  1376 			} else
  1377 				// This should not happen...
  1378 				return;
  1379 			
  1380 		}	
  1381 
  1382 		LinkableMapObj *oldselection=selection;
  1383 
  1384 		mapCenter->reposition();
  1385 		adjustCanvasSize();
  1386 		if (actionSettingsAutoedit->isOn() ||
  1387 			actionSettingsAutoselectHeading->isOn() )
  1388 		{
  1389 			selection->unselect();
  1390 			selection=newbo;
  1391 			selection->select();
  1392 			if (actionSettingsPasteNewHeading->isOn() )
  1393 			{
  1394 				BranchObj *bo2= (BranchObj*)(selection);
  1395 				bo2->setHeading("");
  1396 			}	
  1397 			if (actionSettingsAutoedit->isOn() )
  1398 				editHeading();
  1399 			if (!actionSettingsAutoselectHeading->isOn()
  1400 				&& !wasScrolled)
  1401 			{
  1402 				selection->unselect();
  1403 				selection=oldselection;
  1404 				selection->select();
  1405 			}
  1406 		}	
  1407 	}	
  1408 }
  1409 
  1410 void MapEditor::deleteSelection()
  1411 {
  1412 	// Finish open lineEdits
  1413 	if (lineedit) finishedLineEditNoSave();
  1414 
  1415 	if (selection  && typeid(*selection) ==typeid(BranchObj) ) 
  1416 	{
  1417 		setChanged();
  1418 		saveState(PartOfMap,selection->getParObj());
  1419 		BranchObj* bo=dynamic_cast <BranchObj*> (selection);
  1420 		BranchObj* par=(BranchObj*)(bo->getParObj());
  1421 		bo->unselect();
  1422 		selection=NULL;
  1423 		par->removeBranch(bo);
  1424 		selection=par;
  1425 		selection->select();
  1426 		ensureSelectionVisible();
  1427 		mapCenter->reposition();
  1428 		adjustCanvasSize();
  1429 	}
  1430 	if (selection  && typeid(*selection) ==typeid(FloatImageObj) ) 
  1431 	{
  1432 		setChanged();
  1433 		saveState(PartOfMap,selection->getParObj());
  1434 		FloatImageObj* fio=dynamic_cast <FloatImageObj*> (selection);
  1435 		BranchObj* par=(BranchObj*)(fio->getParObj());
  1436 		fio->unselect();
  1437 		selection=NULL;
  1438 		par->removeFloatImage(fio);
  1439 		selection=par;
  1440 		selection->select();
  1441 		ensureSelectionVisible();
  1442 		mapCenter->reposition();
  1443 		adjustCanvasSize();
  1444 	}
  1445 }
  1446 
  1447 LinkableMapObj* MapEditor::getSelection()
  1448 {
  1449 	return selection;
  1450 }
  1451 
  1452 LinkableMapObj* MapEditor::findObj (QString s)
  1453 {
  1454 	LinkableMapObj *lmo=mapCenter;
  1455 	QString part;
  1456 	QString typ;
  1457 	QString num;
  1458 	while (!s.isEmpty() )
  1459 	{
  1460 		part=s.section(",",0,0);
  1461 		typ=part.left (3);
  1462 		num=part.right(part.length() - 3);
  1463 		
  1464 		if (typ=="mc:")
  1465 		{
  1466 			if (num=="")
  1467 				break;
  1468 			else
  1469 				lmo=mapCenter->getBranchNum (num.toUInt());
  1470 		} else
  1471 			if (typ=="bo:")
  1472 				lmo=((BranchObj*)(lmo))->getBranchNum (num.toUInt());
  1473 			else
  1474 				if (typ=="fi:")
  1475 					lmo=((BranchObj*)(lmo))->getFloatImageNum (num.toUInt());
  1476 		if (!lmo) break;
  1477 		
  1478 		if (s.contains(","))
  1479 			s=s.right(s.length() - part.length() -1 );
  1480 		else	
  1481 			break;
  1482 	}
  1483 	return lmo;
  1484 }
  1485 
  1486 bool MapEditor::select (const QString &s)
  1487 {
  1488 	LinkableMapObj *lmo=findObj(s);
  1489 
  1490 	// Finally select the found object
  1491 	if (lmo)
  1492 	{
  1493 		if (selection) selection->unselect();
  1494 		selection=lmo;
  1495 		selection->select();
  1496 		adjustCanvasSize();
  1497 		ensureSelectionVisible();
  1498 		return true;
  1499 	} else
  1500 		return false;
  1501 }
  1502 
  1503 void MapEditor::unselect()
  1504 {
  1505 	if (selection) 
  1506 	{
  1507 		selectionLast=selection;
  1508 		selection->unselect();
  1509 		selection=NULL;
  1510 	}
  1511 }	
  1512 
  1513 void MapEditor::reselect()
  1514 {
  1515 	if (selectionLast)
  1516 	{
  1517 		selection=selectionLast;
  1518 		selection->select();
  1519 		selectionLast=NULL;
  1520 	}
  1521 }	
  1522 
  1523 void MapEditor::selectNextBranch()
  1524 {
  1525 	// Increase number of branch
  1526 	if (selection)
  1527 	{
  1528 		QString s=selection->getSelectString();
  1529 		QString part;
  1530 		QString typ;
  1531 		QString num;
  1532 
  1533 		// Where am I? 
  1534 		part=s.section(",",-1);
  1535 		typ=part.left (3);
  1536 		num=part.right(part.length() - 3);
  1537 
  1538 		s=s.left (s.length() -num.length());
  1539 
  1540 		// Go to next lmo
  1541 		num=QString ("%1").arg(num.toUInt()+1);
  1542 
  1543 		s=s+num;
  1544 		
  1545 		// Try to select this one
  1546 		if (select (s)) return;
  1547 
  1548 		// We have no direct successor, 
  1549 		// try to increase the parental number in order to
  1550 		// find a successor with same depth
  1551 
  1552 		int d=selection->getDepth();
  1553 		int oldDepth=d;
  1554 		int i;
  1555 		bool found=false;
  1556 		bool b;
  1557 		while (!found && d>0)
  1558 		{
  1559 			s=s.section (",",0,d-1);
  1560 			// replace substring of current depth in s with "1"
  1561 			part=s.section(",",-1);
  1562 			typ=part.left (3);
  1563 			num=part.right(part.length() - 3);
  1564 
  1565 			if (d>1)
  1566 			{	
  1567 				// increase number of parent
  1568 				num=QString ("%1").arg(num.toUInt()+1);
  1569 				s=s.section (",",0,d-2) + ","+ typ+num;
  1570 			} else
  1571 			{
  1572 				// Special case, look at orientation
  1573 				if (selection->getOrientation()==OrientRightOfCenter)
  1574 					num=QString ("%1").arg(num.toUInt()+1);
  1575 				else	
  1576 					num=QString ("%1").arg(num.toUInt()-1);
  1577 				s=typ+num;
  1578 			}	
  1579 
  1580 			if (select (s))
  1581 				// pad to oldDepth, select the first branch for each depth
  1582 				for (i=d;i<oldDepth;i++)
  1583 				{
  1584 					b=select (s);
  1585 					if (b)
  1586 					{	
  1587 						if ( ((BranchObj*)(selection))->countBranches()>0)
  1588 							s+=",bo:0";
  1589 						else	
  1590 							break;
  1591 					} else
  1592 						break;
  1593 				}	
  1594 
  1595 			// try to select the freshly built string
  1596 			found=select(s);
  1597 			d--;
  1598 		}
  1599 		return;
  1600 	}	
  1601 }
  1602 
  1603 void MapEditor::selectPrevBranch()
  1604 {
  1605 	// Decrease number of branch
  1606 	if (selection)
  1607 	{
  1608 		QString s=selection->getSelectString();
  1609 		QString part;
  1610 		QString typ;
  1611 		QString num;
  1612 
  1613 		// Where am I? 
  1614 		part=s.section(",",-1);
  1615 		typ=part.left (3);
  1616 		num=part.right(part.length() - 3);
  1617 
  1618 		s=s.left (s.length() -num.length());
  1619 
  1620 		// Go to next lmo
  1621 		num=QString ("%1").arg(num.toUInt()-1);
  1622 
  1623 		s=s+num;
  1624 		
  1625 		// Try to select this one
  1626 		if (select (s)) return;
  1627 
  1628 		// We have no direct precessor, 
  1629 		// try to decrease the parental number in order to
  1630 		// find a precessor with same depth
  1631 
  1632 		int d=selection->getDepth();
  1633 		int oldDepth=d;
  1634 		int i;
  1635 		bool found=false;
  1636 		bool b;
  1637 		while (!found && d>0)
  1638 		{
  1639 			s=s.section (",",0,d-1);
  1640 			// replace substring of current depth in s with "1"
  1641 			part=s.section(",",-1);
  1642 			typ=part.left (3);
  1643 			num=part.right(part.length() - 3);
  1644 
  1645 			if (d>1)
  1646 			{
  1647 				// decrease number of parent
  1648 				num=QString ("%1").arg(num.toUInt()-1);
  1649 				s=s.section (",",0,d-2) + ","+ typ+num;
  1650 			} else
  1651 			{
  1652 				// Special case, look at orientation
  1653 				if (selection->getOrientation()==OrientRightOfCenter)
  1654 					num=QString ("%1").arg(num.toUInt()-1);
  1655 				else	
  1656 					num=QString ("%1").arg(num.toUInt()+1);
  1657 				s=typ+num;
  1658 			}	
  1659 
  1660 			if (select(s))
  1661 				// pad to oldDepth, select the last branch for each depth
  1662 				for (i=d;i<oldDepth;i++)
  1663 				{
  1664 					b=select (s);
  1665 					if (b)
  1666 						if ( ((BranchObj*)(selection))->countBranches()>0)
  1667 							s+=",bo:"+ QString ("%1").arg( ((BranchObj*)(selection))->countBranches()-1 );
  1668 						else	
  1669 							break;
  1670 					else
  1671 						break;
  1672 				}	
  1673 			
  1674 			// try to select the freshly built string
  1675 			found=select(s);
  1676 			d--;
  1677 		}
  1678 		return;
  1679 	}	
  1680 }
  1681 
  1682 void MapEditor::selectUpperBranch()
  1683 {
  1684 	// Finish open lineEdits
  1685 	if (lineedit) finishedLineEditNoSave();
  1686 
  1687 	if (selection) 
  1688 	{
  1689 		if (typeid(*selection) == typeid(BranchObj))
  1690 		{
  1691 			if (selection->getOrientation()==OrientRightOfCenter)
  1692 				selectPrevBranch();
  1693 			else
  1694 				if (selection->getDepth()==1)
  1695 					selectNextBranch();
  1696 				else
  1697 					selectPrevBranch();
  1698 		}		
  1699 	}
  1700 }
  1701 
  1702 void MapEditor::selectLowerBranch()
  1703 {
  1704 	// Finish open lineEdits
  1705 	if (lineedit) finishedLineEditNoSave();
  1706 
  1707 	if (selection) 
  1708 	{
  1709 		if (typeid(*selection) == typeid(BranchObj))
  1710 		{
  1711 			if (selection->getOrientation()==OrientRightOfCenter)
  1712 				selectNextBranch();
  1713 			else
  1714 				if (selection->getDepth()==1)
  1715 					selectPrevBranch();
  1716 				else
  1717 					selectNextBranch();
  1718 		}		
  1719 	}
  1720 }
  1721 
  1722 
  1723 void MapEditor::selectLeftBranch()
  1724 {
  1725 	// Finish open lineEdits
  1726 	if (lineedit) finishedLineEditNoSave();
  1727 
  1728 	BranchObj* bo;
  1729 	BranchObj* par;
  1730 	if (selection) 
  1731 	{
  1732 		if (typeid(*selection) == typeid(MapCenterObj))
  1733 		{
  1734 			par=  (BranchObj*) (selection);
  1735 			bo=par->getLastSelectedBranch();
  1736 			if (bo)
  1737 			{
  1738 				// Workaround for reselecting on left and right side
  1739 				if (bo->getOrientation()==OrientRightOfCenter)
  1740 				{
  1741 					bo=par->getLastBranch();
  1742 				}	
  1743 				if (bo)
  1744 				{
  1745 					par->unselect();
  1746 					selection=bo;
  1747 					selection->select();
  1748 					adjustCanvasSize();
  1749 					ensureSelectionVisible();
  1750 				}
  1751 			}	
  1752 		} else
  1753 		{
  1754 			par=(BranchObj*)(selection->getParObj());
  1755 			if (selection->getOrientation()==OrientRightOfCenter)
  1756 			{
  1757 				if (typeid(*selection) == typeid(BranchObj) ||
  1758 					typeid(*selection) == typeid(FloatImageObj))
  1759 				{
  1760 					selection->unselect();
  1761 					selection=par;
  1762 					selection->select();
  1763 					adjustCanvasSize();
  1764 					ensureSelectionVisible();
  1765 				}
  1766 			} else
  1767 			{
  1768 				if (typeid(*selection) == typeid(BranchObj) )
  1769 				{
  1770 					bo=((BranchObj*)(selection))->getLastSelectedBranch();
  1771 					if (bo) 
  1772 					{
  1773 						selection->unselect();
  1774 						selection=bo;
  1775 						selection->select();
  1776 						adjustCanvasSize();
  1777 						ensureSelectionVisible();
  1778 					}
  1779 				}
  1780 			}
  1781 		}	
  1782 	}
  1783 }
  1784 
  1785 void MapEditor::selectRightBranch()
  1786 {
  1787 	// Finish open lineEdits
  1788 	if (lineedit) finishedLineEditNoSave();
  1789 
  1790 	BranchObj* bo;
  1791 	BranchObj* par;
  1792 
  1793 	if (selection) 
  1794 	{
  1795 		if (typeid(*selection) == typeid(MapCenterObj))
  1796 		{
  1797 			par=  (BranchObj*) (selection);
  1798 			bo=par->getLastSelectedBranch();
  1799 			if (bo)
  1800 			{
  1801 				// Workaround for relecting on left and right side
  1802 				if (bo->getOrientation()==OrientLeftOfCenter)
  1803 					bo=par->getFirstBranch();
  1804 				if (bo)
  1805 				{
  1806 					par->unselect();
  1807 					selection=bo;
  1808 					selection->select();
  1809 					ensureSelectionVisible();
  1810 				}
  1811 			}
  1812 		} else
  1813 		{
  1814 			par=(BranchObj*)(selection->getParObj());
  1815 			if (selection->getOrientation()==OrientLeftOfCenter)
  1816 			{
  1817 				if (typeid(*selection) == typeid(BranchObj) ||
  1818 					typeid(*selection) == typeid(FloatImageObj))
  1819 				{
  1820 					selection->unselect();
  1821 					selection=par;
  1822 					selection->select();
  1823 					adjustCanvasSize();
  1824 					ensureSelectionVisible();
  1825 				}
  1826 			} else
  1827 			{
  1828 				if (typeid(*selection) == typeid(BranchObj) )
  1829 				{
  1830 					bo=((BranchObj*)(selection))->getLastSelectedBranch();
  1831 					if (bo) 
  1832 					{
  1833 						selection->unselect();
  1834 						selection=bo;
  1835 						selection->select();
  1836 						adjustCanvasSize();
  1837 						ensureSelectionVisible();
  1838 					}
  1839 				}
  1840 			}
  1841 		}
  1842 	}
  1843 }
  1844 
  1845 void MapEditor::selectFirstBranch()
  1846 {
  1847 	// Finish open lineEdits
  1848 	if (lineedit) finishedLineEditNoSave();
  1849 
  1850 	BranchObj *bo1;
  1851 	BranchObj *bo2;
  1852 	BranchObj* par;
  1853 	if (selection) {
  1854 		if (typeid(*selection) == typeid(BranchObj))
  1855 		{
  1856 			bo1=  (BranchObj*) (selection);
  1857 			par=(BranchObj*)(bo1->getParObj());
  1858 			bo2=par->getFirstBranch();
  1859 			if (bo2) {
  1860 				bo1->unselect();
  1861 				selection=bo2;
  1862 				selection->select();
  1863 				ensureSelectionVisible();
  1864 			}
  1865 		}		
  1866 		adjustCanvasSize();
  1867 	}
  1868 }
  1869 
  1870 void MapEditor::selectLastBranch()
  1871 {
  1872 	// Finish open lineEdits
  1873 	if (lineedit) finishedLineEditNoSave();
  1874 
  1875 	BranchObj *bo1;
  1876 	BranchObj *bo2;
  1877 	BranchObj* par;
  1878 	if (selection) {
  1879 		if (typeid(*selection) == typeid(BranchObj))
  1880 		{
  1881 			bo1=  (BranchObj*) (selection);
  1882 			par=(BranchObj*)(bo1->getParObj());
  1883 			bo2=par->getLastBranch();
  1884 			if (bo2) {
  1885 				bo1->unselect();
  1886 				selection=bo2;
  1887 				selection->select();
  1888 				ensureSelectionVisible();
  1889 			}
  1890 		}		
  1891 		adjustCanvasSize();
  1892 	}
  1893 }
  1894 
  1895 void MapEditor::setColor(QColor c)
  1896 {
  1897 	actColor=c;
  1898 }
  1899 
  1900 void MapEditor::selectBackgroundColor()
  1901 {
  1902 	// Finish open lineEdits
  1903 	if (lineedit) finishedLineEditNoSave();
  1904 
  1905 	QColor col = QColorDialog::getColor( mapCanvas->backgroundColor(), this );
  1906 	if ( !col.isValid() ) return;
  1907 	setBackgroundColor( col );
  1908 	setChanged();
  1909 }
  1910 
  1911 void MapEditor::setBackgroundColor(QColor c)
  1912 {
  1913 	mapCanvas->setBackgroundColor (c);
  1914 }
  1915 
  1916 QColor MapEditor::pickColor()
  1917 {
  1918 	if (selection) 
  1919 	{
  1920 		if (typeid(*selection) == typeid(BranchObj) ||
  1921 			typeid(*selection) == typeid(MapCenterObj))
  1922 		{
  1923 			BranchObj *bo=(BranchObj*)(selection);
  1924 			actColor=bo->getColor(); 
  1925 		}    
  1926 	}
  1927 	return actColor;
  1928 }
  1929 
  1930 void MapEditor::colorItem()
  1931 {
  1932 	if (selection) 
  1933 	{
  1934 		if (typeid(*selection) == typeid(BranchObj) ||
  1935 			typeid(*selection) == typeid(MapCenterObj))
  1936 		{
  1937 			setChanged();
  1938 			saveState(PartOfMap,selection);
  1939 			BranchObj *bo=(BranchObj*)(selection);
  1940 			bo->setColor(actColor, false); // color links, color childs
  1941 		}    
  1942 	}
  1943 }
  1944 
  1945 void MapEditor::colorBranch()
  1946 {
  1947 	if (selection) 
  1948 	{
  1949 		if (typeid(*selection) == typeid(BranchObj) ||
  1950 			typeid(*selection) == typeid(MapCenterObj))
  1951 		{
  1952 			setChanged();
  1953 			saveState(PartOfMap,selection);
  1954 			BranchObj *bo=(BranchObj*)(selection);
  1955 			bo->setColor(actColor, true); // color links, color childs
  1956 		}    
  1957 	}
  1958 }
  1959 
  1960 
  1961 void MapEditor::toggleStandardFlag(QString f)
  1962 {
  1963 	if (selection)
  1964 	{
  1965 		setChanged();
  1966 		saveState(PartOfMap,selection);	
  1967 		((BranchObj*)(selection))->toggleStandardFlag (f);
  1968 	}	
  1969 }
  1970 
  1971 void MapEditor::setViewCenter()
  1972 {
  1973 	// transform to CanvasView Coord:
  1974 	QPoint p=worldMatrix().map(movingCenter);
  1975 	center ( p.x(), p.y());
  1976 }
  1977 
  1978 
  1979 BranchObj* MapEditor::findText (QString s, bool cs)
  1980 {
  1981 	if (!itFind) 
  1982 	{	// Nothing found or new find process
  1983 		if (EOFind)
  1984 			// nothing found, start again
  1985 			EOFind=false;
  1986 		itFind=mapCenter->first();
  1987 	}	
  1988 	bool searching=true;
  1989 	bool foundNote=false;
  1990 	while (searching && !EOFind)
  1991 	{
  1992 		if (itFind)
  1993 		{
  1994 			// Searching in Note
  1995 			if (itFind->getNote().contains(s,cs))
  1996 			{
  1997 				if (selection!=itFind) 
  1998 				{
  1999 					if (selection) ((BranchObj*)(selection))->unselect();
  2000 					selection=itFind;
  2001 					selection->select();
  2002 					adjustCanvasSize();
  2003 					ensureSelectionVisible();
  2004 				}
  2005 				if (textEditor->findText(s,cs)) 
  2006 				{
  2007 					searching=false;
  2008 					foundNote=true;
  2009 				}	
  2010 			}
  2011 			// Searching in Heading
  2012 			if (searching && itFind->getHeading().contains (s,cs) ) 
  2013 			{
  2014 				if (selection) ((BranchObj*)(selection))->unselect();
  2015 				selection=itFind;
  2016 				selection->select();
  2017 				adjustCanvasSize();
  2018 				ensureSelectionVisible();
  2019 				searching=false;
  2020 			}
  2021 		}	
  2022 		if (!foundNote)
  2023 		{
  2024 			itFind=itFind->next();
  2025 			if (!itFind) EOFind=true;
  2026 		}
  2027 	}	
  2028 	if (!searching)
  2029 	{
  2030 		adjustCanvasSize();
  2031 		return (BranchObj*)(selection);
  2032 	}	else
  2033 		return NULL;
  2034 }
  2035 
  2036 void MapEditor::findReset()
  2037 {	// Necessary if text to find changes during a find process
  2038 	itFind=NULL;
  2039 	EOFind=false;
  2040 }
  2041 
  2042 void MapEditor::openURL()
  2043 {
  2044 	if (selection )
  2045 	{
  2046 		if (typeid(*selection) == typeid(BranchObj) ||
  2047 			typeid(*selection) == typeid(MapCenterObj))
  2048 		{
  2049 			QString url=((BranchObj*)(selection))->getURL();
  2050 
  2051 			QProcess *proc = new QProcess( this );
  2052 
  2053 #if !defined(Q_OS_MACX)
  2054 			proc->addArgument( settings.readEntry("/vym/mainwindow/readerURL","konqueror" ));
  2055 #else			
  2056 			proc->addArgument( settings.readEntry("/vym/mainwindow/readerURL",
  2057 				"/Applications/Safari.app/Contents/MacOS/Safari" ));
  2058 #endif			
  2059 
  2060 			proc->addArgument( url);
  2061 
  2062 			if ( !proc->start() ) 
  2063 				// error handling
  2064 				if (mainWindow->settingsURL() ) 
  2065 					openURL();
  2066 		}	
  2067 	}	
  2068 }
  2069 
  2070 void MapEditor::editURL()
  2071 {
  2072 	if (selection && (typeid(*selection) == typeid(BranchObj) ||
  2073 			typeid(*selection) == typeid(MapCenterObj)) )
  2074 	{		
  2075 		bool ok;
  2076 		QString text = QInputDialog::getText(
  2077 				"VYM", tr("Enter URL:"), QLineEdit::Normal,
  2078 				((BranchObj*)(selection))->getURL(), &ok, this );
  2079 		if ( ok) 
  2080 		{
  2081 			// user entered something and pressed OK
  2082 			((BranchObj*)(selection))->setURL (text);
  2083 			updateActions();
  2084 			setChanged();
  2085 		}	
  2086 	}
  2087 }
  2088 
  2089 void MapEditor::editHeading2URL()
  2090 {
  2091 	if (selection && (typeid(*selection) == typeid(BranchObj) ||
  2092 			typeid(*selection) == typeid(MapCenterObj)) )
  2093 	{		
  2094 		BranchObj *b=(BranchObj*)(selection);
  2095 		b->setURL (b->getHeading());
  2096 		updateActions();
  2097 		setChanged();
  2098 	}
  2099 }	
  2100 
  2101 void MapEditor::editBugzilla2URL()
  2102 {
  2103 	if (selection && (typeid(*selection) == typeid(BranchObj) ||
  2104 			typeid(*selection) == typeid(MapCenterObj)) )
  2105 	{		
  2106 		BranchObj *b=(BranchObj*)(selection);
  2107 		b->setURL ("http://bugzilla.suse.de/show_bug.cgi?id="+b->getHeading());
  2108 		b->setURL ("https://bugzilla.innerweb.novell.com/show_bug.cgi?id="+b->getHeading());
  2109 		updateActions();
  2110 		setChanged();
  2111 	}
  2112 }	
  2113 
  2114 void MapEditor::editVymLink()
  2115 {
  2116 	if (selection && (typeid(*selection) == typeid(BranchObj) ||
  2117 			typeid(*selection) == typeid(MapCenterObj)) )
  2118 	{		
  2119 		QFileDialog *fd=new QFileDialog( this,tr("VYM - Link to another map"));
  2120 		fd->addFilter (QString (tr("vym map") + " (*.vym)"));
  2121 		fd->setCaption(tr("VYM - Link to another map"));
  2122 		if (! ((BranchObj*)(selection))->getVymLink().isEmpty() )
  2123 			fd->setSelection( ((BranchObj*)(selection))->getVymLink() );
  2124 		fd->show();
  2125 
  2126 		QString fn;
  2127 		if ( fd->exec() == QDialog::Accepted )
  2128 			((BranchObj*)(selection))->setVymLink (fd->selectedFile() );
  2129 		updateActions();
  2130 		mapCenter->reposition();
  2131 		adjustCanvasSize();
  2132 		canvas()->update();
  2133 		setChanged();
  2134 	}
  2135 }
  2136 
  2137 void MapEditor::deleteVymLink()
  2138 {
  2139 	if (selection && (typeid(*selection) == typeid(BranchObj) ||
  2140 			typeid(*selection) == typeid(MapCenterObj)) )
  2141 	{		
  2142 		((BranchObj*)(selection))->setVymLink ("" );
  2143 		updateActions();
  2144 		mapCenter->reposition();
  2145 		adjustCanvasSize();
  2146 		canvas()->update();
  2147 		setChanged();
  2148 	}
  2149 }
  2150 
  2151 QString MapEditor::getVymLink()
  2152 {
  2153 	if (selection && (typeid(*selection) == typeid(BranchObj) ||
  2154 			typeid(*selection) == typeid(MapCenterObj)) )
  2155 	{		
  2156 		return ((BranchObj*)(selection))->getVymLink();
  2157 	}
  2158 	return "";
  2159 	
  2160 }
  2161 
  2162 void MapEditor::editMapInfo()
  2163 {
  2164 	ExtraInfoDialog dia;
  2165 	dia.setMapName (getFileName() );
  2166 	dia.setAuthor (mapCenter->getAuthor() );
  2167 	dia.setComment(mapCenter->getComment() );
  2168 
  2169 	// Calc some stats
  2170 	QString stats;
  2171     int i=0;
  2172     QCanvasItemList l=canvas()->allItems();
  2173     for (QCanvasItemList::Iterator it=l.begin(); it!=l.end(); ++it) 
  2174         i++;
  2175     stats+=QString ("%1 items on canvas\n").arg (i,6);
  2176 
  2177 	uint b=0;
  2178 	uint f=0;
  2179 	uint n=0;
  2180 	BranchObj *bo;
  2181 	bo=mapCenter->first();
  2182 	while (bo) 
  2183 	{
  2184 		if (!bo->getNote().isEmpty() ) n++;
  2185 		f+= bo->countFloatImages();
  2186 		b++;
  2187 		bo=bo->next();
  2188 	}
  2189     stats+=QString ("%1 branches\n").arg (b-1,6);
  2190     stats+=QString ("%1 notes\n").arg (n,6);
  2191     stats+=QString ("%1 images\n").arg (f,6);
  2192 	dia.setStats (stats);
  2193 
  2194 	// Finally show dialog
  2195 	if (dia.exec() == QDialog::Accepted)
  2196 	{
  2197 		mapCenter->setAuthor (dia.getAuthor() );
  2198 		mapCenter->setComment (dia.getComment() );
  2199 		setChanged();
  2200 	}
  2201 }
  2202 
  2203 void MapEditor::updateActions()
  2204 {
  2205 	if (getLinkColorHint()==HeadingColor) 
  2206 		actionFormatLinkColorHint->setOn(true);
  2207 	else	
  2208 		actionFormatLinkColorHint->setOn(false);
  2209 
  2210 	switch (linkstyle)
  2211 	{
  2212 		case StyleLine: 
  2213 			actionFormatLinkStyleLine->setOn(true);
  2214 			break;
  2215 		case StyleParabel:
  2216 			actionFormatLinkStyleParabel->setOn(true);
  2217 			break;
  2218 		case StylePolyLine:	
  2219 			actionFormatLinkStylePolyLine->setOn(true);
  2220 			break;
  2221 		case StylePolyParabel:	
  2222 			actionFormatLinkStylePolyParabel->setOn(true);
  2223 			break;
  2224 		default:
  2225 			break;
  2226 	}	
  2227 
  2228 	QPixmap pix( 16, 16 );
  2229     pix.fill( mapCanvas->backgroundColor() );
  2230     actionFormatBackColor->setIconSet( pix );
  2231     pix.fill( deflinkcolor );
  2232     actionFormatLinkColor->setIconSet( pix );
  2233 
  2234 	actionEditUndo->setEnabled( mapChanged );
  2235 	actionFileSave->setEnabled( mapUnsaved );
  2236 
  2237 	if (selection)
  2238 	{
  2239 		if ( (typeid(*selection) == typeid(BranchObj)) || 
  2240 			(typeid(*selection) == typeid(MapCenterObj))  )
  2241 		{
  2242 			BranchObj *bo=(BranchObj*)(selection);
  2243 			// Take care of links
  2244 			if (bo->countLinks()==0)
  2245 			{
  2246 				branchLinksContextMenu->clear();
  2247 				branchLinksContextMenu->insertItem ("No link available");
  2248 				branchLinksContextMenuDup->clear();
  2249 				branchLinksContextMenuDup->insertItem ("No link available");
  2250 				
  2251 			} else
  2252 			{
  2253 				BranchObj *bot;
  2254 				QString s;
  2255 				branchLinksContextMenu->clear();
  2256 				branchLinksContextMenuDup->clear();
  2257 				for (int i=0; i<=bo->countLinks();i++)
  2258 				{
  2259 					bot=bo->linkTargetAt(i);
  2260 					if (bot)
  2261 					{
  2262 						s=bot->getHeading();
  2263 						if (s.length()>25)
  2264 							s=s.left(25)+"...";
  2265 						branchLinksContextMenu->insertItem (s);
  2266 						branchLinksContextMenuDup->insertItem (s);
  2267 					}	
  2268 				}
  2269 			}
  2270 
  2271 			standardFlagsDefault->setEnabled (true);
  2272 
  2273 			if ( bo->getURL().isEmpty() )
  2274 				actionEditOpenURL->setEnabled (false);
  2275 			else	
  2276 				actionEditOpenURL->setEnabled (true);
  2277 			actionEditURL->setEnabled (true);	
  2278 			actionEditHeading2URL->setEnabled (true);	
  2279 			actionEditBugzilla2URL->setEnabled (true);	
  2280 
  2281 			if ( bo->getVymLink().isEmpty() )
  2282 			{
  2283 				actionEditOpenVymLink->setEnabled (false);
  2284 				actionEditDeleteVymLink->setEnabled (false);
  2285 			} else	
  2286 			{
  2287 				actionEditOpenVymLink->setEnabled (true);
  2288 				actionEditDeleteVymLink->setEnabled (true);
  2289 			}	
  2290 			actionEditVymLink->setEnabled (true);	
  2291 
  2292 			actionEditCopy->setEnabled (true);	
  2293 			actionEditCut->setEnabled (true);	
  2294 			actionEditPaste->setEnabled (true);	
  2295 			actionEditMoveUp->setEnabled (true);	
  2296 			actionEditMoveDown->setEnabled (true);	
  2297 			actionEditToggleScroll->setEnabled (true);	
  2298 			actionEditHeading->setEnabled (true);
  2299 			actionEditDelete->setEnabled (true);
  2300 			actionEditAddBranch->setEnabled (true);
  2301 			actionEditAddBranchAbove->setEnabled (true);
  2302 			actionEditAddBranchBelow->setEnabled (true);
  2303 			actionEditImportAdd->setEnabled (true);
  2304 			actionEditImportReplace->setEnabled (true);
  2305 			actionEditSaveBranch->setEnabled (true);
  2306 			actionEditSelectFirst->setEnabled (true);
  2307 			actionEditSelectLast->setEnabled (true);
  2308 			actionEditToggleFloatExport->setEnabled (false);
  2309 			actionFormatPickColor->setEnabled (true);
  2310 			actionFormatColorBranch->setEnabled (true);
  2311 			actionFormatColorSubtree->setEnabled (true);
  2312 			switch (selection->getFrameType())
  2313 			{
  2314 				case NoFrame: 
  2315 					actionFormatFrameNone->setOn(true);
  2316 					break;
  2317 				case Rectangle:
  2318 					actionFormatFrameRectangle->setOn(true);
  2319 					break;
  2320 				default:
  2321 					break;
  2322 			}	
  2323 		}
  2324 		if ( (typeid(*selection) == typeid(FloatImageObj)) )
  2325 		{
  2326 			standardFlagsDefault->setEnabled (false);
  2327 
  2328 			actionEditOpenURL->setEnabled (false);
  2329 			actionEditURL->setEnabled (false);	
  2330 			actionEditHeading2URL->setEnabled (false);	
  2331 			actionEditBugzilla2URL->setEnabled (false);	
  2332 			actionEditOpenVymLink->setEnabled (false);
  2333 			actionEditVymLink->setEnabled (false);	
  2334 			actionEditDeleteVymLink->setEnabled (false);	
  2335 
  2336 			actionEditCopy->setEnabled (true);
  2337 			actionEditCut->setEnabled (true);	
  2338 			actionEditPaste->setEnabled (false);	//FIXME
  2339 			actionEditMoveUp->setEnabled (false);	
  2340 			actionEditMoveDown->setEnabled (false);	
  2341 			actionEditToggleScroll->setEnabled (false);	
  2342 			actionEditHeading->setEnabled (false);
  2343 			actionEditDelete->setEnabled (true);
  2344 			actionEditAddBranch->setEnabled (false);
  2345 			actionEditAddBranchAbove->setEnabled (false);
  2346 			actionEditAddBranchBelow->setEnabled (false);
  2347 			actionEditImportAdd->setEnabled (false);
  2348 			actionEditSaveBranch->setEnabled (false);
  2349 			actionEditImportReplace->setEnabled (false);
  2350 			actionEditSelectFirst->setEnabled (false);
  2351 			actionEditSelectLast->setEnabled (false);
  2352 			actionEditToggleFloatExport->setOn
  2353 				( ((FloatImageObj*)(selection))->getFloatExport() );
  2354 			actionFormatPickColor->setEnabled (false);
  2355 			actionFormatColorBranch->setEnabled (false);
  2356 			actionFormatColorSubtree->setEnabled (false);
  2357 		}
  2358 
  2359 	} else
  2360 	{
  2361 		standardFlagsDefault->setEnabled (false);
  2362 
  2363 		actionEditCopy->setEnabled (false);	
  2364 		actionEditCut->setEnabled (false);	
  2365 		actionEditPaste->setEnabled (false);	
  2366 		actionEditMoveUp->setEnabled (false);	
  2367 		actionEditMoveDown->setEnabled (false);	
  2368 		actionEditToggleScroll->setEnabled (false);	
  2369 		actionEditOpenURL->setEnabled (false);
  2370 		actionEditURL->setEnabled (false);	
  2371 		actionEditOpenVymLink->setEnabled (false);
  2372 		actionEditVymLink->setEnabled (false);	
  2373 		actionEditDeleteVymLink->setEnabled (false);	
  2374 		actionEditHeading2URL->setEnabled (false);	
  2375 		actionEditBugzilla2URL->setEnabled (false);	
  2376 		actionEditHeading->setEnabled (false);
  2377 		actionEditDelete->setEnabled (false);
  2378 		actionEditAddBranch->setEnabled (false);
  2379 		actionEditAddBranchAbove->setEnabled (false);
  2380 		actionEditAddBranchBelow->setEnabled (false);
  2381 		actionEditSaveBranch->setEnabled (false);
  2382 		actionEditImportReplace->setEnabled (false);
  2383 		actionEditSelectFirst->setEnabled (false);
  2384 		actionEditSelectLast->setEnabled (false);
  2385 		actionEditToggleFloatExport->setEnabled (false);
  2386 		actionFormatPickColor->setEnabled (false);
  2387 		actionFormatColorBranch->setEnabled (false);
  2388 		actionFormatColorSubtree->setEnabled (false);
  2389 	}	
  2390 }
  2391 
  2392 void MapEditor::setLinkStyle (LinkStyle ls)
  2393 {
  2394 	linkstyle=ls;
  2395 
  2396 	BranchObj *bo;
  2397 	bo=mapCenter->first();
  2398 	bo=bo->next();
  2399 	while (bo) 
  2400 	{
  2401 		bo->setLinkStyle(bo->getDefLinkStyle());
  2402 		bo=bo->next();
  2403 	}
  2404 	//setChanged();
  2405 	//saveState();
  2406 }
  2407 
  2408 LinkStyle MapEditor::getLinkStyle ()
  2409 {
  2410 	return linkstyle;
  2411 }	
  2412 
  2413 void MapEditor::setLinkColor(QColor c)
  2414 {
  2415 	deflinkcolor=c;
  2416 	updateActions();
  2417 }
  2418 
  2419 void MapEditor::setLinkColorHint()
  2420 {
  2421 	// called from setLinkColorHint(lch) or at end of parse
  2422 	BranchObj *bo;
  2423 	bo=mapCenter->first();
  2424 	while (bo) 
  2425 	{
  2426 		bo->setLinkColor();
  2427 		bo=bo->next();
  2428 	}
  2429 }
  2430 
  2431 void MapEditor::setLinkColorHint(LinkColorHint lch)
  2432 {
  2433 	linkcolorhint=lch;
  2434 	setLinkColorHint();
  2435 }
  2436 
  2437 void MapEditor::toggleLinkColorHint()
  2438 {
  2439 	if (linkcolorhint==HeadingColor)
  2440 		linkcolorhint=DefaultColor;
  2441 	else	
  2442 		linkcolorhint=HeadingColor;
  2443 	BranchObj *bo;
  2444 	bo=mapCenter->first();
  2445 	while (bo) 
  2446 	{
  2447 		bo->setLinkColor();
  2448 		bo=bo->next();
  2449 	}
  2450 }
  2451 
  2452 LinkColorHint MapEditor::getLinkColorHint()
  2453 {
  2454 	return linkcolorhint;
  2455 }
  2456 
  2457 QColor MapEditor::getDefLinkColor()
  2458 {
  2459 	return deflinkcolor;
  2460 }
  2461 
  2462 void MapEditor::selectLinkColor()
  2463 {
  2464 	// Finish open lineEdits
  2465 	if (lineedit) finishedLineEditNoSave();
  2466 
  2467 	QColor col = QColorDialog::getColor( deflinkcolor, this );
  2468 	if ( !col.isValid() ) return;
  2469 	setLinkColor( col );
  2470 	setChanged();
  2471 }
  2472 
  2473 void MapEditor::toggleScroll()
  2474 {
  2475 	if (selection && (typeid(*selection) == typeid(BranchObj)) )
  2476 	{
  2477 		BranchObj *bo=((BranchObj*)(selection));
  2478 		if (bo->countBranches()==0) return;
  2479 		if (bo->getDepth()==0) return;
  2480 		setChanged();
  2481 		saveState(PartOfMap,selection);
  2482 		bo->toggleScroll();
  2483 		adjustCanvasSize();
  2484 		canvas()->update();
  2485 	}
  2486 }
  2487 
  2488 void MapEditor::unScrollAll()
  2489 {
  2490 	BranchObj *bo;
  2491 	bo=mapCenter->first();
  2492 	while (bo) 
  2493 	{
  2494 		if (bo->isScrolled()) bo->toggleScroll();
  2495 		bo=bo->next();
  2496 	}
  2497 }
  2498 
  2499 void MapEditor::loadFloatImage ()
  2500 {
  2501 	if (selection && 
  2502 		(typeid(*selection) == typeid(BranchObj)) || 
  2503 		(typeid(*selection) == typeid(MapCenterObj))  )
  2504 	{
  2505 		BranchObj *bo=((BranchObj*)(selection));
  2506 
  2507 		QFileDialog *fd=new QFileDialog( this,tr("vym - load image"));
  2508 		fd->addFilter (QString (tr("Images") + " (*.png *.bmp *.xbm *.jpg *.png *.xpm *.gif *.pnm)"));
  2509 		ImagePreview *p =new ImagePreview (fd);
  2510 		fd->setContentsPreviewEnabled( TRUE );
  2511 		fd->setContentsPreview( p, p );
  2512 		fd->setPreviewMode( QFileDialog::Contents );
  2513 		fd->setCaption(tr("vym - Load image"));
  2514 		fd->setDir (lastImageDir);
  2515 		fd->show();
  2516 
  2517 		QString fn;
  2518 		if ( fd->exec() == QDialog::Accepted )
  2519 		{
  2520 			setChanged();
  2521 			saveState(PartOfMap,selection);
  2522 			QString fn=fd->selectedFile();
  2523 			lastImageDir=fn.left(fn.findRev ("/"));
  2524 			bo->addFloatImage();
  2525 			// FIXME check if load was successful
  2526 			bo->getLastFloatImage()->load(fn);
  2527 			bo->getLastFloatImage()->setOriginalFilename(fn);
  2528 			mapCenter->reposition();
  2529 			adjustCanvasSize();
  2530 			canvas()->update();
  2531 		}
  2532 	}
  2533 }
  2534 
  2535 void MapEditor::saveFloatImage (int item)
  2536 {
  2537 	if (selection && 
  2538 		(typeid(*selection) == typeid(FloatImageObj)) )
  2539 	{
  2540 		FloatImageObj *fio=((FloatImageObj*)(selection));
  2541 		const char* fmt = saveImageFormatMenu->text(item);
  2542 
  2543 		QFileDialog *fd=new QFileDialog( this, tr("vym - save image as") + fmt);
  2544 		fd->addFilter ("PNG (*.png)");
  2545 		fd->addFilter ("BMP (*.bmp)");
  2546 		fd->addFilter ("XBM (*.xbm)");
  2547 		fd->addFilter ("JPG (*.jpg)");
  2548 		fd->addFilter ("XPM (*.xpm)");
  2549 		fd->addFilter ("GIF (*.gif)");
  2550 		fd->addFilter ("PNM (*.pnm)");
  2551 		fd->addFilter (QString (tr("Images") + " (*.png *.bmp *.xbm *.jpg *.png *.xpm *.gif *.pnm)"));
  2552 		fd->setCaption(tr("vym - Save image as ") + fmt);
  2553 		fd->setMode( QFileDialog::AnyFile );
  2554 		fd->setSelection (fio->getOriginalFilename());
  2555 		fd->show();
  2556 
  2557 		QString fn;
  2558 		if ( fd->exec() == QDialog::Accepted )
  2559 		{
  2560 			if (QFile (fd->selectedFile()).exists() )
  2561 			{
  2562 				QMessageBox mb( "VYM",
  2563 					tr("The file ") + fd->selectedFile() + 
  2564 					tr(" exists already. "
  2565 					"Do you want to overwrite it?"),
  2566 				QMessageBox::Warning,
  2567 				QMessageBox::Yes | QMessageBox::Default,
  2568 				QMessageBox::Cancel | QMessageBox::Escape,
  2569 				QMessageBox::QMessageBox::NoButton );
  2570 
  2571 				mb.setButtonText( QMessageBox::Yes, tr("Overwrite") );
  2572 				mb.setButtonText( QMessageBox::No, tr("Cancel"));
  2573 				switch( mb.exec() ) 
  2574 				{
  2575 					case QMessageBox::Yes:
  2576 						// save 
  2577 						break;;
  2578 					case QMessageBox::Cancel:
  2579 						// do nothing
  2580 						return;
  2581 						break;
  2582 				}
  2583 			}
  2584 			fio->save (fd->selectedFile(),fmt);
  2585 		}
  2586 	}
  2587 }
  2588 
  2589 void MapEditor::toggleFloatExport()
  2590 {
  2591 	if (selection && 
  2592 		(typeid(*selection) == typeid(FloatImageObj))|| 
  2593 		(typeid(*selection) == typeid(FloatObj)) )
  2594 	{
  2595 		FloatImageObj *fio=((FloatImageObj*)(selection));
  2596 		fio->setFloatExport (actionEditToggleFloatExport->isOn() );
  2597 	}
  2598 }
  2599 
  2600 void MapEditor::setFrame(const FrameType &t)
  2601 {
  2602 	if (selection && 
  2603 		(typeid(*selection) == typeid(BranchObj)) || 
  2604 		(typeid(*selection) == typeid(MapCenterObj))  )
  2605 	{
  2606 		selection->setFrameType (t);
  2607 		mapCenter->reposition();
  2608 		selection->updateLink();
  2609 	}
  2610 }
  2611 
  2612 void MapEditor::importDir(BranchObj *dst, QDir d)
  2613 {
  2614 	if (selection && 
  2615 		(typeid(*selection) == typeid(BranchObj)) || 
  2616 		(typeid(*selection) == typeid(MapCenterObj))  )
  2617 	{
  2618 		BranchObj *bo;
  2619 		
  2620 		// Traverse directories
  2621 		d.setFilter( QDir::Dirs| QDir::Hidden | QDir::NoSymLinks );
  2622 		const QFileInfoList *dirlist = d.entryInfoList();
  2623 		QFileInfoListIterator itdir( *dirlist );
  2624 		QFileInfo *fi;
  2625 
  2626 		while ( (fi = itdir.current()) != 0 ) 
  2627 		{
  2628 			if (fi->fileName() != "." && fi->fileName() != ".." )
  2629 			{
  2630 				dst->addBranch();
  2631 				bo=dst->getLastBranch();
  2632 				bo->setHeading (fi->fileName() );
  2633 				bo->setColor (QColor("blue"),false);
  2634 				bo->toggleScroll();
  2635 				if ( !d.cd(fi->fileName()) ) 
  2636 					QMessageBox::critical (0,tr("Critical Import Error"),tr("Cannot find the directory"));
  2637 				else 
  2638 				{
  2639 					importDir (bo,d);
  2640 					d.cdUp();
  2641 				}
  2642 			}	
  2643 			++itdir;
  2644 		}		
  2645 		// Traverse files
  2646 		d.setFilter( QDir::Files| QDir::Hidden | QDir::NoSymLinks );
  2647 		const QFileInfoList *filelist = d.entryInfoList();
  2648 		QFileInfoListIterator itfile( *filelist );
  2649 
  2650 		while ( (fi = itfile.current()) != 0 ) 
  2651 		{
  2652 			dst->addBranch();
  2653 			bo=dst->getLastBranch();
  2654 			bo->setHeading (fi->fileName() );
  2655 			bo->setColor (QColor("black"),false);
  2656 			++itfile;
  2657 		}	
  2658 	}		
  2659 }
  2660 
  2661 void MapEditor::importDir()
  2662 {
  2663 	if (selection && 
  2664 		(typeid(*selection) == typeid(BranchObj)) || 
  2665 		(typeid(*selection) == typeid(MapCenterObj))  )
  2666 	{
  2667 		QFileDialog *fd=new QFileDialog( this,tr("VYM - Choose directory structur to import"));
  2668 		fd->setMode (QFileDialog::DirectoryOnly);
  2669 		fd->addFilter (QString (tr("vym map") + " (*.vym)"));
  2670 		fd->setCaption(tr("VYM - Choose directory structur to import"));
  2671 		fd->show();
  2672 
  2673 		QString fn;
  2674 		if ( fd->exec() == QDialog::Accepted )
  2675 		{
  2676 			BranchObj *bo=((BranchObj*)(selection));
  2677 			importDir (bo,QDir(fd->selectedFile()) );
  2678 			mapCenter->reposition();
  2679 			adjustCanvasSize();
  2680 			canvas()->update();
  2681 		}
  2682 	}	
  2683 }
  2684 
  2685 void MapEditor::followLink(int i)
  2686 {
  2687 	BranchObj *bo=((BranchObj*)(selection))->linkTargetAt(i);
  2688 	if (bo) 
  2689 	{
  2690 		selection->unselect();
  2691 		selection=bo;
  2692 		selection->select();
  2693 		ensureSelectionVisible();
  2694 	}
  2695 }
  2696 
  2697 void MapEditor::editLink(int i)
  2698 {
  2699 	BranchObj *bo=((BranchObj*)(selection))->linkTargetAt(i);
  2700 	if (bo) 
  2701 	{
  2702 		EditLinkDialog dia;
  2703 		if (dia.exec() == QDialog::Accepted)
  2704 		{
  2705 			if (dia.deleteLink())
  2706 				((BranchObj*)(selection))->deleteLinkAt(i);
  2707 			setChanged();
  2708 		}
  2709 	}	
  2710 }
  2711 
  2712 void MapEditor::testFunction()
  2713 {
  2714 	cout << "MapEditor::testFunction() called\n";
  2715 	if (selection  &&  
  2716 		 (typeid(*selection) == typeid(BranchObj) || 
  2717 		  typeid(*selection) == typeid(MapCenterObj) ) ) 
  2718 	{
  2719 		BranchObj *bo=(BranchObj*)(selection);
  2720 		QPoint p = worldMatrix().map(QPoint (bo->x(),bo->y()));
  2721 		//	lineedit->setGeometry(p.x()-contentsX(),p.y()-contentsY(),200,25);
  2722 		QPopupMenu *popupLinks=new QPopupMenu (this);
  2723 		if (bo->countLinks()==0)
  2724 		{
  2725 			popupLinks->clear();
  2726 			popupLinks->insertItem ("No link available");
  2727 			
  2728 		} else
  2729 		{
  2730 			BranchObj *bot;
  2731 			QString s;
  2732 			popupLinks->clear();
  2733 			for (int i=0; i<=bo->countLinks();i++)
  2734 			{
  2735 				bot=bo->linkTargetAt(i);
  2736 				if (bot)
  2737 				{
  2738 					s=bot->getHeading();
  2739 					if (s.length()>25)
  2740 						s=s.left(25)+"...";
  2741 					popupLinks->insertItem (s);
  2742 				}	
  2743 			}
  2744 		}
  2745 		popupLinks->move(p);
  2746 		popupLinks->exec();
  2747 		
  2748 	}
  2749 }
  2750 
  2751 void MapEditor::ensureSelectionVisible()
  2752 {
  2753 	LinkableMapObj* lmo= dynamic_cast <LinkableMapObj*> (selection);
  2754 	QPoint p;
  2755 	if (selection->getOrientation() == OrientLeftOfCenter)
  2756 		p= worldMatrix().map(QPoint (lmo->x(),lmo->y()));
  2757 	else	
  2758 		p= worldMatrix().map(QPoint (lmo->x()+lmo->width(),lmo->y()+lmo->height()));
  2759 	ensureVisible (p.x(), p.y() );
  2760 
  2761 }
  2762 
  2763 void MapEditor::updateViewCenter()
  2764 {
  2765 	// Update movingCenter, so that we can zoom comfortably later
  2766 	QRect rc = QRect( contentsX(), contentsY(),
  2767 				  visibleWidth(), visibleHeight() );
  2768 	QRect canvasRect = inverseWorldMatrix().mapRect(rc);
  2769 	movingCenter.setX((canvasRect.right() + canvasRect.left())/2);
  2770 	movingCenter.setY((canvasRect.top() + canvasRect.bottom())/2);
  2771 }
  2772 
  2773 void MapEditor::contentsContextMenuEvent ( QContextMenuEvent * e )
  2774 {
  2775 	// Lineedits are already closed by preceding
  2776 	// mouseEvent, we don't need to close here.
  2777 
  2778     QPoint p = inverseWorldMatrix().map(e->pos());
  2779     LinkableMapObj* lmo=mapCenter->findMapObj(p, NULL);
  2780 	
  2781     if (lmo) 
  2782 	{	// MapObj was found
  2783 		if (selection != lmo)
  2784 		{
  2785 			// select the MapObj
  2786 			if (selection) selection->unselect();
  2787 			selection=lmo;
  2788 			selection->select();
  2789 			adjustCanvasSize();
  2790 		}
  2791 		// Context Menu 
  2792 		if (selection) 
  2793 		{
  2794 			if (typeid(*selection)==typeid(BranchObj) ||
  2795 				typeid(*selection)==typeid(MapCenterObj) )
  2796 			{
  2797 				// Context Menu on branch or mapcenter
  2798 				updateActions();
  2799 				branchContextMenu->popup(e->globalPos() );
  2800 			}	
  2801 			if (typeid(*selection)==typeid(FloatImageObj))
  2802 			{
  2803 				// Context Menu on floatimage
  2804 				updateActions();
  2805 				floatimageContextMenu->popup(e->globalPos() );
  2806 			}	
  2807 		}	
  2808 	} else 
  2809 	{ // No MapObj found, we are on the Canvas itself
  2810 		// Context Menu on Canvas
  2811 		updateActions();
  2812 		canvasContextMenu->popup(e->globalPos() );
  2813     } 
  2814 }
  2815 
  2816 void MapEditor::contentsMousePressEvent(QMouseEvent* e)
  2817 {
  2818 	// Finish open lineEdits
  2819 	if (lineedit) finishedLineEditNoSave();
  2820 	
  2821     QPoint p = inverseWorldMatrix().map(e->pos());
  2822     LinkableMapObj* lmo=mapCenter->findMapObj(p, NULL);
  2823 	
  2824 	// Special case: CTRL is pressed
  2825 	if (e->state() & QMouseEvent::ControlButton)
  2826 	{
  2827 		if (actionModModeColor->isOn())
  2828 		{
  2829 			if (e->state() & QMouseEvent::ControlButton)
  2830 			{
  2831 				pickingColor=true;
  2832 				setCursor (pickColorCursor);
  2833 				return;
  2834 			}
  2835 		} 
  2836 		if (actionModModeLink->isOn())
  2837 		{	
  2838 			BranchObj *bo_begin=NULL;
  2839 			if (lmo)
  2840 				bo_begin=(BranchObj*)(lmo);
  2841 			else	
  2842 				if (selection && 
  2843 					((typeid(*selection) == typeid(BranchObj)) || 
  2844 					(typeid(*selection) == typeid(MapCenterObj)))  )
  2845 				bo_begin=(BranchObj*)(selection);
  2846 			if (bo_begin)	
  2847 			{
  2848 				drawingLink=true;
  2849 				linkingObj_src=bo_begin;
  2850 				tmpLink=new LinkObj (mapCanvas);
  2851 				tmpLink->setBegin (bo_begin);
  2852 				tmpLink->setEnd   (p);
  2853 				tmpLink->updateLink();
  2854 				tmpLink->setVisibility (true);
  2855 				return;
  2856 			} 
  2857 		}	
  2858 	}
  2859 
  2860     if (lmo) 
  2861 	{	// MapObj was found
  2862 		if (selection != lmo)
  2863 		{
  2864 			// select the MapObj
  2865 			if (selection) selection->unselect();
  2866 			selection=lmo;
  2867 			selection->select();
  2868 				
  2869 			adjustCanvasSize();
  2870 			
  2871 		}
  2872 
  2873 		// Check, if systemFlag clicked
  2874 		if (typeid(*selection)==typeid(BranchObj) ||
  2875 			typeid(*selection)==typeid(MapCenterObj) )
  2876 		{
  2877 			QString foname=((BranchObj*)(selection))->getSystemFlagName(p);
  2878 			if (!foname.isEmpty())
  2879 			{
  2880 				// Do not move, if systemFlag clicked
  2881 				if (foname=="url") 
  2882 					openURL();
  2883 				else
  2884 					if (foname=="vymLink")
  2885 					{
  2886 						mainWindow->editOpenVymLink();
  2887 						// tabWidget may change, better return now
  2888 						// before segfaulting...
  2889 						return;
  2890 					} else
  2891 						if (foname=="note")
  2892 							mainWindow->windowToggleNoteEditor();
  2893 			}			
  2894 		}	
  2895 			
  2896 		// Left Button	    Move Branches
  2897 		if (e->button() == QMouseEvent::LeftButton )
  2898 		{
  2899 			// If modMode==copy, then we want to "move" the _new_ object around
  2900 			// then we need the offset from p to the _old_ selection, because of tmp
  2901 			if (actionModModeCopy->isOn() &&
  2902 				e->state() & QMouseEvent::ControlButton)
  2903 			{
  2904 				if (typeid(*selection)==typeid(BranchObj) )
  2905 				{
  2906 					copyingObj=true;
  2907 					movingObj_start.setX( p.x() - selection->x() );	
  2908 					movingObj_start.setY( p.y() - selection->y() );	
  2909 					mapCenter->addBranch ((BranchObj*)(selection));
  2910 					unselect();
  2911 					selection=mapCenter->getLastBranch();
  2912 					selection->select();
  2913 					selection->setParObjTmp ((BranchObj*)(selection),p,1); 
  2914 					selection->updateLink();
  2915 				}
  2916 			}	else
  2917 			{
  2918 				movingObj_start.setX( p.x() - selection->x() );	
  2919 				movingObj_start.setY( p.y() - selection->y() );	
  2920 			}
  2921 			
  2922 			movingObj=selection;	
  2923 		} else
  2924 			// Middle Button    Toggle Scroll
  2925 			// (On Mac OS X this won't work, but we still have 
  2926 			// a button in the toolbar)
  2927 			if (e->button() == QMouseEvent::MidButton )
  2928 				toggleScroll();
  2929 		updateActions();
  2930 	} else 
  2931 	{ // No MapObj found, we are on the Canvas itself
  2932 		// Left Button	    move Pos of CanvasView
  2933 		if (e->button() == QMouseEvent::LeftButton )
  2934 		{
  2935 			movingObj=NULL;	// move Content not Obj
  2936 			movingObj_start=e->globalPos();
  2937 			movingCont_start=QPoint (contentsX(), contentsY() );
  2938 			movingVec=QPoint(0,0);
  2939 			setCursor(handOpenCursor);
  2940 		} 
  2941     } 
  2942 }
  2943 
  2944 void MapEditor::contentsMouseMoveEvent(QMouseEvent* e)
  2945 {
  2946 	QPoint p = inverseWorldMatrix().map(e->pos());
  2947 
  2948     // Move the selected MapObj
  2949     if ( selection && movingObj) 
  2950     {	
  2951 		ensureVisible (p.x(),p.y());
  2952 		
  2953 		// Now move the selection, but add relative position 
  2954 		// (movingObj_start) where selection was chosen with 
  2955 		// mousepointer. (This avoids flickering resp. jumping 
  2956 		// of selection back to absPos)
  2957 		
  2958 		LinkableMapObj *lmosel;
  2959 		lmosel =  dynamic_cast <LinkableMapObj*> (selection);
  2960 
  2961 		// Check if we could link 
  2962 		LinkableMapObj* lmo=mapCenter->findMapObj(p, lmosel);
  2963 		
  2964 
  2965 		if (typeid(*selection) == typeid(FloatImageObj))
  2966 		{
  2967 			setChanged();
  2968 			saveState();
  2969 			FloatObj *fo=(FloatObj*)(selection);
  2970 			if (fo->getLinkStyle()==StyleUndef) 
  2971 			{
  2972 				fo->setLinkStyle(fo->getDefLinkStyle());
  2973 				fo->setLinkColor(fo->getParObj()->getLinkColor());
  2974 			}	
  2975 			fo->move   (p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );		
  2976 			fo->setRelPos();
  2977 			fo->reposition();
  2978 
  2979 			// Relink float to new mapcenter or branch, if shift is pressed	
  2980 			// Only relink, if selection really has a new parent
  2981 			if ( (e->state() & QMouseEvent::ShiftButton) && lmo &&
  2982 				( (typeid(*lmo)==typeid(BranchObj)) ||
  2983 				  (typeid(*lmo)==typeid(MapCenterObj)) ) &&
  2984 				( lmo != fo->getParObj())  
  2985 				)
  2986 			{
  2987 				if (typeid(*fo) == typeid(FloatImageObj)) 
  2988 				{
  2989 					FloatImageObj *fio=(FloatImageObj*)(fo);
  2990 					((BranchObj*)(lmo))->addFloatImage (fio);
  2991 					fio->unselect();
  2992 					((BranchObj*)(fio->getParObj()))->removeFloatImage (fio);
  2993 					fio=((BranchObj*)(lmo))->getLastFloatImage();
  2994 					fio->setRelPos();
  2995 					fio->reposition();
  2996 					selection=(LinkableMapObj*)(fio);
  2997 					selection->select();
  2998 					movingObj=(MapObj*)(fio);
  2999 					// setLinkStyle calls updateLink, only set it once
  3000 					if (fio->getLinkStyle()!=fio->getDefLinkStyle() ) 
  3001 						fio->setLinkStyle (fio->getDefLinkStyle());
  3002 
  3003 				}	
  3004 				// TODO if (typeid(*selection) == typeid(FloatTextObj))
  3005 			}
  3006 		} else	// selection != a FloatObj
  3007 		{
  3008 			if (lmosel->getDepth()==0)
  3009 			{
  3010 				if (e->state() == (LeftButton | !ShiftButton)) 
  3011 					// If mapCenter is moved, move all the rest by default, too.
  3012 					mapCenter->moveAll(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );		
  3013 				else	
  3014 					mapCenter->move   (p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );		
  3015 			} else
  3016 			{	
  3017 				if (lmosel->getDepth()==1)
  3018 				{
  3019 					// depth==1, mainbranch
  3020 					setChanged();
  3021 					saveState(PartOfMap,lmosel);
  3022 					lmosel->move(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );		
  3023 				} else
  3024 				{
  3025 					// depth>1
  3026 					if (lmosel->getOrientation() == OrientLeftOfCenter)
  3027 						// Add width of bbox here, otherwise alignRelTo will cause jumping around
  3028 						lmosel->move(p.x() -movingObj_start.x()+lmosel->getBBox().width(), 
  3029 							p.y()-movingObj_start.y() );		
  3030 					else	
  3031 						lmosel->move(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );
  3032 				} 
  3033 				// reposition subbranch
  3034 				lmosel->reposition();	
  3035 				//ensureSelectionVisible();
  3036 
  3037 				if (lmo && (lmo!=selection) &&  
  3038 					(typeid(*lmo) == typeid(BranchObj) ||
  3039 					(typeid(*lmo) == typeid(MapCenterObj) )
  3040 					) )
  3041 				{
  3042 					if (e->state() & QMouseEvent::ControlButton)
  3043 					{
  3044 						// Special case: CTRL to link below lmo
  3045 						lmosel->setParObjTmp (lmo,p,+1);
  3046 					}
  3047 					else if (e->state() & QMouseEvent::ShiftButton)
  3048 						lmosel->setParObjTmp (lmo,p,-1);
  3049 					else
  3050 						lmosel->setParObjTmp (lmo,p,0);
  3051 				} else	
  3052 				{
  3053 					lmosel->unsetParObjTmp();
  3054 					/*
  3055 					if (lmo &&(lmo==selection))   
  3056 						// Could link to myself (happens sometimes...)
  3057 						lmosel->unsetParObjTmp();
  3058 					if (!lmo)  
  3059 						// no Obj under selection, go back to original Parent
  3060 						lmosel->unsetParObjTmp();
  3061 					*/	
  3062 				}		
  3063 			} // depth>0
  3064 
  3065 		} // no FloatImageObj
  3066 
  3067 		canvas()->update();
  3068 		return;
  3069 	} // selection && moving_obj
  3070 		
  3071 	// Draw a link from one branch to another
  3072 	if (drawingLink)
  3073 	{
  3074 		 tmpLink->setEnd (p);
  3075 		 tmpLink->updateLink();
  3076 	}	 
  3077 	
  3078     // Move CanvasView 
  3079     if (!movingObj && !pickingColor &&!drawingLink) 
  3080 	{
  3081 		QPoint p=e->globalPos();
  3082 		movingVec.setX(-p.x() + movingObj_start.x() );
  3083 		movingVec.setY(-p.y() + movingObj_start.y() );
  3084 		setContentsPos( movingCont_start.x() + movingVec.x(),
  3085 	    movingCont_start.y() + movingVec.y());
  3086 
  3087 		updateViewCenter();
  3088     }
  3089 }
  3090 
  3091 
  3092 void MapEditor::contentsMouseReleaseEvent(QMouseEvent* e)
  3093 {
  3094 	LinkableMapObj *dst;
  3095 	// Have we been picking color?
  3096 	if (pickingColor)
  3097 	{
  3098 		pickingColor=false;
  3099 		setCursor (ArrowCursor);
  3100 		// Check if we are over another branch
  3101 		dst=mapCenter->findMapObj(inverseWorldMatrix().map(e->pos() ), NULL);
  3102 		if (dst && selection) 
  3103 		{	
  3104 			if (e->state() & QMouseEvent::ShiftButton)
  3105 			{
  3106 				((BranchObj*)(selection))->setColor (((BranchObj*)(dst))->getColor(),false);
  3107 				((BranchObj*)(selection))->setLinkColor ();
  3108 			}	
  3109 			else	
  3110 			{
  3111 				((BranchObj*)(selection))->setColor (((BranchObj*)(dst))->getColor(),true);
  3112 				((BranchObj*)(selection))->setLinkColor ();
  3113 			}	
  3114 		} 
  3115 		return;
  3116 	}
  3117 
  3118 	// Have we been drawing a link?
  3119 	if (drawingLink)
  3120 	{
  3121 		drawingLink=false;
  3122 		// Check if we are over another branch
  3123 		dst=mapCenter->findMapObj(inverseWorldMatrix().map(e->pos() ), NULL);
  3124 		if (dst && selection) 
  3125 		{	
  3126 			tmpLink->setEnd ( ((BranchObj*)(dst)) );
  3127 			tmpLink->updateLink();
  3128 			tmpLink->activate();
  3129 			setChanged();
  3130 			
  3131 		} else
  3132 		{
  3133 			delete(tmpLink);
  3134 			tmpLink=NULL;
  3135 		}
  3136 		return;
  3137 	}
  3138 	
  3139     // Have we been moving something?
  3140     if ( selection && movingObj ) 
  3141     {	
  3142 		// Check if we are over another branch, but ignore 
  3143 		// any found LMOs, which are FloatObjs
  3144 		dst=mapCenter->findMapObj(inverseWorldMatrix().map(e->pos() ), 
  3145 			((LinkableMapObj*)(selection)) );
  3146 
  3147 		if (dst &&
  3148 		(typeid(*dst)!=typeid(BranchObj)&&typeid(*dst)!=typeid(MapCenterObj))) 
  3149 		{
  3150 			dst=NULL;
  3151 		}	
  3152 		
  3153 		// Now check, if we have been moving a branch 
  3154 		if (typeid(*selection) == typeid(BranchObj)  )
  3155 		{
  3156 			// save the position in case we link to mapcenter
  3157 			QPoint savePos=QPoint (selection->x(),selection->y() );
  3158 
  3159 			// Reset the temporary drawn link to the original one
  3160 			((LinkableMapObj*)(selection))->unsetParObjTmp();
  3161 
  3162 
  3163 			copyingObj=false;	
  3164 			if (!dst ) 
  3165 			{
  3166 				if (copyingObj)
  3167 				{
  3168 					// remove the current selection, if we have no destination
  3169 					selection->unselect();
  3170 					((BranchObj*)(selection->getParObj()))->removeBranch ((BranchObj*)(selection));
  3171 					if (selectionLast)
  3172 					{
  3173 						selection=selectionLast;
  3174 						selectionLast=NULL;
  3175 						selection->select();
  3176 					}
  3177 				}
  3178 			} else
  3179 			{
  3180 				setChanged();
  3181 				saveState();
  3182 				// TODO we also could check, if dest and src are on same branch,
  3183 				// then it would be sufficient to saveState of this branch
  3184 
  3185 				// Modifiers allow to insert above/below dst
  3186 				if (e->state() & QMouseEvent::ShiftButton)
  3187 				{
  3188 						((BranchObj*)(selection))->moveBranchTo 
  3189 						(
  3190 							(BranchObj*)(dst),
  3191 							((BranchObj*)(dst))->getNum()
  3192 						);
  3193 					//if (selection) selection->select();
  3194 				} else 
  3195 				if (e->state() & QMouseEvent::ControlButton)
  3196 				{
  3197 					((BranchObj*)(selection))->moveBranchTo 
  3198 					(
  3199 						(BranchObj*)(dst),
  3200 						((BranchObj*)(dst))->getNum()+1
  3201 					);
  3202 					//if (selection) selection->select();
  3203 				} else	
  3204 				{
  3205 					((BranchObj*)(selection))->moveBranchTo ((BranchObj*)(dst),-1);
  3206 					if (dst->getDepth()==0) 
  3207 						((BranchObj*)(selection))->move (savePos);
  3208 				} 
  3209 			}	
  3210 			// Draw the original link, before selection was moved around
  3211 			mapCenter->reposition();
  3212 		}
  3213 		// Finally resize canvas, if needed
  3214 		adjustCanvasSize();
  3215 		canvas()->update();
  3216 		movingObj=NULL;		
  3217 	} else 
  3218 		// maybe we moved View: set old cursor
  3219 		setCursor (ArrowCursor);
  3220     
  3221 }
  3222 
  3223 void MapEditor::contentsMouseDoubleClickEvent(QMouseEvent* e)
  3224 {
  3225 	// Finish open lineEdits
  3226 	if (lineedit) finishedLineEditNoSave();
  3227 	
  3228 	if (e->button() == QMouseEvent::LeftButton )
  3229 	{
  3230 		QPoint p = inverseWorldMatrix().map(e->pos());
  3231 		LinkableMapObj *lmo=mapCenter->findMapObj(p, NULL);
  3232 		if (lmo) {	// MapObj was found
  3233 			// First select the MapObj than edit heading
  3234 			if (selection) selection->unselect();
  3235 			selection=lmo;
  3236 			selection->select();
  3237 			setChanged();
  3238 			saveState(PartOfMap,selection);
  3239 			editHeading();
  3240 		}
  3241 	}
  3242 }
  3243 
  3244 void MapEditor::resizeEvent (QResizeEvent* e)
  3245 {
  3246 	QCanvasView::resizeEvent( e );
  3247 	
  3248 	QString s="";
  3249 	if (!fileName.isEmpty()) s=fileName;
  3250 	adjustCanvasSize();
  3251 }
  3252