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