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