mapeditor.cpp
author insilmaril
Tue, 09 Mar 2010 08:28:49 +0000
changeset 830 b5537d245165
parent 824 36eb4b8f409e
child 834 0fad394bc330
permissions -rw-r--r--
Minor bugfix: editSortBack is no toggle action
insilmaril@815
     1
#include "mapeditor.h"
insilmaril@0
     2
insilmaril@823
     3
#include <iostream>	
insilmaril@0
     4
insilmaril@720
     5
#include <QObject>
insilmaril@720
     6
insilmaril@750
     7
#include "branchitem.h"
insilmaril@792
     8
#include "geometry.h"
insilmaril@175
     9
#include "mainwindow.h"
insilmaril@175
    10
#include "misc.h"
insilmaril@314
    11
#include "warningdialog.h"
insilmaril@790
    12
#include "xlinkitem.h"
insilmaril@0
    13
insilmaril@0
    14
insilmaril@0
    15
extern int statusbarTime;
insilmaril@0
    16
extern Main *mainWindow;
insilmaril@164
    17
extern QString tmpVymDir;
insilmaril@166
    18
extern QString clipboardDir;
insilmaril@491
    19
extern QString clipboardFile;
insilmaril@166
    20
extern bool clipboardEmpty;
insilmaril@473
    21
extern bool debug;
insilmaril@0
    22
insilmaril@366
    23
extern QMenu* branchContextMenu;
insilmaril@366
    24
extern QMenu* floatimageContextMenu;
insilmaril@366
    25
extern QMenu* canvasContextMenu;
insilmaril@366
    26
insilmaril@0
    27
extern Settings settings;
insilmaril@269
    28
extern QString iconPath;
insilmaril@0
    29
insilmaril@0
    30
///////////////////////////////////////////////////////////////////////
insilmaril@0
    31
///////////////////////////////////////////////////////////////////////
insilmaril@720
    32
MapEditor::MapEditor( VymModel *vm) 
insilmaril@0
    33
{
insilmaril@0
    34
	//cout << "Constructor ME "<<this<<endl;
insilmaril@720
    35
	mapScene= new QGraphicsScene(NULL);
insilmaril@408
    36
	mapScene->setBackgroundBrush (QBrush(Qt::white, Qt::SolidPattern));
insilmaril@408
    37
insilmaril@767
    38
	zoomFactor=zoomFactorTarget=1;
insilmaril@767
    39
insilmaril@720
    40
	model=vm;
insilmaril@645
    41
	model->setScene (mapScene);
insilmaril@720
    42
	model->registerEditor(this);
insilmaril@727
    43
	model->makeDefault();	// No changes in model so far
insilmaril@408
    44
insilmaril@408
    45
    setScene (mapScene);
insilmaril@0
    46
insilmaril@0
    47
    printer=NULL;
insilmaril@0
    48
insilmaril@366
    49
	// Create bitmap cursors, platform dependant
insilmaril@404
    50
	HandOpenCursor=QCursor (QPixmap(iconPath+"cursorhandopen.png"),1,1);		
insilmaril@404
    51
	PickColorCursor=QCursor ( QPixmap(iconPath+"cursorcolorpicker.png"), 5,27 ); 
insilmaril@404
    52
	CopyCursor=QCursor ( QPixmap(iconPath+"cursorcopy.png"), 1,1 ); 
insilmaril@404
    53
	XLinkCursor=QCursor ( QPixmap(iconPath+"cursorxlink.png"), 1,7 ); 
insilmaril@398
    54
insilmaril@769
    55
	//setFocusPolicy (Qt::StrongFocus);	//FIXME-3
insilmaril@0
    56
insilmaril@0
    57
	pickingColor=false;
insilmaril@91
    58
	drawingLink=false;
insilmaril@93
    59
	copyingObj=false;
insilmaril@798
    60
	objectMoved=false;
insilmaril@0
    61
insilmaril@0
    62
    editingBO=NULL;
insilmaril@0
    63
    movingObj=NULL;
insilmaril@0
    64
insilmaril@0
    65
	printFrame=true;
insilmaril@0
    66
	printFooter=true;
insilmaril@0
    67
insilmaril@417
    68
	setAcceptDrops (true);	
insilmaril@0
    69
insilmaril@746
    70
	//model->reposition();	//FIXME-3 really still needed?
insilmaril@381
    71
insilmaril@531
    72
insilmaril@769
    73
	// Shortcuts and actions
insilmaril@769
    74
	QAction *a;
insilmaril@769
    75
    a = new QAction("Select upper branch", this);
insilmaril@769
    76
	a->setShortcut (Qt::Key_Up );
insilmaril@769
    77
	a->setShortcutContext (Qt::WidgetShortcut);
insilmaril@769
    78
	addAction (a);
insilmaril@769
    79
    connect( a, SIGNAL( triggered() ), this, SLOT( cursorUp() ) );
insilmaril@769
    80
insilmaril@769
    81
    a = new QAction( "Select lower branch",this);
insilmaril@769
    82
	a->setShortcut ( Qt::Key_Down );
insilmaril@769
    83
	a->setShortcutContext (Qt::WidgetShortcut);
insilmaril@769
    84
	addAction (a);
insilmaril@769
    85
    connect( a, SIGNAL( triggered() ), this, SLOT( cursorDown() ) );
insilmaril@769
    86
insilmaril@769
    87
    a = new QAction( "Select left branch", this);
insilmaril@769
    88
	a->setShortcut (Qt::Key_Left );
insilmaril@769
    89
//	a->setShortcutContext (Qt::WindowShortcut);
insilmaril@823
    90
//	a->setShortcutContext (Qt::WidgetWithChildrenShortcut);
insilmaril@769
    91
	addAction (a);
insilmaril@769
    92
    connect( a, SIGNAL( triggered() ), this, SLOT( cursorLeft() ) );
insilmaril@769
    93
insilmaril@769
    94
    a = new QAction( "Select child branch", this);
insilmaril@769
    95
	a->setShortcut (Qt::Key_Right);
insilmaril@823
    96
//	a->setShortcutContext (Qt::WidgetWithChildrenShortcut);
insilmaril@769
    97
	addAction (a);
insilmaril@769
    98
    connect( a, SIGNAL( triggered() ), this, SLOT( cursorRight() ) );
insilmaril@769
    99
insilmaril@769
   100
    a = new QAction(  "Select first branch", this);
insilmaril@769
   101
	a->setShortcut (Qt::Key_Home );
insilmaril@769
   102
	a->setShortcutContext (Qt::WidgetWithChildrenShortcut);
insilmaril@769
   103
	addAction (a);
insilmaril@769
   104
    connect( a, SIGNAL( triggered() ), this, SLOT( cursorFirst() ) );
insilmaril@769
   105
insilmaril@769
   106
    a = new QAction( "Select last branch",this);
insilmaril@769
   107
	a->setShortcut ( Qt::Key_End );
insilmaril@769
   108
	a->setShortcutContext (Qt::WidgetWithChildrenShortcut);
insilmaril@769
   109
	addAction (a);
insilmaril@769
   110
    connect( a, SIGNAL( triggered() ), this, SLOT( cursorLast() ) );
insilmaril@769
   111
insilmaril@720
   112
	// Action to embed LineEdit for heading in Scene
insilmaril@720
   113
	editingHeading=false;
insilmaril@819
   114
	lineEdit=NULL;
insilmaril@676
   115
insilmaril@769
   116
	a = new QAction( tr( "Edit heading","MapEditor" ), this);
insilmaril@720
   117
	a->setShortcut ( Qt::Key_Return );					//Edit heading
insilmaril@810
   118
	a->setShortcutContext (Qt::WidgetShortcut);
insilmaril@720
   119
	addAction (a);
insilmaril@720
   120
    connect( a, SIGNAL( triggered() ), this, SLOT( editHeading() ) );
insilmaril@720
   121
	a = new QAction( tr( "Edit heading","MapEditor" ), this);
insilmaril@720
   122
	a->setShortcut ( Qt::Key_Enter);					//Edit heading
insilmaril@720
   123
	addAction (a);
insilmaril@720
   124
    connect( a, SIGNAL( triggered() ), this, SLOT( editHeading() ) );
insilmaril@628
   125
insilmaril@728
   126
	// Selections
insilmaril@728
   127
	selectionColor =QColor (255,255,0);
insilmaril@728
   128
	
insilmaril@728
   129
insilmaril@746
   130
	// Attributes	//FIXME-2 testing only...
insilmaril@638
   131
	QString k;
insilmaril@638
   132
	AttributeDef *ad;
insilmaril@628
   133
	attrTable= new AttributeTable();
insilmaril@638
   134
	k="A - StringList";
insilmaril@638
   135
	ad=attrTable->addKey (k,StringList);
insilmaril@638
   136
	if (ad)
insilmaril@638
   137
	{
insilmaril@638
   138
		QStringList sl;
insilmaril@638
   139
		sl <<"val 1"<<"val 2"<< "val 3";
insilmaril@638
   140
		ad->setValue (QVariant (sl));
insilmaril@638
   141
	}
insilmaril@638
   142
	//attrTable->addValue ("Key A","P 1");
insilmaril@638
   143
	//attrTable->addValue ("Key A","P 2");
insilmaril@638
   144
	//attrTable->addValue ("Key A","P 3");
insilmaril@638
   145
	//attrTable->addValue ("Key A","P 4");
insilmaril@638
   146
	k="B - FreeString";
insilmaril@638
   147
	ad=attrTable->addKey (k,FreeString);
insilmaril@638
   148
	if (ad)
insilmaril@638
   149
	{
insilmaril@638
   150
		//attrTable->addValue ("Key B","w1");
insilmaril@638
   151
		//attrTable->addValue ("Key B","w2");
insilmaril@638
   152
	}
insilmaril@638
   153
	k="C - UniqueString";
insilmaril@638
   154
	ad=attrTable->addKey (k,UniqueString);
insilmaril@638
   155
	if (ad)
insilmaril@638
   156
	{
insilmaril@638
   157
	//attrTable->addKey ("Key Prio");
insilmaril@638
   158
	//attrTable->addValue ("Key Prio","Prio 1");
insilmaril@638
   159
	//attrTable->addValue ("Key Prio","Prio 2");
insilmaril@638
   160
	}
insilmaril@0
   161
}
insilmaril@0
   162
insilmaril@0
   163
MapEditor::~MapEditor()
insilmaril@0
   164
{
insilmaril@746
   165
	//cout <<"Destructor MapEditor for "<<model->getMapName().toStdString()<<endl;
insilmaril@720
   166
	model->unregisterEditor(this);
insilmaril@0
   167
}
insilmaril@0
   168
insilmaril@645
   169
VymModel* MapEditor::getModel()
insilmaril@0
   170
{
insilmaril@645
   171
    return model;
insilmaril@0
   172
}
insilmaril@0
   173
insilmaril@408
   174
QGraphicsScene * MapEditor::getScene()
insilmaril@0
   175
{
insilmaril@408
   176
    return mapScene;
insilmaril@0
   177
}
insilmaril@0
   178
insilmaril@802
   179
void MapEditor::scrollTo (const QModelIndex &index)	
insilmaril@772
   180
{
insilmaril@772
   181
	if (index.isValid())
insilmaril@772
   182
	{
insilmaril@776
   183
		LinkableMapObj* lmo=NULL;
insilmaril@776
   184
		TreeItem *ti= static_cast<TreeItem*>(index.internalPointer());
insilmaril@776
   185
		if (ti->getType()==TreeItem::Image ||ti->isBranchLikeType() )
insilmaril@779
   186
			lmo=((MapItem*)ti)->getLMO();
insilmaril@798
   187
		if (lmo) 
insilmaril@798
   188
		{
insilmaril@798
   189
			QRectF r=lmo->getBBox();
insilmaril@802
   190
			setScrollBarPosTarget (r);
insilmaril@798
   191
		}	
insilmaril@772
   192
	}
insilmaril@772
   193
}
insilmaril@772
   194
insilmaril@767
   195
