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