mapeditor.cpp
author insilmaril
Fri, 06 Mar 2009 15:02:58 +0000
changeset 741 1b4d1ea6ea8c
parent 740 6dc0a20031f7
child 742 54d44ecd6097
permissions -rw-r--r--
Iteration over map works now (again)
     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 	TreeItem *cur=NULL;
   286 	TreeItem *prev=NULL;
   287 	int d;
   288 	cout << "ME::testFunction1  starting to walk the map...\n";
   289 	while (model->next (cur,prev,d) )
   290 		cout << "*** " <<cur->getHeading().toStdString()<<endl;
   291 
   292 
   293 	//BranchObj *bo=model->getSelectedBranch();
   294 	//if (bo) model->moveAway (bo);
   295 	//if (bo) bo->setLinkStyle (LinkableMapObj::Line);
   296 	
   297 
   298 /*
   299 	// Displacement and animation of all non-mainbranches
   300 	QPointF p;
   301 	QPointF q;
   302 	BranchObj *bo;
   303 	TreeItem *cur=NULL;
   304 	TreeItem *prev=NULL;
   305 	int d;
   306 	while (cur) 
   307 	{
   308 		bo=(BranchObj*)(cur->getLMO());
   309 
   310 		if (cur->depth() >0 && !bo->hasScrolledParent(bo) )
   311 		{
   312 			p=QPointF (qrand() %600-300, qrand () %600-300);
   313 			bo->setRelPos();
   314 			q=bo->getRelPos();
   315 			model->startAnimation (bo,p, q);
   316 		}
   317 		model->next(cur,prev,d);
   318 	}
   319 */
   320 
   321 
   322 /* TODO Hide hidden stuff temporary, maybe add this as regular function somewhere
   323 	if (hidemode==HideNone)
   324 	{
   325 		setHideTmpMode (HideExport);
   326 		mapCenter->calcBBoxSizeWithChilds();
   327 		QRectF totalBBox=mapCenter->getTotalBBox();
   328 		QRectF mapRect=totalBBox;
   329 		QCanvasRectangle *frame=NULL;
   330 
   331 		cout << "  map has =("<<totalBBox.x()<<","<<totalBBox.y()<<","<<totalBBox.width()<<","<<totalBBox.height()<<")\n";
   332 	
   333 		mapRect.setRect (totalBBox.x(), totalBBox.y(), 
   334 			totalBBox.width(), totalBBox.height());
   335 		frame=new QCanvasRectangle (mapRect,mapScene);
   336 		frame->setBrush (QColor(white));
   337 		frame->setPen (QColor(black));
   338 		frame->setZValue(0);
   339 		frame->show();    
   340 	}	
   341 	else	
   342 	{
   343 		setHideTmpMode (HideNone);
   344 	}	
   345 	cout <<"  hidemode="<<hidemode<<endl;
   346 	*/
   347 }	
   348 	
   349 void MapEditor::testFunction2()
   350 {
   351 
   352 /*
   353 	// Toggle hidemode
   354 	if (hidemode==HideExport)
   355 		setHideTmpMode (HideNone);
   356 	else	
   357 		setHideTmpMode (HideExport);
   358 */		
   359 }
   360 
   361 void MapEditor::editHeading()
   362 {
   363 	if (editingHeading)
   364 	{
   365 		editHeadingFinished();
   366 		return;
   367 	}
   368 	BranchObj *bo=model->getSelectedBranch();
   369 	if (bo)
   370 	{
   371 		model->setSelectionBlocked(true);
   372 
   373 		lineEdit->setText (bo->getHeading());
   374 		QPoint p = mapTo (this,bo->getAbsPos().toPoint() );
   375 		lineEdit->setGeometry(p.x(),p.y(),230,25);
   376 		lineEdit->selectAll();
   377 		lineEdit->show();
   378 		lineEdit->setFocus();
   379 		lineEdit->grabKeyboard();
   380 		editingHeading=true;
   381 	}
   382 
   383 }
   384 void MapEditor::editHeadingFinished()
   385 {
   386 	editingHeading=false;
   387 	lineEdit->releaseKeyboard();
   388 	model->setHeading (lineEdit->text() );
   389 	model->setSelectionBlocked(false);
   390 	lineEdit->hide();
   391 
   392 	// Maybe reselect previous branch 
   393 	mainWindow->editHeadingFinished (model);
   394 }
   395 
   396 
   397 void MapEditor::contextMenuEvent ( QContextMenuEvent * e )
   398 {
   399 	// Lineedits are already closed by preceding
   400 	// mouseEvent, we don't need to close here.
   401 
   402     QPointF p = mapToScene(e->pos());
   403     LinkableMapObj* lmo=model->findMapObj(p, NULL);
   404 	
   405     if (lmo) 
   406 	{	// MapObj was found
   407 		if (model->getSelectedLMO() != lmo)
   408 		{
   409 			// select the MapObj
   410 			model->select(lmo);
   411 		}
   412 		// Context Menu 
   413 		if (model->getSelectedBranch() ) 
   414 		{
   415 			// Context Menu on branch or mapcenter
   416 			model->updateActions();
   417 			branchContextMenu->popup(e->globalPos() );
   418 		} else
   419 		{
   420 			if (model->getSelectedFloatImage() )
   421 			{
   422 				// Context Menu on floatimage
   423 				model->updateActions();
   424 				floatimageContextMenu->popup(e->globalPos() );
   425 			}	
   426 		}	
   427 	} else 
   428 	{ // No MapObj found, we are on the Canvas itself
   429 		// Context Menu on scene
   430 		model->updateActions();
   431 		
   432 		// Open context menu synchronously to position new mapcenter
   433 		model->setContextPos (p);
   434 		canvasContextMenu->exec(e->globalPos() );
   435 		model->unsetContextPos ();
   436     } 
   437 	e->accept();
   438 }
   439 
   440 void MapEditor::keyPressEvent(QKeyEvent* e)
   441 {
   442 	if (e->modifiers() & Qt::ControlModifier)
   443 	{
   444 		switch (mainWindow->getModMode())
   445 		{
   446 			case Main::ModModeColor: 
   447 				setCursor (PickColorCursor);
   448 				break;
   449 			case Main::ModModeCopy: 
   450 				setCursor (CopyCursor);
   451 				break;
   452 			case Main::ModModeXLink: 
   453 				setCursor (XLinkCursor);
   454 				break;
   455 			default :
   456 				setCursor (Qt::ArrowCursor);
   457 				break;
   458 		} 
   459 	}	
   460 }
   461 
   462 void MapEditor::keyReleaseEvent(QKeyEvent* e)
   463 {
   464 	if (!(e->modifiers() & Qt::ControlModifier))
   465 		setCursor (Qt::ArrowCursor);
   466 }
   467 
   468 void MapEditor::mousePressEvent(QMouseEvent* e)
   469 {
   470 	// Ignore right clicks, these will go to context menus
   471 	if (e->button() == Qt::RightButton )
   472 	{
   473 		e->ignore();
   474 		return;
   475 	}
   476 
   477 	//Ignore clicks while editing heading
   478 	if (model->isSelectionBlocked() ) 
   479 	{
   480 		e->ignore();
   481 		return;
   482 	}
   483 
   484     QPointF p = mapToScene(e->pos());
   485     LinkableMapObj* lmo=model->findMapObj(p, NULL);
   486 	
   487 	e->accept();
   488 
   489 	//Take care of  system flags _or_ modifier modes
   490 	//
   491 	if (lmo && (typeid(*lmo)==typeid(BranchObj) ||
   492 		typeid(*lmo)==typeid(MapCenterObj) ))
   493 	{
   494 		QString foname=((BranchObj*)lmo)->getSystemFlagName(p);
   495 		if (!foname.isEmpty())
   496 		{
   497 			// systemFlag clicked
   498 			model->selectInt (lmo);
   499 			if (foname=="url") 
   500 			{
   501 				if (e->state() & Qt::ControlModifier)
   502 					mainWindow->editOpenURLTab();
   503 				else	
   504 					mainWindow->editOpenURL();
   505 			}	
   506 			else if (foname=="vymLink")
   507 			{
   508 				mainWindow->editOpenVymLink();
   509 				// tabWidget may change, better return now
   510 				// before segfaulting...
   511 			} else if (foname=="note")
   512 				mainWindow->windowToggleNoteEditor();
   513 			else if (foname=="hideInExport")		
   514 				model->toggleHideExport();
   515 			// FIXME needed? xelection.update();	
   516 			return;	
   517 		} 
   518 	}	
   519 	// No system flag clicked, take care of modmodes (CTRL-Click)
   520 	if (e->state() & Qt::ControlModifier)
   521 	{
   522 		if (mainWindow->getModMode()==Main::ModModeColor)
   523 		{
   524 				pickingColor=true;
   525 				setCursor (PickColorCursor);
   526 				return;
   527 		} 
   528 		if (mainWindow->getModMode()==Main::ModModeXLink)
   529 		{	
   530 			BranchObj *bo_begin=NULL;
   531 			if (lmo)
   532 				bo_begin=(BranchObj*)(lmo);
   533 			else	
   534 				bo_begin=model->getSelectedBranch();
   535 			if (bo_begin)	
   536 			{
   537 				drawingLink=true;
   538 				linkingObj_src=bo_begin;
   539 				tmpXLink=new XLinkObj (mapScene);
   540 				tmpXLink->setBegin (bo_begin);
   541 				tmpXLink->setEnd   (p);
   542 				tmpXLink->setColor(model->getMapDefXLinkColor());
   543 				tmpXLink->setWidth(model->getMapDefXLinkWidth());
   544 				tmpXLink->updateXLink();
   545 				tmpXLink->setVisibility (true);
   546 				return;
   547 			} 
   548 		}
   549 	}	// End of modmodes
   550 
   551     if (lmo) 
   552 	{	
   553 	/*
   554 		cout << "ME::mouse pressed\n";
   555 		cout << "  lmo="<<lmo<<endl;
   556 		cout << "  h="<<((BranchObj*)lmo)->getHeading().toStdString()<<endl;
   557 	*/
   558 		// Select the clicked object
   559 
   560 		// FIXME VM better let "find" return an index instead of lmo...
   561 		// Get index of clicked LMO
   562 		TreeItem *ti=lmo->getTreeItem();
   563 		/*
   564 		cout << "  lmo="<<lmo<<"    lmo(ti)="<<ti->getLMO()<<endl;
   565 		cout << "  ti ("<<ti->row()<<","<<ti->column()<<") = "<<ti<<endl;
   566 		*/
   567 		//QModelIndex ix=model->index( ti->row(), ti->column(), model->index (0,0,QModelIndex()) );
   568 		model->select (ti);
   569 
   570 		// Left Button	    Move Branches
   571 		if (e->button() == Qt::LeftButton )
   572 		{
   573 			//movingObj_start.setX( p.x() - selection->x() );// TODO replaced selection->lmo here	
   574 			//movingObj_start.setY( p.y() - selection->y() );	
   575 			movingObj_start.setX( p.x() - lmo->x() );	
   576 			movingObj_start.setY( p.y() - lmo->y() );	
   577 			movingObj_orgPos.setX (lmo->x() );
   578 			movingObj_orgPos.setY (lmo->y() );
   579 			lmo->setRelPos();
   580 			movingObj_orgRelPos=lmo->getRelPos();
   581 
   582 			// If modMode==copy, then we want to "move" the _new_ object around
   583 			// then we need the offset from p to the _old_ selection, because of tmp
   584 			if (mainWindow->getModMode()==Main::ModModeCopy &&
   585 				e->state() & Qt::ControlModifier)
   586 			{
   587 				BranchObj *bo=model->getSelectedBranch();
   588 				if (bo)
   589 				{
   590 					copyingObj=true;
   591 					bo->addBranch (model->getSelectedBranch());
   592 					model->unselect();
   593 					model->select(bo->getLastBranch());
   594 					model->reposition();
   595 				}
   596 			} 
   597 
   598 			movingObj=model->getSelectedLMO();	
   599 		} else
   600 			// Middle Button    Toggle Scroll
   601 			// (On Mac OS X this won't work, but we still have 
   602 			// a button in the toolbar)
   603 			if (e->button() == Qt::MidButton )
   604 				model->toggleScroll();
   605 		model->updateActions();
   606 		// FIXME needed? xelection.update();
   607 	} else 
   608 	{ // No MapObj found, we are on the scene itself
   609 		// Left Button	    move Pos of sceneView
   610 		if (e->button() == Qt::LeftButton )
   611 		{
   612 			movingObj=NULL;	// move Content not Obj
   613 			movingObj_start=e->globalPos();
   614 			movingCont_start=QPointF (
   615 				horizontalScrollBar()->value(),
   616 				verticalScrollBar()->value());
   617 			movingVec=QPointF(0,0);
   618 			setCursor(HandOpenCursor);
   619 		} 
   620     } 
   621 }
   622 
   623 void MapEditor::mouseMoveEvent(QMouseEvent* e)
   624 {
   625     QPointF p = mapToScene(e->pos());
   626 	LinkableMapObj *lmosel=model->getSelectedLMO();
   627 
   628     // Move the selected MapObj
   629     if ( lmosel && movingObj) 
   630     {	
   631 		// reset cursor if we are moving and don't copy
   632 		if (mainWindow->getModMode()!=Main::ModModeCopy)
   633 			setCursor (Qt::ArrowCursor);
   634 
   635 		// To avoid jumping of the sceneView, only 
   636 		// ensureSelectionVisible, if not tmp linked
   637 		if (!lmosel->hasParObjTmp())
   638 			model->ensureSelectionVisible ();
   639 		
   640 		// Now move the selection, but add relative position 
   641 		// (movingObj_start) where selection was chosen with 
   642 		// mousepointer. (This avoids flickering resp. jumping 
   643 		// of selection back to absPos)
   644 		
   645 		// Check if we could link 
   646 		LinkableMapObj* lmo=model->findMapObj(p, lmosel);
   647 		
   648 
   649 		FloatObj *fio=model->getSelectedFloatImage();
   650 		if (fio)
   651 		{
   652 			fio->move   (p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );		
   653 			fio->setRelPos();
   654 			fio->updateLink(); //no need for reposition, if we update link here
   655 			model->updateSelection();	// position has changed
   656 
   657 			// Relink float to new mapcenter or branch, if shift is pressed	
   658 			// Only relink, if selection really has a new parent
   659 			if ( (e->modifiers()==Qt::ShiftModifier) && lmo &&
   660 				( (typeid(*lmo)==typeid(BranchObj)) ||
   661 				  (typeid(*lmo)==typeid(MapCenterObj)) ) &&
   662 				( lmo != fio->getParObj())  
   663 				)
   664 			{
   665 				if (typeid(*fio) == typeid(FloatImageObj) && 
   666 				( (typeid(*lmo)==typeid(BranchObj) ||
   667 				  typeid(*lmo)==typeid(MapCenterObj)) ))  
   668 				{
   669 
   670 					// Also save the move which was done so far
   671 					QString pold=qpointfToString(movingObj_orgRelPos);
   672 					QString pnow=qpointfToString(fio->getRelPos());
   673 					model->saveState(
   674 						fio,
   675 						"moveRel "+pold,
   676 						fio,
   677 						"moveRel "+pnow,
   678 						QString("Move %1 to relative position %2").arg(model->getObjectName(fio)).arg(pnow));
   679 					fio->getParObj()->requestReposition();
   680 					model->reposition();
   681 
   682 					model->linkFloatImageTo (model->getSelectString(lmo));
   683 					//movingObj=lmosel;
   684 					//movingObj_orgRelPos=lmosel->getRelPos();	
   685 
   686 					model->reposition();
   687 				}	
   688 			}
   689 		} else	
   690 		{	// selection != a FloatObj
   691 			if (lmosel->getDepth()==0)
   692 			{
   693 				// Move MapCenter
   694 				if (e->buttons()== Qt::LeftButton && e->modifiers()==Qt::ShiftModifier) 
   695 					((MapCenterObj*)lmosel)->moveAll(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );		
   696 				else	
   697 					lmosel->move   (p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );		
   698 				model->updateRelPositions();
   699 			} else
   700 			{	
   701 				if (lmosel->getDepth()==1)
   702 				{
   703 					// Move mainbranch
   704 					lmosel->move(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );		
   705 					lmosel->setRelPos();
   706 				} else
   707 				{
   708 					// Move ordinary branch
   709 					if (lmosel->getOrientation() == LinkableMapObj::LeftOfCenter)
   710 						// Add width of bbox here, otherwise alignRelTo will cause jumping around
   711 						lmosel->move(p.x() -movingObj_start.x() , //lmosel->getBBox().width(), 
   712 							p.y()-movingObj_start.y() +lmosel->getTopPad() );		
   713 					else	
   714 						lmosel->move(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() -lmosel->getTopPad());
   715 					lmosel->setRelPos();	
   716 				} 
   717 
   718 				// Maybe we can relink temporary?
   719 				if (lmo && (lmo!=lmosel) && model->getSelectedBranch() && 
   720 					 (typeid(*lmo)==typeid(BranchObj) ||
   721 					  typeid(*lmo)==typeid(MapCenterObj)) ) 
   722 
   723 				{
   724 					if (e->modifiers()==Qt::ControlModifier)
   725 					{
   726 						// Special case: CTRL to link below lmo
   727 						lmosel->setParObjTmp (lmo,p,+1);
   728 					}
   729 					else if (e->modifiers()==Qt::ShiftModifier)
   730 						lmosel->setParObjTmp (lmo,p,-1);
   731 					else
   732 						lmosel->setParObjTmp (lmo,p,0);
   733 				} else	
   734 				{
   735 					lmosel->unsetParObjTmp();
   736 				}		
   737 				// reposition subbranch
   738 				lmosel->reposition();	
   739 			} // depth>0
   740 
   741 			QItemSelection sel=model->getSelectionModel()->selection();
   742 			updateSelection(sel,sel);	// position has changed
   743 
   744 		} // no FloatImageObj
   745 
   746 		scene()->update();
   747 		return;
   748 	} // selection && moving_obj
   749 		
   750 	// Draw a link from one branch to another
   751 	if (drawingLink)
   752 	{
   753 		 tmpXLink->setEnd (p);
   754 		 tmpXLink->updateXLink();
   755 	}	 
   756 	
   757     // Move sceneView 
   758     if (!movingObj && !pickingColor &&!drawingLink && e->buttons() == Qt::LeftButton ) 
   759 	{
   760 		QPointF p=e->globalPos();
   761 		movingVec.setX(-p.x() + movingObj_start.x() );
   762 		movingVec.setY(-p.y() + movingObj_start.y() );
   763 		horizontalScrollBar()->setSliderPosition((int)( movingCont_start.x()+movingVec.x() ));
   764 		verticalScrollBar()->setSliderPosition((int)( movingCont_start.y()+movingVec.y() ) );
   765     }
   766 }
   767 
   768 
   769 void MapEditor::mouseReleaseEvent(QMouseEvent* e)
   770 {
   771     QPointF p = mapToScene(e->pos());
   772 	LinkableMapObj *dst;
   773 	LinkableMapObj *lmosel=model->getSelectedLMO();
   774 	// Have we been picking color?
   775 	if (pickingColor)
   776 	{
   777 		pickingColor=false;
   778 		setCursor (Qt::ArrowCursor);
   779 		// Check if we are over another branch
   780 		dst=model->findMapObj(p, NULL);
   781 		if (dst && lmosel) 
   782 		{	
   783 			if (e->state() & Qt::ShiftModifier)
   784 				model->colorBranch (((BranchObj*)dst)->getColor());
   785 			else	
   786 				model->colorSubtree (((BranchObj*)dst)->getColor());
   787 		} 
   788 		return;
   789 	}
   790 
   791 	// Have we been drawing a link?
   792 	if (drawingLink)	
   793 	{
   794 		drawingLink=false;
   795 		// Check if we are over another branch
   796 		dst=model->findMapObj(p, NULL);
   797 		if (dst && lmosel) 
   798 		{	
   799 			tmpXLink->setEnd ( ((BranchObj*)(dst)) );
   800 			tmpXLink->updateXLink();
   801 			tmpXLink->activate(); //FIXME savestate missing
   802 			//model->saveStateComplete(QString("Activate xLink from %1 to %2").arg(model->getObjectName(tmpXLink->getBegin())).arg(model->getObjectName(tmpXLink->getEnd())) );	
   803 		} else
   804 		{
   805 			delete(tmpXLink);
   806 			tmpXLink=NULL;
   807 		}
   808 		return;
   809 	}
   810 	
   811     // Have we been moving something?
   812     if ( lmosel && movingObj ) 
   813     {	
   814 		FloatImageObj *fo=model->getSelectedFloatImage();
   815 		if(fo)
   816 		{
   817 			// Moved FloatObj. Maybe we need to reposition
   818 		    QString pold=qpointfToString(movingObj_orgRelPos);
   819 		    QString pnow=qpointfToString(fo->getRelPos());
   820 			model->saveState(
   821 				fo,
   822 				"moveRel "+pold,
   823 				fo,
   824 				"moveRel "+pnow,
   825 				QString("Move %1 to relative position %2").arg(model->getObjectName(fo)).arg(pnow));
   826 
   827 			fo->getParObj()->requestReposition();
   828 			model->reposition();
   829 		}	
   830 
   831 		// Check if we are over another branch, but ignore 
   832 		// any found LMOs, which are FloatObjs
   833 		dst=model->findMapObj(mapToScene(e->pos() ), lmosel);
   834 
   835 		if (dst && (typeid(*dst)!=typeid(BranchObj) && typeid(*dst)!=typeid(MapCenterObj))) 
   836 			dst=NULL;
   837 		
   838 		BranchObj *bo=model->getSelectedBranch();
   839 		if (bo && bo->getDepth()==0)
   840 		{	
   841             if (movingObj_orgPos != bo->getAbsPos())
   842             {
   843                 QString pold=qpointfToString(movingObj_orgPos);
   844                 QString pnow=qpointfToString(bo->getAbsPos());
   845                 model->saveState(
   846                     bo,
   847                     "move "+pold,
   848                     bo,
   849                     "move "+pnow,
   850                     QString("Move mapcenter %1 to position %2").arg(model->getObjectName(bo)).arg(pnow));
   851             }
   852 		}
   853 	
   854 		if (model->selectionType() == TreeItem::Branch )
   855 		{	// A branch was moved
   856 			
   857 			// save the position in case we link to mapcenter
   858 			QPointF savePos=QPointF (lmosel->getAbsPos()  );
   859 
   860 			// Reset the temporary drawn link to the original one
   861 			lmosel->unsetParObjTmp();
   862 
   863 			// For Redo we may need to save original selection
   864 			QString preSelStr=model->getSelectString(lmosel);
   865 
   866 			copyingObj=false;	
   867 			if (dst ) 
   868 			{
   869 				// We have a destination, relink to that
   870 
   871 				BranchObj* bsel=model->getSelectedBranch();
   872 				BranchObj* bdst=(BranchObj*)dst;
   873 
   874 				QString preParStr=model->getSelectString (bsel->getParObj());
   875 				QString preNum=QString::number (bsel->getNum(),10);
   876 				QString preDstParStr;
   877 
   878 				if (e->state() & Qt::ShiftModifier && dst->getParObj())
   879 				{	// Link above dst
   880 					preDstParStr=model->getSelectString (dst->getParObj());
   881 					bsel->linkTo ( (BranchObj*)(bdst->getParObj()), bdst->getNum());
   882 				} else 
   883 				if (e->state() & Qt::ControlModifier && dst->getParObj())
   884 				{
   885 					// Link below dst
   886 					preDstParStr=model->getSelectString (dst->getParObj());
   887 					bsel->linkTo ( (BranchObj*)(bdst->getParObj()), bdst->getNum()+1);
   888 				} else	
   889 				{	// Append to dst
   890 					preDstParStr=model->getSelectString(dst);
   891 					bsel->linkTo (bdst,-1);
   892 					if (dst->getDepth()==0) bsel->move (savePos);
   893 				} 
   894 				QString postSelStr=model->getSelectString(lmosel);
   895 				QString postNum=QString::number (bsel->getNum(),10);
   896 
   897 				QString undoCom="linkTo (\""+ 
   898 					preParStr+ "\"," + preNum  +"," + 
   899 					QString ("%1,%2").arg(movingObj_orgPos.x()).arg(movingObj_orgPos.y())+ ")";
   900 
   901 				QString redoCom="linkTo (\""+ 
   902 					preDstParStr + "\"," + postNum + "," +
   903 					QString ("%1,%2").arg(savePos.x()).arg(savePos.y())+ ")";
   904 
   905 				model->saveState (
   906 					postSelStr,undoCom,
   907 					preSelStr, redoCom,
   908 					QString("Relink %1 to %2").arg(model->getObjectName(bsel)).arg(model->getObjectName(dst)) );
   909 
   910 				model->reposition();	// not necessary if we undo temporary move  below
   911 			} else
   912 			{
   913 				// No destination, undo  temporary move
   914 
   915 				if (lmosel->getDepth()==1)
   916 				{
   917 					// The select string might be different _after_ moving around.
   918 					// Therefor reposition and then use string of old selection, too
   919 					model->reposition();
   920 
   921                     QPointF rp(lmosel->getRelPos());
   922                     if (rp != movingObj_orgRelPos)
   923                     {
   924                         QString ps=qpointfToString(rp);
   925                         model->saveState(
   926                             model->getSelectString(lmosel), "moveRel "+qpointfToString(movingObj_orgRelPos), 
   927                             preSelStr, "moveRel "+ps, 
   928                             QString("Move %1 to relative position %2").arg(model->getObjectName(lmosel)).arg(ps));
   929                     }
   930 				}
   931 
   932 				// Draw the original link, before selection was moved around
   933 				if (settings.value("/animation/use",false).toBool() && lmosel->getDepth()>1) 
   934 				{
   935 					lmosel->setRelPos();	// calc relPos first for starting point
   936 					QPointF dst=bo->getParObj()->getChildPos();
   937 			//		if (lmosel->getOrientation()==LinkableMapObj::LeftOfCenter) dst.setX (dst.x()+lmosel->width() );
   938 					
   939 					model->startAnimation(
   940 						(BranchObj*)lmosel,
   941 						lmosel->getRelPos(),
   942 						movingObj_orgRelPos
   943 //						QPointF (movingObj_orgPos.x() - dst.x(), movingObj_orgPos.y() - dst.y() )
   944 					);	
   945 				} else	
   946 					model->reposition();
   947 			}
   948 		}
   949 		 model->updateSelection();
   950 		// Finally resize scene, if needed
   951 		scene()->update();
   952 		movingObj=NULL;		
   953 
   954 		// Just make sure, that actions are still ok,e.g. the move branch up/down buttons...
   955 		model->updateActions();
   956 	} else 
   957 		// maybe we moved View: set old cursor
   958 		setCursor (Qt::ArrowCursor);
   959     
   960 }
   961 
   962 void MapEditor::mouseDoubleClickEvent(QMouseEvent* e)
   963 {
   964 	if (model->isSelectionBlocked() ) 
   965 	{
   966 		e->ignore();
   967 		return;
   968 	}
   969 
   970 	if (e->button() == Qt::LeftButton )
   971 	{
   972 		QPointF p = mapToScene(e->pos());
   973 		LinkableMapObj *lmo=model->findMapObj(p, NULL);
   974 		if (lmo) {	// MapObj was found
   975 			// First select the MapObj than edit heading
   976 			model->select (lmo);
   977 			editHeading();
   978 		}
   979 	}
   980 }
   981 
   982 void MapEditor::resizeEvent (QResizeEvent* e)
   983 {
   984 	QGraphicsView::resizeEvent( e );
   985 }
   986 
   987 void MapEditor::dragEnterEvent(QDragEnterEvent *event)
   988 {
   989 	//for (unsigned int i=0;event->format(i);i++) // Debug mime type
   990 	//	cerr << event->format(i) << endl;
   991 
   992 	if (event->mimeData()->hasImage())
   993 		event->acceptProposedAction();
   994 	else	
   995 		if (event->mimeData()->hasUrls())
   996 			event->acceptProposedAction();
   997 }
   998 
   999 void MapEditor::dragMoveEvent(QDragMoveEvent *)
  1000 {
  1001 }
  1002 
  1003 void MapEditor::dragLeaveEvent(QDragLeaveEvent *event)
  1004 {
  1005 	event->accept();
  1006 }
  1007 
  1008 void MapEditor::dropEvent(QDropEvent *event)
  1009 {
  1010 	BranchObj *sel=model->getSelectedBranch();
  1011 	if (sel)
  1012 	{
  1013 		if (debug)
  1014 			foreach (QString format,event->mimeData()->formats()) 
  1015 				cout << "MapEditor: Dropped format: "<<qPrintable (format)<<endl;
  1016 
  1017 
  1018 		QList <QUrl> uris;
  1019 		if (event->mimeData()->hasImage()) 
  1020 		{
  1021 			 QVariant imageData = event->mimeData()->imageData();
  1022 			 model->addFloatImage (qvariant_cast<QPixmap>(imageData));
  1023 		} else
  1024 		if (event->mimeData()->hasUrls())
  1025 			uris=event->mimeData()->urls();
  1026 
  1027 		if (uris.count()>0)
  1028 		{
  1029 			QStringList files;
  1030 			QString s;
  1031 			QString heading;
  1032 			BranchObj *bo;
  1033 			for (int i=0; i<uris.count();i++)
  1034 			{
  1035 				// Workaround to avoid adding empty branches
  1036 				if (!uris.at(i).toString().isEmpty())
  1037 				{
  1038 					bo=sel->addBranch();
  1039 					if (bo)
  1040 					{
  1041 						s=uris.at(i).toLocalFile();
  1042 						if (!s.isEmpty()) 
  1043 						{
  1044 						   QString file = QDir::fromNativeSeparators(s);
  1045 						   heading = QFileInfo(file).baseName();
  1046 						   files.append(file);
  1047 						   if (file.endsWith(".vym", false))
  1048 							   bo->setVymLink(file);
  1049 						   else
  1050 							   bo->setURL(uris.at(i).toString());
  1051 					   } else 
  1052 					   {
  1053 						   bo->setURL(uris.at(i).toString());
  1054 					   }
  1055 
  1056 					   if (!heading.isEmpty())
  1057 						   bo->setHeading(heading);
  1058 					   else
  1059 						   bo->setHeading(uris.at(i).toString());
  1060 					}
  1061 				}
  1062 			}
  1063 			model->reposition();
  1064 		}
  1065 	}	
  1066 	event->acceptProposedAction();
  1067 }
  1068 
  1069 void MapEditor::updateSelection(const QItemSelection &newsel,const QItemSelection &)
  1070 {
  1071 	// Reduce rectangles
  1072 	while (newsel.indexes().count() < selboxList.count() )
  1073 		delete selboxList.takeFirst();
  1074 
  1075 	// Add additonal rectangles
  1076 	QGraphicsRectItem *sb;
  1077 	while (newsel.indexes().count() > selboxList.count() )
  1078 	{
  1079 		sb = mapScene->addRect(
  1080 			QRectF(0,0,0,0), 
  1081 			QPen(selectionColor),
  1082 			selectionColor);
  1083 		sb->setZValue(Z_SELBOX);
  1084 		sb->show();
  1085 		selboxList.append (sb);
  1086 	}
  1087 
  1088 	// Reposition rectangles
  1089 	int i=0;
  1090 	QRectF bbox;
  1091 	QModelIndex index;
  1092 
  1093 	TreeItem *ti;
  1094 	LinkableMapObj *lmo;
  1095 	foreach (sb,selboxList)
  1096 	{
  1097 		index=newsel.indexes().at(i);
  1098 		ti= static_cast<TreeItem*>(index.internalPointer());
  1099 		lmo=ti->getLMO();
  1100 		bbox=lmo->getBBox();
  1101 		sb->setRect (
  1102 			bbox.x(),bbox.y(), 
  1103 			bbox.width(), bbox.height());
  1104 		sb->setPen (selectionColor);	
  1105 		sb->setBrush (selectionColor);	
  1106 		i++;
  1107 	}
  1108 }
  1109 
  1110 void MapEditor::updateCurrent (const QModelIndex &,const QModelIndex &)	//FIXME not used?
  1111 {
  1112 
  1113 /* FIXME testing
  1114 
  1115 	cout << "ME::updateCurrent\n";
  1116 
  1117 	TreeItem *item = static_cast<TreeItem*>(newsel.internalPointer());
  1118 	LinkableMapObj *lmo=item->getLMO();
  1119 	cout << "  lmo="<<lmo<<endl;
  1120 	cout << "  h="<<((BranchObj*)lmo)->getHeading().toStdString()<<endl;
  1121 	*/
  1122 
  1123 }
  1124 
  1125 void MapEditor::setSelectionColor (QColor col)
  1126 {
  1127 	selectionColor=col;
  1128 	QItemSelection sel=model->getSelectionModel()->selection();
  1129 	updateSelection(sel,sel);
  1130 }
  1131 
  1132 QColor MapEditor::getSelectionColor ()
  1133 {
  1134 	return selectionColor;
  1135 }
  1136 
  1137