branchobj.cpp
author insilmaril
Fri Aug 19 07:42:31 2005 +0000 (2005-08-19)
changeset 154 fa05daf043a8
parent 146 1b52ff19aad5
child 158 a33a2bdd8299
permissions -rw-r--r--
added license in new aboutwindow and debian subdir by Steffen Joeris
insilmaril@0
     1
#include "branchobj.h"
insilmaril@0
     2
#include "texteditor.h"
insilmaril@0
     3
#include "mapeditor.h"
insilmaril@0
     4
#include "mainwindow.h"
insilmaril@0
     5
insilmaril@0
     6
extern TextEditor *textEditor;
insilmaril@0
     7
extern Main *mainWindow;
insilmaril@0
     8
extern FlagRowObj *standardFlagsDefault;
insilmaril@0
     9
extern QAction *actionEditOpenURL;
insilmaril@0
    10
insilmaril@0
    11
insilmaril@0
    12
/////////////////////////////////////////////////////////////////
insilmaril@0
    13
// BranchObj
insilmaril@0
    14
/////////////////////////////////////////////////////////////////
insilmaril@0
    15
insilmaril@0
    16
BranchObj* BranchObj::itLast=NULL;
insilmaril@0
    17
insilmaril@0
    18
insilmaril@0
    19
BranchObj::BranchObj () :OrnamentedObj()
insilmaril@0
    20
{
insilmaril@0
    21
//    cout << "Const BranchObj ()\n";
insilmaril@0
    22
    setParObj (this);	
insilmaril@0
    23
    init();
insilmaril@0
    24
    depth=-1;
insilmaril@0
    25
}
insilmaril@0
    26
insilmaril@0
    27
BranchObj::BranchObj (QCanvas* c):OrnamentedObj (c)
insilmaril@0
    28
{
insilmaril@0
    29
//    cout << "Const BranchObj (c)  called from MapCenterObj (c)\n";
insilmaril@83
    30
	parObj=NULL;
insilmaril@0
    31
    canvas=c;
insilmaril@0
    32
}
insilmaril@0
    33
insilmaril@0
    34
BranchObj::BranchObj (QCanvas* c, LinkableMapObj* p):OrnamentedObj (c)
insilmaril@0
    35
{
insilmaril@0
    36
//    cout << "Const BranchObj (c,p)\n";
insilmaril@0
    37
    canvas=c;
insilmaril@0
    38
    setParObj (p);	
insilmaril@0
    39
    depth=p->getDepth()+1;
insilmaril@0
    40
	if (depth==1)
insilmaril@0
    41
		// Calc angle to mapCenter if I am a mainbranch
insilmaril@0
    42
		// needed for reordering the mainbranches clockwise 
insilmaril@0
    43
		// around mapcenter 
insilmaril@0
    44
		angle=getAngle (QPoint ((int)(x() - parObj->getChildPos().x() ), 
insilmaril@0
    45
								(int)(y() - parObj->getChildPos().y() ) ) );
insilmaril@0
    46
    init();
insilmaril@0
    47
}
insilmaril@0
    48
insilmaril@0
    49
BranchObj::~BranchObj ()
insilmaril@0
    50
{
insilmaril@83
    51
//	cout << "Destr BranchObj of "<<this<<endl;
insilmaril@0
    52
	// Check, if this branch was the last child to be deleted
insilmaril@0
    53
	// If so, unset the scrolled flags
insilmaril@0
    54
insilmaril@0
    55
	BranchObj *po=(BranchObj*)(parObj);
insilmaril@83
    56
	BranchObj *bo;
insilmaril@0
    57
	if (po)
insilmaril@0
    58
	{
insilmaril@83
    59
		bo=((BranchObj*)(parObj))->getLastBranch();
insilmaril@0
    60
		if (!bo) po->unScroll();
insilmaril@0
    61
	}
insilmaril@83
    62
	clear();
insilmaril@0
    63
}
insilmaril@0
    64
insilmaril@0
    65
bool BranchObj::operator< ( const BranchObj & other )
insilmaril@0
    66
{
insilmaril@0
    67
    return  angle < other.angle;
insilmaril@0
    68
}
insilmaril@0
    69
insilmaril@0
    70
bool BranchObj::operator== ( const BranchObj & other )
insilmaril@0
    71
{
insilmaril@0
    72
    return angle == other.angle;
insilmaril@0
    73
}
insilmaril@0
    74
insilmaril@0
    75
int BranchObjPtrList::compareItems ( QPtrCollection::Item i, QPtrCollection::Item j)
insilmaril@0
    76
{
insilmaril@0
    77
	// Make sure PtrList::find works
insilmaril@0
    78
	if (i==j) return 0;
insilmaril@0
    79
insilmaril@0
    80
	if ( ((BranchObj*)(i))->angle > ((BranchObj*)(j))->angle )
insilmaril@0
    81
		return 1;
insilmaril@0
    82
	else
insilmaril@0
    83
		return -1;
insilmaril@0
    84
}
insilmaril@0
    85
insilmaril@0
    86
void BranchObj::init () 
insilmaril@0
    87
{
insilmaril@83
    88
    branch.setAutoDelete (false);
insilmaril@0
    89
    floatimage.setAutoDelete (true);
insilmaril@95
    90
    xlink.setAutoDelete (false);
insilmaril@0
    91
insilmaril@83
    92
	if (parObj)
insilmaril@83
    93
	{
insilmaril@83
    94
		absPos=getRandPos();
insilmaril@83
    95
		absPos+=parObj->getChildPos();
insilmaril@83
    96
	}
insilmaril@0
    97
insilmaril@0
    98
    lastSelectedBranch=-1;
insilmaril@0
    99
insilmaril@0
   100
    setChildObj(this);
insilmaril@0
   101
insilmaril@0
   102
	scrolled=false;
insilmaril@0
   103
	tmpUnscrolled=false;
insilmaril@0
   104
insilmaril@0
   105
	url="";
insilmaril@0
   106
	vymLink="";
insilmaril@0
   107
}
insilmaril@0
   108
insilmaril@0
   109
void BranchObj::copy (BranchObj* other)
insilmaril@0
   110
{
insilmaril@0
   111
    OrnamentedObj::copy(other);
insilmaril@0
   112
insilmaril@0
   113
	branch.clear();
insilmaril@0
   114
    BranchObj* b;
insilmaril@0
   115
    for (b=other->branch.first(); b;b=other->branch.next() ) 
insilmaril@0
   116
		// Make deep copy of b
insilmaril@0
   117
		// Because addBranch again calls copy for the childs,
insilmaril@0
   118
		// Those will get a deep copy, too
insilmaril@0
   119
		addBranch(b);	
insilmaril@0
   120
insilmaril@0
   121
	FloatImageObj *fi;
insilmaril@0
   122
	for (fi=other->floatimage.first(); fi;fi=other->floatimage.next() )
insilmaril@0
   123
		addFloatImage (fi);
insilmaril@0
   124
insilmaril@0
   125
	scrolled=other->scrolled;
insilmaril@0
   126
	tmpUnscrolled=other->tmpUnscrolled;
insilmaril@0
   127
	setVisibility (other->visible);
insilmaril@0
   128
insilmaril@0
   129
	url=other->url;
insilmaril@0
   130
	vymLink=other->vymLink;
insilmaril@0
   131
insilmaril@0
   132
	angle=other->angle;
insilmaril@0
   133
insilmaril@0
   134
    positionBBox();
insilmaril@0
   135
}
insilmaril@0
   136
insilmaril@0
   137
void BranchObj::clear() 
insilmaril@0
   138
{
insilmaril@0
   139
	floatimage.clear();
insilmaril@95
   140
	while (!xlink.isEmpty())
insilmaril@95
   141
		deleteXLink (xlink.first() );
insilmaril@83
   142
insilmaril@83
   143
	BranchObj *bo;
insilmaril@83
   144
	while (!branch.isEmpty())
insilmaril@83
   145
	{
insilmaril@83
   146
		bo=branch.first();
insilmaril@83
   147
		branch.removeFirst();
insilmaril@83
   148
		delete (bo);
insilmaril@83
   149
	}
insilmaril@0
   150
}
insilmaril@0
   151
insilmaril@0
   152
int BranchObj::getNum()
insilmaril@0
   153
{
insilmaril@0
   154
	if (parObj)
insilmaril@0
   155
		return ((BranchObj*)(parObj))->getNum ((BranchObj*)(this));
insilmaril@0
   156
	else
insilmaril@0
   157
		return 0;
insilmaril@0
   158
}
insilmaril@0
   159
insilmaril@0
   160
int BranchObj::getNum(BranchObj *bo)
insilmaril@0
   161
{
insilmaril@95
   162
	// keep current pointer in branch, 
insilmaril@95
   163
	// otherwise save might fail
insilmaril@95
   164
	int cur=branch.at();
insilmaril@95
   165
	int ind=branch.findRef (bo);
insilmaril@95
   166
	branch.at(cur);
insilmaril@95
   167
	return ind;
insilmaril@0
   168
}
insilmaril@0
   169
insilmaril@0
   170
int BranchObj::getFloatImageNum(FloatImageObj *fio)
insilmaril@0
   171
{
insilmaril@0
   172
	return floatimage.findRef (fio);
insilmaril@0
   173
}
insilmaril@0
   174
insilmaril@0
   175
