Sat, 05 Mar 2022 17:18:44 +0200
Added the delete action
#include <QMessageBox> #include <document.h> #include "linetypes/edge.h" #include "linetypes/triangle.h" #include "linetypes/quadrilateral.h" #include "drawtool.h" #include "modeleditor.h" static const QBrush pointBrush = {Qt::white}; static const QPen polygonPen = {QBrush{Qt::black}, 2.0, Qt::DashLine}; static const QPen badPolygonPen = {QBrush{Qt::red}, 2.0, Qt::DashLine}; static const QPen pointPen = {QBrush{Qt::black}, 2.0}; static const QBrush greenPolygonBrush = {QColor{64, 255, 128, 192}}; static const QBrush redPolygonBrush = {QColor{255, 96, 96, 192}}; DrawTool::DrawTool(Document* document) : BaseTool{document} { } QString DrawTool::name() const { static const QString result = tr("Draw"); return result; } QString DrawTool::toolTip() const { static const QString result = tr("Draw new elements into the model."); return result; } bool DrawTool::mouseClick(Document* document, Canvas* canvas, QMouseEvent* event) { if (event->button() == Qt::LeftButton) { const auto& worldPosition = canvas->getWorldPosition(); if (worldPosition.has_value()) { const glm::vec3& pos = worldPosition.value(); const auto isCloseToPos = [&](const glm::vec3& x){return geom::isclose(x, pos);}; if (any(this->polygon, isCloseToPos)) { this->closeShape(document); } else { this->polygon.push_back(pos); if (this->polygon.size() == 4) { this->closeShape(document); } } } this->previewPolygon = this->polygon; return true; } else if (event->button() == Qt::RightButton and this->polygon.size() > 0) { this->polygon.erase(this->polygon.end() - 1); this->updatePreviewPolygon(); return true; } else { return false; } } bool DrawTool::mouseMove(Document* document, Canvas* canvas, QMouseEvent *event) { static_cast<void>(document); static_cast<void>(event); const auto& worldPosition = canvas->getWorldPosition(); if (worldPosition.has_value()) { this->previewPoint = worldPosition.value(); if (this->polygon.size() < 4) { this->updatePreviewPolygon(); } } return false; } bool DrawTool::keyReleased(Document*, Canvas* canvas, QKeyEvent* event) { if (event->key() == Qt::Key_Escape) { this->polygon.clear(); this->updatePreviewPolygon(); canvas->update(); return true; } else { return false; } } void DrawTool::updatePreviewPolygon() { this->previewPolygon.resize(this->polygon.size() + 1); this->previewPolygon.back() = this->previewPoint; if (this->previewPolygon.size() > 2) { this->isconcave = not geom::isConvex(this->previewPolygon); } } void DrawTool::reset() { this->polygon.clear(); } void DrawTool::overpaint(Canvas* canvas, QPainter* painter) const { painter->setPen(this->isconcave ? ::badPolygonPen : ::polygonPen); if (this->previewPolygon.size() > 2 and not this->isconcave) { if (canvas->worldPolygonWinding(this->previewPolygon) == Winding::Clockwise) { painter->setBrush(::greenPolygonBrush); } else { painter->setBrush(::redPolygonBrush); } canvas->drawWorldPolygon(painter, this->previewPolygon); } else { canvas->drawWorldPolyline(painter, this->previewPolygon); } painter->setBrush(::pointBrush); painter->setPen(::pointPen); for (const glm::vec3& point : this->polygon) { canvas->drawWorldPoint(painter, point); } if (this->polygon.size() < 4) { canvas->drawWorldPoint(painter, this->previewPoint); } } QString DrawTool::iconName() const { return ":/icons/pencil-outline.png"; } template<std::size_t N, typename T> std::array<T, N> vectorToArray(const std::vector<T>& x) { std::array<T, N> result; for (std::size_t i = 0; i < x.size() and i < N; i += 1) { result[i] = x[i]; } return result; } void DrawTool::closeShape(Document* document) { if (this->polygon.size() >= 2 and this->polygon.size() <= 4) { std::unique_ptr<ModelEditor> modelEditor = document->editModel(); switch (this->polygon.size()) { case 2: modelEditor->append<ldraw::Edge>(vectorToArray<2>(this->polygon), ldraw::EDGE_COLOR); break; case 3: modelEditor->append<ldraw::Triangle>(vectorToArray<3>(this->polygon), ldraw::MAIN_COLOR); break; case 4: modelEditor->append<ldraw::Quadrilateral>(vectorToArray<4>(this->polygon), ldraw::MAIN_COLOR); break; } } this->polygon.clear(); }