Sun, 25 Jul 2021 16:26:38 +0300
replaced preview layers in favor of overpainting callback
#include <QMessageBox> #include "drawtool.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(const Canvas::MouseClickInfo& info) { if (info.worldPosition.has_value()) { const glm::vec3& pos = info.worldPosition.value(); const auto isCloseToPos = [&](const glm::vec3& x){return geom::isclose(x, pos);}; if (any(this->polygon, isCloseToPos)) { this->closeShape(); } else { this->polygon.push_back(pos); if (this->polygon.size() == 4) { this->closeShape(); } } } this->previewPolygon = this->polygon; return true; } bool DrawTool::mouseMove(const Canvas::MouseMoveInfo& info) { if (info.worldPosition.has_value()) { this->previewPoint = info.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); } } void DrawTool::closeShape() { QMessageBox::information(nullptr, "test", "close the polygon"); }