void MapEditor::setScrollBarPosTarget (const QRectF &rect)
insilmaril@767
   196
{
insilmaril@767
   197
	// Code copied from Qt sources
insilmaril@767
   198
	int xmargin=50;
insilmaril@767
   199
	int ymargin=50;
insilmaril@767
   200
insilmaril@767
   201
    qreal width = viewport()->width();
insilmaril@767
   202
    qreal height = viewport()->height();
insilmaril@767
   203
    QRectF viewRect = matrix().mapRect(rect);
insilmaril@767
   204
insilmaril@767
   205
    qreal left = horizontalScrollBar()->value();
insilmaril@767
   206
    qreal right = left + width;
insilmaril@767
   207
    qreal top = verticalScrollBar()->value();
insilmaril@767
   208
    qreal bottom = top + height;
insilmaril@767
   209
insilmaril@802
   210
	scrollBarPosTarget=getScrollBarPos();
insilmaril@802
   211
insilmaril@767
   212
    if (viewRect.left() <= left + xmargin) {
insilmaril@767
   213
        // need to scroll from the left
insilmaril@767
   214
  //      if (!d->leftIndent)
insilmaril@767
   215
            scrollBarPosTarget.setX(int(viewRect.left() - xmargin - 0.5));
insilmaril@767
   216
    }
insilmaril@767
   217
    if (viewRect.right() >= right - xmargin) {
insilmaril@767
   218
        // need to scroll from the right
insilmaril@767
   219
//        if (!d->leftIndent)
insilmaril@767
   220
            scrollBarPosTarget.setX(int(viewRect.right() - width + xmargin + 0.5));
insilmaril@767
   221
    }
insilmaril@767
   222
    if (viewRect.top() <= top + ymargin) {
insilmaril@767
   223
        // need to scroll from the top
insilmaril@767
   224
   //     if (!d->topIndent)
insilmaril@767
   225
            scrollBarPosTarget.setY(int(viewRect.top() - ymargin - 0.5));
insilmaril@767
   226
    }
insilmaril@767
   227
    if (viewRect.bottom() >= bottom - ymargin) {
insilmaril@767
   228
        // need to scroll from the bottom
insilmaril@767
   229
//        if (!d->topIndent)
insilmaril@767
   230
            scrollBarPosTarget.setY(int(viewRect.bottom() - height + ymargin + 0.5));
insilmaril@767
   231
    }
insilmaril@767
   232
insilmaril@802
   233
	if (scrollBarPosTarget==getScrollBarPos()) return;
insilmaril@802
   234
insilmaril@767
   235
	if (scrollBarPosAnimation.state()==QtAbstractAnimation::Running)
insilmaril@767
   236
		scrollBarPosAnimation.stop();
insilmaril@802
   237
	
insilmaril@802
   238
	if (settings.value ("/animation/use/",true).toBool() )
insilmaril@802
   239
	{
insilmaril@802
   240
		scrollBarPosAnimation.setTargetObject (this);
insilmaril@802
   241
		scrollBarPosAnimation.setPropertyName ("scrollBarPos");
insilmaril@802
   242
		scrollBarPosAnimation.setDuration(1000);
insilmaril@802
   243
		scrollBarPosAnimation.setEasingCurve ( QtEasingCurve::OutQuint);
insilmaril@802
   244
		scrollBarPosAnimation.setStartValue(
insilmaril@802
   245
			QPointF (horizontalScrollBar()->value() ,
insilmaril@802
   246
					 verticalScrollBar()->value() ) );
insilmaril@802
   247
		scrollBarPosAnimation.setEndValue(scrollBarPosTarget);
insilmaril@802
   248
		scrollBarPosAnimation.start();
insilmaril@802
   249
	} else
insilmaril@802
   250
		setScrollBarPos (scrollBarPosTarget);
insilmaril@767
   251
}
insilmaril@767
   252
insilmaril@767
   253
QPointF MapEditor::getScrollBarPosTarget()
insilmaril@767
   254
{
insilmaril@767
   255
    return scrollBarPosTarget;
insilmaril@767
   256
}
insilmaril@767
   257
insilmaril@767
   258
insilmaril@767
   259
void MapEditor::setScrollBarPos(const QPointF &p)
insilmaril@767
   260
{
insilmaril@767
   261
    scrollBarPos=p;
insilmaril@767
   262
	horizontalScrollBar()->setValue(int(p.x()));
insilmaril@767
   263
	verticalScrollBar()->setValue(int(p.y()));
insilmaril@767
   264
}
insilmaril@767
   265
insilmaril@767
   266
QPointF MapEditor::getScrollBarPos()
insilmaril@767
   267
{
insilmaril@802
   268
	return QPointF (horizontalScrollBar()->value(),verticalScrollBar()->value());
insilmaril@802
   269
    //return scrollBarPos;
insilmaril@767
   270
}
insilmaril@767
   271
insilmaril@767
   272
void MapEditor::setZoomFactorTarget (const qreal &zft)
insilmaril@767
   273
{
insilmaril@767
   274
	zoomFactorTarget=zft;
insilmaril@767
   275
	if (zoomAnimation.state()==QtAbstractAnimation::Running)
insilmaril@767
   276
		zoomAnimation.stop();
insilmaril@802
   277
	if (settings.value ("/animation/use/",true).toBool() )
insilmaril@802
   278
	{
insilmaril@815
   279
		//zoomAnimation=QPropertyAnimation(this, "zoomFactor");
insilmaril@802
   280
		zoomAnimation.setTargetObject (this);
insilmaril@802
   281
		zoomAnimation.setPropertyName ("zoomFactor");
insilmaril@802
   282
		zoomAnimation.setDuration(1000);
insilmaril@802
   283
		zoomAnimation.setEasingCurve ( QtEasingCurve::OutQuint);
insilmaril@802
   284
		zoomAnimation.setStartValue(zoomFactor);
insilmaril@802
   285
		zoomAnimation.setEndValue(zft);
insilmaril@802
   286
		zoomAnimation.start();
insilmaril@802
   287
	} else
insilmaril@802
   288
		setZoomFactor (zft);
insilmaril@767
   289
}
insilmaril@767
   290
insilmaril@767
   291
qreal MapEditor::getZoomFactorTarget()
insilmaril@767
   292
{
insilmaril@767
   293
    return zoomFactorTarget;
insilmaril@767
   294
}
insilmaril@767
   295
insilmaril@767
   296
insilmaril@767
   297
void MapEditor::setZoomFactor(const qreal &zf)
insilmaril@767
   298
{
insilmaril@767
   299
    zoomFactor=zf;
insilmaril@767
   300
	setMatrix (QMatrix(zf, 0, 0, zf, 0, 0),false );
insilmaril@767
   301
}
insilmaril@767
   302
insilmaril@767
   303
qreal MapEditor::getZoomFactor()
insilmaril@767
   304
{
insilmaril@767
   305
    return zoomFactor;
insilmaril@767
   306
}
insilmaril@767
   307
insilmaril@0
   308
void MapEditor::print()
insilmaril@0
   309
{
insilmaril@0
   310
	if ( !printer ) 
insilmaril@0
   311
	{
insilmaril@0
   312
		printer = new QPrinter;
insilmaril@0
   313
		printer->setColorMode (QPrinter::Color);
insilmaril@366
   314
		printer->setPrinterName (settings.value("/mainwindow/printerName",printer->printerName()).toString());
insilmaril@412
   315
		printer->setOutputFormat((QPrinter::OutputFormat)settings.value("/mainwindow/printerFormat",printer->outputFormat()).toInt());
insilmaril@412
   316
		printer->setOutputFileName(settings.value("/mainwindow/printerFileName",printer->outputFileName()).toString());
insilmaril@0
   317
	}
insilmaril@0
   318
insilmaril@782
   319
	QRectF totalBBox=getTotalBBox();
insilmaril@0
   320
insilmaril@0
   321
	// Try to set orientation automagically
insilmaril@0
   322
	// Note: Interpretation of generated postscript is amibiguous, if 
insilmaril@0
   323
	// there are problems with landscape mode, see
insilmaril@0
   324
	// http://sdb.suse.de/de/sdb/html/jsmeix_print-cups-landscape-81.html
insilmaril@0
   325
insilmaril@0
   326
	if (totalBBox.width()>totalBBox.height())
insilmaril@0
   327
		// recommend landscape
insilmaril@0
   328
		printer->setOrientation (QPrinter::Landscape);
insilmaril@0
   329
	else	
insilmaril@0
   330
		// recommend portrait
insilmaril@0
   331
		printer->setOrientation (QPrinter::Portrait);
insilmaril@0
   332
insilmaril@0
   333
	if ( printer->setup(this) ) 
insilmaril@0
   334
	// returns false, if printing is canceled
insilmaril@0
   335
	{
insilmaril@0
   336
		QPainter pp(printer);
insilmaril@0
   337
insilmaril@412
   338
		pp.setRenderHint(QPainter::Antialiasing,true);
insilmaril@412
   339
insilmaril@0
   340
		// Don't print the visualisation of selection
insilmaril@720
   341
		model->unselect();
insilmaril@0
   342
insilmaril@408
   343
		QRectF mapRect=totalBBox;
insilmaril@411
   344
		QGraphicsRectItem *frame=NULL;
insilmaril@412
   345
insilmaril@417
   346
		if (printFrame) 
insilmaril@0
   347
		{
insilmaril@417
   348
			// Print frame around map
insilmaril@417
   349
			mapRect.setRect (totalBBox.x()-10, totalBBox.y()-10, 
insilmaril@417
   350
				totalBBox.width()+20, totalBBox.height()+20);
insilmaril@417
   351
			frame=mapScene->addRect (mapRect, QPen(Qt::black),QBrush(Qt::NoBrush));
insilmaril@417
   352
			frame->setZValue(0);
insilmaril@417
   353
			frame->show();    
insilmaril@417
   354
		}		
insilmaril@417
   355
insilmaril@417
   356
insilmaril@412
   357
		double paperAspect = (double)printer->width()   / (double)printer->height();
insilmaril@412
   358
		double   mapAspect = (double)mapRect.width() / (double)mapRect.height();
insilmaril@417
   359
		int viewBottom;
insilmaril@0
   360
		if (mapAspect>=paperAspect)
insilmaril@0
   361
		{
insilmaril@0
   362
			// Fit horizontally to paper width
insilmaril@417
   363
			//pp.setViewport(0,0, printer->width(),(int)(printer->width()/mapAspect) );	
insilmaril@417
   364
			viewBottom=(int)(printer->width()/mapAspect);	
insilmaril@0
   365
		}	else
insilmaril@0
   366
		{
insilmaril@0
   367
			// Fit vertically to paper height
insilmaril@417
   368
			//pp.setViewport(0,0,(int)(printer->height()*mapAspect),printer->height());	
insilmaril@417
   369
			viewBottom=printer->height();	
insilmaril@0
   370
		}	
insilmaril@417
   371
		
insilmaril@417
   372
		if (printFooter) 
insilmaril@417
   373
		{
insilmaril@417
   374
			// Print footer below map
insilmaril@417
   375
			QFont font;		
insilmaril@417
   376
			font.setPointSize(10);
insilmaril@417
   377
			pp.setFont (font);
insilmaril@417
   378
			QRectF footerBox(0,viewBottom,printer->width(),15);
insilmaril@746
   379
			// FIXME-3 fileName not any longer available here: pp.drawText ( footerBox,Qt::AlignLeft,"VYM - " +fileName);
insilmaril@417
   380
			pp.drawText ( footerBox, Qt::AlignRight, QDate::currentDate().toString(Qt::TextDate));
insilmaril@417
   381
		}
insilmaril@412
   382
		mapScene->render (
insilmaril@412
   383
			&pp, 
insilmaril@412
   384
			QRectF (0,0,printer->width(),printer->height()-15),
insilmaril@412
   385
			QRectF(mapRect.x(),mapRect.y(),mapRect.width(),mapRect.height())
insilmaril@412
   386
		);
insilmaril@412
   387
		
insilmaril@412
   388
		// Viewport has paper dimension
insilmaril@412
   389
		if (frame)  delete (frame);
insilmaril@0
   390
insilmaril@0
   391
		// Restore selection
insilmaril@720
   392
		model->reselect();
insilmaril@119
   393
insilmaril@119
   394
		// Save settings in vymrc
insilmaril@366
   395
		settings.writeEntry("/mainwindow/printerName",printer->printerName());
insilmaril@412
   396
		settings.writeEntry("/mainwindow/printerFormat",printer->outputFormat());
insilmaril@412
   397
		settings.writeEntry("/mainwindow/printerFileName",printer->outputFileName());
insilmaril@0
   398
	}
insilmaril@0
   399
}
insilmaril@0
   400
insilmaril@818
   401
QRectF MapEditor::getTotalBBox()	
insilmaril@782
   402
{
insilmaril@819
   403
	QPen pen;
insilmaril@819
   404
	pen.setWidth (1);
insilmaril@819
   405
	pen.setCapStyle ( Qt::RoundCap );
insilmaril@819
   406
insilmaril@819
   407
	QRectF rt;
insilmaril@818
   408
	BranchObj *bo;
insilmaril@819
   409
	BranchItem *cur=NULL;
insilmaril@819
   410
	BranchItem *prev=NULL;
insilmaril@819
   411
	model->nextBranch(cur,prev);
insilmaril@819
   412
	while (cur) 
insilmaril@818
   413
	{
insilmaril@819
   414
		if (!cur->hasHiddenExportParent())
insilmaril@819
   415
		{
insilmaril@819
   416
			bo=(BranchObj*)(cur->getLMO());
insilmaril@819
   417
			if (bo && bo->isVisibleObj())
insilmaril@819
   418
			{
insilmaril@819
   419
				bo->calcBBoxSizeWithChildren();
insilmaril@819
   420
				// FIXME-3 testing
insilmaril@819
   421
				//QRectF r1=bo->getBBoxSizeWithChildren();
insilmaril@819
   422
				QRectF r1=bo->getBBox();
insilmaril@819
   423
				//pen.setColor ( QColor(qrand()%32*8,qrand()%32*8,qrand()%32*8));
insilmaril@819
   424
				//mapScene->addRect (r1,pen);
insilmaril@819
   425
insilmaril@819
   426
				if (rt.isNull()) rt=r1;
insilmaril@819
   427
				rt=addBBox (r1, rt);
insilmaril@819
   428
				//FIXME-2 cout <<"ME: r1="<<r1<<"  "<<cur->getHeadingStd()<<endl;
insilmaril@819
   429
				//cout <<"    rt="<<rt<<endl;
insilmaril@819
   430
			}
insilmaril@819
   431
		}
insilmaril@819
   432
		model->nextBranch(cur,prev);
insilmaril@818
   433
	}
insilmaril@819
   434
	return rt;	
insilmaril@782
   435
}
insilmaril@782
   436
insilmaril@782
   437
insilmaril@819
   438
QImage MapEditor::getImage()
insilmaril@782
   439
{
insilmaril@782
   440
	QRectF mapRect=getTotalBBox();
insilmaril@819
   441
	int w=mapRect.width()+2;
insilmaril@819
   442
	int h=mapRect.height()+2;
insilmaril@819
   443
	
insilmaril@819
   444
	QImage pix (w,h,QImage::Format_RGB32);
insilmaril@819
   445
insilmaril@782
   446
	QPainter pp (&pix);
insilmaril@819
   447
insilmaril@782
   448
	pp.setRenderHints(renderHints());
insilmaril@782
   449
	mapScene->render (	&pp, 
insilmaril@819
   450
		QRectF(0,0,w,h),
insilmaril@782
   451
		QRectF(mapRect.x(),mapRect.y(),mapRect.width(),mapRect.height() ));
insilmaril@782
   452
	return pix;
insilmaril@782
   453
}
insilmaril@782
   454
