linkablemapobj.cpp
author insilmaril
Tue, 24 Jan 2006 15:09:49 +0000
changeset 193 544a64d7cbac
parent 175 728f51b71e71
child 218 160459d924a1
permissions -rw-r--r--
Introduced basic export to Open Document format
insilmaril@139
     1
#include <math.h>
insilmaril@139
     2
insilmaril@0
     3
#include "linkablemapobj.h"
insilmaril@0
     4
#include "branchobj.h"
insilmaril@0
     5
#include "mapeditor.h"
insilmaril@0
     6
insilmaril@0
     7
#include "version.h"
insilmaril@0
     8
insilmaril@0
     9
insilmaril@0
    10
/////////////////////////////////////////////////////////////////
insilmaril@0
    11
// LinkableMapObj
insilmaril@0
    12
/////////////////////////////////////////////////////////////////
insilmaril@0
    13
insilmaril@0
    14
LinkableMapObj::LinkableMapObj():MapObj()
insilmaril@0
    15
{
insilmaril@0
    16
  //  cout << "Const LinkableMapObj ()\n";
insilmaril@0
    17
    init ();
insilmaril@0
    18
}
insilmaril@0
    19
insilmaril@0
    20
LinkableMapObj::LinkableMapObj(QCanvas* c) :MapObj(c)
insilmaril@0
    21
{
insilmaril@0
    22
//    cout << "Const LinkableMapObj\n";
insilmaril@0
    23
    init ();
insilmaril@0
    24
}
insilmaril@0
    25
insilmaril@0
    26
LinkableMapObj::LinkableMapObj (LinkableMapObj* lmo) : MapObj (lmo->canvas)
insilmaril@0
    27
{
insilmaril@0
    28
    copy (lmo);
insilmaril@0
    29
}
insilmaril@0
    30
insilmaril@0
    31
LinkableMapObj::~LinkableMapObj()
insilmaril@0
    32
{
insilmaril@0
    33
    delete (bottomline);
insilmaril@0
    34
    delete (selbox);
insilmaril@0
    35
	delete (frame);
insilmaril@0
    36
	delLink();
insilmaril@0
    37
}
insilmaril@0
    38
insilmaril@0
    39
void LinkableMapObj::delLink()
insilmaril@0
    40
{
insilmaril@0
    41
	switch (style)
insilmaril@0
    42
	{
insilmaril@0
    43
		case StyleLine:
insilmaril@0
    44
			delete (l);
insilmaril@0
    45
			break;
insilmaril@0
    46
		case StyleParabel:
insilmaril@0
    47
			segment.clear();
insilmaril@0
    48
			break;
insilmaril@0
    49
		case StylePolyLine:
insilmaril@0
    50
			delete (p);
insilmaril@0
    51
			delete (l);
insilmaril@0
    52
			break;
insilmaril@0
    53
		case StylePolyParabel:
insilmaril@0
    54
			delete (p);
insilmaril@0
    55
			segment.clear();
insilmaril@0
    56
			break;
insilmaril@0
    57
		default:
insilmaril@0
    58
			break;
insilmaril@0
    59
	}		
insilmaril@0
    60
}
insilmaril@0
    61
insilmaril@0
    62
void LinkableMapObj::init ()
insilmaril@0
    63
{
insilmaril@0
    64
    depth=-1;	
insilmaril@164
    65
	mapEditor=NULL;
insilmaril@0
    66
    childObj=NULL;
insilmaril@0
    67
    parObj=NULL;
insilmaril@0
    68
    parObjTmpBuf=NULL;
insilmaril@0
    69
    parPos=QPoint(0,0);
insilmaril@0
    70
    childPos=QPoint(0,0);
insilmaril@0
    71
	link2ParPos=false;
insilmaril@0
    72
    l=NULL;
insilmaril@0
    73
    orientation=OrientUndef;
insilmaril@0
    74
    linkwidth=20;		
insilmaril@0
    75
	thickness_start=8;
insilmaril@0
    76
    style=StyleUndef;
insilmaril@0
    77
	linkpos=LinkBottom;
insilmaril@0
    78
    segment.setAutoDelete (TRUE);
insilmaril@0
    79
    arcsegs=13;
insilmaril@0
    80
	QPointArray pa(arcsegs*2+2);
insilmaril@0
    81
    
insilmaril@0
    82
    bottomline=new QCanvasLine(canvas);
insilmaril@0
    83
    bottomline->setPen( QPen(linkcolor, 1) );
insilmaril@0
    84
    bottomline->setZ(Z_LINK);
insilmaril@0
    85
    bottomline->show();
insilmaril@0
    86
insilmaril@0
    87
    // Prepare showing the selection of a MapObj
insilmaril@0
    88
    selbox = new QCanvasRectangle (canvas);
insilmaril@0
    89
    selbox->setZ(Z_SELBOX);
insilmaril@0
    90
    selbox->setBrush( QColor(255,255,0) );
insilmaril@0
    91
    selbox->setPen( QPen(QColor(255,255,0) ));
insilmaril@0
    92
    selbox->hide();
insilmaril@0
    93
    selected=false;
insilmaril@0
    94
insilmaril@160
    95
	hideLinkUnselected=false;
insilmaril@160
    96
insilmaril@175
    97
	topPad=botPad=leftPad=rightPad=0;
insilmaril@175
    98
insilmaril@0
    99
	// initialize frame
insilmaril@0
   100
	frame = new FrameObj (canvas);
insilmaril@0
   101
	
insilmaril@0
   102
	repositionRequest=false;
insilmaril@0
   103
}
insilmaril@0
   104
