File indexing completed on 2024-06-23 04:25:59

0001 /*
0002  * SPDX-FileCopyrightText: 2008 Cyrille Berger <cberger@cberger.net>
0003  * SPDX-FileCopyrightText: 2010 Geoffry Song <goffrie@gmail.com>
0004  * SPDX-FileCopyrightText: 2017 Scott Petrovic <scottpetrovic@gmail.com>
0005  *
0006  *  SPDX-License-Identifier: LGPL-2.0-or-later
0007  */
0008 
0009 #ifndef _PERSPECTIVE_ASSISTANT_H_
0010 #define _PERSPECTIVE_ASSISTANT_H_
0011 
0012 #include "kis_abstract_perspective_grid.h"
0013 #include "kis_painting_assistant.h"
0014 #include <QObject>
0015 #include <QPolygonF>
0016 #include <QLineF>
0017 #include <QTransform>
0018 
0019 #include <PerspectiveBasedAssistantHelper.h>
0020 
0021 class PerspectiveAssistant : public KisAbstractPerspectiveGrid, public KisPaintingAssistant
0022 {
0023     Q_OBJECT
0024 public:
0025     PerspectiveAssistant(QObject * parent = 0);
0026     KisPaintingAssistantSP clone(QMap<KisPaintingAssistantHandleSP, KisPaintingAssistantHandleSP> &handleMap) const override;
0027 
0028     QPointF adjustPosition(const QPointF& point, const QPointF& strokeBegin, const bool snapToAny, qreal moveThresholdPt) override;
0029     void adjustLine(QPointF &point, QPointF& strokeBegin) override;
0030     void endStroke() override;
0031 
0032     QPointF getDefaultEditorPosition() const override;
0033     int numHandles() const override { return 4; }
0034     void drawAssistant(QPainter& gc, const QRectF& updateRect, const KisCoordinatesConverter* converter, bool cached = true,KisCanvas2* canvas=0, bool assistantVisible=true, bool previewVisible=true) override;
0035 
0036     bool contains(const QPointF& point) const override;
0037     qreal distance(const QPointF& point) const override;
0038     bool isActive() const override;
0039 
0040     int subdivisions() const;
0041     void setSubdivisions(int subdivisions);
0042 
0043     bool isAssistantComplete() const override;
0044 
0045     void saveCustomXml(QXmlStreamWriter *xml) override;
0046     bool loadCustomXml(QXmlStreamReader *xml) override;
0047 
0048 protected:
0049     void drawCache(QPainter& gc, const KisCoordinatesConverter *converter,  bool assistantVisible=true) override;
0050 private:
0051     QPointF project(const QPointF& pt, const QPointF& strokeBegin, const bool snapToAnyDirection, qreal moveThresholdPt);
0052     // creates the convex hull, returns false if it's not a quadrilateral
0053     // finds the transform from perspective coordinates (a unit square) to the document
0054     bool getTransform(QPolygonF& polyOut, QTransform& transformOut) const;
0055     explicit PerspectiveAssistant(const PerspectiveAssistant &rhs, QMap<KisPaintingAssistantHandleSP, KisPaintingAssistantHandleSP> &handleMap);
0056 
0057     // The number of subdivisions to draw
0058     int m_subdivisions {8};
0059     // which direction to snap to (in transformed coordinates)
0060     QLineF m_snapLine;
0061     // cached information
0062     mutable QTransform m_cachedTransform;
0063     mutable QPolygonF m_cachedPolygon;
0064     mutable QPointF m_cachedPoints[4];
0065     mutable bool m_cacheValid {false};
0066 
0067     mutable PerspectiveBasedAssistantHelper::CacheData m_cache;
0068 
0069 };
0070 
0071 class PerspectiveAssistantFactory : public KisPaintingAssistantFactory
0072 {
0073 public:
0074     PerspectiveAssistantFactory();
0075     ~PerspectiveAssistantFactory() override;
0076     QString id() const override;
0077     QString name() const override;
0078     KisPaintingAssistant* createPaintingAssistant() const override;
0079 };
0080 
0081 #endif