insilmaril@782
   455
insilmaril@408
   456
void MapEditor::setAntiAlias (bool b)
insilmaril@408
   457
{
insilmaril@408
   458
	setRenderHint(QPainter::Antialiasing,b);
insilmaril@408
   459
}
insilmaril@408
   460
insilmaril@408
   461
void MapEditor::setSmoothPixmap(bool b)
insilmaril@408
   462
{
insilmaril@408
   463
	setRenderHint(QPainter::SmoothPixmapTransform,b);
insilmaril@408
   464
}
insilmaril@408
   465
insilmaril@819
   466
void MapEditor::setHideTmp (bool b)	// FIXME-4 better use signals
insilmaril@819
   467
{
insilmaril@819
   468
/*
insilmaril@819
   469
	if (b)
insilmaril@819
   470
	{
insilmaril@819
   471
		setHideTmpMode (HideExport);
insilmaril@819
   472
		mapCenter->calcBBoxSizeWithChilds();
insilmaril@819
   473
		QRectF totalBBox=mapCenter->getTotalBBox();
insilmaril@819
   474
		//QRectF mapRect=totalBBox;
insilmaril@819
   475
		cout << "  map has =("<<totalBBox.x()<<","<<totalBBox.y()<<","<<totalBBox.width()<<","<<totalBBox.height()<<")\n";
insilmaril@819
   476
	
insilmaril@819
   477
		mapRect.setRect (totalBBox.x(), totalBBox.y(), 
insilmaril@819
   478
			totalBBox.width(), totalBBox.height());
insilmaril@819
   479
			
insilmaril@819
   480
		QCanvasRectangle *frame=new QCanvasRectangle (mapRect,mapScene);
insilmaril@819
   481
		frame->setBrush (QColor(white));
insilmaril@819
   482
		frame->setPen (QColor(black));
insilmaril@819
   483
		frame->setZValue(0);
insilmaril@819
   484
		frame->show();    
insilmaril@819
   485
		
insilmaril@819
   486
	}	
insilmaril@819
   487
	else	
insilmaril@819
   488
	{
insilmaril@819
   489
		setHideTmpMode (HideNone);
insilmaril@819
   490
	}	
insilmaril@819
   491
	cout <<"  hidemode="<<hidemode<<endl;
insilmaril@819
   492
*/
insilmaril@819
   493
/*
insilmaril@819
   494
	// Toggle hidemode
insilmaril@819
   495
	if (hidemode==HideExport)
insilmaril@819
   496
		setHideTmpMode (HideNone);
insilmaril@819
   497
	else	
insilmaril@819
   498
		setHideTmpMode (HideExport);
insilmaril@819
   499
*/		
insilmaril@819
   500
}
insilmaril@819
   501
insilmaril@754
   502
TreeItem* MapEditor::findMapItem (QPointF p,TreeItem *exclude)
insilmaril@754
   503
{
insilmaril@754
   504
	// Start with mapcenter, no images allowed at rootItem
insilmaril@754
   505
	int i=0;
insilmaril@754
   506
	BranchItem *bi=model->getRootItem()->getFirstBranch();
insilmaril@754
   507
	TreeItem *found=NULL;
insilmaril@754
   508
	while (bi)
insilmaril@754
   509
	{
insilmaril@754
   510
		found=bi->findMapItem (p, exclude);
insilmaril@754
   511
		if (found) return found;
insilmaril@754
   512
		i++;
insilmaril@754
   513
		bi=model->getRootItem()->getBranchNum(i);
insilmaril@754
   514
	}
insilmaril@754
   515
	return NULL;
insilmaril@754
   516
}
insilmaril@754
   517
insilmaril@628
   518
AttributeTable* MapEditor::attributeTable()
insilmaril@628
   519
{
insilmaril@628
   520
	return attrTable;
insilmaril@628
   521
}
insilmaril@628
   522
insilmaril@527
   523
void MapEditor::testFunction1()
insilmaril@0
   524
{
insilmaril@795
   525
	cout << "ME::test1  selected TI="<<model->getSelectedItem()<<endl;
insilmaril@819
   526
	model->setExportMode (true);
insilmaril@798
   527
insilmaril@788
   528
	/*
insilmaril@767
   529
	// Code copied from Qt sources
insilmaril@767
   530
	QRectF rect=model->getSelectedBranchObj()->getBBox();
insilmaril@767
   531
	int xmargin=50;
insilmaril@767
   532
	int ymargin=50;
insilmaril@741
   533
insilmaril@767
   534
    qreal width = viewport()->width();
insilmaril@767
   535
    qreal height = viewport()->height();
insilmaril@767
   536
    QRectF viewRect = matrix().mapRect(rect);
insilmaril@741
   537
insilmaril@767
   538
    qreal left = horizontalScrollBar()->value();
insilmaril@767
   539
    qreal right = left + width;
insilmaril@767
   540
    qreal top = verticalScrollBar()->value();
insilmaril@767
   541
    qreal bottom = top + height;
insilmaril@723
   542
insilmaril@767
   543
    if (viewRect.left() <= left + xmargin) {
insilmaril@767
   544
        // need to scroll from the left
insilmaril@767
   545
  //      if (!d->leftIndent)
insilmaril@767
   546
            horizontalScrollBar()->setValue(int(viewRect.left() - xmargin - 0.5));
insilmaril@767
   547
    }
insilmaril@767
   548
    if (viewRect.right() >= right - xmargin) {
insilmaril@767
   549
        // need to scroll from the right
insilmaril@767
   550
//        if (!d->leftIndent)
insilmaril@767
   551
            horizontalScrollBar()->setValue(int(viewRect.right() - width + xmargin + 0.5));
insilmaril@767
   552
    }
insilmaril@767
   553
    if (viewRect.top() <= top + ymargin) {
insilmaril@767
   554
        // need to scroll from the top
insilmaril@767
   555
   //     if (!d->topIndent)
insilmaril@767
   556
            verticalScrollBar()->setValue(int(viewRect.top() - ymargin - 0.5));
insilmaril@767
   557
    }
insilmaril@767
   558
    if (viewRect.bottom() >= bottom - ymargin) {
insilmaril@767
   559
        // need to scroll from the bottom
insilmaril@767
   560
//        if (!d->topIndent)
insilmaril@767
   561
            verticalScrollBar()->setValue(int(viewRect.bottom() - height + ymargin + 0.5));
insilmaril@767
   562
    }
insilmaril@767
   563
	cout << "test1:  hor="<<horizontalScrollBar()->value()<<endl;
insilmaril@767
   564
	cout << "test1:  ver="<<verticalScrollBar()->value()<<endl;
insilmaril@767
   565
}
insilmaril@788
   566
insilmaril@788
   567
*/
insilmaril@741
   568
/*
insilmaril@767
   569
	 QtPropertyAnimation *animation=new QtPropertyAnimation(this, "sceneRect");
insilmaril@767
   570
	 animation->setDuration(5000);
insilmaril@767
   571
	 //animation->setEasingCurve ( QtEasingCurve::OutElastic);
insilmaril@767
   572
	 animation->setEasingCurve ( QtEasingCurve::OutQuint);
insilmaril@767
   573
	 animation->setStartValue(sceneRect() );
insilmaril@767
   574
	 animation->setEndValue(QRectF(50, 50, 1000, 1000));
insilmaril@740
   575
insilmaril@767
   576
	 animation->start();
insilmaril@767
   577
*/	 
insilmaril@767
   578
/*
insilmaril@767
   579
	QDialog *dia= new QDialog (this);
insilmaril@767
   580
	dia->setGeometry (50,50,10,10);
insilmaril@723
   581
insilmaril@767
   582
     dia->show();
insilmaril@767
   583
     dia ->raise();
insilmaril@767
   584
insilmaril@767
   585
	 QtPropertyAnimation *animation=new QtPropertyAnimation(dia, "geometry");
insilmaril@767
   586
	 animation->setDuration(1000);
insilmaril@767
   587
	 //animation->setEasingCurve ( QtEasingCurve::OutElastic);
insilmaril@767
   588
	 animation->setEasingCurve ( QtEasingCurve::OutQuint);
insilmaril@767
   589
	 animation->setStartValue(QRect(50, 50, 10, 10));
insilmaril@767
   590
	 animation->setEndValue(QRect(250, 250, 100, 100));
insilmaril@767
   591
insilmaril@767
   592
	 animation->start();
insilmaril@767
   593
 */
insilmaril@723
   594
insilmaril@792
   595
}
insilmaril@792
   596
	
insilmaril@792
   597
void MapEditor::testFunction2()
insilmaril@792
   598
{
insilmaril@819
   599
	model->setExportMode (false);
insilmaril@819
   600
	return;
insilmaril@819
   601
insilmaril@792
   602
	// Create list with all bounding polygons
insilmaril@794
   603
	QList <LinkableMapObj*> mapobjects;
insilmaril@792
   604
	QList <ConvexPolygon> polys; 
insilmaril@793
   605
	ConvexPolygon p;
insilmaril@792
   606
	QList <Vector> vectors;
insilmaril@792
   607
	QList <Vector> orgpos;
insilmaril@793
   608
	QStringList headings;	//FIXME-3 testing only
insilmaril@793
   609
	Vector v;
insilmaril@792
   610
	BranchItem *bi;
insilmaril@793
   611
	BranchItem *bi2;
insilmaril@792
   612
	BranchObj *bo;
insilmaril@794
   613
insilmaril@794
   614
	// Outer loop: Iterate until we no more changes in orientation 
insilmaril@794
   615
	bool orientationChanged=true;
insilmaril@794
   616
	while (orientationChanged)
insilmaril@792
   617
	{
insilmaril@794
   618
		BranchItem *ri=model->getRootItem();
insilmaril@794
   619
		for (int i=0;i<ri->branchCount();++i)
insilmaril@792
   620
		{
insilmaril@794
   621
			bi=ri->getBranchNum (i);
insilmaril@794
   622
			bo=(BranchObj*)bi->getLMO();
insilmaril@793
   623
			if (bo)
insilmaril@793
   624
			{
insilmaril@793
   625
				mapobjects.append (bo);
insilmaril@793
   626
				p=bo->getBoundingPolygon();
insilmaril@793
   627
				p.calcCentroid();
insilmaril@793
   628
				polys.append(p);
insilmaril@793
   629
				vectors.append (QPointF(0,0));
insilmaril@793
   630
				orgpos.append (p.at(0));
insilmaril@794
   631
				headings.append (bi->getHeading());
insilmaril@794
   632
			}
insilmaril@794
   633
			for (int j=0;j<bi->branchCount();++j)
insilmaril@792
   634
			{
insilmaril@794
   635
				bi2=bi->getBranchNum (j);
insilmaril@794
   636
				bo=(BranchObj*)bi2->getLMO();
insilmaril@794
   637
				if (bo)
insilmaril@792
   638
				{
insilmaril@794
   639
					mapobjects.append (bo);
insilmaril@794
   640
					p=bo->getBoundingPolygon();
insilmaril@794
   641
					p.calcCentroid();
insilmaril@794
   642
					polys.append(p);
insilmaril@794
   643
					vectors.append (QPointF(0,0));
insilmaril@794
   644
					orgpos.append (p.at(0));
insilmaril@794
   645
					headings.append (bi2->getHeading());
insilmaril@794
   646
				}	
insilmaril@792
   647
			}
insilmaril@792
   648
		}
insilmaril@794
   649
insilmaril@794
   650
		// Iterate moving bounding polygons until we have no more collisions
insilmaril@794
   651
		int collisions=1;
insilmaril@794
   652
		while (collisions>0)
insilmaril@793
   653
		{
insilmaril@794
   654
			collisions=0;
insilmaril@794
   655
			for (int i=0; i<polys.size()-1; ++i)
insilmaril@794
   656
			{
insilmaril@794
   657
				for (int j=i+1; j<polys.size();++j)
insilmaril@794
   658
				{
insilmaril@794
   659
					if (polygonCollision (polys.at(i),polys.at(j), QPointF(0,0)).intersect )
insilmaril@794
   660
					{
insilmaril@794
   661
						collisions++;
insilmaril@794
   662
						//cout << "Collision: "<<headings[i].toStdString()<<" - "<<headings[j].toStdString()<<endl;
insilmaril@794
   663
						v=polys.at(j).centroid()-polys.at(i).centroid();
insilmaril@794
   664
						// Move also away if centroids are identical
insilmaril@794
   665
						if (v.isNull()) 
insilmaril@794
   666
						{
insilmaril@794
   667
							//cout << "v==0="<<polys[i].centroid()<<polys[j].centroid()<<" "<<v<<endl;
insilmaril@794
   668
							v.setX (rand()%200 -100);
insilmaril@794
   669
							v.setY (rand()%200 -100);
insilmaril@794
   670
							//cout << v;
insilmaril@794
   671
						}
insilmaril@794
   672
						v.normalize();
insilmaril@794
   673
						v.scale (2);
insilmaril@794
   674
						//cout <<  "  v="<<v<<endl;
insilmaril@794
   675
						vectors[j]=v;
insilmaril@794
   676
						vectors[i]=v;
insilmaril@794
   677
						vectors[i].invert();
insilmaril@794
   678
					}  
insilmaril@794
   679
				}
insilmaril@794
   680
			}
insilmaril@794
   681
			for (int i=0;i<vectors.size();i++)
insilmaril@794
   682
			{
insilmaril@794
   683
				//cout << " v="<<vectors[i]<<" "<<headings[i].toStdString()<<endl;
insilmaril@794
   684
				polys[i].translate (vectors[i]);
insilmaril@794
   685
			}
insilmaril@794
   686
			cout << "Collisions: "<<collisions<<endl;
insilmaril@794
   687
			//collisions=0;
insilmaril@794
   688
		}	
insilmaril@792
   689
insilmaril@794
   690
		// Finally move the real objects and update 
insilmaril@794
   691
		QList <LinkableMapObj::Orientation> orients;
insilmaril@794
   692
		for (int i=0;i<polys.size();i++)
insilmaril@794
   693
		{
insilmaril@794
   694
			Vector v=polys[i].at(0)-orgpos[i];
insilmaril@794
   695
			orients.append (mapobjects[i]->getOrientation());
insilmaril@794
   696
			mapobjects[i]->moveBy(v.x(),v.y() );
insilmaril@798
   697
			mapobjects[i]->setRelPos();
insilmaril@794
   698
		}	
insilmaril@794
   699
		model->reposition();	
insilmaril@794
   700
		orientationChanged=false;
insilmaril@794
   701
		for (int i=0;i<polys.size();i++)
insilmaril@794
   702
			if (orients[i]!=mapobjects[i]->getOrientation())
insilmaril@794
   703
			{
insilmaril@794
   704
				orientationChanged=true;
insilmaril@794
   705
				break;
insilmaril@794
   706
			}
insilmaril@794
   707
		cout << "Final: orientChanged="<<orientationChanged<<endl;
insilmaril@794
   708
		break;
insilmaril@794
   709
		//orientationChanged=false;
insilmaril@794
   710
	} // loop if orientation has changed
insilmaril@798
   711
insilmaril@798
   712
	model->emitSelectionChanged();
insilmaril@105
   713
}
insilmaril@105
   714