int BranchObj::countBranches()
insilmaril@0
   176
{
insilmaril@0
   177
	return branch.count();
insilmaril@0
   178
}
insilmaril@0
   179
insilmaril@0
   180
int BranchObj::countFloatImages()
insilmaril@0
   181
{
insilmaril@0
   182
	return floatimage.count();
insilmaril@0
   183
}
insilmaril@0
   184
insilmaril@95
   185
int BranchObj::countXLinks()
insilmaril@89
   186
{
insilmaril@95
   187
	return xlink.count();
insilmaril@89
   188
}
insilmaril@89
   189
insilmaril@0
   190
void BranchObj::setParObjTmp(LinkableMapObj* lmo, QPoint m, int off)
insilmaril@0
   191
{
insilmaril@0
   192
	// Temporary link to lmo
insilmaril@0
   193
	// m is position of mouse pointer 
insilmaril@0
   194
	// offset 0: default 1: below lmo   -1 above lmo  (if possible)
insilmaril@0
   195
insilmaril@0
   196
insilmaril@0
   197
	BranchObj* o=(BranchObj*)(lmo);
insilmaril@0
   198
	if (!parObjTmpBuf) 
insilmaril@0
   199
		parObjTmpBuf=parObj;
insilmaril@0
   200
insilmaril@0
   201
	// ignore mapcenter and mainbranch
insilmaril@0
   202
	if (lmo->getDepth()<2) off=0;
insilmaril@0
   203
	if (off==0)
insilmaril@0
   204
	{
insilmaril@0
   205
		link2ParPos=false;
insilmaril@93
   206
	//	parObj=o;
insilmaril@0
   207
	}	
insilmaril@0
   208
	else
insilmaril@0
   209
	{	
insilmaril@0
   210
		link2ParPos=true;
insilmaril@93
   211
	//	if (off>0)
insilmaril@93
   212
	//		parObj=o->getParObj();
insilmaril@93
   213
	//	else	
insilmaril@93
   214
	//		parObj=o->getParObj();
insilmaril@93
   215
	//	parObj=o;
insilmaril@0
   216
	}		
insilmaril@93
   217
	parObj=o;
insilmaril@0
   218
insilmaril@0
   219
	depth=parObj->getDepth()+1;
insilmaril@0
   220
insilmaril@0
   221
	// setLinkStyle calls updateLink, only set it once
insilmaril@0
   222
	if (style!=getDefLinkStyle() ) setLinkStyle (getDefLinkStyle());
insilmaril@0
   223
insilmaril@0
   224
	// Move temporary to new position at destination
insilmaril@0
   225
	// Usually the positioning would be done by reposition(),
insilmaril@0
   226
	// but then also the destination branch would "Jump" around...
insilmaril@0
   227
	// Better just do it approximately
insilmaril@0
   228
	if (depth==1)
insilmaril@0
   229
	{	// new parent is the mapcenter itself
insilmaril@0
   230
insilmaril@0
   231
		QPoint p= normalise ( QPoint (m.x() - o->getChildPos().x(),
insilmaril@0
   232
									  m.y() - o->getChildPos().y() ));
insilmaril@0
   233
		if (p.x()<0) p.setX( p.x()-bbox.width() );
insilmaril@0
   234
		move2RelPos (p);
insilmaril@0
   235
	} else
insilmaril@0
   236
	{	
insilmaril@0
   237
		int y;
insilmaril@0
   238
		if (off==0)
insilmaril@0
   239
		{
insilmaril@0
   240
			// new parent is just a branch, link to it
insilmaril@0
   241
			QRect t=o->getBBoxSizeWithChilds();
insilmaril@0
   242
			if (o->getLastBranch())
insilmaril@0
   243
				y=t.y() + t.height() ;
insilmaril@0
   244
			else
insilmaril@0
   245
				y=t.y();
insilmaril@0
   246
insilmaril@0
   247
		} else
insilmaril@0
   248
		{
insilmaril@0
   249
			if (off<0)
insilmaril@0
   250
				// we want to link above lmo
insilmaril@0
   251
				y=o->y() - height() + 5;
insilmaril@0
   252
			else	
insilmaril@0
   253
				// we want to link below lmo
insilmaril@0
   254
				// Bottom of sel should be 5 pixels above
insilmaril@0
   255
				// the bottom of the branch _below_ the target:
insilmaril@0
   256
				// Don't try to find that branch, guess 12 pixels
insilmaril@0
   257
				y=o->getChildPos().y()  -height() + 12; 
insilmaril@0
   258
		}	
insilmaril@0
   259
		if (o->getOrientation()==OrientLeftOfCenter)
insilmaril@0
   260
			move ( o->getChildPos().x() - linkwidth, y );
insilmaril@0
   261
		else	
insilmaril@0
   262
			move (o->getChildPos().x() + linkwidth, y );
insilmaril@0
   263
	}	
insilmaril@0
   264
insilmaril@0
   265
	// updateLink is called implicitly in move
insilmaril@0
   266
	reposition();	// FIXME shouldn't be this a request?
insilmaril@0
   267
}
insilmaril@0
   268
insilmaril@0
   269
void BranchObj::unsetParObjTmp()
insilmaril@0
   270
{
insilmaril@0
   271
	if (parObjTmpBuf) 
insilmaril@0
   272
	{
insilmaril@0
   273
		link2ParPos=false;
insilmaril@0
   274
		parObj=parObjTmpBuf;
insilmaril@0
   275
		parObjTmpBuf=NULL;
insilmaril@0
   276
		depth=parObj->getDepth()+1;
insilmaril@0
   277
		setLinkStyle (getDefLinkStyle() );
insilmaril@93
   278
		updateLink();
insilmaril@0
   279
	}		
insilmaril@0
   280
}
insilmaril@0
   281
insilmaril@0
   282
void BranchObj::unScroll()
insilmaril@0
   283
{
insilmaril@0
   284
	if (tmpUnscrolled) resetTmpUnscroll();
insilmaril@0
   285
	if (scrolled) toggleScroll();
insilmaril@0
   286
}
insilmaril@0
   287
insilmaril@0
   288
void BranchObj::toggleScroll()
insilmaril@0
   289
{
insilmaril@0
   290
	BranchObj *bo;
insilmaril@0
   291
	if (scrolled)
insilmaril@0
   292
	{
insilmaril@0
   293
		scrolled=false;
insilmaril@0
   294
		systemFlags->deactivate("scrolledright");
insilmaril@0
   295
		for (bo=branch.first(); bo; bo=branch.next() )
insilmaril@0
   296
		{
insilmaril@0
   297
			bo->setVisibility(true);
insilmaril@0
   298
		}
insilmaril@0
   299
	} else
insilmaril@0
   300
	{
insilmaril@0
   301
		scrolled=true;
insilmaril@0
   302
		systemFlags->activate("scrolledright");
insilmaril@0
   303
		for (bo=branch.first(); bo; bo=branch.next() )
insilmaril@0
   304
		{
insilmaril@0
   305
			bo->setVisibility(false);
insilmaril@0
   306
		}
insilmaril@0
   307
	}
insilmaril@0
   308
	calcBBoxSize();
insilmaril@0
   309
	positionBBox();	
insilmaril@0
   310
	move (absPos.x(), absPos.y() );
insilmaril@0
   311
	forceReposition();
insilmaril@0
   312
}
insilmaril@0
   313
insilmaril@0
   314
bool BranchObj::isScrolled()
insilmaril@0
   315
{
insilmaril@0
   316
	return scrolled;
insilmaril@0
   317
}
insilmaril@0
   318
insilmaril@0
   319
bool BranchObj::hasScrolledParent(BranchObj *start)
insilmaril@0
   320
{
insilmaril@0
   321
	// Calls parents recursivly to
insilmaril@0
   322
	// find out, if we are scrolled at all.
insilmaril@0
   323
	// But ignore myself, just look at parents.
insilmaril@0
   324
insilmaril@0
   325
	if (this !=start && scrolled) return true;
insilmaril@0
   326
insilmaril@0
   327
	BranchObj* bo=(BranchObj*)(parObj);
insilmaril@0
   328
	if (bo) 
insilmaril@0
   329
		return bo->hasScrolledParent(start);
insilmaril@0
   330
	else
insilmaril@0
   331
		return false;
insilmaril@0
   332
}
insilmaril@0
   333
insilmaril@0
   334
void BranchObj::tmpUnscroll()
insilmaril@0
   335
{
insilmaril@0
   336
	// Unscroll parent (recursivly)
insilmaril@0
   337
	BranchObj* bo=(BranchObj*)(parObj);
insilmaril@0
   338
	if (bo) bo->tmpUnscroll();
insilmaril@0
   339
		
insilmaril@0
   340
	// Unscroll myself
insilmaril@0
   341
	if (scrolled)
insilmaril@0
   342
	{
insilmaril@0
   343
		tmpUnscrolled=true;
insilmaril@0
   344
		systemFlags->activate("tmpUnscrolledright");
insilmaril@0
   345
		toggleScroll();
insilmaril@0
   346
	}	
insilmaril@0
   347
}
insilmaril@0
   348
insilmaril@0
   349
