mapeditor.cpp
author insilmaril
Wed, 10 Dec 2008 13:10:35 +0000
changeset 731 c8b1a3564c74
parent 730 0594d7b7099d
child 732 b77b56f707f1
permissions -rw-r--r--
selection is now visible again and even animated
     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 		cout << "ME::mouse pressed\n";
   542 		cout << "  lmo="<<lmo<<endl;
   543 		cout << "  h="<<((BranchObj*)lmo)->getHeading().toStdString()<<endl;
   544 		// Select the clicked object
   545 
   546 		// FIXME VM better let "find" return an index instead of lmo...
   547 		// Get index of clicked LMO
   548 		TreeItem *ti=lmo->getTreeItem();
   549 		cout << "  lmo="<<lmo<<"    lmo(ti)="<<ti->getLMO()<<endl;
   550 		cout << "  ti ("<<ti->row()<<","<<ti->column()<<") = "<<ti<<endl;
   551 		//QModelIndex ix=model->index( ti->row(), ti->column(), model->index (0,0,QModelIndex()) );
   552 		QModelIndex ix=model->index(ti);
   553 		selModel->select (ix,QItemSelectionModel::ClearAndSelect  );
   554 		/*
   555 		if (selModel->hasSelection() ) 
   556 		{
   557 			QModelIndex ixsel=selModel->selectedIndexes().first();
   558 			TreeItem *tisel= static_cast<TreeItem*>(ixsel.internalPointer());
   559 	;
   560 			cout << "  ti="<<ti<<"  tisel="<<tisel<<endl;
   561 			if (ti!=tisel) int i=1/0;
   562 		}
   563 		else
   564 			cout <<"MapEditor::mousePressed  No selection!!!\n";
   565 		*/
   566 
   567 
   568 		// Left Button	    Move Branches
   569 		if (e->button() == Qt::LeftButton )
   570 		{
   571 			//movingObj_start.setX( p.x() - selection->x() );// TODO replaced selection->lmo here	
   572 			//movingObj_start.setY( p.y() - selection->y() );	
   573 			movingObj_start.setX( p.x() - lmo->x() );	
   574 			movingObj_start.setY( p.y() - lmo->y() );	
   575 			movingObj_orgPos.setX (lmo->x() );
   576 			movingObj_orgPos.setY (lmo->y() );
   577 			lmo->setRelPos();
   578 			movingObj_orgRelPos=lmo->getRelPos();
   579 
   580 			// If modMode==copy, then we want to "move" the _new_ object around
   581 			// then we need the offset from p to the _old_ selection, because of tmp
   582 			if (mainWindow->getModMode()==Main::ModModeCopy &&
   583 				e->state() & Qt::ControlModifier)
   584 			{
   585 				BranchObj *bo=model->getSelectedBranch();
   586 				if (bo)
   587 				{
   588 					copyingObj=true;
   589 					bo->addBranch ((BranchObj*)model->getSelection());
   590 					model->unselect();
   591 					model->select(bo->getLastBranch());
   592 					model->reposition();
   593 				}
   594 			} 
   595 
   596 			movingObj=model->getSelection();	
   597 		} else
   598 			// Middle Button    Toggle Scroll
   599 			// (On Mac OS X this won't work, but we still have 
   600 			// a button in the toolbar)
   601 			if (e->button() == Qt::MidButton )
   602 				model->toggleScroll();
   603 		model->updateActions();
   604 		// FIXME needed? xelection.update();
   605 	} else 
   606 	{ // No MapObj found, we are on the scene itself
   607 		// Left Button	    move Pos of sceneView
   608 		if (e->button() == Qt::LeftButton )
   609 		{
   610 			movingObj=NULL;	// move Content not Obj
   611 			movingObj_start=e->globalPos();
   612 			movingCont_start=QPointF (
   613 				horizontalScrollBar()->value(),
   614 				verticalScrollBar()->value());
   615 			movingVec=QPointF(0,0);
   616 			setCursor(HandOpenCursor);
   617 		} 
   618     } 
   619 }
   620 
   621 void MapEditor::mouseMoveEvent(QMouseEvent* e)
   622 {
   623     QPointF p = mapToScene(e->pos());
   624 	LinkableMapObj *lmosel=model->getSelection();
   625 
   626     // Move the selected MapObj
   627     if ( lmosel && movingObj) 
   628     {	
   629 		// reset cursor if we are moving and don't copy
   630 		if (mainWindow->getModMode()!=Main::ModModeCopy)
   631 			setCursor (Qt::ArrowCursor);
   632 
   633 		// To avoid jumping of the sceneView, only 
   634 		// ensureSelectionVisible, if not tmp linked
   635 		if (!lmosel->hasParObjTmp())
   636 			model->ensureSelectionVisible ();
   637 		
   638 		// Now move the selection, but add relative position 
   639 		// (movingObj_start) where selection was chosen with 
   640 		// mousepointer. (This avoids flickering resp. jumping 
   641 		// of selection back to absPos)
   642 		
   643 		// Check if we could link 
   644 		LinkableMapObj* lmo=model->findMapObj(p, lmosel);
   645 		
   646 
   647 		FloatObj *fio=model->getSelectedFloatImage();
   648 		if (fio)
   649 		{
   650 			fio->move   (p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );		
   651 			fio->setRelPos();
   652 			fio->updateLink(); //no need for reposition, if we update link here
   653 			model->updateSelection();
   654 
   655 			// Relink float to new mapcenter or branch, if shift is pressed	
   656 			// Only relink, if selection really has a new parent
   657 			if ( (e->modifiers()==Qt::ShiftModifier) && lmo &&
   658 				( (typeid(*lmo)==typeid(BranchObj)) ||
   659 				  (typeid(*lmo)==typeid(MapCenterObj)) ) &&
   660 				( lmo != fio->getParObj())  
   661 				)
   662 			{
   663 				if (typeid(*fio) == typeid(FloatImageObj) && 
   664 				( (typeid(*lmo)==typeid(BranchObj) ||
   665 				  typeid(*lmo)==typeid(MapCenterObj)) ))  
   666 				{
   667 
   668 					// Also save the move which was done so far
   669 					QString pold=qpointfToString(movingObj_orgRelPos);
   670 					QString pnow=qpointfToString(fio->getRelPos());
   671 					model->saveState(
   672 						fio,
   673 						"moveRel "+pold,
   674 						fio,
   675 						"moveRel "+pnow,
   676 						QString("Move %1 to relative position %2").arg(model->getObjectName(fio)).arg(pnow));
   677 					fio->getParObj()->requestReposition();
   678 					model->reposition();
   679 
   680 					model->linkFloatImageTo (model->getSelectString(lmo));
   681 					//movingObj=lmosel;
   682 					//movingObj_orgRelPos=lmosel->getRelPos();	
   683 
   684 					model->reposition();
   685 				}	
   686 			}
   687 		} else	
   688 		{	// selection != a FloatObj
   689 			if (lmosel->getDepth()==0)
   690 			{
   691 				// Move MapCenter
   692 				if (e->buttons()== Qt::LeftButton && e->modifiers()==Qt::ShiftModifier) 
   693 					((MapCenterObj*)lmosel)->moveAll(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );		
   694 				else	
   695 					lmosel->move   (p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );		
   696 				model->updateRelPositions();
   697 			} else
   698 			{	
   699 				if (lmosel->getDepth()==1)
   700 				{
   701 					// Move mainbranch
   702 					lmosel->move(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );		
   703 					lmosel->setRelPos();
   704 				} else
   705 				{
   706 					// Move ordinary branch
   707 					if (lmosel->getOrientation() == LinkableMapObj::LeftOfCenter)
   708 						// Add width of bbox here, otherwise alignRelTo will cause jumping around
   709 						lmosel->move(p.x() -movingObj_start.x() , //lmosel->getBBox().width(), 
   710 							p.y()-movingObj_start.y() +lmosel->getTopPad() );		
   711 					else	
   712 						lmosel->move(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() -lmosel->getTopPad());
   713 					lmosel->setRelPos();	
   714 				} 
   715 
   716 				// Maybe we can relink temporary?
   717 				if (lmo && (lmo!=lmosel) && model->getSelectedBranch() && 
   718 					 (typeid(*lmo)==typeid(BranchObj) ||
   719 					  typeid(*lmo)==typeid(MapCenterObj)) ) 
   720 
   721 				{
   722 					if (e->modifiers()==Qt::ControlModifier)
   723 					{
   724 						// Special case: CTRL to link below lmo
   725 						lmosel->setParObjTmp (lmo,p,+1);
   726 					}
   727 					else if (e->modifiers()==Qt::ShiftModifier)
   728 						lmosel->setParObjTmp (lmo,p,-1);
   729 					else
   730 						lmosel->setParObjTmp (lmo,p,0);
   731 				} else	
   732 				{
   733 					lmosel->unsetParObjTmp();
   734 				}		
   735 				// reposition subbranch
   736 				lmosel->reposition();	
   737 			} // depth>0
   738 
   739 			//model->updateSelection();// FIXME needed? 
   740 			//updateSelectionGeometry();
   741 			updateSelection(selModel->selection(), selModel->selection() );
   742 		} // no FloatImageObj
   743 
   744 		scene()->update();
   745 		return;
   746 	} // selection && moving_obj
   747 		
   748 	// Draw a link from one branch to another
   749 	if (drawingLink)
   750 	{
   751 		 tmpXLink->setEnd (p);
   752 		 tmpXLink->updateXLink();
   753 	}	 
   754 	
   755     // Move sceneView 
   756     if (!movingObj && !pickingColor &&!drawingLink && e->buttons() == Qt::LeftButton ) 
   757 	{
   758 		QPointF p=e->globalPos();
   759 		movingVec.setX(-p.x() + movingObj_start.x() );
   760 		movingVec.setY(-p.y() + movingObj_start.y() );
   761 		horizontalScrollBar()->setSliderPosition((int)( movingCont_start.x()+movingVec.x() ));
   762 		verticalScrollBar()->setSliderPosition((int)( movingCont_start.y()+movingVec.y() ) );
   763     }
   764 }
   765 
   766 
   767 void MapEditor::mouseReleaseEvent(QMouseEvent* e)
   768 {
   769     QPointF p = mapToScene(e->pos());
   770 	LinkableMapObj *dst;
   771 	LinkableMapObj *lmosel=model->getSelection();
   772 	// Have we been picking color?
   773 	if (pickingColor)
   774 	{
   775 		pickingColor=false;
   776 		setCursor (Qt::ArrowCursor);
   777 		// Check if we are over another branch
   778 		dst=model->findMapObj(p, NULL);
   779 		if (dst && lmosel) 
   780 		{	
   781 			if (e->state() & Qt::ShiftModifier)
   782 				model->colorBranch (((BranchObj*)dst)->getColor());
   783 			else	
   784 				model->colorSubtree (((BranchObj*)dst)->getColor());
   785 		} 
   786 		return;
   787 	}
   788 
   789 	// Have we been drawing a link?
   790 	if (drawingLink)	
   791 	{
   792 		drawingLink=false;
   793 		// Check if we are over another branch
   794 		dst=model->findMapObj(p, NULL);
   795 		if (dst && lmosel) 
   796 		{	
   797 			tmpXLink->setEnd ( ((BranchObj*)(dst)) );
   798 			tmpXLink->updateXLink();
   799 			tmpXLink->activate(); //FIXME savestate missing
   800 			//model->saveStateComplete(QString("Activate xLink from %1 to %2").arg(model->getObjectName(tmpXLink->getBegin())).arg(model->getObjectName(tmpXLink->getEnd())) );	
   801 		} else
   802 		{
   803 			delete(tmpXLink);
   804 			tmpXLink=NULL;
   805 		}
   806 		return;
   807 	}
   808 	
   809     // Have we been moving something?
   810     if ( lmosel && movingObj ) 
   811     {	
   812 		FloatImageObj *fo=model->getSelectedFloatImage();
   813 		if(fo)
   814 		{
   815 			// Moved FloatObj. Maybe we need to reposition
   816 		    QString pold=qpointfToString(movingObj_orgRelPos);
   817 		    QString pnow=qpointfToString(fo->getRelPos());
   818 			model->saveState(
   819 				fo,
   820 				"moveRel "+pold,
   821 				fo,
   822 				"moveRel "+pnow,
   823 				QString("Move %1 to relative position %2").arg(model->getObjectName(fo)).arg(pnow));
   824 
   825 			fo->getParObj()->requestReposition();
   826 			model->reposition();
   827 		}	
   828 
   829 		// Check if we are over another branch, but ignore 
   830 		// any found LMOs, which are FloatObjs
   831 		dst=model->findMapObj(mapToScene(e->pos() ), lmosel);
   832 
   833 		if (dst && (typeid(*dst)!=typeid(BranchObj) && typeid(*dst)!=typeid(MapCenterObj))) 
   834 			dst=NULL;
   835 		
   836 		BranchObj *bo=model->getSelectedBranch();
   837 		if (bo && bo->getDepth()==0)
   838 		{	
   839             if (movingObj_orgPos != bo->getAbsPos())
   840             {
   841                 QString pold=qpointfToString(movingObj_orgPos);
   842                 QString pnow=qpointfToString(bo->getAbsPos());
   843                 model->saveState(
   844                     bo,
   845                     "move "+pold,
   846                     bo,
   847                     "move "+pnow,
   848                     QString("Move mapcenter %1 to position %2").arg(model->getObjectName(bo)).arg(pnow));
   849             }
   850 		}
   851 	
   852 		if (model->selectionType() == Selection::Branch )
   853 		{	// A branch was moved
   854 			
   855 			// save the position in case we link to mapcenter
   856 			QPointF savePos=QPointF (lmosel->getAbsPos()  );
   857 
   858 			// Reset the temporary drawn link to the original one
   859 			lmosel->unsetParObjTmp();
   860 
   861 			// For Redo we may need to save original selection
   862 			QString preSelStr=model->getSelectString(lmosel);
   863 
   864 			copyingObj=false;	
   865 			if (dst ) 
   866 			{
   867 				// We have a destination, relink to that
   868 
   869 				BranchObj* bsel=model->getSelectedBranch();
   870 				BranchObj* bdst=(BranchObj*)dst;
   871 
   872 				QString preParStr=model->getSelectString (bsel->getParObj());
   873 				QString preNum=QString::number (bsel->getNum(),10);
   874 				QString preDstParStr;
   875 
   876 				if (e->state() & Qt::ShiftModifier && dst->getParObj())
   877 				{	// Link above dst
   878 					preDstParStr=model->getSelectString (dst->getParObj());
   879 					bsel->linkTo ( (BranchObj*)(bdst->getParObj()), bdst->getNum());
   880 				} else 
   881 				if (e->state() & Qt::ControlModifier && dst->getParObj())
   882 				{
   883 					// Link below dst
   884 					preDstParStr=model->getSelectString (dst->getParObj());
   885 					bsel->linkTo ( (BranchObj*)(bdst->getParObj()), bdst->getNum()+1);
   886 				} else	
   887 				{	// Append to dst
   888 					preDstParStr=model->getSelectString(dst);
   889 					bsel->linkTo (bdst,-1);
   890 					if (dst->getDepth()==0) bsel->move (savePos);
   891 				} 
   892 				QString postSelStr=model->getSelectString(lmosel);
   893 				QString postNum=QString::number (bsel->getNum(),10);
   894 
   895 				QString undoCom="linkTo (\""+ 
   896 					preParStr+ "\"," + preNum  +"," + 
   897 					QString ("%1,%2").arg(movingObj_orgPos.x()).arg(movingObj_orgPos.y())+ ")";
   898 
   899 				QString redoCom="linkTo (\""+ 
   900 					preDstParStr + "\"," + postNum + "," +
   901 					QString ("%1,%2").arg(savePos.x()).arg(savePos.y())+ ")";
   902 
   903 				model->saveState (
   904 					postSelStr,undoCom,
   905 					preSelStr, redoCom,
   906 					QString("Relink %1 to %2").arg(model->getObjectName(bsel)).arg(model->getObjectName(dst)) );
   907 
   908 				model->reposition();	// not necessary if we undo temporary move  below
   909 			} else
   910 			{
   911 				// No destination, undo  temporary move
   912 
   913 				if (lmosel->getDepth()==1)
   914 				{
   915 					// The select string might be different _after_ moving around.
   916 					// Therefor reposition and then use string of old selection, too
   917 					model->reposition();
   918 
   919                     QPointF rp(lmosel->getRelPos());
   920                     if (rp != movingObj_orgRelPos)
   921                     {
   922                         QString ps=qpointfToString(rp);
   923                         model->saveState(
   924                             model->getSelectString(lmosel), "moveRel "+qpointfToString(movingObj_orgRelPos), 
   925                             preSelStr, "moveRel "+ps, 
   926                             QString("Move %1 to relative position %2").arg(model->getObjectName(lmosel)).arg(ps));
   927                     }
   928 				}
   929 
   930 				// Draw the original link, before selection was moved around
   931 				if (settings.value("/animation/use",false).toBool() && lmosel->getDepth()>1) 
   932 				{
   933 					lmosel->setRelPos();	// calc relPos first for starting point
   934 					QPointF dst=bo->getParObj()->getChildPos();
   935 			//		if (lmosel->getOrientation()==LinkableMapObj::LeftOfCenter) dst.setX (dst.x()+lmosel->width() );
   936 					
   937 					model->startAnimation(
   938 						(BranchObj*)lmosel,
   939 						lmosel->getRelPos(),
   940 						movingObj_orgRelPos
   941 //						QPointF (movingObj_orgPos.x() - dst.x(), movingObj_orgPos.y() - dst.y() )
   942 					);	
   943 				} else	
   944 					model->reposition();
   945 			}
   946 		}
   947 		 model->updateSelection();
   948 		// Finally resize scene, if needed
   949 		scene()->update();
   950 		movingObj=NULL;		
   951 
   952 		// Just make sure, that actions are still ok,e.g. the move branch up/down buttons...
   953 		model->updateActions();
   954 	} else 
   955 		// maybe we moved View: set old cursor
   956 		setCursor (Qt::ArrowCursor);
   957     
   958 }
   959 
   960 void MapEditor::mouseDoubleClickEvent(QMouseEvent* e)
   961 {
   962 	if (model->isSelectionBlocked() ) 
   963 	{
   964 		e->ignore();
   965 		return;
   966 	}
   967 
   968 	if (e->button() == Qt::LeftButton )
   969 	{
   970 		QPointF p = mapToScene(e->pos());
   971 		LinkableMapObj *lmo=model->findMapObj(p, NULL);
   972 		if (lmo) {	// MapObj was found
   973 			// First select the MapObj than edit heading
   974 			model->select (lmo);
   975 			editHeading();
   976 		}
   977 	}
   978 }
   979 
   980 void MapEditor::resizeEvent (QResizeEvent* e)
   981 {
   982 	QGraphicsView::resizeEvent( e );
   983 }
   984 
   985 void MapEditor::dragEnterEvent(QDragEnterEvent *event)
   986 {
   987 	//for (unsigned int i=0;event->format(i);i++) // Debug mime type
   988 	//	cerr << event->format(i) << endl;
   989 
   990 	if (event->mimeData()->hasImage())
   991 		event->acceptProposedAction();
   992 	else	
   993 		if (event->mimeData()->hasUrls())
   994 			event->acceptProposedAction();
   995 }
   996 
   997 void MapEditor::dragMoveEvent(QDragMoveEvent *)
   998 {
   999 }
  1000 
  1001 void MapEditor::dragLeaveEvent(QDragLeaveEvent *event)
  1002 {
  1003 	event->accept();
  1004 }
  1005 
  1006 void MapEditor::dropEvent(QDropEvent *event)
  1007 {
  1008 	BranchObj *sel=model->getSelectedBranch();
  1009 	if (sel)
  1010 	{
  1011 		if (debug)
  1012 			foreach (QString format,event->mimeData()->formats()) 
  1013 				cout << "MapEditor: Dropped format: "<<qPrintable (format)<<endl;
  1014 
  1015 
  1016 		QList <QUrl> uris;
  1017 		if (event->mimeData()->hasImage()) 
  1018 		{
  1019 			 QVariant imageData = event->mimeData()->imageData();
  1020 			 model->addFloatImage (qvariant_cast<QPixmap>(imageData));
  1021 		} else
  1022 		if (event->mimeData()->hasUrls())
  1023 			uris=event->mimeData()->urls();
  1024 
  1025 		if (uris.count()>0)
  1026 		{
  1027 			QStringList files;
  1028 			QString s;
  1029 			QString heading;
  1030 			BranchObj *bo;
  1031 			for (int i=0; i<uris.count();i++)
  1032 			{
  1033 				// Workaround to avoid adding empty branches
  1034 				if (!uris.at(i).toString().isEmpty())
  1035 				{
  1036 					bo=sel->addBranch();
  1037 					if (bo)
  1038 					{
  1039 						s=uris.at(i).toLocalFile();
  1040 						if (!s.isEmpty()) 
  1041 						{
  1042 						   QString file = QDir::fromNativeSeparators(s);
  1043 						   heading = QFileInfo(file).baseName();
  1044 						   files.append(file);
  1045 						   if (file.endsWith(".vym", false))
  1046 							   bo->setVymLink(file);
  1047 						   else
  1048 							   bo->setURL(uris.at(i).toString());
  1049 					   } else 
  1050 					   {
  1051 						   bo->setURL(uris.at(i).toString());
  1052 					   }
  1053 
  1054 					   if (!heading.isEmpty())
  1055 						   bo->setHeading(heading);
  1056 					   else
  1057 						   bo->setHeading(uris.at(i).toString());
  1058 					}
  1059 				}
  1060 			}
  1061 			model->reposition();
  1062 		}
  1063 	}	
  1064 	event->acceptProposedAction();
  1065 }
  1066 
  1067 void MapEditor::updateSelection(const QItemSelection &newsel,const QItemSelection &)
  1068 {
  1069 	cout << "ME::updateSelection\n";
  1070 
  1071 
  1072 	if (newsel.isEmpty() )
  1073 	{
  1074 		cout << "   newsel=empty!\n";
  1075 		return;
  1076 	}	
  1077 	QModelIndex index=newsel.indexes().first();	// TODO no multiselections yet
  1078 
  1079 	TreeItem *ti= static_cast<TreeItem*>(index.internalPointer());
  1080 	LinkableMapObj *lmo=ti->getLMO();
  1081 	cout << "   newsel.count="<<newsel.indexes().count()<<"  lmo="<<lmo<<endl;
  1082 	cout << "   h="<<((BranchObj*)lmo)->getHeading().toStdString()<<endl;
  1083 	cout << "   ti ("<<ti->row()<<","<<ti->column()<<") = "<<ti<<endl;
  1084 
  1085 
  1086 	// Reduce rectangles
  1087 	while (newsel.indexes().count() < selboxList.count() )
  1088 		delete selboxList.takeFirst();
  1089 
  1090 	// Add additonal rectangles
  1091 	QGraphicsRectItem *sb;
  1092 	while (newsel.indexes().count() > selboxList.count() )
  1093 	{
  1094 		sb = mapScene->addRect(
  1095 			QRectF(0,0,0,0), 
  1096 			QPen(selectionColor),
  1097 			selectionColor);
  1098 		sb->setZValue(Z_SELBOX);
  1099 		sb->show();
  1100 		selboxList.append (sb);
  1101 	}
  1102 
  1103 	// Reposition rectangles
  1104 	int i=0;
  1105 	QRectF bbox;
  1106 	foreach (sb,selboxList)
  1107 	{
  1108 		index=newsel.indexes().at(i);
  1109 		ti= static_cast<TreeItem*>(index.internalPointer());
  1110 		lmo=ti->getLMO();
  1111 		bbox=lmo->getBBox();
  1112 		sb->setRect (
  1113 			bbox.x(),bbox.y(), 
  1114 			bbox.width(), bbox.height());
  1115 		sb->setPen (selectionColor);	
  1116 		sb->setBrush (selectionColor);	
  1117 		i++;
  1118 	}
  1119 }
  1120 
  1121 void MapEditor::updateCurrent (const QModelIndex &newsel,const QModelIndex &)
  1122 {
  1123 	cout << "ME::updateCurrent\n";
  1124 
  1125 /*
  1126 
  1127 	TreeItem *item = static_cast<TreeItem*>(newsel.internalPointer());
  1128 	LinkableMapObj *lmo=item->getLMO();
  1129 	cout << "  lmo="<<lmo<<endl;
  1130 	cout << "  h="<<((BranchObj*)lmo)->getHeading().toStdString()<<endl;
  1131 	*/
  1132 
  1133 }
  1134 
  1135 void MapEditor::updateSelectionGeometry()	// FIXME VM only called from VM::animate...
  1136 {
  1137 	cout << "ME::updateSelectionGeometry\n";
  1138 
  1139 	// Tell selection to update geometries
  1140 	updateSelection(selModel->selection(), selModel->selection() );
  1141 	//model->updateSelection();
  1142 }
  1143 
  1144 
  1145 void MapEditor::setSelectionColor (QColor col)
  1146 {
  1147 	selectionColor=col;
  1148 	updateSelectionGeometry();
  1149 }
  1150 
  1151 QColor MapEditor::getSelectionColor ()
  1152 {
  1153 	return selectionColor;
  1154 }
  1155 
  1156