insilmaril@787
   715
BranchItem* MapEditor::getBranchDirectAbove (BranchItem *bi)
insilmaril@787
   716
{
insilmaril@787
   717
	if (bi)
insilmaril@787
   718
	{
insilmaril@787
   719
		int i=bi->num();
insilmaril@787
   720
		if (i>0) return bi->parent()->getBranchNum(i-1);
insilmaril@787
   721
	}
insilmaril@787
   722
	return NULL;
insilmaril@787
   723
}
insilmaril@787
   724
insilmaril@787
   725
BranchItem* MapEditor::getBranchAbove (BranchItem *selbi)
insilmaril@787
   726
{
insilmaril@787
   727
	if (selbi)
insilmaril@787
   728
	{
insilmaril@787
   729
		int dz=selbi->depth();	// original depth
insilmaril@787
   730
		bool invert=false;
insilmaril@787
   731
		if (selbi->getLMO()->getOrientation()==LinkableMapObj::LeftOfCenter)
insilmaril@787
   732
			invert=true;
insilmaril@787
   733
insilmaril@787
   734
		BranchItem *bi;
insilmaril@787
   735
insilmaril@787
   736
		// Look for branch with same parent but directly above
insilmaril@787
   737
		if (dz==1 && invert)
insilmaril@787
   738
			bi=getBranchDirectBelow(selbi);
insilmaril@787
   739
		else
insilmaril@787
   740
			bi=getBranchDirectAbove (selbi);
insilmaril@787
   741
insilmaril@787
   742
		if (bi) 
insilmaril@787
   743
			// direct predecessor
insilmaril@787
   744
			return bi;
insilmaril@787
   745
insilmaril@787
   746
		// Go towards center and look for predecessor
insilmaril@787
   747
		while (selbi->depth()>0)
insilmaril@787
   748
		{
insilmaril@787
   749
			selbi=(BranchItem*)(selbi->parent());
insilmaril@787
   750
			if (selbi->depth()==1 && invert)
insilmaril@787
   751
				bi=getBranchDirectBelow (selbi);
insilmaril@787
   752
			else
insilmaril@787
   753
				bi=getBranchDirectAbove (selbi);
insilmaril@787
   754
			if (bi)
insilmaril@787
   755
			{
insilmaril@787
   756
				// turn 
insilmaril@787
   757
				selbi=bi;
insilmaril@787
   758
				while (selbi->depth()<dz)
insilmaril@787
   759
				{
insilmaril@787
   760
					// try to get back to original depth dz
insilmaril@787
   761
					bi=selbi->getLastBranch();
insilmaril@787
   762
					if (!bi) 
insilmaril@787
   763
					{
insilmaril@787
   764
						return selbi;
insilmaril@787
   765
					}
insilmaril@787
   766
					selbi=bi;
insilmaril@787
   767
				}
insilmaril@787
   768
				return selbi;
insilmaril@787
   769
			}
insilmaril@787
   770
		}
insilmaril@787
   771
	}
insilmaril@787
   772
	return NULL;
insilmaril@787
   773
}
insilmaril@787
   774
insilmaril@787
   775
BranchItem* MapEditor::getBranchDirectBelow(BranchItem *bi)
insilmaril@787
   776
{
insilmaril@787
   777
	if (bi)
insilmaril@787
   778
	{
insilmaril@787
   779
		int i=bi->num();
insilmaril@787
   780
		if (i+1<bi->parent()->branchCount()) return bi->parent()->getBranchNum(i+1);
insilmaril@787
   781
	}
insilmaril@787
   782
	return NULL;
insilmaril@787
   783
}
insilmaril@787
   784
insilmaril@787
   785
BranchItem* MapEditor::getBranchBelow (BranchItem *selbi)
insilmaril@787
   786
{
insilmaril@787
   787
	if (selbi)
insilmaril@787
   788
	{
insilmaril@787
   789
		BranchItem *bi;
insilmaril@787
   790
		int dz=selbi->depth();	// original depth
insilmaril@787
   791
		bool invert=false;
insilmaril@787
   792
		if (selbi->getLMO()->getOrientation()==LinkableMapObj::LeftOfCenter)
insilmaril@787
   793
			invert=true;
insilmaril@787
   794
insilmaril@787
   795
insilmaril@787
   796
		// Look for branch with same parent but directly below
insilmaril@787
   797
		if (dz==1 && invert)
insilmaril@787
   798
			bi=getBranchDirectAbove (selbi);
insilmaril@787
   799
		else
insilmaril@787
   800
			bi=getBranchDirectBelow (selbi);
insilmaril@787
   801
		if (bi) 
insilmaril@787
   802
			// direct successor
insilmaril@787
   803
			return bi;
insilmaril@787
   804
insilmaril@787
   805
insilmaril@787
   806
		// Go towards center and look for neighbour
insilmaril@787
   807
		while (selbi->depth()>0)
insilmaril@787
   808
		{
insilmaril@787
   809
			selbi=(BranchItem*)(selbi->parent());
insilmaril@787
   810
			if (selbi->depth()==1 && invert)
insilmaril@787
   811
				bi=getBranchDirectAbove (selbi);
insilmaril@787
   812
			else
insilmaril@787
   813
				bi=getBranchDirectBelow (selbi);
insilmaril@787
   814
			if (bi)
insilmaril@787
   815
			{
insilmaril@787
   816
				// turn 
insilmaril@787
   817
				selbi=bi;
insilmaril@787
   818
				while (selbi->depth()<dz)
insilmaril@787
   819
				{
insilmaril@787
   820
					// try to get back to original depth dz
insilmaril@787
   821
					bi=selbi->getFirstBranch();
insilmaril@787
   822
					if (!bi) 
insilmaril@787
   823
					{
insilmaril@787
   824
						return selbi;
insilmaril@787
   825
					}
insilmaril@787
   826
					selbi=bi;
insilmaril@787
   827
				}
insilmaril@787
   828
				return selbi;
insilmaril@787
   829
			}
insilmaril@787
   830
		}
insilmaril@787
   831
	}
insilmaril@787
   832
	return NULL;
insilmaril@787
   833
}
insilmaril@787
   834
insilmaril@787
   835
BranchItem* MapEditor::getLeftBranch (BranchItem *bi)
insilmaril@787
   836
{
insilmaril@787
   837
	if (bi)
insilmaril@787
   838
	{
insilmaril@787
   839
		if (bi->depth()==0)
insilmaril@787
   840
			// Special case: use alternative selection index
insilmaril@787
   841
			return bi->getLastSelectedBranchAlt();	
insilmaril@787
   842
		if (bi->getBranchObj()->getOrientation()==LinkableMapObj::RightOfCenter)	
insilmaril@787
   843
			// right of center
insilmaril@787
   844
			return (BranchItem*)(bi->parent());
insilmaril@787
   845
		else
insilmaril@787
   846
			// left of center
insilmaril@787
   847
			if (bi->getType()== TreeItem::Branch )
insilmaril@787
   848
				return bi->getLastSelectedBranch();
insilmaril@787
   849
	}
insilmaril@787
   850
	return NULL;
insilmaril@787
   851
}
insilmaril@787
   852
insilmaril@787
   853
BranchItem* MapEditor::getRightBranch(BranchItem *bi)
insilmaril@787
   854
{
insilmaril@787
   855
	if (bi)
insilmaril@787
   856
	{
insilmaril@787
   857
		if (bi->depth()==0) return bi->getLastSelectedBranch();	
insilmaril@787
   858
		if (bi->getBranchObj()->getOrientation()==LinkableMapObj::LeftOfCenter)	
insilmaril@787
   859
			// left of center
insilmaril@787
   860
			return (BranchItem*)(bi->parent());
insilmaril@787
   861
		else
insilmaril@787
   862
			// right of center
insilmaril@787
   863
			if (bi->getType()== TreeItem::Branch )
insilmaril@787
   864
				return (BranchItem*)bi->getLastSelectedBranch();
insilmaril@787
   865
	}
insilmaril@787
   866
	return NULL;
insilmaril@787
   867
}
insilmaril@787
   868
insilmaril@787
   869
insilmaril@787
   870
insilmaril@769
   871
void MapEditor::cursorUp()
insilmaril@769
   872
{
insilmaril@787
   873
	BranchItem *bi=model->getSelectedBranch();
insilmaril@787
   874
	if (bi) model->select (getBranchAbove(bi));
insilmaril@769
   875
}
insilmaril@769
   876
insilmaril@769
   877
void MapEditor::cursorDown()	
insilmaril@769
   878
insilmaril@769
   879
{
insilmaril@787
   880
	BranchItem *bi=model->getSelectedBranch();
insilmaril@787
   881
	if (bi) model->select (getBranchBelow(bi));
insilmaril@769
   882
}
insilmaril@769
   883
insilmaril@769
   884
void MapEditor::cursorLeft()
insilmaril@769
   885
{
insilmaril@787
   886
	BranchItem *bi=getLeftBranch (model->getSelectedBranch());
insilmaril@787
   887
	if (bi) model->select (bi);
insilmaril@769
   888
}
insilmaril@769
   889
insilmaril@769
   890
void MapEditor::cursorRight()	
insilmaril@769
   891
{
insilmaril@787
   892
	BranchItem *bi=getRightBranch (model->getSelectedBranch());
insilmaril@787
   893
	if (bi) model->select (bi);
insilmaril@769
   894
}
insilmaril@769
   895
insilmaril@769
   896
void MapEditor::cursorFirst()	
insilmaril@769
   897
{
insilmaril@769
   898
	model->selectFirstBranch();
insilmaril@769
   899
}
insilmaril@769
   900
insilmaril@769
   901
void MapEditor::cursorLast()	
insilmaril@769
   902
{
insilmaril@769
   903
	model->selectLastBranch();
insilmaril@769
   904
}
insilmaril@769
   905
insilmaril@769
   906
insilmaril@720
   907
void MapEditor::editHeading()
insilmaril@720
   908
{
insilmaril@720
   909
	if (editingHeading)
insilmaril@720
   910
	{
insilmaril@720
   911
		editHeadingFinished();
insilmaril@720
   912
		return;
insilmaril@720
   913
	}
insilmaril@753
   914
	BranchObj *bo=model->getSelectedBranchObj();
insilmaril@787
   915
	BranchItem *bi=model->getSelectedBranch();
insilmaril@773
   916
	if (bo)	
insilmaril@720
   917
	{
insilmaril@720
   918
		model->setSelectionBlocked(true);
insilmaril@720
   919
insilmaril@819
   920
		lineEdit=new QLineEdit;
insilmaril@819
   921
		QGraphicsProxyWidget *pw=scene()->addWidget (lineEdit);
insilmaril@819
   922
		pw->setZValue (Z_LINEEDIT);
insilmaril@819
   923
insilmaril@753
   924
		lineEdit->setText (bi->getHeading());
insilmaril@720
   925
		QPoint p = mapTo (this,bo->getAbsPos().toPoint() );
insilmaril@720
   926
		lineEdit->setGeometry(p.x(),p.y(),230,25);
insilmaril@804
   927
		//lineEdit->selectAll();
insilmaril@804
   928
		//lineEdit->setCursorPosition (1);
insilmaril@720
   929
		lineEdit->show();
insilmaril@720
   930
		lineEdit->setFocus();
insilmaril@720
   931
		lineEdit->grabKeyboard();
insilmaril@720
   932
		editingHeading=true;
insilmaril@720
   933
	}
insilmaril@720
   934
insilmaril@720
   935
}
insilmaril@720
   936
