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