mapeditor.cpp
author insilmaril
Thu, 07 May 2009 08:48:53 +0000
changeset 766 7a71a914afdb
parent 763 8c028a5d9083
child 767 6d2b32f305f9
permissions -rw-r--r--
Started to reanimate flags
     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 TreeItem* MapEditor::findMapItem (QPointF p,TreeItem *exclude)
   254 {
   255 	// Start with mapcenter, no images allowed at rootItem
   256 	int i=0;
   257 	BranchItem *bi=model->getRootItem()->getFirstBranch();
   258 	TreeItem *found=NULL;
   259 	while (bi)
   260 	{
   261 		found=bi->findMapItem (p, exclude);
   262 		if (found) return found;
   263 		i++;
   264 		bi=model->getRootItem()->getBranchNum(i);
   265 	}
   266 	return NULL;
   267 }
   268 
   269 AttributeTable* MapEditor::attributeTable()
   270 {
   271 	return attrTable;
   272 }
   273 
   274 void MapEditor::testFunction1()
   275 {
   276 	BranchItem *cur=NULL;
   277 	BranchItem *prev=NULL;
   278 	int d;
   279 	cout << "ME::testFunction1  starting to walk the map...\n";
   280 	while (model->next (cur,prev,d) )
   281 		cout << "*** " <<cur->getHeading().toStdString()<<endl;
   282 
   283 
   284 	//BranchObj *bo=model->getSelectedBranchObj();
   285 	//if (bo) model->moveAway (bo);
   286 	//if (bo) bo->setLinkStyle (LinkableMapObj::Line);
   287 	
   288 
   289 /*
   290 	// Displacement and animation of all non-mainbranches
   291 	QPointF p;
   292 	QPointF q;
   293 	BranchObj *bo;
   294 	TreeItem *cur=NULL;
   295 	TreeItem *prev=NULL;
   296 	int d;
   297 	while (cur) 
   298 	{
   299 		bo=(BranchObj*)(cur->getLMO());
   300 
   301 		if (cur->depth() >0 && !bo->hasScrolledParent(bo) )
   302 		{
   303 			p=QPointF (qrand() %600-300, qrand () %600-300);
   304 			bo->setRelPos();
   305 			q=bo->getRelPos();
   306 			model->startAnimation (bo,p, q);
   307 		}
   308 		model->next(cur,prev,d);
   309 	}
   310 */
   311 
   312 
   313 /* TODO Hide hidden stuff temporary, maybe add this as regular function somewhere
   314 	if (hidemode==HideNone)
   315 	{
   316 		setHideTmpMode (HideExport);
   317 		mapCenter->calcBBoxSizeWithChilds();
   318 		QRectF totalBBox=mapCenter->getTotalBBox();
   319 		QRectF mapRect=totalBBox;
   320 		QCanvasRectangle *frame=NULL;
   321 
   322 		cout << "  map has =("<<totalBBox.x()<<","<<totalBBox.y()<<","<<totalBBox.width()<<","<<totalBBox.height()<<")\n";
   323 	
   324 		mapRect.setRect (totalBBox.x(), totalBBox.y(), 
   325 			totalBBox.width(), totalBBox.height());
   326 		frame=new QCanvasRectangle (mapRect,mapScene);
   327 		frame->setBrush (QColor(white));
   328 		frame->setPen (QColor(black));
   329 		frame->setZValue(0);
   330 		frame->show();    
   331 	}	
   332 	else	
   333 	{
   334 		setHideTmpMode (HideNone);
   335 	}	
   336 	cout <<"  hidemode="<<hidemode<<endl;
   337 	*/
   338 }	
   339 	
   340 void MapEditor::testFunction2()
   341 {
   342 
   343 /*
   344 	// Toggle hidemode
   345 	if (hidemode==HideExport)
   346 		setHideTmpMode (HideNone);
   347 	else	
   348 		setHideTmpMode (HideExport);
   349 */		
   350 }
   351 
   352 void MapEditor::editHeading()
   353 {
   354 	if (editingHeading)
   355 	{
   356 		editHeadingFinished();
   357 		return;
   358 	}
   359 	BranchObj *bo=model->getSelectedBranchObj();
   360 	BranchItem *bi=model->getSelectedBranchItem();
   361 	if (bo)	//FIXME-3
   362 	{
   363 		model->setSelectionBlocked(true);
   364 
   365 		lineEdit->setText (bi->getHeading());
   366 		QPoint p = mapTo (this,bo->getAbsPos().toPoint() );
   367 		lineEdit->setGeometry(p.x(),p.y(),230,25);
   368 		lineEdit->selectAll();
   369 		lineEdit->show();
   370 		lineEdit->setFocus();
   371 		lineEdit->grabKeyboard();
   372 		editingHeading=true;
   373 	}
   374 
   375 }
   376 void MapEditor::editHeadingFinished()
   377 {
   378 	editingHeading=false;
   379 	lineEdit->releaseKeyboard();
   380 	model->setHeading (lineEdit->text() );
   381 	model->setSelectionBlocked(false);
   382 	lineEdit->hide();
   383 
   384 	// Maybe reselect previous branch 
   385 	mainWindow->editHeadingFinished (model);
   386 }
   387 
   388 
   389 void MapEditor::contextMenuEvent ( QContextMenuEvent * e )
   390 {
   391 	// Lineedits are already closed by preceding
   392 	// mouseEvent, we don't need to close here.
   393 
   394     QPointF p = mapToScene(e->pos());
   395     TreeItem *ti=findMapItem (p, NULL);
   396     LinkableMapObj* lmo=NULL;
   397 	if (ti) lmo=ti->getLMO();	//FIXME-2 get rid of lmo...
   398 	
   399     if (lmo) 
   400 	{	// MapObj was found
   401 		if (model->getSelectedLMO() != lmo)
   402 		{
   403 			// select the MapObj
   404 			model->select(lmo);
   405 		}
   406 		// Context Menu 
   407 		if (model->getSelectedBranchObj() ) 
   408 		{
   409 			// Context Menu on branch or mapcenter
   410 			//FIXME-3 model->updateActions(); needed?
   411 			branchContextMenu->popup(e->globalPos() );
   412 		} else
   413 		{
   414 			if (model->getSelectedFloatImage() )
   415 			{
   416 				// Context Menu on floatimage
   417 				// model->updateActions(); FIXME-3 needed?
   418 				floatimageContextMenu->popup(e->globalPos() );
   419 			}	
   420 		}	
   421 	} else 
   422 	{ // No MapObj found, we are on the Canvas itself
   423 		// Context Menu on scene
   424 		// model->updateActions(); FIXME-3 needed?
   425 		
   426 		// Open context menu synchronously to position new mapcenter
   427 		model->setContextPos (p);
   428 		canvasContextMenu->exec(e->globalPos() );
   429 		model->unsetContextPos ();
   430     } 
   431 	e->accept();
   432 }
   433 
   434 void MapEditor::keyPressEvent(QKeyEvent* e)
   435 {
   436 	if (e->modifiers() & Qt::ControlModifier)
   437 	{
   438 		switch (mainWindow->getModMode())
   439 		{
   440 			case Main::ModModeColor: 
   441 				setCursor (PickColorCursor);
   442 				break;
   443 			case Main::ModModeCopy: 
   444 				setCursor (CopyCursor);
   445 				break;
   446 			case Main::ModModeXLink: 
   447 				setCursor (XLinkCursor);
   448 				break;
   449 			default :
   450 				setCursor (Qt::ArrowCursor);
   451 				break;
   452 		} 
   453 	}	
   454 }
   455 
   456 void MapEditor::keyReleaseEvent(QKeyEvent* e)
   457 {
   458 	if (!(e->modifiers() & Qt::ControlModifier))
   459 		setCursor (Qt::ArrowCursor);
   460 }
   461 
   462 void MapEditor::mousePressEvent(QMouseEvent* e)
   463 {
   464 	// Ignore right clicks, these will go to context menus
   465 	if (e->button() == Qt::RightButton )
   466 	{
   467 		e->ignore();
   468 		return;
   469 	}
   470 
   471 	//Ignore clicks while editing heading
   472 	if (model->isSelectionBlocked() ) 
   473 	{
   474 		e->ignore();
   475 		return;
   476 	}
   477 
   478     QPointF p = mapToScene(e->pos());
   479     TreeItem *ti=findMapItem (p, NULL);
   480     LinkableMapObj* lmo=NULL;
   481 	if (ti) lmo=ti->getLMO();	//FIXME-2 get rid of lmo...
   482 	
   483 	e->accept();
   484 
   485 	//Take care of  system flags _or_ modifier modes
   486 	//
   487 	if (lmo && (typeid(*lmo)==typeid(BranchObj) ||
   488 		typeid(*lmo)==typeid(MapCenterObj) ))
   489 	{
   490 		QString foname=((BranchObj*)lmo)->getSystemFlagName(p);
   491 		if (!foname.isEmpty())
   492 		{
   493 			// systemFlag clicked
   494 			model->select (lmo);	// FIXME-3 was selectInt
   495 			if (foname=="url") 
   496 			{
   497 				if (e->state() & Qt::ControlModifier)
   498 					mainWindow->editOpenURLTab();
   499 				else	
   500 					mainWindow->editOpenURL();
   501 			}	
   502 			else if (foname=="vymLink")
   503 			{
   504 				mainWindow->editOpenVymLink();
   505 				// tabWidget may change, better return now
   506 				// before segfaulting...
   507 			} else if (foname=="note")
   508 				mainWindow->windowToggleNoteEditor();
   509 			else if (foname=="hideInExport")		
   510 				model->toggleHideExport();
   511 			// FIXME-3 needed? xelection.update();	
   512 			return;	
   513 		} 
   514 	}	
   515 	// No system flag clicked, take care of modmodes (CTRL-Click)
   516 	if (e->state() & Qt::ControlModifier)
   517 	{
   518 		if (mainWindow->getModMode()==Main::ModModeColor)
   519 		{
   520 				pickingColor=true;
   521 				setCursor (PickColorCursor);
   522 				return;
   523 		} 
   524 		if (mainWindow->getModMode()==Main::ModModeXLink)
   525 		{	
   526 			BranchObj *bo_begin=NULL;
   527 			if (lmo)
   528 				bo_begin=(BranchObj*)(lmo);
   529 			else	
   530 				bo_begin=model->getSelectedBranchObj();
   531 			if (bo_begin)	
   532 			{
   533 				drawingLink=true;
   534 				linkingObj_src=bo_begin;
   535 				tmpXLink=new XLinkObj (mapScene);
   536 				tmpXLink->setBegin (bo_begin);
   537 				tmpXLink->setEnd   (p);
   538 				tmpXLink->setColor(model->getMapDefXLinkColor());
   539 				tmpXLink->setWidth(model->getMapDefXLinkWidth());
   540 				tmpXLink->updateXLink();
   541 				tmpXLink->setVisibility (true);
   542 				return;
   543 			} 
   544 		}
   545 	}	// End of modmodes
   546 
   547     if (lmo) 
   548 	{	
   549 	/*
   550 		cout << "ME::mouse pressed\n";
   551 		cout << "  lmo="<<lmo<<endl;
   552 		cout << "  h="<<((BranchObj*)lmo)->getHeading().toStdString()<<endl;
   553 	*/
   554 		// Select the clicked object
   555 
   556 		// FIXME-2 VM better let "find" return an index instead of lmo...
   557 		// Get index of clicked LMO
   558 		TreeItem *ti=lmo->getTreeItem();
   559 		/*
   560 		cout << "  lmo="<<lmo<<"    lmo(ti)="<<ti->getLMO()<<endl;
   561 		cout << "  ti ("<<ti->row()<<","<<ti->column()<<") = "<<ti<<endl;
   562 		*/
   563 		//QModelIndex ix=model->index( ti->row(), ti->column(), model->index (0,0,QModelIndex()) );
   564 		model->select (ti);
   565 
   566 		// Left Button	    Move Branches
   567 		if (e->button() == Qt::LeftButton )
   568 		{
   569 			//movingObj_start.setX( p.x() - selection->x() );// TODO replaced selection->lmo here	
   570 			//movingObj_start.setY( p.y() - selection->y() );	
   571 			movingObj_start.setX( p.x() - lmo->x() );	
   572 			movingObj_start.setY( p.y() - lmo->y() );	
   573 			movingObj_orgPos.setX (lmo->x() );
   574 			movingObj_orgPos.setY (lmo->y() );
   575 			lmo->setRelPos();
   576 			movingObj_orgRelPos=lmo->getRelPos();
   577 
   578 			// If modMode==copy, then we want to "move" the _new_ object around
   579 			// then we need the offset from p to the _old_ selection, because of tmp
   580 			if (mainWindow->getModMode()==Main::ModModeCopy &&
   581 				e->state() & Qt::ControlModifier)
   582 			{
   583 				BranchItem *bi=model->getSelectedBranchItem();
   584 				if (bi)
   585 				{
   586 					copyingObj=true;
   587 					//FIXME-2   TreeItem::addBranch (BranchItem still missing) 
   588 					//bi->addBranch (model->getSelectedBranchItem());
   589 					model->unselect();
   590 					model->select(bi->getLastBranch());
   591 					model->reposition();
   592 				}
   593 			} 
   594 
   595 			movingObj=model->getSelectedLMO();	
   596 		} else
   597 			// Middle Button    Toggle Scroll
   598 			// (On Mac OS X this won't work, but we still have 
   599 			// a button in the toolbar)
   600 			if (e->button() == Qt::MidButton )
   601 				model->toggleScroll();
   602 		// model->updateActions(); FIXME-3 needed?
   603 		// FIXME-3 needed? xelection.update();
   604 	} else 
   605 	{ // No MapObj found, we are on the scene itself
   606 		// Left Button	    move Pos of sceneView
   607 		if (e->button() == Qt::LeftButton )
   608 		{
   609 			movingObj=NULL;	// move Content not Obj
   610 			movingObj_start=e->globalPos();
   611 			movingCont_start=QPointF (
   612 				horizontalScrollBar()->value(),
   613 				verticalScrollBar()->value());
   614 			movingVec=QPointF(0,0);
   615 			setCursor(HandOpenCursor);
   616 		} 
   617     } 
   618 }
   619 
   620 void MapEditor::mouseMoveEvent(QMouseEvent* e)
   621 {
   622     QPointF p = mapToScene(e->pos());
   623 	TreeItem *seli=model->getSelectedItem();
   624 	LinkableMapObj* lmosel=NULL;		//FIXME-2 get rid of lmosel
   625 	if (seli)
   626 		lmosel=seli->getLMO();
   627 
   628     // Move the selected MapObj
   629     if ( lmosel && movingObj) 
   630     {	
   631 		// reset cursor if we are moving and don't copy
   632 		if (mainWindow->getModMode()!=Main::ModModeCopy)
   633 			setCursor (Qt::ArrowCursor);
   634 
   635 		// To avoid jumping of the sceneView, only 
   636 		// show selection, if not tmp linked
   637 		if (!lmosel->hasParObjTmp())
   638 			model->emitShowSelection();
   639 		
   640 		// Now move the selection, but add relative position 
   641 		// (movingObj_start) where selection was chosen with 
   642 		// mousepointer. (This avoids flickering resp. jumping 
   643 		// of selection back to absPos)
   644 		
   645 		// Check if we could link 
   646 		TreeItem *dsti=findMapItem (p, seli);
   647 		LinkableMapObj* dst=NULL;
   648 		if (dsti) dst=dsti->getLMO();	//FIXME-2 get rid of lmo...
   649 		
   650 
   651 		FloatObj *fio=model->getSelectedFloatImage();
   652 		if (fio)
   653 		{
   654 			fio->move   (p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );		
   655 			fio->setRelPos();
   656 			fio->updateLink(); //no need for reposition, if we update link here
   657 			model->updateSelection();	// position has changed
   658 
   659 			// Relink float to new mapcenter or branch, if shift is pressed	
   660 			// Only relink, if selection really has a new parent
   661 			if ( (e->modifiers()==Qt::ShiftModifier) && dst &&
   662 				( (typeid(*dst)==typeid(BranchObj)) ||
   663 				  (typeid(*dst)==typeid(MapCenterObj)) ) &&
   664 				( dst != fio->getParObj())  
   665 				)
   666 			{
   667 				if (typeid(*fio) == typeid(FloatImageObj) && 
   668 				( (typeid(*dst)==typeid(BranchObj) ||
   669 				  typeid(*dst)==typeid(MapCenterObj)) ))  
   670 				{
   671 
   672 					// Also save the move which was done so far
   673 					QString pold=qpointfToString(movingObj_orgRelPos);
   674 					QString pnow=qpointfToString(fio->getRelPos());
   675 					model->saveState(
   676 						fio->getTreeItem(),  // FIXME-3 
   677 						"moveRel "+pold,
   678 						fio->getTreeItem(),
   679 						"moveRel "+pnow,
   680 						QString("Move %1 to relative position %2").arg(model->getObjectName(fio)).arg(pnow));
   681 					fio->getParObj()->requestReposition();
   682 					model->reposition();
   683 
   684 					model->linkFloatImageTo (model->getSelectString(dst));
   685 					//movingObj=lmosel;
   686 					//movingObj_orgRelPos=lmosel->getRelPos();	
   687 
   688 					model->reposition();
   689 				}	
   690 			}
   691 		} else	
   692 		{	// selection != a FloatObj
   693 			if (seli->depth()==0)		//FIXME-1 also moved mapcenters could be linked, but not working here...
   694 			{
   695 				// Move MapCenter
   696 				if (e->buttons()== Qt::LeftButton && e->modifiers()==Qt::ShiftModifier) 
   697 					((MapCenterObj*)lmosel)->moveAll(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );		
   698 				else	
   699 					lmosel->move   (p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );		
   700 				model->updateRelPositions();
   701 			} else
   702 			{	
   703 				if (seli->depth()==1)
   704 				{
   705 					// Move mainbranch
   706 					lmosel->move(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );		
   707 					lmosel->setRelPos();
   708 				} else
   709 				{
   710 					// Move ordinary branch
   711 					if (lmosel->getOrientation() == LinkableMapObj::LeftOfCenter)
   712 						// Add width of bbox here, otherwise alignRelTo will cause jumping around
   713 						lmosel->move(p.x() -movingObj_start.x() , //lmosel->getBBox().width(), 
   714 							p.y()-movingObj_start.y() +lmosel->getTopPad() );		
   715 					else	
   716 						lmosel->move(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() -lmosel->getTopPad());
   717 					lmosel->setRelPos();	
   718 				} 
   719 
   720 				// Maybe we can relink temporary?
   721 				if (dsti && (dst!=lmosel) && seli->isBranchLikeType() && dsti->isBranchLikeType() )
   722 				{
   723 					if (e->modifiers()==Qt::ControlModifier)
   724 					{
   725 						// Special case: CTRL to link below dst
   726 						lmosel->setParObjTmp (dst,p,+1);
   727 					}
   728 					else if (e->modifiers()==Qt::ShiftModifier)
   729 						lmosel->setParObjTmp (dst,p,-1);
   730 					else
   731 						lmosel->setParObjTmp (dst,p,0);
   732 				} else	
   733 				{
   734 					lmosel->unsetParObjTmp();
   735 				}		
   736 				// reposition subbranch
   737 				lmosel->reposition();	
   738 			} // depth>0
   739 
   740 			QItemSelection sel=model->getSelectionModel()->selection();
   741 			updateSelection(sel,sel);	// position has changed
   742 
   743 		} // no FloatImageObj
   744 
   745 		scene()->update();
   746 		return;
   747 	} // selection && moving_obj
   748 		
   749 	// Draw a link from one branch to another
   750 	if (drawingLink)
   751 	{
   752 		 tmpXLink->setEnd (p);
   753 		 tmpXLink->updateXLink();
   754 	}	 
   755 	
   756     // Move sceneView 
   757     if (!movingObj && !pickingColor &&!drawingLink && e->buttons() == Qt::LeftButton ) 
   758 	{
   759 		QPointF p=e->globalPos();
   760 		movingVec.setX(-p.x() + movingObj_start.x() );
   761 		movingVec.setY(-p.y() + movingObj_start.y() );
   762 		horizontalScrollBar()->setSliderPosition((int)( movingCont_start.x()+movingVec.x() ));
   763 		verticalScrollBar()->setSliderPosition((int)( movingCont_start.y()+movingVec.y() ) );
   764     }
   765 }
   766 
   767 
   768 void MapEditor::mouseReleaseEvent(QMouseEvent* e)
   769 {
   770     QPointF p = mapToScene(e->pos());
   771 	TreeItem *seli=model->getSelectedItem();
   772 
   773 	TreeItem *dsti=findMapItem(p, seli);
   774 	LinkableMapObj* dst=NULL;
   775 	if (dsti) dst=dsti->getLMO();	//FIXME-2 get rid of dst...
   776 
   777 
   778 	// Have we been picking color?
   779 	if (pickingColor)
   780 	{
   781 		pickingColor=false;
   782 		setCursor (Qt::ArrowCursor);
   783 		// Check if we are over another branch
   784 		if (dsti && seli) 
   785 		{	
   786 			if (e->state() & Qt::ShiftModifier)
   787 				model->colorBranch (((BranchObj*)dst)->getColor());
   788 			else	
   789 				model->colorSubtree (((BranchObj*)dst)->getColor());
   790 		} 
   791 		return;
   792 	}
   793 
   794 	// Have we been drawing a link?
   795 	if (drawingLink)	
   796 	{
   797 		drawingLink=false;
   798 		// Check if we are over another branch
   799 		if (dsti && seli && dsti->isBranchLikeType() ) 
   800 		{	
   801 			tmpXLink->setEnd ( ((BranchObj*)(dst)) );
   802 			tmpXLink->updateXLink();
   803 			tmpXLink->activate(); //FIXME-2 savestate missing
   804 			//model->saveStateComplete(QString("Activate xLink from %1 to %2").arg(model->getObjectName(tmpXLink->getBegin())).arg(model->getObjectName(tmpXLink->getEnd())) );	
   805 		} else
   806 		{
   807 			delete(tmpXLink);
   808 			tmpXLink=NULL;
   809 		}
   810 		return;
   811 	}
   812 	
   813     // Have we been moving something?
   814     if ( seli && movingObj ) 
   815     {	
   816 		if (dsti==seli) 
   817 		{
   818 			dsti=NULL;
   819 			dst=NULL;
   820 		}
   821 		FloatImageObj *fo=model->getSelectedFloatImage();
   822 		if(fo)
   823 		{
   824 			// Moved FloatObj. Maybe we need to reposition
   825 		    QString pold=qpointfToString(movingObj_orgRelPos);
   826 		    QString pnow=qpointfToString(fo->getRelPos());
   827 			model->saveState(
   828 				fo->getTreeItem(),	// FIXME-3
   829 				"moveRel "+pold,
   830 				fo->getTreeItem(),	// FIXME-3
   831 				"moveRel "+pnow,
   832 				QString("Move %1 to relative position %2").arg(model->getObjectName(fo)).arg(pnow));
   833 
   834 			fo->getParObj()->requestReposition();
   835 			model->reposition();
   836 		}	
   837 
   838 		// Check if we are over another branch, but ignore 
   839 		// any found LMOs, which are FloatObjs
   840 
   841 		if (dsti && (!dsti->isBranchLikeType() )) 
   842 		{
   843 			dst=NULL;
   844 			dsti=NULL;
   845 		}	
   846 		
   847 		BranchItem *bi=model->getSelectedBranchItem();
   848 		if (bi && bi->depth()==0)
   849 		{	
   850             if (movingObj_orgPos != bi->getBranchObj()->getAbsPos())	// FIXME-3 check getBO here...
   851             {
   852                 QString pold=qpointfToString(movingObj_orgPos);
   853                 QString pnow=qpointfToString(bi->getBranchObj()->getAbsPos());		// FIXME-3 check getBO here...
   854 
   855                 model->saveState(
   856                     bi,
   857                     "move "+pold,
   858                     bi,
   859                     "move "+pnow,
   860                     QString("Move mapcenter %1 to position %2").arg(model->getObjectName(bi)).arg(pnow));
   861             }
   862 		}
   863 	
   864 		if (seli->getType() == TreeItem::Branch )
   865 		{	// A branch was moved
   866 			LinkableMapObj* lmosel=NULL;		//FIXME-2 get rid of lmosel
   867 			lmosel=seli->getLMO();
   868 				
   869 			// save the position in case we link to mapcenter
   870 			QPointF savePos=QPointF (lmosel->getAbsPos()  );
   871 
   872 			// Reset the temporary drawn link to the original one
   873 			lmosel->unsetParObjTmp();
   874 
   875 			// For Redo we may need to save original selection
   876 			QString preSelStr=model->getSelectString(lmosel);
   877 
   878 			copyingObj=false;	
   879 			if (dsti && dsti->isBranchLikeType() ) 
   880 			{
   881 				// We have a destination, relink to that
   882 
   883 				BranchObj* bsel=model->getSelectedBranchObj();
   884 
   885 				QString preParStr=model->getSelectString (bsel->getParObj());
   886 				QString preNum=QString::number (seli->num(),10);
   887 				QString preDstParStr;
   888 
   889 				if (e->state() & Qt::ShiftModifier && dst->getParObj())
   890 				{	// Link above dst
   891 					preDstParStr=model->getSelectString (dst->getParObj());
   892 					model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti->parent(),((BranchItem*)dsti)->num());
   893 				} else 
   894 				if (e->state() & Qt::ControlModifier && dst->getParObj())
   895 				{
   896 					// Link below dst
   897 					preDstParStr=model->getSelectString (dst->getParObj());
   898 					model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti->parent(),((BranchItem*)seli)->num()+1);
   899 				} else	
   900 				{	// Append to dst
   901 					preDstParStr=model->getSelectString(dst);
   902 					model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti);
   903 					if (dsti->depth()==0) bsel->move (savePos);
   904 				} 
   905 				QString postSelStr=model->getSelectString(lmosel);
   906 				QString postNum=QString::number (seli->num(),10);
   907 
   908 				QString undoCom="linkTo (\""+ 
   909 					preParStr+ "\"," + preNum  +"," + 
   910 					QString ("%1,%2").arg(movingObj_orgPos.x()).arg(movingObj_orgPos.y())+ ")";
   911 
   912 				QString redoCom="linkTo (\""+ 
   913 					preDstParStr + "\"," + postNum + "," +
   914 					QString ("%1,%2").arg(savePos.x()).arg(savePos.y())+ ")";
   915 
   916 				model->saveState (
   917 					postSelStr,undoCom,
   918 					preSelStr, redoCom,
   919 					QString("Relink %1 to %2").arg(model->getObjectName(bsel)).arg(model->getObjectName(dst)) );
   920 
   921 				model->reposition();	// not necessary if we undo temporary move  below
   922 			} else
   923 			{
   924 				// No destination, undo  temporary move
   925 
   926 				if (seli->depth()==1)
   927 				{
   928 					// The select string might be different _after_ moving around.
   929 					// Therefor reposition and then use string of old selection, too
   930 					model->reposition();
   931 
   932                     QPointF rp(lmosel->getRelPos());
   933                     if (rp != movingObj_orgRelPos)
   934                     {
   935                         QString ps=qpointfToString(rp);
   936                         model->saveState(
   937                             model->getSelectString(lmosel), "moveRel "+qpointfToString(movingObj_orgRelPos), 
   938                             preSelStr, "moveRel "+ps, 
   939                             QString("Move %1 to relative position %2").arg(model->getObjectName(lmosel)).arg(ps));
   940                     }
   941 				}
   942 
   943 				// Draw the original link, before selection was moved around
   944 				if (settings.value("/animation/use",false).toBool() && seli->depth()>1) 
   945 				{
   946 					lmosel->setRelPos();	// calc relPos first for starting point
   947 					QPointF dst=bi->getBranchObj()->getParObj()->getChildPos();		// FIXME-3 check getBO here...
   948 			//		if (lmosel->getOrientation()==LinkableMapObj::LeftOfCenter) dst.setX (dst.x()+lmosel->width() );
   949 					
   950 					model->startAnimation(
   951 						(BranchObj*)lmosel,
   952 						lmosel->getRelPos(),
   953 						movingObj_orgRelPos
   954 //						QPointF (movingObj_orgPos.x() - dst.x(), movingObj_orgPos.y() - dst.y() )
   955 					);	
   956 				} else	
   957 					model->reposition();
   958 			}
   959 		}
   960 		model->updateSelection();  //FIXME-3 needed? at least not after pos of selection has changed...
   961 		// Finally resize scene, if needed
   962 		scene()->update();
   963 		movingObj=NULL;		
   964 
   965 		// Just make sure, that actions are still ok,e.g. the move branch up/down buttons...
   966 		// model->updateActions(); FIXME-3 neeeded? 
   967 	} else 
   968 		// maybe we moved View: set old cursor
   969 		setCursor (Qt::ArrowCursor);
   970     
   971 }
   972 
   973 void MapEditor::mouseDoubleClickEvent(QMouseEvent* e)
   974 {
   975 	if (model->isSelectionBlocked() ) 
   976 	{
   977 		e->ignore();
   978 		return;
   979 	}
   980 
   981 	if (e->button() == Qt::LeftButton )
   982 	{
   983 		QPointF p = mapToScene(e->pos());
   984 		TreeItem *ti=findMapItem (p, NULL);
   985 		if (ti) {	// MapObj was found
   986 			// First select the MapObj than edit heading
   987 			model->select (ti);
   988 			editHeading();
   989 		}
   990 	}
   991 }
   992 
   993 void MapEditor::resizeEvent (QResizeEvent* e)
   994 {
   995 	QGraphicsView::resizeEvent( e );
   996 }
   997 
   998 void MapEditor::dragEnterEvent(QDragEnterEvent *event)
   999 {
  1000 	//for (unsigned int i=0;event->format(i);i++) // Debug mime type
  1001 	//	cerr << event->format(i) << endl;
  1002 
  1003 	if (event->mimeData()->hasImage())
  1004 		event->acceptProposedAction();
  1005 	else	
  1006 		if (event->mimeData()->hasUrls())
  1007 			event->acceptProposedAction();
  1008 }
  1009 
  1010 void MapEditor::dragMoveEvent(QDragMoveEvent *)
  1011 {
  1012 }
  1013 
  1014 void MapEditor::dragLeaveEvent(QDragLeaveEvent *event)
  1015 {
  1016 	event->accept();
  1017 }
  1018 
  1019 void MapEditor::dropEvent(QDropEvent *event)
  1020 {
  1021 	BranchItem *selbi=model->getSelectedBranchItem();
  1022 	if (selbi)
  1023 	{
  1024 		if (debug)
  1025 			foreach (QString format,event->mimeData()->formats()) 
  1026 				cout << "MapEditor: Dropped format: "<<qPrintable (format)<<endl;
  1027 
  1028 
  1029 		QList <QUrl> uris;
  1030 		if (event->mimeData()->hasImage()) 
  1031 		{
  1032 			 QVariant imageData = event->mimeData()->imageData();
  1033 			 model->addFloatImage (qvariant_cast<QPixmap>(imageData));
  1034 		} else
  1035 		if (event->mimeData()->hasUrls())
  1036 			uris=event->mimeData()->urls();
  1037 
  1038 		if (uris.count()>0)
  1039 		{
  1040 			QStringList files;
  1041 			QString s;
  1042 			QString heading;
  1043 			BranchItem *bi;
  1044 			for (int i=0; i<uris.count();i++)
  1045 			{
  1046 				// Workaround to avoid adding empty branches
  1047 				if (!uris.at(i).toString().isEmpty())
  1048 				{
  1049 					bi=model->createBranch();
  1050 					if (bi)
  1051 					{
  1052 						   /* FIXME-2 
  1053 						s=uris.at(i).toLocalFile();
  1054 						if (!s.isEmpty()) 
  1055 						{
  1056 						   QString file = QDir::fromNativeSeparators(s);
  1057 						   heading = QFileInfo(file).baseName();
  1058 						   files.append(file);
  1059 						   if (file.endsWith(".vym", false))
  1060 							   bi->setVymLink(file);
  1061 						   else
  1062 							   bi->setURL(uris.at(i).toString());
  1063 					   } else 
  1064 					   {
  1065 						   bo->setURL(uris.at(i).toString());
  1066 					   }
  1067 							 */  
  1068 
  1069 					   if (!heading.isEmpty())
  1070 						   bi->setHeading(heading);
  1071 					   else
  1072 						   bi->setHeading(uris.at(i).toString());
  1073 						   
  1074 					}
  1075 				}
  1076 			}
  1077 			model->reposition();
  1078 		}
  1079 	}	
  1080 	event->acceptProposedAction();
  1081 }
  1082 
  1083 void MapEditor::updateSelection(const QItemSelection &newsel,const QItemSelection &)
  1084 {
  1085 	// Reduce rectangles
  1086 	while (newsel.indexes().count() < selboxList.count() )
  1087 		delete selboxList.takeFirst();
  1088 
  1089 	// Add additonal rectangles
  1090 	QGraphicsRectItem *sb;
  1091 	while (newsel.indexes().count() > selboxList.count() )
  1092 	{
  1093 		sb = mapScene->addRect(
  1094 			QRectF(0,0,0,0), 
  1095 			QPen(selectionColor),
  1096 			selectionColor);
  1097 		sb->setZValue(Z_SELBOX);
  1098 		sb->show();
  1099 		selboxList.append (sb);
  1100 	}
  1101 
  1102 	// Reposition rectangles
  1103 	int i=0;
  1104 	QRectF bbox;
  1105 	QModelIndex index;
  1106 
  1107 	TreeItem *ti;
  1108 	LinkableMapObj *lmo;
  1109 	foreach (sb,selboxList)
  1110 	{
  1111 		index=newsel.indexes().at(i);
  1112 		ti= static_cast<TreeItem*>(index.internalPointer());
  1113 		lmo=ti->getLMO();
  1114 		bbox=lmo->getBBox();
  1115 		sb->setRect (
  1116 			bbox.x(),bbox.y(), 
  1117 			bbox.width(), bbox.height());
  1118 		sb->setPen (selectionColor);	
  1119 		sb->setBrush (selectionColor);	
  1120 		i++;
  1121 	}
  1122 }
  1123 
  1124 void MapEditor::updateData (const QModelIndex &sel)
  1125 {
  1126 	TreeItem *ti= static_cast<TreeItem*>(sel.internalPointer());
  1127 
  1128 /* testing
  1129 	cout << "ME::updateData\n";
  1130 
  1131 	cout << "  ti="<<ti<<endl;
  1132 	cout << "  h="<<ti->getHeading().toStdString()<<endl;
  1133 	*/
  1134 	
  1135 	if (ti->isBranchLikeType())
  1136 	{
  1137 		BranchObj *bo=(BranchObj*)ti->getLMO();
  1138 		bo->updateHeading();
  1139 	}
  1140 
  1141 }
  1142 
  1143 void MapEditor::setSelectionColor (QColor col)
  1144 {
  1145 	selectionColor=col;
  1146 	QItemSelection sel=model->getSelectionModel()->selection();
  1147 	updateSelection(sel,sel);
  1148 }
  1149 
  1150 
  1151 QColor MapEditor::getSelectionColor ()
  1152 {
  1153 	return selectionColor;
  1154 }
  1155 
  1156