-
Notifications
You must be signed in to change notification settings - Fork 1
/
TraceEditor.cpp
730 lines (655 loc) · 17 KB
/
TraceEditor.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
/*
Copyright (C) 2010-2011 Igor Izyumin
This file is part of xpcb.
xpcb 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 3 of the License, or
(at your option) any later version.
xpcb is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with xpcb. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QPoint>
#include "TraceEditor.h"
#include "Document.h"
#include "SegmentLayerDialog.h"
#include "SegmentWidthDialog.h"
using namespace XPcb;
// utility functions
inline bool segParallel(QSharedPointer<Segment> seg1,
QSharedPointer<Segment> seg2)
{
return isParallel(seg1->v1()->pos() - seg1->v2()->pos(),
seg2->v1()->pos() - seg2->v2()->pos());
}
inline bool segZeroLength(QSharedPointer<Segment> seg)
{
return seg->v1()->pos() == seg->v2()->pos();
}
///////////////////////////////////////////////////////////////////////////////
NewTraceEditor::NewTraceEditor(Controller *ctrl)
: AbstractEditor(ctrl), mCtrl(ctrl), mState(PICK_START),
mMode(ModeStraight45), mWidth(XPcb::milToPcb(10)),
mLayer(Layer::LAY_TOP_COPPER)
{
}
void NewTraceEditor::drawOverlay(QPainter* painter)
{
Controller::drawCrosshair45(painter, mPos);
if (mState == PICK_END)
{
mSeg1->draw(painter, mLayer);
mSeg2->draw(painter, mLayer);
mVtxStart->draw(painter, mLayer);
mVtxMid->draw(painter, mLayer);
mVtxEnd->draw(painter, mLayer);
}
}
void NewTraceEditor::init()
{
emit actionsChanged();
}
QList<const CtrlAction*> NewTraceEditor::actions() const
{
return QList<const CtrlAction*>();
}
void NewTraceEditor::mouseMoveEvent(QMouseEvent *event)
{
mPos = mCtrl->snapToRouteGrid(mCtrl->view()->transform().inverted()
.map(event->pos()));
if (mState == PICK_END)
{
mVtxEnd->setPos(mPos);
updateDogleg();
}
emit overlayChanged();
}
void NewTraceEditor::mousePressEvent(QMouseEvent *event)
{
event->accept();
}
void NewTraceEditor::mouseReleaseEvent(QMouseEvent *event)
{
if (mState == PICK_START)
{
// TODO check if a segment or vertex is already present
// create all the pieces
mVtxStart = QSharedPointer<Vertex>(new Vertex(mPos));
mVtxMid = QSharedPointer<Vertex>(new Vertex(mPos));
mVtxEnd = QSharedPointer<Vertex>(new Vertex(mPos));
mSeg1 = QSharedPointer<Segment>(new Segment(mLayer, mWidth));
mSeg2 = QSharedPointer<Segment>(new Segment(mLayer, mWidth));
mSeg1->setV1(mVtxStart);
mSeg1->setV2(mVtxMid);
mSeg2->setV1(mVtxMid);
mSeg2->setV2(mVtxEnd);
mState = PICK_END;
}
else if (mState == PICK_END)
{
// check for null segment
if (mVtxStart->pos() == mVtxEnd->pos())
{
event->accept();
return;
}
QUndoCommand* cmd = dynamic_cast<PCBDoc*>(mCtrl->doc())->traceList()
->addSegmentCmd(mSeg1, mVtxStart, mVtxMid);
mCtrl->doc()->doCommand(cmd);
mSeg1 = mSeg2;
mVtxStart = mVtxMid;
mVtxMid = mVtxEnd;
mVtxEnd = QSharedPointer<Vertex>(new Vertex(mPos));
mSeg2 = QSharedPointer<Segment>(new Segment(mLayer, mWidth));
mSeg2->setV1(mVtxMid);
mSeg2->setV2(mVtxEnd);
toggleMode();
}
event->accept();
}
void NewTraceEditor::keyPressEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_Escape)
emit editorFinished();
}
void NewTraceEditor::updateDogleg()
{
QPoint start = mVtxStart->pos();
QPoint end = mVtxEnd->pos();
QPoint d = end - start;
if (mMode == ModeStraight45)
{
if (abs(d.x()) > abs(d.y()))
mVtxMid->setPos(QPoint(end.x() - sign(d.x())*abs(d.y()),
start.y()));
else
mVtxMid->setPos(QPoint(start.x(),
end.y() - sign(d.y())*abs(d.x())));
}
else if (mMode == Mode45Straight)
{
if (abs(d.x()) > abs(d.y()))
mVtxMid->setPos(QPoint(start.x() + sign(d.x()) * abs(d.y()),
end.y()));
else
mVtxMid->setPos(QPoint(end.x(),
start.y() + sign(d.y()) * abs(d.x())));
}
}
void NewTraceEditor::toggleMode()
{
mMode = (mMode == Mode45Straight) ? ModeStraight45 : Mode45Straight;
}
///////////////////////////////////////////////////////////////////////////////
SegmentEditor::SegmentEditor(Controller *ctrl,
QSharedPointer<Segment> segment)
: AbstractEditor(ctrl), mState(SELECTED), mCtrl(ctrl), mSegment(segment),
mSlideAction(3, "Slide Segment"), mAddVtxAction(2, "Add Vertex"),
mDelAction(7, "Ripup Segment"), mSetLayerAction(1, "Change Layer"),
mSetWidthAction(0, "Change Width")
{
connect(&mSlideAction, SIGNAL(execFired()),
this, SLOT(startSlideSegment()));
connect(&mAddVtxAction, SIGNAL(execFired()),
this, SLOT(startInsertVtx()));
connect(&mDelAction, SIGNAL(execFired()),
this, SLOT(deleteSegment()));
connect(&mSetLayerAction, SIGNAL(execFired()),
this, SLOT(setLayer()));
connect(&mSetWidthAction, SIGNAL(execFired()),
this, SLOT(setWidth()));
ctrl->hideObj(mSegment);
}
void SegmentEditor::drawOverlay(QPainter* painter)
{
if (mState == ADD_VTX || mState == SLIDE)
Controller::drawCrosshair45(painter, mPos);
if (mState == SELECTED)
{
mSegment->draw(painter, Layer::LAY_SELECTION);
}
else if (mState == SLIDE)
{
if (mSeg1)
painter->drawLine(mFixedPt1, mPt1);
painter->drawLine(mPt1, mPt2);
if (mSeg2)
painter->drawLine(mFixedPt2, mPt2);
}
else if (mState == ADD_VTX)
{
painter->drawLine(mPt1, mPos);
painter->drawLine(mPos, mPt2);
}
}
void SegmentEditor::init()
{
emit actionsChanged();
}
QList<const CtrlAction*> SegmentEditor::actions() const
{
QList<const CtrlAction*> out;
if (mState == SELECTED)
out << &mSlideAction << &mAddVtxAction
<< &mDelAction << &mSetLayerAction
<< &mSetWidthAction;
return out;
}
void SegmentEditor::mouseMoveEvent(QMouseEvent *event)
{
QPoint pos = mCtrl->snapToRouteGrid(mCtrl->view()->transform().inverted()
.map(event->pos()));
if (mState == SLIDE)
{
mPos = pos;
updateSlide();
emit overlayChanged();
}
else if (mState == ADD_VTX)
{
mPos = pos;
emit overlayChanged();
}
}
void SegmentEditor::mousePressEvent(QMouseEvent *event)
{
if (mState == SLIDE || mState == ADD_VTX)
{
event->accept();
}
else
{
event->ignore();
}
}
void SegmentEditor::mouseReleaseEvent(QMouseEvent *event)
{
if (mState == SLIDE)
{
event->accept();
finishSlide();
}
else if (mState == ADD_VTX)
{
event->accept();
finishAddVtx();
}
else
{
event->ignore();
}
}
void SegmentEditor::keyPressEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_Escape)
{
if (mState == SLIDE)
abortSlide();
else
mState = SELECTED;
emit actionsChanged();
emit overlayChanged();
}
}
void SegmentEditor::deleteSegment()
{
QUndoCommand *cmd = dynamic_cast<PCBDoc*>(mCtrl->doc())->traceList()->removeSegmentCmd(mSegment);
// exec the command
mCtrl->doc()->doCommand(cmd);
emit editorFinished();
}
void SegmentEditor::setLayer()
{
SegmentLayerDialog dlg(mCtrl->view(), mCtrl);
dlg.init(mSegment.data());
PCBObjState prev = mSegment->getState();
if (dlg.exec() == QDialog::Accepted)
{
// XXX TODO apply to connected segments if needed
mSegment->setLayer(dlg.layer());
PCBObjEditCmd* cmd = new PCBObjEditCmd(0, mSegment, prev);
mCtrl->doc()->doCommand(cmd);
}
}
void SegmentEditor::setWidth()
{
SegmentWidthDialog dlg(mCtrl->view());
dlg.init(mSegment.data());
PCBObjState prev = mSegment->getState();
if (dlg.exec() == QDialog::Accepted)
{
// XXX TODO apply to connected segments if needed
mSegment->setWidth(dlg.width().toPcb());
PCBObjEditCmd* cmd = new PCBObjEditCmd(0, mSegment, prev);
mCtrl->doc()->doCommand(cmd);
}
}
void SegmentEditor::startSlideSegment()
{
// get the neighboring segments
// first, get the neighboring segment vertices
QSharedPointer<Vertex> v1 = mSegment->v1();
QSharedPointer<Vertex> v2 = mSegment->v2();
// find the other segments
QSet<QSharedPointer<Segment> > v1s = v1->segments();
v1s.remove(mSegment);
QSet<QSharedPointer<Segment> > v2s = v2->segments();
v2s.remove(mSegment);
if (v1s.size() > 1 || v2s.size() > 1)
{
// more than 1 neighbor, cannot move (yet)
return;
}
mPt1 = v1->pos();
mPt2 = v2->pos();
mSecantVec = mPt2 - mPt1;
if (v1s.size() == 0)
{
mSeg1.clear();
mVector1 = perp(mSecantVec);
mFixedPt1 = mPt1;
}
else
{
mSeg1 = v1s.values().first();
mCtrl->hideObj(mSeg1);
mFixedPt1 = mSeg1->otherVertex(v1)->pos();
mVector1 = mPt1 - mFixedPt1;
}
if (v2s.size() == 0)
{
// if there are no neighboring segments,
// make fake lines perpendicular to the secant
mSeg2.clear();
mVector2 = perp(mSecantVec);
mFixedPt2 = mPt2;
}
else
{
mSeg2 = v2s.values().first();
mCtrl->hideObj(mSeg2);
mFixedPt2 = mSeg2->otherVertex(v2)->pos();
mVector2 = mPt2 - mFixedPt2;
}
mSignX = sign(mSecantVec.x());
mSignY = sign(mSecantVec.y());
mState = SLIDE;
emit actionsChanged();
}
void SegmentEditor::startInsertVtx()
{
mPt1 = mSegment->v1()->pos();
mPt2 = mSegment->v2()->pos();
mState = ADD_VTX;
}
void SegmentEditor::updateSlide()
{
QPoint pos = mPos;
forever
{
double sc = lineIntersect(mFixedPt1, mVector1, pos, mSecantVec);
QPoint newpt1(mFixedPt1 + mVector1 * sc);
// make sure neighboring segment length is >= 0
if (sc < 0 && mSeg1)
{
// constrain the position and try again
pos = mFixedPt1;
continue;
}
// repeat this exercise for segment 2
sc = lineIntersect(mFixedPt2, mVector2, pos, mSecantVec);
QPoint newpt2(mFixedPt2 + mVector2 * sc);
if (sc < 0 && mSeg2)
{
pos = mFixedPt2;
continue;
}
// ensure this segment length is >=0
short sx = sign((newpt2 - newpt1).x());
short sy = sign((newpt2 - newpt1).y());
if ( (sx && mSignX != sx) ||
(sy && mSignY != sy))
{
// limit to intersection of neighboring segments
pos = lineIntersectPt(mFixedPt1, mVector1, mFixedPt2, mVector2);
continue;
}
// everything is ok, update the points and don't loop again
mPt1 = newpt1;
mPt2 = newpt2;
break;
}
}
struct SegListEntry
{
SegListEntry(QSharedPointer<Segment> s,
QSharedPointer<Vertex> vs,
QSharedPointer<Vertex> ve)
: seg(s), start(vs), end(ve) {}
QSharedPointer<Segment> seg;
QSharedPointer<Vertex> start;
QSharedPointer<Vertex> end;
};
// cleans up the trace (removes collinear and zero-length segments)
void SegmentEditor::cleanUpTrace(QList<QSharedPointer<Segment> > segments,
QUndoCommand* parent)
{
if (segments.length() < 2) return;
QSharedPointer<TraceList> tl = dynamic_cast<PCBDoc*>(mCtrl->doc())->traceList();
// list of nonzero length segments and their start vertices
QList<SegListEntry> nzList;
// start vertex for current segment
QSharedPointer<Vertex> startVtx =
segments[0]->otherVertex(segments[0]->commonVertex(segments[1]));
foreach(QSharedPointer<Segment> s, segments)
{
// remove all zero length segments
if (segZeroLength(s))
{
// remove segment
tl->removeSegmentCmd(s, parent);
}
else
{
if (!nzList.isEmpty() && segParallel(nzList.last().seg, s))
{
// Segment is collinear with previous nonzero segment.
// Remove the previous segment, change the start vertex of this
// one to the start of the previous one, pop it off the list,
// and replace the entry with this segment.
QSharedPointer<Vertex> prevVtx = nzList.last().start;
tl->swapVtxCmd(s, startVtx, prevVtx, parent);
tl->removeSegmentCmd(nzList.last().seg, parent);
nzList.removeLast();
nzList.append(SegListEntry(s, prevVtx,
s->otherVertex(startVtx)));
}
else
{
if (!nzList.isEmpty() && nzList.last().end != startVtx)
{
// There is a zero length segment that was removed.
// Bypass the missing vertex.
tl->swapVtxCmd(s, startVtx, nzList.last().end, parent);
nzList.append(SegListEntry(s, nzList.last().end,
s->otherVertex(startVtx)));
}
else
// Everything squares up. Append the entry for this segment.
nzList.append(SegListEntry(s, startVtx,
s->otherVertex(startVtx)));
}
}
// Traverse the list of vertices.
startVtx = s->otherVertex(startVtx);
}
}
void SegmentEditor::finishSlide()
{
QUndoCommand *parent = new QUndoCommand("slide segment");
// move the points and create undo commands
QSharedPointer<Vertex> v1 = mSegment->v1();
QSharedPointer<Vertex> v2 = mSegment->v2();
PCBObjState st1 = v1->getState();
PCBObjState st2 = v2->getState();
v1->setPos(mPt1);
v2->setPos(mPt2);
new PCBObjEditCmd(parent, v1, st1);
new PCBObjEditCmd(parent, v2, st2);
// make a list for the cleanup function
QList<QSharedPointer<Segment> > l;
if (mSeg1)
{
// get the adjoining segment to seg1
QSet<QSharedPointer<Segment> > sl = mSeg1->otherVertex(
mSeg1->commonVertex(mSegment))->segments();
sl.remove(mSeg1);
if (!sl.isEmpty())
l.append(sl.values().first());
l.append(mSeg1);
}
l.append(mSegment);
if (mSeg2)
{
l.append(mSeg2);
// get the adjoining segment to seg2
QSet<QSharedPointer<Segment> > sl = mSeg2->otherVertex(
mSeg2->commonVertex(mSegment))->segments();
sl.remove(mSeg2);
if (!sl.isEmpty())
l.append(sl.values().first());
}
cleanUpTrace(l, parent);
// unhide objs
if (mSeg1)
mCtrl->unhideObj(mSeg1);
if (mSeg2)
mCtrl->unhideObj(mSeg2);
// execute command and reset state
mCtrl->doc()->doCommand(parent);
if (!dynamic_cast<PCBDoc*>(mCtrl->doc())->traceList()->segments().contains(mSegment))
{
emit editorFinished();
return;
}
mState = SELECTED;
emit actionsChanged();
emit overlayChanged();
}
void SegmentEditor::abortSlide()
{
if (mSeg1)
mCtrl->unhideObj(mSeg1);
if (mSeg2)
mCtrl->unhideObj(mSeg2);
mSeg1.clear();
mSeg2.clear();
mState = SELECTED;
}
void SegmentEditor::finishAddVtx()
{
QUndoCommand *parent = new QUndoCommand("insert vertex");
QSharedPointer<Vertex> v2 = mSegment->v2();
// create a new vertex
QSharedPointer<Vertex> vnew(new Vertex(mPos));
// create a new segment
QSharedPointer<Segment> snew(mSegment->clone());
// make the modifications
QSharedPointer<TraceList> tl = dynamic_cast<PCBDoc*>(mCtrl->doc())->traceList();
tl->swapVtxCmd(mSegment, v2, vnew, parent);
tl->addSegmentCmd(snew, vnew, v2, parent);
mCtrl->doc()->doCommand(parent);
mState = SELECTED;
emit actionsChanged();
emit overlayChanged();
}
///////////////////////////////////////////////////////////////////////////////
VertexEditor::VertexEditor(Controller *ctrl, QSharedPointer<Vertex> vtx)
: AbstractEditor(ctrl), mState(SELECTED), mCtrl(ctrl), mVtx(vtx),
mMoveAction(3, "Move Vertex"), mDelAction(7, "Delete Vertex")
{
connect(&mMoveAction, SIGNAL(execFired()),
this, SLOT(startMove()));
connect(&mDelAction, SIGNAL(execFired()),
this, SLOT(deleteVtx()));
mPos = mVtx->pos();
}
void VertexEditor::drawOverlay(QPainter* painter)
{
if (mState == MOVE)
Controller::drawCrosshair45(painter, mPos);
if (mState == SELECTED)
{
double handSize = 10.0 / ctrl()->view()->transform().m11();
painter->save();
painter->setBrush(Layer::color(Layer::LAY_SELECTION));
QPen pen = painter->pen();
pen.setWidth(0);
painter->setPen(pen);
painter->drawRect(mPos.x() - handSize/2,
mPos.y() - handSize/2,
handSize, handSize);
painter->restore();
}
}
void VertexEditor::init()
{
emit actionsChanged();
}
QList<const CtrlAction*> VertexEditor::actions() const
{
QList<const CtrlAction*> out;
if (mState == SELECTED)
{
out << &mMoveAction;
out << &mDelAction;
}
return out;
}
void VertexEditor::mouseMoveEvent(QMouseEvent *event)
{
if (mState == MOVE)
{
mPos = mCtrl->snapToRouteGrid(mCtrl->view()->transform().inverted()
.map(event->pos()));
updateMove();
emit overlayChanged();
}
}
void VertexEditor::mousePressEvent(QMouseEvent *event)
{
if (mState == MOVE)
{
event->accept();
}
else
{
event->ignore();
}
}
void VertexEditor::mouseReleaseEvent(QMouseEvent *event)
{
if (mState == MOVE)
{
event->accept();
finishMove();
}
else
{
event->ignore();
}
}
void VertexEditor::keyPressEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_Escape && mState == MOVE)
{
abortMove();
}
}
void VertexEditor::startMove()
{
mPrevState = mVtx->getState();
mState = MOVE;
emit actionsChanged();
emit overlayChanged();
}
void VertexEditor::updateMove()
{
mVtx->setPos(mPos);
}
void VertexEditor::finishMove()
{
PCBObjEditCmd* cmd = new PCBObjEditCmd(NULL, mVtx, mPrevState);
mCtrl->doc()->doCommand(cmd);
mState = SELECTED;
emit actionsChanged();
emit overlayChanged();
}
void VertexEditor::abortMove()
{
mVtx->loadState(mPrevState);
mState = SELECTED;
mPos = mVtx->pos();
emit actionsChanged();
emit overlayChanged();
}
void VertexEditor::deleteVtx()
{
QList<QSharedPointer<Segment> > segs = mVtx->segments().toList();
// do not delete tees or vertices with only one segment
if (segs.size() != 2)
return;
QSharedPointer<TraceList> tl = dynamic_cast<PCBDoc*>(mCtrl->doc())->traceList();
QSharedPointer<Vertex> v2 = segs[1]->otherVertex(mVtx);
QUndoCommand *parent = new QUndoCommand("delete vertex");
// reattach the first segment
tl->swapVtxCmd(segs[0], mVtx, v2, parent);
// delete the second segment
tl->removeSegmentCmd(segs[1], parent);
// exec the command
mCtrl->doc()->doCommand(parent);
emit editorFinished();
}