File indexing completed on 2024-12-22 04:16:39

0001 /*
0002  *  SPDX-FileCopyrightText: 2019 Kuntal Majumder <hellozee@disroot.org>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #ifndef KIS_TOOL_SELECT_MAGNETIC_H_
0008 #define KIS_TOOL_SELECT_MAGNETIC_H_
0009 
0010 #include <QPoint>
0011 #include "KisSelectionToolFactoryBase.h"
0012 #include <kis_tool_select_base.h>
0013 #include <kis_signal_compressor.h>
0014 #include <kis_icon.h>
0015 #include "KisMagneticWorker.h"
0016 
0017 class QPainterPath;
0018 
0019 class KisToolSelectMagnetic : public KisToolSelect
0020 {
0021     Q_OBJECT
0022 
0023 public:
0024     KisToolSelectMagnetic(KoCanvasBase *canvas);
0025     ~KisToolSelectMagnetic() override = default;
0026     void beginPrimaryAction(KoPointerEvent *event) override;
0027     void continuePrimaryAction(KoPointerEvent *event) override;
0028     void endPrimaryAction(KoPointerEvent *event) override;
0029     void paint(QPainter& gc, const KoViewConverter &converter) override;
0030 
0031     //void beginPrimaryDoubleClickAction(KoPointerEvent *event) override;
0032 
0033     void keyPressEvent(QKeyEvent *event) override;
0034     void keyReleaseEvent(QKeyEvent *event) override;
0035 
0036     void mouseMoveEvent(KoPointerEvent *event) override;
0037 
0038     void resetCursorStyle() override;
0039     void requestStrokeEnd() override;
0040     void requestStrokeCancellation() override;
0041     QWidget *createOptionWidget() override;
0042 
0043 Q_SIGNALS:
0044     void setButtonsEnabled(bool);
0045 
0046 public Q_SLOTS:
0047     void deactivate() override;
0048     void activate(const QSet<KoShape *> &shapes) override;
0049     void undoPoints();
0050     void slotSetFilterRadius(qreal);
0051     void slotSetThreshold(int);
0052     void slotSetSearchRadius(int);
0053     void slotSetAnchorGap(int);
0054     void slotCalculateEdge();
0055 
0056 protected:
0057     using KisToolSelectBase::m_widgetHelper;
0058 
0059 private:
0060     void finishSelectionAction();
0061     void updateFeedback();
0062     void updateContinuedMode();
0063     void updateCanvas();
0064     void updatePaintPath();
0065     void resetVariables();
0066     void drawAnchors(QPainter &gc);
0067     void checkIfAnchorIsSelected(QPointF pt);
0068     vQPointF computeEdgeWrapper(QPoint a, QPoint b);
0069     void reEvaluatePoints();
0070     void calculateCheckPoints(vQPointF points);
0071     void deleteSelectedAnchor();
0072     void updateSelectedAnchor();
0073     int updateInitialAnchorBounds(QPoint pt);
0074 
0075     QPainterPath m_paintPath;
0076     QVector<QPointF> m_points;
0077     QVector<QPoint> m_anchorPoints;
0078     bool m_continuedMode {false};
0079     QPointF m_lastCursorPos, m_cursorOnPress;
0080     QPoint m_lastAnchor;
0081     bool m_complete {false};
0082     bool m_selected {false};
0083     bool m_finished {false};
0084     QScopedPointer<KisMagneticWorker> m_worker;
0085     int m_threshold {70};
0086     int m_searchRadius {30};
0087     int m_selectedAnchor {0};
0088     int m_anchorGap {30};
0089     qreal m_filterRadius {3.0};
0090     QRectF m_snapBound;
0091     KConfigGroup m_configGroup;
0092     QVector<vQPointF> m_pointCollection;
0093     KisSignalCompressor m_mouseHoverCompressor;
0094 };
0095 
0096 class KisToolSelectMagneticFactory : public KisSelectionToolFactoryBase
0097 {
0098 public:
0099     KisToolSelectMagneticFactory()
0100         : KisSelectionToolFactoryBase("KisToolSelectMagnetic")
0101     {
0102         setToolTip(i18n("Magnetic Selection Tool"));
0103         setSection(ToolBoxSection::Select);
0104         setIconName(koIconNameCStr("tool_magnetic_selection"));
0105         setPriority(8);
0106         setActivationShapeId(KRITA_TOOL_ACTIVATION_ID);
0107     }
0108 
0109     ~KisToolSelectMagneticFactory() override { }
0110 
0111     KoToolBase * createTool(KoCanvasBase *canvas) override
0112     {
0113         return new KisToolSelectMagnetic(canvas);
0114     }
0115 
0116     QList<QAction *> createActionsImpl() override
0117     {
0118         KisActionRegistry *actionRegistry = KisActionRegistry::instance();
0119         QList<QAction *> actions = KisSelectionToolFactoryBase::createActionsImpl();
0120 
0121         actions << actionRegistry->makeQAction("undo_polygon_selection", this);
0122 
0123         return actions;
0124     }
0125 };
0126 
0127 
0128 #endif // __selecttoolmagnetic_h__