forked from rpelorosso/pcb-tracer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComponentDrawingTool.cpp
More file actions
227 lines (204 loc) · 6.1 KB
/
Copy pathComponentDrawingTool.cpp
File metadata and controls
227 lines (204 loc) · 6.1 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
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
#include <QPointF>
#include <QInputDialog>
#include <QGraphicsScene>
#include <cmath>
#include "ComponentDrawingTool.h"
#include "Editor.h"
#include "Node.h"
#include "Component.h"
#include "actions/AddComponent.h"
//#include "AddComponent.h"
ComponentDrawingTool::ComponentDrawingTool() {}
void ComponentDrawingTool::enterMode()
{
Editor::instance()->setCursor(Qt::CrossCursor);
clearPoints();
}
bool ComponentDrawingTool::onMousePress(QMouseEvent* event)
{
QPointF mousePos = Editor::instance()->mapToScene(event->pos());
m_lastClickPos = mousePos;
if (event->button() == Qt::LeftButton)
{
if (event->modifiers() & Qt::ShiftModifier)
{
if (!m_points.isEmpty() && m_points.last().type() == QVariant::PointF)
{
QPointF lastPoint = m_points.takeLast().toPointF();
QVector<QPointF> pointPair = {lastPoint, mousePos};
m_points.append(QVariant::fromValue(pointPair));
}
else
{
m_points.append(QVariant::fromValue(mousePos));
}
}
else
{
m_points.append(QVariant::fromValue(mousePos));
}
// add phantom pad
PhantomPad* phantomPad = new PhantomPad(QString("Phantom_%1").arg(m_phantomPads.size()), mousePos);
m_phantomPads.append(phantomPad);
Editor::instance()->scene()->addItem(phantomPad);
}
return true;
}
bool ComponentDrawingTool::onKeyPress(QKeyEvent* event)
{
if (event->key() == Qt::Key_Escape)
{
clearPoints();
return true;
}
if (event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter)
{
if (!m_points.isEmpty())
{
createComponentNodes();
}
else
{
qDebug() << "No points entered. Add points before creating a component.";
}
}
if (event->key() == Qt::Key_Shift)
{
Editor::instance()->showTracingIndicator();
}
return true;
}
bool ComponentDrawingTool::onKeyRelease(QKeyEvent* event)
{
if (event->key() == Qt::Key_Shift)
{
Editor::instance()->hideTracingIndicator();
}
return true;
}
bool ComponentDrawingTool::onMouseMove(QMouseEvent* event)
{
if (Editor::instance()->m_tracingIndicator && !m_lastClickPos.isNull())
{
QPointF mousePos = Editor::instance()->mapToScene(event->pos());
Editor::instance()->m_tracingIndicator->setLine(
m_lastClickPos.x(),
m_lastClickPos.y(),
mousePos.x(),
mousePos.y()
);
}
return false;
}
void ComponentDrawingTool::createComponentNodes()
{
if (m_points.isEmpty())
{
qDebug() << "No points to create component nodes from.";
return;
}
bool ok;
QString componentName = QInputDialog::getText(Editor::instance(), "Component Name", "Enter component name:", QLineEdit::Normal, QString(), &ok);
if (!ok || componentName.isEmpty())
{
return;
}
int singlePoints = 0;
int pointPairs = 0;
for (const QVariant& point : m_points)
{
if (point.canConvert<QPointF>())
{
singlePoints++;
}
else if (point.canConvert<QVector<QPointF>>())
{
pointPairs++;
}
}
int totalPins;
if (pointPairs > 0)
{
totalPins = QInputDialog::getInt(Editor::instance(), "Total Pins", "Enter total number of pins:", 0, 0, 1000, 1, &ok);
if (!ok || totalPins <= 0)
{
return;
}
}
else
{
totalPins = singlePoints;
}
int pinsForPairs = totalPins - singlePoints;
int pinsPerPair = pointPairs > 0 ? std::ceil(static_cast<double>(pinsForPairs) / pointPairs) : 0;
std::vector<Pad*> pads;
int padCount = 1;
for (const QVariant& item : m_points)
{
if (item.canConvert<QPointF>())
{
QPointF point = item.toPointF();
QString padName = QString("%1 Pin %2").arg(componentName).arg(padCount);
int padId = Node::genNodeId();
Pad* pad = new Pad(padName, padId, point, padCount);
pads.push_back(pad);
padCount++;
}
else if (item.canConvert<QVector<QPointF>>())
{
QVector<QPointF> pointPair = item.value<QVector<QPointF>>();
if (pointPair.size() == 2)
{
QPointF start = pointPair[0];
QPointF end = pointPair[1];
for (int i = 0; i < pinsPerPair; ++i)
{
double t = pinsPerPair > 1 ? static_cast<double>(i) / (pinsPerPair - 1) : 0;
double x = start.x() + t * (end.x() - start.x());
double y = start.y() + t * (end.y() - start.y());
QString padName = QString("%1 Pin %2").arg(componentName).arg(padCount);
int padId = Node::genNodeId();
Pad* pad = new Pad(padName, padId, QPointF(x, y), padCount);
pads.push_back(pad);
padCount++;
if (padCount > totalPins)
{
break;
}
}
}
}
if (padCount > totalPins)
{
break;
}
}
AddComponentMeta meta{.m_name=componentName,.m_pads=pads};
AddComponent* action = new AddComponent(meta);
// add the action to the undo stack
Editor::instance()->m_undoStack.push(action);
qDebug() << "Created component '" << componentName << "' with" << padCount - 1 << "pins";
clearPoints();
}
void ComponentDrawingTool::removePhantomPads()
{
for (PhantomPad* phantomPad : m_phantomPads)
{
Editor::instance()->scene()->removeItem(phantomPad);
delete phantomPad;
}
m_phantomPads.clear();
}
void ComponentDrawingTool::clearPoints()
{
Editor::instance()->hideTracingIndicator();
m_points.clear();
removePhantomPads();
}
void ComponentDrawingTool::exitMode() {
clearPoints();
}
QVector<QVariant> ComponentDrawingTool::getPoints() const
{
return m_points;
}