23 #include "primitives.h" |
23 #include "primitives.h" |
24 #include "algorithms/geometry.h" |
24 #include "algorithms/geometry.h" |
25 #include "generics/ring.h" |
25 #include "generics/ring.h" |
26 |
26 |
27 Canvas::Canvas(LDDocument* document, QWidget* parent) : |
27 Canvas::Canvas(LDDocument* document, QWidget* parent) : |
28 GLRenderer {document, parent}, |
28 gl::Renderer {document, parent}, |
29 m_document {*document}, |
29 m_document {*document}, |
30 m_currentEditMode {AbstractEditMode::createByType (this, EditModeType::Select)} {} |
30 m_currentEditMode {AbstractEditMode::createByType (this, EditModeType::Select)} {} |
31 |
31 |
32 Canvas::~Canvas() |
32 Canvas::~Canvas() |
33 { |
33 { |
34 delete m_currentEditMode; |
34 delete m_currentEditMode; |
35 } |
35 } |
36 |
36 |
37 void Canvas::overpaint(QPainter& painter) |
37 void Canvas::overpaint(QPainter& painter) |
38 { |
38 { |
39 GLRenderer::overpaint(painter); |
39 gl::Renderer::overpaint(painter); |
40 QFontMetrics metrics {QFont {}}; |
40 QFontMetrics metrics {QFont {}}; |
41 |
41 |
42 if (camera() != Camera::Free) |
42 if (camera() != gl::FreeCamera) |
43 { |
43 { |
44 // Paint the coordinates onto the screen. |
44 // Paint the coordinates onto the screen. |
45 Vertex idealized = currentCamera().idealize(m_position3D); |
45 Vertex idealized = currentCamera().idealize(m_position3D); |
46 QString text = format(tr("X: %1, Y: %2, Z: %3, %4"), m_position3D[X], m_position3D[Y], m_position3D[Z], |
46 QString text = format(tr("X: %1, Y: %2, Z: %3, %4"), m_position3D[X], m_position3D[Y], m_position3D[Z], |
47 idealized.toString(true)); |
47 idealized.toString(true)); |
211 |
211 |
212 delete m_currentEditMode; |
212 delete m_currentEditMode; |
213 m_currentEditMode = AbstractEditMode::createByType(this, a); |
213 m_currentEditMode = AbstractEditMode::createByType(this, a); |
214 |
214 |
215 // If we cannot use the free camera, use the top one instead. |
215 // If we cannot use the free camera, use the top one instead. |
216 if (camera() == Camera::Free and not m_currentEditMode->allowFreeCamera()) |
216 if (camera() == gl::FreeCamera and not m_currentEditMode->allowFreeCamera()) |
217 setCamera(Camera::Top); |
217 setCamera(gl::TopCamera); |
218 |
218 |
219 m_window->updateEditModeActions(); |
219 m_window->updateEditModeActions(); |
220 update(); |
220 update(); |
221 } |
221 } |
222 |
222 |
257 } |
257 } |
258 |
258 |
259 void Canvas::keyReleaseEvent(QKeyEvent* event) |
259 void Canvas::keyReleaseEvent(QKeyEvent* event) |
260 { |
260 { |
261 m_currentEditMode->keyReleased(event); |
261 m_currentEditMode->keyReleased(event); |
262 GLRenderer::keyReleaseEvent(event); |
262 gl::Renderer::keyReleaseEvent(event); |
263 } |
263 } |
264 |
264 |
265 void Canvas::mouseMoveEvent(QMouseEvent* event) |
265 void Canvas::mouseMoveEvent(QMouseEvent* event) |
266 { |
266 { |
267 // Calculate 3d position of the cursor |
267 // Calculate 3d position of the cursor |
268 m_position3D = currentCamera().convert2dTo3d(mousePosition(), grid()); |
268 m_position3D = currentCamera().convert2dTo3d(mousePosition(), grid()); |
269 |
269 |
270 if (not m_currentEditMode->mouseMoved(event)) |
270 if (not m_currentEditMode->mouseMoved(event)) |
271 GLRenderer::mouseMoveEvent(event); |
271 gl::Renderer::mouseMoveEvent(event); |
272 } |
272 } |
273 |
273 |
274 void Canvas::mouseReleaseEvent(QMouseEvent *event) |
274 void Canvas::mouseReleaseEvent(QMouseEvent *event) |
275 { |
275 { |
276 AbstractEditMode::MouseEventData data; |
276 AbstractEditMode::MouseEventData data; |
277 data.ev = event; |
277 data.ev = event; |
278 data.mouseMoved = mouseHasMoved(); |
278 data.mouseMoved = mouseHasMoved(); |
279 data.keymods = keyboardModifiers(); |
279 data.keymods = keyboardModifiers(); |
280 data.releasedButtons = lastButtons() & ~event->buttons(); |
280 data.releasedButtons = lastButtons() & ~event->buttons(); |
281 m_currentEditMode->mouseReleased(data); |
281 m_currentEditMode->mouseReleased(data); |
282 GLRenderer::mouseReleaseEvent(event); |
282 gl::Renderer::mouseReleaseEvent(event); |
283 } |
283 } |
284 |
284 |
285 void Canvas::mousePressEvent(QMouseEvent *event) |
285 void Canvas::mousePressEvent(QMouseEvent *event) |
286 { |
286 { |
287 if (m_currentEditMode->mousePressed(event)) |
287 if (m_currentEditMode->mousePressed(event)) |
288 event->accept(); |
288 event->accept(); |
289 |
289 |
290 GLRenderer::mousePressEvent(event); |
290 gl::Renderer::mousePressEvent(event); |
291 } |
291 } |
292 |
292 |
293 const Vertex& Canvas::position3D() const |
293 const Vertex& Canvas::position3D() const |
294 { |
294 { |
295 return m_position3D; |
295 return m_position3D; |
296 } |
296 } |
297 |
297 |
298 void Canvas::drawPoint(QPainter& painter, QPointF pos, QColor color) const |
298 void Canvas::drawPoint(QPainter& painter, QPointF pos, QColor color) const |
299 { |
299 { |
300 int pointSize = 8; |
300 int pointSize = 8; |
301 QPen pen = thinBorderPen; |
301 QPen pen = gl::thinBorderPen; |
302 pen.setWidth(1); |
302 pen.setWidth(1); |
303 painter.setPen(pen); |
303 painter.setPen(pen); |
304 painter.setBrush(color); |
304 painter.setBrush(color); |
305 painter.drawEllipse(pos.x() - pointSize / 2, pos.y() - pointSize / 2, pointSize, pointSize); |
305 painter.drawEllipse(pos.x() - pointSize / 2, pos.y() - pointSize / 2, pointSize, pointSize); |
306 } |
306 } |
372 */ |
372 */ |
373 } |
373 } |
374 |
374 |
375 double Canvas::currentCullValue() const |
375 double Canvas::currentCullValue() const |
376 { |
376 { |
377 if (this->camera() < Camera::Free) |
377 if (this->camera() < gl::FreeCamera) |
378 return far - this->cullValues[static_cast<int>(this->camera())]; |
378 return gl::far - this->cullValues[static_cast<int>(this->camera())]; |
379 else |
379 else |
380 return 0.0; |
380 return 0.0; |
381 } |
381 } |
382 |
382 |
383 void Canvas::setCullValue(double value) |
383 void Canvas::setCullValue(double value) |
384 { |
384 { |
385 if (this->camera() < Camera::Free) |
385 if (this->camera() < gl::FreeCamera) |
386 this->cullValues[static_cast<int>(this->camera())] = far - value; |
386 this->cullValues[static_cast<int>(this->camera())] = gl::far - value; |
387 } |
387 } |
388 |
388 |
389 void Canvas::clearCurrentCullValue() |
389 void Canvas::clearCurrentCullValue() |
390 { |
390 { |
391 if (this->camera() < Camera::Free) |
391 if (this->camera() < gl::FreeCamera) |
392 this->cullValues[static_cast<int>(this->camera())] = 0.0; |
392 this->cullValues[static_cast<int>(this->camera())] = 0.0; |
393 } |
393 } |