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