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