void BranchObj::resetTmpUnscroll()
insilmaril@0
   350
{
insilmaril@0
   351
	// Unscroll parent (recursivly)
insilmaril@0
   352
	BranchObj* bo=(BranchObj*)(parObj);
insilmaril@0
   353
	if (bo)
insilmaril@0
   354
		bo->resetTmpUnscroll();
insilmaril@0
   355
		
insilmaril@0
   356
	// Unscroll myself
insilmaril@0
   357
	if (tmpUnscrolled)
insilmaril@0
   358
	{
insilmaril@0
   359
		tmpUnscrolled=false;
insilmaril@0
   360
		systemFlags->deactivate("tmpUnscrolledright");
insilmaril@0
   361
		toggleScroll();
insilmaril@0
   362
	}	
insilmaril@0
   363
}
insilmaril@0
   364
insilmaril@0
   365
void BranchObj::setVisibility(bool v, int toDepth)
insilmaril@0
   366
{
insilmaril@0
   367
    if (depth <= toDepth)
insilmaril@0
   368
    {
insilmaril@0
   369
		frame->setVisibility(v);
insilmaril@0
   370
		heading->setVisibility(v);
insilmaril@0
   371
		systemFlags->setVisibility(v);
insilmaril@0
   372
		standardFlags->setVisibility(v);
insilmaril@0
   373
		LinkableMapObj::setVisibility (v);
insilmaril@0
   374
		
insilmaril@0
   375
		if (!scrolled && (depth < toDepth))
insilmaril@0
   376
		{
insilmaril@0
   377
			// Now go recursivly through all childs
insilmaril@0
   378
			BranchObj* b;
insilmaril@0
   379
			for (b=branch.first(); b;b=branch.next() ) 
insilmaril@0
   380
				b->setVisibility (v,toDepth);	
insilmaril@0
   381
			FloatImageObj *fio;
insilmaril@0
   382
			for (fio=floatimage.first(); fio; fio=floatimage.next())
insilmaril@0
   383
				fio->setVisibility (v);
insilmaril@95
   384
			XLinkObj* xlo;
insilmaril@95
   385
			for (xlo=xlink.first(); xlo;xlo=xlink.next() ) 
insilmaril@95
   386
				xlo->setVisibility ();	
insilmaril@0
   387
		}
insilmaril@0
   388
    } // depth <= toDepth	
insilmaril@0
   389
	requestReposition();
insilmaril@0
   390
}	
insilmaril@0
   391
insilmaril@0
   392
void BranchObj::setVisibility(bool v)
insilmaril@0
   393
{
insilmaril@0
   394
    setVisibility (v,MAX_DEPTH);
insilmaril@0
   395
}
insilmaril@0
   396
insilmaril@0
   397
insilmaril@0
   398
void BranchObj::setLinkColor ()
insilmaril@0
   399
{
insilmaril@0
   400
	// Overloaded from LinkableMapObj
insilmaril@0
   401
	// BranchObj can use color of heading
insilmaril@0
   402
insilmaril@0
   403
	if (mapEditor->getLinkColorHint()==HeadingColor)
insilmaril@0
   404
		LinkableMapObj::setLinkColor (heading->getColor() );
insilmaril@0
   405
	else	
insilmaril@0
   406
		LinkableMapObj::setLinkColor ();
insilmaril@0
   407
}
insilmaril@0
   408
insilmaril@0
   409
void BranchObj::setColor (QColor col, bool colorChilds)
insilmaril@0
   410
{
insilmaril@0
   411
    heading->setColor(col);
insilmaril@0
   412
	setLinkColor();
insilmaril@0
   413
    if (colorChilds) 
insilmaril@0
   414
    {
insilmaril@0
   415
		BranchObj *bo;
insilmaril@0
   416
		for (bo=branch.first(); bo; bo=branch.next() )
insilmaril@0
   417
			bo->setColor(col,colorChilds);
insilmaril@0
   418
    }	
insilmaril@0
   419
}
insilmaril@0
   420
insilmaril@100
   421
QColor BranchObj::getColor()
insilmaril@100
   422
{
insilmaril@100
   423
	return heading->getColor();
insilmaril@100
   424
}
insilmaril@0
   425
insilmaril@0
   426
BranchObj* BranchObj::first()
insilmaril@0
   427
{
insilmaril@0
   428
	itLast=NULL;	
insilmaril@0
   429
	return this; 
insilmaril@0
   430
}
insilmaril@0
   431
	
insilmaril@0
   432
BranchObj* BranchObj::next()
insilmaril@0
   433
{
insilmaril@0
   434
	BranchObj *lmo;
insilmaril@0
   435
	BranchObj *bo=branch.first();
insilmaril@0
   436
	BranchObj *po=(BranchObj*)(parObj);
insilmaril@0
   437
insilmaril@0
   438
	if (!itLast)
insilmaril@0
   439
	{	// We are just beginning at the mapCenter
insilmaril@0
   440
		if (bo) 
insilmaril@0
   441
		{
insilmaril@0
   442
			itLast=this;
insilmaril@0
   443
			return bo;
insilmaril@0
   444
		}	
insilmaril@0
   445
		else
insilmaril@0
   446
		{
insilmaril@0
   447
			itLast=NULL;
insilmaril@0
   448
			return NULL;
insilmaril@0
   449
		}	
insilmaril@0
   450
	}
insilmaril@0
   451
insilmaril@0
   452
	if (itLast==parObj)
insilmaril@0
   453
	{	// We come from above
insilmaril@0
   454
		if (bo)
insilmaril@0
   455
		{
insilmaril@0
   456
			// there are childs, go there
insilmaril@0
   457
			itLast=this;
insilmaril@0
   458
			return bo;
insilmaril@0
   459
		}	
insilmaril@0
   460
		else
insilmaril@0
   461
		{	// no childs, try to go up again
insilmaril@0
   462
			if (po)
insilmaril@0
   463
			{
insilmaril@0
   464
				// go up
insilmaril@0
   465
				itLast=this;
insilmaril@0
   466
				lmo=po->next();
insilmaril@0
   467
				itLast=this;
insilmaril@0
   468
				return lmo;
insilmaril@0
   469
insilmaril@0
   470
			}	
insilmaril@0
   471
			else
insilmaril@0
   472
			{
insilmaril@0
   473
				// can't go up, I am mapCenter
insilmaril@0
   474
				itLast=NULL;
insilmaril@0
   475
				return NULL;
insilmaril@0
   476
			}	
insilmaril@0
   477
		}
insilmaril@0
   478
	}
insilmaril@0
   479
insilmaril@0
   480
	// Try to find last child, we came from, in my own childs
insilmaril@0
   481
	bool searching=true;
insilmaril@0
   482
	while (bo && searching)
insilmaril@0
   483
	{
insilmaril@0
   484
		if (itLast==bo) searching=false;
insilmaril@0
   485
		bo=branch.next();
insilmaril@0
   486
	}
insilmaril@0
   487
	if (!searching)
insilmaril@0
   488
	{	// found lastLMO in my childs
insilmaril@0
   489
		if (bo)
insilmaril@0
   490
		{
insilmaril@0
   491
			// found a brother of lastLMO 
insilmaril@0
   492
			itLast=this;
insilmaril@0
   493
			return bo;
insilmaril@0
   494
		}	
insilmaril@0
   495
		else
insilmaril@0
   496
		{
insilmaril@0
   497
			if (po)
insilmaril@0
   498
			{
insilmaril@0
   499
				// go up
insilmaril@0
   500
				itLast=this;
insilmaril@0
   501
				lmo=po->next();
insilmaril@0
   502
				itLast=this;
insilmaril@0
   503
				return lmo;
insilmaril@0
   504
			}
insilmaril@0
   505
			else
insilmaril@0
   506
			{
insilmaril@0
   507
				// can't go up, I am mapCenter
insilmaril@0
   508
				itLast=NULL;
insilmaril@0
   509
				return NULL;
insilmaril@0
   510
			}	
insilmaril@0
   511
		}
insilmaril@0
   512
	}
insilmaril@0
   513
insilmaril@0
   514
	// couldn't find last child, it must be a nephew of mine
insilmaril@0
   515
	bo=branch.first();
insilmaril@0
   516
	if (bo)
insilmaril@0
   517
	{
insilmaril@0
   518
		// proceed with my first child
insilmaril@0
   519
		itLast=this;	
insilmaril@0
   520
		return bo;
insilmaril@0
   521
	}	
insilmaril@0
   522
	else
insilmaril@0
   523
	{
insilmaril@0
   524
		// or go back to my parents
insilmaril@0
   525
		if (po)
insilmaril@0
   526
		{
insilmaril@0
   527
			// go up
insilmaril@0
   528
			itLast=this;
insilmaril@0
   529
			lmo=po->next();
insilmaril@0
   530
			itLast=this;
insilmaril@0
   531
			return lmo;
insilmaril@0
   532
		}	
insilmaril@0
   533
		else
insilmaril@0
   534
		{
insilmaril@0
   535
			// can't go up, I am mapCenter
insilmaril@0
   536
			itLast=NULL;
insilmaril@0
   537
			return NULL;
insilmaril@0
   538
		}	
insilmaril@0
   539
	}	
insilmaril@0
   540
}
insilmaril@0
   541
insilmaril@0
   542
BranchObj* BranchObj::getLastIterator()
insilmaril@0
   543
{
insilmaril@0
   544
	return itLast;
insilmaril@0
   545
}
insilmaril@0
   546
insilmaril@0
   547
