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