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