void BranchObj::setLastIterator(BranchObj* it)
insilmaril@0
   548
{
insilmaril@0
   549
	itLast=it;
insilmaril@0
   550
}
insilmaril@0
   551
insilmaril@0
   552
insilmaril@0
   553
void BranchObj::move (double x, double y)
insilmaril@0
   554
{
insilmaril@0
   555
	OrnamentedObj::move (x,y);
insilmaril@0
   556
    positionBBox();
insilmaril@0
   557
}
insilmaril@0
   558
insilmaril@0
   559
void BranchObj::move (QPoint p)
insilmaril@0
   560
{
insilmaril@0
   561
	move (p.x(), p.y());
insilmaril@0
   562
}
insilmaril@0
   563
insilmaril@0
   564
void BranchObj::moveBy (double x, double y)
insilmaril@0
   565
{
insilmaril@0
   566
	OrnamentedObj::moveBy (x,y);
insilmaril@0
   567
    positionBBox();
insilmaril@0
   568
    BranchObj* b;
insilmaril@0
   569
    for (b=branch.first(); b;b=branch.next() ) 
insilmaril@0
   570
		b->moveBy (x,y);
insilmaril@0
   571
}
insilmaril@83
   572
	
insilmaril@0
   573
void BranchObj::moveBy (QPoint p)
insilmaril@0
   574
{
insilmaril@0
   575
	moveBy (p.x(), p.y());
insilmaril@0
   576
}
insilmaril@0
   577
insilmaril@0
   578
insilmaril@0
   579
void BranchObj::positionBBox()
insilmaril@0
   580
{
insilmaril@93
   581
	// FIXME testing (optimization)
insilmaril@83
   582
	/*
insilmaril@83
   583
	QString h=getHeading();
insilmaril@83
   584
	if (!h.isEmpty())
insilmaril@83
   585
		cout << "BO::positionBBox("<<h<<")\n";
insilmaril@83
   586
	else	
insilmaril@83
   587
		cout << "BO::positionBBox (noHeading)\n";
insilmaril@83
   588
	*/	
insilmaril@0
   589
insilmaril@0
   590
    heading->positionBBox();
insilmaril@0
   591
	systemFlags->positionBBox();
insilmaril@0
   592
	standardFlags->positionBBox();
insilmaril@0
   593
	// It seems that setting x,y also affects width,height
insilmaril@0
   594
	int w_old=bbox.width();
insilmaril@0
   595
	int h_old=bbox.height();
insilmaril@0
   596
    bbox.setX (absPos.x() );
insilmaril@0
   597
	bbox.setY (absPos.y() );
insilmaril@0
   598
	bbox.setWidth(w_old);
insilmaril@0
   599
	bbox.setHeight(h_old);
insilmaril@83
   600
	
insilmaril@0
   601
	setSelBox();
insilmaril@0
   602
insilmaril@0
   603
	// set the frame
insilmaril@0
   604
	frame->setRect(QRect(bbox.x(),bbox.y(),bbox.width(),bbox.height() ) );
insilmaril@83
   605
insilmaril@83
   606
	// Update links to other branches
insilmaril@95
   607
	XLinkObj *xlo;
insilmaril@95
   608
    for (xlo=xlink.first(); xlo; xlo=xlink.next() )
insilmaril@95
   609
		xlo->updateXLink();
insilmaril@0
   610
}
insilmaril@0
   611
insilmaril@0
   612
void BranchObj::calcBBoxSize()
insilmaril@0
   613
{
insilmaril@0
   614
    QSize heading_r=heading->getSize();
insilmaril@0
   615
    int heading_w=static_cast <int> (heading_r.width() );
insilmaril@0
   616
    int heading_h=static_cast <int> (heading_r.height() );
insilmaril@0
   617
    QSize sysflags_r=systemFlags->getSize();
insilmaril@0
   618
	int sysflags_h=sysflags_r.height();
insilmaril@0
   619
	int sysflags_w=sysflags_r.width();
insilmaril@0
   620
    QSize stanflags_r=standardFlags->getSize();
insilmaril@0
   621
	int stanflags_h=stanflags_r.height();
insilmaril@0
   622
	int stanflags_w=stanflags_r.width();
insilmaril@0
   623
    int w;
insilmaril@0
   624
    int h;
insilmaril@0
   625
insilmaril@0
   626
	// set width to sum of all widths
insilmaril@0
   627
	w=heading_w + sysflags_w + stanflags_w;
insilmaril@0
   628
	// set height to maximum needed height
insilmaril@0
   629
	h=max (sysflags_h,stanflags_h);
insilmaril@0
   630
	h=max (h,heading_h);
insilmaril@0
   631
insilmaril@0
   632
    w+=frame->getBorder();
insilmaril@0
   633
    h+=frame->getBorder();
insilmaril@0
   634
    bbox.setSize (QSize (w,h));
insilmaril@0
   635
}
insilmaril@0
   636
insilmaril@0
   637
LinkableMapObj* BranchObj::findMapObj(QPoint p, LinkableMapObj* excludeLMO)
insilmaril@0
   638
{
insilmaril@0
   639
	// Search branches
insilmaril@0
   640
    BranchObj *b;
insilmaril@0
   641
    LinkableMapObj *lmo;
insilmaril@0
   642
    for (b=branch.first(); b; b=branch.next() )
insilmaril@0
   643
    {	
insilmaril@0
   644
		lmo=b->findMapObj(p, excludeLMO);
insilmaril@0
   645
		if (lmo != NULL) return lmo;
insilmaril@0
   646
    }
insilmaril@0
   647
	
insilmaril@0
   648
	// Search myself
insilmaril@0
   649
    if (inBBox (p) && (this != excludeLMO) && isVisibleObj() ) 
insilmaril@0
   650
		return this;
insilmaril@0
   651
insilmaril@0
   652
	// Search float images
insilmaril@0
   653
	FloatImageObj *foi;
insilmaril@0
   654
    for (foi=floatimage.first(); foi; foi=floatimage.next() )
insilmaril@0
   655
		if (foi->inBBox(p) && (foi != excludeLMO) && foi->getParObj()!= excludeLMO) return foi;
insilmaril@0
   656
insilmaril@0
   657
    return NULL;
insilmaril@0
   658
}
insilmaril@0
   659
insilmaril@0
   660
void BranchObj::setHeading(QString s)
insilmaril@0
   661
{
insilmaril@146
   662
	/*FIXME
insilmaril@0
   663
    // Adjusting font size
insilmaril@0
   664
    QFont font=heading->getFont();
insilmaril@139
   665
	font.setPointSize(getDefHeadingSize() );
insilmaril@0
   666
    heading->setFont(font);
insilmaril@146
   667
	*/
insilmaril@0
   668
    heading->setText(s);	// set new heading
insilmaril@0
   669
	calcBBoxSize();			// recalculate bbox
insilmaril@0
   670
    positionBBox();			// rearrange contents
insilmaril@0
   671
	requestReposition();
insilmaril@0
   672
}
insilmaril@0
   673
insilmaril@0
   674
void BranchObj::setURL(QString s)
insilmaril@0
   675
{
insilmaril@0
   676
	url=s;
insilmaril@0
   677
	if (!url.isEmpty())
insilmaril@0
   678
		systemFlags->activate("url");
insilmaril@0
   679
	else	
insilmaril@0
   680
		systemFlags->deactivate("url");
insilmaril@0
   681
	calcBBoxSize();			// recalculate bbox
insilmaril@0
   682
    positionBBox();			// rearrange contents
insilmaril@0
   683
	forceReposition();
insilmaril@0
   684
}
insilmaril@0
   685
insilmaril@0
   686
QString BranchObj::getURL()
insilmaril@0
   687
{
insilmaril@0
   688
	return url;
insilmaril@0
   689
}
insilmaril@0
   690
insilmaril@0
   691
void BranchObj::setVymLink(QString s)
insilmaril@0
   692
{
insilmaril@0
   693
	if (!s.isEmpty())
insilmaril@0
   694
	{
insilmaril@0
   695
		// We need the relative (from loading) 
insilmaril@0
   696
		// or absolute path (from User event)
insilmaril@0
   697
		// and build the absolute path.
insilmaril@0
   698
		// Note: If we have relative, use path of
insilmaril@0
   699
		// current map to build absolute path
insilmaril@0
   700
		QDir d(s);
insilmaril@0
   701
		if (!d.path().startsWith ("/"))
insilmaril@0
   702
		{
insilmaril@0
   703
			QString p=mapEditor->getDestPath();
insilmaril@0
   704
			int i=p.findRev("/",-1);
insilmaril@0
   705
			d.setPath(p.left(i)+"/"+s);
insilmaril@0
   706
			d.convertToAbs();
insilmaril@0
   707
		}
insilmaril@0
   708
		vymLink=d.path();
insilmaril@0
   709
		systemFlags->activate("vymLink");
insilmaril@0
   710
	}	
insilmaril@0
   711
	else	
insilmaril@0
   712
	{
insilmaril@0
   713
		systemFlags->deactivate("vymLink");
insilmaril@0
   714
		vymLink="";
insilmaril@0
   715
	}	
insilmaril@0
   716
	calcBBoxSize();			// recalculate bbox
insilmaril@0
   717
    positionBBox();			// rearrange contents
insilmaril@0
   718
	forceReposition();
insilmaril@0
   719
}
insilmaril@0
   720