void MapEditor::editHeadingFinished()
insilmaril@720
   937
{
insilmaril@720
   938
	editingHeading=false;
insilmaril@720
   939
	lineEdit->releaseKeyboard();
insilmaril@819
   940
	lineEdit->clearFocus();
insilmaril@823
   941
	QString s=lineEdit->text();
insilmaril@823
   942
	s.replace (QRegExp ("\\n")," ");	// Don't paste newline chars
insilmaril@823
   943
	model->setHeading (s);
insilmaril@720
   944
	model->setSelectionBlocked(false);
insilmaril@819
   945
	delete (lineEdit);
insilmaril@720
   946
insilmaril@720
   947
	// Maybe reselect previous branch 
insilmaril@720
   948
	mainWindow->editHeadingFinished (model);
insilmaril@720
   949
}
insilmaril@720
   950
insilmaril@720
   951
insilmaril@408
   952
void MapEditor::contextMenuEvent ( QContextMenuEvent * e )
insilmaril@0
   953
{
insilmaril@0
   954
	// Lineedits are already closed by preceding
insilmaril@0
   955
	// mouseEvent, we don't need to close here.
insilmaril@0
   956
insilmaril@408
   957
    QPointF p = mapToScene(e->pos());
insilmaril@754
   958
    TreeItem *ti=findMapItem (p, NULL);
insilmaril@754
   959
    LinkableMapObj* lmo=NULL;
insilmaril@779
   960
	if (ti) lmo=((MapItem*)ti)->getLMO();
insilmaril@0
   961
	
insilmaril@0
   962
    if (lmo) 
insilmaril@0
   963
	{	// MapObj was found
insilmaril@735
   964
		if (model->getSelectedLMO() != lmo)
insilmaril@0
   965
		{
insilmaril@0
   966
			// select the MapObj
insilmaril@720
   967
			model->select(lmo);
insilmaril@0
   968
		}
insilmaril@0
   969
		// Context Menu 
insilmaril@753
   970
		if (model->getSelectedBranchObj() ) 
insilmaril@0
   971
		{
insilmaril@424
   972
			// Context Menu on branch or mapcenter
insilmaril@424
   973
			branchContextMenu->popup(e->globalPos() );
insilmaril@424
   974
		} else
insilmaril@424
   975
		{
insilmaril@787
   976
			if (model->getSelectedImage() )
insilmaril@0
   977
			{
insilmaril@0
   978
				// Context Menu on floatimage
insilmaril@0
   979
				floatimageContextMenu->popup(e->globalPos() );
insilmaril@0
   980
			}	
insilmaril@0
   981
		}	
insilmaril@0
   982
	} else 
insilmaril@0
   983
	{ // No MapObj found, we are on the Canvas itself
insilmaril@408
   984
		// Context Menu on scene
insilmaril@723
   985
		
insilmaril@723
   986
		// Open context menu synchronously to position new mapcenter
insilmaril@723
   987
		model->setContextPos (p);
insilmaril@723
   988
		canvasContextMenu->exec(e->globalPos() );
insilmaril@723
   989
		model->unsetContextPos ();
insilmaril@0
   990
    } 
insilmaril@329
   991
	e->accept();
insilmaril@0
   992
}
insilmaril@0
   993
insilmaril@398
   994
void MapEditor::keyPressEvent(QKeyEvent* e)
insilmaril@398
   995
{
insilmaril@411
   996
	if (e->modifiers() & Qt::ControlModifier)
insilmaril@398
   997
	{
insilmaril@398
   998
		switch (mainWindow->getModMode())
insilmaril@398
   999
		{
insilmaril@473
  1000
			case Main::ModModeColor: 
insilmaril@398
  1001
				setCursor (PickColorCursor);
insilmaril@398
  1002
				break;
insilmaril@473
  1003
			case Main::ModModeCopy: 
insilmaril@398
  1004
				setCursor (CopyCursor);
insilmaril@398
  1005
				break;
insilmaril@473
  1006
			case Main::ModModeXLink: 
insilmaril@398
  1007
				setCursor (XLinkCursor);
insilmaril@398
  1008
				break;
insilmaril@398
  1009
			default :
insilmaril@398
  1010
				setCursor (Qt::ArrowCursor);
insilmaril@398
  1011
				break;
insilmaril@398
  1012
		} 
insilmaril@398
  1013
	}	
insilmaril@398
  1014
}
insilmaril@398
  1015
insilmaril@398
  1016
void MapEditor::keyReleaseEvent(QKeyEvent* e)
insilmaril@398
  1017
{
insilmaril@411
  1018
	if (!(e->modifiers() & Qt::ControlModifier))
insilmaril@398
  1019
		setCursor (Qt::ArrowCursor);
insilmaril@398
  1020
}
insilmaril@398
  1021
insilmaril@408
  1022
void MapEditor::mousePressEvent(QMouseEvent* e)
insilmaril@0
  1023
{
insilmaril@807
  1024
//cout << "ME::mousePressed\n"; //FIXME-3
insilmaril@329
  1025
	// Ignore right clicks, these will go to context menus
insilmaril@366
  1026
	if (e->button() == Qt::RightButton )
insilmaril@329
  1027
	{
insilmaril@807
  1028
		//cout << "  ME::ignoring right mouse event...\n";
insilmaril@329
  1029
		e->ignore();
insilmaril@329
  1030
		return;
insilmaril@329
  1031
	}
insilmaril@329
  1032
insilmaril@491
  1033
	//Ignore clicks while editing heading
insilmaril@720
  1034
	if (model->isSelectionBlocked() ) 
insilmaril@491
  1035
	{
insilmaril@807
  1036
		//cout << "  ME::ignoring other mouse event...\n";
insilmaril@491
  1037
		e->ignore();
insilmaril@491
  1038
		return;
insilmaril@491
  1039
	}
insilmaril@491
  1040
insilmaril@408
  1041
    QPointF p = mapToScene(e->pos());
insilmaril@754
  1042
    TreeItem *ti=findMapItem (p, NULL);
insilmaril@754
  1043
    LinkableMapObj* lmo=NULL;
insilmaril@779
  1044
	if (ti) lmo=((MapItem*)ti)->getLMO();
insilmaril@0
  1045
	
insilmaril@790
  1046
	
insilmaril@329
  1047
	e->accept();
insilmaril@329
  1048
insilmaril@442
  1049
	//Take care of  system flags _or_ modifier modes
insilmaril@267
  1050
	//
insilmaril@773
  1051
	if (lmo && ti->isBranchLikeType() )
insilmaril@267
  1052
	{
insilmaril@267
  1053
		QString foname=((BranchObj*)lmo)->getSystemFlagName(p);
insilmaril@267
  1054
		if (!foname.isEmpty())
insilmaril@267
  1055
		{
insilmaril@267
  1056
			// systemFlag clicked
insilmaril@798
  1057
			model->select (lmo);	
insilmaril@823
  1058
			if (foname.contains("system-url")) 
insilmaril@267
  1059
			{
insilmaril@366
  1060
				if (e->state() & Qt::ControlModifier)
insilmaril@267
  1061
					mainWindow->editOpenURLTab();
insilmaril@267
  1062
				else	
insilmaril@267
  1063
					mainWindow->editOpenURL();
insilmaril@267
  1064
			}	
insilmaril@780
  1065
			else if (foname=="system-vymLink")
insilmaril@267
  1066
			{
insilmaril@267
  1067
				mainWindow->editOpenVymLink();
insilmaril@267
  1068
				// tabWidget may change, better return now
insilmaril@267
  1069
				// before segfaulting...
insilmaril@814
  1070
			} else if (foname=="system-note")
insilmaril@267
  1071
				mainWindow->windowToggleNoteEditor();
insilmaril@267
  1072
			else if (foname=="hideInExport")		
insilmaril@720
  1073
				model->toggleHideExport();
insilmaril@746
  1074
			// FIXME-3 needed? xelection.update();	
insilmaril@267
  1075
			return;	
insilmaril@814
  1076
		} else
insilmaril@814
  1077
		{
insilmaril@814
  1078
			// Take care of xLink
insilmaril@814
  1079
			if (ti->xlinkCount()>0 && lmo->getBBox().width()>30)
insilmaril@814
  1080
			{
insilmaril@817
  1081
				if ((lmo->getOrientation()!=LinkableMapObj::RightOfCenter && p.x() < lmo->getBBox().left()+20)  ||
insilmaril@817
  1082
				    (lmo->getOrientation()!=LinkableMapObj::LeftOfCenter && p.x() > lmo->getBBox().right()-20) ) 
insilmaril@814
  1083
				{
insilmaril@817
  1084
					// FIXME-4 if (ti->xlinkCount()>1)
insilmaril@817
  1085
					if (true)
insilmaril@814
  1086
					{
insilmaril@814
  1087
						QMenu menu;
insilmaril@814
  1088
						QList <QAction*> alist;
insilmaril@814
  1089
						for (int i=0;i<ti->xlinkCount();i++)
insilmaril@814
  1090
							alist.append (new QAction(ti->getXLinkNum(i)->getOtherBranch(ti)->getHeading(),&menu));
insilmaril@814
  1091
						menu.addActions (alist);	
insilmaril@814
  1092
						QAction *ra=menu.exec (e->globalPos() );
insilmaril@814
  1093
						if (ra)
insilmaril@814
  1094
							model->select (ti->getXLinkNum(alist.indexOf(ra))->getOtherBranch (ti));
insilmaril@814
  1095
						while (!alist.isEmpty())
insilmaril@814
  1096
						{
insilmaril@814
  1097
							QAction *a=alist.takeFirst();
insilmaril@814
  1098
							delete a;
insilmaril@814
  1099
						}	
insilmaril@814
  1100
						return;
insilmaril@814
  1101
					} else
insilmaril@814
  1102
					{
insilmaril@814
  1103
						model->select (ti->getXLinkNum(0)->getOtherBranch (ti));
insilmaril@814
  1104
						return;
insilmaril@814
  1105
					}
insilmaril@814
  1106
				}	
insilmaril@814
  1107
			}
insilmaril@814
  1108
		}
insilmaril@720
  1109
	}	
insilmaril@814
  1110
insilmaril@442
  1111
	// No system flag clicked, take care of modmodes (CTRL-Click)
insilmaril@366
  1112
	if (e->state() & Qt::ControlModifier)
insilmaril@0
  1113
	{
insilmaril@473
  1114
		if (mainWindow->getModMode()==Main::ModModeColor)
insilmaril@83
  1115
		{
insilmaril@83
  1116
				pickingColor=true;
insilmaril@398
  1117
				setCursor (PickColorCursor);
insilmaril@83
  1118
				return;
insilmaril@83
  1119
		} 
insilmaril@473
  1120
		if (mainWindow->getModMode()==Main::ModModeXLink)
insilmaril@83
  1121
		{	
insilmaril@790
  1122
			BranchItem *bi_begin=model->getSelectedBranch();
insilmaril@790
  1123
			if (bi_begin)	
insilmaril@83
  1124
			{
insilmaril@83
  1125
				drawingLink=true;
insilmaril@790
  1126
				tmpXLink=model->createXLink(bi_begin,true);
insilmaril@720
  1127
				tmpXLink->setColor(model->getMapDefXLinkColor());
insilmaril@720
  1128
				tmpXLink->setWidth(model->getMapDefXLinkWidth());
insilmaril@790
  1129
				tmpXLink->setEnd   (p);
insilmaril@95
  1130
				tmpXLink->updateXLink();
insilmaril@83
  1131
				return;
insilmaril@89
  1132
			} 
insilmaril@267
  1133
		}
insilmaril@442
  1134
	}	// End of modmodes
insilmaril@442
  1135
insilmaril@0
  1136
    if (lmo) 
insilmaril@267
  1137
	{	
insilmaril@738
  1138
	/*
insilmaril@729
  1139
		cout << "ME::mouse pressed\n";
insilmaril@729
  1140
		cout << "  lmo="<<lmo<<endl;
insilmaril@776
  1141
		cout << "   ti="<<ti->getHeadingStd()<<endl;
insilmaril@738
  1142
	*/
insilmaril@442
  1143
		// Select the clicked object
insilmaril@729
  1144
insilmaril@776
  1145
		// Get clicked LMO
insilmaril@738
  1146
		model->select (ti);
insilmaril@442
  1147
insilmaril@0
  1148
		// Left Button	    Move Branches
insilmaril@366
  1149
		if (e->button() == Qt::LeftButton )
insilmaril@0
  1150
		{
insilmaril@424
  1151
			movingObj_start.setX( p.x() - lmo->x() );	
insilmaril@424
  1152
			movingObj_start.setY( p.y() - lmo->y() );	
insilmaril@129
  1153
			movingObj_orgPos.setX (lmo->x() );
insilmaril@129
  1154
			movingObj_orgPos.setY (lmo->y() );
insilmaril@720
  1155
			lmo->setRelPos();
insilmaril@366
  1156
			movingObj_orgRelPos=lmo->getRelPos();
insilmaril@129
  1157
insilmaril@93
  1158
			// If modMode==copy, then we want to "move" the _new_ object around
insilmaril@93
  1159
			// then we need the offset from p to the _old_ selection, because of tmp
insilmaril@473
  1160
			if (mainWindow->getModMode()==Main::ModModeCopy &&
insilmaril@366
  1161
				e->state() & Qt::ControlModifier)
insilmaril@93
  1162
			{
insilmaril@787
  1163
				BranchItem *bi=model->getSelectedBranch();
insilmaril@750
  1164
				if (bi)
insilmaril@93
  1165
				{
insilmaril@93
  1166
					copyingObj=true;
insilmaril@750
  1167
					//FIXME-2   TreeItem::addBranch (BranchItem still missing) 
insilmaril@787
  1168
					//bi->addBranch (model->getSelectedBranch());
insilmaril@720
  1169
					model->unselect();
insilmaril@750
  1170
					model->select(bi->getLastBranch());
insilmaril@645
  1171
					model->reposition();
insilmaril@93
  1172
				}
insilmaril@421
  1173
			} 
insilmaril@421
  1174
insilmaril@735
  1175
			movingObj=model->getSelectedLMO();	
insilmaril@0
  1176
		} else
insilmaril@0
  1177
			// Middle Button    Toggle Scroll
insilmaril@0
  1178
			// (On Mac OS X this won't work, but we still have 
insilmaril@0
  1179
			// a button in the toolbar)
insilmaril@366
  1180
			if (e->button() == Qt::MidButton )
insilmaril@720
  1181
				model->toggleScroll();
insilmaril@746
  1182
		// model->updateActions(); FIXME-3 needed?
insilmaril@746
  1183
		// FIXME-3 needed? xelection.update();
insilmaril@0
  1184
	} else 
insilmaril@408
  1185
	{ // No MapObj found, we are on the scene itself
insilmaril@408
  1186
		// Left Button	    move Pos of sceneView
insilmaril@366
  1187
		if (e->button() == Qt::LeftButton )
insilmaril@0
  1188
		{
insilmaril@0
  1189
			movingObj=NULL;	// move Content not Obj
insilmaril@0
  1190
			movingObj_start=e->globalPos();
insilmaril@408
  1191
			movingCont_start=QPointF (
insilmaril@408
  1192
				horizontalScrollBar()->value(),
insilmaril@408
  1193
				verticalScrollBar()->value());
insilmaril@408
  1194
			movingVec=QPointF(0,0);
insilmaril@398
  1195
			setCursor(HandOpenCursor);
insilmaril@0
  1196
		} 
insilmaril@0
  1197
    } 
insilmaril@0
  1198
}
insilmaril@0
  1199
