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