insilmaril@0
   105
void LinkableMapObj::copy (LinkableMapObj* other)
insilmaril@0
   106
{
insilmaril@0
   107
    MapObj::copy(other);
insilmaril@0
   108
	bboxTotal=other->bboxTotal;
insilmaril@0
   109
    setLinkStyle(other->style);
insilmaril@0
   110
    setLinkColor (other->linkcolor);
insilmaril@0
   111
}
insilmaril@0
   112
insilmaril@0
   113
void LinkableMapObj::setChildObj(LinkableMapObj* o)
insilmaril@0
   114
{
insilmaril@0
   115
    childObj=o;
insilmaril@0
   116
}
insilmaril@0
   117
insilmaril@0
   118
void LinkableMapObj::setParObj(LinkableMapObj* o)
insilmaril@0
   119
{
insilmaril@0
   120
    parObj=o;
insilmaril@0
   121
	mapEditor=parObj->getMapEditor();
insilmaril@0
   122
}
insilmaril@0
   123
insilmaril@0
   124
void LinkableMapObj::setParObjTmp(LinkableMapObj*,QPoint,int)
insilmaril@0
   125
{
insilmaril@0
   126
}
insilmaril@0
   127
insilmaril@0
   128
void LinkableMapObj::unsetParObjTmp()
insilmaril@0
   129
{
insilmaril@0
   130
}
insilmaril@0
   131
insilmaril@164
   132
bool LinkableMapObj::hasParObjTmp()
insilmaril@164
   133
{
insilmaril@164
   134
	if (parObjTmpBuf) return true;
insilmaril@164
   135
	return false;
insilmaril@164
   136
}
insilmaril@164
   137
insilmaril@175
   138
int LinkableMapObj::getTopPad()
insilmaril@175
   139
{
insilmaril@175
   140
	return topPad;
insilmaril@175
   141
}
insilmaril@175
   142
insilmaril@175
   143
int LinkableMapObj::getLeftPad()
insilmaril@175
   144
{
insilmaril@175
   145
	return leftPad;
insilmaril@175
   146
}
insilmaril@175
   147
insilmaril@175
   148
int LinkableMapObj::getRightPad()
insilmaril@175
   149
{
insilmaril@175
   150
	return rightPad;
insilmaril@175
   151
}
insilmaril@175
   152
insilmaril@0
   153
LinkStyle LinkableMapObj::getDefLinkStyle ()
insilmaril@0
   154
{
insilmaril@164
   155
	if (!mapEditor) return StyleUndef;
insilmaril@164
   156
insilmaril@0
   157
	LinkStyle ls=mapEditor->getLinkStyle();
insilmaril@0
   158
	switch (ls)
insilmaril@0
   159
	{
insilmaril@0
   160
		case StyleLine: 
insilmaril@0
   161
			return ls;
insilmaril@0
   162
			break;
insilmaril@0
   163
		case StyleParabel:
insilmaril@0
   164
			return ls;
insilmaril@0
   165
			break;
insilmaril@0
   166
		case StylePolyLine:	
insilmaril@0
   167
			if (depth>1)
insilmaril@0
   168
				return StyleLine;
insilmaril@0
   169
			else	
insilmaril@0
   170
				return ls;
insilmaril@0
   171
			break;
insilmaril@0
   172
		case StylePolyParabel:	
insilmaril@0
   173
			if (depth>1)
insilmaril@0
   174
				return StyleParabel;
insilmaril@0
   175
			else	
insilmaril@0
   176
				return ls;
insilmaril@0
   177
			break;
insilmaril@0
   178
		default: 
insilmaril@0
   179
			break;	
insilmaril@0
   180
	}	
insilmaril@0
   181
	return StyleUndef;
insilmaril@0
   182
}
insilmaril@0
   183
insilmaril@0
   184