insilmaril@408
  1200
void MapEditor::mouseMoveEvent(QMouseEvent* e)
insilmaril@0
  1201
{
insilmaril@408
  1202
    QPointF p = mapToScene(e->pos());
insilmaril@754
  1203
	TreeItem *seli=model->getSelectedItem();
insilmaril@777
  1204
	LinkableMapObj* lmosel=NULL;	
insilmaril@776
  1205
	if (seli && (seli->isBranchLikeType() ||seli->getType()==TreeItem::Image))
insilmaril@779
  1206
		lmosel=((MapItem*)seli)->getLMO();
insilmaril@83
  1207
insilmaril@0
  1208
    // Move the selected MapObj
insilmaril@424
  1209
    if ( lmosel && movingObj) 
insilmaril@0
  1210
    {	
insilmaril@798
  1211
		objectMoved=true;
insilmaril@421
  1212
		// reset cursor if we are moving and don't copy
insilmaril@473
  1213
		if (mainWindow->getModMode()!=Main::ModModeCopy)
insilmaril@421
  1214
			setCursor (Qt::ArrowCursor);
insilmaril@421
  1215
insilmaril@408
  1216
		// To avoid jumping of the sceneView, only 
insilmaril@763
  1217
		// show selection, if not tmp linked
insilmaril@798
  1218
		/* FIXME-2 if (!lmosel->hasParObjTmp())
insilmaril@763
  1219
			model->emitShowSelection();
insilmaril@798
  1220
			*/
insilmaril@0
  1221
		
insilmaril@83
  1222
		// Now move the selection, but add relative position 
insilmaril@83
  1223
		// (movingObj_start) where selection was chosen with 
insilmaril@83
  1224
		// mousepointer. (This avoids flickering resp. jumping 
insilmaril@0
  1225
		// of selection back to absPos)
insilmaril@0
  1226
		
insilmaril@0
  1227
		// Check if we could link 
insilmaril@777
  1228
		TreeItem *ti=findMapItem (p, seli);
insilmaril@777
  1229
		BranchItem *dsti=NULL;
insilmaril@754
  1230
		LinkableMapObj* dst=NULL;
insilmaril@777
  1231
		if (ti && ti!=seli && ti->isBranchLikeType())
insilmaril@777
  1232
		{
insilmaril@777
  1233
			dsti=(BranchItem*)ti;
insilmaril@777
  1234
			dst=dsti->getLMO(); 
insilmaril@777
  1235
		} else
insilmaril@772
  1236
			dsti=NULL;
insilmaril@0
  1237
		
insilmaril@0
  1238
insilmaril@777
  1239
		if (lmosel && seli->getType()==TreeItem::Image)	
insilmaril@0
  1240
		{
insilmaril@776
  1241
			FloatObj *fio=(FloatImageObj*)lmosel;
insilmaril@424
  1242
			fio->move   (p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );		
insilmaril@424
  1243
			fio->setRelPos();
insilmaril@779
  1244
			fio->updateLinkGeometry(); //no need for reposition, if we update link here
insilmaril@775
  1245
			model->emitSelectionChanged();	// position has changed
insilmaril@0
  1246
insilmaril@0
  1247
			// Relink float to new mapcenter or branch, if shift is pressed	
insilmaril@0
  1248
			// Only relink, if selection really has a new parent
insilmaril@777
  1249
			if ( e->modifiers()==Qt::ShiftModifier && dsti &&  dsti != seli->parent()  )
insilmaril@0
  1250
			{
insilmaril@773
  1251
				// Also save the move which was done so far
insilmaril@792
  1252
				QString pold=qpointFToString(movingObj_orgRelPos);
insilmaril@792
  1253
				QString pnow=qpointFToString(fio->getRelPos());
insilmaril@773
  1254
				model->saveState(
insilmaril@777
  1255
					seli,
insilmaril@773
  1256
					"moveRel "+pold,
insilmaril@777
  1257
					seli,
insilmaril@773
  1258
					"moveRel "+pnow,
insilmaril@773
  1259
					QString("Move %1 to relative position %2").arg(model->getObjectName(fio)).arg(pnow));
insilmaril@773
  1260
				fio->getParObj()->requestReposition();
insilmaril@773
  1261
				model->reposition();
insilmaril@420
  1262
insilmaril@777
  1263
				model->relinkImage ((ImageItem*) seli,dsti);
insilmaril@777
  1264
				model->select (seli);
insilmaril@777
  1265
				//movingObj=lmosel;	//FIXME-3
insilmaril@773
  1266
				//movingObj_orgRelPos=lmosel->getRelPos();	
insilmaril@420
  1267
insilmaril@773
  1268
				model->reposition();
insilmaril@0
  1269
			}
insilmaril@388
  1270
		} else	
insilmaril@388
  1271
		{	// selection != a FloatObj
insilmaril@798
  1272
			if (seli->depth()==0)		
insilmaril@0
  1273
			{
insilmaril@814
  1274
				// Move mapcenter
insilmaril@796
  1275
				lmosel->move   (p-movingObj_start);		
insilmaril@388
  1276
				if (e->buttons()== Qt::LeftButton && e->modifiers()==Qt::ShiftModifier) 
insilmaril@796
  1277
				{
insilmaril@814
  1278
					// Move only mapcenter, leave its children where they are
insilmaril@796
  1279
					QPointF v;
insilmaril@796
  1280
					v=lmosel->getAbsPos();
insilmaril@796
  1281
					for (int i=0; i<seli->branchCount(); ++i)
insilmaril@796
  1282
					{
insilmaril@796
  1283
						seli->getBranchObjNum(i)->setRelPos();
insilmaril@796
  1284
						seli->getBranchObjNum(i)->setOrientation();
insilmaril@796
  1285
					}
insilmaril@796
  1286
				}	
insilmaril@0
  1287
			} else
insilmaril@0
  1288
			{	
insilmaril@755
  1289
				if (seli->depth()==1)
insilmaril@0
  1290
				{
insilmaril@388
  1291
					// Move mainbranch
insilmaril@814
  1292
					if (!lmosel->hasParObjTmp())
insilmaril@814
  1293
						lmosel->move(p-movingObj_start);		
insilmaril@388
  1294
					lmosel->setRelPos();
insilmaril@0
  1295
				} else
insilmaril@0
  1296
				{
insilmaril@388
  1297
					// Move ordinary branch
insilmaril@473
  1298
					if (lmosel->getOrientation() == LinkableMapObj::LeftOfCenter)
insilmaril@0
  1299
						// Add width of bbox here, otherwise alignRelTo will cause jumping around
insilmaril@720
  1300
						lmosel->move(p.x() -movingObj_start.x() , //lmosel->getBBox().width(), 
insilmaril@175
  1301
							p.y()-movingObj_start.y() +lmosel->getTopPad() );		
insilmaril@0
  1302
					else	
insilmaril@175
  1303
						lmosel->move(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() -lmosel->getTopPad());
insilmaril@720
  1304
					lmosel->setRelPos();	
insilmaril@0
  1305
				} 
insilmaril@0
  1306
insilmaril@794
  1307
			} // depth>0
insilmaril@431
  1308
				// Maybe we can relink temporary?
insilmaril@819
  1309
				if (dsti)
insilmaril@0
  1310
				{
insilmaril@388
  1311
					if (e->modifiers()==Qt::ControlModifier)
insilmaril@0
  1312
					{
insilmaril@754
  1313
						// Special case: CTRL to link below dst
insilmaril@754
  1314
						lmosel->setParObjTmp (dst,p,+1);
insilmaril@814
  1315
					} else if (e->modifiers()==Qt::ShiftModifier)
insilmaril@754
  1316
						lmosel->setParObjTmp (dst,p,-1);
insilmaril@0
  1317
					else
insilmaril@754
  1318
						lmosel->setParObjTmp (dst,p,0);
insilmaril@0
  1319
				} else	
insilmaril@0
  1320
				{
insilmaril@93
  1321
					lmosel->unsetParObjTmp();
insilmaril@0
  1322
				}		
insilmaril@428
  1323
				// reposition subbranch
insilmaril@428
  1324
				lmosel->reposition();	
insilmaril@0
  1325
insilmaril@732
  1326
			QItemSelection sel=model->getSelectionModel()->selection();
insilmaril@735
  1327
			updateSelection(sel,sel);	// position has changed
insilmaril@732
  1328
insilmaril@0
  1329
		} // no FloatImageObj
insilmaril@0
  1330
insilmaril@408
  1331
		scene()->update();
insilmaril@0
  1332
		return;
insilmaril@0
  1333
	} // selection && moving_obj
insilmaril@0
  1334
		
insilmaril@83
  1335
	// Draw a link from one branch to another
insilmaril@83
  1336
	if (drawingLink)
insilmaril@83
  1337
	{
insilmaril@95
  1338
		 tmpXLink->setEnd (p);
insilmaril@95
  1339
		 tmpXLink->updateXLink();
insilmaril@83
  1340
	}	 
insilmaril@83
  1341
	
insilmaril@408
  1342
    // Move sceneView 
insilmaril@411
  1343
    if (!movingObj && !pickingColor &&!drawingLink && e->buttons() == Qt::LeftButton ) 
insilmaril@0
  1344
	{
insilmaril@408
  1345
		QPointF p=e->globalPos();
insilmaril@0
  1346
		movingVec.setX(-p.x() + movingObj_start.x() );
insilmaril@0
  1347
		movingVec.setY(-p.y() + movingObj_start.y() );
insilmaril@408
  1348
		horizontalScrollBar()->setSliderPosition((int)( movingCont_start.x()+movingVec.x() ));
insilmaril@408
  1349
		verticalScrollBar()->setSliderPosition((int)( movingCont_start.y()+movingVec.y() ) );
insilmaril@0
  1350
    }
insilmaril@0
  1351
}
insilmaril@0
  1352
insilmaril@0
  1353
insilmaril@408
  1354
