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