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