void MapEditor::mouseReleaseEvent(QMouseEvent* e)
insilmaril@0
  1355
{
insilmaril@408
  1356
    QPointF p = mapToScene(e->pos());
insilmaril@754
  1357
	TreeItem *seli=model->getSelectedItem();
insilmaril@754
  1358
insilmaril@772
  1359
	TreeItem *dsti=NULL;
insilmaril@772
  1360
	if (seli) dsti=findMapItem(p, seli);
insilmaril@755
  1361
	LinkableMapObj* dst=NULL;
insilmaril@772
  1362
	if (dsti && dsti->isBranchLikeType ()) 
insilmaril@779
  1363
		dst=((MapItem*)dsti)->getLMO();	
insilmaril@772
  1364
	else
insilmaril@772
  1365
		dsti=NULL;
insilmaril@754
  1366
insilmaril@754
  1367
insilmaril@0
  1368
	// Have we been picking color?
insilmaril@0
  1369
	if (pickingColor)
insilmaril@0
  1370
	{
insilmaril@0
  1371
		pickingColor=false;
insilmaril@366
  1372
		setCursor (Qt::ArrowCursor);
insilmaril@0
  1373
		// Check if we are over another branch
insilmaril@772
  1374
		if (dst) 
insilmaril@0
  1375
		{	
insilmaril@366
  1376
			if (e->state() & Qt::ShiftModifier)
insilmaril@720
  1377
				model->colorBranch (((BranchObj*)dst)->getColor());
insilmaril@0
  1378
			else	
insilmaril@720
  1379
				model->colorSubtree (((BranchObj*)dst)->getColor());
insilmaril@0
  1380
		} 
insilmaril@0
  1381
		return;
insilmaril@0
  1382
	}
insilmaril@83
  1383
insilmaril@83
  1384
	// Have we been drawing a link?
insilmaril@491
  1385
	if (drawingLink)	
insilmaril@83
  1386
	{
insilmaril@83
  1387
		drawingLink=false;
insilmaril@83
  1388
		// Check if we are over another branch
insilmaril@772
  1389
		if (dsti)
insilmaril@83
  1390
		{	
insilmaril@790
  1391
			tmpXLink->setEnd ( ((BranchItem*)dsti) );
insilmaril@95
  1392
			tmpXLink->updateXLink();
insilmaril@790
  1393
			tmpXLink->activate(); 
insilmaril@794
  1394
			model->saveState(
insilmaril@794
  1395
				tmpXLink,QString("delete ()"),
insilmaril@794
  1396
				dsti,QString("addXLink (\"%1\",\"%2\")").arg(model->getSelectString(tmpXLink->getBegin())).arg(model->getSelectString(dsti)),
insilmaril@794
  1397
				QString("Add xLink from %1 to %2").arg(model->getObjectName(tmpXLink->getBegin())).arg(model->getObjectName(dsti)) 
insilmaril@794
  1398
			);	
insilmaril@83
  1399
		} else
insilmaril@83
  1400
		{
insilmaril@790
  1401
			model->deleteItem(tmpXLink);
insilmaril@95
  1402
			tmpXLink=NULL;
insilmaril@83
  1403
		}
insilmaril@83
  1404
		return;
insilmaril@83
  1405
	}
insilmaril@83
  1406
	
insilmaril@0
  1407
    // Have we been moving something?
insilmaril@754
  1408
    if ( seli && movingObj ) 
insilmaril@0
  1409
    {	
insilmaril@776
  1410
		if (seli->getType()==TreeItem::Image)
insilmaril@175
  1411
		{
insilmaril@779
  1412
			FloatImageObj *fio=(FloatImageObj*)( ((MapItem*)seli)->getLMO());
insilmaril@776
  1413
			if(fio)
insilmaril@776
  1414
			{
insilmaril@776
  1415
				// Moved FloatObj. Maybe we need to reposition
insilmaril@792
  1416
				QString pold=qpointFToString(movingObj_orgRelPos);
insilmaril@792
  1417
				QString pnow=qpointFToString(fio->getRelPos());
insilmaril@776
  1418
				model->saveState(
insilmaril@776
  1419
					seli,
insilmaril@776
  1420
					"moveRel "+pold,
insilmaril@776
  1421
					seli,
insilmaril@776
  1422
					"moveRel "+pnow,
insilmaril@776
  1423
					QString("Move %1 to relative position %2").arg(model->getObjectName(seli)).arg(pnow));
insilmaril@424
  1424
insilmaril@776
  1425
				fio->getParObj()->requestReposition();
insilmaril@776
  1426
				model->reposition();
insilmaril@776
  1427
			}	
insilmaril@776
  1428
		}
insilmaril@175
  1429
insilmaril@787
  1430
		BranchItem *bi=model->getSelectedBranch();
insilmaril@753
  1431
		if (bi && bi->depth()==0)
insilmaril@497
  1432
		{	
insilmaril@753
  1433
            if (movingObj_orgPos != bi->getBranchObj()->getAbsPos())	// FIXME-3 check getBO here...
insilmaril@625
  1434
            {
insilmaril@792
  1435
                QString pold=qpointFToString(movingObj_orgPos);
insilmaril@792
  1436
                QString pnow=qpointFToString(bi->getBranchObj()->getAbsPos());		// FIXME-3 check getBO here...
insilmaril@753
  1437
insilmaril@720
  1438
                model->saveState(
insilmaril@753
  1439
                    bi,
insilmaril@625
  1440
                    "move "+pold,
insilmaril@753
  1441
                    bi,
insilmaril@625
  1442
                    "move "+pnow,
insilmaril@753
  1443
                    QString("Move mapcenter %1 to position %2").arg(model->getObjectName(bi)).arg(pnow));
insilmaril@625
  1444
            }
insilmaril@388
  1445
		}
insilmaril@645
  1446
	
insilmaril@773
  1447
		if (seli->isBranchLikeType() ) //(seli->getType() == TreeItem::Branch )
insilmaril@388
  1448
		{	// A branch was moved
insilmaril@777
  1449
			LinkableMapObj* lmosel=NULL;		
insilmaril@779
  1450
			lmosel=((MapItem*)seli)->getLMO();
insilmaril@760
  1451
				
insilmaril@0
  1452
			// save the position in case we link to mapcenter
insilmaril@424
  1453
			QPointF savePos=QPointF (lmosel->getAbsPos()  );
insilmaril@0
  1454
insilmaril@0
  1455
			// Reset the temporary drawn link to the original one
insilmaril@424
  1456
			lmosel->unsetParObjTmp();
insilmaril@0
  1457
insilmaril@366
  1458
			// For Redo we may need to save original selection
insilmaril@773
  1459
			QString preSelStr=model->getSelectString(seli);
insilmaril@93
  1460
insilmaril@94
  1461
			copyingObj=false;	
insilmaril@798
  1462
			if (dsti && objectMoved)
insilmaril@93
  1463
			{
insilmaril@691
  1464
				// We have a destination, relink to that
insilmaril@691
  1465
insilmaril@753
  1466
				BranchObj* bsel=model->getSelectedBranchObj();
insilmaril@366
  1467
insilmaril@684
  1468
				QString preParStr=model->getSelectString (bsel->getParObj());
insilmaril@755
  1469
				QString preNum=QString::number (seli->num(),10);
insilmaril@366
  1470
				QString preDstParStr;
insilmaril@767
  1471
				bool relinked;
insilmaril@366
  1472
insilmaril@366
  1473
				if (e->state() & Qt::ShiftModifier && dst->getParObj())
insilmaril@366
  1474
				{	// Link above dst
insilmaril@684
  1475
					preDstParStr=model->getSelectString (dst->getParObj());
insilmaril@767
  1476
					relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti->parent(),((BranchItem*)dsti)->num());
insilmaril@366
  1477
				} else 
insilmaril@366
  1478
				if (e->state() & Qt::ControlModifier && dst->getParObj())
insilmaril@366
  1479
				{
insilmaril@366
  1480
					// Link below dst
insilmaril@684
  1481
					preDstParStr=model->getSelectString (dst->getParObj());
insilmaril@767
  1482
					relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti->parent(),((BranchItem*)dsti)->num()+1);
insilmaril@366
  1483
				} else	
insilmaril@366
  1484
				{	// Append to dst
insilmaril@684
  1485
					preDstParStr=model->getSelectString(dst);
insilmaril@767
  1486
					relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti);
insilmaril@755
  1487
					if (dsti->depth()==0) bsel->move (savePos);
insilmaril@366
  1488
				} 
insilmaril@767
  1489
				if (relinked)
insilmaril@767
  1490
				{
insilmaril@767
  1491
					QString postSelStr=model->getSelectString(lmosel);
insilmaril@767
  1492
					QString postNum=QString::number (seli->num(),10);
insilmaril@366
  1493
insilmaril@776
  1494
					QString undoCom="relinkTo (\""+ 
insilmaril@767
  1495
						preParStr+ "\"," + preNum  +"," + 
insilmaril@767
  1496
						QString ("%1,%2").arg(movingObj_orgPos.x()).arg(movingObj_orgPos.y())+ ")";
insilmaril@366
  1497
insilmaril@776
  1498
					QString redoCom="relinkTo (\""+ 
insilmaril@767
  1499
						preDstParStr + "\"," + postNum + "," +
insilmaril@767
  1500
						QString ("%1,%2").arg(savePos.x()).arg(savePos.y())+ ")";
insilmaril@366
  1501
insilmaril@767
  1502
					model->saveState (
insilmaril@767
  1503
						postSelStr,undoCom,
insilmaril@767
  1504
						preSelStr, redoCom,
insilmaril@767
  1505
						QString("Relink %1 to %2").arg(model->getObjectName(bsel)).arg(model->getObjectName(dst)) );
insilmaril@691
  1506
insilmaril@767
  1507
				}
insilmaril@163
  1508
			} else
insilmaril@691
  1509
			{
insilmaril@815
  1510
				// No destination, undo  temporary move	
insilmaril@691
  1511
insilmaril@755
  1512
				if (seli->depth()==1)
insilmaril@366
  1513
				{
insilmaril@366
  1514
					// The select string might be different _after_ moving around.
insilmaril@366
  1515
					// Therefor reposition and then use string of old selection, too
insilmaril@645
  1516
					model->reposition();
insilmaril@366
  1517
insilmaril@625
  1518
                    QPointF rp(lmosel->getRelPos());
insilmaril@625
  1519
                    if (rp != movingObj_orgRelPos)
insilmaril@625
  1520
                    {
insilmaril@792
  1521
                        QString ps=qpointFToString(rp);
insilmaril@720
  1522
                        model->saveState(
insilmaril@792
  1523
                            model->getSelectString(lmosel), "moveRel "+qpointFToString(movingObj_orgRelPos), 
insilmaril@625
  1524
                            preSelStr, "moveRel "+ps, 
insilmaril@726
  1525
                            QString("Move %1 to relative position %2").arg(model->getObjectName(lmosel)).arg(ps));
insilmaril@625
  1526
                    }
insilmaril@366
  1527
				}
insilmaril@691
  1528
insilmaril@691
  1529
				// Draw the original link, before selection was moved around
insilmaril@824
  1530
				if (settings.value("/animation/use",true).toBool() 
insilmaril@824
  1531
					&& seli->depth()>1
insilmaril@824
  1532
//					&& distance (lmosel->getRelPos(),movingObj_orgRelPos)<3
insilmaril@824
  1533
				) 
insilmaril@691
  1534
				{
insilmaril@720
  1535
					lmosel->setRelPos();	// calc relPos first for starting point
insilmaril@720
  1536
					
insilmaril@691
  1537
					model->startAnimation(
insilmaril@723
  1538
						(BranchObj*)lmosel,
insilmaril@691
  1539
						lmosel->getRelPos(),
insilmaril@720
  1540
						movingObj_orgRelPos
insilmaril@691
  1541
					);	
insilmaril@691
  1542
				} else	
insilmaril@691
  1543
					model->reposition();
insilmaril@691
  1544
			}
insilmaril@0
  1545
		}
insilmaril@775
  1546
		model->emitSelectionChanged();  //FIXME-3 needed? at least not after pos of selection has changed...
insilmaril@408
  1547
		// Finally resize scene, if needed
insilmaril@408
  1548
		scene()->update();
insilmaril@0
  1549
		movingObj=NULL;		
insilmaril@798
  1550
		objectMoved=false;
insilmaril@366
  1551
insilmaril@0
  1552
	} else 
insilmaril@83
  1553
		// maybe we moved View: set old cursor
insilmaril@366
  1554
		setCursor (Qt::ArrowCursor);
insilmaril@83
  1555
    
insilmaril@0
  1556
}
insilmaril@0
  1557
insilmaril@408
  1558
void MapEditor::mouseDoubleClickEvent(QMouseEvent* e)
insilmaril@0
  1559
{
insilmaril@802
  1560
	if (debug) 
insilmaril@802
  1561
	{
insilmaril@802
  1562
		cout << "ME p="<<mapToScene (e->pos())<<"  scrollBarPos="<<getScrollBarPos();
insilmaril@802
  1563
		cout << "  min="<<QPointF(horizontalScrollBar()->minimum(),verticalScrollBar()->minimum());
insilmaril@802
  1564
		cout << endl;
insilmaril@802
  1565
	}
insilmaril@798
  1566
insilmaril@798
  1567
insilmaril@720
  1568
	if (model->isSelectionBlocked() ) 
insilmaril@491
  1569
	{
insilmaril@491
  1570
		e->ignore();
insilmaril@491
  1571
		return;
insilmaril@491
  1572
	}
insilmaril@491
  1573
insilmaril@366
  1574
	if (e->button() == Qt::LeftButton )
insilmaril@0
  1575
	{
insilmaril@408
  1576
		QPointF p = mapToScene(e->pos());
insilmaril@754
  1577
		TreeItem *ti=findMapItem (p, NULL);
insilmaril@754
  1578
		if (ti) {	// MapObj was found
insilmaril@0
  1579
			// First select the MapObj than edit heading
insilmaril@754
  1580
			model->select (ti);
insilmaril@720
  1581
			editHeading();
insilmaril@0
  1582
		}
insilmaril@0
  1583
	}
insilmaril@0
  1584
}
insilmaril@0
  1585
insilmaril@0
  1586
void MapEditor::resizeEvent (QResizeEvent* e)
insilmaril@0
  1587
{
insilmaril@408
  1588
	QGraphicsView::resizeEvent( e );
insilmaril@0
  1589
}
insilmaril@0
  1590
insilmaril@412
  1591
void MapEditor::dragEnterEvent(QDragEnterEvent *event)
insilmaril@119
  1592
{
insilmaril@417
  1593
	//for (unsigned int i=0;event->format(i);i++) // Debug mime type
insilmaril@417
  1594
	//	cerr << event->format(i) << endl;
insilmaril@417
  1595
insilmaril@417
  1596
	if (event->mimeData()->hasImage())
insilmaril@417
  1597
		event->acceptProposedAction();
insilmaril@417
  1598
	else	
insilmaril@417
  1599
		if (event->mimeData()->hasUrls())
insilmaril@417
  1600
			event->acceptProposedAction();
insilmaril@417
  1601
}
insilmaril@417
  1602
insilmaril@531
  1603
void MapEditor::dragMoveEvent(QDragMoveEvent *)
insilmaril@417
  1604
{
insilmaril@417
  1605
}
insilmaril@417
  1606
insilmaril@417
  1607
void MapEditor::dragLeaveEvent(QDragLeaveEvent *event)
insilmaril@417
  1608
{
insilmaril@417
  1609
	event->accept();
insilmaril@119
  1610
}
insilmaril@129
  1611
insilmaril@412
  1612
