branchobj.cpp
author insilmaril
Thu, 18 Mar 2010 11:38:14 +0000
changeset 838 e4a44912646c
parent 836 62d621e3e550
child 847 43268373032d
permissions -rw-r--r--
Added missing return values
insilmaril@0
     1
#include "branchobj.h"
insilmaril@616
     2
insilmaril@822
     3
#include "attributeitem.h"
insilmaril@749
     4
#include "branchitem.h"
insilmaril@643
     5
#include "geometry.h"
insilmaril@0
     6
#include "mapeditor.h"
insilmaril@794
     7
#include "mainwindow.h"   
insilmaril@616
     8
#include "misc.h"
insilmaril@616
     9
insilmaril@767
    10
extern FlagRow *standardFlagsMaster;
insilmaril@767
    11
extern FlagRow *systemFlagsMaster;
insilmaril@0
    12
insilmaril@0
    13
/////////////////////////////////////////////////////////////////
insilmaril@0
    14
// BranchObj
insilmaril@0
    15
/////////////////////////////////////////////////////////////////
insilmaril@0
    16
insilmaril@790
    17
BranchObj::BranchObj (QGraphicsScene* s,TreeItem *ti):OrnamentedObj (s)
insilmaril@0
    18
{
insilmaril@755
    19
//    cout << "Const BranchObj (s)  \n";
insilmaril@408
    20
    scene=s;
insilmaril@790
    21
	treeItem=ti;
insilmaril@794
    22
	BranchItem *pi=(BranchItem*)(ti->parent());
insilmaril@794
    23
	if (pi && pi!=ti->getModel()->getRootItem() )
insilmaril@794
    24
		parObj=pi->getLMO();
insilmaril@794
    25
	else
insilmaril@794
    26
		parObj=NULL;
insilmaril@755
    27
	init();
insilmaril@0
    28
}
insilmaril@0
    29
insilmaril@0
    30
BranchObj::~BranchObj ()
insilmaril@0
    31
{
insilmaril@757
    32
//	cout << "Destr BranchObj of "<<this<<" ("<<treeItem->getHeading().toStdString()<<")"<<endl;
insilmaril@757
    33
insilmaril@722
    34
	// If I'm animated, I need to un-animate myself first
insilmaril@722
    35
	if (anim.isAnimated() )
insilmaril@722
    36
	{
insilmaril@722
    37
		anim.setAnimated (false);
insilmaril@773
    38
		VymModel *model=treeItem->getModel();
insilmaril@722
    39
		model->stopAnimation (this);
insilmaril@722
    40
	}
insilmaril@722
    41
insilmaril@83
    42
	clear();
insilmaril@0
    43
}
insilmaril@0
    44
insilmaril@0
    45
void BranchObj::init () 
insilmaril@0
    46
{
insilmaril@83
    47
	if (parObj)
insilmaril@83
    48
	{
insilmaril@83
    49
		absPos=getRandPos();
insilmaril@83
    50
		absPos+=parObj->getChildPos();
insilmaril@83
    51
	}
insilmaril@0
    52
}
insilmaril@0
    53
insilmaril@0
    54
void BranchObj::copy (BranchObj* other)
insilmaril@0
    55
{
insilmaril@0
    56
    OrnamentedObj::copy(other);
insilmaril@0
    57
insilmaril@0
    58
	setVisibility (other->visible);
insilmaril@0
    59
insilmaril@0
    60
    positionBBox();
insilmaril@0
    61
}
insilmaril@0
    62
insilmaril@0
    63
void BranchObj::clear() 
insilmaril@0
    64
{
insilmaril@755
    65
	//setVisibility (true); //FIXME-4 needed?
insilmaril@425
    66
insilmaril@95
    67
	while (!xlink.isEmpty())
insilmaril@406
    68
		delete xlink.takeFirst();
insilmaril@0
    69
}
insilmaril@0
    70
insilmaril@819
    71
