|
| 1 | +/* |
| 2 | + * Copyright (c) 2026, solvcon team <contact@solvcon.net> |
| 3 | + * BSD 3-Clause License, see COPYING |
| 4 | + */ |
| 5 | + |
| 6 | +#include <solvcon/pilot/DrawTool.hpp> |
| 7 | + |
| 8 | +#include <cmath> |
| 9 | +#include <stdexcept> |
| 10 | + |
| 11 | +#include <QColor> |
| 12 | +#include <QPainter> |
| 13 | +#include <QPen> |
| 14 | +#include <QPointF> |
| 15 | + |
| 16 | +namespace solvcon |
| 17 | +{ |
| 18 | + |
| 19 | +void DrawToolBase::paint_preview( |
| 20 | + QPainter & painter, |
| 21 | + ViewTransform2dFp64 const & view, |
| 22 | + std::span<DrawPoint const> points) const |
| 23 | +{ |
| 24 | + QPen pen(QColor(240, 200, 120)); |
| 25 | + pen.setCosmetic(true); |
| 26 | + pen.setWidthF(1.5); |
| 27 | + pen.setStyle(Qt::DashLine); |
| 28 | + painter.setPen(pen); |
| 29 | + painter.setBrush(Qt::NoBrush); |
| 30 | + paint_outline(painter, view, points); |
| 31 | +} |
| 32 | + |
| 33 | +namespace |
| 34 | +{ |
| 35 | + |
| 36 | +/// The default navigation tool: a left-button drag pans and zooms the view instead of drawing. |
| 37 | +class PanTool : public DrawToolBase |
| 38 | +{ |
| 39 | + |
| 40 | +public: |
| 41 | + |
| 42 | + static constexpr char const * NAME = "pan"; |
| 43 | + |
| 44 | + std::string name() const override { return NAME; } |
| 45 | + |
| 46 | + bool can_draw_shape() const override { return false; } |
| 47 | + |
| 48 | + void commit(WorldFp64 &, std::span<DrawPoint const>) const override {} |
| 49 | + |
| 50 | +protected: |
| 51 | + |
| 52 | + void paint_outline(QPainter &, ViewTransform2dFp64 const &, std::span<DrawPoint const>) const override {} |
| 53 | + |
| 54 | +}; /* end class PanTool */ |
| 55 | + |
| 56 | +/// Circle defined by two gesture points: the center and a point on the rim. |
| 57 | +class CircleTool : public DrawToolBase |
| 58 | +{ |
| 59 | + |
| 60 | +public: |
| 61 | + |
| 62 | + static constexpr char const * NAME = "circle"; |
| 63 | + |
| 64 | + std::string name() const override { return NAME; } |
| 65 | + |
| 66 | + bool can_draw_shape() const override { return true; } |
| 67 | + |
| 68 | + void commit(WorldFp64 & world, std::span<DrawPoint const> points) const override |
| 69 | + { |
| 70 | + if (points.size() < 2) |
| 71 | + { |
| 72 | + throw std::invalid_argument("CircleTool::commit: need at least two points"); |
| 73 | + } |
| 74 | + DrawPoint const & center = points.front(); |
| 75 | + DrawPoint const & rim = points.back(); |
| 76 | + world.add_circle(center.x, center.y, std::hypot(rim.x - center.x, rim.y - center.y)); |
| 77 | + } |
| 78 | + |
| 79 | +protected: |
| 80 | + |
| 81 | + void paint_outline(QPainter & painter, |
| 82 | + ViewTransform2dFp64 const & view, |
| 83 | + std::span<DrawPoint const> points) const override |
| 84 | + { |
| 85 | + if (points.size() < 2) |
| 86 | + { |
| 87 | + throw std::invalid_argument("CircleTool::paint_outline: need at least two points"); |
| 88 | + } |
| 89 | + DrawPoint const & center = points.front(); |
| 90 | + DrawPoint const & rim = points.back(); |
| 91 | + double const radius = std::hypot(rim.x - center.x, rim.y - center.y); |
| 92 | + if (radius <= 0.0) |
| 93 | + { |
| 94 | + throw std::invalid_argument("CircleTool::paint_outline: radius must be positive"); |
| 95 | + } |
| 96 | + double center_x = 0.0; |
| 97 | + double center_y = 0.0; |
| 98 | + view.screen_from_world(center.x, center.y, center_x, center_y); |
| 99 | + double const radius_px = view.zoom() * radius; |
| 100 | + painter.drawEllipse(QPointF(center_x, center_y), radius_px, radius_px); |
| 101 | + } |
| 102 | + |
| 103 | +}; /* end class CircleTool */ |
| 104 | + |
| 105 | +/// The tool registry. The first entry is the default tool a fresh canvas |
| 106 | +/// starts with. Add a new shape by writing a `DrawToolBase` subclass above |
| 107 | +/// and appending one entry here. |
| 108 | +struct ToolEntry |
| 109 | +{ |
| 110 | + char const * name; |
| 111 | + std::unique_ptr<DrawToolBase> (*make)(); |
| 112 | +}; |
| 113 | + |
| 114 | +ToolEntry const TOOL_TABLE[] = { |
| 115 | + {PanTool::NAME, []() -> std::unique_ptr<DrawToolBase> |
| 116 | + { return std::make_unique<PanTool>(); }}, |
| 117 | + {CircleTool::NAME, []() -> std::unique_ptr<DrawToolBase> |
| 118 | + { return std::make_unique<CircleTool>(); }}, |
| 119 | +}; |
| 120 | + |
| 121 | +} /* end anonymous namespace */ |
| 122 | + |
| 123 | +std::vector<std::string> const & draw_tool_names() |
| 124 | +{ |
| 125 | + static std::vector<std::string> const names = [] |
| 126 | + { |
| 127 | + std::vector<std::string> out; |
| 128 | + for (ToolEntry const & entry : TOOL_TABLE) |
| 129 | + { |
| 130 | + out.emplace_back(entry.name); |
| 131 | + } |
| 132 | + return out; |
| 133 | + }(); |
| 134 | + return names; |
| 135 | +} |
| 136 | + |
| 137 | +std::string const & default_draw_tool_name() |
| 138 | +{ |
| 139 | + static_assert(std::size(TOOL_TABLE) > 0, "TOOL_TABLE must have at least one entry"); |
| 140 | + return draw_tool_names().front(); |
| 141 | +} |
| 142 | + |
| 143 | +std::unique_ptr<DrawToolBase> make_draw_tool(std::string const & name) |
| 144 | +{ |
| 145 | + for (ToolEntry const & entry : TOOL_TABLE) |
| 146 | + { |
| 147 | + if (name == entry.name) |
| 148 | + { |
| 149 | + return entry.make(); |
| 150 | + } |
| 151 | + } |
| 152 | + throw std::invalid_argument("make_draw_tool: unknown draw tool '" + name + "'"); |
| 153 | +} |
| 154 | + |
| 155 | +bool is_draw_tool(std::string const & name) |
| 156 | +{ |
| 157 | + for (ToolEntry const & entry : TOOL_TABLE) |
| 158 | + { |
| 159 | + if (name == entry.name) |
| 160 | + { |
| 161 | + return true; |
| 162 | + } |
| 163 | + } |
| 164 | + return false; |
| 165 | +} |
| 166 | + |
| 167 | +} /* end namespace solvcon */ |
| 168 | + |
| 169 | +// vim: set ff=unix fenc=utf8 et sw=4 ts=4 sts=4: |
0 commit comments