File: hdPolyLineFigure.cpp

package info (click to toggle)
pgadmin3 1.20.0~beta2-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 73,704 kB
  • ctags: 18,591
  • sloc: cpp: 193,786; ansic: 18,736; sh: 5,154; pascal: 1,120; yacc: 927; makefile: 516; lex: 421; xml: 126; perl: 40
file content (446 lines) | stat: -rw-r--r-- 9,840 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
//////////////////////////////////////////////////////////////////////////
//
// pgAdmin III - PostgreSQL Tools
//
// Copyright (C) 2002 - 2014, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
// hdPolyLineFigure.cpp - A simple line figure that can be split on several lines joined by flexibility points
//
//////////////////////////////////////////////////////////////////////////

#include "pgAdmin3.h"

// wxWindows headers
#include <wx/wx.h>
#include <wx/dcbuffer.h>
#include <wx/pen.h>

// App headers
#include "hotdraw/figures/hdPolyLineFigure.h"
#include "hotdraw/utilities/hdArrayCollection.h"
#include "hotdraw/locators/hdILocator.h"
#include "hotdraw/handles/hdPolyLineHandle.h"
#include "hotdraw/figures/hdLineTerminal.h"
#include "hotdraw/locators/hdPolyLineLocator.h"
#include "hotdraw/utilities/hdGeometry.h"
#include "hotdraw/tools/hdPolyLineFigureTool.h"
#include "hotdraw/tools/hdMenuTool.h"

hdPolyLineFigure::hdPolyLineFigure()
{
	unsigned int i;

	for(i = 0; i < MAXPOS ; i++)
	{
		points.Add(new hdArrayCollection());
	}

	startTerminal = NULL;
	endTerminal = NULL;
	handlesChanged = false;
	startPoint = hdPoint(0, 0);
	endPoint = hdPoint(0, 0);
	pointAtPos = hdPoint(0, 0);
	linePen = wxPen(wxString(wxT("BLACK")), 1, wxSOLID);
}

hdPolyLineFigure::~hdPolyLineFigure()
{
	hdPoint *tmp; //Hack: If just delete points collection an error is raised.
	hdArrayCollection *tmpCollection;

	unsigned int i;
	for(i = 0; i < points.Count(); i++)
	{
		while(points[i]->count() > 0)
		{
			tmp = (hdPoint *) points[i]->getItemAt(0);
			points[i]->removeItemAt(0);
			delete tmp;
		}
		if(points[i])
		{
			tmpCollection = points[i];
			points.RemoveAt(i);
			delete tmpCollection;
		}
	}
	if(startTerminal)
		delete startTerminal;
	if(endTerminal)
		delete endTerminal;
}

void hdPolyLineFigure::AddPosForNewDiagram()
{
	//Add position for new displaybox at new diagram
	hdIFigure::AddPosForNewDiagram();
	//Add new array of point for polylinefigure
	points.Add(new hdArrayCollection());
}

void hdPolyLineFigure::RemovePosOfDiagram(int posIdx)
{
	hdIFigure::RemovePosOfDiagram(posIdx);

	//Hack: If just delete points collection an error is raised.
	hdPoint *tmp;
	hdArrayCollection *tmpCollection;
	while(points[posIdx]->count() > 0)
	{
		tmp = (hdPoint *) points[posIdx]->getItemAt(0);
		points[posIdx]->removeItemAt(0);
		delete tmp;
	}

	if(points[posIdx])
	{
		tmpCollection = points[posIdx];
		points.RemoveAt(posIdx);
		delete tmpCollection;
	}
}

int hdPolyLineFigure::getMaximunIndex()
{
	unsigned int i;
	int max = points[0]->count();

	for(i = 1; i < points.Count(); i++)
	{
		if(points[i]->count() > max)
		{
			max = points[i]->count();
		}

	}
	return max;
}


hdMultiPosRect &hdPolyLineFigure::getBasicDisplayBox()
{
	basicDisplayBox.height = 0;
	basicDisplayBox.width = 0;

	int posIdx;
	//optimize this if needed in a future, because right now calculate displaybox for all posIdx
	hdIteratorBase *iterator;
	for(posIdx = 0; posIdx < basicDisplayBox.CountPositions(); posIdx++)
	{
		if(points[posIdx]->count() >= 1)
		{
			basicDisplayBox.SetPosition(posIdx, pointAt(posIdx, 0));
		}
		else
		{
			basicDisplayBox.SetPosition(posIdx, wxPoint(0, 0));
		}

		iterator = points[posIdx]->createIterator();
		while(iterator->HasNext())
		{
			hdPoint *p = (hdPoint *) iterator->Next();
			hdRect r = hdRect(p->x, p->y, 0, 0);
			basicDisplayBox.add(posIdx, r);
		}

		delete iterator;
	}
	return basicDisplayBox;
}

