aboutsummaryrefslogtreecommitdiff
path: root/symbol.cpp
blob: 5f179deb7b445fcfe5f4c331ac4bec209da22018 (plain)
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
#include <QStringList>

#include <math.h>

#include "symbol.h"

const double ConstPi = 3.14159265358979323846;	//!< Constant for PI.
const double ConstPi2 = ConstPi / 2.0;			//!< Constant for PI / 2.

namespace Operation
{
	Q_NAMESPACE
}

//
//	symbol item functions
//
const SymEditSymbol::Item gDefaultItem;		//!< Empty default item.

//! Constructor.
SymEditSymbol::Item::Item() : Operation(Operation::None), Fill(0), Align(9)
{

}

//! Constructor.
/*!
	\param operation	Item operation.
	\param point		Item position.
	\param end			End position.
	\param value		Item value.
	\param color		Item color index.
	\param fill			Item area fill.
*/
SymEditSymbol::Item::Item(Operation::Type operation, QPoint point, QPoint end, int value, int color, int fill)
	: Operation(operation), Point(point), End(end), Size(0.0), Value(value), Color(color), Fill(fill), Align(0)
{

}

//! Constructor.
/*!
	\param operation	Item operation.
	\param point		Item position.
	\param end			End position.
	\param text			Text string.
	\param size 		Text size.
	\param color		Item color index.
	\param align		Text alignment.
*/
SymEditSymbol::Item::Item(Operation::Type operation, QPoint point, QPoint end, QString text, double size, int color, int align)
	: Operation(operation), Point(point), End(end), Text(text), Size(size), Value(0), Color(color), Fill(0), Align(align)
{

}

//
//	symbol functions
//
//! Constructor.
SymEditSymbol::SymEditSymbol() : ActiveIndex(-1)
{

}

//! Load symbol from string.
/*!
	\param buffer		String buffer.
*/
void SymEditSymbol::Load(const QString& buffer)
{
	Items.clear();
	QPoint position(0, 0);
	double size = 0.0;
	int color = 1, fill = 0, align = 9, angle = 0;
	for ( const auto& string : buffer.split(';', QString::SkipEmptyParts) )
	{
		int type = string.at(0).toLatin1();
		int comma = string.indexOf(',');
		if ( comma >= 0 )
		{
			int x = string.mid(1, comma - 1).toInt();
			int y = string.mid(comma + 1).toInt();
			if ( type == 'D' )
				Items.push_back(Item(Operation::Line, position, QPoint(x, y), 0, color, fill));
			else if ( type == 'B' )
				Items.push_back(Item(Operation::Rectangle, position, QPoint(x, y), 0, color, fill));
//			else if ( type == 'H' )
//				Items.push_back(Item(Operation::Arc, position, QPoint(x, y), color, fill));
			position = QPoint(x, y);
		}
		else	// single parameter
		{
			int value = string.mid(1).toInt();
			if ( type == 'C' )
				color = value;
			else if ( type == 'F' )
				fill = value;
			else if ( type == 'J' )
				align = value;
			else if ( type == 'S' )
				size = string.mid(1).toDouble();
			else if ( type == 'G' )
				angle = value;
			else if ( type == 'R' )
				Items.push_back(Item(Operation::Circle, position, position, value, color, fill));
			else if ( type == '!' || type == '$' || type == '#' )
			{
				QPoint end = position;
				if ( angle )
				{
					double radian = angle / 200.0 * ConstPi;
					end = QPoint(static_cast<int>(cos(radian) * 100.0), static_cast<int>(sin(radian) * 100.0));
				}
				Items.push_back(Item(Operation::Text, position, end, type == '!' ? string.mid(1) : string, size, color, align));
			}
		}
	}
	ActiveIndex = static_cast<int>(Items.size()) - 1;
}

