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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
|
#include <QGuiApplication>
#include <QScreen>
#include <QClipboard>
#include <QSettings>
#include <QDesktopServices>
#include <QTextStream>
#include <QFileInfo>
#include <QFile>
#include <QMessageBox>
#include "symedit.h"
//
// settings functions
//
//! Constructor.
SymEditSettings::SymEditSettings()
{
IntValues.emplace("SnapGrid", 5);
IntValues.emplace("LineWidth", 1);
IntValues.emplace("ColorIndex", 1);
IntValues.emplace("FillItem", 0);
IntValues.emplace("Alignment", 9);
IntValues.emplace("SizeUnit", 0);
IntValues.emplace("Tool", 1);
RealValues.emplace("SymbolSize", 5.0);
RealValues.emplace("TextSize", 0.0);
TextValues.emplace("Language", "");
TextValues.emplace("TextValue", "");
TextValues.emplace("Directory", "");
}
//! Load settings.
void SymEditSettings::Load()
{
QSettings settings;
QSize size = QGuiApplication::primaryScreen()->size();
Position.setX(settings.value("window/x", size.width() / 4).toInt());
Position.setY(settings.value("window/y", size.height() / 6).toInt());
Size.setWidth(settings.value("window/width", size.width() / 2).toInt());
Size.setHeight(settings.value("window/height", size.height() * 2 / 3).toInt());
TextValues.at("Directory") = settings.value("application/directory").toString();
IntValues.at("SnapGrid") = settings.value("editor/snap", 5).toInt();
IntValues.at("LineWidth") = settings.value("editor/width", 1).toInt();
RealValues.at("SymbolSize") = settings.value("editor/symbol", 5.0).toDouble();
TextValues.at("Language") = settings.value("editor/lang").toString();
// IntValues.at("ColorIndex") = settings.value("editor/color", 1).toInt();
// IntValues.at("FillItem") = settings.value("editor/fill", 0).toInt();
// RealValues.at("TextSize") = settings.value("editor/size", 0.0).toDouble();
IntValues.at("Alignment") = settings.value("editor/align", 9).toInt();
IntValues.at("SizeUnit") = settings.value("editor/unit", 0).toInt();
TextValues.at("TextValue") = settings.value("editor/text").toString();
IntValues.at("Tool") = settings.value("editor/tool", 1).toInt();
}
//! Save settings.
void SymEditSettings::Save() const
{
QSettings settings;
settings.setValue("window/x", Position.x());
settings.setValue("window/y", Position.y());
settings.setValue("window/width", Size.width());
settings.setValue("window/height", Size.height());
settings.setValue("application/directory", TextValues.at("Directory"));
settings.setValue("editor/snap", IntValues.at("SnapGrid"));
settings.setValue("editor/width", IntValues.at("LineWidth"));
settings.setValue("editor/symbol", RealValues.at("SymbolSize"));
settings.setValue("editor/lang", TextValues.at("Language"));
settings.setValue("editor/color", IntValues.at("ColorIndex"));
settings.setValue("editor/fill", IntValues.at("FillItem"));
settings.setValue("editor/size", RealValues.at("TextSize"));
settings.setValue("editor/align", IntValues.at("Alignment"));
settings.setValue("editor/unit", IntValues.at("SizeUnit"));
settings.setValue("editor/text", TextValues.at("TextValue"));
settings.setValue("editor/tool", IntValues.at("Tool"));
}
//
// manager functions
//
//! Constructor.
/*!
\param parent Optional parent.
*/
SymEditManager::SymEditManager(QObject* parent) : QObject(parent)
{
Settings.Load();
}
//! Constructor.
/*!
\param filename Symbol file name from command line.
\param symbol Symbol string from command line.
*/
SymEditManager::SymEditManager(const QString& filename, const QString& symbol)
: QObject(nullptr), Filename(filename)
{
Settings.Load();
if ( !Filename.isEmpty() )
open(Filename);
if ( !symbol.isEmpty() )
Symbol.Load(symbol);
}
//! Set window initialized.
void SymEditManager::setInitialized()
{
Initialized = true;
}
//! Set window geometry.
/*!
\param point Window position.
\param size Window size.
*/
void SymEditManager::setGeometry(QPoint point, QSize size)
{
if ( Initialized )
{
Settings.Position = point;
Settings.Size = size;
}
}
//! Get window position.
/*!
\return Window position.
*/
QPoint SymEditManager::getWindowPos() const
{
return Settings.Position;
}
//! Get window size.
/*!
\return Window size.
*/
QSize SymEditManager::getWindowSize() const
{
return Settings.Size;
}
//@{
//! Set setting value.
/*!
\param name Setting name.
\param value Setting value.
*/
void SymEditManager::setIntSetting(QString name, int value)
{
if ( Settings.IntValues.find(name) != Settings.IntValues.end() )
Settings.IntValues.at(name) = value;
}
void SymEditManager::setRealSetting(QString name, double value)
{
if ( Settings.RealValues.find(name) != Settings.RealValues.end() )
Settings.RealValues.at(name) = value;
}
void SymEditManager::setTextSetting(QString name, QString value)
{
if ( Settings.TextValues.find(name) != Settings.TextValues.end() )
Settings.TextValues.at(name) = value;
}
//@}
//@{
//! Get setting value.
/*!
\param name Setting name.
\return Setting value.
*/
int SymEditManager::getIntSetting(QString name) const
{
if ( Settings.IntValues.find(name) != Settings.IntValues.end() )
return Settings.IntValues.at(name);
return 0;
}
double SymEditManager::getRealSetting(QString name) const
{
if ( Settings.RealValues.find(name) != Settings.RealValues.end() )
return Settings.RealValues.at(name);
return 0;
}
QString SymEditManager::getTextSetting(QString name) const
{
if ( Settings.TextValues.find(name) != Settings.TextValues.end() )
return Settings.TextValues.at(name);
return QString();
}
//@}
//! Get symbol as string.
/*!
\param rich Rich text with layout.
\return Symbol as string.
*/
QString SymEditManager::getSymbol(bool rich) const
{
QString buffer;
return Symbol.Save(buffer, rich);
}
//! Add point item.
/*!
\param operation Item operation.
\param point Point position.
\param value Item value.
\param color Color index.
\param fill Item area fill.
\return True for success.
*/
bool SymEditManager::addPointItem(int operation, QPoint point, int value, int color, int fill)
{
undosave();
Symbol.AddItem(static_cast<Operation::Type>(operation), point, point, value, color, fill);
return true;
}
//! Add symbol item.
/*!
\param operation Item operation.
\param point Start position.
\param end End position.
\param value Item value.
\param color Color index.
\param fill Item area fill.
\return True for success.
*/
bool SymEditManager::addLineItem(int operation, QPoint point, QPoint end, int value, int color, int fill)
{
undosave();
Symbol.AddItem(static_cast<Operation::Type>(operation), point, end, value, color, fill);
return true;
}
//! Add Text item.
/*!
\param operation Item operation.
\param point Text position.
\param point End position.
\param text Text string.
\param size Text size.
\param unit Size unit.
\param color Color index.
\param align Text alignment.
\return True for success.
*/
bool SymEditManager::addTextItem(int operation, QPoint point, QPoint end, QString text, double size, int unit, int color, int align)
{
if ( !text.isEmpty() )
{
undosave();
Symbol.AddItem(static_cast<Operation::Type>(operation), point, end, text, size, unit, color, align);
return true;
}
return false;
}
//! Remove active item.
/*!
\return True for success.
Activates previous or first item.
*/
bool SymEditManager::removeItem()
{
if ( Symbol.GetItemCount() )
{
undosave();
return Symbol.RemoveItem(Symbol.GetActiveIndex());
}
return false;
}
//! Get item count.
/*!
\return Item count.
*/
int SymEditManager::getItemCount() const
{
return Symbol.GetItemCount();
}
//! Get item operation.
/*!
\param index Item index.
\return Item operation.
*/
int SymEditManager::getItemOperation(int index) const
{
if ( Symbol.GetItemCount() )
return Symbol.GetItem(index).Operation;
return Operation::None;
}
//! Get item position.
/*!
\param index Item index.
\return Item position.
*/
QPoint SymEditManager::getItemPosition(int index) const
{
if ( Symbol.GetItemCount() )
{
const auto& item = Symbol.GetItem(index);
if ( item.Operation == Operation::Rectangle ) // normalize to upper left
return QPoint(std::min(item.End.x(), item.Point.x()), std::max(item.End.y(), item.Point.y()));
return item.Point;
}
return QPoint(0, 0);
}
//@{
//! Get item data value.
/*!
\param index Item index.
\return Item value.
*/
int SymEditManager::getItemValue(int index) const
{
if ( Symbol.GetItemCount() )
return Symbol.GetItem(index).Value;
return 0;
}
QPoint SymEditManager::getItemPoint(int index) const
{
if ( Symbol.GetItemCount() )
{
const auto& item = Symbol.GetItem(index);
if ( item.Operation == Operation::Rectangle ) // normalize to lower right
return QPoint(std::max(item.End.x(), item.Point.x()), std::min(item.End.y(), item.Point.y()));
return item.End;
}
return QPoint(0, 0);
}
QString SymEditManager::getItemText(int index) const
{
if ( Symbol.GetItemCount() )
return Symbol.GetItem(index).Text;
return "";
}
int SymEditManager::getItemColor(int index) const
{
if ( Symbol.GetItemCount() )
return Symbol.GetItem(index).Color;
return 1;
}
int SymEditManager::getItemFill(int index) const
{
if ( Symbol.GetItemCount() )
return Symbol.GetItem(index).Fill;
return 0;
}
int SymEditManager::getItemAlign(int index) const
{
if ( Symbol.GetItemCount() )
return Symbol.GetItem(index).Align;
return 0;
}
double SymEditManager::getItemSize(int index) const
{
if ( Symbol.GetItemCount() )
return Symbol.GetItem(index).Size;
return 0.0;
}
int SymEditManager::getItemUnit(int index) const
{
if ( Symbol.GetItemCount() )
return Symbol.GetItem(index).Unit;
return 0;
}
//@}
//! Select item nearest to point.
/*!
\param point Point coordinates.
\return Nearest item index.
*/
int SymEditManager::selectItem(QPoint point) const
{
return Symbol.SelectItem(point);
}
//! Set active item index.
/*!
\param index Active item index.
\return True for success.
*/
bool SymEditManager::setActiveIndex(int index)
{
return Symbol.SetActiveIndex(index);
}
//! Get active item index.
/*!
\return Active item index.
*/
int SymEditManager::getActiveIndex() const
{
return Symbol.GetActiveIndex();
}
//! Cut symbol to clipboard.
void SymEditManager::cutClipboard()
{
if ( QClipboard* clipboard = QGuiApplication::clipboard() )
{
undosave();
clipboard->setText(getSymbol(false));
Symbol.Clear();
}
}
//! Copy symbol to clipboard.
void SymEditManager::copyClipboard() const
{
if ( QClipboard* clipboard = QGuiApplication::clipboard() )
clipboard->setText(getSymbol(false));
}
//! Paste symbol from clipboard.
void SymEditManager::pasteClipboard()
{
if ( QClipboard* clipboard = QGuiApplication::clipboard() )
{
undosave();
Symbol.Load(clipboard->text());
}
}
//! Rotate symbol.
/*!
\param dir Positive value rotates right, negative left.
*/
void SymEditManager::rotateSymbol(int dir)
{
undosave();
Symbol.RotateSymbol(dir);
}
//! Raise or lower item.
/*!
\param dir Positive value raises, negative lowers.
\return True for success.
*/
bool SymEditManager::raiseItem(int dir)
{
if ( Symbol.GetItemCount() )
{
undosave();
return Symbol.RaiseItem(dir);
}
return false;
}
//! Undo edit operation.
/*!
\param undo True undoes, false redoes.
\return True for success.
*/
bool SymEditManager::undo(bool undo)
{
if ( undo )
{
if ( UndoStack.empty() )
return false;
QString buffer;
Symbol.Save(buffer);
RedoStack.push_back(buffer);
Symbol.Load(UndoStack.back());
UndoStack.pop_back();
return true;
}
if ( RedoStack.empty() )
return false;
QString buffer;
Symbol.Save(buffer);
UndoStack.push_back(buffer);
Symbol.Load(RedoStack.back());
RedoStack.pop_back();
return true;
}
//! Save item to undo stack.
void SymEditManager::undosave()
{
QString buffer;
Symbol.Save(buffer);
UndoStack.push_back(buffer);
RedoStack.clear();
}
//! Open help.
/*!
\param topic Help topic.
*/
void SymEditManager::help(QString topic) const
{
Q_UNUSED(topic);
QString path = QCoreApplication::applicationDirPath();
path.append("/help/").append(Language).append("/symedit/index.html");
if ( QFileInfo(path).exists() ) // release
QDesktopServices::openUrl(QUrl(path.insert(0, "file:///"), QUrl::TolerantMode));
else // visual studio
{
path = QCoreApplication::applicationDirPath();
path.append("/../threedee/help/").append(Language).append("/symedit/index.html");
if ( QFileInfo(path).exists() )
QDesktopServices::openUrl(QUrl(path.insert(0, "file:///"), QUrl::TolerantMode));
else // qt creator (windows)
{
path = QCoreApplication::applicationDirPath();
path.append("/../../symedit/help/").append(Language).append("/_build/html/index.html");
if ( QFileInfo(path).exists() )
QDesktopServices::openUrl(QUrl(path.insert(0, "file:///"), QUrl::TolerantMode));
else // qt creator (linux)
{
path = QCoreApplication::applicationDirPath();
path.append("/../symedit/help/").append(Language).append("/_build/html/index.html");
if ( QFileInfo(path).exists() )
QDesktopServices::openUrl(QUrl(path.insert(0, "file:///"), QUrl::TolerantMode));
}
}
}
}
//! Open symbol file.
/*!
\param fileurl File name as URL.
\return True for success.
*/
bool SymEditManager::open(QUrl fileurl)
{
QString filestring = fileurl.toString();
int index = filestring.lastIndexOf('/') + 1;
QString directory = filestring.left(index);
setTextSetting("Directory", directory);
return open(fileurl.toLocalFile());
}
//! Save symbol file.
/*!
\param fileurl File name as URL.
\return True for success.
*/
bool SymEditManager::save(QUrl fileurl)
{
QString filename = Filename;
if ( !fileurl.isEmpty() )
{
QString filestring = fileurl.toString();
int index = filestring.lastIndexOf('/') + 1;
QString directory = filestring.left(index);
setTextSetting("Directory", directory);
filename = fileurl.toLocalFile();
}
return save(filename);
}
//! Check file name existence.
/*!
\return True for existing file name.
*/
bool SymEditManager::hasFilename() const
{
return !Filename.isEmpty();
}
//! Get file name without path.
/*!
\return File name as url.
*/
QUrl SymEditManager::getFilename() const
{
int index = Filename.lastIndexOf('/');
return QUrl(Filename.mid(index + 1));
}
//! Open symbol file.
/*!
\param filename Full file path.
\return True for success.
*/
bool SymEditManager::open(const QString& filename)
{
QFile file(filename);
if ( file.open(QIODevice::ReadOnly | QIODevice::Text) )
{
QTextStream input(&file);
Symbol.Load(input.readLine());
Filename = filename;
return true;
}
return false;
}
//! Save symbol file.
/*!
\param filename Full file path.
\return True for success.
*/
bool SymEditManager::save(const QString& filename)
{
QFile file(filename);
if ( file.open(QIODevice::WriteOnly | QIODevice::Text) )
{
QTextStream output(&file);
output << getSymbol(false);
Filename = filename;
return true;
}
return false;
}
//! Set language.
/*!
\param lang Language abbreviation.
*/
void SymEditManager::setLanguage(QString lang)
{
Language = lang;
}
|