void BranchObj::setParObjTmp(LinkableMapObj* dst, QPointF m, int off)	
insilmaril@0
    72
{
insilmaril@796
    73
	// Temporary link to dst
insilmaril@0
    74
	// m is position of mouse pointer 
insilmaril@796
    75
	// offset 0: default 1: below dst   -1 above dst  (if possible)
insilmaril@0
    76
insilmaril@796
    77
	BranchItem *dsti=(BranchItem*)(dst->getTreeItem());
insilmaril@796
    78
insilmaril@796
    79
	BranchItem *pi=(BranchItem*)(dsti->parent());
insilmaril@775
    80
	int pi_depth=pi->depth();
insilmaril@815
    81
	BranchObj* bodst=(BranchObj*)dst;
insilmaril@0
    82
insilmaril@798
    83
	if (!tmpParent) 
insilmaril@798
    84
	{
insilmaril@798
    85
		tmpParent=true;
insilmaril@798
    86
		parObjTmpBuf=parObj;
insilmaril@798
    87
	}
insilmaril@796
    88
insilmaril@775
    89
	if (pi_depth<2) off=0;
insilmaril@0
    90
	if (off==0)
insilmaril@0
    91
		link2ParPos=false;
insilmaril@0
    92
	else
insilmaril@0
    93
		link2ParPos=true;
insilmaril@815
    94
	parObj=bodst;
insilmaril@0
    95
insilmaril@796
    96
	setLinkStyle (dst->getDefLinkStyle (dsti));
insilmaril@814
    97
 
insilmaril@0
    98
	// Move temporary to new position at destination
insilmaril@0
    99
	// Usually the positioning would be done by reposition(),
insilmaril@0
   100
	// but then also the destination branch would "Jump" around...
insilmaril@0
   101
	// Better just do it approximately
insilmaril@814
   102
	if (dsti->depth()==0)	
insilmaril@786
   103
	{	// new parent is a mapcenter
insilmaril@815
   104
		Vector v= ( m - bodst->getChildPos());
insilmaril@798
   105
		if (v.x()<0) v.setX( v.x()-bbox.width() );
insilmaril@798
   106
		v.normalize();
insilmaril@798
   107
		v.scale (100);
insilmaril@798
   108
		move2RelPos (v.toQPointF());
insilmaril@0
   109
	} else
insilmaril@0
   110
	{	
insilmaril@408
   111
		qreal y;
insilmaril@0
   112
		if (off==0)
insilmaril@0
   113
		{
insilmaril@0
   114
			// new parent is just a branch, link to it
insilmaril@815
   115
			QRectF t=bodst->getBBoxSizeWithChildren();
insilmaril@796
   116
			if (dsti->getLastBranch())
insilmaril@0
   117
				y=t.y() + t.height() ;
insilmaril@0
   118
			else
insilmaril@0
   119
				y=t.y();
insilmaril@0
   120
insilmaril@815
   121
			y=t.bottom();
insilmaril@815
   122
insilmaril@0
   123
		} else
insilmaril@0
   124
		{
insilmaril@0
   125
			if (off<0)
insilmaril@796
   126
				// we want to link above dst
insilmaril@815
   127
				y=bodst->y() - height() + 5;
insilmaril@0
   128
			else	
insilmaril@796
   129
				// we want to link below dst
insilmaril@0
   130
				// Bottom of sel should be 5 pixels above
insilmaril@0
   131
				// the bottom of the branch _below_ the target:
insilmaril@0
   132
				// Don't try to find that branch, guess 12 pixels
insilmaril@815
   133
				y=bodst->getChildPos().y()  -height() + 12; 
insilmaril@0
   134
		}	
insilmaril@815
   135
		if (bodst->getOrientation()==LinkableMapObj::LeftOfCenter)
insilmaril@815
   136
			move ( bodst->getChildPos().x() - linkwidth, y );
insilmaril@0
   137
		else	
insilmaril@815
   138
			move (bodst->getChildPos().x() + linkwidth, y );
insilmaril@0
   139
	}	
insilmaril@0
   140
insilmaril@779
   141
	// updateLinkGeometry is called implicitly in move
insilmaril@417
   142
	requestReposition();	
insilmaril@0
   143
}
insilmaril@0
   144
insilmaril@0
   145
void BranchObj::unsetParObjTmp()
insilmaril@0
   146
{
insilmaril@798
   147
	if (tmpParent) 
insilmaril@0
   148
	{
insilmaril@798
   149
		tmpParent=false;
insilmaril@0
   150
		link2ParPos=false;
insilmaril@0
   151
		parObj=parObjTmpBuf;
insilmaril@0
   152
		parObjTmpBuf=NULL;
insilmaril@786
   153
		setLinkStyle (getDefLinkStyle(treeItem->parent() ) );
insilmaril@779
   154
		updateLinkGeometry();
insilmaril@0
   155
	}		
insilmaril@0
   156
}
insilmaril@0
   157
insilmaril@0
   158
void BranchObj::setVisibility(bool v, int toDepth)
insilmaril@0
   159
{
insilmaril@749
   160
	BranchItem *bi=(BranchItem*)treeItem;
insilmaril@755
   161
    if (bi->depth() <= toDepth)
insilmaril@0
   162
    {
insilmaril@0
   163
		frame->setVisibility(v);
insilmaril@0
   164
		heading->setVisibility(v);
insilmaril@0
   165
		systemFlags->setVisibility(v);
insilmaril@0
   166
		standardFlags->setVisibility(v);
insilmaril@0
   167
		LinkableMapObj::setVisibility (v);
insilmaril@730
   168
		int i;
insilmaril@785
   169
		for (i=0; i<treeItem->imageCount(); ++i)
insilmaril@785
   170
			treeItem->getImageObjNum(i)->setVisibility (v);
insilmaril@790
   171
		for (i=0; i<treeItem->xlinkCount(); ++i)	
insilmaril@790
   172
			treeItem->getXLinkObjNum(i)->setVisibility ();	
insilmaril@730
   173
insilmaril@721
   174
		// Only change children, if I am not scrolled
insilmaril@749
   175
		if (! bi->isScrolled() && (bi->depth() < toDepth))
insilmaril@0
   176
		{
insilmaril@785
   177
			// Now go recursivly through all children //FIXME-3 are there multiple calls for lower level items???
insilmaril@750
   178
			for (i=0; i<treeItem->branchCount(); ++i)
insilmaril@753
   179
				treeItem->getBranchObjNum(i)->setVisibility (v,toDepth);	
insilmaril@0
   180
		}
insilmaril@0
   181
    } // depth <= toDepth	
insilmaril@0
   182
	requestReposition();
insilmaril@0
   183
}	
insilmaril@0
   184
insilmaril@0
   185
void BranchObj::setVisibility(bool v)
insilmaril@0
   186
{
insilmaril@0
   187
    setVisibility (v,MAX_DEPTH);
insilmaril@0
   188
}
insilmaril@0
   189
insilmaril@0
   190
insilmaril@0
   191
void BranchObj::setLinkColor ()
insilmaril@0
   192
{
insilmaril@0
   193
	// Overloaded from LinkableMapObj
insilmaril@0
   194
	// BranchObj can use color of heading
insilmaril@0
   195
insilmaril@773
   196
	VymModel *model=treeItem->getModel();
insilmaril@721
   197
	if (model)
insilmaril@611
   198
	{
insilmaril@721
   199
		if (model->getMapLinkColorHint()==HeadingColor)
insilmaril@164
   200
			LinkableMapObj::setLinkColor (heading->getColor() );
insilmaril@164
   201
		else	
insilmaril@164
   202
			LinkableMapObj::setLinkColor ();
insilmaril@611
   203
	}		
insilmaril@0
   204
}
insilmaril@0
   205