void MapEditor::dropEvent(QDropEvent *event)
insilmaril@129
  1613
{
insilmaril@787
  1614
	BranchItem *selbi=model->getSelectedBranch();
insilmaril@753
  1615
	if (selbi)
insilmaril@128
  1616
	{
insilmaril@505
  1617
		if (debug)
insilmaril@820
  1618
		{
insilmaril@505
  1619
			foreach (QString format,event->mimeData()->formats()) 
insilmaril@658
  1620
				cout << "MapEditor: Dropped format: "<<qPrintable (format)<<endl;
insilmaril@820
  1621
			foreach (QString url,event->mimeData()->urls())
insilmaril@820
  1622
				cout << "  URL:"<<url.toStdString()<<endl;
insilmaril@820
  1623
			//foreach (QString plain,event->mimeData()->text())
insilmaril@820
  1624
			//	cout << "   PLAIN:"<<plain.toStdString()<<endl;
insilmaril@820
  1625
			QByteArray ba=event->mimeData()->data("STRING");
insilmaril@820
  1626
			
insilmaril@820
  1627
			QString s;
insilmaril@820
  1628
			s=ba;
insilmaril@820
  1629
			cout << "  STRING:" <<s.toStdString()<<endl;
insilmaril@497
  1630
insilmaril@820
  1631
			ba=event->mimeData()->data("TEXT");
insilmaril@820
  1632
			s=ba;
insilmaril@820
  1633
			cout << "    TEXT:" <<s.toStdString()<<endl;
insilmaril@497
  1634
insilmaril@820
  1635
			ba=event->mimeData()->data("COMPOUND_TEXT");
insilmaril@820
  1636
			s=ba;
insilmaril@820
  1637
			cout << "   CTEXT:" <<s.toStdString()<<endl;
insilmaril@820
  1638
insilmaril@820
  1639
			ba=event->mimeData()->data("text/x-moz-url");
insilmaril@820
  1640
			s=ba;
insilmaril@820
  1641
			cout << "   x-moz-url:" <<s.toStdString()<<endl;
insilmaril@820
  1642
			foreach (char b,ba)
insilmaril@820
  1643
				if (b!=0) cout << "b="<<b<<endl;
insilmaril@820
  1644
		}
insilmaril@820
  1645
insilmaril@417
  1646
		if (event->mimeData()->hasImage()) 
insilmaril@128
  1647
		{
insilmaril@417
  1648
			 QVariant imageData = event->mimeData()->imageData();
insilmaril@720
  1649
			 model->addFloatImage (qvariant_cast<QPixmap>(imageData));
insilmaril@417
  1650
		} else
insilmaril@417
  1651
		if (event->mimeData()->hasUrls())
insilmaril@128
  1652
		{
insilmaril@820
  1653
			//model->selectLastBranch();
insilmaril@820
  1654
			QList <QUrl> uris=event->mimeData()->urls();
insilmaril@417
  1655
			QStringList files;
insilmaril@417
  1656
			QString s;
insilmaril@417
  1657
			QString heading;
insilmaril@753
  1658
			BranchItem *bi;
insilmaril@428
  1659
			for (int i=0; i<uris.count();i++)
insilmaril@408
  1660
			{
insilmaril@428
  1661
				// Workaround to avoid adding empty branches
insilmaril@428
  1662
				if (!uris.at(i).toString().isEmpty())
insilmaril@417
  1663
				{
insilmaril@775
  1664
					bi=model->addNewBranch();
insilmaril@753
  1665
					if (bi)
insilmaril@417
  1666
					{
insilmaril@820
  1667
						model->select(bi);
insilmaril@821
  1668
						QString u=uris.at(i).toString();
insilmaril@428
  1669
						s=uris.at(i).toLocalFile();
insilmaril@428
  1670
						if (!s.isEmpty()) 
insilmaril@428
  1671
						{
insilmaril@625
  1672
						   QString file = QDir::fromNativeSeparators(s);
insilmaril@428
  1673
						   heading = QFileInfo(file).baseName();
insilmaril@428
  1674
						   files.append(file);
insilmaril@428
  1675
						   if (file.endsWith(".vym", false))
insilmaril@820
  1676
							   model->setVymLink(file);
insilmaril@428
  1677
						   else
insilmaril@821
  1678
							   model->setURL(u);
insilmaril@428
  1679
					   } else 
insilmaril@428
  1680
					   {
insilmaril@821
  1681
							model->setURL(u);
insilmaril@821
  1682
insilmaril@821
  1683
/*
insilmaril@821
  1684
							// Automatically try to fetch data from Bugzilla
insilmaril@821
  1685
							if (settings.value( "/mainwindow/showTestMenu",false).toBool() 
insilmaril@821
  1686
								&& u.contains ("https://bugzilla.novell.com"))
insilmaril@821
  1687
								model->getBugzillaData();
insilmaril@821
  1688
*/								
insilmaril@428
  1689
					   }
insilmaril@428
  1690
insilmaril@428
  1691
					   if (!heading.isEmpty())
insilmaril@820
  1692
						   model->setHeading(heading);
insilmaril@428
  1693
					   else
insilmaril@821
  1694
						   model->setHeading(u);
insilmaril@753
  1695
						   
insilmaril@820
  1696
						model->select (bi->parent());	   
insilmaril@428
  1697
					}
insilmaril@417
  1698
				}
insilmaril@417
  1699
			}
insilmaril@820
  1700
			//model->reposition();
insilmaril@417
  1701
		}
insilmaril@417
  1702
	}	
insilmaril@417
  1703
	event->acceptProposedAction();
insilmaril@417
  1704
}
insilmaril@417
  1705
insilmaril@776
  1706
void MapEditor::updateSelection(QItemSelection newsel,QItemSelection oldsel)
insilmaril@728
  1707
{
insilmaril@779
  1708
	// Note: Here we are prepared for multiple selections, though this 
insilmaril@779
  1709
	// is not yet implemented elsewhere
insilmaril@779
  1710
insilmaril@776
  1711
	// Here in MapEditor we can only select Branches and Images
insilmaril@786
  1712
	QList <TreeItem*> treeItemsNew;
insilmaril@786
  1713
	QList <TreeItem*> treeItemsOld;
insilmaril@786
  1714
insilmaril@807
  1715
	QModelIndex newIndex;
insilmaril@807
  1716
insilmaril@796
  1717
	bool do_reposition=false;
insilmaril@796
  1718
insilmaril@776
  1719
	QModelIndex ix;
insilmaril@776
  1720
	foreach (ix,newsel.indexes() )
insilmaril@776
  1721
	{
insilmaril@776
  1722
		TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
insilmaril@787
  1723
		if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image )
insilmaril@786
  1724
			if (!treeItemsNew.contains(ti)) treeItemsNew.append (ti);
insilmaril@786
  1725
	}
insilmaril@786
  1726
	foreach (ix,oldsel.indexes() )
insilmaril@786
  1727
	{
insilmaril@786
  1728
		TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
insilmaril@787
  1729
		if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image )
insilmaril@786
  1730
			if (!treeItemsOld.contains(ti)) treeItemsOld.append (ti);
insilmaril@776
  1731
	}
insilmaril@776
  1732
insilmaril@779
  1733
	// Trim list of selection rectangles 
insilmaril@786
  1734
	while (treeItemsNew.count() < selboxList.count() )
insilmaril@776
  1735
		delete selboxList.takeFirst();
insilmaril@776
  1736
insilmaril@775
  1737
	// Take care to tmp scroll/unscroll
insilmaril@775
  1738
	if (!oldsel.isEmpty())
insilmaril@775
  1739
	{
insilmaril@775
  1740
		QModelIndex ix=oldsel.indexes().first(); 
insilmaril@775
  1741
		if (ix.isValid() )
insilmaril@775
  1742
		{
insilmaril@775
  1743
			TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
insilmaril@779
  1744
			if (ti)
insilmaril@775
  1745
			{
insilmaril@779
  1746
				if (ti->isBranchLikeType() )
insilmaril@779
  1747
				{
insilmaril@779
  1748
					// reset tmp scrolled branches
insilmaril@779
  1749
					BranchItem *bi=(BranchItem*)ti;
insilmaril@796
  1750
					if (bi->resetTmpUnscroll() )
insilmaril@796
  1751
						do_reposition=true;
insilmaril@779
  1752
				}
insilmaril@779
  1753
				if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image)
insilmaril@779
  1754
					// Hide link if not needed
insilmaril@779
  1755
					((MapItem*)ti)->getLMO()->updateVisibility();
insilmaril@775
  1756
			}
insilmaril@775
  1757
		}
insilmaril@775
  1758
	}
insilmaril@775
  1759
insilmaril@786
  1760
	if (!treeItemsNew.isEmpty())
insilmaril@775
  1761
	{
insilmaril@775
  1762
		QModelIndex ix=newsel.indexes().first(); 
insilmaril@775
  1763
		if (ix.isValid() )
insilmaril@775
  1764
		{
insilmaril@807
  1765
			newIndex=ix;
insilmaril@807
  1766
insilmaril@775
  1767
			// Temporary unscroll if necessary
insilmaril@775
  1768
			TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
insilmaril@775
  1769
			if (ti->isBranchLikeType() )
insilmaril@775
  1770
			{
insilmaril@775
  1771
				BranchItem *bi=(BranchItem*)ti;
insilmaril@775
  1772
				if (bi->hasScrolledParent(bi) )
insilmaril@796
  1773
				{
insilmaril@796
  1774
					if (bi->parentBranch()->tmpUnscroll() )
insilmaril@796
  1775
						do_reposition=true;
insilmaril@796
  1776
				}	
insilmaril@775
  1777
			}
insilmaril@779
  1778
			if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image)
insilmaril@779
  1779
				// Show link if needed
insilmaril@779
  1780
				((MapItem*)ti)->getLMO()->updateVisibility();
insilmaril@775
  1781
		}
insilmaril@775
  1782
	}
insilmaril@796
  1783
	if (do_reposition) model->reposition();
insilmaril@775
  1784
insilmaril@731
  1785
	// Reduce rectangles
insilmaril@786
  1786
	while (treeItemsNew.count() < selboxList.count() )
insilmaril@731
  1787
		delete selboxList.takeFirst();
insilmaril@731
  1788
insilmaril@731
  1789
	// Add additonal rectangles
insilmaril@731
  1790
	QGraphicsRectItem *sb;
insilmaril@786
  1791
	while (treeItemsNew.count() > selboxList.count() )
insilmaril@728
  1792
	{
insilmaril@731
  1793
		sb = mapScene->addRect(
insilmaril@731
  1794
			QRectF(0,0,0,0), 
insilmaril@731
  1795
			QPen(selectionColor),
insilmaril@731
  1796
			selectionColor);
insilmaril@731
  1797
		sb->setZValue(Z_SELBOX);
insilmaril@731
  1798
		sb->show();
insilmaril@731
  1799
		selboxList.append (sb);
insilmaril@728
  1800
	}
insilmaril@731
  1801
insilmaril@731
  1802
	// Reposition rectangles
insilmaril@731
  1803
	QRectF bbox;
insilmaril@732
  1804
	QModelIndex index;
insilmaril@732
  1805
insilmaril@732
  1806
	LinkableMapObj *lmo;
insilmaril@786
  1807
	for (int i=0; i<treeItemsNew.count();++i)
insilmaril@731
  1808
	{
insilmaril@786
  1809
		lmo=((MapItem*)treeItemsNew.at(i) )->getLMO();
insilmaril@731
  1810
		bbox=lmo->getBBox();
insilmaril@786
  1811
		sb=selboxList.at(i);
insilmaril@731
  1812
		sb->setRect (
insilmaril@731
  1813
			bbox.x(),bbox.y(), 
insilmaril@731
  1814
			bbox.width(), bbox.height());
insilmaril@731
  1815
		sb->setPen (selectionColor);	
insilmaril@731
  1816
		sb->setBrush (selectionColor);	
insilmaril@731
  1817
		i++;
insilmaril@731
  1818
	}
insilmaril@775
  1819
insilmaril@804
  1820
	scene()->update();  
insilmaril@729
  1821
}
insilmaril@728
  1822
insilmaril@753
  1823
void MapEditor::updateData (const QModelIndex &sel)
insilmaril@729
  1824
{
insilmaril@753
  1825
	TreeItem *ti= static_cast<TreeItem*>(sel.internalPointer());
insilmaril@732
  1826
insilmaril@746
  1827
/* testing
insilmaril@753
  1828
	cout << "ME::updateData\n";
insilmaril@732
  1829
insilmaril@753
  1830
	cout << "  ti="<<ti<<endl;
insilmaril@753
  1831
	cout << "  h="<<ti->getHeading().toStdString()<<endl;
insilmaril@729
  1832
	*/
insilmaril@753
  1833
	
insilmaril@753
  1834
	if (ti->isBranchLikeType())
insilmaril@753
  1835
	{
insilmaril@785
  1836
	//	cout << "  ->updating...\n";
insilmaril@779
  1837
		BranchObj *bo=(BranchObj*) ( ((MapItem*)ti)->getLMO());
insilmaril@767
  1838
		bo->updateData();
insilmaril@753
  1839
	}
insilmaril@726
  1840
}
insilmaril@412
  1841
insilmaril@728
  1842
void MapEditor::setSelectionColor (QColor col)
insilmaril@728
  1843
{
insilmaril@728
  1844
	selectionColor=col;
insilmaril@732
  1845
	QItemSelection sel=model->getSelectionModel()->selection();
insilmaril@732
  1846
	updateSelection(sel,sel);
insilmaril@728
  1847
}
insilmaril@728
  1848
insilmaril@753
  1849
insilmaril@728
  1850
QColor MapEditor::getSelectionColor ()
insilmaril@728
  1851
{
insilmaril@728
  1852
	return selectionColor;
insilmaril@728
  1853
}
insilmaril@728
  1854
insilmaril@728
  1855