Warning, file /graphics/glaxnimate/src/gui/tools/freehand.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 /* 0002 * SPDX-FileCopyrightText: 2019-2023 Mattia Basaglia <dev@dragon.best> 0003 * 0004 * SPDX-License-Identifier: GPL-3.0-or-later 0005 */ 0006 0007 #include "draw_tool_base.hpp" 0008 #include "model/shapes/path.hpp" 0009 #include "math/bezier/operations.hpp" 0010 0011 namespace glaxnimate::gui::tools { 0012 0013 class FreehandTool : public DrawToolBase 0014 { 0015 public: 0016 QString id() const override { return "draw-freehand"; } 0017 QIcon icon() const override { return QIcon::fromTheme("draw-freehand"); } 0018 QString name() const override { return i18n("Draw Freehand"); } 0019 QKeySequence key_sequence() const override { return QKeySequence(i18n("F6"), QKeySequence::PortableText); } 0020 static int static_group() noexcept { return Registry::Draw; } 0021 int group() const noexcept override { return static_group(); } 0022 0023 void mouse_press(const MouseEvent& event) override 0024 { 0025 if ( event.button() == Qt::LeftButton ) 0026 { 0027 path.add_point(event.scene_pos); 0028 } 0029 } 0030 void mouse_move(const MouseEvent& event) override 0031 { 0032 if ( !path.empty() ) 0033 { 0034 path.add_point(event.scene_pos); 0035 event.repaint(); 0036 } 0037 0038 } 0039 void mouse_release(const MouseEvent& event) override 0040 { 0041 if ( path.size() > 1 ) 0042 { 0043 auto shape = std::make_unique<model::Path>(event.window->document()); 0044 math::bezier::simplify(path, 128); 0045 shape->shape.set(path); 0046 path.clear(); 0047 create_shape(i18n("Draw Freehand"), event, std::move(shape)); 0048 } 0049 else 0050 { 0051 if ( path.size() == 1 ) 0052 path.clear(); 0053 edit_clicked(event); 0054 } 0055 } 0056 0057 void mouse_double_click(const MouseEvent& event) override 0058 { 0059 edit_clicked(event); 0060 } 0061 0062 void paint(const PaintEvent& event) override 0063 { 0064 if ( !path.empty() ) 0065 { 0066 QPainterPath ppath; 0067 path.add_to_painter_path(ppath); 0068 draw_shape(event, event.view->mapFromScene(ppath)); 0069 } 0070 } 0071 0072 void key_press(const KeyEvent& event) override 0073 { 0074 if ( event.key() == Qt::Key_Escape ) 0075 { 0076 path.clear(); 0077 event.repaint(); 0078 event.accept(); 0079 } 0080 } 0081 0082 void key_release(const KeyEvent& event) override { Q_UNUSED(event); } 0083 void enable_event(const Event& event) override { Q_UNUSED(event); } 0084 void disable_event(const Event& event) override { Q_UNUSED(event); } 0085 0086 private: 0087 static Autoreg<FreehandTool> autoreg; 0088 math::bezier::Bezier path; 0089 }; 0090 0091 0092 tools::Autoreg<tools::FreehandTool> tools::FreehandTool::autoreg{max_priority + 1}; 0093 0094 } // namespace glaxnimate::gui::tools