mapeditor.cpp
author insilmaril
Mon, 27 Jul 2009 12:53:17 +0000
changeset 784 9db215a4ad53
parent 782 ffb02a9bb508
child 785 5987f9f15bac
permissions -rw-r--r--
Playing with dbus, fixed name of TW translation file
     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 void MapEditor::cursorUp()
   559 {
   560 	model->selectUpperBranch();
   561 }
   562 
   563 void MapEditor::cursorDown()	
   564 
   565 {
   566 	model->selectLowerBranch();
   567 }
   568 
   569 void MapEditor::cursorLeft()
   570 {
   571 	model->selectLeftBranch();
   572 }
   573 
   574 void MapEditor::cursorRight()	
   575 {
   576 	model->selectRightBranch();
   577 }
   578 
   579 void MapEditor::cursorFirst()	
   580 {
   581 	model->selectFirstBranch();
   582 }
   583 
   584 void MapEditor::cursorLast()	
   585 {
   586 	model->selectLastBranch();
   587 }
   588 
   589 
   590 void MapEditor::editHeading()
   591 {
   592 	if (editingHeading)
   593 	{
   594 		editHeadingFinished();
   595 		return;
   596 	}
   597 	BranchObj *bo=model->getSelectedBranchObj();
   598 	BranchItem *bi=model->getSelectedBranchItem();
   599 	if (bo)	
   600 	{
   601 		model->setSelectionBlocked(true);
   602 
   603 		lineEdit->setText (bi->getHeading());
   604 		QPoint p = mapTo (this,bo->getAbsPos().toPoint() );
   605 		lineEdit->setGeometry(p.x(),p.y(),230,25);
   606 		lineEdit->selectAll();
   607 		lineEdit->show();
   608 		lineEdit->setFocus();
   609 		lineEdit->grabKeyboard();
   610 		editingHeading=true;
   611 	}
   612 
   613 }
   614 void MapEditor::editHeadingFinished()
   615 {
   616 	editingHeading=false;
   617 	lineEdit->releaseKeyboard();
   618 	model->setHeading (lineEdit->text() );
   619 	model->setSelectionBlocked(false);
   620 	lineEdit->hide();
   621 
   622 	// Maybe reselect previous branch 
   623 	mainWindow->editHeadingFinished (model);
   624 }
   625 
   626 
   627 void MapEditor::contextMenuEvent ( QContextMenuEvent * e )
   628 {
   629 	// Lineedits are already closed by preceding
   630 	// mouseEvent, we don't need to close here.
   631 
   632     QPointF p = mapToScene(e->pos());
   633     TreeItem *ti=findMapItem (p, NULL);
   634     LinkableMapObj* lmo=NULL;
   635 	if (ti) lmo=((MapItem*)ti)->getLMO();
   636 	
   637     if (lmo) 
   638 	{	// MapObj was found
   639 		if (model->getSelectedLMO() != lmo)
   640 		{
   641 			// select the MapObj
   642 			model->select(lmo);
   643 		}
   644 		// Context Menu 
   645 		if (model->getSelectedBranchObj() ) 
   646 		{
   647 			// Context Menu on branch or mapcenter
   648 			//FIXME-3 model->updateActions(); needed?
   649 			branchContextMenu->popup(e->globalPos() );
   650 		} else
   651 		{
   652 			if (model->getSelectedImageItem() )
   653 			{
   654 				// Context Menu on floatimage
   655 				// model->updateActions(); FIXME-3 needed?
   656 				floatimageContextMenu->popup(e->globalPos() );
   657 			}	
   658 		}	
   659 	} else 
   660 	{ // No MapObj found, we are on the Canvas itself
   661 		// Context Menu on scene
   662 		// model->updateActions(); FIXME-3 needed?
   663 		
   664 		// Open context menu synchronously to position new mapcenter
   665 		model->setContextPos (p);
   666 		canvasContextMenu->exec(e->globalPos() );
   667 		model->unsetContextPos ();
   668     } 
   669 	e->accept();
   670 }
   671 
   672 void MapEditor::keyPressEvent(QKeyEvent* e)
   673 {
   674 	if (e->modifiers() & Qt::ControlModifier)
   675 	{
   676 		switch (mainWindow->getModMode())
   677 		{
   678 			case Main::ModModeColor: 
   679 				setCursor (PickColorCursor);
   680 				break;
   681 			case Main::ModModeCopy: 
   682 				setCursor (CopyCursor);
   683 				break;
   684 			case Main::ModModeXLink: 
   685 				setCursor (XLinkCursor);
   686 				break;
   687 			default :
   688 				setCursor (Qt::ArrowCursor);
   689 				break;
   690 		} 
   691 	}	
   692 }
   693 
   694 void MapEditor::keyReleaseEvent(QKeyEvent* e)
   695 {
   696 	if (!(e->modifiers() & Qt::ControlModifier))
   697 		setCursor (Qt::ArrowCursor);
   698 }
   699 
   700 void MapEditor::mousePressEvent(QMouseEvent* e)
   701 {
   702 	// Ignore right clicks, these will go to context menus
   703 	if (e->button() == Qt::RightButton )
   704 	{
   705 		e->ignore();
   706 		return;
   707 	}
   708 
   709 	//Ignore clicks while editing heading
   710 	if (model->isSelectionBlocked() ) 
   711 	{
   712 		e->ignore();
   713 		return;
   714 	}
   715 
   716     QPointF p = mapToScene(e->pos());
   717     TreeItem *ti=findMapItem (p, NULL);
   718     LinkableMapObj* lmo=NULL;
   719 	if (ti) lmo=((MapItem*)ti)->getLMO();
   720 	
   721 	e->accept();
   722 
   723 	//Take care of  system flags _or_ modifier modes
   724 	//
   725 	if (lmo && ti->isBranchLikeType() )
   726 	{
   727 		QString foname=((BranchObj*)lmo)->getSystemFlagName(p);
   728 		if (!foname.isEmpty())
   729 		{
   730 			// systemFlag clicked
   731 			model->select (lmo);	// FIXME-3 was selectInt
   732 			if (foname=="url") 
   733 			{
   734 				if (e->state() & Qt::ControlModifier)
   735 					mainWindow->editOpenURLTab();
   736 				else	
   737 					mainWindow->editOpenURL();
   738 			}	
   739 			else if (foname=="system-vymLink")
   740 			{
   741 				mainWindow->editOpenVymLink();
   742 				// tabWidget may change, better return now
   743 				// before segfaulting...
   744 			} else if (foname=="note")
   745 				mainWindow->windowToggleNoteEditor();
   746 			else if (foname=="hideInExport")		
   747 				model->toggleHideExport();
   748 			// FIXME-3 needed? xelection.update();	
   749 			return;	
   750 		} 
   751 	}	
   752 	// No system flag clicked, take care of modmodes (CTRL-Click)
   753 	if (e->state() & Qt::ControlModifier)
   754 	{
   755 		if (mainWindow->getModMode()==Main::ModModeColor)
   756 		{
   757 				pickingColor=true;
   758 				setCursor (PickColorCursor);
   759 				return;
   760 		} 
   761 		if (mainWindow->getModMode()==Main::ModModeXLink)
   762 		{	
   763 			BranchObj *bo_begin=NULL;
   764 			if (lmo)
   765 				bo_begin=(BranchObj*)(lmo);
   766 			else	
   767 				bo_begin=model->getSelectedBranchObj();
   768 			if (bo_begin)	
   769 			{
   770 				drawingLink=true;
   771 				linkingObj_src=bo_begin;
   772 				tmpXLink=new XLinkObj (mapScene);
   773 				tmpXLink->setBegin (bo_begin);
   774 				tmpXLink->setEnd   (p);
   775 				tmpXLink->setColor(model->getMapDefXLinkColor());
   776 				tmpXLink->setWidth(model->getMapDefXLinkWidth());
   777 				tmpXLink->updateXLink();
   778 				tmpXLink->setVisibility (true);
   779 				return;
   780 			} 
   781 		}
   782 	}	// End of modmodes
   783 
   784     if (lmo) 
   785 	{	
   786 	/*
   787 		cout << "ME::mouse pressed\n";
   788 		cout << "  lmo="<<lmo<<endl;
   789 		cout << "   ti="<<ti->getHeadingStd()<<endl;
   790 	*/
   791 		// Select the clicked object
   792 
   793 		// Get clicked LMO
   794 		model->select (ti);
   795 
   796 		// Left Button	    Move Branches
   797 		if (e->button() == Qt::LeftButton )
   798 		{
   799 			movingObj_start.setX( p.x() - lmo->x() );	
   800 			movingObj_start.setY( p.y() - lmo->y() );	
   801 			movingObj_orgPos.setX (lmo->x() );
   802 			movingObj_orgPos.setY (lmo->y() );
   803 			lmo->setRelPos();
   804 			movingObj_orgRelPos=lmo->getRelPos();
   805 
   806 			// If modMode==copy, then we want to "move" the _new_ object around
   807 			// then we need the offset from p to the _old_ selection, because of tmp
   808 			if (mainWindow->getModMode()==Main::ModModeCopy &&
   809 				e->state() & Qt::ControlModifier)
   810 			{
   811 				BranchItem *bi=model->getSelectedBranchItem();
   812 				if (bi)
   813 				{
   814 					copyingObj=true;
   815 					//FIXME-2   TreeItem::addBranch (BranchItem still missing) 
   816 					//bi->addBranch (model->getSelectedBranchItem());
   817 					model->unselect();
   818 					model->select(bi->getLastBranch());
   819 					model->reposition();
   820 				}
   821 			} 
   822 
   823 			movingObj=model->getSelectedLMO();	
   824 		} else
   825 			// Middle Button    Toggle Scroll
   826 			// (On Mac OS X this won't work, but we still have 
   827 			// a button in the toolbar)
   828 			if (e->button() == Qt::MidButton )
   829 				model->toggleScroll();
   830 		// model->updateActions(); FIXME-3 needed?
   831 		// FIXME-3 needed? xelection.update();
   832 	} else 
   833 	{ // No MapObj found, we are on the scene itself
   834 		// Left Button	    move Pos of sceneView
   835 		if (e->button() == Qt::LeftButton )
   836 		{
   837 			movingObj=NULL;	// move Content not Obj
   838 			movingObj_start=e->globalPos();
   839 			movingCont_start=QPointF (
   840 				horizontalScrollBar()->value(),
   841 				verticalScrollBar()->value());
   842 			movingVec=QPointF(0,0);
   843 			setCursor(HandOpenCursor);
   844 		} 
   845     } 
   846 }
   847 
   848 void MapEditor::mouseMoveEvent(QMouseEvent* e)
   849 {
   850     QPointF p = mapToScene(e->pos());
   851 	TreeItem *seli=model->getSelectedItem();
   852 	LinkableMapObj* lmosel=NULL;	
   853 	if (seli && (seli->isBranchLikeType() ||seli->getType()==TreeItem::Image))
   854 		lmosel=((MapItem*)seli)->getLMO();
   855 
   856     // Move the selected MapObj
   857     if ( lmosel && movingObj) 
   858     {	
   859 		// reset cursor if we are moving and don't copy
   860 		if (mainWindow->getModMode()!=Main::ModModeCopy)
   861 			setCursor (Qt::ArrowCursor);
   862 
   863 		// To avoid jumping of the sceneView, only 
   864 		// show selection, if not tmp linked
   865 		if (!lmosel->hasParObjTmp())
   866 			model->emitShowSelection();
   867 		
   868 		// Now move the selection, but add relative position 
   869 		// (movingObj_start) where selection was chosen with 
   870 		// mousepointer. (This avoids flickering resp. jumping 
   871 		// of selection back to absPos)
   872 		
   873 		// Check if we could link 
   874 		TreeItem *ti=findMapItem (p, seli);
   875 		BranchItem *dsti=NULL;
   876 		LinkableMapObj* dst=NULL;
   877 		if (ti && ti!=seli && ti->isBranchLikeType())
   878 		{
   879 			dsti=(BranchItem*)ti;
   880 			dst=dsti->getLMO(); 
   881 		} else
   882 			dsti=NULL;
   883 		
   884 
   885 		if (lmosel && seli->getType()==TreeItem::Image)	
   886 		{
   887 			FloatObj *fio=(FloatImageObj*)lmosel;
   888 			fio->move   (p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );		
   889 			fio->setRelPos();
   890 			fio->updateLinkGeometry(); //no need for reposition, if we update link here
   891 			model->emitSelectionChanged();	// position has changed
   892 
   893 			// Relink float to new mapcenter or branch, if shift is pressed	
   894 			// Only relink, if selection really has a new parent
   895 			if ( e->modifiers()==Qt::ShiftModifier && dsti &&  dsti != seli->parent()  )
   896 			{
   897 				// Also save the move which was done so far
   898 				QString pold=qpointfToString(movingObj_orgRelPos);
   899 				QString pnow=qpointfToString(fio->getRelPos());
   900 				model->saveState(
   901 					seli,
   902 					"moveRel "+pold,
   903 					seli,
   904 					"moveRel "+pnow,
   905 					QString("Move %1 to relative position %2").arg(model->getObjectName(fio)).arg(pnow));
   906 				fio->getParObj()->requestReposition();
   907 				model->reposition();
   908 
   909 				model->relinkImage ((ImageItem*) seli,dsti);
   910 				model->select (seli);
   911 				//movingObj=lmosel;	//FIXME-3
   912 				//movingObj_orgRelPos=lmosel->getRelPos();	
   913 
   914 				model->reposition();
   915 			}
   916 		} else	
   917 		{	// selection != a FloatObj
   918 			if (seli->depth()==0)		//FIXME-1 also moved mapcenters could be linked, but not working here...
   919 			{
   920 				// Move MapCenter
   921 				if (e->buttons()== Qt::LeftButton && e->modifiers()==Qt::ShiftModifier) 
   922 					((BranchObj*)lmosel)->moveBy(
   923 						QPointF(p.x() -movingObj_start.x(), 
   924 						p.y()-movingObj_start.y()) );		
   925 				else	
   926 					lmosel->move   (p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );		
   927 				model->updateRelPositions();
   928 			} else
   929 			{	
   930 				if (seli->depth()==1)
   931 				{
   932 					// Move mainbranch
   933 					lmosel->move(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );		
   934 					lmosel->setRelPos();
   935 				} else
   936 				{
   937 					// Move ordinary branch
   938 					if (lmosel->getOrientation() == LinkableMapObj::LeftOfCenter)
   939 						// Add width of bbox here, otherwise alignRelTo will cause jumping around
   940 						lmosel->move(p.x() -movingObj_start.x() , //lmosel->getBBox().width(), 
   941 							p.y()-movingObj_start.y() +lmosel->getTopPad() );		
   942 					else	
   943 						lmosel->move(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() -lmosel->getTopPad());
   944 					lmosel->setRelPos();	
   945 				} 
   946 
   947 				// Maybe we can relink temporary?
   948 				if (dsti)
   949 				{
   950 					if (e->modifiers()==Qt::ControlModifier)
   951 					{
   952 						// Special case: CTRL to link below dst
   953 						lmosel->setParObjTmp (dst,p,+1);
   954 					}
   955 					else if (e->modifiers()==Qt::ShiftModifier)
   956 						lmosel->setParObjTmp (dst,p,-1);
   957 					else
   958 						lmosel->setParObjTmp (dst,p,0);
   959 				} else	
   960 				{
   961 					lmosel->unsetParObjTmp();
   962 				}		
   963 				// reposition subbranch
   964 				lmosel->reposition();	
   965 			} // depth>0
   966 
   967 			QItemSelection sel=model->getSelectionModel()->selection();
   968 			updateSelection(sel,sel);	// position has changed
   969 
   970 		} // no FloatImageObj
   971 
   972 		scene()->update();
   973 		return;
   974 	} // selection && moving_obj
   975 		
   976 	// Draw a link from one branch to another
   977 	if (drawingLink)
   978 	{
   979 		 tmpXLink->setEnd (p);
   980 		 tmpXLink->updateXLink();
   981 	}	 
   982 	
   983     // Move sceneView 
   984     if (!movingObj && !pickingColor &&!drawingLink && e->buttons() == Qt::LeftButton ) 
   985 	{
   986 		QPointF p=e->globalPos();
   987 		movingVec.setX(-p.x() + movingObj_start.x() );
   988 		movingVec.setY(-p.y() + movingObj_start.y() );
   989 		horizontalScrollBar()->setSliderPosition((int)( movingCont_start.x()+movingVec.x() ));
   990 		verticalScrollBar()->setSliderPosition((int)( movingCont_start.y()+movingVec.y() ) );
   991     }
   992 }
   993 
   994 
   995 void MapEditor::mouseReleaseEvent(QMouseEvent* e)
   996 {
   997     QPointF p = mapToScene(e->pos());
   998 	TreeItem *seli=model->getSelectedItem();
   999 
  1000 	TreeItem *dsti=NULL;
  1001 	if (seli) dsti=findMapItem(p, seli);
  1002 	LinkableMapObj* dst=NULL;
  1003 	if (dsti && dsti->isBranchLikeType ()) 
  1004 		dst=((MapItem*)dsti)->getLMO();	
  1005 	else
  1006 		dsti=NULL;
  1007 
  1008 
  1009 	// Have we been picking color?
  1010 	if (pickingColor)
  1011 	{
  1012 		pickingColor=false;
  1013 		setCursor (Qt::ArrowCursor);
  1014 		// Check if we are over another branch
  1015 		if (dst) 
  1016 		{	
  1017 			if (e->state() & Qt::ShiftModifier)
  1018 				model->colorBranch (((BranchObj*)dst)->getColor());
  1019 			else	
  1020 				model->colorSubtree (((BranchObj*)dst)->getColor());
  1021 		} 
  1022 		return;
  1023 	}
  1024 
  1025 	// Have we been drawing a link?
  1026 	if (drawingLink)	
  1027 	{
  1028 		drawingLink=false;
  1029 		// Check if we are over another branch
  1030 		if (dsti)
  1031 		{	
  1032 			tmpXLink->setEnd ( ((BranchObj*)(dst)) );
  1033 			tmpXLink->updateXLink();
  1034 			tmpXLink->activate(); //FIXME-2 savestate missing
  1035 			//model->saveStateComplete(QString("Activate xLink from %1 to %2").arg(model->getObjectName(tmpXLink->getBegin())).arg(model->getObjectName(tmpXLink->getEnd())) );	
  1036 		} else
  1037 		{
  1038 			delete(tmpXLink);
  1039 			tmpXLink=NULL;
  1040 		}
  1041 		return;
  1042 	}
  1043 	
  1044     // Have we been moving something?
  1045     if ( seli && movingObj ) 
  1046     {	
  1047 		if (seli->getType()==TreeItem::Image)
  1048 		{
  1049 			FloatImageObj *fio=(FloatImageObj*)( ((MapItem*)seli)->getLMO());
  1050 			if(fio)
  1051 			{
  1052 				// Moved FloatObj. Maybe we need to reposition
  1053 				QString pold=qpointfToString(movingObj_orgRelPos);
  1054 				QString pnow=qpointfToString(fio->getRelPos());
  1055 				model->saveState(
  1056 					seli,
  1057 					"moveRel "+pold,
  1058 					seli,
  1059 					"moveRel "+pnow,
  1060 					QString("Move %1 to relative position %2").arg(model->getObjectName(seli)).arg(pnow));
  1061 
  1062 				fio->getParObj()->requestReposition();
  1063 				model->reposition();
  1064 			}	
  1065 		}
  1066 
  1067 		BranchItem *bi=model->getSelectedBranchItem();
  1068 		if (bi && bi->depth()==0)
  1069 		{	
  1070             if (movingObj_orgPos != bi->getBranchObj()->getAbsPos())	// FIXME-3 check getBO here...
  1071             {
  1072                 QString pold=qpointfToString(movingObj_orgPos);
  1073                 QString pnow=qpointfToString(bi->getBranchObj()->getAbsPos());		// FIXME-3 check getBO here...
  1074 
  1075                 model->saveState(
  1076                     bi,
  1077                     "move "+pold,
  1078                     bi,
  1079                     "move "+pnow,
  1080                     QString("Move mapcenter %1 to position %2").arg(model->getObjectName(bi)).arg(pnow));
  1081             }
  1082 		}
  1083 	
  1084 		if (seli->isBranchLikeType() ) //(seli->getType() == TreeItem::Branch )
  1085 		{	// A branch was moved
  1086 			LinkableMapObj* lmosel=NULL;		
  1087 			lmosel=((MapItem*)seli)->getLMO();
  1088 				
  1089 			// save the position in case we link to mapcenter
  1090 			QPointF savePos=QPointF (lmosel->getAbsPos()  );
  1091 
  1092 			// Reset the temporary drawn link to the original one
  1093 			lmosel->unsetParObjTmp();
  1094 
  1095 			// For Redo we may need to save original selection
  1096 			QString preSelStr=model->getSelectString(seli);
  1097 
  1098 			copyingObj=false;	
  1099 			if (dsti)
  1100 			{
  1101 				// We have a destination, relink to that
  1102 
  1103 				BranchObj* bsel=model->getSelectedBranchObj();
  1104 
  1105 				QString preParStr=model->getSelectString (bsel->getParObj());
  1106 				QString preNum=QString::number (seli->num(),10);
  1107 				QString preDstParStr;
  1108 				bool relinked;
  1109 
  1110 				if (e->state() & Qt::ShiftModifier && dst->getParObj())
  1111 				{	// Link above dst
  1112 					preDstParStr=model->getSelectString (dst->getParObj());
  1113 					relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti->parent(),((BranchItem*)dsti)->num());
  1114 				} else 
  1115 				if (e->state() & Qt::ControlModifier && dst->getParObj())
  1116 				{
  1117 					// Link below dst
  1118 					preDstParStr=model->getSelectString (dst->getParObj());
  1119 					relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti->parent(),((BranchItem*)dsti)->num()+1);
  1120 				} else	
  1121 				{	// Append to dst
  1122 					preDstParStr=model->getSelectString(dst);
  1123 					relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti);
  1124 					if (dsti->depth()==0) bsel->move (savePos);
  1125 				} 
  1126 				if (relinked)
  1127 				{
  1128 					QString postSelStr=model->getSelectString(lmosel);
  1129 					QString postNum=QString::number (seli->num(),10);
  1130 
  1131 					QString undoCom="relinkTo (\""+ 
  1132 						preParStr+ "\"," + preNum  +"," + 
  1133 						QString ("%1,%2").arg(movingObj_orgPos.x()).arg(movingObj_orgPos.y())+ ")";
  1134 
  1135 					QString redoCom="relinkTo (\""+ 
  1136 						preDstParStr + "\"," + postNum + "," +
  1137 						QString ("%1,%2").arg(savePos.x()).arg(savePos.y())+ ")";
  1138 
  1139 					model->saveState (
  1140 						postSelStr,undoCom,
  1141 						preSelStr, redoCom,
  1142 						QString("Relink %1 to %2").arg(model->getObjectName(bsel)).arg(model->getObjectName(dst)) );
  1143 
  1144 				}
  1145 			} else
  1146 			{
  1147 				// No destination, undo  temporary move
  1148 
  1149 				if (seli->depth()==1)
  1150 				{
  1151 					// The select string might be different _after_ moving around.
  1152 					// Therefor reposition and then use string of old selection, too
  1153 					model->reposition();
  1154 
  1155                     QPointF rp(lmosel->getRelPos());
  1156                     if (rp != movingObj_orgRelPos)
  1157                     {
  1158                         QString ps=qpointfToString(rp);
  1159                         model->saveState(
  1160                             model->getSelectString(lmosel), "moveRel "+qpointfToString(movingObj_orgRelPos), 
  1161                             preSelStr, "moveRel "+ps, 
  1162                             QString("Move %1 to relative position %2").arg(model->getObjectName(lmosel)).arg(ps));
  1163                     }
  1164 				}
  1165 
  1166 				// Draw the original link, before selection was moved around
  1167 				if (settings.value("/animation/use",false).toBool() && seli->depth()>1) 
  1168 				{
  1169 					lmosel->setRelPos();	// calc relPos first for starting point
  1170 					QPointF dst=bi->getBranchObj()->getParObj()->getChildPos();		// FIXME-3 check getBO here...
  1171 			//		if (lmosel->getOrientation()==LinkableMapObj::LeftOfCenter) dst.setX (dst.x()+lmosel->width() );
  1172 					
  1173 					model->startAnimation(
  1174 						(BranchObj*)lmosel,
  1175 						lmosel->getRelPos(),
  1176 						movingObj_orgRelPos
  1177 //						QPointF (movingObj_orgPos.x() - dst.x(), movingObj_orgPos.y() - dst.y() )
  1178 					);	
  1179 				} else	
  1180 					model->reposition();
  1181 			}
  1182 		}
  1183 		model->emitSelectionChanged();  //FIXME-3 needed? at least not after pos of selection has changed...
  1184 		// Finally resize scene, if needed
  1185 		scene()->update();
  1186 		movingObj=NULL;		
  1187 
  1188 		// Just make sure, that actions are still ok,e.g. the move branch up/down buttons...
  1189 		// model->updateActions(); FIXME-3 neeeded? 
  1190 	} else 
  1191 		// maybe we moved View: set old cursor
  1192 		setCursor (Qt::ArrowCursor);
  1193     
  1194 }
  1195 
  1196 void MapEditor::mouseDoubleClickEvent(QMouseEvent* e)
  1197 {
  1198 	if (model->isSelectionBlocked() ) 
  1199 	{
  1200 		e->ignore();
  1201 		return;
  1202 	}
  1203 
  1204 	if (e->button() == Qt::LeftButton )
  1205 	{
  1206 		QPointF p = mapToScene(e->pos());
  1207 		TreeItem *ti=findMapItem (p, NULL);
  1208 		if (ti) {	// MapObj was found
  1209 			// First select the MapObj than edit heading
  1210 			model->select (ti);
  1211 			editHeading();
  1212 		}
  1213 	}
  1214 }
  1215 
  1216 void MapEditor::resizeEvent (QResizeEvent* e)
  1217 {
  1218 	QGraphicsView::resizeEvent( e );
  1219 }
  1220 
  1221 void MapEditor::dragEnterEvent(QDragEnterEvent *event)
  1222 {
  1223 	//for (unsigned int i=0;event->format(i);i++) // Debug mime type
  1224 	//	cerr << event->format(i) << endl;
  1225 
  1226 	if (event->mimeData()->hasImage())
  1227 		event->acceptProposedAction();
  1228 	else	
  1229 		if (event->mimeData()->hasUrls())
  1230 			event->acceptProposedAction();
  1231 }
  1232 
  1233 void MapEditor::dragMoveEvent(QDragMoveEvent *)
  1234 {
  1235 }
  1236 
  1237 void MapEditor::dragLeaveEvent(QDragLeaveEvent *event)
  1238 {
  1239 	event->accept();
  1240 }
  1241 
  1242 void MapEditor::dropEvent(QDropEvent *event)
  1243 {
  1244 	BranchItem *selbi=model->getSelectedBranchItem();
  1245 	if (selbi)
  1246 	{
  1247 		if (debug)
  1248 			foreach (QString format,event->mimeData()->formats()) 
  1249 				cout << "MapEditor: Dropped format: "<<qPrintable (format)<<endl;
  1250 
  1251 
  1252 		QList <QUrl> uris;
  1253 		if (event->mimeData()->hasImage()) 
  1254 		{
  1255 			 QVariant imageData = event->mimeData()->imageData();
  1256 			 model->addFloatImage (qvariant_cast<QPixmap>(imageData));
  1257 		} else
  1258 		if (event->mimeData()->hasUrls())
  1259 			uris=event->mimeData()->urls();
  1260 
  1261 		if (uris.count()>0)
  1262 		{
  1263 			QStringList files;
  1264 			QString s;
  1265 			QString heading;
  1266 			BranchItem *bi;
  1267 			for (int i=0; i<uris.count();i++)
  1268 			{
  1269 				// Workaround to avoid adding empty branches
  1270 				if (!uris.at(i).toString().isEmpty())
  1271 				{
  1272 					bi=model->addNewBranch();
  1273 					if (bi)
  1274 					{
  1275 						   /* FIXME-2 
  1276 						s=uris.at(i).toLocalFile();
  1277 						if (!s.isEmpty()) 
  1278 						{
  1279 						   QString file = QDir::fromNativeSeparators(s);
  1280 						   heading = QFileInfo(file).baseName();
  1281 						   files.append(file);
  1282 						   if (file.endsWith(".vym", false))
  1283 							   bi->setVymLink(file);
  1284 						   else
  1285 							   bi->setURL(uris.at(i).toString());
  1286 					   } else 
  1287 					   {
  1288 						   bo->setURL(uris.at(i).toString());
  1289 					   }
  1290 							 */  
  1291 
  1292 					   if (!heading.isEmpty())
  1293 						   bi->setHeading(heading);
  1294 					   else
  1295 						   bi->setHeading(uris.at(i).toString());
  1296 						   
  1297 					}
  1298 				}
  1299 			}
  1300 			model->reposition();
  1301 		}
  1302 	}	
  1303 	event->acceptProposedAction();
  1304 }
  1305 
  1306 void MapEditor::updateSelection(QItemSelection newsel,QItemSelection oldsel)
  1307 {
  1308 	// Note: Here we are prepared for multiple selections, though this 
  1309 	// is not yet implemented elsewhere
  1310 
  1311 	// Here in MapEditor we can only select Branches and Images
  1312 	QModelIndex ix;
  1313 	foreach (ix,newsel.indexes() )
  1314 	{
  1315 		TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
  1316 		if (ti->getType()!=TreeItem::Branch && ti->getType()!=TreeItem::Image )
  1317 			newsel.indexes().removeOne (ix);
  1318 	}
  1319 
  1320 	// Trim list of selection rectangles 
  1321 	while (newsel.indexes().count() < selboxList.count() )
  1322 		delete selboxList.takeFirst();
  1323 
  1324 	// Take care to tmp scroll/unscroll
  1325 	if (!oldsel.isEmpty())
  1326 	{
  1327 		QModelIndex ix=oldsel.indexes().first(); 
  1328 		if (ix.isValid() )
  1329 		{
  1330 			TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
  1331 			if (ti)
  1332 			{
  1333 				if (ti->isBranchLikeType() )
  1334 				{
  1335 					// reset tmp scrolled branches
  1336 					BranchItem *bi=(BranchItem*)ti;
  1337 					bi->resetTmpUnscroll();
  1338 				}
  1339 				if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image)
  1340 					// Hide link if not needed
  1341 					((MapItem*)ti)->getLMO()->updateVisibility();
  1342 			}
  1343 		}
  1344 	}
  1345 
  1346 	if (!newsel.isEmpty())
  1347 	{
  1348 		QModelIndex ix=newsel.indexes().first(); 
  1349 		if (ix.isValid() )
  1350 		{
  1351 			// Temporary unscroll if necessary
  1352 			TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
  1353 			if (ti->isBranchLikeType() )
  1354 			{
  1355 				BranchItem *bi=(BranchItem*)ti;
  1356 				if (bi->hasScrolledParent(bi) )
  1357 					bi->tmpUnscroll();
  1358 			}
  1359 			scrollTo (ix);
  1360 			if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image)
  1361 				// Show link if needed
  1362 				((MapItem*)ti)->getLMO()->updateVisibility();
  1363 		}
  1364 	}
  1365 
  1366 	// Reduce rectangles
  1367 	while (newsel.indexes().count() < selboxList.count() )
  1368 		delete selboxList.takeFirst();
  1369 
  1370 	// Add additonal rectangles
  1371 	QGraphicsRectItem *sb;
  1372 	while (newsel.indexes().count() > selboxList.count() )
  1373 	{
  1374 		sb = mapScene->addRect(
  1375 			QRectF(0,0,0,0), 
  1376 			QPen(selectionColor),
  1377 			selectionColor);
  1378 		sb->setZValue(Z_SELBOX);
  1379 		sb->show();
  1380 		selboxList.append (sb);
  1381 	}
  1382 
  1383 	// Reposition rectangles
  1384 	int i=0;
  1385 	QRectF bbox;
  1386 	QModelIndex index;
  1387 
  1388 	TreeItem *ti;
  1389 	LinkableMapObj *lmo;
  1390 	foreach (sb,selboxList)
  1391 	{
  1392 		index=newsel.indexes().at(i);
  1393 		ti= static_cast<TreeItem*>(index.internalPointer());
  1394 		lmo=((MapItem*)ti)->getLMO();
  1395 		bbox=lmo->getBBox();
  1396 		sb->setRect (
  1397 			bbox.x(),bbox.y(), 
  1398 			bbox.width(), bbox.height());
  1399 		sb->setPen (selectionColor);	
  1400 		sb->setBrush (selectionColor);	
  1401 		i++;
  1402 	}
  1403 
  1404 	scene()->update();
  1405 }
  1406 
  1407 void MapEditor::updateData (const QModelIndex &sel)
  1408 {
  1409 	TreeItem *ti= static_cast<TreeItem*>(sel.internalPointer());
  1410 
  1411 /* testing
  1412 	cout << "ME::updateData\n";
  1413 
  1414 	cout << "  ti="<<ti<<endl;
  1415 	cout << "  h="<<ti->getHeading().toStdString()<<endl;
  1416 	*/
  1417 	
  1418 	if (ti->isBranchLikeType())
  1419 	{
  1420 		BranchObj *bo=(BranchObj*) ( ((MapItem*)ti)->getLMO());
  1421 		bo->updateData();
  1422 	}
  1423 
  1424 }
  1425 
  1426 void MapEditor::setSelectionColor (QColor col)
  1427 {
  1428 	selectionColor=col;
  1429 	QItemSelection sel=model->getSelectionModel()->selection();
  1430 	updateSelection(sel,sel);
  1431 }
  1432 
  1433 
  1434 QColor MapEditor::getSelectionColor ()
  1435 {
  1436 	return selectionColor;
  1437 }
  1438 
  1439