insilmaril@753
   206
void BranchObj::updateContentSize()
insilmaril@753
   207
{
insilmaril@753
   208
	calcBBoxSize();
insilmaril@753
   209
	positionBBox();
insilmaril@753
   210
	requestReposition();
insilmaril@100
   211
}
insilmaril@0
   212
insilmaril@333
   213
void BranchObj::positionContents()
insilmaril@333
   214
{
insilmaril@785
   215
	for (int i=0; i<treeItem->imageCount(); ++i)
insilmaril@785
   216
		treeItem->getImageObjNum(i)->reposition();
insilmaril@333
   217
	OrnamentedObj::positionContents();
insilmaril@333
   218
}
insilmaril@0
   219
insilmaril@0
   220
void BranchObj::move (double x, double y)
insilmaril@0
   221
{
insilmaril@0
   222
	OrnamentedObj::move (x,y);
insilmaril@0
   223
    positionBBox();
insilmaril@0
   224
}
insilmaril@0
   225
insilmaril@408
   226
void BranchObj::move (QPointF p)
insilmaril@0
   227
{
insilmaril@0
   228
	move (p.x(), p.y());
insilmaril@0
   229
}
insilmaril@0
   230
insilmaril@0
   231
void BranchObj::moveBy (double x, double y)
insilmaril@0
   232
{
insilmaril@0
   233
	OrnamentedObj::moveBy (x,y);
insilmaril@750
   234
	for (int i=0; i<treeItem->branchCount(); ++i)
insilmaril@753
   235
		treeItem->getBranchObjNum(i)->moveBy (x,y);
insilmaril@333
   236
    positionBBox();
insilmaril@0
   237
}
insilmaril@83
   238
	
insilmaril@408
   239
void BranchObj::moveBy (QPointF p)
insilmaril@0
   240
{
insilmaril@0
   241
	moveBy (p.x(), p.y());
insilmaril@0
   242
}
insilmaril@0
   243
insilmaril@0
   244
insilmaril@0
   245
void BranchObj::positionBBox()
insilmaril@0
   246
{
insilmaril@408
   247
	QPointF ap=getAbsPos();
insilmaril@223
   248
	bbox.moveTopLeft (ap);
insilmaril@169
   249
	positionContents();
insilmaril@0
   250
insilmaril@0
   251
	// set the frame
insilmaril@408
   252
	frame->setRect(QRectF(bbox.x(),bbox.y(),bbox.width(),bbox.height() ) );
insilmaril@83
   253
insilmaril@790
   254
	//Update links to other branches
insilmaril@790
   255
	XLinkObj *xlo;
insilmaril@790
   256
	for (int i=0; i<treeItem->xlinkCount(); ++i)	
insilmaril@790
   257
	{
insilmaril@790
   258
		xlo=treeItem->getXLinkObjNum(i);
insilmaril@790
   259
		if (xlo) xlo->updateXLink();
insilmaril@790
   260
	}	
insilmaril@0
   261
}
insilmaril@0
   262
insilmaril@0
   263
void BranchObj::calcBBoxSize()
insilmaril@0
   264
{
insilmaril@408
   265
    QSizeF heading_r=heading->getSize();
insilmaril@408
   266
    qreal heading_w=(qreal) heading_r.width() ;
insilmaril@408
   267
    qreal heading_h=(qreal) heading_r.height() ;
insilmaril@767
   268
    QSizeF sysflags_r=systemFlags->getSize();
insilmaril@767
   269
	qreal sysflags_h=sysflags_r.height();
insilmaril@767
   270
	qreal sysflags_w=sysflags_r.width();
insilmaril@767
   271
    QSizeF stanflags_r=standardFlags->getSize();
insilmaril@767
   272
	qreal stanflags_h=stanflags_r.height();
insilmaril@767
   273
	qreal stanflags_w=stanflags_r.width();
insilmaril@408
   274
    qreal w;
insilmaril@408
   275
    qreal h;
insilmaril@0
   276
insilmaril@0
   277
	// set width to sum of all widths
insilmaril@0
   278
	w=heading_w + sysflags_w + stanflags_w;
insilmaril@0
   279
	// set height to maximum needed height
insilmaril@0
   280
	h=max (sysflags_h,stanflags_h);
insilmaril@0
   281
	h=max (h,heading_h);
insilmaril@0
   282
insilmaril@223
   283
	// Save the dimension of flags and heading
insilmaril@408
   284
	ornamentsBBox.setSize ( QSizeF(w,h));
insilmaril@223
   285
insilmaril@223
   286
	// clickBox includes Flags and Heading
insilmaril@223
   287
    clickBox.setSize (ornamentsBBox.size() );
insilmaril@169
   288
insilmaril@169
   289
	// Floatimages 
insilmaril@408
   290
	QPointF rp;
insilmaril@169
   291
insilmaril@169
   292
	topPad=botPad=leftPad=rightPad=0;
insilmaril@785
   293
	bool incV=((BranchItem*)treeItem)->getIncludeImagesVer();
insilmaril@785
   294
	bool incH=((BranchItem*)treeItem)->getIncludeImagesHor();
insilmaril@785
   295
	if (incH || incV)
insilmaril@169
   296
	{
insilmaril@785
   297
		FloatImageObj *fio;
insilmaril@785
   298
		for (int i=0; i<treeItem->imageCount(); ++i )	
insilmaril@169
   299
		{
insilmaril@785
   300
			fio=treeItem->getImageObjNum(i);
insilmaril@785
   301
			rp=fio->getRelPos();
insilmaril@785
   302
			if (incV)
insilmaril@169
   303
			{
insilmaril@785
   304
				if (rp.y() < 0) 
insilmaril@785
   305
					topPad=max (topPad,-rp.y()-h);
insilmaril@785
   306
				if (rp.y()+fio->height() > 0)
insilmaril@785
   307
					botPad=max (botPad,rp.y()+fio->height());
insilmaril@785
   308
			}		
insilmaril@785
   309
			if (incH)
insilmaril@785
   310
			{
insilmaril@785
   311
				if (orientation==LinkableMapObj::RightOfCenter)
insilmaril@169
   312
				{
insilmaril@785
   313
					if (-rp.x()-w > 0) 
insilmaril@785
   314
						leftPad=max (leftPad,-rp.x()-w);
insilmaril@785
   315
					if (rp.x()+fio->width() > 0)
insilmaril@785
   316
						rightPad=max (rightPad,rp.x()+fio->width());
insilmaril@785
   317
				} else
insilmaril@213
   318
				{
insilmaril@785
   319
					if (rp.x()< 0) 
insilmaril@785
   320
						leftPad=max (leftPad,-rp.x());
insilmaril@785
   321
					if (rp.x()+fio->width() > w)
insilmaril@785
   322
						rightPad=max (rightPad,rp.x()+fio->width()-w);
insilmaril@785
   323
				}
insilmaril@785
   324
			}		
insilmaril@169
   325
		}	
insilmaril@169
   326
		h+=topPad+botPad;
insilmaril@213
   327
		w+=leftPad+rightPad;
insilmaril@169
   328
	}
insilmaril@169
   329
insilmaril@169
   330
	// Frame thickness
insilmaril@462
   331
    w+=frame->getPadding();
insilmaril@462
   332
    h+=frame->getPadding();
insilmaril@169
   333
	
insilmaril@169
   334
	// Finally set size
insilmaril@408
   335
    bbox.setSize (QSizeF (w,h));
insilmaril@0
   336
}
insilmaril@0
   337
