-
-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy pathschematic.cpp
2709 lines (2345 loc) · 87.3 KB
/
schematic.cpp
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
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
schematic.cpp
---------------
begin : Sat Mar 3 2006
copyright : (C) 2006 by Michael Margraf
email : michael.margraf@alumni.tu-berlin.de
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include <algorithm>
#include <cassert>
#include <limits.h>
#include <stdlib.h>
#include <QAction>
#include <QApplication>
#include <QClipboard>
#include <QDebug>
#include <QDir>
#include <QDragEnterEvent>
#include <QDragLeaveEvent>
#include <QDragMoveEvent>
#include <QDropEvent>
#include <QEvent>
#include <QFileInfo>
#include <QLineEdit>
#include <QListWidget>
#include <QMouseEvent>
#include <QPaintDevice>
#include <QPainter>
#include <QPixmap>
#include <QPoint>
#include <QPrinter>
#include <QRect>
#include <QTextStream>
#include <QUrl>
#include <QWheelEvent>
#include <qt3_compat/qt_compat.h>
#include <QRegularExpression>
#include "components/vafile.h"
#include "components/verilogfile.h"
#include "components/vhdlfile.h"
#include "diagrams/diagrams.h"
#include "main.h"
#include "mouseactions.h"
#include "node.h"
#include "paintings/paintings.h"
#include "qucs.h"
#include "schematic.h"
#include "settings.h"
#include "textdoc.h"
#include "misc.h"
// just dummies for empty lists
Q3PtrList<Wire> SymbolWires;
Q3PtrList<Node> SymbolNodes;
Q3PtrList<Diagram> SymbolDiags;
Q3PtrList<Component> SymbolComps;
/**
If \c point does not lie within \c rect then returns a new
rectangle made by enlarging the source rectangle to include
the \c point. Otherwise returns a rectangle of the same size.
*/
static QRect includePoint(QRect rect, QPoint point) {
if (rect.contains(point)) {
return rect;
} else {
return rect.united(QRect{point, point});
}
}
Schematic::Schematic(QucsApp *App_, const QString &Name_) :
QucsDoc(App_, Name_),
a_Wires(&a_DocWires),
a_DocWires(),
a_Nodes(&a_DocNodes),
a_DocNodes(),
a_Diagrams(&a_DocDiags),
a_DocDiags(),
a_Paintings(&a_DocPaints),
a_DocPaints(),
a_Components(&a_DocComps),
a_DocComps(),
a_SymbolPaints(),
a_PostedPaintEvents(),
a_symbolMode(false),
a_isSymbolOnly(false),
a_GridX(10),
a_GridY(10),
a_ViewX1(0),
a_ViewY1(0),
a_ViewX2(1),
a_ViewY2(1),
a_showFrame(0), // don't show
a_Frame_Text0(tr("Title")),
a_Frame_Text1(tr("Drawn By:")),
a_Frame_Text2(tr("Date:")),
a_Frame_Text3(tr("Revision:")),
a_tmpScale(1.0),
a_tmpViewX1(-200),
a_tmpViewY1(-200),
a_tmpViewX2(200),
a_tmpViewY2(200),
a_tmpUsedX1(-200),
a_tmpUsedY1(-200),
a_tmpUsedX2(200),
a_tmpUsedY2(200),
a_undoActionIdx(0),
// The 'i' means state for being unchanged.
a_undoAction((QVector<QString*>() << new QString(" i\n</>\n</>\n</>\n</>\n"))),
a_undoSymbolIdx(0),
// The 'i' means state for being unchanged.
a_undoSymbol((QVector<QString*>() << new QString(" i\n</>\n</>\n</>\n</>\n"))),
a_UsedX1(INT_MAX),
a_UsedY1(INT_MAX),
a_UsedX2(INT_MAX),
a_UsedY2(INT_MAX),
a_previousCursorPosition(),
a_dragIsOkay(false),
a_FileInfo(),
a_Signals(),
a_PortTypes(),
a_isAnalog(false),
a_isVerilog(false),
a_creatingLib(false)
{
setFont(QucsSettings.font);
a_GridColor = _settings::Get().item<QString>("GridColor");
a_tmpPosX = a_tmpPosY = -100;
a_DocComps.setAutoDelete(true);
a_DocWires.setAutoDelete(true);
a_DocNodes.setAutoDelete(true);
a_DocDiags.setAutoDelete(true);
a_DocPaints.setAutoDelete(true);
a_SymbolPaints.setAutoDelete(true);
setVScrollBarMode(Q3ScrollView::AlwaysOn);
setHScrollBarMode(Q3ScrollView::AlwaysOn);
misc::setWidgetBackgroundColor(viewport(), QucsSettings.BGColor);
viewport()->setMouseTracking(true);
viewport()->setAcceptDrops(true); // enable drag'n drop
// to repair some strange scrolling artefacts
connect(this, SIGNAL(horizontalSliderReleased()), viewport(), SLOT(update()));
connect(this, SIGNAL(verticalSliderReleased()), viewport(), SLOT(update()));
if (App_) {
connect(this,SIGNAL(signalCursorPosChanged(int, int, QString)),App_,SLOT(printCursorPosition(int, int, QString)));
connect(this, SIGNAL(horizontalSliderPressed()), App_, SLOT(slotHideEdit()));
connect(this, SIGNAL(verticalSliderPressed()), App_, SLOT(slotHideEdit()));
connect(this, SIGNAL(signalUndoState(bool)), App_, SLOT(slotUpdateUndo(bool)));
connect(this, SIGNAL(signalRedoState(bool)), App_, SLOT(slotUpdateRedo(bool)));
connect(this, SIGNAL(signalFileChanged(bool)), App_, SLOT(slotFileChanged(bool)));
}
}
Schematic::~Schematic() {}
// ---------------------------------------------------
bool Schematic::createSubcircuitSymbol()
{
// If the number of ports is not equal, remove or add some.
unsigned int countPort = adjustPortNumbers();
// If a symbol does not yet exist, create one.
if (a_SymbolPaints.count() != countPort)
return false;
int h = 30 * ((countPort - 1) / 2) + 10;
a_SymbolPaints.prepend(new ID_Text(-20, h + 4));
a_SymbolPaints.append(new GraphicLine(-20, -h, 40, 0, QPen(Qt::darkBlue, 2)));
a_SymbolPaints.append(new GraphicLine(20, -h, 0, 2 * h, QPen(Qt::darkBlue, 2)));
a_SymbolPaints.append(new GraphicLine(-20, h, 40, 0, QPen(Qt::darkBlue, 2)));
a_SymbolPaints.append(new GraphicLine(-20, -h, 0, 2 * h, QPen(Qt::darkBlue, 2)));
unsigned int i = 0, y = 10 - h;
while (i < countPort) {
i++;
a_SymbolPaints.append(new GraphicLine(-30, y, 10, 0, QPen(Qt::darkBlue, 2)));
a_SymbolPaints.at(i)->setCenter(-30, y);
if (i == countPort)
break;
i++;
a_SymbolPaints.append(new GraphicLine(20, y, 10, 0, QPen(Qt::darkBlue, 2)));
a_SymbolPaints.at(i)->setCenter(30, y);
y += 60;
}
return true;
}
// ---------------------------------------------------
void Schematic::becomeCurrent(bool update)
{
emit signalCursorPosChanged(0, 0, "");
// update appropriate menu entry
if (a_symbolMode) {
a_App->symEdit->setText(tr("Edit Schematic"));
a_App->symEdit->setStatusTip(tr("Edits the schematic"));
a_App->symEdit->setWhatsThis(tr("Edit Schematic\n\nEdits the schematic"));
} else {
a_App->symEdit->setText(tr("Edit Circuit Symbol"));
a_App->symEdit->setStatusTip(tr("Edits the symbol for this schematic"));
a_App->symEdit->setWhatsThis(
tr("Edit Circuit Symbol\n\nEdits the symbol for this schematic"));
}
if (a_symbolMode) {
a_Nodes = &SymbolNodes;
a_Wires = &SymbolWires;
a_Diagrams = &SymbolDiags;
a_Paintings = &a_SymbolPaints;
a_Components = &SymbolComps;
// "Schematic" is used to edit usual schematic files (containing
// a schematic and a subcircuit symbol) and *.sym files (which
// contain *only* a symbol definition). If we're dealing with
// symbol file, then there is no need to create a subcircuit
// symbol, a symbol is already there.
if (!a_DocName.endsWith(".sym") && createSubcircuitSymbol()) {
updateAllBoundingRect();
setChanged(true, true);
}
emit signalUndoState(a_undoSymbolIdx != 0);
emit signalRedoState(a_undoSymbolIdx != a_undoSymbol.size() - 1);
} else {
a_Nodes = &a_DocNodes;
a_Wires = &a_DocWires;
a_Diagrams = &a_DocDiags;
a_Paintings = &a_DocPaints;
a_Components = &a_DocComps;
emit signalUndoState(a_undoActionIdx != 0);
emit signalRedoState(a_undoActionIdx != a_undoAction.size() - 1);
if (update)
reloadGraphs(); // load recent simulation data
}
}
// ---------------------------------------------------
void Schematic::setName(const QString &Name_)
{
a_DocName = Name_;
QFileInfo Info(a_DocName);
QString base = Info.completeBaseName();
QString ext = Info.suffix();
a_DataSet = base + ".dat";
a_Script = base + ".m";
if (ext != "dpl")
a_DataDisplay = base + ".dpl";
else
a_DataDisplay = base + ".sch";
}
// ---------------------------------------------------
// Sets the document to be changed or not to be changed.
void Schematic::setChanged(bool c, bool fillStack, char Op)
{
if ((!a_DocChanged) && c)
emit signalFileChanged(true);
else if (a_DocChanged && (!c))
emit signalFileChanged(false);
a_DocChanged = c;
a_showBias = -1; // schematic changed => bias points may be invalid
if (!fillStack)
return;
// ................................................
if (a_symbolMode) { // for symbol edit mode
while (a_undoSymbol.size() > a_undoSymbolIdx + 1) {
delete a_undoSymbol.last();
a_undoSymbol.pop_back();
}
a_undoSymbol.append(new QString(createSymbolUndoString(Op)));
a_undoSymbolIdx++;
emit signalUndoState(true);
emit signalRedoState(false);
while (static_cast<unsigned int>(a_undoSymbol.size())
> QucsSettings.maxUndo) { // "while..." because
delete a_undoSymbol.first();
a_undoSymbol.pop_front();
a_undoSymbolIdx--;
}
return;
}
// ................................................
// for schematic edit mode
while (a_undoAction.size() > a_undoActionIdx + 1) {
delete a_undoAction.last();
a_undoAction.pop_back();
}
if (Op == 'm') { // only one for move marker
if (a_undoAction.at(a_undoActionIdx)->at(0) == Op) {
delete a_undoAction.last();
a_undoAction.pop_back();
a_undoActionIdx--;
}
}
a_undoAction.append(new QString(createUndoString(Op)));
a_undoActionIdx++;
emit signalUndoState(true);
emit signalRedoState(false);
while (static_cast<unsigned int>(a_undoAction.size())
> QucsSettings.maxUndo) { // "while..." because
delete a_undoAction.first(); // "maxUndo" could be decreased meanwhile
a_undoAction.pop_front();
a_undoActionIdx--;
}
return;
}
// -----------------------------------------------------------
bool Schematic::sizeOfFrame(int &xall, int &yall)
{
// Values exclude border of 1.5cm at each side.
switch (a_showFrame) {
case 1:
xall = 1020;
yall = 765;
break; // DIN A5 landscape
case 2:
xall = 765;
yall = 1020;
break; // DIN A5 portrait
case 3:
xall = 1530;
yall = 1020;
break; // DIN A4 landscape
case 4:
xall = 1020;
yall = 1530;
break; // DIN A4 portrait
case 5:
xall = 2295;
yall = 1530;
break; // DIN A3 landscape
case 6:
xall = 1530;
yall = 2295;
break; // DIN A3 portrait
case 7:
xall = 1414;
yall = 1054;
break; // letter landscape
case 8:
xall = 1054;
yall = 1414;
break; // letter portrait
default:
return false;
}
return true;
}
void Schematic::paintFrame(QPainter* painter) {
// dimensions: X cm / 2.54 * 144
int frame_width, frame_height;
if (!sizeOfFrame(frame_width, frame_height))
return;
painter->save();
painter->setPen(QPen(Qt::darkGray, 1));
// Width of stripe along frame border in column and row labels are placed
const int frame_margin = painter->fontMetrics().lineSpacing() + 4;
// Outer rect
painter->drawRect(0, 0, frame_width, frame_height);
// a bit smaller than outer rect
painter->drawRect(frame_margin, frame_margin, frame_width - 2 * frame_margin, frame_height - 2 * frame_margin);
// Column labels
{
const int h_step = frame_width / ((frame_width + 127) / 255);
uint column_number = 1;
for (int x = h_step; x <= frame_width; x += h_step) {
painter->drawLine(x, 0, x, frame_margin);
painter->drawLine(x, frame_height - frame_margin, x, frame_height);
auto cn = QString::number(column_number);
auto tx = x - h_step / 2 + 5;
painter->drawText(tx, 3, 1, 1, Qt::TextDontClip, cn);
painter->drawText(tx, frame_height - frame_margin + 3, 1, 1, Qt::TextDontClip, cn);
column_number++;
}
}
// Row labels
{
const int v_step = frame_height / ((frame_height + 127) / 255);
char row_letter = 'A';
for (int y = v_step; y <= frame_height; y += v_step) {
painter->drawLine(0, y, frame_margin, y);
painter->drawLine(frame_width - frame_margin, y, frame_width, y);
auto rl = QString::fromLatin1(&row_letter, 1);
auto ty = y - v_step/2 + 5;
painter->drawText(5, ty, rl);
painter->drawText(frame_width - frame_margin + 5, ty, rl);
row_letter++;
}
}
// draw text box with text
int x1_ = frame_width - 340 - frame_margin;
int y1_ = frame_height - 3 - frame_margin;
int x2_ = frame_width - frame_margin - 3;
int y2_ = frame_height - frame_margin - 3;
const int d = 6;
const double z = 200.0;
y1_ -= painter->fontMetrics().lineSpacing() + d;
painter->drawLine(x1_, y1_, x2_, y1_);
painter->drawText(x1_ + d, y1_ + (d >> 1), 1, 1, Qt::TextDontClip, a_Frame_Text2);
painter->drawLine(x1_ + z, y1_, x1_ + z, y1_ + painter->fontMetrics().lineSpacing() + d);
painter->drawText(x1_ + d + z, y1_ + (d >> 1), 1, 1, Qt::TextDontClip, a_Frame_Text3);
y1_ -= painter->fontMetrics().lineSpacing() + d;
painter->drawLine(x1_, y1_, x2_, y1_);
painter->drawText(x1_ + d, y1_ + (d >> 1), 1, 1, Qt::TextDontClip, a_Frame_Text1);
y1_ -= (a_Frame_Text0.count('\n') + 1) * painter->fontMetrics().lineSpacing() + d;
painter->drawRect(x2_, y2_, x1_ - x2_ - 1, y1_ - y2_ - 1);
painter->drawText(x1_ + d, y1_ + (d >> 1), 1, 1, Qt::TextDontClip, a_Frame_Text0);
painter->restore();
}
// -----------------------------------------------------------
// Is called when the content (schematic or data display) has to be drawn.
void Schematic::drawContents(QPainter *p, int, int, int, int)
{
QTransform trf{p->transform()};
trf
.scale(a_Scale, a_Scale)
.translate(-a_ViewX1, -a_ViewY1);
p->setTransform(trf);
auto renderHints = p->renderHints();
renderHints
.setFlag(QPainter::Antialiasing)
.setFlag(QPainter::TextAntialiasing)
.setFlag(QPainter::SmoothPixmapTransform);
p->setRenderHints(renderHints);
p->setFont(QucsSettings.font);
drawGrid(p);
if (!a_symbolMode)
paintFrame(p);
drawElements(p);
if (a_showBias > 0) {
drawDcBiasPoints(p);
}
drawPostPaintEvents(p);
}
void Schematic::drawElements(QPainter* painter) {
for (auto* component : *a_Components) {
component->paint(painter);
}
for (auto* wire : *a_Wires) {
wire->paint(painter);
if (wire->Label) {
wire->Label->paint(painter); // separate because of paintSelected
}
}
for (auto* node : *a_Nodes) {
node->paint(painter);
if (node->Label) {
node->Label->paint(painter); // separate because of paintSelected
}
}
for (auto* diagram : *a_Diagrams) {
diagram->paint(painter);
}
for (auto* painting : *a_Paintings) {
painting->paint(painter);
}
}
void Schematic::drawDcBiasPoints(QPainter* painter) {
painter->save();
int x, y, z;
const int xOffset = 10;
const int yOffset = 10;
for (auto* pn : *a_Nodes) {
if (pn->Name.isEmpty())
continue;
QString value = misc::formatValue(pn->Name, 4);
x = pn->cx;
y = pn->cy + 4;
z = pn->x1;
QRect textRect = painter->fontMetrics().boundingRect(value);
int rectWidth = textRect.width() + 6;
int rectHeight = textRect.height() + 4;
if (z & 0x10) {
x += xOffset;
y -= yOffset;
} else {
x -= xOffset;
y -= yOffset;
}
int rectX = x - rectWidth / 2;
int rectY = y - rectHeight / 2;
painter->setBrush(QBrush(QColor(230,230,230)));
painter->setPen(Qt::NoPen);
painter->drawRoundedRect( QRectF(rectX, rectY, rectWidth, rectHeight),15,15,Qt::RelativeSize);
painter->setPen(z & 0x10 ? Qt::darkGreen : Qt::blue);
painter->drawText(x - textRect.width() / 2, y + textRect.height() / 4, value);
}
painter->restore();
}
void Schematic::drawPostPaintEvents(QPainter* painter) {
painter->save();
/*
* The following events used to be drawn from mouseactions.cpp, but since Qt4
* Paint actions can only be called from within the paint event, so they
* are put into a QList (PostedPaintEvents) and processed here
*/
for (auto p : a_PostedPaintEvents) {
// QPainter painter2(viewport()); for if(p.PaintOnViewport)
QPen pen(Qt::black);
painter->setPen(Qt::black);
switch (p.pe) {
case _NotRop:
painter->setCompositionMode(QPainter::RasterOp_SourceAndNotDestination);
break;
case _Rect:
painter->drawRect(p.x1, p.y1, p.x2, p.y2);
break;
case _SelectionRect:
pen.setCosmetic(true);
pen.setStyle(Qt::DashLine);
pen.setColor(QColor(50, 50, 50, 100));
painter->setPen(pen);
painter->fillRect(p.x1, p.y1, p.x2, p.y2, QColor(200, 220, 240, 100));
painter->drawRect(p.x1, p.y1, p.x2, p.y2);
break;
case _Line: {
painter->save();
QPen lp{painter->pen()};
lp.setWidth(p.a == 0 ? 1 : p.a);
painter->setPen(lp);
painter->drawLine(p.x1, p.y1, p.x2, p.y2);
painter->restore();
break;
}
case _Ellipse:
painter->drawEllipse(p.x1, p.y1, p.x2, p.y2);
break;
case _Arc:
painter->drawArc(p.x1, p.y1, p.x2, p.y2, p.a, p.b);
break;
case _DotLine:
painter->setPen(Qt::DotLine);
painter->drawLine(p.x1, p.y1, p.x2, p.y2);
break;
case _DotRect:
painter->setPen(Qt::DotLine);
painter->drawRect(p.x1, p.y1, p.x2, p.y2);
break;
case _Translate:; //painter2.translate(p.x1, p.y1);
case _Scale:; //painter2.scale(p.x1,p.y1);
break;
}
}
a_PostedPaintEvents.clear();
painter->restore();
}
void Schematic::PostPaintEvent(
PE pe, int x1, int y1, int x2, int y2, int a, int b, bool PaintOnViewport)
{
PostedPaintEvent p = {pe, x1, y1, x2, y2, a, b, PaintOnViewport};
a_PostedPaintEvents.push_back(p);
viewport()->update();
update();
}
// ---------------------------------------------------
void Schematic::contentsMouseMoveEvent(QMouseEvent *Event)
{
const QPoint modelPos = contentsToModel(Event->pos());
auto xpos = modelPos.x();
auto ypos = modelPos.y();
QString text = "";
auto doubleToString = [](bool condition, double number) {
return condition ? misc::num2str(number) : misc::StringNiceNum(number);
};
if (a_Diagrams == nullptr) return; // fix for crash on document closing; appears time to time
for (Diagram* diagram = a_Diagrams->last(); diagram != nullptr; diagram = a_Diagrams->prev()) {
// BUG: Obtaining the diagram type by name is marked as a bug elsewhere (to be solved separately).
// TODO: Currently only rectangular diagrams are supported.
if (diagram->getSelected(xpos, ypos) && diagram->Name == "Rect") {
bool hasY1, hasY2 = false;
for (auto graph: diagram->Graphs) {
hasY1 |= graph->yAxisNo == 0;
hasY2 |= graph->yAxisNo == 1;
}
QPointF mouseClickPoint = QPointF(xpos - diagram->cx, diagram->cy - ypos);
MappedPoint mp = diagram->pointToValue(mouseClickPoint);
auto _x = doubleToString(diagram->engineeringNotation, mp.x);
text = "X=" + _x;
if (hasY1) {
text.append("; Y1=");
auto _y1 = doubleToString(diagram->engineeringNotation, mp.y1);
text.append(_y1);
}
if (hasY2) {
text.append("; Y2=");
auto _y2 = doubleToString(diagram->engineeringNotation, mp.y2);
text.append(_y2);
}
break;
}
}
emit signalCursorPosChanged(xpos, ypos, text);
// Perform "pan with mouse"
if (Event->buttons() & Qt::MiddleButton) {
const QPoint currentCursorPosition = contentsToViewport(Event->pos());
const int dx = currentCursorPosition.x() - a_previousCursorPosition.x();
if (dx < 0) {
scrollRight(std::abs(dx));
} else if (dx > 0) {
scrollLeft(dx);
}
const int dy = currentCursorPosition.y() - a_previousCursorPosition.y();
if (dy < 0) {
scrollDown(std::abs(dy));
} else if (dy > 0) {
scrollUp(dy);
}
a_previousCursorPosition = currentCursorPosition;
}
if (a_App->MouseMoveAction)
(a_App->view->*(a_App->MouseMoveAction))(this, Event);
}
// -----------------------------------------------------------
void Schematic::contentsMousePressEvent(QMouseEvent *Event)
{
a_App->editText->setHidden(true); // disable text edit of component property
this->setFocus();
if (a_App->MouseReleaseAction == &MouseActions::MReleasePaste)
return;
const QPoint inModel = contentsToModel(Event->pos());
if (Event->button() == Qt::RightButton)
if (a_App->MousePressAction != &MouseActions::MPressElement)
if (a_App->MousePressAction != &MouseActions::MPressWire2) {
// show menu on right mouse button
a_App->view->rightPressMenu(this, Event, inModel.x(), inModel.y());
if (a_App->MouseReleaseAction)
// Is not called automatically because menu has focus.
(a_App->view->*(a_App->MouseReleaseAction))(this, Event);
return;
}
// Begin "pan with mouse" action. Panning starts if *only*
// the middle button is pressed.
if (Event->button() == Qt::MiddleButton) {
a_previousCursorPosition = contentsToViewport(Event->pos());
setCursor(Qt::ClosedHandCursor);
return;
}
if (a_App->MousePressAction)
(a_App->view->*(a_App->MousePressAction))(this, Event, inModel.x(), inModel.y());
}
// -----------------------------------------------------------
void Schematic::contentsMouseReleaseEvent(QMouseEvent *Event)
{
// End "pan with mouse" action.
if (Event->button() == Qt::MiddleButton) {
unsetCursor();
return;
}
if (a_App->MouseReleaseAction)
(a_App->view->*(a_App->MouseReleaseAction))(this, Event);
}
// -----------------------------------------------------------
void Schematic::contentsMouseDoubleClickEvent(QMouseEvent *Event)
{
if (a_App->MouseDoubleClickAction)
(a_App->view->*(a_App->MouseDoubleClickAction))(this, Event);
}
void Schematic::print(QPrinter*, QPainter* painter, bool printAll,
bool fitToPage, QMargins margins) {
painter->save();
const QRectF pageSize{0, 0, static_cast<double>(painter->device()->width()),
static_cast<double>(painter->device()->height())};
QRect printedArea = printAll ? allBoundingRect() : currentSelection().bounds;
if (printAll && a_showFrame) {
int frame_width, frame_height;
sizeOfFrame(frame_width, frame_height);
printedArea |= QRect{0, 0, frame_width, frame_height};
}
printedArea = printedArea.marginsAdded(margins);
double scale = 1.0;
if (fitToPage) {
scale = std::min(pageSize.width() / printedArea.width(),
pageSize.height() / printedArea.height());
} else {
QFontInfo printerFontInfo{QFont{QucsSettings.font, painter->device()}};
QFontInfo schematicFontInfo{QucsSettings.font};
scale = static_cast<double>(printerFontInfo.pixelSize()) /
static_cast<double>(schematicFontInfo.pixelSize());
}
painter->scale(scale, scale);
painter->translate(-printedArea.left(), -printedArea.top());
// put picture in center
{
auto w = pageSize.width() / scale;
if (printedArea.width() <= w) {
auto d = (w - printedArea.width()) / 2;
painter->translate(d, 0);
}
auto h = pageSize.height() / scale;
if (printedArea.height() <= h) {
auto d = (h - printedArea.height()) / 2;
painter->translate(0, d);
}
}
// User chose a font with size in points while looking at the font
// on screen. The same font size in *points* equals to different
// amount of pixels when shown on screen or printed on paper,
// because underlying painting devices have different resolutions.
// To preserve the ratio of text sizes and elements' sizes
// font size has to be set in pixels here. This makes lines, squares,
// circles, etc. and size of font be measured in same units and scale
// them equally.
auto f = QucsSettings.font;
QFontInfo fi{f};
f.setPixelSize(fi.pixelSize());
painter->setFont(f);
paintSchToViewpainter(painter, printAll);
painter->restore();
}
namespace {
// helper to be used in Schematic::paintSchToViewpainter
template <typename T> void draw_preserve_selection(T* elem, QPainter* p) {
bool selected = elem->isSelected;
elem->isSelected = false;
elem->paint(p);
elem->isSelected = selected;
}
} // namespace
void Schematic::paintSchToViewpainter(QPainter* painter, bool printAll) {
if (printAll && a_showFrame && !a_symbolMode) {
paintFrame(painter);
}
const auto should_draw = [=](Element* drawable) {
return printAll || drawable->isSelected;
};
for (auto* component : *a_Components) {
if (should_draw(component)) {
draw_preserve_selection(component, painter);
}
}
for (auto* wire : *a_Wires) {
if (should_draw(wire)) {
draw_preserve_selection(wire, painter);
}
if (auto* label = wire->Label) {
if (should_draw(label)) {
draw_preserve_selection(label, painter);
}
}
}
for (auto* node : *a_Nodes) {
for (auto* connected : *node) {
if (should_draw(connected)) {
draw_preserve_selection(node, painter);
break;
}
}
if (auto* label = node->Label) {
if (should_draw(label)) {
draw_preserve_selection(label, painter);
}
}
}
for (auto* painting : *a_Paintings) {
if (should_draw(painting)) {
draw_preserve_selection(painting, painter);
}
}
for (auto* diagram : *a_Diagrams) {
if (!should_draw(diagram)) {
continue;
}
// if graph or marker is selected, deselect during printing
for (Graph* pg : diagram->Graphs) {
if (pg->isSelected) {
pg->Type |= 1; // remember selection
}
pg->isSelected = false;
for (Marker* pm : pg->Markers) {
if (pm->isSelected) {
pm->Type |= 1; // remember selection
}
pm->isSelected = false;
}
}
draw_preserve_selection(diagram, painter);
// revert selection of graphs and markers
for (Graph* pg : diagram->Graphs) {
if (pg->Type & 1) {
pg->isSelected = true;
}
pg->Type &= -2;
for (Marker* pm : pg->Markers) {
if (pm->Type & 1) {
pm->isSelected = true;
}
pm->Type &= -2;
}
}
}
if (a_showBias > 0) { // show DC bias points in schematic ?
drawDcBiasPoints(painter);
}
}
void Schematic::zoomAroundPoint(double offeredScaleChange, QPoint coords, bool viewportRelative=true)
{
const double desiredScale = a_Scale * offeredScaleChange;
const auto viewportCoords =
viewportRelative ? coords : coords - QPoint{contentsX(), contentsY()};
const auto focusPoint = viewportToModel(viewportCoords);
const auto model = includePoint(modelRect(), focusPoint);
renderModel(desiredScale, model, focusPoint, viewportCoords);
}
// -----------------------------------------------------------
float Schematic::zoomBy(float s)
{
// Change scale and keep the point displayed in the center
// of the viewport at same place after scaling.
const double newScale = a_Scale * s;
const auto vpCenter = viewportRect().center();
const auto centerPoint = viewportToModel(vpCenter);
const auto model = includePoint(modelRect(), centerPoint);
return renderModel(newScale, model, centerPoint, vpCenter);
}
// ---------------------------------------------------
void Schematic::showAll()
{
sizeOfAll(a_UsedX1, a_UsedY1, a_UsedX2, a_UsedY2);
if (a_UsedX1 == 0)
if (a_UsedX2 == 0)
if (a_UsedY1 == 0)
if (a_UsedY2 == 0) {
a_UsedX1 = a_UsedY1 = INT_MAX;
a_UsedX2 = a_UsedY2 = INT_MIN;
return;
}
// Reshape model plane to cut off unused parts
constexpr int margin = 40;
QRect newModelBounds = modelRect();
newModelBounds.setLeft(a_UsedX1 - margin);
newModelBounds.setTop(a_UsedY1 - margin);
newModelBounds.setRight(a_UsedX2 + margin);
newModelBounds.setBottom(a_UsedY2 + margin);
// The shape of the model plane may not fit the shape of the viewport,
// so we looking for a scale value which enables to fit the whole model
// into the viewport
const double xScale = static_cast<double>(viewport()->width()) /
static_cast<double>(newModelBounds.width());
const double yScale = static_cast<double>(viewport()->height()) /
static_cast<double>(newModelBounds.height());
const double newScale = std::min(xScale, yScale);
renderModel(newScale, newModelBounds, newModelBounds.center(), viewportRect().center());
}
// ------------------------------------------------------
void Schematic::zoomToSelection() {
sizeOfAll(a_UsedX1, a_UsedY1, a_UsedX2, a_UsedY2);
if (a_UsedX1 == 0)
if (a_UsedX2 == 0)
if (a_UsedY1 == 0)
if (a_UsedY2 == 0) {
a_UsedX1 = a_UsedY1 = INT_MAX;
a_UsedX2 = a_UsedY2 = INT_MIN;
// No elements present – nothing can be selected; quit
return;
}
const QRect selectedBoundingRect{ currentSelection().bounds };
// Working with raw coordinates is clumsy, abstract them out
const QRect usedBoundingRect{a_UsedX1, a_UsedY1, a_UsedX2 - a_UsedX1, a_UsedY2 - a_UsedY1};
if (selectedBoundingRect.width() == 0 || selectedBoundingRect.height() == 0) {
// If nothing is selected, then what should be shown? Probably it's best
// to do nothing.
return;
}
// While we here, lets reshape model plane to cut off unused parts
constexpr int margin = 40;
QRect modelBounds = modelRect();
modelBounds.setLeft(usedBoundingRect.left() - margin);