void LinkableMapObj::setLinkStyle(LinkStyle newstyle)
insilmaril@0
   185
{
insilmaril@83
   186
	//if (newstyle=style) return;
insilmaril@0
   187
	delLink();
insilmaril@0
   188
		
insilmaril@0
   189
	style=newstyle;
insilmaril@0
   190
insilmaril@0
   191
    if (childObj!=NULL && parObj != NULL)
insilmaril@0
   192
    {
insilmaril@0
   193
		int i;
insilmaril@0
   194
		QCanvasLine* cl;
insilmaril@0
   195
		switch (style)
insilmaril@0
   196
		{
insilmaril@0
   197
			case StyleUndef:
insilmaril@0
   198
				bottomline->hide();
insilmaril@0
   199
				break;
insilmaril@0
   200
			case StyleLine: 
insilmaril@0
   201
				l = new QCanvasLine(canvas);
insilmaril@0
   202
				l->setPen( QPen(linkcolor, 1) );
insilmaril@0
   203
				l->setZ(Z_LINK);
insilmaril@0
   204
				if (visible)
insilmaril@0
   205
					l->show();
insilmaril@0
   206
				else
insilmaril@0
   207
					l->hide();
insilmaril@0
   208
				break;
insilmaril@0
   209
			case StyleParabel:
insilmaril@0
   210
				for (i=0;i<arcsegs;i++)
insilmaril@0
   211
				{
insilmaril@0
   212
					cl = new QCanvasLine(canvas);
insilmaril@0
   213
					cl->setPen( QPen(linkcolor, 1) );
insilmaril@0
   214
					cl->setPoints( 0,0,i*10,100);
insilmaril@0
   215
					cl->setZ(Z_LINK);
insilmaril@0
   216
					if (visible)
insilmaril@0
   217
						cl->show();
insilmaril@0
   218
					else
insilmaril@0
   219
						cl->hide();
insilmaril@0
   220
					segment.append(cl);
insilmaril@0
   221
				}
insilmaril@0
   222
				pa0.resize (arcsegs+1);
insilmaril@0
   223
				break;
insilmaril@0
   224
			case StylePolyLine:	
insilmaril@0
   225
				p = new QCanvasPolygon(canvas);
insilmaril@0
   226
				p->setBrush( linkcolor );
insilmaril@0
   227
				p->setZ(Z_LINK);
insilmaril@0
   228
				if (visible)
insilmaril@0
   229
					p->show();
insilmaril@0
   230
				else
insilmaril@0
   231
					p->hide();
insilmaril@0
   232
				pa0.resize (3);
insilmaril@164
   233
				// TODO a bit awkward: draw the lines additionally to polygon, to avoid
insilmaril@0
   234
				// missing pixels, when polygon is extremly flat
insilmaril@0
   235
				l = new QCanvasLine(canvas);
insilmaril@0
   236
				l->setPen( QPen(linkcolor, 1) );
insilmaril@0
   237
				l->setZ(Z_LINK);
insilmaril@0
   238
				if (visible)
insilmaril@0
   239
					l->show();
insilmaril@0
   240
				else
insilmaril@0
   241
					l->hide();
insilmaril@0
   242
				break;
insilmaril@0
   243
			case StylePolyParabel:	
insilmaril@0
   244
				p = new QCanvasPolygon(canvas);
insilmaril@0
   245
				p->setBrush( linkcolor );
insilmaril@0
   246
				p->setZ(Z_LINK);
insilmaril@0
   247
				if (visible)
insilmaril@0
   248
					p->show();
insilmaril@0
   249
				else
insilmaril@0
   250
					p->hide();
insilmaril@0
   251
				pa0.resize (arcsegs*2+2);
insilmaril@0
   252
				pa1.resize (arcsegs+1);
insilmaril@0
   253
				pa2.resize (arcsegs+1);
insilmaril@0
   254
insilmaril@164
   255
				// TODO a bit awkward: draw the lines additionally 
insilmaril@0
   256
				// to polygon, to avoid missing pixels, 
insilmaril@0
   257
				// if polygon is extremly flat
insilmaril@0
   258
				for (i=0;i<arcsegs;i++)
insilmaril@0
   259
				{
insilmaril@0
   260
					cl = new QCanvasLine(canvas);
insilmaril@0
   261
					cl->setPen( QPen(linkcolor, 1) );
insilmaril@0
   262
					cl->setPoints( 0,0,i*10,100);
insilmaril@0
   263
					cl->setZ(Z_LINK);
insilmaril@0
   264
					if (visible)
insilmaril@0
   265
						cl->show();
insilmaril@0
   266
					else
insilmaril@0
   267
						cl->hide();
insilmaril@0
   268
					segment.append(cl);
insilmaril@0
   269
				}
insilmaril@0
   270
				break;
insilmaril@0
   271
			default: 
insilmaril@0
   272
				break;	
insilmaril@0
   273
		}	
insilmaril@164
   274
	} 
insilmaril@0
   275
}
insilmaril@0
   276
insilmaril@0
   277
LinkStyle LinkableMapObj::getLinkStyle()
insilmaril@0
   278
{
insilmaril@0
   279
	return style;
insilmaril@0
   280
}
insilmaril@0
   281
insilmaril@160
   282
void LinkableMapObj::setHideLinkUnselected(bool b)
insilmaril@160
   283
{
insilmaril@160
   284
	hideLinkUnselected=b;
insilmaril@160
   285
	setVisibility (visible);
insilmaril@160
   286
	updateLink();
insilmaril@160
   287
}
insilmaril@160
   288
insilmaril@160
   289
bool LinkableMapObj::getHideLinkUnselected()
insilmaril@160
   290
{
insilmaril@160
   291
	return hideLinkUnselected;
insilmaril@160
   292
}
insilmaril@160
   293
insilmaril@0
   294
void LinkableMapObj::setLinkPos(LinkPos lp)
insilmaril@0
   295
{
insilmaril@0
   296
	linkpos=lp;
insilmaril@0
   297
}
insilmaril@0
   298
insilmaril@0
   299
LinkPos LinkableMapObj::getLinkPos()
insilmaril@0
   300
{
insilmaril@0
   301
	return linkpos;
insilmaril@0
   302
}
insilmaril@0
   303
insilmaril@0
   304
insilmaril@0
   305
