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