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