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