insilmaril@223
   338
void BranchObj::setDockPos()
insilmaril@223
   339
{
insilmaril@775
   340
	if (treeItem->getType()==TreeItem::MapCenter)
insilmaril@775
   341
	{
insilmaril@775
   342
		// set childPos to middle of MapCenterObj
insilmaril@775
   343
		childPos.setX( clickBox.topLeft().x() + clickBox.width()/2 );
insilmaril@775
   344
		childPos.setY( clickBox.topLeft().y() + clickBox.height()/2 );
insilmaril@775
   345
		parPos=childPos;		
insilmaril@775
   346
		for (int i=0; i<treeItem->branchCount(); ++i)
insilmaril@779
   347
			treeItem->getBranchObjNum(i)->updateLinkGeometry();
insilmaril@775
   348
insilmaril@775
   349
	} else
insilmaril@775
   350
	{
insilmaril@775
   351
		// Sets childpos and parpos depending on orientation
insilmaril@775
   352
		if (getOrientation()==LinkableMapObj::LeftOfCenter )
insilmaril@775
   353
		{
insilmaril@775
   354
			childPos=QPointF (
insilmaril@775
   355
				ornamentsBBox.bottomLeft().x(), 
insilmaril@775
   356
				bottomlineY);
insilmaril@775
   357
			parPos=QPointF (
insilmaril@775
   358
				ornamentsBBox.bottomRight().x(),
insilmaril@775
   359
				bottomlineY);
insilmaril@775
   360
		} else
insilmaril@775
   361
		{
insilmaril@775
   362
			childPos=QPointF (
insilmaril@775
   363
				ornamentsBBox.bottomRight().x(), 
insilmaril@775
   364
				bottomlineY);
insilmaril@775
   365
			parPos=QPointF (
insilmaril@775
   366
				ornamentsBBox.bottomLeft().x(),
insilmaril@775
   367
				bottomlineY);
insilmaril@775
   368
		}
insilmaril@775
   369
	}
insilmaril@223
   370
}
insilmaril@382
   371
insilmaril@767
   372