void LinkableMapObj::setLinkColor()
insilmaril@0
   306
{
insilmaril@0
   307
	// Overloaded in BranchObj and childs
insilmaril@0
   308
	// here only set default color
insilmaril@164
   309
	if (mapEditor)
insilmaril@164
   310
		setLinkColor (mapEditor->getDefLinkColor());
insilmaril@0
   311
}
insilmaril@0
   312
insilmaril@0
   313
void LinkableMapObj::setLinkColor(QColor col)
insilmaril@0
   314
{
insilmaril@0
   315
	linkcolor=col;
insilmaril@0
   316
    bottomline->setPen( QPen(linkcolor, 1) );
insilmaril@0
   317
	QCanvasLine *cl;
insilmaril@0
   318
	switch (style)
insilmaril@0
   319
	{
insilmaril@0
   320
		case StyleLine:
insilmaril@0
   321
			l->setPen( QPen(col,1));
insilmaril@0
   322
			break;	
insilmaril@0
   323
		case StyleParabel:	
insilmaril@0
   324
			for (cl=segment.first(); cl; cl=segment.next() )
insilmaril@0
   325
				cl->setPen( QPen(col,1));
insilmaril@0
   326
			break;
insilmaril@0
   327
		case StylePolyLine:
insilmaril@0
   328
			p->setBrush( QBrush(col));
insilmaril@0
   329
			l->setPen( QPen(col,1));
insilmaril@0
   330
			break;
insilmaril@0
   331
		case StylePolyParabel:	
insilmaril@0
   332
			p->setBrush( QBrush(col));
insilmaril@0
   333
			for (cl=segment.first(); cl; cl=segment.next() )
insilmaril@0
   334
				cl->setPen( QPen(col,1));
insilmaril@0
   335
			break;
insilmaril@0
   336
		default:
insilmaril@0
   337
			break;
insilmaril@0
   338
	} // switch (style)	
insilmaril@0
   339
}
insilmaril@0
   340
insilmaril@0
   341
QColor LinkableMapObj::getLinkColor()
insilmaril@0
   342
{
insilmaril@0
   343
	return linkcolor;
insilmaril@0
   344
}
insilmaril@0
   345
insilmaril@0
   346
FrameType LinkableMapObj::getFrameType()
insilmaril@0
   347
{
insilmaril@0
   348
	return frame->getFrameType();
insilmaril@0
   349
}
insilmaril@0
   350
insilmaril@0
   351
void LinkableMapObj::setFrameType(const FrameType &t)
insilmaril@0
   352
{
insilmaril@0
   353
	frame->setFrameType(t);
insilmaril@0
   354
	calcBBoxSize();
insilmaril@0
   355
	positionBBox();
insilmaril@0
   356
	requestReposition();
insilmaril@0
   357
}
insilmaril@0
   358
insilmaril@0
   359
void LinkableMapObj::setFrameType(const QString &t)
insilmaril@0
   360
{
insilmaril@0
   361
	frame->setFrameType(t);
insilmaril@0
   362
	calcBBoxSize();
insilmaril@0
   363
	positionBBox();
insilmaril@0
   364
	requestReposition();
insilmaril@0
   365
}
insilmaril@0
   366
insilmaril@0
   367
void LinkableMapObj::setVisibility (bool v)
insilmaril@0
   368
{
insilmaril@160
   369
	QCanvasLine* cl;
insilmaril@0
   370
	MapObj::setVisibility (v);
insilmaril@160
   371
	bool visnow=visible;
insilmaril@160
   372
	if (hideLinkUnselected && !selected)
insilmaril@160
   373
		visnow=false;
insilmaril@160
   374
insilmaril@160
   375
	if (visnow) 
insilmaril@0
   376
	{
insilmaril@0
   377
		bottomline->show();
insilmaril@160
   378
		switch (style)
insilmaril@0
   379
		{
insilmaril@160
   380
			case StyleLine:
insilmaril@160
   381
				if (l) l->show();
insilmaril@160
   382
				break;
insilmaril@160
   383
			case StyleParabel:	
insilmaril@160
   384
				for (cl=segment.first(); cl; cl=segment.next() )
insilmaril@160
   385
					cl->show();
insilmaril@160
   386
				break;	
insilmaril@160
   387
			case StylePolyLine:
insilmaril@160
   388
				if (p) p->show();
insilmaril@160
   389
				if (l) l->show();
insilmaril@160
   390
				break;
insilmaril@160
   391
			case StylePolyParabel:	
insilmaril@160
   392
				for (cl=segment.first(); cl; cl=segment.next() )
insilmaril@160
   393
					cl->show();
insilmaril@160
   394
				if (p) p->show();
insilmaril@160
   395
				break;
insilmaril@160
   396
			default:
insilmaril@160
   397
				break;
insilmaril@160
   398
		}
insilmaril@0
   399
	} else 
insilmaril@0
   400
	{
insilmaril@0
   401
		bottomline->hide();
insilmaril@160
   402
		switch (style)
insilmaril@0
   403
		{
insilmaril@160
   404
			case StyleLine:
insilmaril@160
   405
				if (l) l->hide();
insilmaril@160
   406
				break;
insilmaril@160
   407
			case StyleParabel:	
insilmaril@160
   408
				for (cl=segment.first(); cl; cl=segment.next() )
insilmaril@160
   409
					cl->hide();
insilmaril@160
   410
				break;	
insilmaril@160
   411
			case StylePolyLine:
insilmaril@160
   412
				if (p) p->hide();
insilmaril@160
   413
				if (l) l->hide();
insilmaril@160
   414
				break;
insilmaril@160
   415
			case StylePolyParabel:	
insilmaril@160
   416
				for (cl=segment.first(); cl; cl=segment.next() )
insilmaril@160
   417
					cl->hide();
insilmaril@160
   418
				if (p) p->hide();
insilmaril@160
   419
				break;
insilmaril@160
   420
			default:
insilmaril@160
   421
				break;
insilmaril@160
   422
		}
insilmaril@0
   423
	}	
insilmaril@0
   424
}
insilmaril@0
   425
