Sun, 25 Jul 2021 20:39:21 +0300
use QT_NO_KEYWORDS
#include <QMessageBox> #include <document.h> #include "linetypes/edge.h" #include "linetypes/triangle.h" #include "linetypes/quadrilateral.h" #include "drawtool.h" #include "modeleditcontext.h" static const QBrush pointBrush = {Qt::white}; static const QPen polygonPen = {QBrush{Qt::black}, 2.0, Qt::DashLine}; static const QPen pointPen = {QBrush{Qt::black}, 2.0}; static const QBrush polygonBrush = {QColor{64, 255, 128, 192}}; DrawTool::DrawTool(QObject* parent) : BaseTool{parent} {} 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) { 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; } bool DrawTool::mouseMove(Document* document, Canvas* canvas) { static_cast<void>(document); const auto& worldPosition = canvas->getWorldPosition(); if (worldPosition.has_value()) { this->previewPoint = worldPosition.value(); if (this->polygon.size() < 4) { this->previewPolygon.resize(this->polygon.size() + 1); this->previewPolygon.back() = this->previewPoint; } } return false; } void DrawTool::reset() { this->polygon.clear(); } void DrawTool::overpaint(Canvas* canvas, QPainter* painter) const { painter->setBrush(::polygonBrush); painter->setPen(::polygonPen); canvas->drawWorldPolygon(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); } } 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) { Model::EditContext edit = document->editModel(); switch (this->polygon.size()) { case 2: edit.append<ldraw::Edge>(vectorToArray<2>(this->polygon), ldraw::edgeColor); break; case 3: edit.append<ldraw::Triangle>(vectorToArray<3>(this->polygon), ldraw::mainColor); break; case 4: edit.append<ldraw::Quadrilateral>(vectorToArray<4>(this->polygon), ldraw::mainColor); break; } } this->polygon.clear(); }