mapeditor.cpp
author insilmaril
Tue, 28 Apr 2009 09:51:48 +0000
changeset 758 04039e47ac74
parent 756 a8a5c7288f57
child 760 59614eaf5fbb
permissions -rw-r--r--
colors work again
     1 #include "mapeditor.h"
     2 
     3 #include <iostream>
     4 #include <cstdlib>
     5 #include <typeinfo>
     6 
     7 #include <QObject>
     8 
     9 #include "branchitem.h"
    10 #include "mainwindow.h"
    11 #include "misc.h"
    12 #include "warningdialog.h"
    13 
    14 
    15 extern int statusbarTime;
    16 extern Main *mainWindow;
    17 extern QString tmpVymDir;
    18 extern QString clipboardDir;
    19 extern QString clipboardFile;
    20 extern bool clipboardEmpty;
    21 extern bool debug;
    22 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 (seli->depth()==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 (seli->depth()==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 *seli=model->getSelectedItem();
   801 	LinkableMapObj *lmosel=seli->getLMO();	// FIXME-2 get rid of lmosel
   802 
   803 	TreeItem *dsti=findMapItem(p, seli);
   804 	LinkableMapObj* dst=NULL;
   805 	if (dsti) dst=dsti->getLMO();	//FIXME-2 get rid of dst...
   806 
   807 
   808 	// Have we been picking color?
   809 	if (pickingColor)
   810 	{
   811 		pickingColor=false;
   812 		setCursor (Qt::ArrowCursor);
   813 		// Check if we are over another branch
   814 		if (dsti && lmosel) 
   815 		{	
   816 			if (e->state() & Qt::ShiftModifier)
   817 				model->colorBranch (((BranchObj*)dst)->getColor());
   818 			else	
   819 				model->colorSubtree (((BranchObj*)dst)->getColor());
   820 		} 
   821 		return;
   822 	}
   823 
   824 	// Have we been drawing a link?
   825 	if (drawingLink)	
   826 	{
   827 		drawingLink=false;
   828 		// Check if we are over another branch
   829 		if (dsti && lmosel && dsti->isBranchLikeType() ) 
   830 		{	
   831 			tmpXLink->setEnd ( ((BranchObj*)(dst)) );
   832 			tmpXLink->updateXLink();
   833 			tmpXLink->activate(); //FIXME-2 savestate missing
   834 			//model->saveStateComplete(QString("Activate xLink from %1 to %2").arg(model->getObjectName(tmpXLink->getBegin())).arg(model->getObjectName(tmpXLink->getEnd())) );	
   835 		} else
   836 		{
   837 			delete(tmpXLink);
   838 			tmpXLink=NULL;
   839 		}
   840 		return;
   841 	}
   842 	
   843     // Have we been moving something?
   844     if ( seli && movingObj ) 
   845     {	
   846 		if (dsti==seli) 
   847 		{
   848 			dsti=NULL;
   849 			dst=NULL;
   850 		}
   851 		FloatImageObj *fo=model->getSelectedFloatImage();
   852 		if(fo)
   853 		{
   854 			// Moved FloatObj. Maybe we need to reposition
   855 		    QString pold=qpointfToString(movingObj_orgRelPos);
   856 		    QString pnow=qpointfToString(fo->getRelPos());
   857 			model->saveState(
   858 				fo->getTreeItem(),	// FIXME-3
   859 				"moveRel "+pold,
   860 				fo->getTreeItem(),	// FIXME-3
   861 				"moveRel "+pnow,
   862 				QString("Move %1 to relative position %2").arg(model->getObjectName(fo)).arg(pnow));
   863 
   864 			fo->getParObj()->requestReposition();
   865 			model->reposition();
   866 		}	
   867 
   868 		// Check if we are over another branch, but ignore 
   869 		// any found LMOs, which are FloatObjs
   870 
   871 		if (dsti && (!dsti->isBranchLikeType() )) 
   872 		{
   873 			dst=NULL;
   874 			dsti=NULL;
   875 		}	
   876 		
   877 		BranchItem *bi=model->getSelectedBranchItem();
   878 		if (bi && bi->depth()==0)
   879 		{	
   880             if (movingObj_orgPos != bi->getBranchObj()->getAbsPos())	// FIXME-3 check getBO here...
   881             {
   882                 QString pold=qpointfToString(movingObj_orgPos);
   883                 QString pnow=qpointfToString(bi->getBranchObj()->getAbsPos());		// FIXME-3 check getBO here...
   884 
   885                 model->saveState(
   886                     bi,
   887                     "move "+pold,
   888                     bi,
   889                     "move "+pnow,
   890                     QString("Move mapcenter %1 to position %2").arg(model->getObjectName(bi)).arg(pnow));
   891             }
   892 		}
   893 	
   894 		if (seli->getType() == TreeItem::Branch )
   895 		{	// A branch was moved
   896 			
   897 			// save the position in case we link to mapcenter
   898 			QPointF savePos=QPointF (lmosel->getAbsPos()  );
   899 
   900 			// Reset the temporary drawn link to the original one
   901 			lmosel->unsetParObjTmp();
   902 
   903 			// For Redo we may need to save original selection
   904 			QString preSelStr=model->getSelectString(lmosel);
   905 
   906 			copyingObj=false;	
   907 			if (dsti && dsti->isBranchLikeType() ) 
   908 			{
   909 				// We have a destination, relink to that
   910 
   911 				BranchObj* bsel=model->getSelectedBranchObj();
   912 				BranchObj* bdst=(BranchObj*)dst;
   913 
   914 				QString preParStr=model->getSelectString (bsel->getParObj());
   915 				QString preNum=QString::number (seli->num(),10);
   916 				QString preDstParStr;
   917 
   918 				if (e->state() & Qt::ShiftModifier && dst->getParObj())
   919 				{	// Link above dst
   920 					preDstParStr=model->getSelectString (dst->getParObj());
   921 					bsel->linkTo ( (BranchObj*)(bdst->getParObj()), seli->num());	//FIXME-2 not needed
   922 					model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti->parent(),((BranchItem*)dsti)->num());
   923 				} else 
   924 				if (e->state() & Qt::ControlModifier && dst->getParObj())
   925 				{
   926 					// Link below dst
   927 					preDstParStr=model->getSelectString (dst->getParObj());
   928 					bsel->linkTo ( (BranchObj*)(bdst->getParObj()), seli->num()+1);//FIXME-2 not needed
   929 					model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti->parent(),((BranchItem*)seli)->num()+1);
   930 				} else	
   931 				{	// Append to dst
   932 					preDstParStr=model->getSelectString(dst);
   933 					bsel->linkTo (bdst,-1);//FIXME-2 not needed
   934 					model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti);
   935 					if (dsti->depth()==0) bsel->move (savePos);
   936 				} 
   937 				QString postSelStr=model->getSelectString(lmosel);
   938 				QString postNum=QString::number (seli->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 (seli->depth()==1)
   959 				{
   960 					// The select string might be different _after_ moving around.
   961 					// Therefor reposition and then use string of old selection, too
   962 					model->reposition();
   963 
   964                     QPointF rp(lmosel->getRelPos());
   965                     if (rp != movingObj_orgRelPos)
   966                     {
   967                         QString ps=qpointfToString(rp);
   968                         model->saveState(
   969                             model->getSelectString(lmosel), "moveRel "+qpointfToString(movingObj_orgRelPos), 
   970                             preSelStr, "moveRel "+ps, 
   971                             QString("Move %1 to relative position %2").arg(model->getObjectName(lmosel)).arg(ps));
   972                     }
   973 				}
   974 
   975 				// Draw the original link, before selection was moved around
   976 				if (settings.value("/animation/use",false).toBool() && seli->depth()>1) 
   977 				{
   978 					lmosel->setRelPos();	// calc relPos first for starting point
   979 					QPointF dst=bi->getBranchObj()->getParObj()->getChildPos();		// FIXME-3 check getBO here...
   980 			//		if (lmosel->getOrientation()==LinkableMapObj::LeftOfCenter) dst.setX (dst.x()+lmosel->width() );
   981 					
   982 					model->startAnimation(
   983 						(BranchObj*)lmosel,
   984 						lmosel->getRelPos(),
   985 						movingObj_orgRelPos
   986 //						QPointF (movingObj_orgPos.x() - dst.x(), movingObj_orgPos.y() - dst.y() )
   987 					);	
   988 				} else	
   989 					model->reposition();
   990 			}
   991 		}
   992 		model->updateSelection();  //FIXME-3 needed? at least not after pos of selection has changed...
   993 		// Finally resize scene, if needed
   994 		scene()->update();
   995 		movingObj=NULL;		
   996 
   997 		// Just make sure, that actions are still ok,e.g. the move branch up/down buttons...
   998 		// model->updateActions(); FIXME-3 neeeded? 
   999 	} else 
  1000 		// maybe we moved View: set old cursor
  1001 		setCursor (Qt::ArrowCursor);
  1002     
  1003 }
  1004 
  1005 void MapEditor::mouseDoubleClickEvent(QMouseEvent* e)
  1006 {
  1007 	if (model->isSelectionBlocked() ) 
  1008 	{
  1009 		e->ignore();
  1010 		return;
  1011 	}
  1012 
  1013 	if (e->button() == Qt::LeftButton )
  1014 	{
  1015 		QPointF p = mapToScene(e->pos());
  1016 		TreeItem *ti=findMapItem (p, NULL);
  1017 		if (ti) {	// MapObj was found
  1018 			// First select the MapObj than edit heading
  1019 			model->select (ti);
  1020 			editHeading();
  1021 		}
  1022 	}
  1023 }
  1024 
  1025 void MapEditor::resizeEvent (QResizeEvent* e)
  1026 {
  1027 	QGraphicsView::resizeEvent( e );
  1028 }
  1029 
  1030 void MapEditor::dragEnterEvent(QDragEnterEvent *event)
  1031 {
  1032 	//for (unsigned int i=0;event->format(i);i++) // Debug mime type
  1033 	//	cerr << event->format(i) << endl;
  1034 
  1035 	if (event->mimeData()->hasImage())
  1036 		event->acceptProposedAction();
  1037 	else	
  1038 		if (event->mimeData()->hasUrls())
  1039 			event->acceptProposedAction();
  1040 }
  1041 
  1042 void MapEditor::dragMoveEvent(QDragMoveEvent *)
  1043 {
  1044 }
  1045 
  1046 void MapEditor::dragLeaveEvent(QDragLeaveEvent *event)
  1047 {
  1048 	event->accept();
  1049 }
  1050 
  1051 void MapEditor::dropEvent(QDropEvent *event)
  1052 {
  1053 	BranchItem *selbi=model->getSelectedBranchItem();
  1054 	if (selbi)
  1055 	{
  1056 		if (debug)
  1057 			foreach (QString format,event->mimeData()->formats()) 
  1058 				cout << "MapEditor: Dropped format: "<<qPrintable (format)<<endl;
  1059 
  1060 
  1061 		QList <QUrl> uris;
  1062 		if (event->mimeData()->hasImage()) 
  1063 		{
  1064 			 QVariant imageData = event->mimeData()->imageData();
  1065 			 model->addFloatImage (qvariant_cast<QPixmap>(imageData));
  1066 		} else
  1067 		if (event->mimeData()->hasUrls())
  1068 			uris=event->mimeData()->urls();
  1069 
  1070 		if (uris.count()>0)
  1071 		{
  1072 			QStringList files;
  1073 			QString s;
  1074 			QString heading;
  1075 			BranchItem *bi;
  1076 			for (int i=0; i<uris.count();i++)
  1077 			{
  1078 				// Workaround to avoid adding empty branches
  1079 				if (!uris.at(i).toString().isEmpty())
  1080 				{
  1081 					bi=model->createBranch();
  1082 					if (bi)
  1083 					{
  1084 						   /* FIXME-2 
  1085 						s=uris.at(i).toLocalFile();
  1086 						if (!s.isEmpty()) 
  1087 						{
  1088 						   QString file = QDir::fromNativeSeparators(s);
  1089 						   heading = QFileInfo(file).baseName();
  1090 						   files.append(file);
  1091 						   if (file.endsWith(".vym", false))
  1092 							   bi->setVymLink(file);
  1093 						   else
  1094 							   bi->setURL(uris.at(i).toString());
  1095 					   } else 
  1096 					   {
  1097 						   bo->setURL(uris.at(i).toString());
  1098 					   }
  1099 							 */  
  1100 
  1101 					   if (!heading.isEmpty())
  1102 						   bi->setHeading(heading);
  1103 					   else
  1104 						   bi->setHeading(uris.at(i).toString());
  1105 						   
  1106 					}
  1107 				}
  1108 			}
  1109 			model->reposition();
  1110 		}
  1111 	}	
  1112 	event->acceptProposedAction();
  1113 }
  1114 
  1115 void MapEditor::updateSelection(const QItemSelection &newsel,const QItemSelection &)
  1116 {
  1117 	// Reduce rectangles
  1118 	while (newsel.indexes().count() < selboxList.count() )
  1119 		delete selboxList.takeFirst();
  1120 
  1121 	// Add additonal rectangles
  1122 	QGraphicsRectItem *sb;
  1123 	while (newsel.indexes().count() > selboxList.count() )
  1124 	{
  1125 		sb = mapScene->addRect(
  1126 			QRectF(0,0,0,0), 
  1127 			QPen(selectionColor),
  1128 			selectionColor);
  1129 		sb->setZValue(Z_SELBOX);
  1130 		sb->show();
  1131 		selboxList.append (sb);
  1132 	}
  1133 
  1134 	// Reposition rectangles
  1135 	int i=0;
  1136 	QRectF bbox;
  1137 	QModelIndex index;
  1138 
  1139 	TreeItem *ti;
  1140 	LinkableMapObj *lmo;
  1141 	foreach (sb,selboxList)
  1142 	{
  1143 		index=newsel.indexes().at(i);
  1144 		ti= static_cast<TreeItem*>(index.internalPointer());
  1145 		lmo=ti->getLMO();
  1146 		bbox=lmo->getBBox();
  1147 		sb->setRect (
  1148 			bbox.x(),bbox.y(), 
  1149 			bbox.width(), bbox.height());
  1150 		sb->setPen (selectionColor);	
  1151 		sb->setBrush (selectionColor);	
  1152 		i++;
  1153 	}
  1154 }
  1155 
  1156 void MapEditor::updateData (const QModelIndex &sel)
  1157 {
  1158 	TreeItem *ti= static_cast<TreeItem*>(sel.internalPointer());
  1159 
  1160 /* testing
  1161 	cout << "ME::updateData\n";
  1162 
  1163 	cout << "  ti="<<ti<<endl;
  1164 	cout << "  h="<<ti->getHeading().toStdString()<<endl;
  1165 	*/
  1166 	
  1167 	if (ti->isBranchLikeType())
  1168 	{
  1169 		BranchObj *bo=(BranchObj*)ti->getLMO();
  1170 		bo->updateHeading();
  1171 	}
  1172 
  1173 }
  1174 
  1175 void MapEditor::setSelectionColor (QColor col)
  1176 {
  1177 	selectionColor=col;
  1178 	QItemSelection sel=model->getSelectionModel()->selection();
  1179 	updateSelection(sel,sel);
  1180 }
  1181 
  1182 
  1183 QColor MapEditor::getSelectionColor ()
  1184 {
  1185 	return selectionColor;
  1186 }
  1187 
  1188