//! Save symbol to string.
/*!
	\param buffer		String buffer.
	\param rich			Rich text with layout.
	\return				Reference to buffer.
*/
QString& SymEditSymbol::Save(QString& buffer, bool rich) const
{
	auto appendpoint = [](QString& buffer, const QPoint& point, int count)
	{
		QString value;
		buffer.append(value.setNum(point.x()));
		if ( count > 1 )
			buffer.append(',').append(value.setNum(point.y()));
		buffer.append(';');
	};
	auto appendoption = [](QChar option, QString& buffer, int index)
	{
		QString value;
		buffer.append(option).append(value.setNum(index)).append(';');
		return index;
	};

	buffer.clear();
	QPoint position(0, 0);
	double size = 0.0;
	int index = 0, color = 1, fill = 0, align = 9, angle = 0;
	for ( const auto& item : Items )
	{
		if ( rich && index == ActiveIndex )
			buffer.append("<b>");
		switch ( item.Operation )
		{
			case Operation::Line:
			case Operation::Rectangle:
			{
				if ( item.Color != color )
					color = appendoption('C', buffer, item.Color);
				if ( item.Fill != fill )
					fill = appendoption('F', buffer, item.Fill);
				if ( item.Point != position )
					appendpoint(buffer.append('U'), item.Point, 2);
				appendpoint(buffer.append(item.Operation == Operation::Line ? 'D' : 'B'), item.End, 2);
				position = item.End;
				break;
			}
			case Operation::Circle:
			{
				if ( item.Color != color )
					color = appendoption('C', buffer, item.Color);
				if ( item.Fill != fill )
					fill = appendoption('F', buffer, item.Fill);
				if ( item.Point != position )
					appendpoint(buffer.append('U'), item.Point, 2);
				appendoption('R', buffer, item.Value);
				position = item.Point;
				break;
			}
			case Operation::Text:
			{
				int temp = 0;
				if ( item.Point.y() != item.End.y() || item.Point.x() != item.End.x() )
					temp = static_cast<int>(atan2(item.End.y() - item.Point.y(), item.End.x() - item.Point.x()) / ConstPi * 200.0);
				if ( temp != angle )
					angle = appendoption('G', buffer, temp);
				if ( item.Color != color )
					color = appendoption('C', buffer, item.Color);
				if ( item.Align != align )
					align = appendoption('J', buffer, item.Align);
				if ( item.Size != size )
				{
//					size = appendoption('S', buffer, item.Size);
					QString value;
					size = item.Size;
					buffer.append('S').append(value.setNum(size)).append(';');
				}
				if ( item.Point != position )
					appendpoint(buffer.append('U'), item.Point, 2);
				if ( item.Text.front() != '$' && item.Text.front() != '#' )
					buffer.append('!');		// constant text
				buffer.append(item.Text).append(';');
				break;
			}
			case Operation::Arc:
			{
				if ( item.Color != color )
					color = appendoption('C', buffer, item.Color);
				if ( item.Fill != fill )
					fill = appendoption('F', buffer, item.Fill);
				if ( item.Point != position )
					appendpoint(buffer.append('U'), item.Point, 2);
				appendoption('H', buffer, item.Value);
				position = item.Point;
				break;
			}
			default:		assert(0);
		}
		if ( rich && index == ActiveIndex )
			buffer.append("</b>");
		++index;
	}
	if ( !buffer.isEmpty() && buffer.back() == ';' )
		buffer.chop(1);

	return buffer;
}

//! Clear symbol.
void SymEditSymbol::Clear()
{
	Items.clear();
}

//! Add symbol item.
/*!
	\param operation	Item operation.
	\param point		Start position.
	\param end			End position.
	\param value		Item value.
	\param color		Item color index.
	\param fill			Item area fill.
	\return				Reference to item.
*/
SymEditSymbol::Item& SymEditSymbol::AddItem(Operation::Type operation, QPoint point, QPoint end, int value, int color, int fill)
{
	Item item(operation, point, end, value, color, fill);
	ActiveIndex = static_cast<int>(Items.size());
	Items.push_back(item);
	return Items.back();
}

//! Add symbol item.
/*!
	\param operation	Item operation.
	\param point		Start position.
	\param end			End position.
	\param value		Item value.
	\param color		Item color index.
	\param fill			Item area fill.
	\return				Reference to item.
*/
SymEditSymbol::Item& SymEditSymbol::AddItem(Operation::Type operation, QPoint point, QPoint end, QString text, double size, int color, int align)
{
	Item item(operation, point, end, text, size, color, align);
	ActiveIndex = static_cast<int>(Items.size());
	Items.push_back(item);
	return Items.back();
}

//! Remove item.
/*!
	\param index		Item index.
	\return				True for success.

	Activates previous or first item.
*/
bool SymEditSymbol::RemoveItem(int index)
{
	if ( static_cast<size_t>(index) < Items.size() )
	{
		Items.erase(Items.begin() + index);
		ActiveIndex = (index == 0 && !Items.empty() ? 0 : index - 1);
		return true;
	}
	return false;
}