int hdPolyLineFigure::pointLinesCount()
{
	return points.Count();
}
int hdPolyLineFigure::pointCount(int posIdx)
{
	return points[posIdx]->count();
}

hdPoint &hdPolyLineFigure::getStartPoint(int posIdx)
{
	startPoint.x = ((hdPoint *)points[posIdx]->getItemAt(0))->x;
	startPoint.y = ((hdPoint *)points[posIdx]->getItemAt(0))->y;
	return startPoint;
}

void hdPolyLineFigure::setStartPoint(int posIdx, hdPoint point)
{
	willChange();
	if(points[posIdx]->count() == 0)
		addPoint(posIdx, point.x, point.y);
	else
	{
		hdPoint *p = (hdPoint *) points[posIdx]->getItemAt(0);
		p->x = point.x;
		p->y = point.y;
	}
	changed(posIdx);
}

hdPoint &hdPolyLineFigure::getEndPoint(int posIdx)
{
	endPoint.x = ((hdPoint *)points[posIdx]->getItemAt(points[posIdx]->count() - 1))->x;
	endPoint.y = ((hdPoint *)points[posIdx]->getItemAt(points[posIdx]->count() - 1))->y;
	return endPoint;
}

void hdPolyLineFigure::setEndPoint(int posIdx, hdPoint point)
{
	willChange();
	if(points[posIdx]->count() < 2)
		addPoint(posIdx, point.x, point.y);
	else
	{
		hdPoint *p = (hdPoint *) points[posIdx]->getItemAt(points[posIdx]->count() - 1);
		p->x = point.x;
		p->y = point.y;
	}
	changed(posIdx);
}

void hdPolyLineFigure::setStartTerminal(hdLineTerminal *terminal)
{
	startTerminal = terminal;
}

hdLineTerminal *hdPolyLineFigure::getStartTerminal()
{
	return startTerminal;
}

void hdPolyLineFigure::setEndTerminal(hdLineTerminal *terminal)
{
	endTerminal = terminal;
}

hdLineTerminal *hdPolyLineFigure::getEndTerminal()
{
	return endTerminal;
}

hdCollection *hdPolyLineFigure::handlesEnumerator()
{
	return handles;
}

void hdPolyLineFigure::addPoint (int posIdx, int x, int y)
{
	willChange();
	points[posIdx]->addItem((hdObject *) new hdPoint(x, y) );

	if( handles->count() < getMaximunIndex() )
	{
		//Update handles
		handles->addItem(new hdPolyLineHandle(this, new hdPolyLineLocator(0), 0));
		updateHandlesIndexes();
	}
	changed(posIdx);
}

void hdPolyLineFigure::changed(int posIdx)
{
	handlesChanged = true;
}

void hdPolyLineFigure::removePointAt (int posIdx, int index)
{
	willChange();
	hdPoint *p = (hdPoint *) points[posIdx]->getItemAt(index);
	points[posIdx]->removeItemAt(index);
	delete p;
	//Update handles [If there are more handles than maximum points of a line in a view]
	if( handles->count() > getMaximunIndex() )
	{
		handles->removeItemAt(index);
		updateHandlesIndexes();
	}
	changed(posIdx);
}

void hdPolyLineFigure::basicDrawSelected(wxBufferedDC &context, hdDrawingView *view)
{
	basicDraw(context, view);
}

void hdPolyLineFigure::basicDraw(wxBufferedDC &context, hdDrawingView *view)
{
	int posIdx = view->getIdx();
	if(points[posIdx]->count() < 2)
	{
		return;
	}
	hdPoint start, end;

	if(startTerminal)
	{
		startTerminal->setLinePen(linePen);
		start = startTerminal->draw(context, getStartPoint(posIdx), pointAt(posIdx, 1), view);
	}
	else
	{
		start = getStartPoint(posIdx);
	}

	if(endTerminal)
	{
		endTerminal->setLinePen(linePen);
		end = endTerminal->draw(context, getEndPoint(posIdx), pointAt(posIdx, pointCount(posIdx) - 2), view);
	}
	else
	{
		end = getEndPoint(posIdx);
	}

	context.SetPen(linePen);
	for(int i = 0; i < points[posIdx]->count() - 1; i++)
	{
		hdPoint *p1 = (hdPoint *) points[posIdx]->getItemAt(i);
		hdPoint *p2 = (hdPoint *) points[posIdx]->getItemAt(i + 1);

		hdPoint copyP1 = hdPoint (*p1);
		view->CalcScrolledPosition(copyP1.x, copyP1.y, &copyP1.x, &copyP1.y);
		hdPoint copyP2 = hdPoint (*p2);
		view->CalcScrolledPosition(copyP2.x, copyP2.y, &copyP2.x, &copyP2.y);

		context.DrawLine(copyP1, copyP2);
	}
}

