mapeditor.cpp
author insilmaril
Mon, 20 Apr 2009 10:42:05 +0000
changeset 754 db0ec4bcf416
parent 753 25a77484ec72
child 755 ed5b407975b3
permissions -rw-r--r--
minor fixes
     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 extern FlagRowObj *standardFlagsDefault;
    23 
    24 extern QMenu* branchContextMenu;
    25 extern QMenu* branchAddContextMenu;
    26 extern QMenu* branchRemoveContextMenu;
    27 extern QMenu* branchLinksContextMenu;
    28 extern QMenu* branchXLinksContextMenuEdit;
    29 extern QMenu* branchXLinksContextMenuFollow;
    30 extern QMenu* floatimageContextMenu;
    31 extern QMenu* canvasContextMenu;
    32 
    33 extern Settings settings;
    34 extern QString iconPath;
    35 
    36 ///////////////////////////////////////////////////////////////////////
    37 ///////////////////////////////////////////////////////////////////////
    38 MapEditor::MapEditor( VymModel *vm) 
    39 {
    40 	//cout << "Constructor ME "<<this<<endl;
    41 	mapScene= new QGraphicsScene(NULL);
    42 	mapScene->setBackgroundBrush (QBrush(Qt::white, Qt::SolidPattern));
    43 
    44 	model=vm;
    45 	model->setScene (mapScene);
    46 	model->registerEditor(this);
    47 //	model->addMapCenter();	//  FIXME-3 create this in MapEditor until BO and MCO are independent of scene
    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);
    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 	// Action to embed LineEdit for heading in Scene
    78 	editingHeading=false;
    79 	lineEdit=new QLineEdit;
    80 	lineEdit->hide();
    81 	QGraphicsProxyWidget *pw=scene()->addWidget (lineEdit);
    82 	pw->setZValue (100);
    83 
    84 	QAction *a = new QAction( tr( "Edit heading","MapEditor" ), this);
    85 	a->setShortcut ( Qt::Key_Return );					//Edit heading
    86 	addAction (a);
    87     connect( a, SIGNAL( triggered() ), this, SLOT( editHeading() ) );
    88 	a = new QAction( tr( "Edit heading","MapEditor" ), this);
    89 	a->setShortcut ( Qt::Key_Enter);					//Edit heading
    90 	addAction (a);
    91     connect( a, SIGNAL( triggered() ), this, SLOT( editHeading() ) );
    92 
    93 	// Selections
    94 	selectionColor =QColor (255,255,0);
    95 	
    96 
    97 	// Attributes	//FIXME-2 testing only...
    98 	QString k;
    99 	AttributeDef *ad;
   100 	attrTable= new AttributeTable();
   101 	k="A - StringList";
   102 	ad=attrTable->addKey (k,StringList);
   103 	if (ad)
   104 	{
   105 		QStringList sl;
   106 		sl <<"val 1"<<"val 2"<< "val 3";
   107 		ad->setValue (QVariant (sl));
   108 	}
   109 	//attrTable->addValue ("Key A","P 1");
   110 	//attrTable->addValue ("Key A","P 2");
   111 	//attrTable->addValue ("Key A","P 3");
   112 	//attrTable->addValue ("Key A","P 4");
   113 	k="B - FreeString";
   114 	ad=attrTable->addKey (k,FreeString);
   115 	if (ad)
   116 	{
   117 		//attrTable->addValue ("Key B","w1");
   118 		//attrTable->addValue ("Key B","w2");
   119 	}
   120 	k="C - UniqueString";
   121 	ad=attrTable->addKey (k,UniqueString);
   122 	if (ad)
   123 	{
   124 	//attrTable->addKey ("Key Prio");
   125 	//attrTable->addValue ("Key Prio","Prio 1");
   126 	//attrTable->addValue ("Key Prio","Prio 2");
   127 	}
   128 }
   129 
   130 MapEditor::~MapEditor()
   131 {
   132 	//cout <<"Destructor MapEditor for "<<model->getMapName().toStdString()<<endl;
   133 	// No need to remove tmp files:
   134 	// tmpMapDir is in tmpVymDir, 
   135 	// so it gets removed automagically when vym closes
   136 	
   137 	model->unregisterEditor(this);
   138 }
   139 
   140 VymModel* MapEditor::getModel()
   141 {
   142     return model;
   143 }
   144 
   145 QGraphicsScene * MapEditor::getScene()
   146 {
   147     return mapScene;
   148 }
   149 
   150 void MapEditor::print()
   151 {
   152 	if ( !printer ) 
   153 	{
   154 		printer = new QPrinter;
   155 		printer->setColorMode (QPrinter::Color);
   156 		printer->setPrinterName (settings.value("/mainwindow/printerName",printer->printerName()).toString());
   157 		printer->setOutputFormat((QPrinter::OutputFormat)settings.value("/mainwindow/printerFormat",printer->outputFormat()).toInt());
   158 		printer->setOutputFileName(settings.value("/mainwindow/printerFileName",printer->outputFileName()).toString());
   159 	}
   160 
   161 	QRectF totalBBox=model->getTotalBBox();
   162 
   163 	// Try to set orientation automagically
   164 	// Note: Interpretation of generated postscript is amibiguous, if 
   165 	// there are problems with landscape mode, see
   166 	// http://sdb.suse.de/de/sdb/html/jsmeix_print-cups-landscape-81.html
   167 
   168 	if (totalBBox.width()>totalBBox.height())
   169 		// recommend landscape
   170 		printer->setOrientation (QPrinter::Landscape);
   171 	else	
   172 		// recommend portrait
   173 		printer->setOrientation (QPrinter::Portrait);
   174 
   175 	if ( printer->setup(this) ) 
   176 	// returns false, if printing is canceled
   177 	{
   178 		QPainter pp(printer);
   179 
   180 		pp.setRenderHint(QPainter::Antialiasing,true);
   181 
   182 		// Don't print the visualisation of selection
   183 		model->unselect();
   184 
   185 		QRectF mapRect=totalBBox;
   186 		QGraphicsRectItem *frame=NULL;
   187 
   188 		if (printFrame) 
   189 		{
   190 			// Print frame around map
   191 			mapRect.setRect (totalBBox.x()-10, totalBBox.y()-10, 
   192 				totalBBox.width()+20, totalBBox.height()+20);
   193 			frame=mapScene->addRect (mapRect, QPen(Qt::black),QBrush(Qt::NoBrush));
   194 			frame->setZValue(0);
   195 			frame->show();    
   196 		}		
   197 
   198 
   199 		double paperAspect = (double)printer->width()   / (double)printer->height();
   200 		double   mapAspect = (double)mapRect.width() / (double)mapRect.height();
   201 		int viewBottom;
   202 		if (mapAspect>=paperAspect)
   203 		{
   204 			// Fit horizontally to paper width
   205 			//pp.setViewport(0,0, printer->width(),(int)(printer->width()/mapAspect) );	
   206 			viewBottom=(int)(printer->width()/mapAspect);	
   207 		}	else
   208 		{
   209 			// Fit vertically to paper height
   210 			//pp.setViewport(0,0,(int)(printer->height()*mapAspect),printer->height());	
   211 			viewBottom=printer->height();	
   212 		}	
   213 		
   214 		if (printFooter) 
   215 		{
   216 			// Print footer below map
   217 			QFont font;		
   218 			font.setPointSize(10);
   219 			pp.setFont (font);
   220 			QRectF footerBox(0,viewBottom,printer->width(),15);
   221 			// FIXME-3 fileName not any longer available here: pp.drawText ( footerBox,Qt::AlignLeft,"VYM - " +fileName);
   222 			pp.drawText ( footerBox, Qt::AlignRight, QDate::currentDate().toString(Qt::TextDate));
   223 		}
   224 		mapScene->render (
   225 			&pp, 
   226 			QRectF (0,0,printer->width(),printer->height()-15),
   227 			QRectF(mapRect.x(),mapRect.y(),mapRect.width(),mapRect.height())
   228 		);
   229 		
   230 		// Viewport has paper dimension
   231 		if (frame)  delete (frame);
   232 
   233 		// Restore selection
   234 		model->reselect();
   235 
   236 		// Save settings in vymrc
   237 		settings.writeEntry("/mainwindow/printerName",printer->printerName());
   238 		settings.writeEntry("/mainwindow/printerFormat",printer->outputFormat());
   239 		settings.writeEntry("/mainwindow/printerFileName",printer->outputFileName());
   240 	}
   241 }
   242 
   243 void MapEditor::setAntiAlias (bool b)
   244 {
   245 	setRenderHint(QPainter::Antialiasing,b);
   246 }
   247 
   248 void MapEditor::setSmoothPixmap(bool b)
   249 {
   250 	setRenderHint(QPainter::SmoothPixmapTransform,b);
   251 }
   252 
   253 void MapEditor::toggleStandardFlag(QString f)
   254 {
   255 	BranchItem *bi=model->getSelectedBranchItem();
   256 	if (bi) 
   257 	{
   258 		QString u,r;
   259 		/* FIXME-2 isSetStandardFlag needs to be ported
   260 		if (bi->isSetStandardFlag(f))
   261 		{
   262 			r="unsetFlag";
   263 			u="setFlag";
   264 		}	
   265 		else
   266 		{
   267 			u="unsetFlag";
   268 			r="setFlag";
   269 		}	
   270 		*/
   271 		model->saveState(
   272 			bi,
   273 			QString("%1 (\"%2\")").arg(u).arg(f), 
   274 			bi,
   275 			QString("%1 (\"%2\")").arg(r).arg(f),
   276 			QString("Toggling standard flag \"%1\" of %2").arg(f).arg(model->getObjectName(bi)));
   277 		// FIXME-2 bi->toggleStandardFlag (f,mainWindow->useFlagGroups());
   278 		model->updateSelection();	// geometry has changed
   279 	}
   280 }
   281 
   282 TreeItem* MapEditor::findMapItem (QPointF p,TreeItem *exclude)
   283 {
   284 	// Start with mapcenter, no images allowed at rootItem
   285 	int i=0;
   286 	BranchItem *bi=model->getRootItem()->getFirstBranch();
   287 	TreeItem *found=NULL;
   288 	while (bi)
   289 	{
   290 		found=bi->findMapItem (p, exclude);
   291 		if (found) return found;
   292 		i++;
   293 		bi=model->getRootItem()->getBranchNum(i);
   294 	}
   295 	return NULL;
   296 }
   297 
   298 AttributeTable* MapEditor::attributeTable()
   299 {
   300 	return attrTable;
   301 }
   302 
   303 void MapEditor::testFunction1()
   304 {
   305 	BranchItem *cur=NULL;
   306 	BranchItem *prev=NULL;
   307 	int d;
   308 	cout << "ME::testFunction1  starting to walk the map...\n";
   309 	while (model->next (cur,prev,d) )
   310 		cout << "*** " <<cur->getHeading().toStdString()<<endl;
   311 
   312 
   313 	//BranchObj *bo=model->getSelectedBranchObj();
   314 	//if (bo) model->moveAway (bo);
   315 	//if (bo) bo->setLinkStyle (LinkableMapObj::Line);
   316 	
   317 
   318 /*
   319 	// Displacement and animation of all non-mainbranches
   320 	QPointF p;
   321 	QPointF q;
   322 	BranchObj *bo;
   323 	TreeItem *cur=NULL;
   324 	TreeItem *prev=NULL;
   325 	int d;
   326 	while (cur) 
   327 	{
   328 		bo=(BranchObj*)(cur->getLMO());
   329 
   330 		if (cur->depth() >0 && !bo->hasScrolledParent(bo) )
   331 		{
   332 			p=QPointF (qrand() %600-300, qrand () %600-300);
   333 			bo->setRelPos();
   334 			q=bo->getRelPos();
   335 			model->startAnimation (bo,p, q);
   336 		}
   337 		model->next(cur,prev,d);
   338 	}
   339 */
   340 
   341 
   342 /* TODO Hide hidden stuff temporary, maybe add this as regular function somewhere
   343 	if (hidemode==HideNone)
   344 	{
   345 		setHideTmpMode (HideExport);
   346 		mapCenter->calcBBoxSizeWithChilds();
   347 		QRectF totalBBox=mapCenter->getTotalBBox();
   348 		QRectF mapRect=totalBBox;
   349 		QCanvasRectangle *frame=NULL;
   350 
   351 		cout << "  map has =("<<totalBBox.x()<<","<<totalBBox.y()<<","<<totalBBox.width()<<","<<totalBBox.height()<<")\n";
   352 	
   353 		mapRect.setRect (totalBBox.x(), totalBBox.y(), 
   354 			totalBBox.width(), totalBBox.height());
   355 		frame=new QCanvasRectangle (mapRect,mapScene);
   356 		frame->setBrush (QColor(white));
   357 		frame->setPen (QColor(black));
   358 		frame->setZValue(0);
   359 		frame->show();    
   360 	}	
   361 	else	
   362 	{
   363 		setHideTmpMode (HideNone);
   364 	}	
   365 	cout <<"  hidemode="<<hidemode<<endl;
   366 	*/
   367 }	
   368 	
   369 void MapEditor::testFunction2()
   370 {
   371 
   372 /*
   373 	// Toggle hidemode
   374 	if (hidemode==HideExport)
   375 		setHideTmpMode (HideNone);
   376 	else	
   377 		setHideTmpMode (HideExport);
   378 */		
   379 }
   380 
   381 void MapEditor::editHeading()
   382 {
   383 	if (editingHeading)
   384 	{
   385 		editHeadingFinished();
   386 		return;
   387 	}
   388 	BranchObj *bo=model->getSelectedBranchObj();
   389 	BranchItem *bi=model->getSelectedBranchItem();
   390 	if (bo)	//FIXME-3
   391 	{
   392 		model->setSelectionBlocked(true);
   393 
   394 		lineEdit->setText (bi->getHeading());
   395 		QPoint p = mapTo (this,bo->getAbsPos().toPoint() );
   396 		lineEdit->setGeometry(p.x(),p.y(),230,25);
   397 		lineEdit->selectAll();
   398 		lineEdit->show();
   399 		lineEdit->setFocus();
   400 		lineEdit->grabKeyboard();
   401 		editingHeading=true;
   402 	}
   403 
   404 }
   405 void MapEditor::editHeadingFinished()
   406 {
   407 	editingHeading=false;
   408 	lineEdit->releaseKeyboard();
   409 	model->setHeading (lineEdit->text() );
   410 	model->setSelectionBlocked(false);
   411 	lineEdit->hide();
   412 
   413 	// Maybe reselect previous branch 
   414 	mainWindow->editHeadingFinished (model);
   415 }
   416 
   417 
   418 void MapEditor::contextMenuEvent ( QContextMenuEvent * e )
   419 {
   420 	// Lineedits are already closed by preceding
   421 	// mouseEvent, we don't need to close here.
   422 
   423     QPointF p = mapToScene(e->pos());
   424     TreeItem *ti=findMapItem (p, NULL);
   425     LinkableMapObj* lmo=NULL;
   426 	if (ti) lmo=ti->getLMO();	//FIXME-2 get rid of lmo...
   427 	
   428     if (lmo) 
   429 	{	// MapObj was found
   430 		if (model->getSelectedLMO() != lmo)
   431 		{
   432 			// select the MapObj
   433 			model->select(lmo);
   434 		}
   435 		// Context Menu 
   436 		if (model->getSelectedBranchObj() ) 
   437 		{
   438 			// Context Menu on branch or mapcenter
   439 			//FIXME-3 model->updateActions(); needed?
   440 			branchContextMenu->popup(e->globalPos() );
   441 		} else
   442 		{
   443 			if (model->getSelectedFloatImage() )
   444 			{
   445 				// Context Menu on floatimage
   446 				// model->updateActions(); FIXME-3 needed?
   447 				floatimageContextMenu->popup(e->globalPos() );
   448 			}	
   449 		}	
   450 	} else 
   451 	{ // No MapObj found, we are on the Canvas itself
   452 		// Context Menu on scene
   453 		// model->updateActions(); FIXME-3 needed?
   454 		
   455 		// Open context menu synchronously to position new mapcenter
   456 		model->setContextPos (p);
   457 		canvasContextMenu->exec(e->globalPos() );
   458 		model->unsetContextPos ();
   459     } 
   460 	e->accept();
   461 }
   462 
   463 void MapEditor::keyPressEvent(QKeyEvent* e)
   464 {
   465 	if (e->modifiers() & Qt::ControlModifier)
   466 	{
   467 		switch (mainWindow->getModMode())
   468 		{
   469 			case Main::ModModeColor: 
   470 				setCursor (PickColorCursor);
   471 				break;
   472 			case Main::ModModeCopy: 
   473 				setCursor (CopyCursor);
   474 				break;
   475 			case Main::ModModeXLink: 
   476 				setCursor (XLinkCursor);
   477 				break;
   478 			default :
   479 				setCursor (Qt::ArrowCursor);
   480 				break;
   481 		} 
   482 	}	
   483 }
   484 
   485 void MapEditor::keyReleaseEvent(QKeyEvent* e)
   486 {
   487 	if (!(e->modifiers() & Qt::ControlModifier))
   488 		setCursor (Qt::ArrowCursor);
   489 }
   490 
   491 void MapEditor::mousePressEvent(QMouseEvent* e)
   492 {
   493 	// Ignore right clicks, these will go to context menus
   494 	if (e->button() == Qt::RightButton )
   495 	{
   496 		e->ignore();
   497 		return;
   498 	}
   499 
   500 	//Ignore clicks while editing heading
   501 	if (model->isSelectionBlocked() ) 
   502 	{
   503 		e->ignore();
   504 		return;
   505 	}
   506 
   507     QPointF p = mapToScene(e->pos());
   508     TreeItem *ti=findMapItem (p, NULL);
   509     LinkableMapObj* lmo=NULL;
   510 	if (ti) lmo=ti->getLMO();	//FIXME-2 get rid of lmo...
   511 	
   512 	e->accept();
   513 
   514 	//Take care of  system flags _or_ modifier modes
   515 	//
   516 	if (lmo && (typeid(*lmo)==typeid(BranchObj) ||
   517 		typeid(*lmo)==typeid(MapCenterObj) ))
   518 	{
   519 		QString foname=((BranchObj*)lmo)->getSystemFlagName(p);
   520 		if (!foname.isEmpty())
   521 		{
   522 			// systemFlag clicked
   523 			model->select (lmo);	// FIXME-3 was selectInt
   524 			if (foname=="url") 
   525 			{
   526 				if (e->state() & Qt::ControlModifier)
   527 					mainWindow->editOpenURLTab();
   528 				else	
   529 					mainWindow->editOpenURL();
   530 			}	
   531 			else if (foname=="vymLink")
   532 			{
   533 				mainWindow->editOpenVymLink();
   534 				// tabWidget may change, better return now
   535 				// before segfaulting...
   536 			} else if (foname=="note")
   537 				mainWindow->windowToggleNoteEditor();
   538 			else if (foname=="hideInExport")		
   539 				model->toggleHideExport();
   540 			// FIXME-3 needed? xelection.update();	
   541 			return;	
   542 		} 
   543 	}	
   544 	// No system flag clicked, take care of modmodes (CTRL-Click)
   545 	if (e->state() & Qt::ControlModifier)
   546 	{
   547 		if (mainWindow->getModMode()==Main::ModModeColor)
   548 		{
   549 				pickingColor=true;
   550 				setCursor (PickColorCursor);
   551 				return;
   552 		} 
   553 		if (mainWindow->getModMode()==Main::ModModeXLink)
   554 		{	
   555 			BranchObj *bo_begin=NULL;
   556 			if (lmo)
   557 				bo_begin=(BranchObj*)(lmo);
   558 			else	
   559 				bo_begin=model->getSelectedBranchObj();
   560 			if (bo_begin)	
   561 			{
   562 				drawingLink=true;
   563 				linkingObj_src=bo_begin;
   564 				tmpXLink=new XLinkObj (mapScene);
   565 				tmpXLink->setBegin (bo_begin);
   566 				tmpXLink->setEnd   (p);
   567 				tmpXLink->setColor(model->getMapDefXLinkColor());
   568 				tmpXLink->setWidth(model->getMapDefXLinkWidth());
   569 				tmpXLink->updateXLink();
   570 				tmpXLink->setVisibility (true);
   571 				return;
   572 			} 
   573 		}
   574 	}	// End of modmodes
   575 
   576     if (lmo) 
   577 	{	
   578 	/*
   579 		cout << "ME::mouse pressed\n";
   580 		cout << "  lmo="<<lmo<<endl;
   581 		cout << "  h="<<((BranchObj*)lmo)->getHeading().toStdString()<<endl;
   582 	*/
   583 		// Select the clicked object
   584 
   585 		// FIXME-2 VM better let "find" return an index instead of lmo...
   586 		// Get index of clicked LMO
   587 		TreeItem *ti=lmo->getTreeItem();
   588 		/*
   589 		cout << "  lmo="<<lmo<<"    lmo(ti)="<<ti->getLMO()<<endl;
   590 		cout << "  ti ("<<ti->row()<<","<<ti->column()<<") = "<<ti<<endl;
   591 		*/
   592 		//QModelIndex ix=model->index( ti->row(), ti->column(), model->index (0,0,QModelIndex()) );
   593 		model->select (ti);
   594 
   595 		// Left Button	    Move Branches
   596 		if (e->button() == Qt::LeftButton )
   597 		{
   598 			//movingObj_start.setX( p.x() - selection->x() );// TODO replaced selection->lmo here	
   599 			//movingObj_start.setY( p.y() - selection->y() );	
   600 			movingObj_start.setX( p.x() - lmo->x() );	
   601 			movingObj_start.setY( p.y() - lmo->y() );	
   602 			movingObj_orgPos.setX (lmo->x() );
   603 			movingObj_orgPos.setY (lmo->y() );
   604 			lmo->setRelPos();
   605 			movingObj_orgRelPos=lmo->getRelPos();
   606 
   607 			// If modMode==copy, then we want to "move" the _new_ object around
   608 			// then we need the offset from p to the _old_ selection, because of tmp
   609 			if (mainWindow->getModMode()==Main::ModModeCopy &&
   610 				e->state() & Qt::ControlModifier)
   611 			{
   612 				BranchItem *bi=model->getSelectedBranchItem();
   613 				if (bi)
   614 				{
   615 					copyingObj=true;
   616 					//FIXME-2   TreeItem::addBranch (BranchItem still missing) 
   617 					//bi->addBranch (model->getSelectedBranchItem());
   618 					model->unselect();
   619 					model->select(bi->getLastBranch());
   620 					model->reposition();
   621 				}
   622 			} 
   623 
   624 			movingObj=model->getSelectedLMO();	
   625 		} else
   626 			// Middle Button    Toggle Scroll
   627 			// (On Mac OS X this won't work, but we still have 
   628 			// a button in the toolbar)
   629 			if (e->button() == Qt::MidButton )
   630 				model->toggleScroll();
   631 		// model->updateActions(); FIXME-3 needed?
   632 		// FIXME-3 needed? xelection.update();
   633 	} else 
   634 	{ // No MapObj found, we are on the scene itself
   635 		// Left Button	    move Pos of sceneView
   636 		if (e->button() == Qt::LeftButton )
   637 		{
   638 			movingObj=NULL;	// move Content not Obj
   639 			movingObj_start=e->globalPos();
   640 			movingCont_start=QPointF (
   641 				horizontalScrollBar()->value(),
   642 				verticalScrollBar()->value());
   643 			movingVec=QPointF(0,0);
   644 			setCursor(HandOpenCursor);
   645 		} 
   646     } 
   647 }
   648 
   649 void MapEditor::mouseMoveEvent(QMouseEvent* e)
   650 {
   651     QPointF p = mapToScene(e->pos());
   652 	TreeItem *seli=model->getSelectedItem();
   653 	LinkableMapObj* lmosel=NULL;		//FIXME-2 get rid of lmosel
   654 	if (seli)
   655 		lmosel=seli->getLMO();
   656 
   657     // Move the selected MapObj
   658     if ( lmosel && movingObj) 
   659     {	
   660 		// reset cursor if we are moving and don't copy
   661 		if (mainWindow->getModMode()!=Main::ModModeCopy)
   662 			setCursor (Qt::ArrowCursor);
   663 
   664 		// To avoid jumping of the sceneView, only 
   665 		// ensureSelectionVisible, if not tmp linked
   666 		if (!lmosel->hasParObjTmp())
   667 			model->ensureSelectionVisible ();
   668 		
   669 		// Now move the selection, but add relative position 
   670 		// (movingObj_start) where selection was chosen with 
   671 		// mousepointer. (This avoids flickering resp. jumping 
   672 		// of selection back to absPos)
   673 		
   674 		// Check if we could link 
   675 		TreeItem *dsti=findMapItem (p, seli);
   676 		LinkableMapObj* dst=NULL;
   677 		if (dsti) dst=dsti->getLMO();	//FIXME-2 get rid of lmo...
   678 		
   679 
   680 		FloatObj *fio=model->getSelectedFloatImage();
   681 		if (fio)
   682 		{
   683 			fio->move   (p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );		
   684 			fio->setRelPos();
   685 			fio->updateLink(); //no need for reposition, if we update link here
   686 			model->updateSelection();	// position has changed
   687 
   688 			// Relink float to new mapcenter or branch, if shift is pressed	
   689 			// Only relink, if selection really has a new parent
   690 			if ( (e->modifiers()==Qt::ShiftModifier) && dst &&
   691 				( (typeid(*dst)==typeid(BranchObj)) ||
   692 				  (typeid(*dst)==typeid(MapCenterObj)) ) &&
   693 				( dst != fio->getParObj())  
   694 				)
   695 			{
   696 				if (typeid(*fio) == typeid(FloatImageObj) && 
   697 				( (typeid(*dst)==typeid(BranchObj) ||
   698 				  typeid(*dst)==typeid(MapCenterObj)) ))  
   699 				{
   700 
   701 					// Also save the move which was done so far
   702 					QString pold=qpointfToString(movingObj_orgRelPos);
   703 					QString pnow=qpointfToString(fio->getRelPos());
   704 					model->saveState(
   705 						fio->getTreeItem(),  // FIXME-3 
   706 						"moveRel "+pold,
   707 						fio->getTreeItem(),
   708 						"moveRel "+pnow,
   709 						QString("Move %1 to relative position %2").arg(model->getObjectName(fio)).arg(pnow));
   710 					fio->getParObj()->requestReposition();
   711 					model->reposition();
   712 
   713 					model->linkFloatImageTo (model->getSelectString(dst));
   714 					//movingObj=lmosel;
   715 					//movingObj_orgRelPos=lmosel->getRelPos();	
   716 
   717 					model->reposition();
   718 				}	
   719 			}
   720 		} else	
   721 		{	// selection != a FloatObj
   722 			if (lmosel->getDepth()==0)		//FIXME-1 also moved mapcenters could be linked, but not working here...
   723 			{
   724 				// Move MapCenter
   725 				if (e->buttons()== Qt::LeftButton && e->modifiers()==Qt::ShiftModifier) 
   726 					((MapCenterObj*)lmosel)->moveAll(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );		
   727 				else	
   728 					lmosel->move   (p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );		
   729 				model->updateRelPositions();
   730 			} else
   731 			{	
   732 				if (lmosel->getDepth()==1)
   733 				{
   734 					// Move mainbranch
   735 					lmosel->move(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );		
   736 					lmosel->setRelPos();
   737 				} else
   738 				{
   739 					// Move ordinary branch
   740 					if (lmosel->getOrientation() == LinkableMapObj::LeftOfCenter)
   741 						// Add width of bbox here, otherwise alignRelTo will cause jumping around
   742 						lmosel->move(p.x() -movingObj_start.x() , //lmosel->getBBox().width(), 
   743 							p.y()-movingObj_start.y() +lmosel->getTopPad() );		
   744 					else	
   745 						lmosel->move(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() -lmosel->getTopPad());
   746 					lmosel->setRelPos();	
   747 				} 
   748 
   749 				// Maybe we can relink temporary?
   750 				if (dsti && (dst!=lmosel) && seli->isBranchLikeType() && dsti->isBranchLikeType() )
   751 				{
   752 					if (e->modifiers()==Qt::ControlModifier)
   753 					{
   754 						// Special case: CTRL to link below dst
   755 						lmosel->setParObjTmp (dst,p,+1);
   756 					}
   757 					else if (e->modifiers()==Qt::ShiftModifier)
   758 						lmosel->setParObjTmp (dst,p,-1);
   759 					else
   760 						lmosel->setParObjTmp (dst,p,0);
   761 				} else	
   762 				{
   763 					lmosel->unsetParObjTmp();
   764 				}		
   765 				// reposition subbranch
   766 				lmosel->reposition();	
   767 			} // depth>0
   768 
   769 			QItemSelection sel=model->getSelectionModel()->selection();
   770 			updateSelection(sel,sel);	// position has changed
   771 
   772 		} // no FloatImageObj
   773 
   774 		scene()->update();
   775 		return;
   776 	} // selection && moving_obj
   777 		
   778 	// Draw a link from one branch to another
   779 	if (drawingLink)
   780 	{
   781 		 tmpXLink->setEnd (p);
   782 		 tmpXLink->updateXLink();
   783 	}	 
   784 	
   785     // Move sceneView 
   786     if (!movingObj && !pickingColor &&!drawingLink && e->buttons() == Qt::LeftButton ) 
   787 	{
   788 		QPointF p=e->globalPos();
   789 		movingVec.setX(-p.x() + movingObj_start.x() );
   790 		movingVec.setY(-p.y() + movingObj_start.y() );
   791 		horizontalScrollBar()->setSliderPosition((int)( movingCont_start.x()+movingVec.x() ));
   792 		verticalScrollBar()->setSliderPosition((int)( movingCont_start.y()+movingVec.y() ) );
   793     }
   794 }
   795 
   796 
   797 void MapEditor::mouseReleaseEvent(QMouseEvent* e)
   798 {
   799     QPointF p = mapToScene(e->pos());
   800 	TreeItem *dsti=findMapItem(p, NULL);
   801 	LinkableMapObj* dst=NULL;
   802 	if (dsti) dst=dsti->getLMO();	//FIXME-2 get rid of dst...
   803 
   804 	TreeItem *seli=model->getSelectedItem();
   805 	LinkableMapObj *lmosel=seli->getLMO();	// FIXME-2 get rid of lmosel
   806 
   807 
   808 
   809 	// Have we been picking color?
   810 	if (pickingColor)
   811 	{
   812 		pickingColor=false;
   813 		setCursor (Qt::ArrowCursor);
   814 		// Check if we are over another branch
   815 		if (dsti && lmosel) 
   816 		{	
   817 			if (e->state() & Qt::ShiftModifier)
   818 				model->colorBranch (((BranchObj*)dst)->getColor());
   819 			else	
   820 				model->colorSubtree (((BranchObj*)dst)->getColor());
   821 		} 
   822 		return;
   823 	}
   824 
   825 	// Have we been drawing a link?
   826 	if (drawingLink)	
   827 	{
   828 		drawingLink=false;
   829 		// Check if we are over another branch
   830 		if (dsti && lmosel && dsti->isBranchLikeType() ) 
   831 		{	
   832 			tmpXLink->setEnd ( ((BranchObj*)(dst)) );
   833 			tmpXLink->updateXLink();
   834 			tmpXLink->activate(); //FIXME-2 savestate missing
   835 			//model->saveStateComplete(QString("Activate xLink from %1 to %2").arg(model->getObjectName(tmpXLink->getBegin())).arg(model->getObjectName(tmpXLink->getEnd())) );	
   836 		} else
   837 		{
   838 			delete(tmpXLink);
   839 			tmpXLink=NULL;
   840 		}
   841 		return;
   842 	}
   843 	
   844     // Have we been moving something?
   845     if ( seli && movingObj ) 
   846     {	
   847 		if (dsti==seli) 
   848 		{
   849 			dsti=NULL;
   850 			dst=NULL;
   851 		}
   852 		FloatImageObj *fo=model->getSelectedFloatImage();
   853 		if(fo)
   854 		{
   855 			// Moved FloatObj. Maybe we need to reposition
   856 		    QString pold=qpointfToString(movingObj_orgRelPos);
   857 		    QString pnow=qpointfToString(fo->getRelPos());
   858 			model->saveState(
   859 				fo->getTreeItem(),	// FIXME-3
   860 				"moveRel "+pold,
   861 				fo->getTreeItem(),	// FIXME-3
   862 				"moveRel "+pnow,
   863 				QString("Move %1 to relative position %2").arg(model->getObjectName(fo)).arg(pnow));
   864 
   865 			fo->getParObj()->requestReposition();
   866 			model->reposition();
   867 		}	
   868 
   869 		// Check if we are over another branch, but ignore 
   870 		// any found LMOs, which are FloatObjs
   871 
   872 		if (dsti && (!dsti->isBranchLikeType() )) 
   873 		{
   874 			dst=NULL;
   875 			dsti=NULL;
   876 		}	
   877 		
   878 		BranchItem *bi=model->getSelectedBranchItem();
   879 		if (bi && bi->depth()==0)
   880 		{	
   881             if (movingObj_orgPos != bi->getBranchObj()->getAbsPos())	// FIXME-3 check getBO here...
   882             {
   883                 QString pold=qpointfToString(movingObj_orgPos);
   884                 QString pnow=qpointfToString(bi->getBranchObj()->getAbsPos());		// FIXME-3 check getBO here...
   885 
   886                 model->saveState(
   887                     bi,
   888                     "move "+pold,
   889                     bi,
   890                     "move "+pnow,
   891                     QString("Move mapcenter %1 to position %2").arg(model->getObjectName(bi)).arg(pnow));
   892             }
   893 		}
   894 	
   895 		if (seli->getType() == TreeItem::Branch )
   896 		{	// A branch was moved
   897 			
   898 			// save the position in case we link to mapcenter
   899 			QPointF savePos=QPointF (lmosel->getAbsPos()  );
   900 
   901 			// Reset the temporary drawn link to the original one
   902 			lmosel->unsetParObjTmp();
   903 
   904 			// For Redo we may need to save original selection
   905 			QString preSelStr=model->getSelectString(lmosel);
   906 
   907 			copyingObj=false;	
   908 			if (dsti ) 
   909 			{
   910 				// We have a destination, relink to that
   911 
   912 				BranchObj* bsel=model->getSelectedBranchObj();
   913 				TreeItem * tisel=model->getSelectedItem();
   914 				BranchObj* bdst=(BranchObj*)dst;
   915 				TreeItem* tidst=dst->getTreeItem();
   916 
   917 				QString preParStr=model->getSelectString (bsel->getParObj());
   918 				QString preNum=QString::number (tisel->num(),10);
   919 				QString preDstParStr;
   920 
   921 				if (e->state() & Qt::ShiftModifier && dst->getParObj())
   922 				{	// Link above dst
   923 					preDstParStr=model->getSelectString (dst->getParObj());
   924 					bsel->linkTo ( (BranchObj*)(bdst->getParObj()), tidst->num());
   925 				} else 
   926 				if (e->state() & Qt::ControlModifier && dst->getParObj())
   927 				{
   928 					// Link below dst
   929 					preDstParStr=model->getSelectString (dst->getParObj());
   930 					bsel->linkTo ( (BranchObj*)(bdst->getParObj()), tidst->num()+1);
   931 				} else	
   932 				{	// Append to dst
   933 					preDstParStr=model->getSelectString(dst);
   934 					bsel->linkTo (bdst,-1);
   935 					if (dst->getDepth()==0) bsel->move (savePos);
   936 				} 
   937 				QString postSelStr=model->getSelectString(lmosel);
   938 				QString postNum=QString::number (tisel->num(),10);
   939 
   940 				QString undoCom="linkTo (\""+ 
   941 					preParStr+ "\"," + preNum  +"," + 
   942 					QString ("%1,%2").arg(movingObj_orgPos.x()).arg(movingObj_orgPos.y())+ ")";
   943 
   944 				QString redoCom="linkTo (\""+ 
   945 					preDstParStr + "\"," + postNum + "," +
   946 					QString ("%1,%2").arg(savePos.x()).arg(savePos.y())+ ")";
   947 
   948 				model->saveState (
   949 					postSelStr,undoCom,
   950 					preSelStr, redoCom,
   951 					QString("Relink %1 to %2").arg(model->getObjectName(bsel)).arg(model->getObjectName(dst)) );
   952 
   953 				model->reposition();	// not necessary if we undo temporary move  below
   954 			} else
   955 			{
   956 				// No destination, undo  temporary move
   957 
   958 				if (lmosel->getDepth()==1)
   959 				{
   960 					cout << "ME::releaseMouse d=1\n"; // FIXME_1    better use depth
   961 					// The select string might be different _after_ moving around.
   962 					// Therefor reposition and then use string of old selection, too
   963 					model->reposition();
   964 
   965                     QPointF rp(lmosel->getRelPos());
   966                     if (rp != movingObj_orgRelPos)
   967                     {
   968                         QString ps=qpointfToString(rp);
   969                         model->saveState(
   970                             model->getSelectString(lmosel), "moveRel "+qpointfToString(movingObj_orgRelPos), 
   971                             preSelStr, "moveRel "+ps, 
   972                             QString("Move %1 to relative position %2").arg(model->getObjectName(lmosel)).arg(ps));
   973                     }
   974 				}
   975 
   976 				// Draw the original link, before selection was moved around
   977 				if (settings.value("/animation/use",false).toBool() && lmosel->getDepth()>1) 
   978 				{
   979 					lmosel->setRelPos();	// calc relPos first for starting point
   980 					QPointF dst=bi->getBranchObj()->getParObj()->getChildPos();		// FIXME-3 check getBO here...
   981 			//		if (lmosel->getOrientation()==LinkableMapObj::LeftOfCenter) dst.setX (dst.x()+lmosel->width() );
   982 					
   983 					model->startAnimation(
   984 						(BranchObj*)lmosel,
   985 						lmosel->getRelPos(),
   986 						movingObj_orgRelPos
   987 //						QPointF (movingObj_orgPos.x() - dst.x(), movingObj_orgPos.y() - dst.y() )
   988 					);	
   989 				} else	
   990 					model->reposition();
   991 			}
   992 		}
   993 		model->updateSelection();  //FIXME-3 needed? at least not after pos of selection has changed...
   994 		// Finally resize scene, if needed
   995 		scene()->update();
   996 		movingObj=NULL;		
   997 
   998 		// Just make sure, that actions are still ok,e.g. the move branch up/down buttons...
   999 		// model->updateActions(); FIXME-3 neeeded? 
  1000 	} else 
  1001 		// maybe we moved View: set old cursor
  1002 		setCursor (Qt::ArrowCursor);
  1003     
  1004 }
  1005 
  1006 void MapEditor::mouseDoubleClickEvent(QMouseEvent* e)
  1007 {
  1008 	if (model->isSelectionBlocked() ) 
  1009 	{
  1010 		e->ignore();
  1011 		return;
  1012 	}
  1013 
  1014 	if (e->button() == Qt::LeftButton )
  1015 	{
  1016 		QPointF p = mapToScene(e->pos());
  1017 		TreeItem *ti=findMapItem (p, NULL);
  1018 		if (ti) {	// MapObj was found
  1019 			// First select the MapObj than edit heading
  1020 			model->select (ti);
  1021 			editHeading();
  1022 		}
  1023 	}
  1024 }
  1025 
  1026 void MapEditor::resizeEvent (QResizeEvent* e)
  1027 {
  1028 	QGraphicsView::resizeEvent( e );
  1029 }
  1030 
  1031 void MapEditor::dragEnterEvent(QDragEnterEvent *event)
  1032 {
  1033 	//for (unsigned int i=0;event->format(i);i++) // Debug mime type
  1034 	//	cerr << event->format(i) << endl;
  1035 
  1036 	if (event->mimeData()->hasImage())
  1037 		event->acceptProposedAction();
  1038 	else	
  1039 		if (event->mimeData()->hasUrls())
  1040 			event->acceptProposedAction();
  1041 }
  1042 
  1043 void MapEditor::dragMoveEvent(QDragMoveEvent *)
  1044 {
  1045 }
  1046 
  1047 void MapEditor::dragLeaveEvent(QDragLeaveEvent *event)
  1048 {
  1049 	event->accept();
  1050 }
  1051 
  1052 void MapEditor::dropEvent(QDropEvent *event)
  1053 {
  1054 	BranchItem *selbi=model->getSelectedBranchItem();
  1055 	if (selbi)
  1056 	{
  1057 		if (debug)
  1058 			foreach (QString format,event->mimeData()->formats()) 
  1059 				cout << "MapEditor: Dropped format: "<<qPrintable (format)<<endl;
  1060 
  1061 
  1062 		QList <QUrl> uris;
  1063 		if (event->mimeData()->hasImage()) 
  1064 		{
  1065 			 QVariant imageData = event->mimeData()->imageData();
  1066 			 model->addFloatImage (qvariant_cast<QPixmap>(imageData));
  1067 		} else
  1068 		if (event->mimeData()->hasUrls())
  1069 			uris=event->mimeData()->urls();
  1070 
  1071 		if (uris.count()>0)
  1072 		{
  1073 			QStringList files;
  1074 			QString s;
  1075 			QString heading;
  1076 			BranchItem *bi;
  1077 			for (int i=0; i<uris.count();i++)
  1078 			{
  1079 				// Workaround to avoid adding empty branches
  1080 				if (!uris.at(i).toString().isEmpty())
  1081 				{
  1082 					bi=model->createBranch();
  1083 					if (bi)
  1084 					{
  1085 						   /* FIXME-2 
  1086 						s=uris.at(i).toLocalFile();
  1087 						if (!s.isEmpty()) 
  1088 						{
  1089 						   QString file = QDir::fromNativeSeparators(s);
  1090 						   heading = QFileInfo(file).baseName();
  1091 						   files.append(file);
  1092 						   if (file.endsWith(".vym", false))
  1093 							   bi->setVymLink(file);
  1094 						   else
  1095 							   bi->setURL(uris.at(i).toString());
  1096 					   } else 
  1097 					   {
  1098 						   bo->setURL(uris.at(i).toString());
  1099 					   }
  1100 							 */  
  1101 
  1102 					   if (!heading.isEmpty())
  1103 						   bi->setHeading(heading);
  1104 					   else
  1105 						   bi->setHeading(uris.at(i).toString());
  1106 						   
  1107 					}
  1108 				}
  1109 			}
  1110 			model->reposition();
  1111 		}
  1112 	}	
  1113 	event->acceptProposedAction();
  1114 }
  1115 
  1116 void MapEditor::updateSelection(const QItemSelection &newsel,const QItemSelection &)
  1117 {
  1118 	// Reduce rectangles
  1119 	while (newsel.indexes().count() < selboxList.count() )
  1120 		delete selboxList.takeFirst();
  1121 
  1122 	// Add additonal rectangles
  1123 	QGraphicsRectItem *sb;
  1124 	while (newsel.indexes().count() > selboxList.count() )
  1125 	{
  1126 		sb = mapScene->addRect(
  1127 			QRectF(0,0,0,0), 
  1128 			QPen(selectionColor),
  1129 			selectionColor);
  1130 		sb->setZValue(Z_SELBOX);
  1131 		sb->show();
  1132 		selboxList.append (sb);
  1133 	}
  1134 
  1135 	// Reposition rectangles
  1136 	int i=0;
  1137 	QRectF bbox;
  1138 	QModelIndex index;
  1139 
  1140 	TreeItem *ti;
  1141 	LinkableMapObj *lmo;
  1142 	foreach (sb,selboxList)
  1143 	{
  1144 		index=newsel.indexes().at(i);
  1145 		ti= static_cast<TreeItem*>(index.internalPointer());
  1146 		lmo=ti->getLMO();
  1147 		bbox=lmo->getBBox();
  1148 		sb->setRect (
  1149 			bbox.x(),bbox.y(), 
  1150 			bbox.width(), bbox.height());
  1151 		sb->setPen (selectionColor);	
  1152 		sb->setBrush (selectionColor);	
  1153 		i++;
  1154 	}
  1155 }
  1156 
  1157 void MapEditor::updateData (const QModelIndex &sel)
  1158 {
  1159 	TreeItem *ti= static_cast<TreeItem*>(sel.internalPointer());
  1160 
  1161 /* testing
  1162 	cout << "ME::updateData\n";
  1163 
  1164 	cout << "  ti="<<ti<<endl;
  1165 	cout << "  h="<<ti->getHeading().toStdString()<<endl;
  1166 	*/
  1167 	
  1168 	if (ti->isBranchLikeType())
  1169 	{
  1170 		BranchObj *bo=(BranchObj*)ti->getLMO();
  1171 		bo->updateHeading();
  1172 	}
  1173 
  1174 }
  1175 
  1176 void MapEditor::setSelectionColor (QColor col)
  1177 {
  1178 	selectionColor=col;
  1179 	QItemSelection sel=model->getSelectionModel()->selection();
  1180 	updateSelection(sel,sel);
  1181 }
  1182 
  1183 
  1184 QColor MapEditor::getSelectionColor ()
  1185 {
  1186 	return selectionColor;
  1187 }
  1188 
  1189