-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpcbview.cpp
More file actions
157 lines (139 loc) · 4.97 KB
/
pcbview.cpp
File metadata and controls
157 lines (139 loc) · 4.97 KB
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
#include "pcbviewscene.h"
#include "pcbview.h"
#include <QDebug>
#include <QScrollBar>
#include <QGraphicsItem>
#include <QFileDialog>
#include <QGraphicsSvgItem>
#include <QSvgRenderer>
PCBView::PCBView(QWidget *parent) :
QGraphicsView(parent) {
setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
setDragMode(QGraphicsView::NoDrag);
_scene = new PCBViewScene(parent);
_scene->setBackgroundBrush( QBrush(Qt::black, Qt::SolidPattern) );
setScene(_scene);
_pan = false;
_silkLayer = new BoardLayer();
_copperLayer = new BoardLayer();
PCBViewScene *_scene = (PCBViewScene*)scene();
_scene->setBackgroundBrush( QBrush(Qt::black, Qt::SolidPattern) );
// setInteractive(false);
}
QPointF PCBView::mapComponentToScene(QPointF pos) {
float w0 = _refPointsScene[0].x();
float u0 = _refPointsScene[0].y();
float w1 = _refPointsScene[1].x();
float u1 = _refPointsScene[1].y();
float x0 = _refPointsComponent[0].x();
float y0 = _refPointsComponent[0].y();
float x1 = _refPointsComponent[1].x();
float y1 = _refPointsComponent[1].y();
float kx = (w1-w0)/(x1-x0);
float ky = (u1-u0)/(y1-y0);
float x = pos.x();
float y = pos.y();
float w = w0 + (x - x0)*kx;
float u = u0 + (y - y0)*ky;
return QPointF(w, u);
}
void PCBView::wheelEvent(QWheelEvent *event) {
float zoomFactor = 1.20f;
setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
if( event->delta() > 0 ) {
scale(zoomFactor, zoomFactor);
}
else {
scale(1/zoomFactor,1/zoomFactor);
}
}
void PCBView::mousePressEvent(QMouseEvent *event) {
if( event->button() == Qt::RightButton ) {
_pan = true;
_panStart = event->pos();
setCursor(Qt::ClosedHandCursor);
}
else if( event->button() == Qt::LeftButton ) {
_scene->clearMarkers();
QPoint clickPos = event->pos();
QPointF scenePos = mapToScene( clickPos );
_scene->addMarker(scenePos);
_lastClickPos = scenePos;
}
}
void PCBView::mouseReleaseEvent(QMouseEvent *event) {
if( event->button() == Qt::RightButton ) {
_pan = false;
setCursor(Qt::ArrowCursor);
}
}
void PCBView::mouseMoveEvent(QMouseEvent *event) {
QGraphicsView::mouseMoveEvent(event);
if (_pan) {
setTransformationAnchor(QGraphicsView::NoAnchor);
horizontalScrollBar()->setValue(horizontalScrollBar()->value() - (event->x() - _panStart.x()) );
verticalScrollBar()->setValue(verticalScrollBar()->value() - (event->y() - _panStart.y()));
_panStart = event->pos();
}
}
void PCBView::paintEvent(QPaintEvent *event) {
QGraphicsView::paintEvent(event);
}
void PCBView::onComponentsSelected(QList<BOMEntry> selectedEntries) {
if( _refPointsScene[0].isNull() || _refPointsScene[1].isNull() ) {
return;
}
_scene->clearMarkers();
float xmin, xmax, ymin, ymax;
for( int i=0; i<selectedEntries.length(); i++ ) {
BOMEntry _entry = selectedEntries.at(i);
QPointF compPos = _entry.position();
QPointF scenePos = mapComponentToScene(compPos);
_scene->addMarker(scenePos);
if( i==0 ) {
xmin = scenePos.x();
xmax = scenePos.x();
ymin = scenePos.y();
ymax = scenePos.y();
}
else {
if( scenePos.x() > xmax ) xmax = scenePos.x();
if( scenePos.x() < xmin ) xmin = scenePos.x();
if( scenePos.y() > ymax ) ymax = scenePos.y();
if( scenePos.y() < ymin ) ymin = scenePos.y();
}
if( selectedEntries.length() == 1 ) {
ensureVisible( _scene->firstMarker(), 150, 150 );
}
else {
fitInView( xmin-75, ymin-75, 150+xmax-xmin, 150+ymax-ymin, Qt::KeepAspectRatio );
}
}
}
void PCBView::onSetRefPoint(int index, QPointF pos) {
if( (index == 0 || index == 1) && !_lastClickPos.isNull() ) {
_refPointsScene[index] = _lastClickPos;
_refPointsComponent[index] = pos;
qDebug() << "Set reference" << index;
qDebug() << "Scene position:" << _lastClickPos;
qDebug() << "BOM position:" << pos;
}
}
void PCBView::onLoadBoardFile() {
QString filters("SVG files (*.svg)");
QString silkFilename = QFileDialog::getOpenFileName(this, "Load silkscreen file", "", filters);
QString copperFilename = QFileDialog::getOpenFileName(this, "Load copper file", "", filters);
if( !copperFilename.isNull() ) {
_copperLayer->loadSvg(copperFilename);
_copperLayer->setColor(QColor(255,0,0));
_scene->addItem(_copperLayer->pixmapItem);
}
if( !silkFilename.isNull() ) {
_silkLayer->loadSvg(silkFilename);
_silkLayer->setColor(QColor(0,255,0));
_scene->addItem(_silkLayer->pixmapItem);
}
}
void PCBView::onRotateView180() {
rotate(180.0);
}