mapeditor.cpp
author insilmaril
Mon, 07 Sep 2009 15:36:57 +0000
changeset 791 f1006de05c54
parent 790 133e2ed6b9c5
child 792 7d67be709091
permissions -rw-r--r--
Fixed several Model errors using ModelTest
     1 #include "mapeditor.h"
     2 
     3 #include <iostream>
     4 #include <cstdlib>
     5 #include <typeinfo>
     6 
     7 #include <QObject>
     8 
     9 #include "branchitem.h"
    10 #include "mainwindow.h"
    11 #include "misc.h"
    12 #include "warningdialog.h"
    13 #include "xlinkitem.h"
    14 
    15 
    16 extern int statusbarTime;
    17 extern Main *mainWindow;
    18 extern QString tmpVymDir;
    19 extern QString clipboardDir;
    20 extern QString clipboardFile;
    21 extern bool clipboardEmpty;
    22 extern bool debug;
    23 
    24 extern QMenu* branchContextMenu;
    25 extern QMenu* branchAddContextMenu;
    26 extern QMenu* branchRemoveContextMenu;
    27 extern QMenu* branchLinksContextMenu;
    28 extern QMenu* branchXLinksContextMenuEdit;
    29 extern QMenu* branchXLinksContextMenuFollow;
    30 extern QMenu* floatimageContextMenu;
    31 extern QMenu* canvasContextMenu;
    32 
    33 extern Settings settings;
    34 extern QString iconPath;
    35 
    36 ///////////////////////////////////////////////////////////////////////
    37 ///////////////////////////////////////////////////////////////////////
    38 MapEditor::MapEditor( VymModel *vm) 
    39 {
    40 	//cout << "Constructor ME "<<this<<endl;
    41 	mapScene= new QGraphicsScene(NULL);
    42 	mapScene->setBackgroundBrush (QBrush(Qt::white, Qt::SolidPattern));
    43 
    44 	zoomFactor=zoomFactorTarget=1;
    45 
    46 	model=vm;
    47 	model->setScene (mapScene);
    48 	model->registerEditor(this);
    49 	model->makeDefault();	// No changes in model so far
    50 
    51     setScene (mapScene);
    52 
    53     printer=NULL;
    54 
    55 	// Create bitmap cursors, platform dependant
    56 	HandOpenCursor=QCursor (QPixmap(iconPath+"cursorhandopen.png"),1,1);		
    57 	PickColorCursor=QCursor ( QPixmap(iconPath+"cursorcolorpicker.png"), 5,27 ); 
    58 	CopyCursor=QCursor ( QPixmap(iconPath+"cursorcopy.png"), 1,1 ); 
    59 	XLinkCursor=QCursor ( QPixmap(iconPath+"cursorxlink.png"), 1,7 ); 
    60 
    61 	//setFocusPolicy (Qt::StrongFocus);	//FIXME-3
    62 
    63 	pickingColor=false;
    64 	drawingLink=false;
    65 	copyingObj=false;
    66 
    67     editingBO=NULL;
    68     movingObj=NULL;
    69 
    70 	printFrame=true;
    71 	printFooter=true;
    72 
    73 	setAcceptDrops (true);	
    74 
    75 	//model->reposition();	//FIXME-3 really still needed?
    76 
    77 
    78 	// Shortcuts and actions
    79 	QAction *a;
    80     a = new QAction("Select upper branch", this);
    81 	a->setShortcut (Qt::Key_Up );
    82 	a->setShortcutContext (Qt::WidgetShortcut);
    83 	addAction (a);
    84     connect( a, SIGNAL( triggered() ), this, SLOT( cursorUp() ) );
    85 
    86     a = new QAction( "Select lower branch",this);
    87 	a->setShortcut ( Qt::Key_Down );
    88 	a->setShortcutContext (Qt::WidgetShortcut);
    89 	addAction (a);
    90     connect( a, SIGNAL( triggered() ), this, SLOT( cursorDown() ) );
    91 
    92     a = new QAction( "Select left branch", this);
    93 	a->setShortcut (Qt::Key_Left );
    94 //	a->setShortcutContext (Qt::WindowShortcut);
    95 	a->setShortcutContext (Qt::WidgetWithChildrenShortcut);
    96 	addAction (a);
    97     connect( a, SIGNAL( triggered() ), this, SLOT( cursorLeft() ) );
    98 
    99     a = new QAction( "Select child branch", this);
   100 	a->setShortcut (Qt::Key_Right);
   101 	a->setShortcutContext (Qt::WidgetWithChildrenShortcut);
   102 	addAction (a);
   103     connect( a, SIGNAL( triggered() ), this, SLOT( cursorRight() ) );
   104 
   105     a = new QAction(  "Select first branch", this);
   106 	a->setShortcut (Qt::Key_Home );
   107 	a->setShortcutContext (Qt::WidgetWithChildrenShortcut);
   108 	addAction (a);
   109     connect( a, SIGNAL( triggered() ), this, SLOT( cursorFirst() ) );
   110 
   111     a = new QAction( "Select last branch",this);
   112 	a->setShortcut ( Qt::Key_End );
   113 	a->setShortcutContext (Qt::WidgetWithChildrenShortcut);
   114 	addAction (a);
   115     connect( a, SIGNAL( triggered() ), this, SLOT( cursorLast() ) );
   116 
   117 	// Action to embed LineEdit for heading in Scene
   118 	editingHeading=false;
   119 	lineEdit=new QLineEdit;
   120 	lineEdit->hide();
   121 	QGraphicsProxyWidget *pw=scene()->addWidget (lineEdit);
   122 	pw->setZValue (Z_LINEEDIT);
   123 
   124 	a = new QAction( tr( "Edit heading","MapEditor" ), this);
   125 	a->setShortcut ( Qt::Key_Return );					//Edit heading
   126 	addAction (a);
   127     connect( a, SIGNAL( triggered() ), this, SLOT( editHeading() ) );
   128 	a = new QAction( tr( "Edit heading","MapEditor" ), this);
   129 	a->setShortcut ( Qt::Key_Enter);					//Edit heading
   130 	addAction (a);
   131     connect( a, SIGNAL( triggered() ), this, SLOT( editHeading() ) );
   132 
   133 	// Selections
   134 	selectionColor =QColor (255,255,0);
   135 	
   136 
   137 	// Attributes	//FIXME-2 testing only...
   138 	QString k;
   139 	AttributeDef *ad;
   140 	attrTable= new AttributeTable();
   141 	k="A - StringList";
   142 	ad=attrTable->addKey (k,StringList);
   143 	if (ad)
   144 	{
   145 		QStringList sl;
   146 		sl <<"val 1"<<"val 2"<< "val 3";
   147 		ad->setValue (QVariant (sl));
   148 	}
   149 	//attrTable->addValue ("Key A","P 1");
   150 	//attrTable->addValue ("Key A","P 2");
   151 	//attrTable->addValue ("Key A","P 3");
   152 	//attrTable->addValue ("Key A","P 4");
   153 	k="B - FreeString";
   154 	ad=attrTable->addKey (k,FreeString);
   155 	if (ad)
   156 	{
   157 		//attrTable->addValue ("Key B","w1");
   158 		//attrTable->addValue ("Key B","w2");
   159 	}
   160 	k="C - UniqueString";
   161 	ad=attrTable->addKey (k,UniqueString);
   162 	if (ad)
   163 	{
   164 	//attrTable->addKey ("Key Prio");
   165 	//attrTable->addValue ("Key Prio","Prio 1");
   166 	//attrTable->addValue ("Key Prio","Prio 2");
   167 	}
   168 }
   169 
   170 MapEditor::~MapEditor()
   171 {
   172 	//cout <<"Destructor MapEditor for "<<model->getMapName().toStdString()<<endl;
   173 	model->unregisterEditor(this);
   174 }
   175 
   176 VymModel* MapEditor::getModel()
   177 {
   178     return model;
   179 }
   180 
   181 QGraphicsScene * MapEditor::getScene()
   182 {
   183     return mapScene;
   184 }
   185 
   186 void MapEditor::scrollTo (const QModelIndex &index)
   187 {
   188 	if (index.isValid())
   189 	{
   190 		LinkableMapObj* lmo=NULL;
   191 		TreeItem *ti= static_cast<TreeItem*>(index.internalPointer());
   192 		if (ti->getType()==TreeItem::Image ||ti->isBranchLikeType() )
   193 			lmo=((MapItem*)ti)->getLMO();
   194 		if (lmo) setScrollBarPosTarget (lmo->getBBox() );
   195 	}
   196 }
   197 
   198 void MapEditor::setScrollBarPosTarget (const QRectF &rect)
   199 {
   200 	// Code copied from Qt sources
   201 	int xmargin=50;
   202 	int ymargin=50;
   203 
   204     qreal width = viewport()->width();
   205     qreal height = viewport()->height();
   206     QRectF viewRect = matrix().mapRect(rect);
   207 
   208     qreal left = horizontalScrollBar()->value();
   209     qreal right = left + width;
   210     qreal top = verticalScrollBar()->value();
   211     qreal bottom = top + height;
   212 
   213     if (viewRect.left() <= left + xmargin) {
   214         // need to scroll from the left
   215   //      if (!d->leftIndent)
   216             scrollBarPosTarget.setX(int(viewRect.left() - xmargin - 0.5));
   217     }
   218     if (viewRect.right() >= right - xmargin) {
   219         // need to scroll from the right
   220 //        if (!d->leftIndent)
   221             scrollBarPosTarget.setX(int(viewRect.right() - width + xmargin + 0.5));
   222     }
   223     if (viewRect.top() <= top + ymargin) {
   224         // need to scroll from the top
   225    //     if (!d->topIndent)
   226             scrollBarPosTarget.setY(int(viewRect.top() - ymargin - 0.5));
   227     }
   228     if (viewRect.bottom() >= bottom - ymargin) {
   229         // need to scroll from the bottom
   230 //        if (!d->topIndent)
   231             scrollBarPosTarget.setY(int(viewRect.bottom() - height + ymargin + 0.5));
   232     }
   233 
   234 	if (scrollBarPosAnimation.state()==QtAbstractAnimation::Running)
   235 		scrollBarPosAnimation.stop();
   236 	scrollBarPosAnimation.setTargetObject (this);
   237 	scrollBarPosAnimation.setPropertyName ("scrollBarPos");
   238 	scrollBarPosAnimation.setDuration(1000);
   239 	scrollBarPosAnimation.setEasingCurve ( QtEasingCurve::OutQuint);
   240 	scrollBarPosAnimation.setStartValue(
   241 		QPointF (horizontalScrollBar()->value() ,
   242 		         verticalScrollBar()->value() ) );
   243 	scrollBarPosAnimation.setEndValue(scrollBarPosTarget);
   244 	scrollBarPosAnimation.start();
   245 }
   246 
   247 QPointF MapEditor::getScrollBarPosTarget()
   248 {
   249     return scrollBarPosTarget;
   250 }
   251 
   252 
   253 void MapEditor::setScrollBarPos(const QPointF &p)
   254 {
   255     scrollBarPos=p;
   256 	horizontalScrollBar()->setValue(int(p.x()));
   257 	verticalScrollBar()->setValue(int(p.y()));
   258 }
   259 
   260 QPointF MapEditor::getScrollBarPos()
   261 {
   262     return scrollBarPos;
   263 }
   264 
   265 void MapEditor::setZoomFactorTarget (const qreal &zft)
   266 {
   267 	zoomFactorTarget=zft;
   268 	if (zoomAnimation.state()==QtAbstractAnimation::Running)
   269 		zoomAnimation.stop();
   270 	//zoomAnimation=QtPropertyAnimation(this, "zoomFactor");
   271 	zoomAnimation.setTargetObject (this);
   272 	zoomAnimation.setPropertyName ("zoomFactor");
   273 	zoomAnimation.setDuration(1000);
   274 	zoomAnimation.setEasingCurve ( QtEasingCurve::OutQuint);
   275 	zoomAnimation.setStartValue(zoomFactor);
   276 	zoomAnimation.setEndValue(zft);
   277 	zoomAnimation.start();
   278 }
   279 
   280 qreal MapEditor::getZoomFactorTarget()
   281 {
   282     return zoomFactorTarget;
   283 }
   284 
   285 
   286 void MapEditor::setZoomFactor(const qreal &zf)
   287 {
   288     zoomFactor=zf;
   289 	setMatrix (QMatrix(zf, 0, 0, zf, 0, 0),false );
   290 }
   291 
   292 qreal MapEditor::getZoomFactor()
   293 {
   294     return zoomFactor;
   295 }
   296 
   297 void MapEditor::print()
   298 {
   299 	if ( !printer ) 
   300 	{
   301 		printer = new QPrinter;
   302 		printer->setColorMode (QPrinter::Color);
   303 		printer->setPrinterName (settings.value("/mainwindow/printerName",printer->printerName()).toString());
   304 		printer->setOutputFormat((QPrinter::OutputFormat)settings.value("/mainwindow/printerFormat",printer->outputFormat()).toInt());
   305 		printer->setOutputFileName(settings.value("/mainwindow/printerFileName",printer->outputFileName()).toString());
   306 	}
   307 
   308 	QRectF totalBBox=getTotalBBox();
   309 
   310 	// Try to set orientation automagically
   311 	// Note: Interpretation of generated postscript is amibiguous, if 
   312 	// there are problems with landscape mode, see
   313 	// http://sdb.suse.de/de/sdb/html/jsmeix_print-cups-landscape-81.html
   314 
   315 	if (totalBBox.width()>totalBBox.height())
   316 		// recommend landscape
   317 		printer->setOrientation (QPrinter::Landscape);
   318 	else	
   319 		// recommend portrait
   320 		printer->setOrientation (QPrinter::Portrait);
   321 
   322 	if ( printer->setup(this) ) 
   323 	// returns false, if printing is canceled
   324 	{
   325 		QPainter pp(printer);
   326 
   327 		pp.setRenderHint(QPainter::Antialiasing,true);
   328 
   329 		// Don't print the visualisation of selection
   330 		model->unselect();
   331 
   332 		QRectF mapRect=totalBBox;
   333 		QGraphicsRectItem *frame=NULL;
   334 
   335 		if (printFrame) 
   336 		{
   337 			// Print frame around map
   338 			mapRect.setRect (totalBBox.x()-10, totalBBox.y()-10, 
   339 				totalBBox.width()+20, totalBBox.height()+20);
   340 			frame=mapScene->addRect (mapRect, QPen(Qt::black),QBrush(Qt::NoBrush));
   341 			frame->setZValue(0);
   342 			frame->show();    
   343 		}		
   344 
   345 
   346 		double paperAspect = (double)printer->width()   / (double)printer->height();
   347 		double   mapAspect = (double)mapRect.width() / (double)mapRect.height();
   348 		int viewBottom;
   349 		if (mapAspect>=paperAspect)
   350 		{
   351 			// Fit horizontally to paper width
   352 			//pp.setViewport(0,0, printer->width(),(int)(printer->width()/mapAspect) );	
   353 			viewBottom=(int)(printer->width()/mapAspect);	
   354 		}	else
   355 		{
   356 			// Fit vertically to paper height
   357 			//pp.setViewport(0,0,(int)(printer->height()*mapAspect),printer->height());	
   358 			viewBottom=printer->height();	
   359 		}	
   360 		
   361 		if (printFooter) 
   362 		{
   363 			// Print footer below map
   364 			QFont font;		
   365 			font.setPointSize(10);
   366 			pp.setFont (font);
   367 			QRectF footerBox(0,viewBottom,printer->width(),15);
   368 			// FIXME-3 fileName not any longer available here: pp.drawText ( footerBox,Qt::AlignLeft,"VYM - " +fileName);
   369 			pp.drawText ( footerBox, Qt::AlignRight, QDate::currentDate().toString(Qt::TextDate));
   370 		}
   371 		mapScene->render (
   372 			&pp, 
   373 			QRectF (0,0,printer->width(),printer->height()-15),
   374 			QRectF(mapRect.x(),mapRect.y(),mapRect.width(),mapRect.height())
   375 		);
   376 		
   377 		// Viewport has paper dimension
   378 		if (frame)  delete (frame);
   379 
   380 		// Restore selection
   381 		model->reselect();
   382 
   383 		// Save settings in vymrc
   384 		settings.writeEntry("/mainwindow/printerName",printer->printerName());
   385 		settings.writeEntry("/mainwindow/printerFormat",printer->outputFormat());
   386 		settings.writeEntry("/mainwindow/printerFileName",printer->outputFileName());
   387 	}
   388 }
   389 
   390 QRectF MapEditor::getTotalBBox()	//FIXME-2 needed e.g. for image export
   391 {
   392 	QRectF r;
   393 /*
   394 	for (int i=0;i<rootItem->branchCount(); i++)
   395 		r=addBBox (rootItem->getBranchNum(i)->getTotalBBox(), r);
   396 */ 
   397 	return r;	
   398 }
   399 
   400 
   401 QPixmap MapEditor::getPixmap()
   402 {
   403 	QRectF mapRect=getTotalBBox();
   404 	QPixmap pix((int)mapRect.width()+2,(int)mapRect.height()+1);
   405 	QPainter pp (&pix);
   406 	
   407 	pp.setRenderHints(renderHints());
   408 
   409 	// Don't print the visualisation of selection
   410 	model->unselect();
   411 
   412 	mapScene->render (	&pp, 
   413 		QRectF(0,0,mapRect.width()+2,mapRect.height()+2),
   414 		QRectF(mapRect.x(),mapRect.y(),mapRect.width(),mapRect.height() ));
   415 
   416 	// Restore selection
   417 	model->reselect();
   418 	
   419 	return pix;
   420 }
   421 
   422 
   423 void MapEditor::setAntiAlias (bool b)
   424 {
   425 	setRenderHint(QPainter::Antialiasing,b);
   426 }
   427 
   428 void MapEditor::setSmoothPixmap(bool b)
   429 {
   430 	setRenderHint(QPainter::SmoothPixmapTransform,b);
   431 }
   432 
   433 TreeItem* MapEditor::findMapItem (QPointF p,TreeItem *exclude)
   434 {
   435 	// Start with mapcenter, no images allowed at rootItem
   436 	int i=0;
   437 	BranchItem *bi=model->getRootItem()->getFirstBranch();
   438 	TreeItem *found=NULL;
   439 	while (bi)
   440 	{
   441 		found=bi->findMapItem (p, exclude);
   442 		if (found) return found;
   443 		i++;
   444 		bi=model->getRootItem()->getBranchNum(i);
   445 	}
   446 	return NULL;
   447 }
   448 
   449 AttributeTable* MapEditor::attributeTable()
   450 {
   451 	return attrTable;
   452 }
   453 
   454 void MapEditor::testFunction1()
   455 {
   456 	/*
   457 	// Code copied from Qt sources
   458 	QRectF rect=model->getSelectedBranchObj()->getBBox();
   459 	int xmargin=50;
   460 	int ymargin=50;
   461 
   462     qreal width = viewport()->width();
   463     qreal height = viewport()->height();
   464     QRectF viewRect = matrix().mapRect(rect);
   465 
   466     qreal left = horizontalScrollBar()->value();
   467     qreal right = left + width;
   468     qreal top = verticalScrollBar()->value();
   469     qreal bottom = top + height;
   470 
   471     if (viewRect.left() <= left + xmargin) {
   472         // need to scroll from the left
   473   //      if (!d->leftIndent)
   474             horizontalScrollBar()->setValue(int(viewRect.left() - xmargin - 0.5));
   475     }
   476     if (viewRect.right() >= right - xmargin) {
   477         // need to scroll from the right
   478 //        if (!d->leftIndent)
   479             horizontalScrollBar()->setValue(int(viewRect.right() - width + xmargin + 0.5));
   480     }
   481     if (viewRect.top() <= top + ymargin) {
   482         // need to scroll from the top
   483    //     if (!d->topIndent)
   484             verticalScrollBar()->setValue(int(viewRect.top() - ymargin - 0.5));
   485     }
   486     if (viewRect.bottom() >= bottom - ymargin) {
   487         // need to scroll from the bottom
   488 //        if (!d->topIndent)
   489             verticalScrollBar()->setValue(int(viewRect.bottom() - height + ymargin + 0.5));
   490     }
   491 	cout << "test1:  hor="<<horizontalScrollBar()->value()<<endl;
   492 	cout << "test1:  ver="<<verticalScrollBar()->value()<<endl;
   493 }
   494 
   495 */
   496 /*
   497 	 QtPropertyAnimation *animation=new QtPropertyAnimation(this, "sceneRect");
   498 	 animation->setDuration(5000);
   499 	 //animation->setEasingCurve ( QtEasingCurve::OutElastic);
   500 	 animation->setEasingCurve ( QtEasingCurve::OutQuint);
   501 	 animation->setStartValue(sceneRect() );
   502 	 animation->setEndValue(QRectF(50, 50, 1000, 1000));
   503 
   504 	 animation->start();
   505 */	 
   506 /*
   507 	QDialog *dia= new QDialog (this);
   508 	dia->setGeometry (50,50,10,10);
   509 
   510      dia->show();
   511      dia ->raise();
   512 
   513 	 QtPropertyAnimation *animation=new QtPropertyAnimation(dia, "geometry");
   514 	 animation->setDuration(1000);
   515 	 //animation->setEasingCurve ( QtEasingCurve::OutElastic);
   516 	 animation->setEasingCurve ( QtEasingCurve::OutQuint);
   517 	 animation->setStartValue(QRect(50, 50, 10, 10));
   518 	 animation->setEndValue(QRect(250, 250, 100, 100));
   519 
   520 	 animation->start();
   521  */
   522 
   523 /* FIXME-4 Hide hidden stuff temporary, maybe add this as regular function somewhere
   524 	if (hidemode==HideNone)
   525 	{
   526 		setHideTmpMode (HideExport);
   527 		mapCenter->calcBBoxSizeWithChilds();
   528 		QRectF totalBBox=mapCenter->getTotalBBox();
   529 		QRectF mapRect=totalBBox;
   530 		QCanvasRectangle *frame=NULL;
   531 
   532 		cout << "  map has =("<<totalBBox.x()<<","<<totalBBox.y()<<","<<totalBBox.width()<<","<<totalBBox.height()<<")\n";
   533 	
   534 		mapRect.setRect (totalBBox.x(), totalBBox.y(), 
   535 			totalBBox.width(), totalBBox.height());
   536 		frame=new QCanvasRectangle (mapRect,mapScene);
   537 		frame->setBrush (QColor(white));
   538 		frame->setPen (QColor(black));
   539 		frame->setZValue(0);
   540 		frame->show();    
   541 	}	
   542 	else	
   543 	{
   544 		setHideTmpMode (HideNone);
   545 	}	
   546 	cout <<"  hidemode="<<hidemode<<endl;
   547 	*/
   548 }
   549 	
   550 void MapEditor::testFunction2()
   551 {
   552 
   553 /*
   554 	// Toggle hidemode
   555 	if (hidemode==HideExport)
   556 		setHideTmpMode (HideNone);
   557 	else	
   558 		setHideTmpMode (HideExport);
   559 */		
   560 }
   561 
   562 BranchItem* MapEditor::getBranchDirectAbove (BranchItem *bi)
   563 {
   564 	if (bi)
   565 	{
   566 		int i=bi->num();
   567 		if (i>0) return bi->parent()->getBranchNum(i-1);
   568 	}
   569 	return NULL;
   570 }
   571 
   572 BranchItem* MapEditor::getBranchAbove (BranchItem *selbi)
   573 {
   574 	if (selbi)
   575 	{
   576 		int dz=selbi->depth();	// original depth
   577 		bool invert=false;
   578 		if (selbi->getLMO()->getOrientation()==LinkableMapObj::LeftOfCenter)
   579 			invert=true;
   580 
   581 		BranchItem *bi;
   582 
   583 		// Look for branch with same parent but directly above
   584 		if (dz==1 && invert)
   585 			bi=getBranchDirectBelow(selbi);
   586 		else
   587 			bi=getBranchDirectAbove (selbi);
   588 
   589 		if (bi) 
   590 			// direct predecessor
   591 			return bi;
   592 
   593 		// Go towards center and look for predecessor
   594 		while (selbi->depth()>0)
   595 		{
   596 			selbi=(BranchItem*)(selbi->parent());
   597 			if (selbi->depth()==1 && invert)
   598 				bi=getBranchDirectBelow (selbi);
   599 			else
   600 				bi=getBranchDirectAbove (selbi);
   601 			if (bi)
   602 			{
   603 				// turn 
   604 				selbi=bi;
   605 				while (selbi->depth()<dz)
   606 				{
   607 					// try to get back to original depth dz
   608 					bi=selbi->getLastBranch();
   609 					if (!bi) 
   610 					{
   611 						return selbi;
   612 					}
   613 					selbi=bi;
   614 				}
   615 				return selbi;
   616 			}
   617 		}
   618 	}
   619 	return NULL;
   620 }
   621 
   622 BranchItem* MapEditor::getBranchDirectBelow(BranchItem *bi)
   623 {
   624 	if (bi)
   625 	{
   626 		int i=bi->num();
   627 		if (i+1<bi->parent()->branchCount()) return bi->parent()->getBranchNum(i+1);
   628 	}
   629 	return NULL;
   630 }
   631 
   632 BranchItem* MapEditor::getBranchBelow (BranchItem *selbi)
   633 {
   634 	if (selbi)
   635 	{
   636 		BranchItem *bi;
   637 		int dz=selbi->depth();	// original depth
   638 		bool invert=false;
   639 		if (selbi->getLMO()->getOrientation()==LinkableMapObj::LeftOfCenter)
   640 			invert=true;
   641 
   642 
   643 		// Look for branch with same parent but directly below
   644 		if (dz==1 && invert)
   645 			bi=getBranchDirectAbove (selbi);
   646 		else
   647 			bi=getBranchDirectBelow (selbi);
   648 		if (bi) 
   649 			// direct successor
   650 			return bi;
   651 
   652 
   653 		// Go towards center and look for neighbour
   654 		while (selbi->depth()>0)
   655 		{
   656 			selbi=(BranchItem*)(selbi->parent());
   657 			if (selbi->depth()==1 && invert)
   658 				bi=getBranchDirectAbove (selbi);
   659 			else
   660 				bi=getBranchDirectBelow (selbi);
   661 			if (bi)
   662 			{
   663 				// turn 
   664 				selbi=bi;
   665 				while (selbi->depth()<dz)
   666 				{
   667 					// try to get back to original depth dz
   668 					bi=selbi->getFirstBranch();
   669 					if (!bi) 
   670 					{
   671 						return selbi;
   672 					}
   673 					selbi=bi;
   674 				}
   675 				return selbi;
   676 			}
   677 		}
   678 	}
   679 	return NULL;
   680 }
   681 
   682 BranchItem* MapEditor::getLeftBranch (BranchItem *bi)
   683 {
   684 	if (bi)
   685 	{
   686 		if (bi->depth()==0)
   687 			// Special case: use alternative selection index
   688 			return bi->getLastSelectedBranchAlt();	
   689 		if (bi->getBranchObj()->getOrientation()==LinkableMapObj::RightOfCenter)	
   690 			// right of center
   691 			return (BranchItem*)(bi->parent());
   692 		else
   693 			// left of center
   694 			if (bi->getType()== TreeItem::Branch )
   695 				return bi->getLastSelectedBranch();
   696 	}
   697 	return NULL;
   698 }
   699 
   700 BranchItem* MapEditor::getRightBranch(BranchItem *bi)
   701 {
   702 	if (bi)
   703 	{
   704 		if (bi->depth()==0) return bi->getLastSelectedBranch();	
   705 		if (bi->getBranchObj()->getOrientation()==LinkableMapObj::LeftOfCenter)	
   706 			// left of center
   707 			return (BranchItem*)(bi->parent());
   708 		else
   709 			// right of center
   710 			if (bi->getType()== TreeItem::Branch )
   711 				return (BranchItem*)bi->getLastSelectedBranch();
   712 	}
   713 	return NULL;
   714 }
   715 
   716 
   717 
   718 void MapEditor::cursorUp()
   719 {
   720 	BranchItem *bi=model->getSelectedBranch();
   721 	if (bi) model->select (getBranchAbove(bi));
   722 }
   723 
   724 void MapEditor::cursorDown()	
   725 
   726 {
   727 	BranchItem *bi=model->getSelectedBranch();
   728 	if (bi) model->select (getBranchBelow(bi));
   729 }
   730 
   731 void MapEditor::cursorLeft()
   732 {
   733 	BranchItem *bi=getLeftBranch (model->getSelectedBranch());
   734 	if (bi) model->select (bi);
   735 }
   736 
   737 void MapEditor::cursorRight()	
   738 {
   739 	BranchItem *bi=getRightBranch (model->getSelectedBranch());
   740 	if (bi) model->select (bi);
   741 }
   742 
   743 void MapEditor::cursorFirst()	
   744 {
   745 	model->selectFirstBranch();
   746 }
   747 
   748 void MapEditor::cursorLast()	
   749 {
   750 	model->selectLastBranch();
   751 }
   752 
   753 
   754 void MapEditor::editHeading()
   755 {
   756 	if (editingHeading)
   757 	{
   758 		editHeadingFinished();
   759 		return;
   760 	}
   761 	BranchObj *bo=model->getSelectedBranchObj();
   762 	BranchItem *bi=model->getSelectedBranch();
   763 	if (bo)	
   764 	{
   765 		model->setSelectionBlocked(true);
   766 
   767 		lineEdit->setText (bi->getHeading());
   768 		QPoint p = mapTo (this,bo->getAbsPos().toPoint() );
   769 		lineEdit->setGeometry(p.x(),p.y(),230,25);
   770 		lineEdit->selectAll();
   771 		lineEdit->show();
   772 		lineEdit->setFocus();
   773 		lineEdit->grabKeyboard();
   774 		editingHeading=true;
   775 	}
   776 
   777 }
   778 void MapEditor::editHeadingFinished()
   779 {
   780 	editingHeading=false;
   781 	lineEdit->releaseKeyboard();
   782 	model->setHeading (lineEdit->text() );
   783 	model->setSelectionBlocked(false);
   784 	lineEdit->hide();
   785 
   786 	// Maybe reselect previous branch 
   787 	mainWindow->editHeadingFinished (model);
   788 }
   789 
   790 
   791 void MapEditor::contextMenuEvent ( QContextMenuEvent * e )
   792 {
   793 	// Lineedits are already closed by preceding
   794 	// mouseEvent, we don't need to close here.
   795 
   796     QPointF p = mapToScene(e->pos());
   797     TreeItem *ti=findMapItem (p, NULL);
   798     LinkableMapObj* lmo=NULL;
   799 	if (ti) lmo=((MapItem*)ti)->getLMO();
   800 	
   801     if (lmo) 
   802 	{	// MapObj was found
   803 		if (model->getSelectedLMO() != lmo)
   804 		{
   805 			// select the MapObj
   806 			model->select(lmo);
   807 		}
   808 		// Context Menu 
   809 		if (model->getSelectedBranchObj() ) 
   810 		{
   811 			// Context Menu on branch or mapcenter
   812 			branchContextMenu->popup(e->globalPos() );
   813 		} else
   814 		{
   815 			if (model->getSelectedImage() )
   816 			{
   817 				// Context Menu on floatimage
   818 				floatimageContextMenu->popup(e->globalPos() );
   819 			}	
   820 		}	
   821 	} else 
   822 	{ // No MapObj found, we are on the Canvas itself
   823 		// Context Menu on scene
   824 		
   825 		// Open context menu synchronously to position new mapcenter
   826 		model->setContextPos (p);
   827 		canvasContextMenu->exec(e->globalPos() );
   828 		model->unsetContextPos ();
   829     } 
   830 	e->accept();
   831 }
   832 
   833 void MapEditor::keyPressEvent(QKeyEvent* e)
   834 {
   835 	if (e->modifiers() & Qt::ControlModifier)
   836 	{
   837 		switch (mainWindow->getModMode())
   838 		{
   839 			case Main::ModModeColor: 
   840 				setCursor (PickColorCursor);
   841 				break;
   842 			case Main::ModModeCopy: 
   843 				setCursor (CopyCursor);
   844 				break;
   845 			case Main::ModModeXLink: 
   846 				setCursor (XLinkCursor);
   847 				break;
   848 			default :
   849 				setCursor (Qt::ArrowCursor);
   850 				break;
   851 		} 
   852 	}	
   853 }
   854 
   855 void MapEditor::keyReleaseEvent(QKeyEvent* e)
   856 {
   857 	if (!(e->modifiers() & Qt::ControlModifier))
   858 		setCursor (Qt::ArrowCursor);
   859 }
   860 
   861 void MapEditor::mousePressEvent(QMouseEvent* e)
   862 {
   863 	// Ignore right clicks, these will go to context menus
   864 	if (e->button() == Qt::RightButton )
   865 	{
   866 		e->ignore();
   867 		return;
   868 	}
   869 
   870 	//Ignore clicks while editing heading
   871 	if (model->isSelectionBlocked() ) 
   872 	{
   873 		e->ignore();
   874 		return;
   875 	}
   876 
   877     QPointF p = mapToScene(e->pos());
   878     TreeItem *ti=findMapItem (p, NULL);
   879     LinkableMapObj* lmo=NULL;
   880 	if (ti) lmo=((MapItem*)ti)->getLMO();
   881 	
   882 	
   883 	e->accept();
   884 
   885 	//Take care of  system flags _or_ modifier modes
   886 	//
   887 	if (lmo && ti->isBranchLikeType() )
   888 	{
   889 		QString foname=((BranchObj*)lmo)->getSystemFlagName(p);
   890 		if (!foname.isEmpty())
   891 		{
   892 			// systemFlag clicked
   893 			model->select (lmo);	// FIXME-3 was selectInt
   894 			if (foname=="url") 
   895 			{
   896 				if (e->state() & Qt::ControlModifier)
   897 					mainWindow->editOpenURLTab();
   898 				else	
   899 					mainWindow->editOpenURL();
   900 			}	
   901 			else if (foname=="system-vymLink")
   902 			{
   903 				mainWindow->editOpenVymLink();
   904 				// tabWidget may change, better return now
   905 				// before segfaulting...
   906 			} else if (foname=="note")
   907 				mainWindow->windowToggleNoteEditor();
   908 			else if (foname=="hideInExport")		
   909 				model->toggleHideExport();
   910 			// FIXME-3 needed? xelection.update();	
   911 			return;	
   912 		} 
   913 	}	
   914 	// No system flag clicked, take care of modmodes (CTRL-Click)
   915 	if (e->state() & Qt::ControlModifier)
   916 	{
   917 		if (mainWindow->getModMode()==Main::ModModeColor)
   918 		{
   919 				pickingColor=true;
   920 				setCursor (PickColorCursor);
   921 				return;
   922 		} 
   923 		if (mainWindow->getModMode()==Main::ModModeXLink)
   924 		{	
   925 			BranchItem *bi_begin=model->getSelectedBranch();
   926 			if (bi_begin)	
   927 			{
   928 				drawingLink=true;
   929 				tmpXLink=model->createXLink(bi_begin,true);
   930 				tmpXLink->setColor(model->getMapDefXLinkColor());
   931 				tmpXLink->setWidth(model->getMapDefXLinkWidth());
   932 				tmpXLink->setEnd   (p);
   933 				tmpXLink->updateXLink();
   934 				return;
   935 			} 
   936 		}
   937 	}	// End of modmodes
   938 
   939     if (lmo) 
   940 	{	
   941 	/*
   942 		cout << "ME::mouse pressed\n";
   943 		cout << "  lmo="<<lmo<<endl;
   944 		cout << "   ti="<<ti->getHeadingStd()<<endl;
   945 	*/
   946 		// Select the clicked object
   947 
   948 		// Get clicked LMO
   949 		model->select (ti);
   950 
   951 		// Left Button	    Move Branches
   952 		if (e->button() == Qt::LeftButton )
   953 		{
   954 			movingObj_start.setX( p.x() - lmo->x() );	
   955 			movingObj_start.setY( p.y() - lmo->y() );	
   956 			movingObj_orgPos.setX (lmo->x() );
   957 			movingObj_orgPos.setY (lmo->y() );
   958 			lmo->setRelPos();
   959 			movingObj_orgRelPos=lmo->getRelPos();
   960 
   961 			// If modMode==copy, then we want to "move" the _new_ object around
   962 			// then we need the offset from p to the _old_ selection, because of tmp
   963 			if (mainWindow->getModMode()==Main::ModModeCopy &&
   964 				e->state() & Qt::ControlModifier)
   965 			{
   966 				BranchItem *bi=model->getSelectedBranch();
   967 				if (bi)
   968 				{
   969 					copyingObj=true;
   970 					//FIXME-2   TreeItem::addBranch (BranchItem still missing) 
   971 					//bi->addBranch (model->getSelectedBranch());
   972 					model->unselect();
   973 					model->select(bi->getLastBranch());
   974 					model->reposition();
   975 				}
   976 			} 
   977 
   978 			movingObj=model->getSelectedLMO();	
   979 		} else
   980 			// Middle Button    Toggle Scroll
   981 			// (On Mac OS X this won't work, but we still have 
   982 			// a button in the toolbar)
   983 			if (e->button() == Qt::MidButton )
   984 				model->toggleScroll();
   985 		// model->updateActions(); FIXME-3 needed?
   986 		// FIXME-3 needed? xelection.update();
   987 	} else 
   988 	{ // No MapObj found, we are on the scene itself
   989 		// Left Button	    move Pos of sceneView
   990 		if (e->button() == Qt::LeftButton )
   991 		{
   992 			movingObj=NULL;	// move Content not Obj
   993 			movingObj_start=e->globalPos();
   994 			movingCont_start=QPointF (
   995 				horizontalScrollBar()->value(),
   996 				verticalScrollBar()->value());
   997 			movingVec=QPointF(0,0);
   998 			setCursor(HandOpenCursor);
   999 		} 
  1000     } 
  1001 }
  1002 
  1003 void MapEditor::mouseMoveEvent(QMouseEvent* e)
  1004 {
  1005     QPointF p = mapToScene(e->pos());
  1006 	TreeItem *seli=model->getSelectedItem();
  1007 	LinkableMapObj* lmosel=NULL;	
  1008 	if (seli && (seli->isBranchLikeType() ||seli->getType()==TreeItem::Image))
  1009 		lmosel=((MapItem*)seli)->getLMO();
  1010 
  1011     // Move the selected MapObj
  1012     if ( lmosel && movingObj) 
  1013     {	
  1014 		// reset cursor if we are moving and don't copy
  1015 		if (mainWindow->getModMode()!=Main::ModModeCopy)
  1016 			setCursor (Qt::ArrowCursor);
  1017 
  1018 		// To avoid jumping of the sceneView, only 
  1019 		// show selection, if not tmp linked
  1020 		if (!lmosel->hasParObjTmp())
  1021 			model->emitShowSelection();
  1022 		
  1023 		// Now move the selection, but add relative position 
  1024 		// (movingObj_start) where selection was chosen with 
  1025 		// mousepointer. (This avoids flickering resp. jumping 
  1026 		// of selection back to absPos)
  1027 		
  1028 		// Check if we could link 
  1029 		TreeItem *ti=findMapItem (p, seli);
  1030 		BranchItem *dsti=NULL;
  1031 		LinkableMapObj* dst=NULL;
  1032 		if (ti && ti!=seli && ti->isBranchLikeType())
  1033 		{
  1034 			dsti=(BranchItem*)ti;
  1035 			dst=dsti->getLMO(); 
  1036 		} else
  1037 			dsti=NULL;
  1038 		
  1039 
  1040 		if (lmosel && seli->getType()==TreeItem::Image)	
  1041 		{
  1042 			FloatObj *fio=(FloatImageObj*)lmosel;
  1043 			fio->move   (p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );		
  1044 			fio->setRelPos();
  1045 			fio->updateLinkGeometry(); //no need for reposition, if we update link here
  1046 			model->emitSelectionChanged();	// position has changed
  1047 
  1048 			// Relink float to new mapcenter or branch, if shift is pressed	
  1049 			// Only relink, if selection really has a new parent
  1050 			if ( e->modifiers()==Qt::ShiftModifier && dsti &&  dsti != seli->parent()  )
  1051 			{
  1052 				// Also save the move which was done so far
  1053 				QString pold=qpointfToString(movingObj_orgRelPos);
  1054 				QString pnow=qpointfToString(fio->getRelPos());
  1055 				model->saveState(
  1056 					seli,
  1057 					"moveRel "+pold,
  1058 					seli,
  1059 					"moveRel "+pnow,
  1060 					QString("Move %1 to relative position %2").arg(model->getObjectName(fio)).arg(pnow));
  1061 				fio->getParObj()->requestReposition();
  1062 				model->reposition();
  1063 
  1064 				model->relinkImage ((ImageItem*) seli,dsti);
  1065 				model->select (seli);
  1066 				//movingObj=lmosel;	//FIXME-3
  1067 				//movingObj_orgRelPos=lmosel->getRelPos();	
  1068 
  1069 				model->reposition();
  1070 			}
  1071 		} else	
  1072 		{	// selection != a FloatObj
  1073 			if (seli->depth()==0)		//FIXME-1 also moved mapcenters could be linked, but not working here...
  1074 			{
  1075 				// Move MapCenter
  1076 				if (e->buttons()== Qt::LeftButton && e->modifiers()==Qt::ShiftModifier) 
  1077 					((BranchObj*)lmosel)->moveBy(
  1078 						QPointF(p.x() -movingObj_start.x(), 
  1079 						p.y()-movingObj_start.y()) );		
  1080 				else	
  1081 					lmosel->move   (p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );		
  1082 				model->updateRelPositions();
  1083 			} else
  1084 			{	
  1085 				if (seli->depth()==1)
  1086 				{
  1087 					// Move mainbranch
  1088 					lmosel->move(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );		
  1089 					lmosel->setRelPos();
  1090 				} else
  1091 				{
  1092 					// Move ordinary branch
  1093 					if (lmosel->getOrientation() == LinkableMapObj::LeftOfCenter)
  1094 						// Add width of bbox here, otherwise alignRelTo will cause jumping around
  1095 						lmosel->move(p.x() -movingObj_start.x() , //lmosel->getBBox().width(), 
  1096 							p.y()-movingObj_start.y() +lmosel->getTopPad() );		
  1097 					else	
  1098 						lmosel->move(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() -lmosel->getTopPad());
  1099 					lmosel->setRelPos();	
  1100 				} 
  1101 
  1102 				// Maybe we can relink temporary?
  1103 				if (dsti)
  1104 				{
  1105 					if (e->modifiers()==Qt::ControlModifier)
  1106 					{
  1107 						// Special case: CTRL to link below dst
  1108 						lmosel->setParObjTmp (dst,p,+1);
  1109 					}
  1110 					else if (e->modifiers()==Qt::ShiftModifier)
  1111 						lmosel->setParObjTmp (dst,p,-1);
  1112 					else
  1113 						lmosel->setParObjTmp (dst,p,0);
  1114 				} else	
  1115 				{
  1116 					lmosel->unsetParObjTmp();
  1117 				}		
  1118 				// reposition subbranch
  1119 				lmosel->reposition();	
  1120 			} // depth>0
  1121 
  1122 			QItemSelection sel=model->getSelectionModel()->selection();
  1123 			updateSelection(sel,sel);	// position has changed
  1124 
  1125 		} // no FloatImageObj
  1126 
  1127 		scene()->update();
  1128 		return;
  1129 	} // selection && moving_obj
  1130 		
  1131 	// Draw a link from one branch to another
  1132 	if (drawingLink)
  1133 	{
  1134 		 tmpXLink->setEnd (p);
  1135 		 tmpXLink->updateXLink();
  1136 	}	 
  1137 	
  1138     // Move sceneView 
  1139     if (!movingObj && !pickingColor &&!drawingLink && e->buttons() == Qt::LeftButton ) 
  1140 	{
  1141 		QPointF p=e->globalPos();
  1142 		movingVec.setX(-p.x() + movingObj_start.x() );
  1143 		movingVec.setY(-p.y() + movingObj_start.y() );
  1144 		horizontalScrollBar()->setSliderPosition((int)( movingCont_start.x()+movingVec.x() ));
  1145 		verticalScrollBar()->setSliderPosition((int)( movingCont_start.y()+movingVec.y() ) );
  1146     }
  1147 }
  1148 
  1149 
  1150 void MapEditor::mouseReleaseEvent(QMouseEvent* e)
  1151 {
  1152     QPointF p = mapToScene(e->pos());
  1153 	TreeItem *seli=model->getSelectedItem();
  1154 
  1155 	TreeItem *dsti=NULL;
  1156 	if (seli) dsti=findMapItem(p, seli);
  1157 	LinkableMapObj* dst=NULL;
  1158 	if (dsti && dsti->isBranchLikeType ()) 
  1159 		dst=((MapItem*)dsti)->getLMO();	
  1160 	else
  1161 		dsti=NULL;
  1162 
  1163 
  1164 	// Have we been picking color?
  1165 	if (pickingColor)
  1166 	{
  1167 		pickingColor=false;
  1168 		setCursor (Qt::ArrowCursor);
  1169 		// Check if we are over another branch
  1170 		if (dst) 
  1171 		{	
  1172 			if (e->state() & Qt::ShiftModifier)
  1173 				model->colorBranch (((BranchObj*)dst)->getColor());
  1174 			else	
  1175 				model->colorSubtree (((BranchObj*)dst)->getColor());
  1176 		} 
  1177 		return;
  1178 	}
  1179 
  1180 	// Have we been drawing a link?
  1181 	if (drawingLink)	
  1182 	{
  1183 		drawingLink=false;
  1184 		// Check if we are over another branch
  1185 		if (dsti)
  1186 		{	
  1187 			tmpXLink->setEnd ( ((BranchItem*)dsti) );
  1188 			tmpXLink->updateXLink();
  1189 			tmpXLink->activate(); 
  1190 			//FIXME-0 model->saveStateComplete(QString("Activate xLink from %1 to %2").arg(model->getObjectName(tmpXLink->getBegin())).arg(model->getObjectName(tmpXLink->getEnd())) );	
  1191 		} else
  1192 		{
  1193 			model->deleteItem(tmpXLink);
  1194 			tmpXLink=NULL;
  1195 		}
  1196 		return;
  1197 	}
  1198 	
  1199     // Have we been moving something?
  1200     if ( seli && movingObj ) 
  1201     {	
  1202 		if (seli->getType()==TreeItem::Image)
  1203 		{
  1204 			FloatImageObj *fio=(FloatImageObj*)( ((MapItem*)seli)->getLMO());
  1205 			if(fio)
  1206 			{
  1207 				// Moved FloatObj. Maybe we need to reposition
  1208 				QString pold=qpointfToString(movingObj_orgRelPos);
  1209 				QString pnow=qpointfToString(fio->getRelPos());
  1210 				model->saveState(
  1211 					seli,
  1212 					"moveRel "+pold,
  1213 					seli,
  1214 					"moveRel "+pnow,
  1215 					QString("Move %1 to relative position %2").arg(model->getObjectName(seli)).arg(pnow));
  1216 
  1217 				cout << "ME::release mouse\n";
  1218 				fio->getParObj()->requestReposition();
  1219 				model->reposition();
  1220 			}	
  1221 		}
  1222 
  1223 		BranchItem *bi=model->getSelectedBranch();
  1224 		if (bi && bi->depth()==0)
  1225 		{	
  1226             if (movingObj_orgPos != bi->getBranchObj()->getAbsPos())	// FIXME-3 check getBO here...
  1227             {
  1228                 QString pold=qpointfToString(movingObj_orgPos);
  1229                 QString pnow=qpointfToString(bi->getBranchObj()->getAbsPos());		// FIXME-3 check getBO here...
  1230 
  1231                 model->saveState(
  1232                     bi,
  1233                     "move "+pold,
  1234                     bi,
  1235                     "move "+pnow,
  1236                     QString("Move mapcenter %1 to position %2").arg(model->getObjectName(bi)).arg(pnow));
  1237             }
  1238 		}
  1239 	
  1240 		if (seli->isBranchLikeType() ) //(seli->getType() == TreeItem::Branch )
  1241 		{	// A branch was moved
  1242 			LinkableMapObj* lmosel=NULL;		
  1243 			lmosel=((MapItem*)seli)->getLMO();
  1244 				
  1245 			// save the position in case we link to mapcenter
  1246 			QPointF savePos=QPointF (lmosel->getAbsPos()  );
  1247 
  1248 			// Reset the temporary drawn link to the original one
  1249 			lmosel->unsetParObjTmp();
  1250 
  1251 			// For Redo we may need to save original selection
  1252 			QString preSelStr=model->getSelectString(seli);
  1253 
  1254 			copyingObj=false;	
  1255 			if (dsti)
  1256 			{
  1257 				// We have a destination, relink to that
  1258 
  1259 				BranchObj* bsel=model->getSelectedBranchObj();
  1260 
  1261 				QString preParStr=model->getSelectString (bsel->getParObj());
  1262 				QString preNum=QString::number (seli->num(),10);
  1263 				QString preDstParStr;
  1264 				bool relinked;
  1265 
  1266 				if (e->state() & Qt::ShiftModifier && dst->getParObj())
  1267 				{	// Link above dst
  1268 					preDstParStr=model->getSelectString (dst->getParObj());
  1269 					relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti->parent(),((BranchItem*)dsti)->num());
  1270 				} else 
  1271 				if (e->state() & Qt::ControlModifier && dst->getParObj())
  1272 				{
  1273 					// Link below dst
  1274 					preDstParStr=model->getSelectString (dst->getParObj());
  1275 					relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti->parent(),((BranchItem*)dsti)->num()+1);
  1276 				} else	
  1277 				{	// Append to dst
  1278 					preDstParStr=model->getSelectString(dst);
  1279 					relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti);
  1280 					if (dsti->depth()==0) bsel->move (savePos);
  1281 				} 
  1282 				if (relinked)
  1283 				{
  1284 					QString postSelStr=model->getSelectString(lmosel);
  1285 					QString postNum=QString::number (seli->num(),10);
  1286 
  1287 					QString undoCom="relinkTo (\""+ 
  1288 						preParStr+ "\"," + preNum  +"," + 
  1289 						QString ("%1,%2").arg(movingObj_orgPos.x()).arg(movingObj_orgPos.y())+ ")";
  1290 
  1291 					QString redoCom="relinkTo (\""+ 
  1292 						preDstParStr + "\"," + postNum + "," +
  1293 						QString ("%1,%2").arg(savePos.x()).arg(savePos.y())+ ")";
  1294 
  1295 					model->saveState (
  1296 						postSelStr,undoCom,
  1297 						preSelStr, redoCom,
  1298 						QString("Relink %1 to %2").arg(model->getObjectName(bsel)).arg(model->getObjectName(dst)) );
  1299 
  1300 				}
  1301 			} else
  1302 			{
  1303 				// No destination, undo  temporary move
  1304 
  1305 				if (seli->depth()==1)
  1306 				{
  1307 					// The select string might be different _after_ moving around.
  1308 					// Therefor reposition and then use string of old selection, too
  1309 					model->reposition();
  1310 
  1311                     QPointF rp(lmosel->getRelPos());
  1312                     if (rp != movingObj_orgRelPos)
  1313                     {
  1314                         QString ps=qpointfToString(rp);
  1315                         model->saveState(
  1316                             model->getSelectString(lmosel), "moveRel "+qpointfToString(movingObj_orgRelPos), 
  1317                             preSelStr, "moveRel "+ps, 
  1318                             QString("Move %1 to relative position %2").arg(model->getObjectName(lmosel)).arg(ps));
  1319                     }
  1320 				}
  1321 
  1322 				// Draw the original link, before selection was moved around
  1323 				if (settings.value("/animation/use",false).toBool() && seli->depth()>1) 
  1324 				{
  1325 					lmosel->setRelPos();	// calc relPos first for starting point
  1326 					QPointF dst=bi->getBranchObj()->getParObj()->getChildPos();		// FIXME-3 check getBO here...
  1327 			//		if (lmosel->getOrientation()==LinkableMapObj::LeftOfCenter) dst.setX (dst.x()+lmosel->width() );
  1328 					
  1329 					model->startAnimation(
  1330 						(BranchObj*)lmosel,
  1331 						lmosel->getRelPos(),
  1332 						movingObj_orgRelPos
  1333 //						QPointF (movingObj_orgPos.x() - dst.x(), movingObj_orgPos.y() - dst.y() )
  1334 					);	
  1335 				} else	
  1336 					model->reposition();
  1337 			}
  1338 		}
  1339 		model->emitSelectionChanged();  //FIXME-3 needed? at least not after pos of selection has changed...
  1340 		// Finally resize scene, if needed
  1341 		scene()->update();
  1342 		movingObj=NULL;		
  1343 
  1344 		// Just make sure, that actions are still ok,e.g. the move branch up/down buttons...
  1345 		// model->updateActions(); FIXME-3 neeeded? 
  1346 	} else 
  1347 		// maybe we moved View: set old cursor
  1348 		setCursor (Qt::ArrowCursor);
  1349     
  1350 }
  1351 
  1352 void MapEditor::mouseDoubleClickEvent(QMouseEvent* e)
  1353 {
  1354 	if (model->isSelectionBlocked() ) 
  1355 	{
  1356 		e->ignore();
  1357 		return;
  1358 	}
  1359 
  1360 	if (e->button() == Qt::LeftButton )
  1361 	{
  1362 		QPointF p = mapToScene(e->pos());
  1363 		TreeItem *ti=findMapItem (p, NULL);
  1364 		if (ti) {	// MapObj was found
  1365 			// First select the MapObj than edit heading
  1366 			model->select (ti);
  1367 			editHeading();
  1368 		}
  1369 	}
  1370 }
  1371 
  1372 void MapEditor::resizeEvent (QResizeEvent* e)
  1373 {
  1374 	QGraphicsView::resizeEvent( e );
  1375 }
  1376 
  1377 void MapEditor::dragEnterEvent(QDragEnterEvent *event)
  1378 {
  1379 	//for (unsigned int i=0;event->format(i);i++) // Debug mime type
  1380 	//	cerr << event->format(i) << endl;
  1381 
  1382 	if (event->mimeData()->hasImage())
  1383 		event->acceptProposedAction();
  1384 	else	
  1385 		if (event->mimeData()->hasUrls())
  1386 			event->acceptProposedAction();
  1387 }
  1388 
  1389 void MapEditor::dragMoveEvent(QDragMoveEvent *)
  1390 {
  1391 }
  1392 
  1393 void MapEditor::dragLeaveEvent(QDragLeaveEvent *event)
  1394 {
  1395 	event->accept();
  1396 }
  1397 
  1398 void MapEditor::dropEvent(QDropEvent *event)
  1399 {
  1400 	BranchItem *selbi=model->getSelectedBranch();
  1401 	if (selbi)
  1402 	{
  1403 		if (debug)
  1404 			foreach (QString format,event->mimeData()->formats()) 
  1405 				cout << "MapEditor: Dropped format: "<<qPrintable (format)<<endl;
  1406 
  1407 
  1408 		QList <QUrl> uris;
  1409 		if (event->mimeData()->hasImage()) 
  1410 		{
  1411 			 QVariant imageData = event->mimeData()->imageData();
  1412 			 model->addFloatImage (qvariant_cast<QPixmap>(imageData));
  1413 		} else
  1414 		if (event->mimeData()->hasUrls())
  1415 			uris=event->mimeData()->urls();
  1416 
  1417 		if (uris.count()>0)
  1418 		{
  1419 			QStringList files;
  1420 			QString s;
  1421 			QString heading;
  1422 			BranchItem *bi;
  1423 			for (int i=0; i<uris.count();i++)
  1424 			{
  1425 				// Workaround to avoid adding empty branches
  1426 				if (!uris.at(i).toString().isEmpty())
  1427 				{
  1428 					bi=model->addNewBranch();
  1429 					if (bi)
  1430 					{
  1431 						   /* FIXME-2 
  1432 						s=uris.at(i).toLocalFile();
  1433 						if (!s.isEmpty()) 
  1434 						{
  1435 						   QString file = QDir::fromNativeSeparators(s);
  1436 						   heading = QFileInfo(file).baseName();
  1437 						   files.append(file);
  1438 						   if (file.endsWith(".vym", false))
  1439 							   bi->setVymLink(file);
  1440 						   else
  1441 							   bi->setURL(uris.at(i).toString());
  1442 					   } else 
  1443 					   {
  1444 						   bo->setURL(uris.at(i).toString());
  1445 					   }
  1446 							 */  
  1447 
  1448 					   if (!heading.isEmpty())
  1449 						   bi->setHeading(heading);
  1450 					   else
  1451 						   bi->setHeading(uris.at(i).toString());
  1452 						   
  1453 					}
  1454 				}
  1455 			}
  1456 			model->reposition();
  1457 		}
  1458 	}	
  1459 	event->acceptProposedAction();
  1460 }
  1461 
  1462 void MapEditor::updateSelection(QItemSelection newsel,QItemSelection oldsel)
  1463 {
  1464 	// Note: Here we are prepared for multiple selections, though this 
  1465 	// is not yet implemented elsewhere
  1466 
  1467 	// Here in MapEditor we can only select Branches and Images
  1468 	QList <TreeItem*> treeItemsNew;
  1469 	QList <TreeItem*> treeItemsOld;
  1470 
  1471 	QModelIndex ix;
  1472 	foreach (ix,newsel.indexes() )
  1473 	{
  1474 		TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
  1475 		if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image )
  1476 			if (!treeItemsNew.contains(ti)) treeItemsNew.append (ti);
  1477 	}
  1478 	foreach (ix,oldsel.indexes() )
  1479 	{
  1480 		TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
  1481 		if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image )
  1482 			if (!treeItemsOld.contains(ti)) treeItemsOld.append (ti);
  1483 	}
  1484 
  1485 	// Trim list of selection rectangles 
  1486 	while (treeItemsNew.count() < selboxList.count() )
  1487 		delete selboxList.takeFirst();
  1488 
  1489 	// Take care to tmp scroll/unscroll
  1490 	if (!oldsel.isEmpty())
  1491 	{
  1492 		QModelIndex ix=oldsel.indexes().first(); 
  1493 		if (ix.isValid() )
  1494 		{
  1495 			TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
  1496 			if (ti)
  1497 			{
  1498 				if (ti->isBranchLikeType() )
  1499 				{
  1500 					// reset tmp scrolled branches
  1501 					BranchItem *bi=(BranchItem*)ti;
  1502 					bi->resetTmpUnscroll();
  1503 				}
  1504 				if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image)
  1505 					// Hide link if not needed
  1506 					((MapItem*)ti)->getLMO()->updateVisibility();
  1507 			}
  1508 		}
  1509 	}
  1510 
  1511 	if (!treeItemsNew.isEmpty())
  1512 	{
  1513 		QModelIndex ix=newsel.indexes().first(); 
  1514 		if (ix.isValid() )
  1515 		{
  1516 			// Temporary unscroll if necessary
  1517 			TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
  1518 			if (ti->isBranchLikeType() )
  1519 			{
  1520 				BranchItem *bi=(BranchItem*)ti;
  1521 				if (bi->hasScrolledParent(bi) )
  1522 					bi->tmpUnscroll();
  1523 			}
  1524 			scrollTo (ix);
  1525 			if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image)
  1526 				// Show link if needed
  1527 				((MapItem*)ti)->getLMO()->updateVisibility();
  1528 		}
  1529 	}
  1530 
  1531 	// Reduce rectangles
  1532 	while (treeItemsNew.count() < selboxList.count() )
  1533 		delete selboxList.takeFirst();
  1534 
  1535 	// Add additonal rectangles
  1536 	QGraphicsRectItem *sb;
  1537 	while (treeItemsNew.count() > selboxList.count() )
  1538 	{
  1539 		sb = mapScene->addRect(
  1540 			QRectF(0,0,0,0), 
  1541 			QPen(selectionColor),
  1542 			selectionColor);
  1543 		sb->setZValue(Z_SELBOX);
  1544 		sb->show();
  1545 		selboxList.append (sb);
  1546 	}
  1547 
  1548 
  1549 
  1550 	// Reposition rectangles
  1551 	QRectF bbox;
  1552 	QModelIndex index;
  1553 
  1554 	LinkableMapObj *lmo;
  1555 	for (int i=0; i<treeItemsNew.count();++i)
  1556 	{
  1557 		lmo=((MapItem*)treeItemsNew.at(i) )->getLMO();
  1558 		bbox=lmo->getBBox();
  1559 		sb=selboxList.at(i);
  1560 		sb->setRect (
  1561 			bbox.x(),bbox.y(), 
  1562 			bbox.width(), bbox.height());
  1563 		sb->setPen (selectionColor);	
  1564 		sb->setBrush (selectionColor);	
  1565 		i++;
  1566 	}
  1567 
  1568 	scene()->update();
  1569 }
  1570 
  1571 void MapEditor::updateData (const QModelIndex &sel)
  1572 {
  1573 	TreeItem *ti= static_cast<TreeItem*>(sel.internalPointer());
  1574 
  1575 /* testing
  1576 	cout << "ME::updateData\n";
  1577 
  1578 	cout << "  ti="<<ti<<endl;
  1579 	cout << "  h="<<ti->getHeading().toStdString()<<endl;
  1580 	*/
  1581 	
  1582 	if (ti->isBranchLikeType())
  1583 	{
  1584 	//	cout << "  ->updating...\n";
  1585 		BranchObj *bo=(BranchObj*) ( ((MapItem*)ti)->getLMO());
  1586 		bo->updateData();
  1587 	}
  1588 }
  1589 
  1590 void MapEditor::setSelectionColor (QColor col)
  1591 {
  1592 	selectionColor=col;
  1593 	QItemSelection sel=model->getSelectionModel()->selection();
  1594 	updateSelection(sel,sel);
  1595 }
  1596 
  1597 
  1598 QColor MapEditor::getSelectionColor ()
  1599 {
  1600 	return selectionColor;
  1601 }
  1602 
  1603