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