insilmaril@0
   426
void LinkableMapObj::updateLink()
insilmaril@0
   427
{
insilmaril@0
   428
    // needs:
insilmaril@0
   429
    //	childPos of parent
insilmaril@0
   430
    //	orient   of parent
insilmaril@0
   431
    //	style
insilmaril@0
   432
    // 
insilmaril@0
   433
    // sets:
insilmaril@0
   434
    //	orientation
insilmaril@0
   435
    //	childPos
insilmaril@0
   436
    //	parPos
insilmaril@175
   437
	//  bottomlineY
insilmaril@0
   438
    //	drawing of the link itself
insilmaril@0
   439
insilmaril@0
   440
insilmaril@0
   441
	// updateLink is called from move, but called from constructor we don't
insilmaril@0
   442
	// have parents yet...
insilmaril@0
   443
	if (style==StyleUndef) return;	
insilmaril@0
   444
insilmaril@0
   445
	if (frame->getFrameType() == NoFrame)
insilmaril@0
   446
		linkpos=LinkBottom;
insilmaril@0
   447
	else	
insilmaril@0
   448
		linkpos=LinkMiddle;
insilmaril@0
   449
	switch (linkpos)
insilmaril@0
   450
	{
insilmaril@0
   451
		case LinkMiddle:
insilmaril@175
   452
			bottomlineY=clickBox.top()+clickBox.height() /2;	// draw link to middle (of frame)
insilmaril@0
   453
			break;
insilmaril@0
   454
		default :
insilmaril@175
   455
			bottomlineY=clickBox.bottom()-1;	// draw link to bottom of bbox
insilmaril@0
   456
			break;
insilmaril@0
   457
	}
insilmaril@0
   458
	
insilmaril@0
   459
    double p2x,p2y;								// Set P2 Before setting
insilmaril@0
   460
	if (!link2ParPos)
insilmaril@0
   461
	{
insilmaril@0
   462
		p2x=QPoint( parObj->getChildPos() ).x();	// P1, we have to look at
insilmaril@0
   463
		p2y=QPoint( parObj->getChildPos() ).y();	// orientation
insilmaril@0
   464
	} else	
insilmaril@0
   465
	{
insilmaril@0
   466
		p2x=QPoint( parObj->getParPos() ).x();	
insilmaril@0
   467
		p2y=QPoint( parObj->getParPos() ).y();
insilmaril@0
   468
	} 
insilmaril@0
   469
insilmaril@0
   470
	LinkOrient orientOld=orientation;
insilmaril@0
   471
insilmaril@0
   472
    // Set orientation, first look for orientation of parent
insilmaril@0
   473
    if (parObj->getOrientation() != OrientUndef ) 
insilmaril@0
   474
		// use the orientation of the parent:
insilmaril@0
   475
		orientation=parObj->getOrientation();
insilmaril@0
   476
    else
insilmaril@0
   477
    {
insilmaril@0
   478
		// calc orientation depending on position rel to mapCenter
insilmaril@0
   479
		if (absPos.x() < QPoint(parObj->getChildPos() ).x() )
insilmaril@0
   480
			orientation=OrientLeftOfCenter; 
insilmaril@0
   481
		else
insilmaril@0
   482
			orientation=OrientRightOfCenter;
insilmaril@0
   483
    }
insilmaril@0
   484
insilmaril@0
   485
	if ((orientation!=orientOld) && (orientOld!= OrientUndef))
insilmaril@0
   486
	{
insilmaril@0
   487
		// Orientation just changed. Reorient this subbranch, because move is called
insilmaril@0
   488
		// before updateLink => Position is still the old one, which could lead to 
insilmaril@0
   489
		// linking of subranch to itself => segfault
insilmaril@0
   490
		//
insilmaril@0
   491
		// Also possible: called in BranchObj::init(), then orientOld==OrientUndef,
insilmaril@0
   492
		// no need to reposition now
insilmaril@0
   493
		reposition();
insilmaril@0
   494
	}
insilmaril@0
   495
	
insilmaril@0
   496
    if (orientation==OrientLeftOfCenter )
insilmaril@0
   497
    {
insilmaril@175
   498
		childPos=QPoint (absPos.x(),bottomlineY);
insilmaril@175
   499
		parPos=QPoint (absPos.x()+ bbox.width(),  bottomlineY );
insilmaril@0
   500
    } else
insilmaril@0
   501
    {
insilmaril@175
   502
		childPos=QPoint (absPos.x()+ bbox.width(), bottomlineY ); 
insilmaril@175
   503
		parPos=QPoint (absPos.x(),bottomlineY);
insilmaril@0
   504
    }
insilmaril@0
   505
insilmaril@0
   506
	double p1x=parPos.x();	// Link is drawn from P1 to P2
insilmaril@0
   507
	double p1y=parPos.y();
insilmaril@0
   508
insilmaril@0
   509
	double vx=p2x - p1x;	// V=P2-P1
insilmaril@0
   510
	double vy=p2y - p1y;
insilmaril@0
   511
insilmaril@0
   512
	// Draw the horizontal line below heading (from ChildPos to ParPos)
insilmaril@104
   513
	bottomline->setPoints (qRound(childPos.x()),
insilmaril@104
   514
		qRound(childPos.y()),
insilmaril@104
   515
		qRound(p1x),
insilmaril@104
   516
		qRound(p1y) );
insilmaril@0
   517
insilmaril@0
   518
	double a;	// angle
insilmaril@0
   519
	if (vx > -0.000001 && vx < 0.000001)
insilmaril@0
   520
		a=M_PI_2;
insilmaril@0
   521
	else
insilmaril@0
   522
		a=atan( vy / vx );
insilmaril@0
   523
	// "turning point" for drawing polygonal links
insilmaril@104
   524
	QPoint tp (-qRound(sin (a)*thickness_start), qRound(cos (a)*thickness_start));	
insilmaril@0
   525
	
insilmaril@0
   526
	QCanvasLine *cl;
insilmaril@0
   527
insilmaril@0
   528
	int i;
insilmaril@0
   529
insilmaril@0
   530
    // Draw the link
insilmaril@0
   531
	switch (style)
insilmaril@0
   532
	{
insilmaril@0
   533
		case StyleLine:
insilmaril@104
   534
			l->setPoints( qRound (parPos.x()),
insilmaril@104
   535
				qRound(parPos.y()),
insilmaril@104
   536
				qRound(p2x),
insilmaril@104
   537
				qRound(p2y) );
insilmaril@0
   538
			break;	
insilmaril@0
   539
		case StyleParabel:	
insilmaril@0
   540
			parabel (pa0, p1x,p1y,p2x,p2y);
insilmaril@0
   541
			i=0;
insilmaril@0
   542
			for (cl=segment.first(); cl; cl=segment.next() )
insilmaril@0
   543
			{	
insilmaril@0
   544
				cl->setPoints( pa0.point(i).x(), pa0.point(i).y(),pa0.point(i+1).x(),pa0.point(i+1).y());
insilmaril@0
   545
				i++;
insilmaril@0
   546
			}
insilmaril@0
   547
			break;
insilmaril@0
   548
		case StylePolyLine:
insilmaril@104
   549
			pa0[0]=QPoint (qRound(p2x+tp.x()), qRound(p2y+tp.y()));
insilmaril@104
   550
			pa0[1]=QPoint (qRound(p2x-tp.x()), qRound(p2y-tp.y()));
insilmaril@104
   551
			pa0[2]=QPoint (qRound (parPos.x()), qRound(parPos.y()) );
insilmaril@0
   552
			p->setPoints (pa0);
insilmaril@0
   553
			// here too, draw line to avoid missing pixels
insilmaril@104
   554
			l->setPoints( qRound (parPos.x()),
insilmaril@104
   555
				qRound(parPos.y()),
insilmaril@104
   556
				qRound(p2x),
insilmaril@104
   557
				qRound(p2y) );
insilmaril@0
   558
			break;
insilmaril@0
   559
		case StylePolyParabel:	
insilmaril@0
   560
			parabel (pa1, p1x,p1y,p2x+tp.x(),p2y+tp.y());
insilmaril@0
   561
			parabel (pa2, p1x,p1y,p2x-tp.x(),p2y-tp.y());
insilmaril@0
   562
			for (i=0;i<=arcsegs;i++)
insilmaril@0
   563
			{
insilmaril@0
   564
				// Combine the arrays to a single one
insilmaril@0
   565
				pa0[i]=pa1[i];
insilmaril@0
   566
				pa0[i+arcsegs+1]=pa2[arcsegs-i];
insilmaril@0
   567
			}	
insilmaril@0
   568
			p->setPoints (pa0);
insilmaril@0
   569
			i=0;
insilmaril@0
   570
			for (cl=segment.first(); cl; cl=segment.next() )
insilmaril@0
   571
			{	
insilmaril@0
   572
				cl->setPoints( pa1.point(i).x(), pa1.point(i).y(),pa1.point(i+1).x(),pa1.point(i+1).y());
insilmaril@0
   573
				i++;
insilmaril@0
   574
			}
insilmaril@0
   575
			break;
insilmaril@0
   576
		default:
insilmaril@0
   577
			break;
insilmaril@0
   578
	} // switch (style)	
insilmaril@0
   579
}
insilmaril@0
   580
	