void hdPolyLineFigure::basicMoveBy(int posIdx, int x, int y)
{
	hdPoint *movPoint;
	for(int i = 0 ; i < points[posIdx]->count() ; i++)
	{
		movPoint = (hdPoint *) points[posIdx]->getItemAt(i);
		movPoint->x += x;
		movPoint->y += y;
	}
}

hdITool *hdPolyLineFigure::CreateFigureTool(hdDrawingView *view, hdITool *defaultTool)
{
	return new hdPolyLineFigureTool(view, this, new hdMenuTool(view, this, defaultTool));
}


int hdPolyLineFigure::findSegment (int posIdx, int x, int y)
{
	for(int i = 0 ; i < points[posIdx]->count() - 1 ; i++)
	{
		hdPoint p1 = pointAt(posIdx, i);
		hdPoint p2 = pointAt(posIdx, i + 1);
		hdGeometry g;
		if(g.lineContainsPoint(p1.x, p1.y, p2.x, p2.y, x, y))
		{
			return i + 1;
		}
	}
	return -1;
}

hdPoint &hdPolyLineFigure::pointAt(int posIdx, int index)
{
	//hack to avoid error with bad indexes calls
	if(index < 0)
	{
		pointAtPos.x = 0;
		pointAtPos.y = 0;
	}
	else
	{
		pointAtPos.x = ((hdPoint *)points[posIdx]->getItemAt(index))->x;
		pointAtPos.y = ((hdPoint *)points[posIdx]->getItemAt(index))->y;
	}
	return pointAtPos;
}

bool hdPolyLineFigure::containsPoint (int posIdx, int x, int y)
{
	hdRect rect = this->displayBox().gethdRect(posIdx);
	rect.Inflate(4, 4);
	if(!rect.Contains(x, y))
	{
		return false;
	}

	for(int i = 0 ; i < points[posIdx]->count() - 1 ; i++)
	{
		hdPoint p1 = pointAt(posIdx, i);
		hdPoint p2 = pointAt(posIdx, i + 1);
		hdGeometry g;
		if(g.lineContainsPoint(p1.x, p1.y, p2.x, p2.y, x, y))
		{
			return true;
		}
	}
	return false;
}

void hdPolyLineFigure::clearPoints(int posIdx)
{
	points[posIdx]->deleteAll();
}

void hdPolyLineFigure::insertPointAt (int posIdx, int index, int x, int y)
{
	willChange();
	points[posIdx]->insertAtIndex((hdObject *) new hdPoint(x, y), index);

	if( handles->count() < getMaximunIndex() )
	{
		//Update handles
		handles->insertAtIndex(new hdPolyLineHandle(this, new hdPolyLineLocator(index), index), index);
		updateHandlesIndexes();
	}

	changed(posIdx);
}

void hdPolyLineFigure::setPointAt (int posIdx, int index, int x, int y)
{
	willChange();
	hdPoint *p = (hdPoint *) points[posIdx]->getItemAt(index);
	p->x = x;
	p->y = y;
	changed(posIdx);
}

void hdPolyLineFigure::splitSegment(int posIdx, int x, int y)
{
	int index = findSegment(posIdx, x, y);

	if(index != -1)
	{
		insertPointAt(posIdx, index, x, y);
	}
}

void hdPolyLineFigure::updateHandlesIndexes()
{
	hdPolyLineHandle *h = NULL;

	//Get maximun point position in a collection of points
	int i, maxPosition = getMaximunIndex();

	//Update Handles indexes
	for(i = 0; i < maxPosition; i++)
	{
		h = (hdPolyLineHandle *) handles->getItemAt(i);
		h->setIndex(i);
	}

}

void hdPolyLineFigure::setLinePen(wxPen pen)
{
	linePen = pen;
}

int hdPolyLineFigure::countPointsAt(int posIdx)
{
	return points[posIdx]->count();
}