insilmaril@0
   721
QString BranchObj::getVymLink()
insilmaril@0
   722
{
insilmaril@0
   723
	return vymLink;
insilmaril@0
   724
}
insilmaril@0
   725
insilmaril@0
   726
QString BranchObj::saveToDir (const QString &tmpdir,const QString &prefix, const QPoint& offset)
insilmaril@0
   727
{
insilmaril@0
   728
    QString s,a;
insilmaril@0
   729
	QString scrolledAttr;
insilmaril@0
   730
	if (scrolled) 
insilmaril@0
   731
		scrolledAttr=attribut ("scrolled","yes");
insilmaril@0
   732
	else
insilmaril@0
   733
		scrolledAttr="";
insilmaril@0
   734
insilmaril@0
   735
	QString posAttr;
insilmaril@0
   736
	if (depth<2) posAttr=
insilmaril@0
   737
		attribut("absPosX",QString().setNum(absPos.x(),10)) +
insilmaril@0
   738
		attribut("absPosY",QString().setNum(absPos.y(),10)); 
insilmaril@0
   739
	else
insilmaril@0
   740
		posAttr="";
insilmaril@0
   741
insilmaril@0
   742
	QString urlAttr;
insilmaril@0
   743
	if (!url.isEmpty())
insilmaril@0
   744
		urlAttr=attribut ("url",url);
insilmaril@0
   745
insilmaril@0
   746
	QString vymLinkAttr;
insilmaril@0
   747
	if (!vymLink.isEmpty())
insilmaril@0
   748
		vymLinkAttr=attribut ("vymLink",convertToRel(mapEditor->getDestPath(),vymLink) );
insilmaril@0
   749
insilmaril@0
   750
	QString frameAttr;
insilmaril@0
   751
	if (frame->getFrameType()!=NoFrame)
insilmaril@0
   752
		frameAttr=attribut ("frameType",frame->getFrameTypeName());
insilmaril@0
   753
	else
insilmaril@0
   754
		frameAttr="";
insilmaril@0
   755
insilmaril@0
   756
	// save area, if not scrolled
insilmaril@0
   757
	QString areaAttr;
insilmaril@0
   758
	if (!((BranchObj*)(parObj))->isScrolled() )
insilmaril@0
   759
	{
insilmaril@0
   760
		areaAttr=
insilmaril@0
   761
			attribut("x1",QString().setNum(absPos.x()-offset.x(),10)) +
insilmaril@0
   762
			attribut("y1",QString().setNum(absPos.y()-offset.y(),10)) +
insilmaril@0
   763
			attribut("x2",QString().setNum(absPos.x()+width()-offset.x(),10)) +
insilmaril@0
   764
			attribut("y2",QString().setNum(absPos.y()+height()-offset.y(),10));
insilmaril@0
   765
insilmaril@0
   766
	} else
insilmaril@0
   767
		areaAttr="";
insilmaril@0
   768
	
insilmaril@93
   769
    s=beginElement ("branch" +scrolledAttr +posAttr +urlAttr +vymLinkAttr +frameAttr +areaAttr );
insilmaril@0
   770
    incIndent();
insilmaril@0
   771
insilmaril@0
   772
	// save heading
insilmaril@0
   773
    s=s+valueElement("heading", getHeading(),
insilmaril@0
   774
		attribut ("textColor",QColor(heading->getColor()).name()));
insilmaril@0
   775
insilmaril@0
   776
	// save names of flags set
insilmaril@0
   777
	s+=standardFlags->saveToDir(tmpdir,prefix,0);
insilmaril@0
   778
	
insilmaril@0
   779
	// save note
insilmaril@0
   780
	if (!note.isEmpty() )
insilmaril@0
   781
		s+=note.saveToDir();
insilmaril@0
   782
	
insilmaril@0
   783
	// Save branches
insilmaril@0
   784
    BranchObj *bo;
insilmaril@0
   785
    for (bo=branch.first(); bo; bo=branch.next() )
insilmaril@0
   786
		s+=bo->saveToDir(tmpdir,prefix,offset);
insilmaril@0
   787
insilmaril@0
   788
	// Save FloatImages
insilmaril@0
   789
	FloatImageObj *fio;
insilmaril@0
   790
	for (fio=floatimage.first(); fio; fio=floatimage.next() )
insilmaril@0
   791
		s+=fio->saveToDir (tmpdir,prefix);
insilmaril@0
   792
insilmaril@95
   793
	// Save XLinks
insilmaril@95
   794
	XLinkObj *xlo;
insilmaril@95
   795
    for (xlo=xlink.first(); xlo; xlo=xlink.next() )
insilmaril@95
   796
		s+=xlo->saveToDir();
insilmaril@95
   797
insilmaril@95
   798
    decIndent();
insilmaril@0
   799
    s+=endElement   ("branch");
insilmaril@0
   800
    return s;
insilmaril@0
   801
}
insilmaril@0
   802
insilmaril@95
   803
void BranchObj::addXLink (XLinkObj *xlo)
insilmaril@83
   804
{
insilmaril@95
   805
	xlink.append (xlo);
insilmaril@83
   806
	
insilmaril@83
   807
}
insilmaril@83
   808
insilmaril@95
   809
void BranchObj::removeXLinkRef (XLinkObj *xlo)
insilmaril@83
   810
{
insilmaril@95
   811
	xlink.remove (xlo);
insilmaril@83
   812
}
insilmaril@83
   813
insilmaril@95
   814
void BranchObj::deleteXLink(XLinkObj *xlo)
insilmaril@83
   815
{
insilmaril@95
   816
	xlo->deactivate();
insilmaril@95
   817
	if (!xlo->isUsed()) delete (xlo);
insilmaril@83
   818
}
insilmaril@83
   819
insilmaril@95
   820
void BranchObj::deleteXLinkAt (int i)
insilmaril@94
   821
{
insilmaril@95
   822
	XLinkObj *xlo=xlink.at(i);
insilmaril@95
   823
	xlo->deactivate();
insilmaril@95
   824
	if (!xlo->isUsed()) delete(xlo);
insilmaril@94
   825
}
insilmaril@94
   826
insilmaril@97
   827
XLinkObj* BranchObj::XLinkAt (int i)
insilmaril@97
   828
{
insilmaril@97
   829
	return xlink.at(i);
insilmaril@97
   830
}
insilmaril@97
   831
insilmaril@95
   832
int BranchObj::countXLink()
insilmaril@83
   833
{
insilmaril@95
   834
	return xlink.count();
insilmaril@83
   835
}
insilmaril@83
   836
insilmaril@95
   837
BranchObj* BranchObj::XLinkTargetAt (int i)
insilmaril@89
   838
{
insilmaril@95
   839
	if (xlink.at(i))
insilmaril@95
   840
		return xlink.at(i)->otherBranch (this);
insilmaril@89
   841
	else
insilmaril@89
   842
		return NULL;
insilmaril@89
   843
}
insilmaril@89
   844
insilmaril@0
   845
LinkableMapObj* BranchObj::addFloatImage ()
insilmaril@0
   846
{
insilmaril@0
   847
	FloatImageObj *newfi=new FloatImageObj (canvas,this);
insilmaril@0
   848
	floatimage.append (newfi);
insilmaril@0
   849
	if (hasScrolledParent(this) )
insilmaril@0
   850
		newfi->setVisibility (false);
insilmaril@0
   851
	else	
insilmaril@0
   852
		newfi->setVisibility(visible);
insilmaril@0
   853
	requestReposition();
insilmaril@0
   854
	return newfi;
insilmaril@0
   855
}
insilmaril@0
   856
insilmaril@0
   857
LinkableMapObj* BranchObj::addFloatImage (FloatImageObj *fio)
insilmaril@0
   858
{
insilmaril@0
   859
	FloatImageObj *newfi=new FloatImageObj (canvas,this);
insilmaril@0
   860
	floatimage.append (newfi);
insilmaril@0
   861
	newfi->copy (fio);
insilmaril@0
   862
	if (hasScrolledParent(this) )
insilmaril@0
   863
		newfi->setVisibility (false);
insilmaril@0
   864
	else	
insilmaril@0
   865
		newfi->setVisibility(visible);
insilmaril@0
   866
	requestReposition();
insilmaril@0
   867
	return newfi;
insilmaril@0
   868
}
insilmaril@0
   869
insilmaril@0
   870
FloatImageObj* BranchObj::getFirstFloatImage ()
insilmaril@0
   871
{
insilmaril@0
   872
    return floatimage.first();
insilmaril@0
   873
}
insilmaril@0
   874
insilmaril@0
   875
FloatImageObj* BranchObj::getLastFloatImage ()
insilmaril@0
   876
{
insilmaril@0
   877
    return floatimage.last();
insilmaril@0
   878
}
insilmaril@0
   879
insilmaril@0
   880
FloatImageObj* BranchObj::getFloatImageNum (const uint &i)
insilmaril@0
   881
{
insilmaril@0
   882
    return floatimage.at(i);
insilmaril@0
   883
}
insilmaril@0
   884
insilmaril@0
   885
void BranchObj::removeFloatImage (FloatImageObj *fio)
insilmaril@0
   886
{
insilmaril@0
   887
	floatimage.remove (fio);
insilmaril@0
   888
	requestReposition();
insilmaril@0
   889
}
insilmaril@0
   890