insilmaril@0
   581
LinkableMapObj* LinkableMapObj::getChildObj()
insilmaril@0
   582
{
insilmaril@0
   583
    return childObj;
insilmaril@0
   584
}
insilmaril@0
   585
insilmaril@0
   586
LinkableMapObj* LinkableMapObj::getParObj()
insilmaril@0
   587
{
insilmaril@0
   588
    return parObj;
insilmaril@0
   589
}
insilmaril@0
   590
insilmaril@106
   591
LinkableMapObj* LinkableMapObj::findObjBySelect (QString s)
insilmaril@95
   592
{
insilmaril@95
   593
	LinkableMapObj *lmo=this;
insilmaril@95
   594
	QString part;
insilmaril@95
   595
	QString typ;
insilmaril@95
   596
	QString num;
insilmaril@95
   597
	while (!s.isEmpty() )
insilmaril@95
   598
	{
insilmaril@95
   599
		part=s.section(",",0,0);
insilmaril@95
   600
		typ=part.left (3);
insilmaril@95
   601
		num=part.right(part.length() - 3);
insilmaril@95
   602
		if (typ=="mc:")
insilmaril@95
   603
		{
insilmaril@95
   604
			if (depth>0)
insilmaril@95
   605
				return false;	// in a subtree there is no center
insilmaril@95
   606
			else
insilmaril@95
   607
				break;
insilmaril@95
   608
		} else
insilmaril@95
   609
			if (typ=="bo:")
insilmaril@95
   610
				lmo=((BranchObj*)(lmo))->getBranchNum (num.toUInt());
insilmaril@95
   611
			else
insilmaril@95
   612
				if (typ=="fi:")
insilmaril@95
   613
					lmo=((BranchObj*)(lmo))->getFloatImageNum (num.toUInt());
insilmaril@95
   614
		if (!lmo) break;
insilmaril@95
   615
		
insilmaril@95
   616
		if (s.contains(","))
insilmaril@95
   617
			s=s.right(s.length() - part.length() -1 );
insilmaril@95
   618
		else	
insilmaril@95
   619
			break;
insilmaril@95
   620
	}
insilmaril@95
   621
	return lmo;
insilmaril@95
   622
}
insilmaril@95
   623