void BranchObj::updateData()
insilmaril@0
   373
{
insilmaril@767
   374
	bool changed=false;
insilmaril@753
   375
	if (!treeItem)
insilmaril@254
   376
	{
insilmaril@753
   377
		qWarning ("BranchObj::udpateHeading treeItem==NULL");
insilmaril@753
   378
		return;
insilmaril@753
   379
	}
insilmaril@767
   380
	QString s=treeItem->getHeading();
insilmaril@767
   381
	if (s!=heading->text())
insilmaril@767
   382
	{
insilmaril@767
   383
		heading->setText (s);
insilmaril@767
   384
		changed=true;
insilmaril@767
   385
	}
insilmaril@767
   386
	QStringList TIactiveFlags=treeItem->activeStandardFlagNames();
insilmaril@767
   387
insilmaril@767
   388
	// Add missing standard flags active in TreeItem
insilmaril@767
   389
	for (int i=0;i<=TIactiveFlags.size()-1;i++)
insilmaril@767
   390
	{	
insilmaril@767
   391
		if (!standardFlags->isActive (TIactiveFlags.at(i) ))
insilmaril@767
   392
		{
insilmaril@767
   393
			Flag *f=standardFlagsMaster->getFlag(TIactiveFlags.at(i));
insilmaril@767
   394
			if (f) standardFlags->activate (f);
insilmaril@767
   395
			changed=true;
insilmaril@767
   396
		}
insilmaril@767
   397
	}
insilmaril@767
   398
	// Remove standard flags no longer active in TreeItem
insilmaril@767
   399
	QStringList BOactiveFlags=standardFlags->activeFlagNames();
insilmaril@767
   400
	for (int i=0;i<BOactiveFlags.size();++i)
insilmaril@767
   401
		if (!TIactiveFlags.contains (BOactiveFlags.at(i)))
insilmaril@767
   402
		{
insilmaril@767
   403
			standardFlags->deactivate (BOactiveFlags.at(i));
insilmaril@767
   404
			changed=true;
insilmaril@767
   405
		}	
insilmaril@767
   406
insilmaril@767
   407
	// Add missing system flags active in TreeItem
insilmaril@767
   408
	TIactiveFlags=treeItem->activeSystemFlagNames();
insilmaril@767
   409
	for (int i=0;i<TIactiveFlags.size();++i)
insilmaril@767
   410
	{	
insilmaril@767
   411
		if (!systemFlags->isActive (TIactiveFlags.at(i) ))
insilmaril@767
   412
		{
insilmaril@767
   413
			Flag *f=systemFlagsMaster->getFlag(TIactiveFlags.at(i));
insilmaril@767
   414
			if (f) systemFlags->activate (f);
insilmaril@767
   415
			changed=true;
insilmaril@767
   416
		}
insilmaril@767
   417
	}
insilmaril@767
   418
	// Remove system flags no longer active in TreeItem
insilmaril@767
   419
	BOactiveFlags=systemFlags->activeFlagNames();
insilmaril@767
   420
	for (int i=0;i<BOactiveFlags.size();++i)
insilmaril@767
   421
	{
insilmaril@767
   422
		if (!TIactiveFlags.contains (BOactiveFlags.at(i)))
insilmaril@767
   423
		{
insilmaril@767
   424
			systemFlags->deactivate (BOactiveFlags.at(i));
insilmaril@767
   425
			changed=true;
insilmaril@767
   426
		}	
insilmaril@767
   427
	}
insilmaril@819
   428
	if (changed) updateContentSize(); 
insilmaril@254
   429
}
insilmaril@254
   430
insilmaril@822
   431
void BranchObj::setDefAttr (BranchModification mod, bool keepFrame)
insilmaril@146
   432
{
insilmaril@146
   433
	int fontsize;
insilmaril@755
   434
	switch (treeItem->depth())
insilmaril@146
   435
	{
insilmaril@798
   436
		case 0: 
insilmaril@798
   437
			fontsize=16; 
insilmaril@798
   438
			break;
insilmaril@798
   439
		case 1: 
insilmaril@804
   440
			fontsize=14; 
insilmaril@804
   441
			break;
insilmaril@804
   442
		case 2: 
insilmaril@798
   443
			fontsize=12; 
insilmaril@798
   444
			break;
insilmaril@798
   445
		default: 
insilmaril@798
   446
			fontsize=10; 
insilmaril@798
   447
			break;
insilmaril@146
   448
	}	
insilmaril@786
   449
	setLinkStyle(getDefLinkStyle(treeItem->parent() ));
insilmaril@146
   450
	setLinkColor ();
insilmaril@146
   451
	QFont font("Sans Serif,8,-1,5,50,0,0,0,0,0");
insilmaril@146
   452
	font.setPointSize(fontsize);
insilmaril@146
   453
	heading->setFont(font );
insilmaril@146
   454
insilmaril@822
   455
	if (mod==NewBranch && !keepFrame)
insilmaril@823
   456
	{
insilmaril@823
   457
		if (treeItem->depth()==0)
insilmaril@823
   458
			setFrameType (FrameObj::Rectangle);
insilmaril@823
   459
		else	
insilmaril@823
   460
			setFrameType (FrameObj::NoFrame);
insilmaril@823
   461
	}
insilmaril@146
   462
	if (mod==NewBranch)
insilmaril@760
   463
		setColor (treeItem->getHeadingColor() );
insilmaril@798
   464
	else
insilmaril@822
   465
	{
insilmaril@798
   466
		// Also set styles for children
insilmaril@798
   467
		for (int i=0; i<treeItem->branchCount(); ++i)
insilmaril@798
   468
			treeItem->getBranchObjNum(i)->setDefAttr(MovedBranch);
insilmaril@822
   469
	}		
insilmaril@146
   470
	calcBBoxSize();
insilmaril@146
   471
}
insilmaril@146
   472
insilmaril@721
   473