insilmaril@0
   891
void BranchObj::savePosInAngle ()
insilmaril@0
   892
{
insilmaril@0
   893
	// Save position in angle
insilmaril@0
   894
    BranchObj *b;
insilmaril@0
   895
	int i=0;
insilmaril@0
   896
    for (b=branch.first(); b; b=branch.next() )
insilmaril@0
   897
	{
insilmaril@0
   898
		b->angle=i;
insilmaril@0
   899
		i++;
insilmaril@0
   900
	}
insilmaril@0
   901
}
insilmaril@0
   902
insilmaril@146
   903
void BranchObj::setDefAttr (BranchModification mod)
insilmaril@146
   904
{
insilmaril@146
   905
	int fontsize;
insilmaril@146
   906
	switch (depth)
insilmaril@146
   907
	{
insilmaril@146
   908
		case 0: fontsize=16; break;
insilmaril@146
   909
		case 1: fontsize=12; break;
insilmaril@146
   910
		default: fontsize=10; break;
insilmaril@146
   911
	}	
insilmaril@146
   912
insilmaril@146
   913
	setLinkColor ();
insilmaril@146
   914
	setLinkStyle(getDefLinkStyle());
insilmaril@146
   915
	QFont font("Sans Serif,8,-1,5,50,0,0,0,0,0");
insilmaril@146
   916
	font.setPointSize(fontsize);
insilmaril@146
   917
	heading->setFont(font );
insilmaril@146
   918
insilmaril@146
   919
	if (mod==NewBranch)
insilmaril@146
   920
		setColor (((BranchObj*)(parObj))->getColor(),false);
insilmaril@146
   921
	
insilmaril@146
   922
	calcBBoxSize();
insilmaril@146
   923
}
insilmaril@146
   924
insilmaril@0
   925
BranchObj* BranchObj::addBranch()
insilmaril@0
   926
{
insilmaril@0
   927
    BranchObj* newbo=new BranchObj(canvas,this);
insilmaril@0
   928
    branch.append (newbo);
insilmaril@0
   929
    newbo->setParObj(this);
insilmaril@146
   930
	newbo->setDefAttr(NewBranch);
insilmaril@0
   931
    newbo->setHeading ("new");
insilmaril@0
   932
	if (scrolled)
insilmaril@0
   933
		newbo->setVisibility (false);
insilmaril@0
   934
	else	
insilmaril@0
   935
		newbo->setVisibility(visible);
insilmaril@93
   936
	newbo->updateLink();	
insilmaril@0
   937
	requestReposition();
insilmaril@0
   938
	return newbo;
insilmaril@0
   939
}
insilmaril@0
   940
insilmaril@0
   941
BranchObj* BranchObj::addBranch(BranchObj* bo)
insilmaril@0
   942
{
insilmaril@0
   943
    BranchObj* newbo=new BranchObj(canvas,this);
insilmaril@0
   944
    branch.append (newbo);
insilmaril@0
   945
    newbo->copy(bo);
insilmaril@0
   946
    newbo->setParObj(this);
insilmaril@146
   947
	newbo->setDefAttr(NewBranch);
insilmaril@0
   948
	if (scrolled)
insilmaril@0
   949
		newbo->setVisibility (false);
insilmaril@0
   950
	else	
insilmaril@0
   951
		newbo->setVisibility(bo->visible);
insilmaril@93
   952
	newbo->updateLink();	
insilmaril@0
   953
	requestReposition();
insilmaril@0
   954
	return newbo;
insilmaril@0
   955
}
insilmaril@0
   956
insilmaril@83
   957
BranchObj* BranchObj::addBranchPtr(BranchObj* bo)
insilmaril@83
   958
{
insilmaril@83
   959
	branch.append (bo);
insilmaril@83
   960
	bo->setParObj (this);
insilmaril@83
   961
	bo->depth=depth+1;
insilmaril@146
   962
	bo->setDefAttr(MovedBranch);
insilmaril@83
   963
	if (scrolled) tmpUnscroll();
insilmaril@83
   964
	setLastSelectedBranch (bo);
insilmaril@83
   965
	return bo;
insilmaril@83
   966
}
insilmaril@83
   967
insilmaril@0
   968
BranchObj* BranchObj::insertBranch(int pos)
insilmaril@0
   969
{
insilmaril@0
   970
	savePosInAngle();
insilmaril@0
   971
	// Add new bo and resort branches
insilmaril@0
   972
	BranchObj *newbo=addBranch ();
insilmaril@0
   973
	newbo->angle=pos-0.5;
insilmaril@0
   974
	branch.sort();
insilmaril@0
   975
	return newbo;
insilmaril@0
   976
}
insilmaril@0
   977
insilmaril@0
   978
BranchObj* BranchObj::insertBranch(BranchObj* bo, int pos)
insilmaril@0
   979
{
insilmaril@0
   980
	savePosInAngle();
insilmaril@0
   981
	// Add new bo and resort branches
insilmaril@0
   982
	bo->angle=pos-0.5;
insilmaril@0
   983
	BranchObj *newbo=addBranch (bo);
insilmaril@0
   984
	branch.sort();
insilmaril@0
   985
	return newbo;
insilmaril@0
   986
}
insilmaril@0
   987
insilmaril@83
   988
BranchObj* BranchObj::insertBranchPtr (BranchObj* bo, int pos)
insilmaril@83
   989
{
insilmaril@83
   990
	savePosInAngle();
insilmaril@83
   991
	// Add new bo and resort branches
insilmaril@83
   992
	bo->angle=pos-0.5;
insilmaril@83
   993
	branch.append (bo);
insilmaril@83
   994
	bo->setParObj (this);
insilmaril@83
   995
	bo->depth=depth+1;
insilmaril@146
   996
	bo->setDefAttr (MovedBranch);
insilmaril@83
   997
	if (scrolled) tmpUnscroll();
insilmaril@83
   998
	setLastSelectedBranch (bo);
insilmaril@83
   999
	branch.sort();
insilmaril@83
  1000
	return bo;
insilmaril@83
  1001
}
insilmaril@83
  1002
insilmaril@98
  1003
void BranchObj::removeBranchHere(BranchObj* borem)
insilmaril@98
  1004
{
insilmaril@98
  1005
	// This removes the branch bo from list, but 
insilmaril@98
  1006
	// inserts its childs at the place of bo
insilmaril@98
  1007
	BranchObj *bo;
insilmaril@98
  1008
	bo=borem->getLastBranch();
insilmaril@139
  1009
	int pos=borem->getNum();
insilmaril@98
  1010
	while (bo)
insilmaril@98
  1011
	{
insilmaril@139
  1012
		bo->moveBranchTo (this,pos+1);
insilmaril@98
  1013
		bo=borem->getLastBranch();
insilmaril@98
  1014
	}	
insilmaril@98
  1015
	removeBranch (borem);
insilmaril@98
  1016
}
insilmaril@98
  1017
insilmaril@98
  1018
void BranchObj::removeChilds()
insilmaril@98
  1019
{
insilmaril@98
  1020
	clear();
insilmaril@98
  1021
}
insilmaril@98
  1022
insilmaril@0
  1023
void BranchObj::removeBranch(BranchObj* bo)
insilmaril@0
  1024
{
insilmaril@0
  1025
    // if bo is not in branch remove returns false, we
insilmaril@0
  1026
    // don't care...
insilmaril@83
  1027
	
insilmaril@83
  1028
    if (branch.remove (bo))
insilmaril@83
  1029
		delete (bo);
insilmaril@83
  1030
	else
insilmaril@83
  1031
		qWarning ("BranchObj::removeBranch tried to remove non existing branch?!\n");
insilmaril@83
  1032
	requestReposition();
insilmaril@83
  1033
}
insilmaril@83
  1034
insilmaril@83
  1035
void BranchObj::removeBranchPtr(BranchObj* bo)
insilmaril@83
  1036
{
insilmaril@83
  1037
	branch.remove (bo);
insilmaril@0
  1038
	requestReposition();
insilmaril@0
  1039
}
insilmaril@0
  1040
insilmaril@0
  1041
void BranchObj::setLastSelectedBranch (BranchObj* bo)
insilmaril@0
  1042
{
insilmaril@0
  1043
    lastSelectedBranch=branch.find(bo);
insilmaril@0
  1044
}
insilmaril@0
  1045
insilmaril@0
  1046
BranchObj* BranchObj::getLastSelectedBranch ()
insilmaril@0
  1047
{
insilmaril@0
  1048
    if (lastSelectedBranch>=0) 
insilmaril@0
  1049
	{
insilmaril@0
  1050
		BranchObj* bo=branch.at(lastSelectedBranch);
insilmaril@0
  1051
		if (bo) return bo;
insilmaril@0
  1052
    }	
insilmaril@0
  1053
    return branch.first();
insilmaril@0
  1054
}
insilmaril@0
  1055
insilmaril@0
  1056
BranchObj* BranchObj::getFirstBranch ()
insilmaril@0
  1057
{
insilmaril@0
  1058
    return branch.first();
insilmaril@0
  1059
}
insilmaril@0
  1060
insilmaril@0
  1061
BranchObj* BranchObj::getLastBranch ()
insilmaril@0
  1062
{
insilmaril@0
  1063
    return branch.last();
insilmaril@0
  1064
}
insilmaril@0
  1065
