File: branchobj.cpp

package info (click to toggle)
vym 1.8.1-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 3,000 kB
  • ctags: 1,599
  • sloc: cpp: 14,723; sh: 373; xml: 277; perl: 89; makefile: 16
file content (1490 lines) | stat: -rw-r--r-- 30,770 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
#include "branchobj.h"
#include "texteditor.h"
#include "mapeditor.h"
#include "mainwindow.h"

extern TextEditor *textEditor;
extern Main *mainWindow;
extern FlagRowObj *standardFlagsDefault;
extern QAction *actionEditOpenURL;


/////////////////////////////////////////////////////////////////
// BranchObj
/////////////////////////////////////////////////////////////////

BranchObj* BranchObj::itLast=NULL;


BranchObj::BranchObj () :OrnamentedObj()
{
//    cout << "Const BranchObj ()\n";
    setParObj (this);	
    init();
    depth=-1;
}

BranchObj::BranchObj (QCanvas* c):OrnamentedObj (c)
{
//    cout << "Const BranchObj (c)  called from MapCenterObj (c)\n";
	parObj=NULL;
    canvas=c;
}

BranchObj::BranchObj (QCanvas* c, LinkableMapObj* p):OrnamentedObj (c)
{
//    cout << "Const BranchObj (c,p)\n";
    canvas=c;
    setParObj (p);	
    depth=p->getDepth()+1;
	if (depth==1)
		// Calc angle to mapCenter if I am a mainbranch
		// needed for reordering the mainbranches clockwise 
		// around mapcenter 
		angle=getAngle (QPoint ((int)(x() - parObj->getChildPos().x() ), 
								(int)(y() - parObj->getChildPos().y() ) ) );
    init();
}

BranchObj::~BranchObj ()
{
//	cout << "Destr BranchObj of "<<this<<endl;
	// Check, if this branch was the last child to be deleted
	// If so, unset the scrolled flags

	BranchObj *po=(BranchObj*)(parObj);
	BranchObj *bo;
	if (po)
	{
		bo=((BranchObj*)(parObj))->getLastBranch();
		if (!bo) po->unScroll();
	}
	clear();
}

bool BranchObj::operator< ( const BranchObj & other )
{
    return  angle < other.angle;
}

bool BranchObj::operator== ( const BranchObj & other )
{
    return angle == other.angle;
}

int BranchObjPtrList::compareItems ( QPtrCollection::Item i, QPtrCollection::Item j)
{
	// Make sure PtrList::find works
	if (i==j) return 0;

	if ( ((BranchObj*)(i))->angle > ((BranchObj*)(j))->angle )
		return 1;
	else
		return -1;
}

void BranchObj::init () 
{
    branch.setAutoDelete (false);
    floatimage.setAutoDelete (true);
    xlink.setAutoDelete (false);

	if (parObj)
	{
		absPos=getRandPos();
		absPos+=parObj->getChildPos();
	}

    lastSelectedBranch=-1;

    setChildObj(this);

	scrolled=false;
	tmpUnscrolled=false;

	includeImagesVer=false;
	includeImagesHor=false;
}

void BranchObj::copy (BranchObj* other)
{
    OrnamentedObj::copy(other);

	branch.clear();
    BranchObj* b;
    for (b=other->branch.first(); b;b=other->branch.next() ) 
		// Make deep copy of b
		// Because addBranch again calls copy for the childs,
		// Those will get a deep copy, too
		addBranch(b);	

	FloatImageObj *fi;
	for (fi=other->floatimage.first(); fi;fi=other->floatimage.next() )
		addFloatImage (fi);

	scrolled=other->scrolled;
	tmpUnscrolled=other->tmpUnscrolled;
	setVisibility (other->visible);

	angle=other->angle;

    positionBBox();
}

void BranchObj::clear() 
{
	floatimage.clear();
	while (!xlink.isEmpty())
		deleteXLink (xlink.first() );

	BranchObj *bo;
	while (!branch.isEmpty())
	{
		bo=branch.first();
		branch.removeFirst();
		delete (bo);
	}
}

int BranchObj::getNum()
{
	if (parObj)
		return ((BranchObj*)(parObj))->getNum ((BranchObj*)(this));
	else
		return 0;
}

int BranchObj::getNum(BranchObj *bo)
{
	// keep current pointer in branch, 
	// otherwise save might fail
	int cur=branch.at();
	int ind=branch.findRef (bo);
	branch.at(cur);
	return ind;
}

int BranchObj::getFloatImageNum(FloatImageObj *fio)
{
	return floatimage.findRef (fio);
}

int BranchObj::countBranches()
{
	return branch.count();
}

int BranchObj::countFloatImages()
{
	return floatimage.count();
}

int BranchObj::countXLinks()
{
	return xlink.count();
}