void BranchObj::alignRelativeTo (QPointF ref,bool alignSelf)
insilmaril@0
   474
{
insilmaril@408
   475
	qreal th = bboxTotal.height();	
insilmaril@817
   476
	int depth=0;
insilmaril@817
   477
	if (parObj)	depth=1 + parObj->getTreeItem()->depth();
insilmaril@223
   478
// TODO testing
insilmaril@223
   479
/*
insilmaril@817
   480
	QString h=QString (depth+1,' ');
insilmaril@753
   481
	h+=treeItem->getHeading();
insilmaril@753
   482
	h+=QString (15,' ');
insilmaril@753
   483
	h.truncate (15);
insilmaril@817
   484
	QPointF pp; 
insilmaril@817
   485
	if (parObj) pp=parObj->getChildPos();
insilmaril@753
   486
	cout << "BO::alignRelTo ";
insilmaril@753
   487
	cout<<h.toStdString();
insilmaril@817
   488
	cout << "    d="<<depth;
insilmaril@817
   489
	cout <<"  parO="<<parObj;
insilmaril@755
   490
//cout<<  "  ref="<<ref<<
insilmaril@817
   491
//cout <<	"  bbox.tL="<<bboxTotal.topLeft();
insilmaril@817
   492
cout<<	"  absPos="<<absPos<<
insilmaril@795
   493
		"  relPos="<<relPos<<
insilmaril@721
   494
//		"  parPos="<<pp<<
insilmaril@795
   495
//		"  w="<<bbox.width()<<
insilmaril@795
   496
//		"  h="<<bbox.height()<<
insilmaril@753
   497
//		"  orient="<<orientation<<
insilmaril@753
   498
//		"  alignSelf="<<alignSelf<<
insilmaril@753
   499
//		"  scrolled="<<((BranchItem*)treeItem)->isScrolled()<<
insilmaril@254
   500
//		"  pad="<<topPad<<","<<botPad<<","<<leftPad<<","<<rightPad<<
insilmaril@382
   501
//		"  hidden="<<hidden<<
insilmaril@817
   502
//		"  th="<<th<<
insilmaril@382
   503
		endl;
insilmaril@394
   504
*/
insilmaril@0
   505
insilmaril@382
   506
	setOrientation();
insilmaril@779
   507
	//updateLinkGeometry();
insilmaril@382
   508
insilmaril@796
   509
	if (depth==1)
insilmaril@796
   510
	{
insilmaril@796
   511
		move2RelPos (getRelPos() );
insilmaril@796
   512
	}
insilmaril@753
   513
	if (depth>1)
insilmaril@0
   514
    {
insilmaril@0
   515
		// Align myself depending on orientation and parent, but
insilmaril@382
   516
		// only if I am not a mainbranch or mapcenter itself
insilmaril@688
   517
insilmaril@688
   518
		if (anim.isAnimated())
insilmaril@0
   519
		{
insilmaril@688
   520
			move2RelPos(anim);
insilmaril@688
   521
		} else
insilmaril@688
   522
		{
insilmaril@688
   523
			LinkableMapObj::Orientation o;
insilmaril@688
   524
			o=parObj->getOrientation();
insilmaril@721
   525
			if (alignSelf)
insilmaril@721
   526
				switch (orientation) 
insilmaril@721
   527
				{
insilmaril@721
   528
					case LinkableMapObj::LeftOfCenter:
insilmaril@721
   529
						move (ref.x() - bbox.width(), ref.y() + (th-bbox.height())/2 );
insilmaril@721
   530
						//move (ref.x() , ref.y() + (th-bbox.height())/2 );
insilmaril@721
   531
					break;
insilmaril@721
   532
					case LinkableMapObj::RightOfCenter:	
insilmaril@721
   533
						move (ref.x() , ref.y() + (th-bbox.height())/2  );
insilmaril@721
   534
					break;
insilmaril@721
   535
					default:
insilmaril@721
   536
						qWarning ("LMO::alignRelativeTo: oops, no orientation given...");
insilmaril@721
   537
					break;
insilmaril@753
   538
			}
insilmaril@688
   539
		}
insilmaril@0
   540
    }		
insilmaril@0
   541
insilmaril@749
   542
	if ( ((BranchItem*)treeItem)->isScrolled() ) return;
insilmaril@0
   543
insilmaril@721
   544
    // Set reference point for alignment of children
insilmaril@408
   545
    QPointF ref2;
insilmaril@462
   546
    if (orientation==LinkableMapObj::LeftOfCenter)
insilmaril@223
   547
		ref2.setX(bbox.topLeft().x() - linkwidth);
insilmaril@0
   548
    else	
insilmaril@223
   549
		ref2.setX(bbox.topRight().x() + linkwidth);
insilmaril@0
   550
insilmaril@0
   551
	if (depth==1)
insilmaril@0
   552
		ref2.setY(absPos.y()-(bboxTotal.height()-bbox.height())/2);
insilmaril@0
   553
	else	
insilmaril@0
   554
		ref2.setY(ref.y() );	
insilmaril@0
   555
insilmaril@822
   556
    // Align the attribute children depending on reference point 
insilmaril@822
   557
	// on top like in TreeEditor
insilmaril@822
   558
	for (int i=0; i<treeItem->attributeCount(); ++i)
insilmaril@822
   559
    {	
insilmaril@822
   560
		if (!treeItem->getAttributeNum(i)->isHidden())
insilmaril@822
   561
		{
insilmaril@822
   562
			BranchObj *bo=(BranchObj*)(treeItem->getAttributeNum(i)->getBranchObj());
insilmaril@822
   563
			if (!bo) break;
insilmaril@822
   564
			bo->alignRelativeTo (ref2,true);
insilmaril@822
   565
insilmaril@822
   566
			// append next branch below current one
insilmaril@822
   567
			ref2.setY(ref2.y() + bo->getBBoxSizeWithChildren().height() );
insilmaril@822
   568
		}
insilmaril@822
   569
    }
insilmaril@822
   570
    // Align the branch children depending on reference point 
insilmaril@750
   571
	for (int i=0; i<treeItem->branchCount(); ++i)
insilmaril@0
   572
    {	
insilmaril@753
   573
		if (!treeItem->getBranchNum(i)->isHidden())
insilmaril@254
   574
		{
insilmaril@753
   575
			treeItem->getBranchObjNum(i)->alignRelativeTo (ref2,true);
insilmaril@721
   576
insilmaril@721
   577
			// append next branch below current one
insilmaril@753
   578
			ref2.setY(ref2.y() + treeItem->getBranchObjNum(i)->getBBoxSizeWithChildren().height() );
insilmaril@254
   579
		}
insilmaril@0
   580
    }
insilmaril@0
   581
}
insilmaril@0
   582
insilmaril@0
   583
insilmaril@0
   584