insilmaril@0
   624
QPoint LinkableMapObj::getChildPos()
insilmaril@0
   625
{
insilmaril@0
   626
    return childPos;
insilmaril@0
   627
}
insilmaril@0
   628
insilmaril@0
   629
QPoint LinkableMapObj::getParPos()
insilmaril@0
   630
{
insilmaril@0
   631
    return parPos;
insilmaril@0
   632
}
insilmaril@0
   633
insilmaril@0
   634
QPoint LinkableMapObj::getRelPos()
insilmaril@0
   635
{
insilmaril@0
   636
	if (!parObj) return QPoint (0,0);
insilmaril@0
   637
    return QPoint(
insilmaril@0
   638
		absPos.x() - parObj->x(),
insilmaril@0
   639
		absPos.y() - parObj->y()
insilmaril@0
   640
	);
insilmaril@0
   641
}
insilmaril@0
   642
insilmaril@0
   643
LinkOrient LinkableMapObj::getOrientation()
insilmaril@0
   644
{
insilmaril@0
   645
    return orientation;
insilmaril@0
   646
}
insilmaril@0
   647
insilmaril@0
   648
int LinkableMapObj::getDepth()
insilmaril@0
   649
{
insilmaril@0
   650
    return depth;
insilmaril@0
   651
}
insilmaril@0
   652
insilmaril@0
   653
void LinkableMapObj::setMapEditor (MapEditor *me)
insilmaril@0
   654
{
insilmaril@0
   655
	mapEditor=me;
insilmaril@0
   656
}
insilmaril@0
   657
insilmaril@0
   658
MapEditor* LinkableMapObj::getMapEditor ()
insilmaril@0
   659
{
insilmaril@0
   660
	return mapEditor;
insilmaril@0
   661
}
insilmaril@0
   662
insilmaril@0
   663
QPoint LinkableMapObj::getRandPos()
insilmaril@0
   664
{
insilmaril@0
   665
	// Choose a random position with given distance to parent:
insilmaril@0
   666
	double a=rand()%360 * 2 * M_PI / 360;
insilmaril@0
   667
    return QPoint ( (int)( + 150*cos (a)),
insilmaril@0
   668
                    (int)( + 150*sin (a)));
insilmaril@0
   669
}
insilmaril@0
   670
insilmaril@0
   671
void LinkableMapObj::alignRelativeTo (QPoint ref)
insilmaril@0
   672
{
insilmaril@166
   673
	// FIXME testing, seems not to be used right now...
insilmaril@95
   674
	cout << "LMO::alignRelTo   ref="<<ref<<endl;
insilmaril@0
   675
}
insilmaril@0
   676
insilmaril@0
   677
void LinkableMapObj::reposition()
insilmaril@0
   678
{
insilmaril@0
   679
	if (depth==0)
insilmaril@0
   680
	{
insilmaril@0
   681
		// only calculate the sizes once. If the deepest LMO changes its height,
insilmaril@0
   682
		// all upper LMOs have to change, too.
insilmaril@0
   683
		calcBBoxSizeWithChilds();
insilmaril@0
   684
insilmaril@0
   685
	    alignRelativeTo ( QPoint (absPos.x(),
insilmaril@0
   686
							absPos.y()-(bboxTotal.height()-bbox.height())/2) );
insilmaril@0
   687
	} else
insilmaril@0
   688
	{
insilmaril@0
   689
		// This is only important for moving branches:
insilmaril@0
   690
		// For editing a branch it isn't called...
insilmaril@0
   691
	    alignRelativeTo ( QPoint (absPos.x(),
insilmaril@0
   692
							absPos.y()-(bboxTotal.height()-bbox.height())/2) );
insilmaril@0
   693
	}
insilmaril@0
   694
}
insilmaril@0
   695
