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