void BranchObj::reposition()
insilmaril@0
   585
{	
insilmaril@164
   586
/* TODO testing only
insilmaril@753
   587
	if (!treeItem->getHeading().isEmpty())
insilmaril@753
   588
		cout << "BO::reposition  "<<qPrintable(treeItem->getHeading())<<endl;
insilmaril@0
   589
	else	
insilmaril@0
   590
		cout << "BO::reposition  ???"<<endl;
insilmaril@753
   591
//	cout << "  orient="<<orientation<<endl;
insilmaril@0
   592
*/		
insilmaril@382
   593
insilmaril@755
   594
	if (treeItem->depth()==0)
insilmaril@0
   595
	{
insilmaril@0
   596
		// only calculate the sizes once. If the deepest LMO 
insilmaril@0
   597
		// changes its height,
insilmaril@0
   598
		// all upper LMOs have to change, too.
insilmaril@721
   599
		calcBBoxSizeWithChildren();
insilmaril@779
   600
		updateLinkGeometry();	// This update is needed if the scene is resized 
insilmaril@333
   601
						// due to excessive moving of a FIO
insilmaril@333
   602
insilmaril@408
   603
	    alignRelativeTo ( QPointF (absPos.x(),
insilmaril@0
   604
			absPos.y()-(bboxTotal.height()-bbox.height())/2) );
insilmaril@333
   605
		positionBBox();	// Reposition bbox and contents
insilmaril@0
   606
	} else
insilmaril@0
   607
	{
insilmaril@0
   608
		// This is only important for moving branches:
insilmaril@0
   609
		// For editing a branch it isn't called...
insilmaril@408
   610
	    alignRelativeTo ( QPointF (absPos.x(),
insilmaril@0
   611
							absPos.y()-(bboxTotal.height()-bbox.height())/2) );
insilmaril@0
   612
	}
insilmaril@0
   613
}
insilmaril@0
   614
insilmaril@382
   615
void BranchObj::unsetAllRepositionRequests()
insilmaril@382
   616
{
insilmaril@382
   617
	repositionRequest=false;
insilmaril@750
   618
	for (int i=0; i<treeItem->branchCount(); ++i)
insilmaril@753
   619
		treeItem->getBranchObjNum(i)->unsetAllRepositionRequests();
insilmaril@382
   620
}
insilmaril@382
   621
insilmaril@721
   622
QRectF BranchObj::getBBoxSizeWithChildren()
insilmaril@0
   623
{
insilmaril@0
   624
	return bboxTotal;
insilmaril@0
   625
}
insilmaril@0
   626
insilmaril@798
   627
ConvexPolygon BranchObj::getBoundingPolygon()	
insilmaril@793
   628
{
insilmaril@794
   629
/*
insilmaril@794
   630
	if (!pi)	//FIXME-3 Testing only
insilmaril@794
   631
	{
insilmaril@794
   632
		pi=scene->addPolygon(MapObj::getBoundingPolygon() );
insilmaril@794
   633
		pi->setPen(Qt::NoPen);
insilmaril@794
   634
		pi->setBrush( QColor(qrand()%32*8,qrand()%32*8,qrand()%32*8) );
insilmaril@794
   635
		pi->setZValue(Z_BBOX);
insilmaril@794
   636
	}
insilmaril@794
   637
	*/
insilmaril@798
   638
insilmaril@794
   639
	if (treeItem->branchCount()==0 || treeItem->depth()==0)
insilmaril@794
   640
	{
insilmaril@794
   641
		if (pi) pi->setPolygon (MapObj::getBoundingPolygon() );
insilmaril@793
   642
		return MapObj::getBoundingPolygon();
insilmaril@794
   643
	}
insilmaril@793
   644
insilmaril@823
   645
	calcBBoxSizeWithChildren();	//FIXME-3 really needed?
insilmaril@793
   646
	QPolygonF p;
insilmaril@793
   647
	p<<bboxTotal.topLeft();
insilmaril@793
   648
	p<<bboxTotal.topRight();
insilmaril@793
   649
	p<<bboxTotal.bottomRight();
insilmaril@793
   650
	p<<bboxTotal.bottomLeft();
insilmaril@798
   651
	//cout << "BO::getBP (total)  "<<treeItem->getHeadingStd()<<"  tL="<<bboxTotal.topLeft()<<"  bR="<<bboxTotal.bottomRight()<<endl;
insilmaril@798
   652
	//cout << "                   "<<"  tL="<<bbox.topLeft()<<"  bR="<<bbox.bottomRight()<<endl;
insilmaril@798
   653
	if (pi) pi->setPolygon (p );
insilmaril@793
   654
	return p;
insilmaril@793
   655
}
insilmaril@793
   656
insilmaril@721
   657