insilmaril@0
   696
void LinkableMapObj::requestReposition()
insilmaril@0
   697
{
insilmaril@0
   698
	if (!repositionRequest)
insilmaril@0
   699
	{
insilmaril@0
   700
		// Pass on the request to parental objects, if this hasn't
insilmaril@0
   701
		// been done yet
insilmaril@0
   702
		repositionRequest=true;
insilmaril@0
   703
		if (parObj) parObj->requestReposition();
insilmaril@0
   704
	}
insilmaril@0
   705
}
insilmaril@0
   706
insilmaril@0
   707
void LinkableMapObj::forceReposition()
insilmaril@0
   708
{
insilmaril@0
   709
	// Sometimes a reposition has to be done immediatly: For example
insilmaril@0
   710
	// if the note editor flag changes, there is no user event in mapeditor
insilmaril@0
   711
	// which could collect requests for a reposition.
insilmaril@0
   712
	// Then we have to call forceReposition()
insilmaril@0
   713
	// But no rule without exception: While loading a map or undoing it,
insilmaril@0
   714
	// we want to block expensive repositioning, but just do it once at
insilmaril@0
   715
	// the end, thus check first:
insilmaril@0
   716
insilmaril@139
   717
	if (mapEditor->isRepositionBlocked()) return;
insilmaril@0
   718
	
insilmaril@0
   719
	// Pass on the request to parental objects, if this hasn't been done yet
insilmaril@0
   720
	
insilmaril@0
   721
	if (parObj) 
insilmaril@0
   722
		parObj->forceReposition(); 
insilmaril@0
   723
	else 
insilmaril@83
   724
		reposition(); 
insilmaril@83
   725
}
insilmaril@0
   726
insilmaril@0
   727
bool LinkableMapObj::repositionRequested()
insilmaril@0
   728
{
insilmaril@0
   729
	return repositionRequest;
insilmaril@0
   730
}
insilmaril@0
   731
insilmaril@0
   732
insilmaril@0
   733
void LinkableMapObj::setSelBox()
insilmaril@0
   734
{
insilmaril@175
   735
    selbox->setX (clickBox.x() );
insilmaril@175
   736
    selbox->setY (clickBox.y() );
insilmaril@175
   737
    selbox->setSize (clickBox.width(), clickBox.height() );
insilmaril@0
   738
}
insilmaril@0
   739
insilmaril@0
   740
void LinkableMapObj::select()
insilmaril@0
   741
{
insilmaril@0
   742
	setSelBox();
insilmaril@0
   743
    selected=true;
insilmaril@0
   744
    selbox->show();
insilmaril@160
   745
	setVisibility (visible);
insilmaril@0
   746
}
insilmaril@0
   747
insilmaril@0
   748
insilmaril@0
   749
void LinkableMapObj::unselect()
insilmaril@0
   750
{
insilmaril@0
   751
    selected=false;
insilmaril@0
   752
    selbox->hide();
insilmaril@160
   753
	setVisibility (visible);
insilmaril@0
   754
}
insilmaril@0
   755
insilmaril@0
   756
void LinkableMapObj::parabel (QPointArray &ya, double p1x, double p1y, double p2x, double p2y)
insilmaril@0
   757
insilmaril@0
   758
{
insilmaril@0
   759
	double vx=p2x - p1x;	// V=P2-P1
insilmaril@0
   760
	double vy=p2y - p1y;
insilmaril@0
   761
insilmaril@0
   762
	double dx;				// delta x during calculation of parabel
insilmaril@0
   763
	
insilmaril@0
   764
	double pnx;				// next point
insilmaril@0
   765
	double pny;
insilmaril@0
   766
	double m;
insilmaril@0
   767
insilmaril@0
   768
	if (vx > -0.0001 && vx < 0.0001)
insilmaril@0
   769
		m=0;
insilmaril@0
   770
	else	
insilmaril@0
   771
		m=(vy / (vx*vx));
insilmaril@0
   772
	dx=vx/(arcsegs);
insilmaril@0
   773
	int i;
insilmaril@104
   774
	ya.setPoint (0,QPoint (qRound(p1x),qRound(p1y)));
insilmaril@0
   775
	for (i=1;i<=arcsegs;i++)
insilmaril@0
   776
	{	
insilmaril@0
   777
		pnx=p1x+dx;
insilmaril@0
   778
		pny=m*(pnx-parPos.x())*(pnx-parPos.x())+parPos.y();
insilmaril@104
   779
		ya.setPoint (i,QPoint (qRound(pnx),qRound(pny)));
insilmaril@0
   780
		p1x=pnx;
insilmaril@0
   781
		p1y=pny;
insilmaril@0
   782
	}	
insilmaril@0
   783
}
insilmaril@0
   784
insilmaril@160
   785
QString LinkableMapObj::getLinkAttr ()
insilmaril@160
   786
{
insilmaril@160
   787
	if (hideLinkUnselected)
insilmaril@175
   788
		return attribut ("hideLink","true");
insilmaril@160
   789
	else
insilmaril@175
   790
		return attribut ("hideLink","false");
insilmaril@160
   791
	
insilmaril@160
   792
}