mapeditor.cpp
author insilmaril
Tue, 04 Nov 2008 12:09:10 +0000
changeset 728 a8a98a94899a
parent 727 96402b172173
child 729 7ddbe3fa34a1
permissions -rw-r--r--
Selections work (a little bit)
     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 	selModel=vm->getSelectionModel();
    94 	selectionColor =QColor (255,255,0);
    95 	
    96 
    97 	// Attributes	//FIXME testing only...
    98 	QString k;
    99 	AttributeDef *ad;
   100 	attrTable= new AttributeTable();
   101 	k="A - StringList";
   102 	ad=attrTable->addKey (k,StringList);
   103 	if (ad)
   104 	{
   105 		QStringList sl;
   106 		sl <<"val 1"<<"val 2"<< "val 3";
   107 		ad->setValue (QVariant (sl));
   108 	}
   109 	//attrTable->addValue ("Key A","P 1");
   110 	//attrTable->addValue ("Key A","P 2");
   111 	//attrTable->addValue ("Key A","P 3");
   112 	//attrTable->addValue ("Key A","P 4");
   113 	k="B - FreeString";
   114 	ad=attrTable->addKey (k,FreeString);
   115 	if (ad)
   116 	{
   117 		//attrTable->addValue ("Key B","w1");
   118 		//attrTable->addValue ("Key B","w2");
   119 	}
   120 	k="C - UniqueString";
   121 	ad=attrTable->addKey (k,UniqueString);
   122 	if (ad)
   123 	{
   124 	//attrTable->addKey ("Key Prio");
   125 	//attrTable->addValue ("Key Prio","Prio 1");
   126 	//attrTable->addValue ("Key Prio","Prio 2");
   127 	}
   128 }
   129 
   130 MapEditor::~MapEditor()
   131 {
   132 	//cout <<"Destructor MapEditor\n";
   133 	// tmpMapDir is in tmpVymDir, so it gets removed automagically when vym closes
   134 	
   135 	//removeDir(QDir(tmpMapDir));	// FIXME check?!?
   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 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();
   276 	}
   277 }
   278 
   279 AttributeTable* MapEditor::attributeTable()
   280 {
   281 	return attrTable;
   282 }
   283 
   284 void MapEditor::testFunction1()
   285 {
   286 	//BranchObj *bo=model->getSelectedBranch();
   287 	//if (bo) model->moveAway (bo);
   288 	//if (bo) bo->setLinkStyle (LinkableMapObj::Line);
   289 	
   290 
   291 	// Displacement and animation of all non-mainbranches
   292 	QPointF p;
   293 	QPointF q;
   294 	BranchObj *bo;
   295 	bo=model->first();
   296 	while (bo) 
   297 	{
   298 		if (bo->getDepth() >0 && !bo->hasScrolledParent(bo) )
   299 		{
   300 			p=QPointF (qrand() %600-300, qrand () %600-300);
   301 			bo->setRelPos();
   302 			q=bo->getRelPos();
   303 			model->startAnimation (bo,p, q);
   304 		}
   305 		bo=model->next(bo);
   306 	}
   307 
   308 
   309 
   310 /* TODO Hide hidden stuff temporary, maybe add this as regular function somewhere
   311 	if (hidemode==HideNone)
   312 	{
   313 		setHideTmpMode (HideExport);
   314 		mapCenter->calcBBoxSizeWithChilds();
   315 		QRectF totalBBox=mapCenter->getTotalBBox();
   316 		QRectF mapRect=totalBBox;
   317 		QCanvasRectangle *frame=NULL;
   318 
   319 		cout << "  map has =("<<totalBBox.x()<<","<<totalBBox.y()<<","<<totalBBox.width()<<","<<totalBBox.height()<<")\n";
   320 	
   321 		mapRect.setRect (totalBBox.x(), totalBBox.y(), 
   322 			totalBBox.width(), totalBBox.height());
   323 		frame=new QCanvasRectangle (mapRect,mapScene);
   324 		frame->setBrush (QColor(white));
   325 		frame->setPen (QColor(black));
   326 		frame->setZValue(0);
   327 		frame->show();    
   328 	}	
   329 	else	
   330 	{
   331 		setHideTmpMode (HideNone);
   332 	}	
   333 	cout <<"  hidemode="<<hidemode<<endl;
   334 	*/
   335 }	
   336 	
   337 void MapEditor::testFunction2()
   338 {
   339 
   340 /*
   341 	// Toggle hidemode
   342 	if (hidemode==HideExport)
   343 		setHideTmpMode (HideNone);
   344 	else	
   345 		setHideTmpMode (HideExport);
   346 */		
   347 }
   348 
   349 void MapEditor::editHeading()
   350 {
   351 	if (editingHeading)
   352 	{
   353 		editHeadingFinished();
   354 		return;
   355 	}
   356 	BranchObj *bo=model->getSelectedBranch();
   357 	if (bo)
   358 	{
   359 		model->setSelectionBlocked(true);
   360 
   361 		lineEdit->setText (bo->getHeading());
   362 		QPoint p = mapTo (this,bo->getAbsPos().toPoint() );
   363 		lineEdit->setGeometry(p.x(),p.y(),230,25);
   364 		lineEdit->selectAll();
   365 		lineEdit->show();
   366 		lineEdit->setFocus();
   367 		lineEdit->grabKeyboard();
   368 		editingHeading=true;
   369 	}
   370 
   371 }
   372 void MapEditor::editHeadingFinished()
   373 {
   374 	editingHeading=false;
   375 	lineEdit->releaseKeyboard();
   376 	model->setHeading (lineEdit->text() );
   377 	model->setSelectionBlocked(false);
   378 	lineEdit->hide();
   379 
   380 	// Maybe reselect previous branch 
   381 	mainWindow->editHeadingFinished (model);
   382 }
   383 
   384 
   385 void MapEditor::contextMenuEvent ( QContextMenuEvent * e )
   386 {
   387 	// Lineedits are already closed by preceding
   388 	// mouseEvent, we don't need to close here.
   389 
   390     QPointF p = mapToScene(e->pos());
   391     LinkableMapObj* lmo=model->findMapObj(p, NULL);
   392 	
   393     if (lmo) 
   394 	{	// MapObj was found
   395 		if (model->getSelection() != lmo)
   396 		{
   397 			// select the MapObj
   398 			model->select(lmo);
   399 		}
   400 		// Context Menu 
   401 		if (model->getSelectedBranch() ) 
   402 		{
   403 			// Context Menu on branch or mapcenter
   404 			model->updateActions();
   405 			branchContextMenu->popup(e->globalPos() );
   406 		} else
   407 		{
   408 			if (model->getSelectedFloatImage() )
   409 			{
   410 				// Context Menu on floatimage
   411 				model->updateActions();
   412 				floatimageContextMenu->popup(e->globalPos() );
   413 			}	
   414 		}	
   415 	} else 
   416 	{ // No MapObj found, we are on the Canvas itself
   417 		// Context Menu on scene
   418 		model->updateActions();
   419 		
   420 		// Open context menu synchronously to position new mapcenter
   421 		model->setContextPos (p);
   422 		canvasContextMenu->exec(e->globalPos() );
   423 		model->unsetContextPos ();
   424     } 
   425 	e->accept();
   426 }
   427 
   428 void MapEditor::keyPressEvent(QKeyEvent* e)
   429 {
   430 	if (e->modifiers() & Qt::ControlModifier)
   431 	{
   432 		switch (mainWindow->getModMode())
   433 		{
   434 			case Main::ModModeColor: 
   435 				setCursor (PickColorCursor);
   436 				break;
   437 			case Main::ModModeCopy: 
   438 				setCursor (CopyCursor);
   439 				break;
   440 			case Main::ModModeXLink: 
   441 				setCursor (XLinkCursor);
   442 				break;
   443 			default :
   444 				setCursor (Qt::ArrowCursor);
   445 				break;
   446 		} 
   447 	}	
   448 }
   449 
   450 void MapEditor::keyReleaseEvent(QKeyEvent* e)
   451 {
   452 	if (!(e->modifiers() & Qt::ControlModifier))
   453 		setCursor (Qt::ArrowCursor);
   454 }
   455 
   456 void MapEditor::mousePressEvent(QMouseEvent* e)
   457 {
   458 	// Ignore right clicks, these will go to context menus
   459 	if (e->button() == Qt::RightButton )
   460 	{
   461 		e->ignore();
   462 		return;
   463 	}
   464 
   465 	//Ignore clicks while editing heading
   466 	if (model->isSelectionBlocked() ) 
   467 	{
   468 		e->ignore();
   469 		return;
   470 	}
   471 
   472     QPointF p = mapToScene(e->pos());
   473     LinkableMapObj* lmo=model->findMapObj(p, NULL);
   474 	
   475 	e->accept();
   476 
   477 	//Take care of  system flags _or_ modifier modes
   478 	//
   479 	if (lmo && (typeid(*lmo)==typeid(BranchObj) ||
   480 		typeid(*lmo)==typeid(MapCenterObj) ))
   481 	{
   482 		QString foname=((BranchObj*)lmo)->getSystemFlagName(p);
   483 		if (!foname.isEmpty())
   484 		{
   485 			// systemFlag clicked
   486 			model->selectInt (lmo);
   487 			if (foname=="url") 
   488 			{
   489 				if (e->state() & Qt::ControlModifier)
   490 					mainWindow->editOpenURLTab();
   491 				else	
   492 					mainWindow->editOpenURL();
   493 			}	
   494 			else if (foname=="vymLink")
   495 			{
   496 				mainWindow->editOpenVymLink();
   497 				// tabWidget may change, better return now
   498 				// before segfaulting...
   499 			} else if (foname=="note")
   500 				mainWindow->windowToggleNoteEditor();
   501 			else if (foname=="hideInExport")		
   502 				model->toggleHideExport();
   503 			// FIXME needed? xelection.update();	
   504 			return;	
   505 		} 
   506 	}	
   507 	// No system flag clicked, take care of modmodes (CTRL-Click)
   508 	if (e->state() & Qt::ControlModifier)
   509 	{
   510 		if (mainWindow->getModMode()==Main::ModModeColor)
   511 		{
   512 				pickingColor=true;
   513 				setCursor (PickColorCursor);
   514 				return;
   515 		} 
   516 		if (mainWindow->getModMode()==Main::ModModeXLink)
   517 		{	
   518 			BranchObj *bo_begin=NULL;
   519 			if (lmo)
   520 				bo_begin=(BranchObj*)(lmo);
   521 			else	
   522 				bo_begin=model->getSelectedBranch();
   523 			if (bo_begin)	
   524 			{
   525 				drawingLink=true;
   526 				linkingObj_src=bo_begin;
   527 				tmpXLink=new XLinkObj (mapScene);
   528 				tmpXLink->setBegin (bo_begin);
   529 				tmpXLink->setEnd   (p);
   530 				tmpXLink->setColor(model->getMapDefXLinkColor());
   531 				tmpXLink->setWidth(model->getMapDefXLinkWidth());
   532 				tmpXLink->updateXLink();
   533 				tmpXLink->setVisibility (true);
   534 				return;
   535 			} 
   536 		}
   537 	}	// End of modmodes
   538 
   539     if (lmo) 
   540 	{	
   541 		// Select the clicked object
   542 		model->selectInt (lmo);
   543 
   544 		// Left Button	    Move Branches
   545 		if (e->button() == Qt::LeftButton )
   546 		{
   547 			//movingObj_start.setX( p.x() - selection->x() );// TODO replaced selection->lmo here	
   548 			//movingObj_start.setY( p.y() - selection->y() );	
   549 			movingObj_start.setX( p.x() - lmo->x() );	
   550 			movingObj_start.setY( p.y() - lmo->y() );	
   551 			movingObj_orgPos.setX (lmo->x() );
   552 			movingObj_orgPos.setY (lmo->y() );
   553 			lmo->setRelPos();
   554 			movingObj_orgRelPos=lmo->getRelPos();
   555 
   556 			// If modMode==copy, then we want to "move" the _new_ object around
   557 			// then we need the offset from p to the _old_ selection, because of tmp
   558 			if (mainWindow->getModMode()==Main::ModModeCopy &&
   559 				e->state() & Qt::ControlModifier)
   560 			{
   561 				BranchObj *bo=model->getSelectedBranch();
   562 				if (bo)
   563 				{
   564 					copyingObj=true;
   565 					bo->addBranch ((BranchObj*)model->getSelection());
   566 					model->unselect();
   567 					model->select(bo->getLastBranch());
   568 					model->reposition();
   569 				}
   570 			} 
   571 
   572 			movingObj=model->getSelection();	
   573 		} else
   574 			// Middle Button    Toggle Scroll
   575 			// (On Mac OS X this won't work, but we still have 
   576 			// a button in the toolbar)
   577 			if (e->button() == Qt::MidButton )
   578 				model->toggleScroll();
   579 		model->updateActions();
   580 		// FIXME needed? xelection.update();
   581 	} else 
   582 	{ // No MapObj found, we are on the scene itself
   583 		// Left Button	    move Pos of sceneView
   584 		if (e->button() == Qt::LeftButton )
   585 		{
   586 			movingObj=NULL;	// move Content not Obj
   587 			movingObj_start=e->globalPos();
   588 			movingCont_start=QPointF (
   589 				horizontalScrollBar()->value(),
   590 				verticalScrollBar()->value());
   591 			movingVec=QPointF(0,0);
   592 			setCursor(HandOpenCursor);
   593 		} 
   594     } 
   595 }
   596 
   597 void MapEditor::mouseMoveEvent(QMouseEvent* e)
   598 {
   599     QPointF p = mapToScene(e->pos());
   600 	LinkableMapObj *lmosel=model->getSelection();
   601 
   602     // Move the selected MapObj
   603     if ( lmosel && movingObj) 
   604     {	
   605 		// reset cursor if we are moving and don't copy
   606 		if (mainWindow->getModMode()!=Main::ModModeCopy)
   607 			setCursor (Qt::ArrowCursor);
   608 
   609 		// To avoid jumping of the sceneView, only 
   610 		// ensureSelectionVisible, if not tmp linked
   611 		if (!lmosel->hasParObjTmp())
   612 			model->ensureSelectionVisible ();
   613 		
   614 		// Now move the selection, but add relative position 
   615 		// (movingObj_start) where selection was chosen with 
   616 		// mousepointer. (This avoids flickering resp. jumping 
   617 		// of selection back to absPos)
   618 		
   619 		// Check if we could link 
   620 		LinkableMapObj* lmo=model->findMapObj(p, lmosel);
   621 		
   622 
   623 		FloatObj *fio=model->getSelectedFloatImage();
   624 		if (fio)
   625 		{
   626 			fio->move   (p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );		
   627 			fio->setRelPos();
   628 			fio->updateLink(); //no need for reposition, if we update link here
   629 			model->updateSelection();
   630 
   631 			// Relink float to new mapcenter or branch, if shift is pressed	
   632 			// Only relink, if selection really has a new parent
   633 			if ( (e->modifiers()==Qt::ShiftModifier) && lmo &&
   634 				( (typeid(*lmo)==typeid(BranchObj)) ||
   635 				  (typeid(*lmo)==typeid(MapCenterObj)) ) &&
   636 				( lmo != fio->getParObj())  
   637 				)
   638 			{
   639 				if (typeid(*fio) == typeid(FloatImageObj) && 
   640 				( (typeid(*lmo)==typeid(BranchObj) ||
   641 				  typeid(*lmo)==typeid(MapCenterObj)) ))  
   642 				{
   643 
   644 					// Also save the move which was done so far
   645 					QString pold=qpointfToString(movingObj_orgRelPos);
   646 					QString pnow=qpointfToString(fio->getRelPos());
   647 					model->saveState(
   648 						fio,
   649 						"moveRel "+pold,
   650 						fio,
   651 						"moveRel "+pnow,
   652 						QString("Move %1 to relative position %2").arg(model->getObjectName(fio)).arg(pnow));
   653 					fio->getParObj()->requestReposition();
   654 					model->reposition();
   655 
   656 					model->linkFloatImageTo (model->getSelectString(lmo));
   657 					//movingObj=lmosel;
   658 					//movingObj_orgRelPos=lmosel->getRelPos();	
   659 
   660 					model->reposition();
   661 				}	
   662 			}
   663 		} else	
   664 		{	// selection != a FloatObj
   665 			if (lmosel->getDepth()==0)
   666 			{
   667 				// Move MapCenter
   668 				if (e->buttons()== Qt::LeftButton && e->modifiers()==Qt::ShiftModifier) 
   669 					((MapCenterObj*)lmosel)->moveAll(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );		
   670 				else	
   671 					lmosel->move   (p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );		
   672 				model->updateRelPositions();
   673 			} else
   674 			{	
   675 				if (lmosel->getDepth()==1)
   676 				{
   677 					// Move mainbranch
   678 					lmosel->move(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );		
   679 					lmosel->setRelPos();
   680 				} else
   681 				{
   682 					// Move ordinary branch
   683 					if (lmosel->getOrientation() == LinkableMapObj::LeftOfCenter)
   684 						// Add width of bbox here, otherwise alignRelTo will cause jumping around
   685 						lmosel->move(p.x() -movingObj_start.x() , //lmosel->getBBox().width(), 
   686 							p.y()-movingObj_start.y() +lmosel->getTopPad() );		
   687 					else	
   688 						lmosel->move(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() -lmosel->getTopPad());
   689 					lmosel->setRelPos();	
   690 				} 
   691 
   692 				// Maybe we can relink temporary?
   693 				if (lmo && (lmo!=lmosel) && model->getSelectedBranch() && 
   694 					 (typeid(*lmo)==typeid(BranchObj) ||
   695 					  typeid(*lmo)==typeid(MapCenterObj)) ) 
   696 
   697 				{
   698 					if (e->modifiers()==Qt::ControlModifier)
   699 					{
   700 						// Special case: CTRL to link below lmo
   701 						lmosel->setParObjTmp (lmo,p,+1);
   702 					}
   703 					else if (e->modifiers()==Qt::ShiftModifier)
   704 						lmosel->setParObjTmp (lmo,p,-1);
   705 					else
   706 						lmosel->setParObjTmp (lmo,p,0);
   707 				} else	
   708 				{
   709 					lmosel->unsetParObjTmp();
   710 				}		
   711 				// reposition subbranch
   712 				lmosel->reposition();	
   713 			} // depth>0
   714 
   715 			model->updateSelection();// FIXME needed? 
   716 		} // no FloatImageObj
   717 
   718 		scene()->update();
   719 		return;
   720 	} // selection && moving_obj
   721 		
   722 	// Draw a link from one branch to another
   723 	if (drawingLink)
   724 	{
   725 		 tmpXLink->setEnd (p);
   726 		 tmpXLink->updateXLink();
   727 	}	 
   728 	
   729     // Move sceneView 
   730     if (!movingObj && !pickingColor &&!drawingLink && e->buttons() == Qt::LeftButton ) 
   731 	{
   732 		QPointF p=e->globalPos();
   733 		movingVec.setX(-p.x() + movingObj_start.x() );
   734 		movingVec.setY(-p.y() + movingObj_start.y() );
   735 		horizontalScrollBar()->setSliderPosition((int)( movingCont_start.x()+movingVec.x() ));
   736 		verticalScrollBar()->setSliderPosition((int)( movingCont_start.y()+movingVec.y() ) );
   737     }
   738 }
   739 
   740 
   741 void MapEditor::mouseReleaseEvent(QMouseEvent* e)
   742 {
   743     QPointF p = mapToScene(e->pos());
   744 	LinkableMapObj *dst;
   745 	LinkableMapObj *lmosel=model->getSelection();
   746 	// Have we been picking color?
   747 	if (pickingColor)
   748 	{
   749 		pickingColor=false;
   750 		setCursor (Qt::ArrowCursor);
   751 		// Check if we are over another branch
   752 		dst=model->findMapObj(p, NULL);
   753 		if (dst && lmosel) 
   754 		{	
   755 			if (e->state() & Qt::ShiftModifier)
   756 				model->colorBranch (((BranchObj*)dst)->getColor());
   757 			else	
   758 				model->colorSubtree (((BranchObj*)dst)->getColor());
   759 		} 
   760 		return;
   761 	}
   762 
   763 	// Have we been drawing a link?
   764 	if (drawingLink)	
   765 	{
   766 		drawingLink=false;
   767 		// Check if we are over another branch
   768 		dst=model->findMapObj(p, NULL);
   769 		if (dst && lmosel) 
   770 		{	
   771 			tmpXLink->setEnd ( ((BranchObj*)(dst)) );
   772 			tmpXLink->updateXLink();
   773 			tmpXLink->activate(); //FIXME savestate missing
   774 			//model->saveStateComplete(QString("Activate xLink from %1 to %2").arg(model->getObjectName(tmpXLink->getBegin())).arg(model->getObjectName(tmpXLink->getEnd())) );	
   775 		} else
   776 		{
   777 			delete(tmpXLink);
   778 			tmpXLink=NULL;
   779 		}
   780 		return;
   781 	}
   782 	
   783     // Have we been moving something?
   784     if ( lmosel && movingObj ) 
   785     {	
   786 		FloatImageObj *fo=model->getSelectedFloatImage();
   787 		if(fo)
   788 		{
   789 			// Moved FloatObj. Maybe we need to reposition
   790 		    QString pold=qpointfToString(movingObj_orgRelPos);
   791 		    QString pnow=qpointfToString(fo->getRelPos());
   792 			model->saveState(
   793 				fo,
   794 				"moveRel "+pold,
   795 				fo,
   796 				"moveRel "+pnow,
   797 				QString("Move %1 to relative position %2").arg(model->getObjectName(fo)).arg(pnow));
   798 
   799 			fo->getParObj()->requestReposition();
   800 			model->reposition();
   801 		}	
   802 
   803 		// Check if we are over another branch, but ignore 
   804 		// any found LMOs, which are FloatObjs
   805 		dst=model->findMapObj(mapToScene(e->pos() ), lmosel);
   806 
   807 		if (dst && (typeid(*dst)!=typeid(BranchObj) && typeid(*dst)!=typeid(MapCenterObj))) 
   808 			dst=NULL;
   809 		
   810 		BranchObj *bo=model->getSelectedBranch();
   811 		if (bo && bo->getDepth()==0)
   812 		{	
   813             if (movingObj_orgPos != bo->getAbsPos())
   814             {
   815                 QString pold=qpointfToString(movingObj_orgPos);
   816                 QString pnow=qpointfToString(bo->getAbsPos());
   817                 model->saveState(
   818                     bo,
   819                     "move "+pold,
   820                     bo,
   821                     "move "+pnow,
   822                     QString("Move mapcenter %1 to position %2").arg(model->getObjectName(bo)).arg(pnow));
   823             }
   824 		}
   825 	
   826 		if (model->selectionType() == Selection::Branch )
   827 		{	// A branch was moved
   828 			
   829 			// save the position in case we link to mapcenter
   830 			QPointF savePos=QPointF (lmosel->getAbsPos()  );
   831 
   832 			// Reset the temporary drawn link to the original one
   833 			lmosel->unsetParObjTmp();
   834 
   835 			// For Redo we may need to save original selection
   836 			QString preSelStr=model->getSelectString(lmosel);
   837 
   838 			copyingObj=false;	
   839 			if (dst ) 
   840 			{
   841 				// We have a destination, relink to that
   842 
   843 				BranchObj* bsel=model->getSelectedBranch();
   844 				BranchObj* bdst=(BranchObj*)dst;
   845 
   846 				QString preParStr=model->getSelectString (bsel->getParObj());
   847 				QString preNum=QString::number (bsel->getNum(),10);
   848 				QString preDstParStr;
   849 
   850 				if (e->state() & Qt::ShiftModifier && dst->getParObj())
   851 				{	// Link above dst
   852 					preDstParStr=model->getSelectString (dst->getParObj());
   853 					bsel->linkTo ( (BranchObj*)(bdst->getParObj()), bdst->getNum());
   854 				} else 
   855 				if (e->state() & Qt::ControlModifier && dst->getParObj())
   856 				{
   857 					// Link below dst
   858 					preDstParStr=model->getSelectString (dst->getParObj());
   859 					bsel->linkTo ( (BranchObj*)(bdst->getParObj()), bdst->getNum()+1);
   860 				} else	
   861 				{	// Append to dst
   862 					preDstParStr=model->getSelectString(dst);
   863 					bsel->linkTo (bdst,-1);
   864 					if (dst->getDepth()==0) bsel->move (savePos);
   865 				} 
   866 				QString postSelStr=model->getSelectString(lmosel);
   867 				QString postNum=QString::number (bsel->getNum(),10);
   868 
   869 				QString undoCom="linkTo (\""+ 
   870 					preParStr+ "\"," + preNum  +"," + 
   871 					QString ("%1,%2").arg(movingObj_orgPos.x()).arg(movingObj_orgPos.y())+ ")";
   872 
   873 				QString redoCom="linkTo (\""+ 
   874 					preDstParStr + "\"," + postNum + "," +
   875 					QString ("%1,%2").arg(savePos.x()).arg(savePos.y())+ ")";
   876 
   877 				model->saveState (
   878 					postSelStr,undoCom,
   879 					preSelStr, redoCom,
   880 					QString("Relink %1 to %2").arg(model->getObjectName(bsel)).arg(model->getObjectName(dst)) );
   881 
   882 				model->reposition();	// not necessary if we undo temporary move  below
   883 			} else
   884 			{
   885 				// No destination, undo  temporary move
   886 
   887 				if (lmosel->getDepth()==1)
   888 				{
   889 					// The select string might be different _after_ moving around.
   890 					// Therefor reposition and then use string of old selection, too
   891 					model->reposition();
   892 
   893                     QPointF rp(lmosel->getRelPos());
   894                     if (rp != movingObj_orgRelPos)
   895                     {
   896                         QString ps=qpointfToString(rp);
   897                         model->saveState(
   898                             model->getSelectString(lmosel), "moveRel "+qpointfToString(movingObj_orgRelPos), 
   899                             preSelStr, "moveRel "+ps, 
   900                             QString("Move %1 to relative position %2").arg(model->getObjectName(lmosel)).arg(ps));
   901                     }
   902 				}
   903 
   904 				// Draw the original link, before selection was moved around
   905 				if (settings.value("/animation/use",false).toBool() && lmosel->getDepth()>1) 
   906 				{
   907 					lmosel->setRelPos();	// calc relPos first for starting point
   908 					QPointF dst=bo->getParObj()->getChildPos();
   909 			//		if (lmosel->getOrientation()==LinkableMapObj::LeftOfCenter) dst.setX (dst.x()+lmosel->width() );
   910 					
   911 					model->startAnimation(
   912 						(BranchObj*)lmosel,
   913 						lmosel->getRelPos(),
   914 						movingObj_orgRelPos
   915 //						QPointF (movingObj_orgPos.x() - dst.x(), movingObj_orgPos.y() - dst.y() )
   916 					);	
   917 				} else	
   918 					model->reposition();
   919 			}
   920 		}
   921 		 model->updateSelection();
   922 		// Finally resize scene, if needed
   923 		scene()->update();
   924 		movingObj=NULL;		
   925 
   926 		// Just make sure, that actions are still ok,e.g. the move branch up/down buttons...
   927 		model->updateActions();
   928 	} else 
   929 		// maybe we moved View: set old cursor
   930 		setCursor (Qt::ArrowCursor);
   931     
   932 }
   933 
   934 void MapEditor::mouseDoubleClickEvent(QMouseEvent* e)
   935 {
   936 	if (model->isSelectionBlocked() ) 
   937 	{
   938 		e->ignore();
   939 		return;
   940 	}
   941 
   942 	if (e->button() == Qt::LeftButton )
   943 	{
   944 		QPointF p = mapToScene(e->pos());
   945 		LinkableMapObj *lmo=model->findMapObj(p, NULL);
   946 		if (lmo) {	// MapObj was found
   947 			// First select the MapObj than edit heading
   948 			model->select (lmo);
   949 			editHeading();
   950 		}
   951 	}
   952 }
   953 
   954 void MapEditor::resizeEvent (QResizeEvent* e)
   955 {
   956 	QGraphicsView::resizeEvent( e );
   957 }
   958 
   959 void MapEditor::dragEnterEvent(QDragEnterEvent *event)
   960 {
   961 	//for (unsigned int i=0;event->format(i);i++) // Debug mime type
   962 	//	cerr << event->format(i) << endl;
   963 
   964 	if (event->mimeData()->hasImage())
   965 		event->acceptProposedAction();
   966 	else	
   967 		if (event->mimeData()->hasUrls())
   968 			event->acceptProposedAction();
   969 }
   970 
   971 void MapEditor::dragMoveEvent(QDragMoveEvent *)
   972 {
   973 }
   974 
   975 void MapEditor::dragLeaveEvent(QDragLeaveEvent *event)
   976 {
   977 	event->accept();
   978 }
   979 
   980 void MapEditor::dropEvent(QDropEvent *event)
   981 {
   982 	BranchObj *sel=model->getSelectedBranch();
   983 	if (sel)
   984 	{
   985 		if (debug)
   986 			foreach (QString format,event->mimeData()->formats()) 
   987 				cout << "MapEditor: Dropped format: "<<qPrintable (format)<<endl;
   988 
   989 
   990 		QList <QUrl> uris;
   991 		if (event->mimeData()->hasImage()) 
   992 		{
   993 			 QVariant imageData = event->mimeData()->imageData();
   994 			 model->addFloatImage (qvariant_cast<QPixmap>(imageData));
   995 		} else
   996 		if (event->mimeData()->hasUrls())
   997 			uris=event->mimeData()->urls();
   998 
   999 		if (uris.count()>0)
  1000 		{
  1001 			QStringList files;
  1002 			QString s;
  1003 			QString heading;
  1004 			BranchObj *bo;
  1005 			for (int i=0; i<uris.count();i++)
  1006 			{
  1007 				// Workaround to avoid adding empty branches
  1008 				if (!uris.at(i).toString().isEmpty())
  1009 				{
  1010 					bo=sel->addBranch();
  1011 					if (bo)
  1012 					{
  1013 						s=uris.at(i).toLocalFile();
  1014 						if (!s.isEmpty()) 
  1015 						{
  1016 						   QString file = QDir::fromNativeSeparators(s);
  1017 						   heading = QFileInfo(file).baseName();
  1018 						   files.append(file);
  1019 						   if (file.endsWith(".vym", false))
  1020 							   bo->setVymLink(file);
  1021 						   else
  1022 							   bo->setURL(uris.at(i).toString());
  1023 					   } else 
  1024 					   {
  1025 						   bo->setURL(uris.at(i).toString());
  1026 					   }
  1027 
  1028 					   if (!heading.isEmpty())
  1029 						   bo->setHeading(heading);
  1030 					   else
  1031 						   bo->setHeading(uris.at(i).toString());
  1032 					}
  1033 				}
  1034 			}
  1035 			model->reposition();
  1036 		}
  1037 	}	
  1038 	event->acceptProposedAction();
  1039 }
  1040 
  1041 void MapEditor::updateSelection(const QItemSelection &newsel,const QItemSelection &)
  1042 {
  1043 	cout << "ME::updateSelection\n";
  1044 
  1045 	QGraphicsRectItem *sb;
  1046 
  1047 	// Check if selection is empty
  1048 	if (newsel.indexes().isEmpty() )
  1049 	{
  1050 		if (!selboxList.isEmpty())
  1051 			while (!selboxList.isEmpty() )
  1052 				delete selboxList.takeFirst();
  1053 		return;		
  1054 	}
  1055 
  1056 	if (selboxList.isEmpty())
  1057 	{
  1058 		sb = mapScene->addRect(
  1059 			QRectF(0,0,0,0), 
  1060 			QPen(selectionColor),
  1061 			selectionColor);
  1062 		sb->setZValue(Z_SELBOX);
  1063 		sb->show();
  1064 		selboxList.append (sb);
  1065 	} else
  1066 		sb=selboxList.first();
  1067 		
  1068 	QModelIndex index=newsel.indexes().first();	// TODO no multiselections yet
  1069 
  1070 	TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
  1071 	LinkableMapObj *lmo=item->getLMO();
  1072 
  1073 	int w=0;	// FIXME what for?
  1074 	if (lmo)
  1075 	{
  1076 		QRectF bbox=lmo->getBBox();
  1077 		selboxList.first()->setRect (
  1078 			bbox.x()-w,bbox.y()-w, 
  1079 			bbox.width()+2*w, bbox.height()+2*w);
  1080 		selboxList.first()->setPen (selectionColor);	
  1081 		selboxList.first()->setBrush (selectionColor);	
  1082 	}
  1083 	
  1084 
  1085 
  1086 }
  1087 
  1088 void MapEditor::updateSelectionGeometry()	// FIXME VM only called from VM::animate...
  1089 {
  1090 	cout << "ME::updateSelectionGeometry\n";
  1091 
  1092 	// Tell selection to update geometries
  1093 	model->updateSelection();
  1094 }
  1095 
  1096 
  1097 void MapEditor::setSelectionColor (QColor col)
  1098 {
  1099 	selectionColor=col;
  1100 	updateSelectionGeometry();
  1101 }
  1102 
  1103 QColor MapEditor::getSelectionColor ()
  1104 {
  1105 	return selectionColor;
  1106 }
  1107 
  1108