void BranchObj::setParObjTmp(LinkableMapObj* lmo, QPoint m, int off)
{
	// Temporary link to lmo
	// m is position of mouse pointer 
	// offset 0: default 1: below lmo   -1 above lmo  (if possible)


	BranchObj* o=(BranchObj*)(lmo);
	if (!parObjTmpBuf) 
		parObjTmpBuf=parObj;

	// ignore mapcenter and mainbranch
	if (lmo->getDepth()<2) off=0;
	if (off==0)
		link2ParPos=false;
	else
		link2ParPos=true;
	parObj=o;

	depth=parObj->getDepth()+1;

	// setLinkStyle calls updateLink, only set it once
	if (style!=getDefLinkStyle() ) setLinkStyle (getDefLinkStyle());

	// Move temporary to new position at destination
	// Usually the positioning would be done by reposition(),
	// but then also the destination branch would "Jump" around...
	// Better just do it approximately
	if (depth==1)
	{	// new parent is the mapcenter itself

		QPoint p= normalise ( QPoint (m.x() - o->getChildPos().x(),
									  m.y() - o->getChildPos().y() ));
		if (p.x()<0) p.setX( p.x()-bbox.width() );
		move2RelPos (p);
	} else
	{	
		int y;
		if (off==0)
		{
			// new parent is just a branch, link to it
			QRect t=o->getBBoxSizeWithChilds();
			if (o->getLastBranch())
				y=t.y() + t.height() ;
			else
				y=t.y();

		} else
		{
			if (off<0)
				// we want to link above lmo
				y=o->y() - height() + 5;
			else	
				// we want to link below lmo
				// Bottom of sel should be 5 pixels above
				// the bottom of the branch _below_ the target:
				// Don't try to find that branch, guess 12 pixels
				y=o->getChildPos().y()  -height() + 12; 
		}	
		if (o->getOrientation()==OrientLeftOfCenter)
			move ( o->getChildPos().x() - linkwidth, y );
		else	
			move (o->getChildPos().x() + linkwidth, y );
	}	

	// updateLink is called implicitly in move
	reposition();	// FIXME shouldn't be this a request?
}

void BranchObj::unsetParObjTmp()
{
	if (parObjTmpBuf) 
	{
		link2ParPos=false;
		parObj=parObjTmpBuf;
		parObjTmpBuf=NULL;
		depth=parObj->getDepth()+1;
		setLinkStyle (getDefLinkStyle() );
		updateLink();
	}		
}

void BranchObj::unScroll()
{
	if (tmpUnscrolled) resetTmpUnscroll();
	if (scrolled) toggleScroll();
}

void BranchObj::toggleScroll()
{
	BranchObj *bo;
	if (scrolled)
	{
		scrolled=false;
		systemFlags->deactivate("scrolledright");
		for (bo=branch.first(); bo; bo=branch.next() )
		{
			bo->setVisibility(true);
		}
	} else
	{
		scrolled=true;
		systemFlags->activate("scrolledright");
		for (bo=branch.first(); bo; bo=branch.next() )
		{
			bo->setVisibility(false);
		}
	}
	calcBBoxSize();
	positionBBox();	
	move (absPos.x(), absPos.y() );
	forceReposition();
}

bool BranchObj::isScrolled()
{
	return scrolled;
}

bool BranchObj::hasScrolledParent(BranchObj *start)
{
	// Calls parents recursivly to
	// find out, if we are scrolled at all.
	// But ignore myself, just look at parents.

	if (this !=start && scrolled) return true;

	BranchObj* bo=(BranchObj*)(parObj);
	if (bo) 
		return bo->hasScrolledParent(start);
	else
		return false;
}

void BranchObj::tmpUnscroll()
{
	// Unscroll parent (recursivly)
	BranchObj* bo=(BranchObj*)(parObj);
	if (bo) bo->tmpUnscroll();
		
	// Unscroll myself
	if (scrolled)
	{
		tmpUnscrolled=true;
		systemFlags->activate("tmpUnscrolledright");
		toggleScroll();
	}	
}

void BranchObj::resetTmpUnscroll()
{
	// Unscroll parent (recursivly)
	BranchObj* bo=(BranchObj*)(parObj);
	if (bo)
		bo->resetTmpUnscroll();
		
	// Unscroll myself
	if (tmpUnscrolled)
	{
		tmpUnscrolled=false;
		systemFlags->deactivate("tmpUnscrolledright");
		toggleScroll();
	}	
}

void BranchObj::setVisibility(bool v, int toDepth)
{
    if (depth <= toDepth)
    {
		frame->setVisibility(v);
		heading->setVisibility(v);
		systemFlags->setVisibility(v);
		standardFlags->setVisibility(v);
		LinkableMapObj::setVisibility (v);
		
		// Only change childs, if I am not scrolled
		if (!scrolled && (depth < toDepth))
		{
			// Now go recursivly through all childs
			BranchObj* b;
			for (b=branch.first(); b;b=branch.next() ) 
				b->setVisibility (v,toDepth);	
			FloatImageObj *fio;
			for (fio=floatimage.first(); fio; fio=floatimage.next())
				fio->setVisibility (v);
			XLinkObj* xlo;
			for (xlo=xlink.first(); xlo;xlo=xlink.next() ) 
				xlo->setVisibility ();	
		}
    } // depth <= toDepth	
	requestReposition();
}	