//! Select item nearest to point.
/*!
	\param point		Point coordinates.
	\return				Nearest item index.
*/
int SymEditSymbol::SelectItem(QPoint point) const
{
	ActiveIndex = -1;
	int index = 0;
	double minimum = 100.0;

	auto difference = [](const QPoint& start, const QPoint& end) { return QPoint(end.x() - start.x(), end.y() - start.y()); };
	auto length = [](const QPoint& point) { return sqrt(point.x() * point.x() + point.y() * point.y()); };
	auto scalar = [](const QPoint& start, const QPoint& end) { return static_cast<double>(start.x() * end.x() + start.y() * end.y()); };
	auto cross = [](const QPoint& start, const QPoint& end) { return static_cast<double>(start.x() * end.y() - start.y() * end.x()); };
	auto projection = [&](const QPoint& point, const QPoint& start, const QPoint& end)
	{
		QPoint line = difference(start, end);
		return scalar(difference(start, point), line) / length(line);
	};
	auto check = [&](double distance)
	{
		if ( ActiveIndex < 0 || distance < minimum )
		{
			ActiveIndex = index;
			minimum = distance;
		}
	};

	auto distance = [&](const QPoint& point, const QPoint& start, const QPoint& end)
	{
		QPoint line = difference(start, end);
		double len = length(line), a = projection(point, start, end);
		if ( a > 0.0 && a < len )
			check(fabs(cross(difference(start, point), line) / len));
		check(length(difference(point, start)));
		check(length(difference(point, end)));
	};

	for ( const auto& item : Items )
	{
		switch ( item.Operation )
		{
			case Operation::Line:
			{
				distance(point, item.Point, item.End);
				break;
			}
			case Operation::Rectangle:
			{
				distance(point, item.Point, QPoint(item.Point.x(), item.End.y()));
				distance(point, QPoint(item.Point.x(), item.End.y()), item.End);
				distance(point, item.End, QPoint(item.End.x(), item.Point.y()));
				distance(point, QPoint(item.End.x(), item.Point.y()), item.Point);
				break;
			}
			case Operation::Circle:
			{
				check(fabs(length(difference(point, item.Point)) - item.Value));
				break;
			}
			case Operation::Text:
			{
				check(fabs(length(difference(point, item.Point))));
				break;
			}
			case Operation::Arc:
			{
				//##
				break;
			}
			default:		assert(0);
		}
		++index;
	}
	return ActiveIndex;
}

//! Set active item index.
/*!
	\param index		Active item index or -1 for deactivation.
	\return				True for success.
*/
bool SymEditSymbol::SetActiveIndex(int index)
{
	if ( index >= -1 && index < static_cast<int>(Items.size()) )
	{
		ActiveIndex = index;
		return true;
	}
	return false;
}

//! Get active item index.
/*!
	\return				Active item index.
*/
int SymEditSymbol::GetActiveIndex() const
{
	return static_cast<int>(ActiveIndex);
}

//! Get item count.
/*!
	\return				Item count.
*/
int SymEditSymbol::GetItemCount() const
{
	return static_cast<int>(Items.size());
}

//! Get item from index.
/*!
	\param index		Item index.
	\return				Item from index.
*/
const SymEditSymbol::Item& SymEditSymbol::GetItem(int index) const
{
	if ( index >= 0 && index < static_cast<int>(Items.size()) )
		return Items.at(static_cast<size_t>(index));
	return gDefaultItem;
}

//! Rotate symbol.
/*!
	\param dir			Positive value rotates right, negative left
*/
void SymEditSymbol::RotateSymbol(int dir)
{
	auto rotate = [](QPoint& point, int dir)
	{
		double length = sqrt(point.x() * point.x() + point.y() * point.y());
		double angle = atan2(point.y(), point.x());
		angle += (dir > 0 ? -ConstPi2 : ConstPi2);
		double x = cos(angle) * length, y = sin(angle) * length;
		point.setX(static_cast<int>(x + (x > 0.0 ? 0.5 : -0.5)));
		point.setY(static_cast<int>(y + (y > 0.0 ? 0.5 : -0.5)));
	};

	for ( auto& item : Items )
	{
		rotate(item.Point, dir);
		if ( item.Operation == Operation::Line || item.Operation == Operation::Rectangle )
			rotate(item.End, dir);
	}
}

//! Raise or lower item.
/*!
	\param dir			Positive value raises, negative lowers.
	\return				True for success.
*/
bool SymEditSymbol::RaiseItem(int dir)
{
	if ( !Items.empty() )
	{
		size_t index = static_cast<size_t>(ActiveIndex);
		if ( dir > 0 && index < Items.size() - 1 )	// raise
		{
			std::swap(Items[index], Items[index + 1]);
			++ActiveIndex;
			return true;
		}
		if ( dir < 0 && index > 0 )		// lower
		{
			std::swap(Items[index - 1], Items[index]);
			--ActiveIndex;
			return true;
		}
	}
	return false;
}