insilmaril@0
  1066
BranchObj* BranchObj::getBranchNum (const uint &i)
insilmaril@0
  1067
{
insilmaril@0
  1068
    return branch.at(i);
insilmaril@0
  1069
}
insilmaril@0
  1070
insilmaril@0
  1071
insilmaril@0
  1072
BranchObj* BranchObj::moveBranchUp(BranchObj* bo1) // move a branch up (modify myself)
insilmaril@0
  1073
{
insilmaril@0
  1074
	savePosInAngle();
insilmaril@0
  1075
    int i=branch.find(bo1);
insilmaril@0
  1076
    if (i>0) 
insilmaril@0
  1077
	{	// -1 if bo1 not found 
insilmaril@0
  1078
		branch.at(i)->angle--;
insilmaril@0
  1079
		branch.at(i-1)->angle++;
insilmaril@0
  1080
		branch.sort();
insilmaril@0
  1081
		return branch.at(i-1);
insilmaril@0
  1082
	} else
insilmaril@0
  1083
		return branch.at(i);
insilmaril@0
  1084
}
insilmaril@0
  1085
insilmaril@0
  1086
BranchObj* BranchObj::moveBranchDown(BranchObj* bo1)
insilmaril@0
  1087
{
insilmaril@0
  1088
	savePosInAngle();
insilmaril@0
  1089
    int i=branch.find(bo1);
insilmaril@0
  1090
	int j;
insilmaril@0
  1091
	if (branch.next())
insilmaril@0
  1092
	{
insilmaril@0
  1093
		j = branch.at();
insilmaril@0
  1094
		branch.at(i)->angle++;
insilmaril@0
  1095
		branch.at(j)->angle--;
insilmaril@0
  1096
		branch.sort();
insilmaril@0
  1097
		return branch.at(j);
insilmaril@0
  1098
	} else
insilmaril@0
  1099
		return branch.at(i);
insilmaril@0
  1100
}
insilmaril@0
  1101
insilmaril@83
  1102
BranchObj* BranchObj::moveBranchTo (BranchObj* dst, int pos)
insilmaril@83
  1103
{
insilmaril@83
  1104
	// Find current parent and 
insilmaril@83
  1105
	// remove pointer to myself there
insilmaril@94
  1106
	if (!dst) return NULL;
insilmaril@83
  1107
	BranchObj *par=(BranchObj*)(parObj);
insilmaril@83
  1108
	if (par)
insilmaril@83
  1109
		par->removeBranchPtr (this);
insilmaril@83
  1110
	else
insilmaril@83
  1111
		return NULL;
insilmaril@83
  1112
insilmaril@129
  1113
	// Create new pointer to myself at dst
insilmaril@98
  1114
	if (pos<0||dst->getDepth()==0)
insilmaril@83
  1115
	{	
insilmaril@98
  1116
		// links myself as last branch at dst
insilmaril@83
  1117
		dst->addBranchPtr (this);
insilmaril@151
  1118
		updateLink();
insilmaril@83
  1119
		return this;
insilmaril@83
  1120
	} else
insilmaril@83
  1121
	{
insilmaril@83
  1122
		// inserts me at pos in parent of dst
insilmaril@83
  1123
		if (par)
insilmaril@139
  1124
		{
insilmaril@139
  1125
			BranchObj *bo=dst->insertBranchPtr (this,pos);
insilmaril@146
  1126
			bo->setDefAttr(MovedBranch);
insilmaril@151
  1127
			updateLink();
insilmaril@139
  1128
			return bo;
insilmaril@139
  1129
insilmaril@139
  1130
		} else
insilmaril@83
  1131
			return NULL;
insilmaril@83
  1132
	}	
insilmaril@83
  1133
}
insilmaril@83
  1134
insilmaril@0
  1135
void BranchObj::alignRelativeTo (QPoint ref)
insilmaril@0
  1136
{
insilmaril@0
  1137
/* FIXME testing
insilmaril@0
  1138
	if (!getHeading().isEmpty())
insilmaril@0
  1139
		cout << "BO::alignRelTo "<<getHeading()<<endl;
insilmaril@0
  1140
	else	
insilmaril@0
  1141
		cout << "BO::alignRelTo  ???"<<endl;
insilmaril@0
  1142
	cout << "  d="<<depth<<endl;
insilmaril@0
  1143
*/	
insilmaril@0
  1144
	int th = bboxTotal.height();	
insilmaril@0
  1145
insilmaril@0
  1146
	// If I am the mapcenter or a mainbranch, reposition heading
insilmaril@0
  1147
	if (depth<2)
insilmaril@0
  1148
	{
insilmaril@0
  1149
		move (absPos.x(),absPos.y());
insilmaril@0
  1150
		if (depth==1)
insilmaril@0
  1151
		{
insilmaril@0
  1152
			// Calc angle to mapCenter if I am a mainbranch
insilmaril@0
  1153
			// needed for reordering the mainbranches clockwise 
insilmaril@0
  1154
			// around mapcenter 
insilmaril@0
  1155
			angle=getAngle (QPoint ((int)(x() - parObj->getChildPos().x() ), 
insilmaril@0
  1156
									(int)(y() - parObj->getChildPos().y() ) ) );
insilmaril@0
  1157
		}	
insilmaril@0
  1158
	} 
insilmaril@0
  1159
	else
insilmaril@0
  1160
    {
insilmaril@0
  1161
		// Align myself depending on orientation and parent, but
insilmaril@0
  1162
		// only if I am not the mainbranch or mapcenter itself
insilmaril@0
  1163
		switch (orientation) 
insilmaril@0
  1164
		{
insilmaril@0
  1165
			case OrientLeftOfCenter:
insilmaril@0
  1166
				move (ref.x()-bbox.width(), ref.y() + (th-bbox.height())/2 );
insilmaril@0
  1167
			break;
insilmaril@0
  1168
			case OrientRightOfCenter:	
insilmaril@0
  1169
				move (ref.x(), ref.y() + (th-bbox.height())/2 );
insilmaril@0
  1170
			break;
insilmaril@0
  1171
			default:
insilmaril@0
  1172
				cout <<"LMO::alignRelativeTo: oops, no orientation given...\n";
insilmaril@0
  1173
			break;
insilmaril@0
  1174
		}		
insilmaril@0
  1175
    }		
insilmaril@0
  1176
insilmaril@0
  1177
	FloatImageObj *fio;
insilmaril@0
  1178
    for (fio=floatimage.first(); fio; fio=floatimage.next() )
insilmaril@0
  1179
		fio->reposition();
insilmaril@0
  1180
insilmaril@0
  1181
	if (scrolled) return;
insilmaril@0
  1182
insilmaril@0
  1183
    // Set reference point for alignment of childs
insilmaril@0
  1184
    QPoint ref2;
insilmaril@0
  1185
    if (orientation==OrientLeftOfCenter)
insilmaril@0
  1186
		ref2.setX(childPos.x() - linkwidth);
insilmaril@0
  1187
    else	
insilmaril@0
  1188
		ref2.setX(childPos.x() + linkwidth);
insilmaril@0
  1189
insilmaril@0
  1190
	if (depth==1)
insilmaril@0
  1191
		ref2.setY(absPos.y()-(bboxTotal.height()-bbox.height())/2);
insilmaril@0
  1192
	else	
insilmaril@0
  1193
		ref2.setY(ref.y() );	
insilmaril@0
  1194
insilmaril@0
  1195
    // Align the childs depending on reference point 
insilmaril@0
  1196
    BranchObj *b;
insilmaril@0
  1197
    for (b=branch.first(); b; b=branch.next() )
insilmaril@0
  1198
    {	
insilmaril@0
  1199
		b->alignRelativeTo (ref2);
insilmaril@0
  1200
		ref2.setY(ref2.y() + b->getBBoxSizeWithChilds().height() );
insilmaril@0
  1201
    }
insilmaril@0
  1202
}
insilmaril@0
  1203
insilmaril@0
  1204
insilmaril@0
  1205
void BranchObj::reposition()
insilmaril@0
  1206
{	
insilmaril@0
  1207
/* FIXME testing
insilmaril@0
  1208
	if (!getHeading().isEmpty())
insilmaril@0
  1209
		cout << "BO::reposition  "<<getHeading()<<endl;
insilmaril@0
  1210
	else	
insilmaril@0
  1211
		cout << "BO::reposition  ???"<<endl;
insilmaril@0
  1212
*/		
insilmaril@0
  1213
	if (depth==0)
insilmaril@0
  1214
	{
insilmaril@0
  1215
		// only calculate the sizes once. If the deepest LMO 
insilmaril@0
  1216
		// changes its height,
insilmaril@0
  1217
		// all upper LMOs have to change, too.
insilmaril@0
  1218
		calcBBoxSizeWithChilds();
insilmaril@0
  1219
	    alignRelativeTo ( QPoint (absPos.x(),
insilmaril@0
  1220
			absPos.y()-(bboxTotal.height()-bbox.height())/2) );
insilmaril@0
  1221
		branch.sort();	
insilmaril@0
  1222
	} else
insilmaril@0
  1223
	{
insilmaril@0
  1224
		// This is only important for moving branches:
insilmaril@0
  1225
		// For editing a branch it isn't called...
insilmaril@0
  1226
	    alignRelativeTo ( QPoint (absPos.x(),
insilmaril@0
  1227
							absPos.y()-(bboxTotal.height()-bbox.height())/2) );
insilmaril@0
  1228
	}
insilmaril@0
  1229
}
insilmaril@0
  1230