void BranchObj::setVisibility(bool v)
{
    setVisibility (v,MAX_DEPTH);
}


void BranchObj::setLinkColor ()
{
	// Overloaded from LinkableMapObj
	// BranchObj can use color of heading

	if (mapEditor)
		if (mapEditor->getLinkColorHint()==HeadingColor)
			LinkableMapObj::setLinkColor (heading->getColor() );
		else	
			LinkableMapObj::setLinkColor ();
}

void BranchObj::setColorChilds (QColor col)
{
	OrnamentedObj::setColor (col);
	BranchObj *bo;
	for (bo=branch.first(); bo; bo=branch.next() )
		bo->setColorChilds(col);
}

BranchObj* BranchObj::first()
{
	itLast=NULL;	
	return this; 
}
	
BranchObj* BranchObj::next()
{
	BranchObj *lmo;
	BranchObj *bo=branch.first();
	BranchObj *po=(BranchObj*)(parObj);

	if (!itLast)
	{	// We are just beginning at the mapCenter
		if (bo) 
		{
			itLast=this;
			return bo;
		}	
		else
		{
			itLast=NULL;
			return NULL;
		}	
	}

	if (itLast==parObj)
	{	// We come from above
		if (bo)
		{
			// there are childs, go there
			itLast=this;
			return bo;
		}	
		else
		{	// no childs, try to go up again
			if (po)
			{
				// go up
				itLast=this;
				lmo=po->next();
				itLast=this;
				return lmo;

			}	
			else
			{
				// can't go up, I am mapCenter
				itLast=NULL;
				return NULL;
			}	
		}
	}

	// Try to find last child, we came from, in my own childs
	bool searching=true;
	while (bo && searching)
	{
		if (itLast==bo) searching=false;
		bo=branch.next();
	}
	if (!searching)
	{	// found lastLMO in my childs
		if (bo)
		{
			// found a brother of lastLMO 
			itLast=this;
			return bo;
		}	
		else
		{
			if (po)
			{
				// go up
				itLast=this;
				lmo=po->next();
				itLast=this;
				return lmo;
			}
			else
			{
				// can't go up, I am mapCenter
				itLast=NULL;
				return NULL;
			}	
		}
	}

	// couldn't find last child, it must be a nephew of mine
	bo=branch.first();
	if (bo)
	{
		// proceed with my first child
		itLast=this;	
		return bo;
	}	
	else
	{
		// or go back to my parents
		if (po)
		{
			// go up
			itLast=this;
			lmo=po->next();
			itLast=this;
			return lmo;
		}	
		else
		{
			// can't go up, I am mapCenter
			itLast=NULL;
			return NULL;
		}	
	}	
}

BranchObj* BranchObj::getLastIterator()
{
	return itLast;
}

void BranchObj::setLastIterator(BranchObj* it)
{
	itLast=it;
}

void BranchObj::positionContents()
{
	FloatImageObj *fio;
    for (fio=floatimage.first(); fio; fio=floatimage.next() )
		fio->reposition();
	OrnamentedObj::positionContents();
}

void BranchObj::move (double x, double y)
{
	OrnamentedObj::move (x,y);
	FloatImageObj *fio;
    for (fio=floatimage.first(); fio; fio=floatimage.next() )
		fio->reposition();
    positionBBox();
}

void BranchObj::move (QPoint p)
{
	move (p.x(), p.y());
}

void BranchObj::moveBy (double x, double y)
{
	OrnamentedObj::moveBy (x,y);
    BranchObj* b;
    for (b=branch.first(); b;b=branch.next() ) 
		b->moveBy (x,y);
    positionBBox();
}
	
void BranchObj::moveBy (QPoint p)
{
	moveBy (p.x(), p.y());
}


void BranchObj::positionBBox()
{
	QPoint ap=getAbsPos();
	bbox.moveTopLeft (ap);
	positionContents();
	setSelBox();

	// set the frame
	frame->setRect(QRect(bbox.x(),bbox.y(),bbox.width(),bbox.height() ) );

	// Update links to other branches
	XLinkObj *xlo;
    for (xlo=xlink.first(); xlo; xlo=xlink.next() )
		xlo->updateXLink();
}

