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