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