void BranchObj::calcBBoxSize()
{
    QSize heading_r=heading->getSize();
    int heading_w=(int) heading_r.width() ;
    int heading_h=(int) heading_r.height() ;
    QSize sysflags_r=systemFlags->getSize();
	int sysflags_h=sysflags_r.height();
	int sysflags_w=sysflags_r.width();
    QSize stanflags_r=standardFlags->getSize();
	int stanflags_h=stanflags_r.height();
	int stanflags_w=stanflags_r.width();
    int w;
    int h;

	// set width to sum of all widths
	w=heading_w + sysflags_w + stanflags_w;
	// set height to maximum needed height
	h=max (sysflags_h,stanflags_h);
	h=max (h,heading_h);

	// Save the dimension of flags and heading
	ornamentsBBox.setSize ( QSize(w,h));

	// clickBox includes Flags and Heading
    clickBox.setSize (ornamentsBBox.size() );

	// Floatimages 
	QPoint rp;
	FloatImageObj *foi;

	topPad=botPad=leftPad=rightPad=0;
	if (includeImagesVer || includeImagesHor)
	{
		if (countFloatImages()>0)
		{
			for (foi=floatimage.first(); foi; foi=floatimage.next() )
			{
				rp=foi->getRelPos();
				if (includeImagesVer)
				{
					if (rp.y() < 0) 
						topPad=max (topPad,-rp.y()-h);
					if (rp.y()+foi->height() > 0)
						botPad=max (botPad,rp.y()+foi->height());
				}		
				if (includeImagesHor)
				{
					if (orientation==OrientRightOfCenter)
					{
						if (-rp.x()-w > 0) 
							leftPad=max (leftPad,-rp.x()-w);
						if (rp.x()+foi->width() > 0)
							rightPad=max (rightPad,rp.x()+foi->width());
					} else
					{
						if (rp.x()< 0) 
							leftPad=max (leftPad,-rp.x());
						if (rp.x()+foi->width() > w)
							rightPad=max (rightPad,rp.x()+foi->width()-w);
					}
				}		
			}	
		}	
		h+=topPad+botPad;
		w+=leftPad+rightPad;
	}

	// Frame thickness
    w+=frame->getBorder();
    h+=frame->getBorder();
	
	// Finally set size
    bbox.setSize (QSize (w,h));
}

void BranchObj::setDockPos()
{
	if (getOrientation()==OrientLeftOfCenter )
    {
		childPos=QPoint (ornamentsBBox.bottomLeft().x(), ornamentsBBox.bottomLeft().y() );
		parPos=QPoint (ornamentsBBox.bottomRight().x(),ornamentsBBox.bottomRight().y() );
    } else
    {
		childPos=QPoint (ornamentsBBox.bottomRight().x(), ornamentsBBox.bottomRight().y() );
		parPos=QPoint (ornamentsBBox.bottomLeft().x(),ornamentsBBox.bottomLeft().y() );
    }
}
LinkableMapObj* BranchObj::findMapObj(QPoint p, LinkableMapObj* excludeLMO)
{
	// Search branches
    BranchObj *b;
    LinkableMapObj *lmo;
    for (b=branch.first(); b; b=branch.next() )
    {	
		lmo=b->findMapObj(p, excludeLMO);
		if (lmo != NULL) return lmo;
    }
	
	// Search myself
    if (inBox (p) && (this != excludeLMO) && isVisibleObj() ) 
		return this;

	// Search float images
	FloatImageObj *foi;
    for (foi=floatimage.first(); foi; foi=floatimage.next() )
		if (foi->inBox(p) && 
			(foi != excludeLMO) && 
			foi->getParObj()!= excludeLMO &&
			foi->isVisibleObj() 
		) return foi;

    return NULL;
}

void BranchObj::setHeading(QString s)
{
    heading->setText(s);	// set new heading
	calcBBoxSize();			// recalculate bbox
    positionBBox();			// rearrange contents
	requestReposition();
}

void BranchObj::setHideTmp (HideTmpMode mode)
{
	if (mode==HideExport && hasHiddenExportParent(this))
	{
		setVisibility (false);
		hidden=true;
	}else
	{
		if (hasScrolledParent(this))
			setVisibility (false);
		else
			setVisibility (true);
		hidden=false;
	}	

    BranchObj *bo;
    for (bo=branch.first(); bo; bo=branch.next() )
		bo->setHideTmp (mode);
}

bool BranchObj::hasHiddenExportParent(BranchObj *start)
{
	// Calls parents recursivly to
	// find out, if we are temp. hidden

	if (hideExport) return true;

	BranchObj* bo=(BranchObj*)(parObj);
	if (bo) 
		return bo->hasHiddenExportParent(start);
	else
		return false;
}

