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