mapeditor.cpp
author insilmaril
Mon, 03 Aug 2009 10:42:12 +0000
changeset 785 5987f9f15bac
parent 782 ffb02a9bb508
child 786 6269016c9905
permissions -rw-r--r--
Fixed problem with images included in branches. Added missing adaptormodel.* files
     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 				cout << "ME::release mouse\n";
  1063 				fio->getParObj()->requestReposition();
  1064 				model->reposition();
  1065 			}	
  1066 		}
  1067 
  1068 		BranchItem *bi=model->getSelectedBranchItem();
  1069 		if (bi && bi->depth()==0)
  1070 		{	
  1071             if (movingObj_orgPos != bi->getBranchObj()->getAbsPos())	// FIXME-3 check getBO here...
  1072             {
  1073                 QString pold=qpointfToString(movingObj_orgPos);
  1074                 QString pnow=qpointfToString(bi->getBranchObj()->getAbsPos());		// FIXME-3 check getBO here...
  1075 
  1076                 model->saveState(
  1077                     bi,
  1078                     "move "+pold,
  1079                     bi,
  1080                     "move "+pnow,
  1081                     QString("Move mapcenter %1 to position %2").arg(model->getObjectName(bi)).arg(pnow));
  1082             }
  1083 		}
  1084 	
  1085 		if (seli->isBranchLikeType() ) //(seli->getType() == TreeItem::Branch )
  1086 		{	// A branch was moved
  1087 			LinkableMapObj* lmosel=NULL;		
  1088 			lmosel=((MapItem*)seli)->getLMO();
  1089 				
  1090 			// save the position in case we link to mapcenter
  1091 			QPointF savePos=QPointF (lmosel->getAbsPos()  );
  1092 
  1093 			// Reset the temporary drawn link to the original one
  1094 			lmosel->unsetParObjTmp();
  1095 
  1096 			// For Redo we may need to save original selection
  1097 			QString preSelStr=model->getSelectString(seli);
  1098 
  1099 			copyingObj=false;	
  1100 			if (dsti)
  1101 			{
  1102 				// We have a destination, relink to that
  1103 
  1104 				BranchObj* bsel=model->getSelectedBranchObj();
  1105 
  1106 				QString preParStr=model->getSelectString (bsel->getParObj());
  1107 				QString preNum=QString::number (seli->num(),10);
  1108 				QString preDstParStr;
  1109 				bool relinked;
  1110 
  1111 				if (e->state() & Qt::ShiftModifier && dst->getParObj())
  1112 				{	// Link above dst
  1113 					preDstParStr=model->getSelectString (dst->getParObj());
  1114 					relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti->parent(),((BranchItem*)dsti)->num());
  1115 				} else 
  1116 				if (e->state() & Qt::ControlModifier && dst->getParObj())
  1117 				{
  1118 					// Link below dst
  1119 					preDstParStr=model->getSelectString (dst->getParObj());
  1120 					relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti->parent(),((BranchItem*)dsti)->num()+1);
  1121 				} else	
  1122 				{	// Append to dst
  1123 					preDstParStr=model->getSelectString(dst);
  1124 					relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti);
  1125 					if (dsti->depth()==0) bsel->move (savePos);
  1126 				} 
  1127 				if (relinked)
  1128 				{
  1129 					QString postSelStr=model->getSelectString(lmosel);
  1130 					QString postNum=QString::number (seli->num(),10);
  1131 
  1132 					QString undoCom="relinkTo (\""+ 
  1133 						preParStr+ "\"," + preNum  +"," + 
  1134 						QString ("%1,%2").arg(movingObj_orgPos.x()).arg(movingObj_orgPos.y())+ ")";
  1135 
  1136 					QString redoCom="relinkTo (\""+ 
  1137 						preDstParStr + "\"," + postNum + "," +
  1138 						QString ("%1,%2").arg(savePos.x()).arg(savePos.y())+ ")";
  1139 
  1140 					model->saveState (
  1141 						postSelStr,undoCom,
  1142 						preSelStr, redoCom,
  1143 						QString("Relink %1 to %2").arg(model->getObjectName(bsel)).arg(model->getObjectName(dst)) );
  1144 
  1145 				}
  1146 			} else
  1147 			{
  1148 				// No destination, undo  temporary move
  1149 
  1150 				if (seli->depth()==1)
  1151 				{
  1152 					// The select string might be different _after_ moving around.
  1153 					// Therefor reposition and then use string of old selection, too
  1154 					model->reposition();
  1155 
  1156                     QPointF rp(lmosel->getRelPos());
  1157                     if (rp != movingObj_orgRelPos)
  1158                     {
  1159                         QString ps=qpointfToString(rp);
  1160                         model->saveState(
  1161                             model->getSelectString(lmosel), "moveRel "+qpointfToString(movingObj_orgRelPos), 
  1162                             preSelStr, "moveRel "+ps, 
  1163                             QString("Move %1 to relative position %2").arg(model->getObjectName(lmosel)).arg(ps));
  1164                     }
  1165 				}
  1166 
  1167 				// Draw the original link, before selection was moved around
  1168 				if (settings.value("/animation/use",false).toBool() && seli->depth()>1) 
  1169 				{
  1170 					lmosel->setRelPos();	// calc relPos first for starting point
  1171 					QPointF dst=bi->getBranchObj()->getParObj()->getChildPos();		// FIXME-3 check getBO here...
  1172 			//		if (lmosel->getOrientation()==LinkableMapObj::LeftOfCenter) dst.setX (dst.x()+lmosel->width() );
  1173 					
  1174 					model->startAnimation(
  1175 						(BranchObj*)lmosel,
  1176 						lmosel->getRelPos(),
  1177 						movingObj_orgRelPos
  1178 //						QPointF (movingObj_orgPos.x() - dst.x(), movingObj_orgPos.y() - dst.y() )
  1179 					);	
  1180 				} else	
  1181 					model->reposition();
  1182 			}
  1183 		}
  1184 		model->emitSelectionChanged();  //FIXME-3 needed? at least not after pos of selection has changed...
  1185 		// Finally resize scene, if needed
  1186 		scene()->update();
  1187 		movingObj=NULL;		
  1188 
  1189 		// Just make sure, that actions are still ok,e.g. the move branch up/down buttons...
  1190 		// model->updateActions(); FIXME-3 neeeded? 
  1191 	} else 
  1192 		// maybe we moved View: set old cursor
  1193 		setCursor (Qt::ArrowCursor);
  1194     
  1195 }
  1196 
  1197 void MapEditor::mouseDoubleClickEvent(QMouseEvent* e)
  1198 {
  1199 	if (model->isSelectionBlocked() ) 
  1200 	{
  1201 		e->ignore();
  1202 		return;
  1203 	}
  1204 
  1205 	if (e->button() == Qt::LeftButton )
  1206 	{
  1207 		QPointF p = mapToScene(e->pos());
  1208 		TreeItem *ti=findMapItem (p, NULL);
  1209 		if (ti) {	// MapObj was found
  1210 			// First select the MapObj than edit heading
  1211 			model->select (ti);
  1212 			editHeading();
  1213 		}
  1214 	}
  1215 }
  1216 
  1217 void MapEditor::resizeEvent (QResizeEvent* e)
  1218 {
  1219 	QGraphicsView::resizeEvent( e );
  1220 }
  1221 
  1222 void MapEditor::dragEnterEvent(QDragEnterEvent *event)
  1223 {
  1224 	//for (unsigned int i=0;event->format(i);i++) // Debug mime type
  1225 	//	cerr << event->format(i) << endl;
  1226 
  1227 	if (event->mimeData()->hasImage())
  1228 		event->acceptProposedAction();
  1229 	else	
  1230 		if (event->mimeData()->hasUrls())
  1231 			event->acceptProposedAction();
  1232 }
  1233 
  1234 void MapEditor::dragMoveEvent(QDragMoveEvent *)
  1235 {
  1236 }
  1237 
  1238 void MapEditor::dragLeaveEvent(QDragLeaveEvent *event)
  1239 {
  1240 	event->accept();
  1241 }
  1242 
  1243 void MapEditor::dropEvent(QDropEvent *event)
  1244 {
  1245 	BranchItem *selbi=model->getSelectedBranchItem();
  1246 	if (selbi)
  1247 	{
  1248 		if (debug)
  1249 			foreach (QString format,event->mimeData()->formats()) 
  1250 				cout << "MapEditor: Dropped format: "<<qPrintable (format)<<endl;
  1251 
  1252 
  1253 		QList <QUrl> uris;
  1254 		if (event->mimeData()->hasImage()) 
  1255 		{
  1256 			 QVariant imageData = event->mimeData()->imageData();
  1257 			 model->addFloatImage (qvariant_cast<QPixmap>(imageData));
  1258 		} else
  1259 		if (event->mimeData()->hasUrls())
  1260 			uris=event->mimeData()->urls();
  1261 
  1262 		if (uris.count()>0)
  1263 		{
  1264 			QStringList files;
  1265 			QString s;
  1266 			QString heading;
  1267 			BranchItem *bi;
  1268 			for (int i=0; i<uris.count();i++)
  1269 			{
  1270 				// Workaround to avoid adding empty branches
  1271 				if (!uris.at(i).toString().isEmpty())
  1272 				{
  1273 					bi=model->addNewBranch();
  1274 					if (bi)
  1275 					{
  1276 						   /* FIXME-2 
  1277 						s=uris.at(i).toLocalFile();
  1278 						if (!s.isEmpty()) 
  1279 						{
  1280 						   QString file = QDir::fromNativeSeparators(s);
  1281 						   heading = QFileInfo(file).baseName();
  1282 						   files.append(file);
  1283 						   if (file.endsWith(".vym", false))
  1284 							   bi->setVymLink(file);
  1285 						   else
  1286 							   bi->setURL(uris.at(i).toString());
  1287 					   } else 
  1288 					   {
  1289 						   bo->setURL(uris.at(i).toString());
  1290 					   }
  1291 							 */  
  1292 
  1293 					   if (!heading.isEmpty())
  1294 						   bi->setHeading(heading);
  1295 					   else
  1296 						   bi->setHeading(uris.at(i).toString());
  1297 						   
  1298 					}
  1299 				}
  1300 			}
  1301 			model->reposition();
  1302 		}
  1303 	}	
  1304 	event->acceptProposedAction();
  1305 }
  1306 
  1307 void MapEditor::updateSelection(QItemSelection newsel,QItemSelection oldsel)
  1308 {
  1309 	// Note: Here we are prepared for multiple selections, though this 
  1310 	// is not yet implemented elsewhere
  1311 
  1312 	// Here in MapEditor we can only select Branches and Images
  1313 	QModelIndex ix;
  1314 	foreach (ix,newsel.indexes() )
  1315 	{
  1316 		TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
  1317 		if (ti->getType()!=TreeItem::Branch && ti->getType()!=TreeItem::Image )
  1318 			newsel.indexes().removeOne (ix);
  1319 	}
  1320 
  1321 	// Trim list of selection rectangles 
  1322 	while (newsel.indexes().count() < selboxList.count() )
  1323 		delete selboxList.takeFirst();
  1324 
  1325 	// Take care to tmp scroll/unscroll
  1326 	if (!oldsel.isEmpty())
  1327 	{
  1328 		QModelIndex ix=oldsel.indexes().first(); 
  1329 		if (ix.isValid() )
  1330 		{
  1331 			TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
  1332 			if (ti)
  1333 			{
  1334 				if (ti->isBranchLikeType() )
  1335 				{
  1336 					// reset tmp scrolled branches
  1337 					BranchItem *bi=(BranchItem*)ti;
  1338 					bi->resetTmpUnscroll();
  1339 				}
  1340 				if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image)
  1341 					// Hide link if not needed
  1342 					((MapItem*)ti)->getLMO()->updateVisibility();
  1343 			}
  1344 		}
  1345 	}
  1346 
  1347 	if (!newsel.isEmpty())
  1348 	{
  1349 		QModelIndex ix=newsel.indexes().first(); 
  1350 		if (ix.isValid() )
  1351 		{
  1352 			// Temporary unscroll if necessary
  1353 			TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
  1354 			if (ti->isBranchLikeType() )
  1355 			{
  1356 				BranchItem *bi=(BranchItem*)ti;
  1357 				if (bi->hasScrolledParent(bi) )
  1358 					bi->tmpUnscroll();
  1359 			}
  1360 			scrollTo (ix);
  1361 			if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image)
  1362 				// Show link if needed
  1363 				((MapItem*)ti)->getLMO()->updateVisibility();
  1364 		}
  1365 	}
  1366 
  1367 	// Reduce rectangles
  1368 	while (newsel.indexes().count() < selboxList.count() )
  1369 		delete selboxList.takeFirst();
  1370 
  1371 	// Add additonal rectangles
  1372 	QGraphicsRectItem *sb;
  1373 	while (newsel.indexes().count() > selboxList.count() )
  1374 	{
  1375 		sb = mapScene->addRect(
  1376 			QRectF(0,0,0,0), 
  1377 			QPen(selectionColor),
  1378 			selectionColor);
  1379 		sb->setZValue(Z_SELBOX);
  1380 		sb->show();
  1381 		selboxList.append (sb);
  1382 	}
  1383 
  1384 	// Reposition rectangles
  1385 	int i=0;
  1386 	QRectF bbox;
  1387 	QModelIndex index;
  1388 
  1389 	TreeItem *ti;
  1390 	LinkableMapObj *lmo;
  1391 	foreach (sb,selboxList)
  1392 	{
  1393 		index=newsel.indexes().at(i);
  1394 		ti= static_cast<TreeItem*>(index.internalPointer());
  1395 		lmo=((MapItem*)ti)->getLMO();
  1396 		bbox=lmo->getBBox();
  1397 		sb->setRect (
  1398 			bbox.x(),bbox.y(), 
  1399 			bbox.width(), bbox.height());
  1400 		sb->setPen (selectionColor);	
  1401 		sb->setBrush (selectionColor);	
  1402 		i++;
  1403 	}
  1404 
  1405 	scene()->update();
  1406 }
  1407 
  1408 void MapEditor::updateData (const QModelIndex &sel)
  1409 {
  1410 	TreeItem *ti= static_cast<TreeItem*>(sel.internalPointer());
  1411 
  1412 /* testing
  1413 	cout << "ME::updateData\n";
  1414 
  1415 	cout << "  ti="<<ti<<endl;
  1416 	cout << "  h="<<ti->getHeading().toStdString()<<endl;
  1417 	*/
  1418 	
  1419 	if (ti->isBranchLikeType())
  1420 	{
  1421 	//	cout << "  ->updating...\n";
  1422 		BranchObj *bo=(BranchObj*) ( ((MapItem*)ti)->getLMO());
  1423 		bo->updateData();
  1424 	}
  1425 }
  1426 
  1427 void MapEditor::setSelectionColor (QColor col)
  1428 {
  1429 	selectionColor=col;
  1430 	QItemSelection sel=model->getSelectionModel()->selection();
  1431 	updateSelection(sel,sel);
  1432 }
  1433 
  1434 
  1435 QColor MapEditor::getSelectionColor ()
  1436 {
  1437 	return selectionColor;
  1438 }
  1439 
  1440