QString BranchObj::saveToDir (const QString &tmpdir,const QString &prefix, const QPoint& offset)
{
	if (hidden) return "";

    QString s,a;
	QString scrolledAttr;
	if (scrolled) 
		scrolledAttr=attribut ("scrolled","yes");
	else
		scrolledAttr="";

	QString frameAttr;
	if (frame->getFrameType()!=NoFrame)
		frameAttr=attribut ("frameType",frame->getFrameTypeName());
	else
		frameAttr="";

	// save area, if not scrolled
	QString areaAttr;
	if (!((BranchObj*)(parObj))->isScrolled() )
	{
		areaAttr=
			attribut("x1",QString().setNum(absPos.x()-offset.x(),10)) +
			attribut("y1",QString().setNum(absPos.y()-offset.y(),10)) +
			attribut("x2",QString().setNum(absPos.x()+width()-offset.x(),10)) +
			attribut("y2",QString().setNum(absPos.y()+height()-offset.y(),10));

	} else
		areaAttr="";
	
	// Providing an ID for a branch makes export to XHTML easier
	QString idAttr;
	if (countXLinks()>0)
		idAttr=attribut ("id",getSelectString());
	else
		idAttr="";

    s=beginElement ("branch" 
		+getOrnAttr() 
		+scrolledAttr 
		+frameAttr 
		+areaAttr 
		+idAttr 
		+getIncludeImageAttr() );
    incIndent();

	// save heading
    s+=valueElement("heading", getHeading(),
		attribut ("textColor",QColor(heading->getColor()).name()));

	// save names of flags set
	s+=standardFlags->saveToDir(tmpdir,prefix,0);
	
	// save note
	if (!note.isEmpty() )
		s+=note.saveToDir();
	
	// Save branches
    BranchObj *bo;
    for (bo=branch.first(); bo; bo=branch.next() )
		s+=bo->saveToDir(tmpdir,prefix,offset);

	// Save FloatImages
	FloatImageObj *fio;
	for (fio=floatimage.first(); fio; fio=floatimage.next() )
		s+=fio->saveToDir (tmpdir,prefix,offset);

	// Save XLinks
	XLinkObj *xlo;
	//FIXME exponential increase in xlinks...
	QString ol;	// old link
	QString cl;	// current link
    for (xlo=xlink.first(); xlo; xlo=xlink.next() )
	{
		cl=xlo->saveToDir();
		if (cl!=ol)
		{
			s+=cl;
			ol=cl;
		} else
		{
			qWarning (QString("Ignoring of duplicate xLink in %1").arg(getHeading()));
		}
	}	

    decIndent();
    s+=endElement   ("branch");
    return s;
}

void BranchObj::addXLink (XLinkObj *xlo)
{
	xlink.append (xlo);
	
}

void BranchObj::removeXLinkRef (XLinkObj *xlo)
{
	xlink.remove (xlo);
}

void BranchObj::deleteXLink(XLinkObj *xlo)
{
	xlo->deactivate();
	if (!xlo->isUsed()) delete (xlo);
}

void BranchObj::deleteXLinkAt (int i)
{
	XLinkObj *xlo=xlink.at(i);
	xlo->deactivate();
	if (!xlo->isUsed()) delete(xlo);
}

XLinkObj* BranchObj::XLinkAt (int i)
{
	return xlink.at(i);
}

int BranchObj::countXLink()
{
	return xlink.count();
}


BranchObj* BranchObj::XLinkTargetAt (int i)
{
	if (xlink.at(i))
		return xlink.at(i)->otherBranch (this);
	else
		return NULL;
}

void BranchObj::setIncludeImagesVer(bool b)
{
	includeImagesVer=b;
	calcBBoxSize();
	positionBBox();
	requestReposition();
	//FIXME undo needed
}

bool BranchObj::getIncludeImagesVer()
{
	return includeImagesVer;
}

void BranchObj::setIncludeImagesHor(bool b)
{
	includeImagesHor=b;
	calcBBoxSize();
	positionBBox();
	requestReposition();
	//FIXME undo needed
}

bool BranchObj::getIncludeImagesHor()
{
	return includeImagesHor;
}

QString BranchObj::getIncludeImageAttr()
{
	QString a;
	if (includeImagesVer)
		a=attribut ("incImgV","true");
	else
		a=attribut ("incImgV","false");
	if (includeImagesHor)
		a+=attribut ("incImgH","true");
	else
		a+=attribut ("incImgH","false");
	return a;	
}

LinkableMapObj* BranchObj::addFloatImage ()
{
	FloatImageObj *newfi=new FloatImageObj (canvas,this);
	floatimage.append (newfi);
	if (hasScrolledParent(this) )
		newfi->setVisibility (false);
	else	
		newfi->setVisibility(visible);
	calcBBoxSize();
	positionBBox();
	requestReposition();
	return newfi;
	//FIXME undo needed
}

LinkableMapObj* BranchObj::addFloatImage (FloatImageObj *fio)
{
	FloatImageObj *newfi=new FloatImageObj (canvas,this);
	floatimage.append (newfi);
	newfi->copy (fio);
	if (hasScrolledParent(this) )
		newfi->setVisibility (false);
	else	
		newfi->setVisibility(visible);
	calcBBoxSize();
	positionBBox();
	requestReposition();
	return newfi;
	// FIMXE undo needed
}

FloatImageObj* BranchObj::getFirstFloatImage ()
{
    return floatimage.first();
}