insilmaril@0
  1231
insilmaril@0
  1232
QRect BranchObj::getTotalBBox()
insilmaril@0
  1233
{
insilmaril@0
  1234
	QRect r=bbox;
insilmaril@0
  1235
insilmaril@0
  1236
	if (scrolled) return r;
insilmaril@0
  1237
insilmaril@0
  1238
	BranchObj* b;
insilmaril@0
  1239
	for (b=branch.first();b ;b=branch.next() )
insilmaril@0
  1240
		r=addBBox(b->getTotalBBox(),r);
insilmaril@0
  1241
insilmaril@0
  1242
	FloatImageObj* fio;
insilmaril@0
  1243
	for (fio=floatimage.first();fio ;fio=floatimage.next() )
insilmaril@0
  1244
		r=addBBox(fio->getTotalBBox(),r);
insilmaril@0
  1245
		
insilmaril@0
  1246
	return r;
insilmaril@0
  1247
}
insilmaril@0
  1248
insilmaril@0
  1249
QRect BranchObj::getBBoxSizeWithChilds()
insilmaril@0
  1250
{
insilmaril@0
  1251
	return bboxTotal;
insilmaril@0
  1252
}
insilmaril@0
  1253
insilmaril@0
  1254
void BranchObj::calcBBoxSizeWithChilds()
insilmaril@0
  1255
{
insilmaril@0
  1256
	// This is called only from reposition and
insilmaril@0
  1257
	// and only for mapcenter. So it won't be
insilmaril@0
  1258
	// called more than once for a single user 
insilmaril@0
  1259
	// action
insilmaril@0
  1260
	
insilmaril@0
  1261
	// Calculate size of LMO including all childs (to align them later)
insilmaril@0
  1262
insilmaril@0
  1263
	bboxTotal.setX(bbox.x() );
insilmaril@0
  1264
	bboxTotal.setY(bbox.y() );
insilmaril@0
  1265
insilmaril@0
  1266
	// if branch is scrolled, ignore childs, but still consider floatimages
insilmaril@0
  1267
	if (scrolled)
insilmaril@0
  1268
	{
insilmaril@0
  1269
		bboxTotal.setWidth (bbox.width());
insilmaril@0
  1270
		bboxTotal.setHeight(bbox.height());
insilmaril@0
  1271
		return;
insilmaril@0
  1272
	}
insilmaril@0
  1273
	
insilmaril@0
  1274
	QRect r(0,0,0,0);
insilmaril@0
  1275
	QRect br;
insilmaril@0
  1276
	// Now calculate recursivly
insilmaril@0
  1277
	// sum of heights 
insilmaril@0
  1278
	// maximum of widths 
insilmaril@0
  1279
	// minimum of y
insilmaril@0
  1280
	BranchObj* b;
insilmaril@0
  1281
	for (b=branch.first();b ;b=branch.next() )
insilmaril@0
  1282
	{
insilmaril@0
  1283
		b->calcBBoxSizeWithChilds();
insilmaril@0
  1284
		br=b->getBBoxSizeWithChilds();
insilmaril@0
  1285
		r.setWidth( max (br.width(), r.width() ));
insilmaril@0
  1286
		r.setHeight(br.height() + r.height() );
insilmaril@0
  1287
		if (br.y()<bboxTotal.y()) bboxTotal.setY(br.y());
insilmaril@0
  1288
	}
insilmaril@0
  1289
	// Add myself and also
insilmaril@0
  1290
	// add width of link to sum if necessary
insilmaril@0
  1291
	if (branch.isEmpty())
insilmaril@0
  1292
		bboxTotal.setWidth (bbox.width() + r.width() );
insilmaril@0
  1293
	else	
insilmaril@0
  1294
		bboxTotal.setWidth (bbox.width() + r.width() + linkwidth);
insilmaril@0
  1295
	bboxTotal.setHeight(max (r.height(),  bbox.height() ) );
insilmaril@0
  1296
//	frame->setRect(QRect(bbox.x(),bbox.y(),bbox.width(),bbox.height() ) );
insilmaril@0
  1297
}
insilmaril@0
  1298
insilmaril@0
  1299
void BranchObj::select()
insilmaril@0
  1300
{
insilmaril@0
  1301
    LinkableMapObj::select();
insilmaril@0
  1302
	// Tell parent that I am selected now:
insilmaril@0
  1303
	BranchObj* po=(BranchObj*)(parObj);
insilmaril@0
  1304
    if (po)	// TODO	    Try to get rid of this cast...
insilmaril@0
  1305
        po->setLastSelectedBranch(this);
insilmaril@0
  1306
		
insilmaril@0
  1307
	// temporary unscroll, if we have scrolled parents somewhere
insilmaril@0
  1308
	if (parObj) ((BranchObj*)(parObj))->tmpUnscroll();
insilmaril@0
  1309
insilmaril@0
  1310
	// set Text in Editor	
insilmaril@0
  1311
	textEditor->setText(note.getNote() );
insilmaril@106
  1312
	QString fnh=note.getFilenameHint();
insilmaril@106
  1313
	if (fnh!="")
insilmaril@106
  1314
		textEditor->setFilenameHint(note.getFilenameHint() );
insilmaril@106
  1315
	else	
insilmaril@106
  1316
		textEditor->setFilenameHint(getHeading() );
insilmaril@0
  1317
	textEditor->setFontHint (note.getFontHint() );
insilmaril@0
  1318
	connect (textEditor, SIGNAL (textHasChanged() ), this, SLOT (updateNoteFlag() ) ); 
insilmaril@0
  1319
	connect (textEditor, SIGNAL (fontSizeHasChanged() ), this, SLOT (updateNoteFlag() ) ); 
insilmaril@0
  1320
insilmaril@0
  1321
	// Show URL and link in statusbar
insilmaril@0
  1322
	QString status;
insilmaril@0
  1323
	if (!url.isEmpty()) status+="URL: "+url+"  ";
insilmaril@0
  1324
	if (!vymLink.isEmpty()) status+="Link: "+vymLink;
insilmaril@0
  1325
	if (!status.isEmpty()) mainWindow->statusMessage (status);
insilmaril@0
  1326
insilmaril@0
  1327
	// Update Toolbar
insilmaril@103
  1328
	standardFlags->updateToolbar();
insilmaril@0
  1329
insilmaril@0
  1330
	// Update Browserbutton
insilmaril@0
  1331
	if (!url.isEmpty())
insilmaril@0
  1332
		actionEditOpenURL->setEnabled (true);
insilmaril@0
  1333
	else	
insilmaril@0
  1334
		actionEditOpenURL->setEnabled (false);
insilmaril@0
  1335
insilmaril@0
  1336
	// Update actions in mapeditor
insilmaril@0
  1337
	mapEditor->updateActions();
insilmaril@0
  1338
}
insilmaril@0
  1339
insilmaril@0
  1340
void BranchObj::unselect()
insilmaril@0
  1341
{
insilmaril@0
  1342
	LinkableMapObj::unselect();
insilmaril@0
  1343
	// Delete any messages like vymLink in StatusBar
insilmaril@0
  1344
	mainWindow->statusMessage ("");
insilmaril@0
  1345
insilmaril@0
  1346
	// save note from editor and set flag
insilmaril@0
  1347
	// text is done by updateNoteFlag(), just save
insilmaril@0
  1348
	// filename here
insilmaril@0
  1349
	note.setFilenameHint (textEditor->getFilename());
insilmaril@0
  1350
insilmaril@0
  1351
	// reset temporary unscroll, if we have scrolled parents somewhere
insilmaril@0
  1352
	if (parObj) ((BranchObj*)(parObj))->resetTmpUnscroll();
insilmaril@0
  1353
insilmaril@0
  1354
	// Disconnect textEditor from this LMO
insilmaril@0
  1355
	disconnect( textEditor, SIGNAL(textHasChanged()), 0, 0 );
insilmaril@0
  1356
	disconnect( textEditor, SIGNAL (fontSizeHasChanged()),0,0 ); 
insilmaril@0
  1357
insilmaril@0
  1358
	// Erase content of editor 
insilmaril@0
  1359
	textEditor->setInactive();
insilmaril@0
  1360
insilmaril@0
  1361
	// unselect all buttons in toolbar
insilmaril@103
  1362
	standardFlagsDefault->updateToolbar();
insilmaril@0
  1363
}
insilmaril@0
  1364
insilmaril@0
  1365
QString BranchObj::getSelectString()
insilmaril@0
  1366
{
insilmaril@0
  1367
	QString s;
insilmaril@0
  1368
	if (parObj)
insilmaril@0
  1369
	{
insilmaril@95
  1370
		if (depth==1)
insilmaril@0
  1371
			s= "bo:" + QString("%1").arg(getNum());
insilmaril@0
  1372
		else	
insilmaril@0
  1373
			s= ((BranchObj*)(parObj))->getSelectString() + ",bo:" + QString("%1").arg(getNum());
insilmaril@0
  1374
	} else
insilmaril@0
  1375
		s="mc:";
insilmaril@0
  1376
	return s;
insilmaril@0
  1377
}
insilmaril@0
  1378