mapeditor.cpp
author insilmaril
Wed, 03 Jun 2009 20:37:17 +0000
changeset 775 6e4b586aa88a
parent 773 340bc29da9a0
child 776 25e634a7e1dc
permissions -rw-r--r--
Unscrolling temporary works again
     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=(static_cast<TreeItem*>(index.internalPointer()))->getLMO();
   190 		if (lmo) setScrollBarPosTarget (lmo->getBBox() );
   191 	}
   192 }
   193 
   194 void MapEditor::setScrollBarPosTarget (const QRectF &rect)
   195 {
   196 	// Code copied from Qt sources
   197 	int xmargin=50;
   198 	int ymargin=50;
   199 
   200     qreal width = viewport()->width();
   201     qreal height = viewport()->height();
   202     QRectF viewRect = matrix().mapRect(rect);
   203 
   204     qreal left = horizontalScrollBar()->value();
   205     qreal right = left + width;
   206     qreal top = verticalScrollBar()->value();
   207     qreal bottom = top + height;
   208 
   209     if (viewRect.left() <= left + xmargin) {
   210         // need to scroll from the left
   211   //      if (!d->leftIndent)
   212             scrollBarPosTarget.setX(int(viewRect.left() - xmargin - 0.5));
   213     }
   214     if (viewRect.right() >= right - xmargin) {
   215         // need to scroll from the right
   216 //        if (!d->leftIndent)
   217             scrollBarPosTarget.setX(int(viewRect.right() - width + xmargin + 0.5));
   218     }
   219     if (viewRect.top() <= top + ymargin) {
   220         // need to scroll from the top
   221    //     if (!d->topIndent)
   222             scrollBarPosTarget.setY(int(viewRect.top() - ymargin - 0.5));
   223     }
   224     if (viewRect.bottom() >= bottom - ymargin) {
   225         // need to scroll from the bottom
   226 //        if (!d->topIndent)
   227             scrollBarPosTarget.setY(int(viewRect.bottom() - height + ymargin + 0.5));
   228     }
   229 
   230 	if (scrollBarPosAnimation.state()==QtAbstractAnimation::Running)
   231 		scrollBarPosAnimation.stop();
   232 	scrollBarPosAnimation.setTargetObject (this);
   233 	scrollBarPosAnimation.setPropertyName ("scrollBarPos");
   234 	scrollBarPosAnimation.setDuration(1000);
   235 	scrollBarPosAnimation.setEasingCurve ( QtEasingCurve::OutQuint);
   236 	scrollBarPosAnimation.setStartValue(
   237 		QPointF (horizontalScrollBar()->value() ,
   238 		         verticalScrollBar()->value() ) );
   239 	scrollBarPosAnimation.setEndValue(scrollBarPosTarget);
   240 	scrollBarPosAnimation.start();
   241 }
   242 
   243 QPointF MapEditor::getScrollBarPosTarget()
   244 {
   245     return scrollBarPosTarget;
   246 }
   247 
   248 
   249 void MapEditor::setScrollBarPos(const QPointF &p)
   250 {
   251     scrollBarPos=p;
   252 	horizontalScrollBar()->setValue(int(p.x()));
   253 	verticalScrollBar()->setValue(int(p.y()));
   254 }
   255 
   256 QPointF MapEditor::getScrollBarPos()
   257 {
   258     return scrollBarPos;
   259 }
   260 
   261 void MapEditor::setZoomFactorTarget (const qreal &zft)
   262 {
   263 	zoomFactorTarget=zft;
   264 	if (zoomAnimation.state()==QtAbstractAnimation::Running)
   265 		zoomAnimation.stop();
   266 	//zoomAnimation=QtPropertyAnimation(this, "zoomFactor");
   267 	zoomAnimation.setTargetObject (this);
   268 	zoomAnimation.setPropertyName ("zoomFactor");
   269 	zoomAnimation.setDuration(1000);
   270 	zoomAnimation.setEasingCurve ( QtEasingCurve::OutQuint);
   271 	zoomAnimation.setStartValue(zoomFactor);
   272 	zoomAnimation.setEndValue(zft);
   273 	zoomAnimation.start();
   274 }
   275 
   276 qreal MapEditor::getZoomFactorTarget()
   277 {
   278     return zoomFactorTarget;
   279 }
   280 
   281 
   282 void MapEditor::setZoomFactor(const qreal &zf)
   283 {
   284     zoomFactor=zf;
   285 	setMatrix (QMatrix(zf, 0, 0, zf, 0, 0),false );
   286 }
   287 
   288 qreal MapEditor::getZoomFactor()
   289 {
   290     return zoomFactor;
   291 }
   292 
   293 void MapEditor::print()
   294 {
   295 	if ( !printer ) 
   296 	{
   297 		printer = new QPrinter;
   298 		printer->setColorMode (QPrinter::Color);
   299 		printer->setPrinterName (settings.value("/mainwindow/printerName",printer->printerName()).toString());
   300 		printer->setOutputFormat((QPrinter::OutputFormat)settings.value("/mainwindow/printerFormat",printer->outputFormat()).toInt());
   301 		printer->setOutputFileName(settings.value("/mainwindow/printerFileName",printer->outputFileName()).toString());
   302 	}
   303 
   304 	QRectF totalBBox=model->getTotalBBox();
   305 
   306 	// Try to set orientation automagically
   307 	// Note: Interpretation of generated postscript is amibiguous, if 
   308 	// there are problems with landscape mode, see
   309 	// http://sdb.suse.de/de/sdb/html/jsmeix_print-cups-landscape-81.html
   310 
   311 	if (totalBBox.width()>totalBBox.height())
   312 		// recommend landscape
   313 		printer->setOrientation (QPrinter::Landscape);
   314 	else	
   315 		// recommend portrait
   316 		printer->setOrientation (QPrinter::Portrait);
   317 
   318 	if ( printer->setup(this) ) 
   319 	// returns false, if printing is canceled
   320 	{
   321 		QPainter pp(printer);
   322 
   323 		pp.setRenderHint(QPainter::Antialiasing,true);
   324 
   325 		// Don't print the visualisation of selection
   326 		model->unselect();
   327 
   328 		QRectF mapRect=totalBBox;
   329 		QGraphicsRectItem *frame=NULL;
   330 
   331 		if (printFrame) 
   332 		{
   333 			// Print frame around map
   334 			mapRect.setRect (totalBBox.x()-10, totalBBox.y()-10, 
   335 				totalBBox.width()+20, totalBBox.height()+20);
   336 			frame=mapScene->addRect (mapRect, QPen(Qt::black),QBrush(Qt::NoBrush));
   337 			frame->setZValue(0);
   338 			frame->show();    
   339 		}		
   340 
   341 
   342 		double paperAspect = (double)printer->width()   / (double)printer->height();
   343 		double   mapAspect = (double)mapRect.width() / (double)mapRect.height();
   344 		int viewBottom;
   345 		if (mapAspect>=paperAspect)
   346 		{
   347 			// Fit horizontally to paper width
   348 			//pp.setViewport(0,0, printer->width(),(int)(printer->width()/mapAspect) );	
   349 			viewBottom=(int)(printer->width()/mapAspect);	
   350 		}	else
   351 		{
   352 			// Fit vertically to paper height
   353 			//pp.setViewport(0,0,(int)(printer->height()*mapAspect),printer->height());	
   354 			viewBottom=printer->height();	
   355 		}	
   356 		
   357 		if (printFooter) 
   358 		{
   359 			// Print footer below map
   360 			QFont font;		
   361 			font.setPointSize(10);
   362 			pp.setFont (font);
   363 			QRectF footerBox(0,viewBottom,printer->width(),15);
   364 			// FIXME-3 fileName not any longer available here: pp.drawText ( footerBox,Qt::AlignLeft,"VYM - " +fileName);
   365 			pp.drawText ( footerBox, Qt::AlignRight, QDate::currentDate().toString(Qt::TextDate));
   366 		}
   367 		mapScene->render (
   368 			&pp, 
   369 			QRectF (0,0,printer->width(),printer->height()-15),
   370 			QRectF(mapRect.x(),mapRect.y(),mapRect.width(),mapRect.height())
   371 		);
   372 		
   373 		// Viewport has paper dimension
   374 		if (frame)  delete (frame);
   375 
   376 		// Restore selection
   377 		model->reselect();
   378 
   379 		// Save settings in vymrc
   380 		settings.writeEntry("/mainwindow/printerName",printer->printerName());
   381 		settings.writeEntry("/mainwindow/printerFormat",printer->outputFormat());
   382 		settings.writeEntry("/mainwindow/printerFileName",printer->outputFileName());
   383 	}
   384 }
   385 
   386 void MapEditor::setAntiAlias (bool b)
   387 {
   388 	setRenderHint(QPainter::Antialiasing,b);
   389 }
   390 
   391 void MapEditor::setSmoothPixmap(bool b)
   392 {
   393 	setRenderHint(QPainter::SmoothPixmapTransform,b);
   394 }
   395 
   396 TreeItem* MapEditor::findMapItem (QPointF p,TreeItem *exclude)
   397 {
   398 	// Start with mapcenter, no images allowed at rootItem
   399 	int i=0;
   400 	BranchItem *bi=model->getRootItem()->getFirstBranch();
   401 	TreeItem *found=NULL;
   402 	while (bi)
   403 	{
   404 		found=bi->findMapItem (p, exclude);
   405 		if (found) return found;
   406 		i++;
   407 		bi=model->getRootItem()->getBranchNum(i);
   408 	}
   409 	return NULL;
   410 }
   411 
   412 AttributeTable* MapEditor::attributeTable()
   413 {
   414 	return attrTable;
   415 }
   416 
   417 void MapEditor::testFunction1()
   418 {
   419 	
   420 	// Code copied from Qt sources
   421 	QRectF rect=model->getSelectedBranchObj()->getBBox();
   422 	int xmargin=50;
   423 	int ymargin=50;
   424 
   425     qreal width = viewport()->width();
   426     qreal height = viewport()->height();
   427     QRectF viewRect = matrix().mapRect(rect);
   428 
   429     qreal left = horizontalScrollBar()->value();
   430     qreal right = left + width;
   431     qreal top = verticalScrollBar()->value();
   432     qreal bottom = top + height;
   433 
   434     if (viewRect.left() <= left + xmargin) {
   435         // need to scroll from the left
   436   //      if (!d->leftIndent)
   437             horizontalScrollBar()->setValue(int(viewRect.left() - xmargin - 0.5));
   438     }
   439     if (viewRect.right() >= right - xmargin) {
   440         // need to scroll from the right
   441 //        if (!d->leftIndent)
   442             horizontalScrollBar()->setValue(int(viewRect.right() - width + xmargin + 0.5));
   443     }
   444     if (viewRect.top() <= top + ymargin) {
   445         // need to scroll from the top
   446    //     if (!d->topIndent)
   447             verticalScrollBar()->setValue(int(viewRect.top() - ymargin - 0.5));
   448     }
   449     if (viewRect.bottom() >= bottom - ymargin) {
   450         // need to scroll from the bottom
   451 //        if (!d->topIndent)
   452             verticalScrollBar()->setValue(int(viewRect.bottom() - height + ymargin + 0.5));
   453     }
   454 	cout << "test1:  hor="<<horizontalScrollBar()->value()<<endl;
   455 	cout << "test1:  ver="<<verticalScrollBar()->value()<<endl;
   456 }
   457 /*
   458 	 QtPropertyAnimation *animation=new QtPropertyAnimation(this, "sceneRect");
   459 	 animation->setDuration(5000);
   460 	 //animation->setEasingCurve ( QtEasingCurve::OutElastic);
   461 	 animation->setEasingCurve ( QtEasingCurve::OutQuint);
   462 	 animation->setStartValue(sceneRect() );
   463 	 animation->setEndValue(QRectF(50, 50, 1000, 1000));
   464 
   465 	 animation->start();
   466 */	 
   467 /*
   468 	QDialog *dia= new QDialog (this);
   469 	dia->setGeometry (50,50,10,10);
   470 
   471      dia->show();
   472      dia ->raise();
   473 
   474 	 QtPropertyAnimation *animation=new QtPropertyAnimation(dia, "geometry");
   475 	 animation->setDuration(1000);
   476 	 //animation->setEasingCurve ( QtEasingCurve::OutElastic);
   477 	 animation->setEasingCurve ( QtEasingCurve::OutQuint);
   478 	 animation->setStartValue(QRect(50, 50, 10, 10));
   479 	 animation->setEndValue(QRect(250, 250, 100, 100));
   480 
   481 	 animation->start();
   482  */
   483 
   484 /* TODO Hide hidden stuff temporary, maybe add this as regular function somewhere
   485 	if (hidemode==HideNone)
   486 	{
   487 		setHideTmpMode (HideExport);
   488 		mapCenter->calcBBoxSizeWithChilds();
   489 		QRectF totalBBox=mapCenter->getTotalBBox();
   490 		QRectF mapRect=totalBBox;
   491 		QCanvasRectangle *frame=NULL;
   492 
   493 		cout << "  map has =("<<totalBBox.x()<<","<<totalBBox.y()<<","<<totalBBox.width()<<","<<totalBBox.height()<<")\n";
   494 	
   495 		mapRect.setRect (totalBBox.x(), totalBBox.y(), 
   496 			totalBBox.width(), totalBBox.height());
   497 		frame=new QCanvasRectangle (mapRect,mapScene);
   498 		frame->setBrush (QColor(white));
   499 		frame->setPen (QColor(black));
   500 		frame->setZValue(0);
   501 		frame->show();    
   502 	}	
   503 	else	
   504 	{
   505 		setHideTmpMode (HideNone);
   506 	}	
   507 	cout <<"  hidemode="<<hidemode<<endl;
   508 	*/
   509 	
   510 void MapEditor::testFunction2()
   511 {
   512 
   513 /*
   514 	// Toggle hidemode
   515 	if (hidemode==HideExport)
   516 		setHideTmpMode (HideNone);
   517 	else	
   518 		setHideTmpMode (HideExport);
   519 */		
   520 }
   521 
   522 void MapEditor::cursorUp()
   523 {
   524 	model->selectUpperBranch();
   525 }
   526 
   527 void MapEditor::cursorDown()	
   528 
   529 {
   530 	model->selectLowerBranch();
   531 }
   532 
   533 void MapEditor::cursorLeft()
   534 {
   535 	model->selectLeftBranch();
   536 }
   537 
   538 void MapEditor::cursorRight()	
   539 {
   540 	model->selectRightBranch();
   541 }
   542 
   543 void MapEditor::cursorFirst()	
   544 {
   545 	model->selectFirstBranch();
   546 }
   547 
   548 void MapEditor::cursorLast()	
   549 {
   550 	model->selectLastBranch();
   551 }
   552 
   553 
   554 void MapEditor::editHeading()
   555 {
   556 	if (editingHeading)
   557 	{
   558 		editHeadingFinished();
   559 		return;
   560 	}
   561 	BranchObj *bo=model->getSelectedBranchObj();
   562 	BranchItem *bi=model->getSelectedBranchItem();
   563 	if (bo)	
   564 	{
   565 		model->setSelectionBlocked(true);
   566 
   567 		lineEdit->setText (bi->getHeading());
   568 		QPoint p = mapTo (this,bo->getAbsPos().toPoint() );
   569 		lineEdit->setGeometry(p.x(),p.y(),230,25);
   570 		lineEdit->selectAll();
   571 		lineEdit->show();
   572 		lineEdit->setFocus();
   573 		lineEdit->grabKeyboard();
   574 		editingHeading=true;
   575 	}
   576 
   577 }
   578 void MapEditor::editHeadingFinished()
   579 {
   580 	editingHeading=false;
   581 	lineEdit->releaseKeyboard();
   582 	model->setHeading (lineEdit->text() );
   583 	model->setSelectionBlocked(false);
   584 	lineEdit->hide();
   585 
   586 	// Maybe reselect previous branch 
   587 	mainWindow->editHeadingFinished (model);
   588 }
   589 
   590 
   591 void MapEditor::contextMenuEvent ( QContextMenuEvent * e )
   592 {
   593 	// Lineedits are already closed by preceding
   594 	// mouseEvent, we don't need to close here.
   595 
   596     QPointF p = mapToScene(e->pos());
   597     TreeItem *ti=findMapItem (p, NULL);
   598     LinkableMapObj* lmo=NULL;
   599 	if (ti) lmo=ti->getLMO();	//FIXME-2 get rid of lmo...
   600 	
   601     if (lmo) 
   602 	{	// MapObj was found
   603 		if (model->getSelectedLMO() != lmo)
   604 		{
   605 			// select the MapObj
   606 			model->select(lmo);
   607 		}
   608 		// Context Menu 
   609 		if (model->getSelectedBranchObj() ) 
   610 		{
   611 			// Context Menu on branch or mapcenter
   612 			//FIXME-3 model->updateActions(); needed?
   613 			branchContextMenu->popup(e->globalPos() );
   614 		} else
   615 		{
   616 			if (model->getSelectedFloatImage() )
   617 			{
   618 				// Context Menu on floatimage
   619 				// model->updateActions(); FIXME-3 needed?
   620 				floatimageContextMenu->popup(e->globalPos() );
   621 			}	
   622 		}	
   623 	} else 
   624 	{ // No MapObj found, we are on the Canvas itself
   625 		// Context Menu on scene
   626 		// model->updateActions(); FIXME-3 needed?
   627 		
   628 		// Open context menu synchronously to position new mapcenter
   629 		model->setContextPos (p);
   630 		canvasContextMenu->exec(e->globalPos() );
   631 		model->unsetContextPos ();
   632     } 
   633 	e->accept();
   634 }
   635 
   636 void MapEditor::keyPressEvent(QKeyEvent* e)
   637 {
   638 	if (e->modifiers() & Qt::ControlModifier)
   639 	{
   640 		switch (mainWindow->getModMode())
   641 		{
   642 			case Main::ModModeColor: 
   643 				setCursor (PickColorCursor);
   644 				break;
   645 			case Main::ModModeCopy: 
   646 				setCursor (CopyCursor);
   647 				break;
   648 			case Main::ModModeXLink: 
   649 				setCursor (XLinkCursor);
   650 				break;
   651 			default :
   652 				setCursor (Qt::ArrowCursor);
   653 				break;
   654 		} 
   655 	}	
   656 }
   657 
   658 void MapEditor::keyReleaseEvent(QKeyEvent* e)
   659 {
   660 	if (!(e->modifiers() & Qt::ControlModifier))
   661 		setCursor (Qt::ArrowCursor);
   662 }
   663 
   664 void MapEditor::mousePressEvent(QMouseEvent* e)
   665 {
   666 	// Ignore right clicks, these will go to context menus
   667 	if (e->button() == Qt::RightButton )
   668 	{
   669 		e->ignore();
   670 		return;
   671 	}
   672 
   673 	//Ignore clicks while editing heading
   674 	if (model->isSelectionBlocked() ) 
   675 	{
   676 		e->ignore();
   677 		return;
   678 	}
   679 
   680     QPointF p = mapToScene(e->pos());
   681     TreeItem *ti=findMapItem (p, NULL);
   682     LinkableMapObj* lmo=NULL;
   683 	if (ti) lmo=ti->getLMO();	//FIXME-3 get rid of lmo...
   684 	
   685 	e->accept();
   686 
   687 	//Take care of  system flags _or_ modifier modes
   688 	//
   689 	if (lmo && ti->isBranchLikeType() )
   690 	{
   691 		QString foname=((BranchObj*)lmo)->getSystemFlagName(p);
   692 		if (!foname.isEmpty())
   693 		{
   694 			// systemFlag clicked
   695 			model->select (lmo);	// FIXME-3 was selectInt
   696 			if (foname=="url") 
   697 			{
   698 				if (e->state() & Qt::ControlModifier)
   699 					mainWindow->editOpenURLTab();
   700 				else	
   701 					mainWindow->editOpenURL();
   702 			}	
   703 			else if (foname=="vymLink")
   704 			{
   705 				mainWindow->editOpenVymLink();
   706 				// tabWidget may change, better return now
   707 				// before segfaulting...
   708 			} else if (foname=="note")
   709 				mainWindow->windowToggleNoteEditor();
   710 			else if (foname=="hideInExport")		
   711 				model->toggleHideExport();
   712 			// FIXME-3 needed? xelection.update();	
   713 			return;	
   714 		} 
   715 	}	
   716 	// No system flag clicked, take care of modmodes (CTRL-Click)
   717 	if (e->state() & Qt::ControlModifier)
   718 	{
   719 		if (mainWindow->getModMode()==Main::ModModeColor)
   720 		{
   721 				pickingColor=true;
   722 				setCursor (PickColorCursor);
   723 				return;
   724 		} 
   725 		if (mainWindow->getModMode()==Main::ModModeXLink)
   726 		{	
   727 			BranchObj *bo_begin=NULL;
   728 			if (lmo)
   729 				bo_begin=(BranchObj*)(lmo);
   730 			else	
   731 				bo_begin=model->getSelectedBranchObj();
   732 			if (bo_begin)	
   733 			{
   734 				drawingLink=true;
   735 				linkingObj_src=bo_begin;
   736 				tmpXLink=new XLinkObj (mapScene);
   737 				tmpXLink->setBegin (bo_begin);
   738 				tmpXLink->setEnd   (p);
   739 				tmpXLink->setColor(model->getMapDefXLinkColor());
   740 				tmpXLink->setWidth(model->getMapDefXLinkWidth());
   741 				tmpXLink->updateXLink();
   742 				tmpXLink->setVisibility (true);
   743 				return;
   744 			} 
   745 		}
   746 	}	// End of modmodes
   747 
   748     if (lmo) 
   749 	{	
   750 	/*
   751 		cout << "ME::mouse pressed\n";
   752 		cout << "  lmo="<<lmo<<endl;
   753 		cout << "  h="<<((BranchObj*)lmo)->getHeading().toStdString()<<endl;
   754 	*/
   755 		// Select the clicked object
   756 
   757 		// FIXME-2 VM better let "find" return an index instead of lmo...
   758 		// Get index of clicked LMO
   759 		TreeItem *ti=lmo->getTreeItem();
   760 		/*
   761 		cout << "  lmo="<<lmo<<"    lmo(ti)="<<ti->getLMO()<<endl;
   762 		cout << "  ti ("<<ti->row()<<","<<ti->column()<<") = "<<ti<<endl;
   763 		*/
   764 		//QModelIndex ix=model->index( ti->row(), ti->column(), model->index (0,0,QModelIndex()) );
   765 		model->select (ti);
   766 
   767 		// Left Button	    Move Branches
   768 		if (e->button() == Qt::LeftButton )
   769 		{
   770 			//movingObj_start.setX( p.x() - selection->x() );// TODO replaced selection->lmo here	
   771 			//movingObj_start.setY( p.y() - selection->y() );	
   772 			movingObj_start.setX( p.x() - lmo->x() );	
   773 			movingObj_start.setY( p.y() - lmo->y() );	
   774 			movingObj_orgPos.setX (lmo->x() );
   775 			movingObj_orgPos.setY (lmo->y() );
   776 			lmo->setRelPos();
   777 			movingObj_orgRelPos=lmo->getRelPos();
   778 
   779 			// If modMode==copy, then we want to "move" the _new_ object around
   780 			// then we need the offset from p to the _old_ selection, because of tmp
   781 			if (mainWindow->getModMode()==Main::ModModeCopy &&
   782 				e->state() & Qt::ControlModifier)
   783 			{
   784 				BranchItem *bi=model->getSelectedBranchItem();
   785 				if (bi)
   786 				{
   787 					copyingObj=true;
   788 					//FIXME-2   TreeItem::addBranch (BranchItem still missing) 
   789 					//bi->addBranch (model->getSelectedBranchItem());
   790 					model->unselect();
   791 					model->select(bi->getLastBranch());
   792 					model->reposition();
   793 				}
   794 			} 
   795 
   796 			movingObj=model->getSelectedLMO();	
   797 		} else
   798 			// Middle Button    Toggle Scroll
   799 			// (On Mac OS X this won't work, but we still have 
   800 			// a button in the toolbar)
   801 			if (e->button() == Qt::MidButton )
   802 				model->toggleScroll();
   803 		// model->updateActions(); FIXME-3 needed?
   804 		// FIXME-3 needed? xelection.update();
   805 	} else 
   806 	{ // No MapObj found, we are on the scene itself
   807 		// Left Button	    move Pos of sceneView
   808 		if (e->button() == Qt::LeftButton )
   809 		{
   810 			movingObj=NULL;	// move Content not Obj
   811 			movingObj_start=e->globalPos();
   812 			movingCont_start=QPointF (
   813 				horizontalScrollBar()->value(),
   814 				verticalScrollBar()->value());
   815 			movingVec=QPointF(0,0);
   816 			setCursor(HandOpenCursor);
   817 		} 
   818     } 
   819 }
   820 
   821 void MapEditor::mouseMoveEvent(QMouseEvent* e)
   822 {
   823     QPointF p = mapToScene(e->pos());
   824 	TreeItem *seli=model->getSelectedItem();
   825 	LinkableMapObj* lmosel=NULL;		//FIXME-2 get rid of lmosel
   826 	if (seli)
   827 		lmosel=seli->getLMO();
   828 
   829     // Move the selected MapObj
   830     if ( lmosel && movingObj) 
   831     {	
   832 		// reset cursor if we are moving and don't copy
   833 		if (mainWindow->getModMode()!=Main::ModModeCopy)
   834 			setCursor (Qt::ArrowCursor);
   835 
   836 		// To avoid jumping of the sceneView, only 
   837 		// show selection, if not tmp linked
   838 		if (!lmosel->hasParObjTmp())
   839 			model->emitShowSelection();
   840 		
   841 		// Now move the selection, but add relative position 
   842 		// (movingObj_start) where selection was chosen with 
   843 		// mousepointer. (This avoids flickering resp. jumping 
   844 		// of selection back to absPos)
   845 		
   846 		// Check if we could link 
   847 		TreeItem *dsti=findMapItem (p, seli);
   848 		LinkableMapObj* dst=NULL;
   849 		if (dsti && dsti!=seli && dsti->isBranchLikeType())
   850 			dst=dsti->getLMO(); //FIXME-2 get rid of lmo...
   851 		else
   852 			dsti=NULL;
   853 		
   854 
   855 		FloatObj *fio=model->getSelectedFloatImage();
   856 		if (fio)
   857 		{
   858 			fio->move   (p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );		
   859 			fio->setRelPos();
   860 			fio->updateLink(); //no need for reposition, if we update link here
   861 			model->emitSelectionChanged();	// position has changed
   862 
   863 			// Relink float to new mapcenter or branch, if shift is pressed	
   864 			// Only relink, if selection really has a new parent
   865 			if ( (e->modifiers()==Qt::ShiftModifier) && dst && ( dst != fio->getParObj())  
   866 				)
   867 			{
   868 				// Also save the move which was done so far
   869 				QString pold=qpointfToString(movingObj_orgRelPos);
   870 				QString pnow=qpointfToString(fio->getRelPos());
   871 				model->saveState(
   872 					fio->getTreeItem(),  // FIXME-3 
   873 					"moveRel "+pold,
   874 					fio->getTreeItem(),
   875 					"moveRel "+pnow,
   876 					QString("Move %1 to relative position %2").arg(model->getObjectName(fio)).arg(pnow));
   877 				fio->getParObj()->requestReposition();
   878 				model->reposition();
   879 
   880 				model->linkFloatImageTo (model->getSelectString(dst));
   881 				//movingObj=lmosel;
   882 				//movingObj_orgRelPos=lmosel->getRelPos();	
   883 
   884 				model->reposition();
   885 			}
   886 		} else	
   887 		{	// selection != a FloatObj
   888 			if (seli->depth()==0)		//FIXME-1 also moved mapcenters could be linked, but not working here...
   889 			{
   890 				// Move MapCenter
   891 				if (e->buttons()== Qt::LeftButton && e->modifiers()==Qt::ShiftModifier) 
   892 					((BranchObj*)lmosel)->moveBy(
   893 						QPointF(p.x() -movingObj_start.x(), 
   894 						p.y()-movingObj_start.y()) );		
   895 				else	
   896 					lmosel->move   (p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );		
   897 				model->updateRelPositions();
   898 			} else
   899 			{	
   900 				if (seli->depth()==1)
   901 				{
   902 					// Move mainbranch
   903 					lmosel->move(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );		
   904 					lmosel->setRelPos();
   905 				} else
   906 				{
   907 					// Move ordinary branch
   908 					if (lmosel->getOrientation() == LinkableMapObj::LeftOfCenter)
   909 						// Add width of bbox here, otherwise alignRelTo will cause jumping around
   910 						lmosel->move(p.x() -movingObj_start.x() , //lmosel->getBBox().width(), 
   911 							p.y()-movingObj_start.y() +lmosel->getTopPad() );		
   912 					else	
   913 						lmosel->move(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() -lmosel->getTopPad());
   914 					lmosel->setRelPos();	
   915 				} 
   916 
   917 				// Maybe we can relink temporary?
   918 				if (dsti)
   919 				{
   920 					if (e->modifiers()==Qt::ControlModifier)
   921 					{
   922 						// Special case: CTRL to link below dst
   923 						lmosel->setParObjTmp (dst,p,+1);
   924 					}
   925 					else if (e->modifiers()==Qt::ShiftModifier)
   926 						lmosel->setParObjTmp (dst,p,-1);
   927 					else
   928 						lmosel->setParObjTmp (dst,p,0);
   929 				} else	
   930 				{
   931 					lmosel->unsetParObjTmp();
   932 				}		
   933 				// reposition subbranch
   934 				lmosel->reposition();	
   935 			} // depth>0
   936 
   937 			QItemSelection sel=model->getSelectionModel()->selection();
   938 			updateSelection(sel,sel);	// position has changed
   939 
   940 		} // no FloatImageObj
   941 
   942 		scene()->update();
   943 		return;
   944 	} // selection && moving_obj
   945 		
   946 	// Draw a link from one branch to another
   947 	if (drawingLink)
   948 	{
   949 		 tmpXLink->setEnd (p);
   950 		 tmpXLink->updateXLink();
   951 	}	 
   952 	
   953     // Move sceneView 
   954     if (!movingObj && !pickingColor &&!drawingLink && e->buttons() == Qt::LeftButton ) 
   955 	{
   956 		QPointF p=e->globalPos();
   957 		movingVec.setX(-p.x() + movingObj_start.x() );
   958 		movingVec.setY(-p.y() + movingObj_start.y() );
   959 		horizontalScrollBar()->setSliderPosition((int)( movingCont_start.x()+movingVec.x() ));
   960 		verticalScrollBar()->setSliderPosition((int)( movingCont_start.y()+movingVec.y() ) );
   961     }
   962 }
   963 
   964 
   965 void MapEditor::mouseReleaseEvent(QMouseEvent* e)
   966 {
   967     QPointF p = mapToScene(e->pos());
   968 	TreeItem *seli=model->getSelectedItem();
   969 
   970 	TreeItem *dsti=NULL;
   971 	if (seli) dsti=findMapItem(p, seli);
   972 	LinkableMapObj* dst=NULL;
   973 	if (dsti && dsti->isBranchLikeType ()) 
   974 		dst=dsti->getLMO();	//FIXME-2 get rid of dst...
   975 	else
   976 		dsti=NULL;
   977 
   978 
   979 	// Have we been picking color?
   980 	if (pickingColor)
   981 	{
   982 		pickingColor=false;
   983 		setCursor (Qt::ArrowCursor);
   984 		// Check if we are over another branch
   985 		if (dst) 
   986 		{	
   987 			if (e->state() & Qt::ShiftModifier)
   988 				model->colorBranch (((BranchObj*)dst)->getColor());
   989 			else	
   990 				model->colorSubtree (((BranchObj*)dst)->getColor());
   991 		} 
   992 		return;
   993 	}
   994 
   995 	// Have we been drawing a link?
   996 	if (drawingLink)	
   997 	{
   998 		drawingLink=false;
   999 		// Check if we are over another branch
  1000 		if (dsti)
  1001 		{	
  1002 			tmpXLink->setEnd ( ((BranchObj*)(dst)) );
  1003 			tmpXLink->updateXLink();
  1004 			tmpXLink->activate(); //FIXME-2 savestate missing
  1005 			//model->saveStateComplete(QString("Activate xLink from %1 to %2").arg(model->getObjectName(tmpXLink->getBegin())).arg(model->getObjectName(tmpXLink->getEnd())) );	
  1006 		} else
  1007 		{
  1008 			delete(tmpXLink);
  1009 			tmpXLink=NULL;
  1010 		}
  1011 		return;
  1012 	}
  1013 	
  1014     // Have we been moving something?
  1015     if ( seli && movingObj ) 
  1016     {	
  1017 		FloatImageObj *fo=model->getSelectedFloatImage();
  1018 		if(fo)
  1019 		{
  1020 			// Moved FloatObj. Maybe we need to reposition
  1021 		    QString pold=qpointfToString(movingObj_orgRelPos);
  1022 		    QString pnow=qpointfToString(fo->getRelPos());
  1023 			model->saveState(
  1024 				fo->getTreeItem(),	// FIXME-3
  1025 				"moveRel "+pold,
  1026 				fo->getTreeItem(),	// FIXME-3
  1027 				"moveRel "+pnow,
  1028 				QString("Move %1 to relative position %2").arg(model->getObjectName(fo)).arg(pnow));
  1029 
  1030 			fo->getParObj()->requestReposition();
  1031 			model->reposition();
  1032 		}	
  1033 
  1034 		BranchItem *bi=model->getSelectedBranchItem();
  1035 		if (bi && bi->depth()==0)
  1036 		{	
  1037             if (movingObj_orgPos != bi->getBranchObj()->getAbsPos())	// FIXME-3 check getBO here...
  1038             {
  1039                 QString pold=qpointfToString(movingObj_orgPos);
  1040                 QString pnow=qpointfToString(bi->getBranchObj()->getAbsPos());		// FIXME-3 check getBO here...
  1041 
  1042                 model->saveState(
  1043                     bi,
  1044                     "move "+pold,
  1045                     bi,
  1046                     "move "+pnow,
  1047                     QString("Move mapcenter %1 to position %2").arg(model->getObjectName(bi)).arg(pnow));
  1048             }
  1049 		}
  1050 	
  1051 		if (seli->isBranchLikeType() ) //(seli->getType() == TreeItem::Branch )
  1052 		{	// A branch was moved
  1053 			LinkableMapObj* lmosel=NULL;		//FIXME-2 get rid of lmosel
  1054 			lmosel=seli->getLMO();
  1055 				
  1056 			// save the position in case we link to mapcenter
  1057 			QPointF savePos=QPointF (lmosel->getAbsPos()  );
  1058 
  1059 			// Reset the temporary drawn link to the original one
  1060 			lmosel->unsetParObjTmp();
  1061 
  1062 			// For Redo we may need to save original selection
  1063 			QString preSelStr=model->getSelectString(seli);
  1064 
  1065 			copyingObj=false;	
  1066 			if (dsti)
  1067 			{
  1068 				// We have a destination, relink to that
  1069 
  1070 				BranchObj* bsel=model->getSelectedBranchObj();
  1071 
  1072 				QString preParStr=model->getSelectString (bsel->getParObj());
  1073 				QString preNum=QString::number (seli->num(),10);
  1074 				QString preDstParStr;
  1075 				bool relinked;
  1076 
  1077 				if (e->state() & Qt::ShiftModifier && dst->getParObj())
  1078 				{	// Link above dst
  1079 					preDstParStr=model->getSelectString (dst->getParObj());
  1080 					relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti->parent(),((BranchItem*)dsti)->num());
  1081 				} else 
  1082 				if (e->state() & Qt::ControlModifier && dst->getParObj())
  1083 				{
  1084 					// Link below dst
  1085 					preDstParStr=model->getSelectString (dst->getParObj());
  1086 					relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti->parent(),((BranchItem*)dsti)->num()+1);
  1087 				} else	
  1088 				{	// Append to dst
  1089 					preDstParStr=model->getSelectString(dst);
  1090 					relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti);
  1091 					if (dsti->depth()==0) bsel->move (savePos);
  1092 				} 
  1093 				if (relinked)
  1094 				{
  1095 					QString postSelStr=model->getSelectString(lmosel);
  1096 					QString postNum=QString::number (seli->num(),10);
  1097 
  1098 					QString undoCom="linkTo (\""+ 
  1099 						preParStr+ "\"," + preNum  +"," + 
  1100 						QString ("%1,%2").arg(movingObj_orgPos.x()).arg(movingObj_orgPos.y())+ ")";
  1101 
  1102 					QString redoCom="linkTo (\""+ 
  1103 						preDstParStr + "\"," + postNum + "," +
  1104 						QString ("%1,%2").arg(savePos.x()).arg(savePos.y())+ ")";
  1105 
  1106 					model->saveState (
  1107 						postSelStr,undoCom,
  1108 						preSelStr, redoCom,
  1109 						QString("Relink %1 to %2").arg(model->getObjectName(bsel)).arg(model->getObjectName(dst)) );
  1110 
  1111 				}
  1112 			} else
  1113 			{
  1114 				// No destination, undo  temporary move
  1115 
  1116 				if (seli->depth()==1)
  1117 				{
  1118 					// The select string might be different _after_ moving around.
  1119 					// Therefor reposition and then use string of old selection, too
  1120 					model->reposition();
  1121 
  1122                     QPointF rp(lmosel->getRelPos());
  1123                     if (rp != movingObj_orgRelPos)
  1124                     {
  1125                         QString ps=qpointfToString(rp);
  1126                         model->saveState(
  1127                             model->getSelectString(lmosel), "moveRel "+qpointfToString(movingObj_orgRelPos), 
  1128                             preSelStr, "moveRel "+ps, 
  1129                             QString("Move %1 to relative position %2").arg(model->getObjectName(lmosel)).arg(ps));
  1130                     }
  1131 				}
  1132 
  1133 				// Draw the original link, before selection was moved around
  1134 				if (settings.value("/animation/use",false).toBool() && seli->depth()>1) 
  1135 				{
  1136 					lmosel->setRelPos();	// calc relPos first for starting point
  1137 					QPointF dst=bi->getBranchObj()->getParObj()->getChildPos();		// FIXME-3 check getBO here...
  1138 			//		if (lmosel->getOrientation()==LinkableMapObj::LeftOfCenter) dst.setX (dst.x()+lmosel->width() );
  1139 					
  1140 					model->startAnimation(
  1141 						(BranchObj*)lmosel,
  1142 						lmosel->getRelPos(),
  1143 						movingObj_orgRelPos
  1144 //						QPointF (movingObj_orgPos.x() - dst.x(), movingObj_orgPos.y() - dst.y() )
  1145 					);	
  1146 				} else	
  1147 					model->reposition();
  1148 			}
  1149 		}
  1150 		model->emitSelectionChanged();  //FIXME-3 needed? at least not after pos of selection has changed...
  1151 		// Finally resize scene, if needed
  1152 		scene()->update();
  1153 		movingObj=NULL;		
  1154 
  1155 		// Just make sure, that actions are still ok,e.g. the move branch up/down buttons...
  1156 		// model->updateActions(); FIXME-3 neeeded? 
  1157 	} else 
  1158 		// maybe we moved View: set old cursor
  1159 		setCursor (Qt::ArrowCursor);
  1160     
  1161 }
  1162 
  1163 void MapEditor::mouseDoubleClickEvent(QMouseEvent* e)
  1164 {
  1165 	if (model->isSelectionBlocked() ) 
  1166 	{
  1167 		e->ignore();
  1168 		return;
  1169 	}
  1170 
  1171 	if (e->button() == Qt::LeftButton )
  1172 	{
  1173 		QPointF p = mapToScene(e->pos());
  1174 		TreeItem *ti=findMapItem (p, NULL);
  1175 		if (ti) {	// MapObj was found
  1176 			// First select the MapObj than edit heading
  1177 			model->select (ti);
  1178 			editHeading();
  1179 		}
  1180 	}
  1181 }
  1182 
  1183 void MapEditor::resizeEvent (QResizeEvent* e)
  1184 {
  1185 	QGraphicsView::resizeEvent( e );
  1186 }
  1187 
  1188 void MapEditor::dragEnterEvent(QDragEnterEvent *event)
  1189 {
  1190 	//for (unsigned int i=0;event->format(i);i++) // Debug mime type
  1191 	//	cerr << event->format(i) << endl;
  1192 
  1193 	if (event->mimeData()->hasImage())
  1194 		event->acceptProposedAction();
  1195 	else	
  1196 		if (event->mimeData()->hasUrls())
  1197 			event->acceptProposedAction();
  1198 }
  1199 
  1200 void MapEditor::dragMoveEvent(QDragMoveEvent *)
  1201 {
  1202 }
  1203 
  1204 void MapEditor::dragLeaveEvent(QDragLeaveEvent *event)
  1205 {
  1206 	event->accept();
  1207 }
  1208 
  1209 void MapEditor::dropEvent(QDropEvent *event)
  1210 {
  1211 	BranchItem *selbi=model->getSelectedBranchItem();
  1212 	if (selbi)
  1213 	{
  1214 		if (debug)
  1215 			foreach (QString format,event->mimeData()->formats()) 
  1216 				cout << "MapEditor: Dropped format: "<<qPrintable (format)<<endl;
  1217 
  1218 
  1219 		QList <QUrl> uris;
  1220 		if (event->mimeData()->hasImage()) 
  1221 		{
  1222 			 QVariant imageData = event->mimeData()->imageData();
  1223 			 model->addFloatImage (qvariant_cast<QPixmap>(imageData));
  1224 		} else
  1225 		if (event->mimeData()->hasUrls())
  1226 			uris=event->mimeData()->urls();
  1227 
  1228 		if (uris.count()>0)
  1229 		{
  1230 			QStringList files;
  1231 			QString s;
  1232 			QString heading;
  1233 			BranchItem *bi;
  1234 			for (int i=0; i<uris.count();i++)
  1235 			{
  1236 				// Workaround to avoid adding empty branches
  1237 				if (!uris.at(i).toString().isEmpty())
  1238 				{
  1239 					bi=model->addNewBranch();
  1240 					if (bi)
  1241 					{
  1242 						   /* FIXME-2 
  1243 						s=uris.at(i).toLocalFile();
  1244 						if (!s.isEmpty()) 
  1245 						{
  1246 						   QString file = QDir::fromNativeSeparators(s);
  1247 						   heading = QFileInfo(file).baseName();
  1248 						   files.append(file);
  1249 						   if (file.endsWith(".vym", false))
  1250 							   bi->setVymLink(file);
  1251 						   else
  1252 							   bi->setURL(uris.at(i).toString());
  1253 					   } else 
  1254 					   {
  1255 						   bo->setURL(uris.at(i).toString());
  1256 					   }
  1257 							 */  
  1258 
  1259 					   if (!heading.isEmpty())
  1260 						   bi->setHeading(heading);
  1261 					   else
  1262 						   bi->setHeading(uris.at(i).toString());
  1263 						   
  1264 					}
  1265 				}
  1266 			}
  1267 			model->reposition();
  1268 		}
  1269 	}	
  1270 	event->acceptProposedAction();
  1271 }
  1272 
  1273 void MapEditor::updateSelection(const QItemSelection &newsel,const QItemSelection &oldsel)
  1274 {
  1275 	// Take care to tmp scroll/unscroll
  1276 	if (!oldsel.isEmpty())
  1277 	{
  1278 		QModelIndex ix=oldsel.indexes().first(); 
  1279 		if (ix.isValid() )
  1280 		{
  1281 			TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
  1282 			if (ti && ti->isBranchLikeType() )
  1283 			{
  1284 				BranchItem *bi=(BranchItem*)ti;
  1285 				bi->resetTmpUnscroll();
  1286 			}
  1287 		}
  1288 	}
  1289 
  1290 	if (!newsel.isEmpty())
  1291 	{
  1292 		QModelIndex ix=newsel.indexes().first(); 
  1293 		if (ix.isValid() )
  1294 		{
  1295 			// Temporary unscroll if necessary
  1296 			TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
  1297 			if (ti->isBranchLikeType() )
  1298 			{
  1299 				BranchItem *bi=(BranchItem*)ti;
  1300 				if (bi->hasScrolledParent(bi) )
  1301 					bi->tmpUnscroll();
  1302 			}
  1303 			scrollTo (ix);
  1304 		}
  1305 	}
  1306 
  1307 	// Reduce rectangles
  1308 	while (newsel.indexes().count() < selboxList.count() )
  1309 		delete selboxList.takeFirst();
  1310 
  1311 	// Add additonal rectangles
  1312 	QGraphicsRectItem *sb;
  1313 	while (newsel.indexes().count() > selboxList.count() )
  1314 	{
  1315 		sb = mapScene->addRect(
  1316 			QRectF(0,0,0,0), 
  1317 			QPen(selectionColor),
  1318 			selectionColor);
  1319 		sb->setZValue(Z_SELBOX);
  1320 		sb->show();
  1321 		selboxList.append (sb);
  1322 	}
  1323 
  1324 	// Reposition rectangles
  1325 	int i=0;
  1326 	QRectF bbox;
  1327 	QModelIndex index;
  1328 
  1329 	TreeItem *ti;
  1330 	LinkableMapObj *lmo;
  1331 	foreach (sb,selboxList)
  1332 	{
  1333 		index=newsel.indexes().at(i);
  1334 		ti= static_cast<TreeItem*>(index.internalPointer());
  1335 		lmo=ti->getLMO();
  1336 		bbox=lmo->getBBox();
  1337 		sb->setRect (
  1338 			bbox.x(),bbox.y(), 
  1339 			bbox.width(), bbox.height());
  1340 		sb->setPen (selectionColor);	
  1341 		sb->setBrush (selectionColor);	
  1342 		i++;
  1343 	}
  1344 
  1345 	scene()->update();
  1346 }
  1347 
  1348 void MapEditor::updateData (const QModelIndex &sel)
  1349 {
  1350 	TreeItem *ti= static_cast<TreeItem*>(sel.internalPointer());
  1351 
  1352 /* testing
  1353 	cout << "ME::updateData\n";
  1354 
  1355 	cout << "  ti="<<ti<<endl;
  1356 	cout << "  h="<<ti->getHeading().toStdString()<<endl;
  1357 	*/
  1358 	
  1359 	if (ti->isBranchLikeType())
  1360 	{
  1361 		BranchObj *bo=(BranchObj*)ti->getLMO();
  1362 		bo->updateData();
  1363 	}
  1364 
  1365 }
  1366 
  1367 void MapEditor::setSelectionColor (QColor col)
  1368 {
  1369 	selectionColor=col;
  1370 	QItemSelection sel=model->getSelectionModel()->selection();
  1371 	updateSelection(sel,sel);
  1372 }
  1373 
  1374 
  1375 QColor MapEditor::getSelectionColor ()
  1376 {
  1377 	return selectionColor;
  1378 }
  1379 
  1380