FloatImageObj* BranchObj::getLastFloatImage ()
{
    return floatimage.last();
}

FloatImageObj* BranchObj::getFloatImageNum (const uint &i)
{
    return floatimage.at(i);
}

void BranchObj::removeFloatImage (FloatImageObj *fio)
{
	floatimage.remove (fio);
	calcBBoxSize();
	positionBBox();
	requestReposition();
	// FIMXE undo needed
}

void BranchObj::savePosInAngle ()
{
	// Save position in angle
    BranchObj *b;
	int i=0;
    for (b=branch.first(); b; b=branch.next() )
	{
		b->angle=i;
		i++;
	}
}

void BranchObj::setDefAttr (BranchModification mod)
{
	int fontsize;
	switch (depth)
	{
		case 0: fontsize=16; break;
		case 1: fontsize=12; break;
		default: fontsize=10; break;
	}	

	setLinkColor ();
	setLinkStyle(getDefLinkStyle());
	QFont font("Sans Serif,8,-1,5,50,0,0,0,0,0");
	font.setPointSize(fontsize);
	heading->setFont(font );

	if (mod==NewBranch)
		setColor (((BranchObj*)(parObj))->getColor());
	
	calcBBoxSize();
}

BranchObj* BranchObj::addBranch()
{
    BranchObj* newbo=new BranchObj(canvas,this);
    branch.append (newbo);
    newbo->setParObj(this);
	newbo->setDefAttr(NewBranch);
    newbo->setHeading ("new");
	if (scrolled)
		newbo->setVisibility (false);
	else	
		newbo->setVisibility(visible);
	newbo->updateLink();	
	requestReposition();
	return newbo;
}

BranchObj* BranchObj::addBranch(BranchObj* bo)
{
    BranchObj* newbo=new BranchObj(canvas,this);
    branch.append (newbo);
    newbo->copy(bo);
    newbo->setParObj(this);
	newbo->setDefAttr(MovedBranch);
	if (scrolled)
		newbo->setVisibility (false);
	else	
		newbo->setVisibility(bo->visible);
	newbo->updateLink();	
	requestReposition();
	return newbo;
}

BranchObj* BranchObj::addBranchPtr(BranchObj* bo)
{
	branch.append (bo);
	bo->setParObj (this);
	bo->depth=depth+1;
	bo->setDefAttr(MovedBranch);
	if (scrolled) tmpUnscroll();
	setLastSelectedBranch (bo);
	return bo;
}

BranchObj* BranchObj::insertBranch(int pos)
{
	savePosInAngle();
	// Add new bo and resort branches
	BranchObj *newbo=addBranch ();
	newbo->angle=pos-0.5;
	branch.sort();
	return newbo;
}

BranchObj* BranchObj::insertBranch(BranchObj* bo, int pos)
{
	savePosInAngle();
	// Add new bo and resort branches
	bo->angle=pos-0.5;
	BranchObj *newbo=addBranch (bo);
	branch.sort();
	return newbo;
}

BranchObj* BranchObj::insertBranchPtr (BranchObj* bo, int pos)
{
	savePosInAngle();
	// Add new bo and resort branches
	bo->angle=pos-0.5;
	branch.append (bo);
	bo->setParObj (this);
	bo->depth=depth+1;
	bo->setDefAttr (MovedBranch);
	if (scrolled) tmpUnscroll();
	setLastSelectedBranch (bo);
	branch.sort();
	return bo;
}

void BranchObj::removeBranchHere(BranchObj* borem)
{
	// This removes the branch bo from list, but 
	// inserts its childs at the place of bo
	BranchObj *bo;
	bo=borem->getLastBranch();
	int pos=borem->getNum();
	while (bo)
	{
		bo->moveBranchTo (this,pos+1);
		bo=borem->getLastBranch();
	}	
	removeBranch (borem);
}

void BranchObj::removeChilds()
{
	clear();
}

void BranchObj::removeBranch(BranchObj* bo)
{
    // if bo is not in branch remove returns false, we
    // don't care...
	
    if (branch.remove (bo))
		delete (bo);
	else
		qWarning ("BranchObj::removeBranch tried to remove non existing branch?!\n");
	requestReposition();
}

void BranchObj::removeBranchPtr(BranchObj* bo)
{
	branch.remove (bo);
	requestReposition();
}

void BranchObj::setLastSelectedBranch (BranchObj* bo)
{
    lastSelectedBranch=branch.find(bo);
}

BranchObj* BranchObj::getLastSelectedBranch ()
{
    if (lastSelectedBranch>=0) 
	{
		BranchObj* bo=branch.at(lastSelectedBranch);
		if (bo) return bo;
    }	
    return branch.first();
}

BranchObj* BranchObj::getFirstBranch ()
{
    return branch.first();
}

BranchObj* BranchObj::getLastBranch ()
{
    return branch.last();
}

BranchObj* BranchObj::getBranchNum (const uint &i)
{
    return branch.at(i);
}

