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