-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPCBView.cpp
274 lines (249 loc) · 6.86 KB
/
PCBView.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
/*
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 "global.h"
#include "PCBView.h"
#include "Document.h"
#include "Log.h"
#include "Controller.h"
#include <QSize>
#include <QPainter>
#include <QMouseEvent>
#include <QSettings>
PCBView::PCBView(QWidget *parent)
: QWidget(parent), mCtrl(NULL), mWheelAngle(0)
{
// initialize transform
// 100 pixels = 1 inch
mTransform.translate(0, 300);
mTransform.scale(100.0/XPcb::inchToPcb(1), -100.0/XPcb::inchToPcb(1));
mVisibleGrid = XPcb::inchToPcb(0.1);
setMouseTracking(true);
setFocusPolicy(Qt::StrongFocus);
}
PCBView::~PCBView()
{
}
QSize PCBView::sizeHint() const
{
return QSize(800,600);
}
void PCBView::setCtrl(Controller *ctrl)
{
mCtrl = ctrl;
}
void PCBView::visGridChanged(int grid)
{
this->mVisibleGrid = grid;
update();
}
void PCBView::paintEvent(QPaintEvent *e)
{
QPainter painter(this);
// erase background
painter.setBackground(QBrush(Layer::color(Layer::LAY_BACKGND)));
painter.setClipping(true);
painter.eraseRect(this->rect());
// draw document
if (mCtrl && mCtrl->docIsOpen())
{
QList<Layer> layers = mCtrl->doc()
->layerList(PCBDoc::DrawPriorityOrder);
// draw grid
QPen pen(Layer::color(Layer::LAY_VISIBLE_GRID));
pen.setCapStyle(Qt::RoundCap);
pen.setJoinStyle(Qt::RoundJoin);
painter.setTransform(mTransform);
painter.setPen(pen);
if (mCtrl->isLayerVisible(Layer::LAY_VISIBLE_GRID))
{
drawOrigin(&painter);
drawGrid(&painter);
}
// turn on AA
painter.setRenderHint(QPainter::Antialiasing);
// set transform
QRect bb = mTransform.inverted().mapRect(e->rect());
// figure out active layer
Layer active = mCtrl->activeLayer();
// draw the layers
bool activeDrawn = false;
QListIterator<Layer> i(layers);
while(i.hasNext())
{
Q_ASSERT(painter.isActive());
Layer curr;
// find first copper layer to draw current active layer
if (!activeDrawn && !i.peekNext().isCopper()
&& i.hasPrevious() && i.peekPrevious().isCopper())
{
curr = active;
activeDrawn = true;
}
else
{
curr = i.next();
if (curr == active)
continue; // will draw later
}
if (mCtrl->isLayerVisible(curr))
{
// set color / fill
QPen p = painter.pen();
p.setColor(curr.color());
p.setWidth(0);
painter.setPen(p);
QBrush b = painter.brush();
b.setColor(curr.color());
if (curr.type() != Layer::LAY_SELECTION)
b.setStyle(Qt::SolidPattern);
else
b.setStyle(Qt::NoBrush);
painter.setBrush(b);
// tell controller to draw it
mCtrl->draw(&painter, bb, curr);
}
}
}
painter.end();
}
void PCBView::drawOrigin(QPainter *painter)
{
// circle with 4 lines
painter->drawEllipse(XPcb::inchToPcb(-0.05),XPcb::inchToPcb(-0.05),
XPcb::inchToPcb(0.1), XPcb::inchToPcb(0.1));
painter->drawLine(XPcb::inchToPcb(0.05), 0, XPcb::inchToPcb(0.25), 0);
painter->drawLine(0, XPcb::inchToPcb(0.05), 0, XPcb::inchToPcb(0.25));
painter->drawLine(XPcb::inchToPcb(-0.05), 0, XPcb::inchToPcb(-0.25), 0);
painter->drawLine(0, XPcb::inchToPcb(-0.05), 0, XPcb::inchToPcb(-0.25));
}
void PCBView::drawGrid(QPainter *painter)
{
// painter->drawRect(QRect(QPoint(-PCB_BOUND, -PCB_BOUND), QPoint(PCB_BOUND, PCB_BOUND)));
QRect viewport = mTransform.inverted().mapRect(this->rect());
if (mTransform.map(QLine(QPoint(0,0), QPoint(mVisibleGrid, 0))).dx() <= 5)
return;
int startX = (viewport.x() / mVisibleGrid) * mVisibleGrid;
int startY = (viewport.y() / mVisibleGrid) * mVisibleGrid;
int endX = viewport.x() + viewport.width();
int endY = viewport.y() + viewport.height();
for(int x = startX; x <= endX; x += mVisibleGrid)
for(int y = startY; y <= endY; y += mVisibleGrid)
painter->drawPoint(x, y);
}
void PCBView::mouseMoveEvent(QMouseEvent * event)
{
mMousePos = event->pos();
emit mouseMoved(mTransform.inverted().map(mMousePos));
}
void PCBView::mousePressEvent(QMouseEvent * event)
{
event->ignore();
}
void PCBView::mouseReleaseEvent(QMouseEvent * event)
{
event->ignore();
}
void PCBView::wheelEvent(QWheelEvent *event)
{
mWheelAngle += event->delta();
if (mWheelAngle >= 120)
{
// zoom in
mWheelAngle -= 120;
zoom(1.25, event->pos());
}
else if (mWheelAngle <= -120)
{
// zoom out
mWheelAngle += 120;
zoom(0.8, event->pos());
}
}
void PCBView::keyPressEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_Space)
{
recenter(mMousePos);
}
else
event->ignore();
}
void PCBView::enterEvent(QEvent */*event*/)
{
setFocus(Qt::MouseFocusReason);
}
void PCBView::leaveEvent(QEvent */*event*/)
{
clearFocus();
}
/// Recenter view around pt (in screen coords by default)
void PCBView::recenter(QPoint pt, bool world)
{
if (!mCtrl || !mCtrl->docIsOpen()) return;
QPoint ctr = mTransform.inverted().map(this->rect().center());
if (!world)
pt = mTransform.inverted().map(pt);
ctr -= pt;
mTransform.translate(ctr.x(), ctr.y());
// check if the viewport is out of bounds
QRect bound = QRect(QPoint(-XPcb::PCB_BOUND, -XPcb::PCB_BOUND),
QPoint(XPcb::PCB_BOUND, XPcb::PCB_BOUND));
QRect vp = mTransform.inverted().mapRect(this->rect());
if (!bound.contains(vp))
{
Log::instance().message(QString("(L: %1; R: %2; T: %3; B: %4)")
.arg(vp.left()).arg(vp.right())
.arg(vp.top()).arg(vp.bottom()));
if (vp.left() < bound.left())
{
mTransform.translate(-(bound.left() - vp.left()), 0);
}
else if (vp.right() > bound.right())
{
mTransform.translate(-(bound.right() - vp.right()), 0);
}
if (vp.top() < bound.top())
{
mTransform.translate(0, -(bound.top() - vp.top()));
}
if (vp.bottom() > bound.bottom())
{
mTransform.translate(0, -(bound.bottom() - vp.bottom()));
}
}
// move cursor to middle of the window
QCursor::setPos(mapToGlobal(rect().center()));
// force repaint
update();
}
/// Pos is in screen coords
void PCBView::zoom(double factor, QPoint pos)
{
if (!mCtrl || !mCtrl->docIsOpen()) return;
recenter(pos);
QPoint p = mTransform.inverted().map(rect().center());
QTransform test = mTransform;
test.scale(factor, factor);
// check if the bounding rectangle does not enclose the viewport
// refuse to zoom out (factor < 1) if that's the case
if(!test.mapRect(QRect(QPoint(-XPcb::PCB_BOUND, -XPcb::PCB_BOUND),
QPoint(XPcb::PCB_BOUND, XPcb::PCB_BOUND)))
.contains(this->rect()) && factor < 1 )
{
return;
}
mTransform = test;
recenter(p, true);
}