bool BranchObj::canMoveBranchUp() 
{
	if (!parObj) return false;
	BranchObj* par=(BranchObj*)parObj;
	if (this==par->getFirstBranch())
		return false;
	else
		return true;
}

BranchObj* BranchObj::moveBranchUp(BranchObj* bo1) // move a branch up (modify myself)
{
	savePosInAngle();
    int i=branch.find(bo1);
    if (i>0) 
	{	// -1 if bo1 not found 
		branch.at(i)->angle--;
		branch.at(i-1)->angle++;
		branch.sort();
		return branch.at(i-1);
	} else
		return branch.at(i);
}

bool BranchObj::canMoveBranchDown() 
{
	if (!parObj) return false;
	BranchObj* par=(BranchObj*)parObj;
	if (this==par->getLastBranch())
		return false;
	else
		return true;
}

BranchObj* BranchObj::moveBranchDown(BranchObj* bo1)
{
	savePosInAngle();
    int i=branch.find(bo1);
	int j;
	if (branch.next())
	{
		j = branch.at();
		branch.at(i)->angle++;
		branch.at(j)->angle--;
		branch.sort();
		return branch.at(j);
	} else
		return branch.at(i);
}

BranchObj* BranchObj::moveBranchTo (BranchObj* dst, int pos)
{
	// Find current parent and 
	// remove pointer to myself there
	if (!dst) return NULL;
	BranchObj *par=(BranchObj*)(parObj);
	if (par)
		par->removeBranchPtr (this);
	else
		return NULL;

	// Create new pointer to myself at dst
	if (pos<0||dst->getDepth()==0)
	{	
		// links myself as last branch at dst
		dst->addBranchPtr (this);
		updateLink();
		return this;
	} else
	{
		// inserts me at pos in parent of dst
		if (par)
		{
			BranchObj *bo=dst->insertBranchPtr (this,pos);
			bo->setDefAttr(MovedBranch);
			updateLink();
			return bo;

		} else
			return NULL;
	}	
}

void BranchObj::alignRelativeTo (QPoint ref)
{
	int th = bboxTotal.height();	
// TODO testing
/*
	cout << "BO::alignRelTo "<<getHeading()<<endl;
	cout << "  d="<<depth<<
		"  ref="<<ref<<
//		"  bbox.topLeft="<<bboxTotal.topLeft()<<
		"  absPos="<<absPos<<
//		"  pad="<<topPad<<","<<botPad<<","<<leftPad<<","<<rightPad<<
		"  hidden="<<hidden<<
		"  th="<<th<<endl;
*/

	// If I am the mapcenter or a mainbranch, reposition heading
	if (depth<2)
	{	//FIXME ugly! optimize this   move for MCO needed to initially position text in box...
		if (depth==1)
			// Calc angle to mapCenter if I am a mainbranch
			// needed for reordering the mainbranches clockwise 
			// around mapcenter 
			angle=getAngle (QPoint ((int)(x() - parObj->getChildPos().x() ), 
									(int)(y() - parObj->getChildPos().y() ) ) );
	} 
	else
    {
		// Align myself depending on orientation and parent, but
		// only if I am not the mainbranch or mapcenter itself
		switch (orientation) 
		{
			case OrientLeftOfCenter:
				move (ref.x() - bbox.width(), ref.y() + (th-bbox.height())/2 );
			break;
			case OrientRightOfCenter:	
				move (ref.x() , ref.y() + (th-bbox.height())/2  );
			break;
			default:
				qWarning ("LMO::alignRelativeTo: oops, no orientation given...");
			break;
		}		
    }		

	if (scrolled) return;

    // Set reference point for alignment of childs
    QPoint ref2;
    if (orientation==OrientLeftOfCenter)
		ref2.setX(bbox.topLeft().x() - linkwidth);
    else	
		ref2.setX(bbox.topRight().x() + linkwidth);

	if (depth==1)
		ref2.setY(absPos.y()-(bboxTotal.height()-bbox.height())/2);
	else	
		ref2.setY(ref.y() );	

    // Align the childs depending on reference point 
    BranchObj *b;
    for (b=branch.first(); b; b=branch.next() )
    {	
		if (!b->isHidden())
		{
			b->alignRelativeTo (ref2);
			ref2.setY(ref2.y() + b->getBBoxSizeWithChilds().height() );
		}
    }
}


void BranchObj::reposition()
{	
/* TODO testing only
	if (!getHeading().isEmpty())
		cout << "BO::reposition  "<<getHeading()<<endl;
	else	
		cout << "BO::reposition  ???"<<endl;
*/		
	if (depth==0)
	{
		// only calculate the sizes once. If the deepest LMO 
		// changes its height,
		// all upper LMOs have to change, too.
		calcBBoxSizeWithChilds();
		updateLink();	// This update is needed if the canvas is resized 
						// due to excessive moving of a FIO

	    alignRelativeTo ( QPoint (absPos.x(),
			absPos.y()-(bboxTotal.height()-bbox.height())/2) );
		branch.sort();	
		positionBBox();	// Reposition bbox and contents
	} else
	{
		// This is only important for moving branches:
		// For editing a branch it isn't called...
	    alignRelativeTo ( QPoint (absPos.x(),
							absPos.y()-(bboxTotal.height()-bbox.height())/2) );
	}
}


