mapeditor.cpp
author insilmaril
Wed, 16 Jul 2008 11:56:44 +0000
changeset 722 462d39502273
parent 720 192e1392ba6a
child 723 11f9124c1cca
permissions -rw-r--r--
no more segfault when deleting animated part
     1 #include "mapeditor.h"
     2 
     3 #include <iostream>
     4 #include <cstdlib>
     5 #include <typeinfo>
     6 
     7 #include <QObject>
     8 
     9 #include "mainwindow.h"
    10 #include "misc.h"
    11 #include "warningdialog.h"
    12 
    13 
    14 extern int statusbarTime;
    15 extern Main *mainWindow;
    16 extern QString tmpVymDir;
    17 extern QString clipboardDir;
    18 extern QString clipboardFile;
    19 extern bool clipboardEmpty;
    20 extern bool debug;
    21 extern FlagRowObj *standardFlagsDefault;
    22 
    23 extern QMenu* branchContextMenu;
    24 extern QMenu* branchAddContextMenu;
    25 extern QMenu* branchRemoveContextMenu;
    26 extern QMenu* branchLinksContextMenu;
    27 extern QMenu* branchXLinksContextMenuEdit;
    28 extern QMenu* branchXLinksContextMenuFollow;
    29 extern QMenu* floatimageContextMenu;
    30 extern QMenu* canvasContextMenu;
    31 
    32 extern Settings settings;
    33 extern QString iconPath;
    34 
    35 ///////////////////////////////////////////////////////////////////////
    36 ///////////////////////////////////////////////////////////////////////
    37 MapEditor::MapEditor( VymModel *vm) 
    38 {
    39 	//cout << "Constructor ME "<<this<<endl;
    40 	mapScene= new QGraphicsScene(NULL);
    41 	mapScene->setBackgroundBrush (QBrush(Qt::white, Qt::SolidPattern));
    42 
    43 	model=vm;
    44 	model->setScene (mapScene);
    45 	model->setMapEditor (this);
    46 	model->registerEditor(this);
    47 	model->addMapCenter();	//  FIXME create this in MapEditor until BO and MCO are independent of scene
    48 	model->makeDefault();
    49 
    50     setScene (mapScene);
    51 
    52     printer=NULL;
    53 
    54 	// Create bitmap cursors, platform dependant
    55 	HandOpenCursor=QCursor (QPixmap(iconPath+"cursorhandopen.png"),1,1);		
    56 	PickColorCursor=QCursor ( QPixmap(iconPath+"cursorcolorpicker.png"), 5,27 ); 
    57 	CopyCursor=QCursor ( QPixmap(iconPath+"cursorcopy.png"), 1,1 ); 
    58 	XLinkCursor=QCursor ( QPixmap(iconPath+"cursorxlink.png"), 1,7 ); 
    59 
    60 	setFocusPolicy (Qt::StrongFocus);
    61 
    62 	pickingColor=false;
    63 	drawingLink=false;
    64 	copyingObj=false;
    65 
    66     editingBO=NULL;
    67     movingObj=NULL;
    68 
    69 	printFrame=true;
    70 	printFooter=true;
    71 
    72 	setAcceptDrops (true);	
    73 
    74 	model->reposition();	//FIXME really still needed?
    75 
    76 
    77 	// Action to embed LineEdit for heading in Scene
    78 	editingHeading=false;
    79 	lineEdit=new QLineEdit;
    80 	lineEdit->hide();
    81 	QGraphicsProxyWidget *pw=scene()->addWidget (lineEdit);
    82 	pw->setZValue (100);
    83 
    84 	QAction *a = new QAction( tr( "Edit heading","MapEditor" ), this);
    85 	a->setShortcut ( Qt::Key_Return );					//Edit heading
    86 	//a->setShortcutContext (Qt::WindowShortcut);
    87 	addAction (a);
    88     connect( a, SIGNAL( triggered() ), this, SLOT( editHeading() ) );
    89 	a = new QAction( tr( "Edit heading","MapEditor" ), this);
    90 	a->setShortcut ( Qt::Key_Enter);					//Edit heading
    91 	//a->setShortcutContext (Qt::WindowShortcut);
    92 	addAction (a);
    93     connect( a, SIGNAL( triggered() ), this, SLOT( editHeading() ) );
    94 
    95 	// Attributes	//FIXME testing only...
    96 	QString k;
    97 	AttributeDef *ad;
    98 	attrTable= new AttributeTable();
    99 	k="A - StringList";
   100 	ad=attrTable->addKey (k,StringList);
   101 	if (ad)
   102 	{
   103 		QStringList sl;
   104 		sl <<"val 1"<<"val 2"<< "val 3";
   105 		ad->setValue (QVariant (sl));
   106 	}
   107 	//attrTable->addValue ("Key A","P 1");
   108 	//attrTable->addValue ("Key A","P 2");
   109 	//attrTable->addValue ("Key A","P 3");
   110 	//attrTable->addValue ("Key A","P 4");
   111 	k="B - FreeString";
   112 	ad=attrTable->addKey (k,FreeString);
   113 	if (ad)
   114 	{
   115 		//attrTable->addValue ("Key B","w1");
   116 		//attrTable->addValue ("Key B","w2");
   117 	}
   118 	k="C - UniqueString";
   119 	ad=attrTable->addKey (k,UniqueString);
   120 	if (ad)
   121 	{
   122 	//attrTable->addKey ("Key Prio");
   123 	//attrTable->addValue ("Key Prio","Prio 1");
   124 	//attrTable->addValue ("Key Prio","Prio 2");
   125 	}
   126 }
   127 
   128 MapEditor::~MapEditor()
   129 {
   130 	//cout <<"Destructor MapEditor\n";
   131 	// tmpMapDir is in tmpVymDir, so it gets removed automagically when vym closes
   132 	
   133 	//removeDir(QDir(tmpMapDir));	// FIXME check?!?
   134 	model->unregisterEditor(this);
   135 }
   136 
   137 VymModel* MapEditor::getModel()
   138 {
   139     return model;
   140 }
   141 
   142 QGraphicsScene * MapEditor::getScene()
   143 {
   144     return mapScene;
   145 }
   146 
   147 void MapEditor::print()
   148 {
   149 	if ( !printer ) 
   150 	{
   151 		printer = new QPrinter;
   152 		printer->setColorMode (QPrinter::Color);
   153 		printer->setPrinterName (settings.value("/mainwindow/printerName",printer->printerName()).toString());
   154 		printer->setOutputFormat((QPrinter::OutputFormat)settings.value("/mainwindow/printerFormat",printer->outputFormat()).toInt());
   155 		printer->setOutputFileName(settings.value("/mainwindow/printerFileName",printer->outputFileName()).toString());
   156 	}
   157 
   158 	QRectF totalBBox=model->getTotalBBox();
   159 
   160 	// Try to set orientation automagically
   161 	// Note: Interpretation of generated postscript is amibiguous, if 
   162 	// there are problems with landscape mode, see
   163 	// http://sdb.suse.de/de/sdb/html/jsmeix_print-cups-landscape-81.html
   164 
   165 	if (totalBBox.width()>totalBBox.height())
   166 		// recommend landscape
   167 		printer->setOrientation (QPrinter::Landscape);
   168 	else	
   169 		// recommend portrait
   170 		printer->setOrientation (QPrinter::Portrait);
   171 
   172 	if ( printer->setup(this) ) 
   173 	// returns false, if printing is canceled
   174 	{
   175 		QPainter pp(printer);
   176 
   177 		pp.setRenderHint(QPainter::Antialiasing,true);
   178 
   179 		// Don't print the visualisation of selection
   180 		model->unselect();
   181 
   182 		QRectF mapRect=totalBBox;
   183 		QGraphicsRectItem *frame=NULL;
   184 
   185 		if (printFrame) 
   186 		{
   187 			// Print frame around map
   188 			mapRect.setRect (totalBBox.x()-10, totalBBox.y()-10, 
   189 				totalBBox.width()+20, totalBBox.height()+20);
   190 			frame=mapScene->addRect (mapRect, QPen(Qt::black),QBrush(Qt::NoBrush));
   191 			frame->setZValue(0);
   192 			frame->show();    
   193 		}		
   194 
   195 
   196 		double paperAspect = (double)printer->width()   / (double)printer->height();
   197 		double   mapAspect = (double)mapRect.width() / (double)mapRect.height();
   198 		int viewBottom;
   199 		if (mapAspect>=paperAspect)
   200 		{
   201 			// Fit horizontally to paper width
   202 			//pp.setViewport(0,0, printer->width(),(int)(printer->width()/mapAspect) );	
   203 			viewBottom=(int)(printer->width()/mapAspect);	
   204 		}	else
   205 		{
   206 			// Fit vertically to paper height
   207 			//pp.setViewport(0,0,(int)(printer->height()*mapAspect),printer->height());	
   208 			viewBottom=printer->height();	
   209 		}	
   210 		
   211 		if (printFooter) 
   212 		{
   213 			// Print footer below map
   214 			QFont font;		
   215 			font.setPointSize(10);
   216 			pp.setFont (font);
   217 			QRectF footerBox(0,viewBottom,printer->width(),15);
   218 			// FIXME fileName not any longer available here: pp.drawText ( footerBox,Qt::AlignLeft,"VYM - " +fileName);
   219 			pp.drawText ( footerBox, Qt::AlignRight, QDate::currentDate().toString(Qt::TextDate));
   220 		}
   221 		mapScene->render (
   222 			&pp, 
   223 			QRectF (0,0,printer->width(),printer->height()-15),
   224 			QRectF(mapRect.x(),mapRect.y(),mapRect.width(),mapRect.height())
   225 		);
   226 		
   227 		// Viewport has paper dimension
   228 		if (frame)  delete (frame);
   229 
   230 		// Restore selection
   231 		model->reselect();
   232 
   233 		// Save settings in vymrc
   234 		settings.writeEntry("/mainwindow/printerName",printer->printerName());
   235 		settings.writeEntry("/mainwindow/printerFormat",printer->outputFormat());
   236 		settings.writeEntry("/mainwindow/printerFileName",printer->outputFileName());
   237 	}
   238 }
   239 
   240 void MapEditor::setAntiAlias (bool b)
   241 {
   242 	setRenderHint(QPainter::Antialiasing,b);
   243 }
   244 
   245 void MapEditor::setSmoothPixmap(bool b)
   246 {
   247 	setRenderHint(QPainter::SmoothPixmapTransform,b);
   248 }
   249 
   250 void MapEditor::toggleStandardFlag(QString f)
   251 {
   252 	BranchObj *bo=model->getSelectedBranch();
   253 	if (bo) 
   254 	{
   255 		QString u,r;
   256 		if (bo->isSetStandardFlag(f))
   257 		{
   258 			r="unsetFlag";
   259 			u="setFlag";
   260 		}	
   261 		else
   262 		{
   263 			u="unsetFlag";
   264 			r="setFlag";
   265 		}	
   266 		model->saveState(
   267 			bo,
   268 			QString("%1 (\"%2\")").arg(u).arg(f), 
   269 			bo,
   270 			QString("%1 (\"%2\")").arg(r).arg(f),
   271 			QString("Toggling standard flag \"%1\" of %2").arg(f).arg(getName(bo)));
   272 		bo->toggleStandardFlag (f,mainWindow->useFlagGroups());
   273 		model->updateSelection();
   274 	}
   275 }
   276 
   277 void MapEditor::updateSelection()
   278 {
   279 	// Tell selection to update geometries
   280 	model->updateSelection();
   281 }
   282 
   283 AttributeTable* MapEditor::attributeTable()
   284 {
   285 	return attrTable;
   286 }
   287 
   288 void MapEditor::testFunction1()
   289 {
   290 	BranchObj *bo=model->getSelectedBranch();
   291 	//if (bo) model->moveAway (bo);
   292 	if (bo) bo->setLinkStyle (LinkableMapObj::Line);
   293 	
   294 /* TODO Hide hidden stuff temporary, maybe add this as regular function somewhere
   295 	if (hidemode==HideNone)
   296 	{
   297 		setHideTmpMode (HideExport);
   298 		mapCenter->calcBBoxSizeWithChilds();
   299 		QRectF totalBBox=mapCenter->getTotalBBox();
   300 		QRectF mapRect=totalBBox;
   301 		QCanvasRectangle *frame=NULL;
   302 
   303 		cout << "  map has =("<<totalBBox.x()<<","<<totalBBox.y()<<","<<totalBBox.width()<<","<<totalBBox.height()<<")\n";
   304 	
   305 		mapRect.setRect (totalBBox.x(), totalBBox.y(), 
   306 			totalBBox.width(), totalBBox.height());
   307 		frame=new QCanvasRectangle (mapRect,mapScene);
   308 		frame->setBrush (QColor(white));
   309 		frame->setPen (QColor(black));
   310 		frame->setZValue(0);
   311 		frame->show();    
   312 	}	
   313 	else	
   314 	{
   315 		setHideTmpMode (HideNone);
   316 	}	
   317 	cout <<"  hidemode="<<hidemode<<endl;
   318 	*/
   319 }	
   320 	
   321 void MapEditor::testFunction2()
   322 {
   323 
   324 /*
   325 	// Toggle hidemode
   326 	if (hidemode==HideExport)
   327 		setHideTmpMode (HideNone);
   328 	else	
   329 		setHideTmpMode (HideExport);
   330 */		
   331 }
   332 
   333 void MapEditor::editHeading()
   334 {
   335 	if (editingHeading)
   336 	{
   337 		editHeadingFinished();
   338 		return;
   339 	}
   340 	BranchObj *bo=model->getSelectedBranch();
   341 	if (bo)
   342 	{
   343 		model->setSelectionBlocked(true);
   344 
   345 		lineEdit->setText (bo->getHeading());
   346 		QPoint p = mapTo (this,bo->getAbsPos().toPoint() );
   347 		lineEdit->setGeometry(p.x(),p.y(),230,25);
   348 		lineEdit->selectAll();
   349 		lineEdit->show();
   350 		lineEdit->setFocus();
   351 		lineEdit->grabKeyboard();
   352 		editingHeading=true;
   353 	}
   354 
   355 }
   356 void MapEditor::editHeadingFinished()
   357 {
   358 	editingHeading=false;
   359 	lineEdit->releaseKeyboard();
   360 	model->setHeading (lineEdit->text() );
   361 	model->setSelectionBlocked(false);
   362 	lineEdit->hide();
   363 
   364 	// Maybe reselect previous branch 
   365 	mainWindow->editHeadingFinished (model);
   366 }
   367 
   368 
   369 void MapEditor::contextMenuEvent ( QContextMenuEvent * e )
   370 {
   371 	// Lineedits are already closed by preceding
   372 	// mouseEvent, we don't need to close here.
   373 
   374     QPointF p = mapToScene(e->pos());
   375     LinkableMapObj* lmo=model->findMapObj(p, NULL);
   376 	
   377     if (lmo) 
   378 	{	// MapObj was found
   379 		if (model->getSelection() != lmo)
   380 		{
   381 			// select the MapObj
   382 			model->select(lmo);
   383 		}
   384 		// Context Menu 
   385 		if (model->getSelectedBranch() ) 
   386 		{
   387 			// Context Menu on branch or mapcenter
   388 			model->updateActions();
   389 			branchContextMenu->popup(e->globalPos() );
   390 		} else
   391 		{
   392 			if (model->getSelectedFloatImage() )
   393 			{
   394 				// Context Menu on floatimage
   395 				model->updateActions();
   396 				floatimageContextMenu->popup(e->globalPos() );
   397 			}	
   398 		}	
   399 	} else 
   400 	{ // No MapObj found, we are on the Canvas itself
   401 		// Context Menu on scene
   402 		model->updateActions();
   403 		contextMenuPos=p;
   404 		canvasContextMenu->popup(e->globalPos() );
   405     } 
   406 	e->accept();
   407 }
   408 
   409 void MapEditor::keyPressEvent(QKeyEvent* e)
   410 {
   411 	if (e->modifiers() & Qt::ControlModifier)
   412 	{
   413 		switch (mainWindow->getModMode())
   414 		{
   415 			case Main::ModModeColor: 
   416 				setCursor (PickColorCursor);
   417 				break;
   418 			case Main::ModModeCopy: 
   419 				setCursor (CopyCursor);
   420 				break;
   421 			case Main::ModModeXLink: 
   422 				setCursor (XLinkCursor);
   423 				break;
   424 			default :
   425 				setCursor (Qt::ArrowCursor);
   426 				break;
   427 		} 
   428 	}	
   429 }
   430 
   431 void MapEditor::keyReleaseEvent(QKeyEvent* e)
   432 {
   433 	if (!(e->modifiers() & Qt::ControlModifier))
   434 		setCursor (Qt::ArrowCursor);
   435 }
   436 
   437 void MapEditor::mousePressEvent(QMouseEvent* e)
   438 {
   439 	// Ignore right clicks, these will go to context menus
   440 	if (e->button() == Qt::RightButton )
   441 	{
   442 		e->ignore();
   443 		return;
   444 	}
   445 
   446 	//Ignore clicks while editing heading
   447 	if (model->isSelectionBlocked() ) 
   448 	{
   449 		e->ignore();
   450 		return;
   451 	}
   452 
   453     QPointF p = mapToScene(e->pos());
   454     LinkableMapObj* lmo=model->findMapObj(p, NULL);
   455 	
   456 	e->accept();
   457 
   458 	//Take care of  system flags _or_ modifier modes
   459 	//
   460 	if (lmo && (typeid(*lmo)==typeid(BranchObj) ||
   461 		typeid(*lmo)==typeid(MapCenterObj) ))
   462 	{
   463 		QString foname=((BranchObj*)lmo)->getSystemFlagName(p);
   464 		if (!foname.isEmpty())
   465 		{
   466 			// systemFlag clicked
   467 			model->selectInt (lmo);
   468 			if (foname=="url") 
   469 			{
   470 				if (e->state() & Qt::ControlModifier)
   471 					mainWindow->editOpenURLTab();
   472 				else	
   473 					mainWindow->editOpenURL();
   474 			}	
   475 			else if (foname=="vymLink")
   476 			{
   477 				mainWindow->editOpenVymLink();
   478 				// tabWidget may change, better return now
   479 				// before segfaulting...
   480 			} else if (foname=="note")
   481 				mainWindow->windowToggleNoteEditor();
   482 			else if (foname=="hideInExport")		
   483 				model->toggleHideExport();
   484 			// FIXME needed? xelection.update();	
   485 			return;	
   486 		} 
   487 	}	
   488 	// No system flag clicked, take care of modmodes (CTRL-Click)
   489 	if (e->state() & Qt::ControlModifier)
   490 	{
   491 		if (mainWindow->getModMode()==Main::ModModeColor)
   492 		{
   493 				pickingColor=true;
   494 				setCursor (PickColorCursor);
   495 				return;
   496 		} 
   497 		if (mainWindow->getModMode()==Main::ModModeXLink)
   498 		{	
   499 			BranchObj *bo_begin=NULL;
   500 			if (lmo)
   501 				bo_begin=(BranchObj*)(lmo);
   502 			else	
   503 				bo_begin=model->getSelectedBranch();
   504 			if (bo_begin)	
   505 			{
   506 				drawingLink=true;
   507 				linkingObj_src=bo_begin;
   508 				tmpXLink=new XLinkObj (mapScene);
   509 				tmpXLink->setBegin (bo_begin);
   510 				tmpXLink->setEnd   (p);
   511 				tmpXLink->setColor(model->getMapDefXLinkColor());
   512 				tmpXLink->setWidth(model->getMapDefXLinkWidth());
   513 				tmpXLink->updateXLink();
   514 				tmpXLink->setVisibility (true);
   515 				return;
   516 			} 
   517 		}
   518 	}	// End of modmodes
   519 
   520     if (lmo) 
   521 	{	
   522 		// Select the clicked object
   523 		model->selectInt (lmo);
   524 
   525 		// Left Button	    Move Branches
   526 		if (e->button() == Qt::LeftButton )
   527 		{
   528 			//movingObj_start.setX( p.x() - selection->x() );// TODO replaced selection->lmo here	
   529 			//movingObj_start.setY( p.y() - selection->y() );	
   530 			movingObj_start.setX( p.x() - lmo->x() );	
   531 			movingObj_start.setY( p.y() - lmo->y() );	
   532 			movingObj_orgPos.setX (lmo->x() );
   533 			movingObj_orgPos.setY (lmo->y() );
   534 			lmo->setRelPos();
   535 			movingObj_orgRelPos=lmo->getRelPos();
   536 
   537 			// If modMode==copy, then we want to "move" the _new_ object around
   538 			// then we need the offset from p to the _old_ selection, because of tmp
   539 			if (mainWindow->getModMode()==Main::ModModeCopy &&
   540 				e->state() & Qt::ControlModifier)
   541 			{
   542 				BranchObj *bo=model->getSelectedBranch();
   543 				if (bo)
   544 				{
   545 					copyingObj=true;
   546 					bo->addBranch ((BranchObj*)model->getSelection());
   547 					model->unselect();
   548 					model->select(bo->getLastBranch());
   549 					model->reposition();
   550 				}
   551 			} 
   552 
   553 			movingObj=model->getSelection();	
   554 		} else
   555 			// Middle Button    Toggle Scroll
   556 			// (On Mac OS X this won't work, but we still have 
   557 			// a button in the toolbar)
   558 			if (e->button() == Qt::MidButton )
   559 				model->toggleScroll();
   560 		model->updateActions();
   561 		// FIXME needed? xelection.update();
   562 	} else 
   563 	{ // No MapObj found, we are on the scene itself
   564 		// Left Button	    move Pos of sceneView
   565 		if (e->button() == Qt::LeftButton )
   566 		{
   567 			movingObj=NULL;	// move Content not Obj
   568 			movingObj_start=e->globalPos();
   569 			movingCont_start=QPointF (
   570 				horizontalScrollBar()->value(),
   571 				verticalScrollBar()->value());
   572 			movingVec=QPointF(0,0);
   573 			setCursor(HandOpenCursor);
   574 		} 
   575     } 
   576 }
   577 
   578 void MapEditor::mouseMoveEvent(QMouseEvent* e)
   579 {
   580     QPointF p = mapToScene(e->pos());
   581 	LinkableMapObj *lmosel=model->getSelection();
   582 
   583     // Move the selected MapObj
   584     if ( lmosel && movingObj) 
   585     {	
   586 		// reset cursor if we are moving and don't copy
   587 		if (mainWindow->getModMode()!=Main::ModModeCopy)
   588 			setCursor (Qt::ArrowCursor);
   589 
   590 		// To avoid jumping of the sceneView, only 
   591 		// ensureSelectionVisible, if not tmp linked
   592 		if (!lmosel->hasParObjTmp())
   593 			model->ensureSelectionVisible ();
   594 		
   595 		// Now move the selection, but add relative position 
   596 		// (movingObj_start) where selection was chosen with 
   597 		// mousepointer. (This avoids flickering resp. jumping 
   598 		// of selection back to absPos)
   599 		
   600 		// Check if we could link 
   601 		LinkableMapObj* lmo=model->findMapObj(p, lmosel);
   602 		
   603 
   604 		FloatObj *fio=model->getSelectedFloatImage();
   605 		if (fio)
   606 		{
   607 			fio->move   (p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );		
   608 			fio->setRelPos();
   609 			fio->updateLink(); //no need for reposition, if we update link here
   610 			model->updateSelection();
   611 
   612 			// Relink float to new mapcenter or branch, if shift is pressed	
   613 			// Only relink, if selection really has a new parent
   614 			if ( (e->modifiers()==Qt::ShiftModifier) && lmo &&
   615 				( (typeid(*lmo)==typeid(BranchObj)) ||
   616 				  (typeid(*lmo)==typeid(MapCenterObj)) ) &&
   617 				( lmo != fio->getParObj())  
   618 				)
   619 			{
   620 				if (typeid(*fio) == typeid(FloatImageObj) && 
   621 				( (typeid(*lmo)==typeid(BranchObj) ||
   622 				  typeid(*lmo)==typeid(MapCenterObj)) ))  
   623 				{
   624 
   625 					// Also save the move which was done so far
   626 					QString pold=qpointfToString(movingObj_orgRelPos);
   627 					QString pnow=qpointfToString(fio->getRelPos());
   628 					model->saveState(
   629 						fio,
   630 						"moveRel "+pold,
   631 						fio,
   632 						"moveRel "+pnow,
   633 						QString("Move %1 to relative position %2").arg(getName(fio)).arg(pnow));
   634 					fio->getParObj()->requestReposition();
   635 					model->reposition();
   636 
   637 					model->linkFloatImageTo (model->getSelectString(lmo));
   638 					//movingObj=lmosel;
   639 					//movingObj_orgRelPos=lmosel->getRelPos();	
   640 
   641 					model->reposition();
   642 				}	
   643 			}
   644 		} else	
   645 		{	// selection != a FloatObj
   646 			if (lmosel->getDepth()==0)
   647 			{
   648 				// Move MapCenter
   649 				if (e->buttons()== Qt::LeftButton && e->modifiers()==Qt::ShiftModifier) 
   650 					((MapCenterObj*)lmosel)->moveAll(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );		
   651 				else	
   652 					lmosel->move   (p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );		
   653 				model->updateRelPositions();
   654 			} else
   655 			{	
   656 				if (lmosel->getDepth()==1)
   657 				{
   658 					// Move mainbranch
   659 					lmosel->move(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );		
   660 					lmosel->setRelPos();
   661 				} else
   662 				{
   663 					// Move ordinary branch
   664 					if (lmosel->getOrientation() == LinkableMapObj::LeftOfCenter)
   665 						// Add width of bbox here, otherwise alignRelTo will cause jumping around
   666 						lmosel->move(p.x() -movingObj_start.x() , //lmosel->getBBox().width(), 
   667 							p.y()-movingObj_start.y() +lmosel->getTopPad() );		
   668 					else	
   669 						lmosel->move(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() -lmosel->getTopPad());
   670 					lmosel->setRelPos();	
   671 				} 
   672 
   673 				// Maybe we can relink temporary?
   674 				if (lmo && (lmo!=lmosel) && model->getSelectedBranch() && 
   675 					 (typeid(*lmo)==typeid(BranchObj) ||
   676 					  typeid(*lmo)==typeid(MapCenterObj)) ) 
   677 
   678 				{
   679 					if (e->modifiers()==Qt::ControlModifier)
   680 					{
   681 						// Special case: CTRL to link below lmo
   682 						lmosel->setParObjTmp (lmo,p,+1);
   683 					}
   684 					else if (e->modifiers()==Qt::ShiftModifier)
   685 						lmosel->setParObjTmp (lmo,p,-1);
   686 					else
   687 						lmosel->setParObjTmp (lmo,p,0);
   688 				} else	
   689 				{
   690 					lmosel->unsetParObjTmp();
   691 				}		
   692 				// reposition subbranch
   693 				lmosel->reposition();	
   694 			} // depth>0
   695 
   696 			model->updateSelection();// FIXME needed? 
   697 		} // no FloatImageObj
   698 
   699 		scene()->update();
   700 		return;
   701 	} // selection && moving_obj
   702 		
   703 	// Draw a link from one branch to another
   704 	if (drawingLink)
   705 	{
   706 		 tmpXLink->setEnd (p);
   707 		 tmpXLink->updateXLink();
   708 	}	 
   709 	
   710     // Move sceneView 
   711     if (!movingObj && !pickingColor &&!drawingLink && e->buttons() == Qt::LeftButton ) 
   712 	{
   713 		QPointF p=e->globalPos();
   714 		movingVec.setX(-p.x() + movingObj_start.x() );
   715 		movingVec.setY(-p.y() + movingObj_start.y() );
   716 		horizontalScrollBar()->setSliderPosition((int)( movingCont_start.x()+movingVec.x() ));
   717 		verticalScrollBar()->setSliderPosition((int)( movingCont_start.y()+movingVec.y() ) );
   718     }
   719 }
   720 
   721 
   722 void MapEditor::mouseReleaseEvent(QMouseEvent* e)
   723 {
   724     QPointF p = mapToScene(e->pos());
   725 	LinkableMapObj *dst;
   726 	LinkableMapObj *lmosel=model->getSelection();
   727 	// Have we been picking color?
   728 	if (pickingColor)
   729 	{
   730 		pickingColor=false;
   731 		setCursor (Qt::ArrowCursor);
   732 		// Check if we are over another branch
   733 		dst=model->findMapObj(p, NULL);
   734 		if (dst && lmosel) 
   735 		{	
   736 			if (e->state() & Qt::ShiftModifier)
   737 				model->colorBranch (((BranchObj*)dst)->getColor());
   738 			else	
   739 				model->colorSubtree (((BranchObj*)dst)->getColor());
   740 		} 
   741 		return;
   742 	}
   743 
   744 	// Have we been drawing a link?
   745 	if (drawingLink)	
   746 	{
   747 		drawingLink=false;
   748 		// Check if we are over another branch
   749 		dst=model->findMapObj(p, NULL);
   750 		if (dst && lmosel) 
   751 		{	
   752 			tmpXLink->setEnd ( ((BranchObj*)(dst)) );
   753 			tmpXLink->updateXLink();
   754 			tmpXLink->activate(); //FIXME savestate missing
   755 			//model->saveStateComplete(QString("Activate xLink from %1 to %2").arg(getName(tmpXLink->getBegin())).arg(getName(tmpXLink->getEnd())) );	
   756 		} else
   757 		{
   758 			delete(tmpXLink);
   759 			tmpXLink=NULL;
   760 		}
   761 		return;
   762 	}
   763 	
   764     // Have we been moving something?
   765     if ( lmosel && movingObj ) 
   766     {	
   767 		FloatImageObj *fo=model->getSelectedFloatImage();
   768 		if(fo)
   769 		{
   770 			// Moved FloatObj. Maybe we need to reposition
   771 		    QString pold=qpointfToString(movingObj_orgRelPos);
   772 		    QString pnow=qpointfToString(fo->getRelPos());
   773 			model->saveState(
   774 				fo,
   775 				"moveRel "+pold,
   776 				fo,
   777 				"moveRel "+pnow,
   778 				QString("Move %1 to relative position %2").arg(getName(fo)).arg(pnow));
   779 
   780 			fo->getParObj()->requestReposition();
   781 			model->reposition();
   782 		}	
   783 
   784 		// Check if we are over another branch, but ignore 
   785 		// any found LMOs, which are FloatObjs
   786 		dst=model->findMapObj(mapToScene(e->pos() ), lmosel);
   787 
   788 		if (dst && (typeid(*dst)!=typeid(BranchObj) && typeid(*dst)!=typeid(MapCenterObj))) 
   789 			dst=NULL;
   790 		
   791 		BranchObj *bo=model->getSelectedBranch();
   792 		if (bo && bo->getDepth()==0)
   793 		{	
   794             if (movingObj_orgPos != bo->getAbsPos())
   795             {
   796                 QString pold=qpointfToString(movingObj_orgPos);
   797                 QString pnow=qpointfToString(bo->getAbsPos());
   798                 model->saveState(
   799                     fo,
   800                     "move "+pold,
   801                     fo,
   802                     "move "+pnow,
   803                     QString("Move mapcenter %1 to position %2").arg(getName(bo)).arg(pnow));
   804             }
   805 		}
   806 	
   807 		if (model->selectionType() == Selection::Branch )
   808 		{	// A branch was moved
   809 			
   810 			// save the position in case we link to mapcenter
   811 			QPointF savePos=QPointF (lmosel->getAbsPos()  );
   812 
   813 			// Reset the temporary drawn link to the original one
   814 			lmosel->unsetParObjTmp();
   815 
   816 			// For Redo we may need to save original selection
   817 			QString preSelStr=model->getSelectString(lmosel);
   818 
   819 			copyingObj=false;	
   820 			if (dst ) 
   821 			{
   822 				// We have a destination, relink to that
   823 
   824 				BranchObj* bsel=model->getSelectedBranch();
   825 				BranchObj* bdst=(BranchObj*)dst;
   826 
   827 				QString preParStr=model->getSelectString (bsel->getParObj());
   828 				QString preNum=QString::number (bsel->getNum(),10);
   829 				QString preDstParStr;
   830 
   831 				if (e->state() & Qt::ShiftModifier && dst->getParObj())
   832 				{	// Link above dst
   833 					preDstParStr=model->getSelectString (dst->getParObj());
   834 					bsel->linkTo ( (BranchObj*)(bdst->getParObj()), bdst->getNum());
   835 				} else 
   836 				if (e->state() & Qt::ControlModifier && dst->getParObj())
   837 				{
   838 					// Link below dst
   839 					preDstParStr=model->getSelectString (dst->getParObj());
   840 					bsel->linkTo ( (BranchObj*)(bdst->getParObj()), bdst->getNum()+1);
   841 				} else	
   842 				{	// Append to dst
   843 					preDstParStr=model->getSelectString(dst);
   844 					bsel->linkTo (bdst,-1);
   845 					if (dst->getDepth()==0) bsel->move (savePos);
   846 				} 
   847 				QString postSelStr=model->getSelectString(lmosel);
   848 				QString postNum=QString::number (bsel->getNum(),10);
   849 
   850 				QString undoCom="linkTo (\""+ 
   851 					preParStr+ "\"," + preNum  +"," + 
   852 					QString ("%1,%2").arg(movingObj_orgPos.x()).arg(movingObj_orgPos.y())+ ")";
   853 
   854 				QString redoCom="linkTo (\""+ 
   855 					preDstParStr + "\"," + postNum + "," +
   856 					QString ("%1,%2").arg(savePos.x()).arg(savePos.y())+ ")";
   857 
   858 				model->saveState (
   859 					postSelStr,undoCom,
   860 					preSelStr, redoCom,
   861 					QString("Relink %1 to %2").arg(getName(bsel)).arg(getName(dst)) );
   862 
   863 				model->reposition();	// not necessary if we undo temporary move  below
   864 			} else
   865 			{
   866 				// No destination, undo  temporary move
   867 
   868 				if (lmosel->getDepth()==1)
   869 				{
   870 					// The select string might be different _after_ moving around.
   871 					// Therefor reposition and then use string of old selection, too
   872 					model->reposition();
   873 
   874                     QPointF rp(lmosel->getRelPos());
   875                     if (rp != movingObj_orgRelPos)
   876                     {
   877                         QString ps=qpointfToString(rp);
   878                         model->saveState(
   879                             model->getSelectString(lmosel), "moveRel "+qpointfToString(movingObj_orgRelPos), 
   880                             preSelStr, "moveRel "+ps, 
   881                             QString("Move %1 to relative position %2").arg(getName(lmosel)).arg(ps));
   882                     }
   883 				}
   884 
   885 				// Draw the original link, before selection was moved around
   886 				if (settings.value("/animation/use",false).toBool() && lmosel->getDepth()>1) 
   887 				{
   888 					lmosel->setRelPos();	// calc relPos first for starting point
   889 					QPointF dst=bo->getParObj()->getChildPos();
   890 			//		if (lmosel->getOrientation()==LinkableMapObj::LeftOfCenter) dst.setX (dst.x()+lmosel->width() );
   891 					
   892 					model->startAnimation(
   893 						lmosel->getRelPos(),
   894 						movingObj_orgRelPos
   895 //						QPointF (movingObj_orgPos.x() - dst.x(), movingObj_orgPos.y() - dst.y() )
   896 					);	
   897 				} else	
   898 					model->reposition();
   899 			}
   900 		}
   901 		 model->updateSelection();
   902 		// Finally resize scene, if needed
   903 		scene()->update();
   904 		movingObj=NULL;		
   905 
   906 		// Just make sure, that actions are still ok,e.g. the move branch up/down buttons...
   907 		model->updateActions();
   908 	} else 
   909 		// maybe we moved View: set old cursor
   910 		setCursor (Qt::ArrowCursor);
   911     
   912 }
   913 
   914 void MapEditor::mouseDoubleClickEvent(QMouseEvent* e)
   915 {
   916 	if (model->isSelectionBlocked() ) 
   917 	{
   918 		e->ignore();
   919 		return;
   920 	}
   921 
   922 	if (e->button() == Qt::LeftButton )
   923 	{
   924 		QPointF p = mapToScene(e->pos());
   925 		LinkableMapObj *lmo=model->findMapObj(p, NULL);
   926 		if (lmo) {	// MapObj was found
   927 			// First select the MapObj than edit heading
   928 			model->select (lmo);
   929 			editHeading();
   930 		}
   931 	}
   932 }
   933 
   934 void MapEditor::resizeEvent (QResizeEvent* e)
   935 {
   936 	QGraphicsView::resizeEvent( e );
   937 }
   938 
   939 void MapEditor::dragEnterEvent(QDragEnterEvent *event)
   940 {
   941 	//for (unsigned int i=0;event->format(i);i++) // Debug mime type
   942 	//	cerr << event->format(i) << endl;
   943 
   944 	if (event->mimeData()->hasImage())
   945 		event->acceptProposedAction();
   946 	else	
   947 		if (event->mimeData()->hasUrls())
   948 			event->acceptProposedAction();
   949 }
   950 
   951 void MapEditor::dragMoveEvent(QDragMoveEvent *)
   952 {
   953 }
   954 
   955 void MapEditor::dragLeaveEvent(QDragLeaveEvent *event)
   956 {
   957 	event->accept();
   958 }
   959 
   960 void MapEditor::dropEvent(QDropEvent *event)
   961 {
   962 	BranchObj *sel=model->getSelectedBranch();
   963 	if (sel)
   964 	{
   965 		if (debug)
   966 			foreach (QString format,event->mimeData()->formats()) 
   967 				cout << "MapEditor: Dropped format: "<<qPrintable (format)<<endl;
   968 
   969 
   970 		QList <QUrl> uris;
   971 		if (event->mimeData()->hasImage()) 
   972 		{
   973 			 QVariant imageData = event->mimeData()->imageData();
   974 			 model->addFloatImage (qvariant_cast<QPixmap>(imageData));
   975 		} else
   976 		if (event->mimeData()->hasUrls())
   977 			uris=event->mimeData()->urls();
   978 
   979 		if (uris.count()>0)
   980 		{
   981 			QStringList files;
   982 			QString s;
   983 			QString heading;
   984 			BranchObj *bo;
   985 			for (int i=0; i<uris.count();i++)
   986 			{
   987 				// Workaround to avoid adding empty branches
   988 				if (!uris.at(i).toString().isEmpty())
   989 				{
   990 					bo=sel->addBranch();
   991 					if (bo)
   992 					{
   993 						s=uris.at(i).toLocalFile();
   994 						if (!s.isEmpty()) 
   995 						{
   996 						   QString file = QDir::fromNativeSeparators(s);
   997 						   heading = QFileInfo(file).baseName();
   998 						   files.append(file);
   999 						   if (file.endsWith(".vym", false))
  1000 							   bo->setVymLink(file);
  1001 						   else
  1002 							   bo->setURL(uris.at(i).toString());
  1003 					   } else 
  1004 					   {
  1005 						   bo->setURL(uris.at(i).toString());
  1006 					   }
  1007 
  1008 					   if (!heading.isEmpty())
  1009 						   bo->setHeading(heading);
  1010 					   else
  1011 						   bo->setHeading(uris.at(i).toString());
  1012 					}
  1013 				}
  1014 			}
  1015 			model->reposition();
  1016 		}
  1017 	}	
  1018 	event->acceptProposedAction();
  1019 }
  1020 
  1021 
  1022 
  1023 bool isUnicode16(const QByteArray &d) 
  1024 {
  1025   // TODO: make more precise check for unicode 16.
  1026   // Guess unicode16 if any of second bytes are zero
  1027   unsigned int length = max(0,d.size()-2)/2;
  1028   for (unsigned int i = 0; i<length ; i++)
  1029     if (d.at(i*2+1)==0) return true;
  1030   return false;
  1031 }
  1032       
  1033 
  1034 // FIXME the following are not needed...
  1035 QString MapEditor::getName(const LinkableMapObj*) {return QString();}
  1036