void BranchObj::calcBBoxSizeWithChildren()
insilmaril@169
   658
{	
insilmaril@169
   659
	// This is initially called only from reposition and
insilmaril@0
   660
	// and only for mapcenter. So it won't be
insilmaril@0
   661
	// called more than once for a single user 
insilmaril@0
   662
	// action
insilmaril@0
   663
	
insilmaril@169
   664
insilmaril@721
   665
	// Calculate size of LMO including all children (to align them later)
insilmaril@0
   666
	bboxTotal.setX(bbox.x() );
insilmaril@0
   667
	bboxTotal.setY(bbox.y() );
insilmaril@0
   668
insilmaril@721
   669
	// if branch is scrolled, ignore children, but still consider floatimages
insilmaril@753
   670
	BranchItem *bi=(BranchItem*)treeItem;
insilmaril@753
   671
	if ( bi->isScrolled() ) 
insilmaril@0
   672
	{
insilmaril@0
   673
		bboxTotal.setWidth (bbox.width());
insilmaril@0
   674
		bboxTotal.setHeight(bbox.height());
insilmaril@0
   675
		return;
insilmaril@0
   676
	}
insilmaril@0
   677
	
insilmaril@753
   678
	if (bi->isHidden())
insilmaril@254
   679
	{
insilmaril@254
   680
		bboxTotal.setWidth (0);
insilmaril@254
   681
		bboxTotal.setHeight(0);
insilmaril@254
   682
		if (parObj)
insilmaril@254
   683
		{
insilmaril@254
   684
			bboxTotal.setX (parObj->x());
insilmaril@254
   685
			bboxTotal.setY (parObj->y());
insilmaril@254
   686
		} else
insilmaril@254
   687
		{
insilmaril@254
   688
			bboxTotal.setX (bbox.x());
insilmaril@254
   689
			bboxTotal.setY (bbox.y());
insilmaril@254
   690
		}
insilmaril@254
   691
		return;
insilmaril@254
   692
	}
insilmaril@254
   693
	
insilmaril@408
   694
	QRectF r(0,0,0,0);
insilmaril@408
   695
	QRectF br;
insilmaril@0
   696
	// Now calculate recursivly
insilmaril@0
   697
	// sum of heights 
insilmaril@0
   698
	// maximum of widths 
insilmaril@0
   699
	// minimum of y
insilmaril@755
   700
	for (int i=0; i<treeItem->branchCount(); i++)
insilmaril@0
   701
	{
insilmaril@753
   702
		if (!bi->getBranchNum(i)->isHidden())
insilmaril@254
   703
		{
insilmaril@822
   704
			BranchObj *bo=bi->getBranchObjNum(i);
insilmaril@822
   705
			bo->calcBBoxSizeWithChildren();
insilmaril@822
   706
			br=bo->getBBoxSizeWithChildren();
insilmaril@822
   707
			r.setWidth( max (br.width(), r.width() ));
insilmaril@822
   708
			r.setHeight(br.height() + r.height() );
insilmaril@822
   709
			if (br.y()<bboxTotal.y()) bboxTotal.setY(br.y());
insilmaril@822
   710
			if (br.x()<bboxTotal.x()) bboxTotal.setX(br.x());
insilmaril@822
   711
		}
insilmaril@822
   712
	}
insilmaril@822
   713
	for (int i=0; i<treeItem->attributeCount(); i++)
insilmaril@822
   714
	{
insilmaril@822
   715
		if (!bi->getAttributeNum(i)->isHidden())
insilmaril@822
   716
		{
insilmaril@822
   717
			BranchObj *bo=bi->getAttributeNum(i)->getBranchObj();
insilmaril@822
   718
			bo->calcBBoxSizeWithChildren();
insilmaril@822
   719
			br=bo->getBBoxSizeWithChildren();
insilmaril@254
   720
			r.setWidth( max (br.width(), r.width() ));
insilmaril@254
   721
			r.setHeight(br.height() + r.height() );
insilmaril@254
   722
			if (br.y()<bboxTotal.y()) bboxTotal.setY(br.y());
insilmaril@798
   723
			if (br.x()<bboxTotal.x()) bboxTotal.setX(br.x());
insilmaril@254
   724
		}
insilmaril@0
   725
	}
insilmaril@0
   726
	// Add myself and also
insilmaril@0
   727
	// add width of link to sum if necessary
insilmaril@753
   728
	if (bi->branchCount()<1)
insilmaril@0
   729
		bboxTotal.setWidth (bbox.width() + r.width() );
insilmaril@0
   730
	else	
insilmaril@0
   731
		bboxTotal.setWidth (bbox.width() + r.width() + linkwidth);
insilmaril@169
   732
	
insilmaril@169
   733
	bboxTotal.setHeight(max (r.height(),  bbox.height()));
insilmaril@0
   734
}
insilmaril@0
   735
insilmaril@688
   736
void BranchObj::setAnimation(const AnimPoint &ap)
insilmaril@462
   737
{
insilmaril@688
   738
	anim=ap;
insilmaril@462
   739
}
insilmaril@462
   740
insilmaril@688
   741
bool BranchObj::animate()
insilmaril@688
   742
{
insilmaril@688
   743
	anim.animate ();
insilmaril@721
   744
	if ( anim.isAnimated() )
insilmaril@721
   745
	{
insilmaril@835
   746
		//if (((MapItem*)treeItem)->getPositionMode()==MapItem::Relative) 
insilmaril@835
   747
		if (useRelPos)
insilmaril@835
   748
		{
insilmaril@835
   749
			setRelPos (anim);
insilmaril@835
   750
			//cout<<"Animating rel "<<treeItem->getHeading().toStdString()<<" "<<anim<<" "<<anim.getStart()<<"->"<<anim.getDest()<<"  "<<relPos<<endl;
insilmaril@835
   751
		}
insilmaril@835
   752
		else
insilmaril@835
   753
		{
insilmaril@835
   754
			move (anim);
insilmaril@835
   755
			//cout<<"Animating abs "<<treeItem->getHeading().toStdString()<<" "<<anim<<" "<<anim.getStart()<<"->"<<anim.getDest()<<"  "<<absPos<<endl;
insilmaril@835
   756
		}
insilmaril@721
   757
		return true;
insilmaril@721
   758
	}
insilmaril@835
   759
	/*FIXME-3 reposition in BO:animate nearly never reached? needed?	
insilmaril@835
   760
	if (((MapItem*)treeItem)->getPositionMode()==MapItem::Relative)
insilmaril@835
   761
		parObj->reposition();	// we might have been relinked meanwhile
insilmaril@835
   762
	*/	
insilmaril@721
   763
	return false;
insilmaril@688
   764
}
insilmaril@688
   765