QRect BranchObj::getTotalBBox()
{
	QRect r=bbox;

	if (scrolled) return r;

	BranchObj* b;
	for (b=branch.first();b ;b=branch.next() )
		if (!b->isHidden())
			r=addBBox(b->getTotalBBox(),r);

	FloatImageObj* fio;
	for (fio=floatimage.first();fio ;fio=floatimage.next() )
		if (!fio->isHidden())
			r=addBBox(fio->getTotalBBox(),r);
		
	return r;
}

QRect BranchObj::getBBoxSizeWithChilds()
{
	return bboxTotal;
}

void BranchObj::calcBBoxSizeWithChilds()
{	
	// This is initially called only from reposition and
	// and only for mapcenter. So it won't be
	// called more than once for a single user 
	// action
	

	// Calculate size of LMO including all childs (to align them later)
	bboxTotal.setX(bbox.x() );
	bboxTotal.setY(bbox.y() );

	// if branch is scrolled, ignore childs, but still consider floatimages
	if (scrolled)
	{
		bboxTotal.setWidth (bbox.width());
		bboxTotal.setHeight(bbox.height());
		return;
	}
	
	if (hidden)
	{
		bboxTotal.setWidth (0);
		bboxTotal.setHeight(0);
		if (parObj)
		{
			bboxTotal.setX (parObj->x());
			bboxTotal.setY (parObj->y());
		} else
		{
			bboxTotal.setX (bbox.x());
			bboxTotal.setY (bbox.y());
		}
		return;
	}
	
	QRect r(0,0,0,0);
	QRect br;
	// Now calculate recursivly
	// sum of heights 
	// maximum of widths 
	// minimum of y
	BranchObj* b;
	for (b=branch.first();b ;b=branch.next() )
	{
		if (!b->isHidden())
		{
			b->calcBBoxSizeWithChilds();
			br=b->getBBoxSizeWithChilds();
			r.setWidth( max (br.width(), r.width() ));
			r.setHeight(br.height() + r.height() );
			if (br.y()<bboxTotal.y()) bboxTotal.setY(br.y());
		}
	}
	// Add myself and also
	// add width of link to sum if necessary
	if (branch.isEmpty())
		bboxTotal.setWidth (bbox.width() + r.width() );
	else	
		bboxTotal.setWidth (bbox.width() + r.width() + linkwidth);
	
	bboxTotal.setHeight(max (r.height(),  bbox.height()));
}

void BranchObj::select()
{
	// set Text in Editor	
	textEditor->setText(note.getNote() );
	QString fnh=note.getFilenameHint();
	if (fnh!="")
		textEditor->setFilenameHint(note.getFilenameHint() );
	else	
		textEditor->setFilenameHint(getHeading() );
	textEditor->setFontHint (note.getFontHint() );

    LinkableMapObj::select();
	// Tell parent that I am selected now:
	BranchObj* po=(BranchObj*)(parObj);
    if (po)	// TODO	    Try to get rid of this cast...
        po->setLastSelectedBranch(this);
		
	// temporary unscroll, if we have scrolled parents somewhere
	if (parObj) ((BranchObj*)(parObj))->tmpUnscroll();

	// Show URL and link in statusbar
	QString status;
	if (!url.isEmpty()) status+="URL: "+url+"  ";
	if (!vymLink.isEmpty()) status+="Link: "+vymLink;
	if (!status.isEmpty()) mainWindow->statusMessage (status);

	// Update Toolbar
	standardFlags->updateToolbar();

	// Update actions in mapeditor
	mapEditor->updateActions();
}

void BranchObj::unselect()
{
	LinkableMapObj::unselect();
	// Delete any messages like vymLink in StatusBar
	mainWindow->statusMessage ("");

	// save note from editor and set flag
	// text is done by updateNoteFlag(), just save
	// filename here
	note.setFilenameHint (textEditor->getFilename());

	// reset temporary unscroll, if we have scrolled parents somewhere
	if (parObj) ((BranchObj*)(parObj))->resetTmpUnscroll();

	// Erase content of editor 
	textEditor->setInactive();

	// unselect all buttons in toolbar
	standardFlagsDefault->updateToolbar();
}

QString BranchObj::getSelectString()
{
	QString s;
	if (parObj)
	{
		if (depth==1)
			s= "bo:" + QString("%1").arg(getNum());
		else	
			s= ((BranchObj*)(parObj))->getSelectString() + ",bo:" + QString("%1").arg(getNum());
	} else
		s="mc:";
	return s;
}