Commit c2227e69 authored by Tusooa Zhu's avatar Tusooa Zhu 🔼

Implement clone() methods for KisPaintingAssistant heirarchy

parent 9055dbd6
......@@ -70,7 +70,7 @@ void KisPaintingAssistantHandle::setType(char type)
d->handle_type = type;
}
char KisPaintingAssistantHandle::handleType()
char KisPaintingAssistantHandle::handleType() const
{
return d->handle_type;
}
......@@ -93,7 +93,7 @@ void KisPaintingAssistantHandle::unregisterAssistant(KisPaintingAssistant* assis
Q_ASSERT(!d->assistants.contains(assistant));
}
bool KisPaintingAssistantHandle::containsAssistant(KisPaintingAssistant* assistant)
bool KisPaintingAssistantHandle::containsAssistant(KisPaintingAssistant* assistant) const
{
return d->assistants.contains(assistant);
}
......@@ -120,6 +120,8 @@ void KisPaintingAssistantHandle::uncache()
}
struct KisPaintingAssistant::Private {
explicit Private(const Private &rhs);
KisPaintingAssistantHandleSP reuseOrCreateHandle(QMap<KisPaintingAssistantHandleSP, KisPaintingAssistantHandleSP> &handleMap, KisPaintingAssistantHandleSP origHandle, KisPaintingAssistant *q);
QString id;
QString name;
bool isSnappingActive;
......@@ -146,6 +148,34 @@ struct KisPaintingAssistant::Private {
QColor assistantCustomColor = KisConfig(true).defaultAssistantsColor();
};
KisPaintingAssistant::Private::Private(const Private &rhs)
: id(rhs.id)
, name(rhs.name)
, isSnappingActive(rhs.isSnappingActive)
, outlineVisible(rhs.outlineVisible)
// handles are shared, need to use map
, cached(rhs.cached)
, cachedRect(rhs.cachedRect)
, m_canvas(rhs.m_canvas)
, cachedTransform(rhs.cachedTransform)
, assistantGlobalColorCache(rhs.assistantGlobalColorCache)
, useCustomColor(rhs.useCustomColor)
, assistantCustomColor(rhs.assistantCustomColor)
{
}
KisPaintingAssistantHandleSP KisPaintingAssistant::Private::reuseOrCreateHandle(QMap<KisPaintingAssistantHandleSP, KisPaintingAssistantHandleSP> &handleMap, KisPaintingAssistantHandleSP origHandle, KisPaintingAssistant *q)
{
KisPaintingAssistantHandleSP mappedHandle = handleMap.value();
if (!mappedHandle) {
mappedHandle = KisPaintingAssistantHandleSP(new KisPaintingAssistantHandle(origHandle));
mappedHandle->setType(origHandle->handleType());
handleMap.insert(origHandle, mappedHandle);
}
mappedHandle.registerAssistant(q);
return mappedHandle;
}
bool KisPaintingAssistant::useCustomColor()
{
return d->useCustomColor;
......@@ -184,6 +214,32 @@ KisPaintingAssistant::KisPaintingAssistant(const QString& id, const QString& nam
d->outlineVisible = true;
}
KisPaintingAssistant::KisPaintingAssistant(const KisPaintingAssistant &rhs, QMap<KisPaintingAssistantHandleMap, KisPaintingAssistantHandleSP> &handleMap)
: d(new Private(rhs.d))
{
Q_FOREACH (const KisPaintingAssistantHandleSP origHandle, rhs.d->handles) {
d->handles << d->reuseOrCreateHandle(handleMap, origHandle, this);
}
Q_FOREACH (const KisPaintingAssistantHandleSP origHandle, rhs.d->sideHandles) {
d->sideHandles << d->reuseOrCreateHandle(handleMap, origHandle, this);
}
#define _REUSE_H(name) d->name = d->reuseOrCreateHandle(handleMap, rhs.d->name, this)
_REUSE_H(topLeft);
_REUSE_H(bottomLeft);
_REUSE_H(topRight);
_REUSE_H(bottomRight);
_REUSE_H(topMiddle);
_REUSE_H(bottomMiddle);
_REUSE_H(rightMiddle);
_REUSE_H(leftMiddle);
#undef _REUSE_H
}
KisPaintingAssistantSP KisPaintingAssistant::clone(QMap<KisPaintingAssistantHandleSP, KisPaintingAssistantHandleSP> &handleMap)
{
return KisPaintingAssistantSP(new KisPaintingAssistant(*this, handleMap));
}
bool KisPaintingAssistant::isSnappingActive() const
{
return d->isSnappingActive;
......
......@@ -27,9 +27,11 @@
#include <QObject>
#include <QColor>
#include <QXmlStreamWriter>
#include <QMap>
#include <kritaui_export.h>
#include <kis_shared.h>
#include <kis_types.h>
class QPainter;
class QRect;
......@@ -74,12 +76,12 @@ public:
void uncache();
KisPaintingAssistantHandle& operator=(const QPointF&);
void setType(char type);
char handleType();
char handleType() const;
private:
void registerAssistant(KisPaintingAssistant*);
void unregisterAssistant(KisPaintingAssistant*);
bool containsAssistant(KisPaintingAssistant*);
bool containsAssistant(KisPaintingAssistant*) const;
private:
struct Private;
......@@ -95,6 +97,7 @@ class KRITAUI_EXPORT KisPaintingAssistant
public:
KisPaintingAssistant(const QString& id, const QString& name);
virtual ~KisPaintingAssistant();
virtual KisPaintingAssistantSP clone(QMap<KisPaintingAssistantHandleSP, KisPaintingAssistantHandleSP> &handleMap);
const QString& id() const;
const QString& name() const;
bool isSnappingActive() const;
......@@ -189,6 +192,8 @@ public:
static double norm2(const QPointF& p);
protected:
explicit KisPaintingAssistant(const KisPaintingAssistant &rhs, QMap<KisPaintingAssistantHandleSP, KisPaintingAssistantSP> &handleMap);
virtual QRect boundingRect() const;
/// performance layer where the graphics can be drawn from a cache instead of generated every render update
......
......@@ -35,6 +35,18 @@ ConcentricEllipseAssistant::ConcentricEllipseAssistant()
{
}
KisPaintingAssistantSP ConcentricEllipseAssistant::clone(QMap<KisPaintingAssistantHandleSP, KisPaintingAssistantHandleSP> &handleMap) const
{
return KisPaintingAssistantSP(new ConcentricEllipseAssistant(*this, handleMap));
}
ConcentricEllipseAssistant::ConcentricEllipseAssistant(const ConcentricEllipseAssistant &rhs, QMap<KisPaintingAssistantHandleSP, KisPaintingAssistantHandleSP> &handleMap)
: KisPaintingAssistant(rhs, handleMap)
, m_ellipse(rhs.m_ellipse)
, m_extraEllipse(rhs.m_extraEllipse)
{
}
QPointF ConcentricEllipseAssistant::project(const QPointF& pt, const QPointF& strokeBegin) const
{
Q_ASSERT(isAssistantComplete());
......
......@@ -29,6 +29,7 @@ class ConcentricEllipseAssistant : public KisPaintingAssistant
{
public:
ConcentricEllipseAssistant();
KisPaintingAssistantSP clone(QMap<KisPaintingAssistantHandleSP, KisPaintingAssistantHandleSP> &handleMap) override;
QPointF adjustPosition(const QPointF& point, const QPointF& strokeBegin) override;
QPointF buttonPosition() const override;
int numHandles() const override { return 3; }
......@@ -42,6 +43,7 @@ private:
QPointF project(const QPointF& pt, const QPointF& strokeBegin) const;
mutable Ellipse m_ellipse;
mutable Ellipse m_extraEllipse;
explicit ConcentricEllipseAssistant(const ConcentricEllipseAssistant &rhs, QMap<KisPaintingAssistantHandleSP, KisPaintingAssistantHandleSP> &handleMap);
};
class ConcentricEllipseAssistantFactory : public KisPaintingAssistantFactory
......
......@@ -35,6 +35,17 @@ EllipseAssistant::EllipseAssistant()
{
}
EllipseAssistant::EllipseAssistant(const EllipseAssistant &rhs, QMap<KisPaintingAssistantHandleSP, KisPaintingAssistantHandlebSP> &handleMap)
: KisPaintingAssistant(rhs, handleMap)
, e(rhs.e)
{
}
KisPaintingAssistantSP EllipseAssistant::clone(QMap<KisPaintingAssistantHandleSP, KisPaintingAssistantHandleSP> &handleMap) const
{
return KisPaintingAssistantSP(new EllipseAssistant(*this, handleMap));
}
QPointF EllipseAssistant::project(const QPointF& pt) const
{
Q_ASSERT(isAssistantComplete());
......
......@@ -28,6 +28,7 @@ class EllipseAssistant : public KisPaintingAssistant
{
public:
EllipseAssistant();
KisPaintingAssistantSP clone(QMap<KisPaintingAssistantHandleSP, KisPaintingAssistantHandleSP> &handleMap) const override;
QPointF adjustPosition(const QPointF& point, const QPointF& strokeBegin) override;
QPointF buttonPosition() const override;
int numHandles() const override { return 3; }
......@@ -39,6 +40,7 @@ protected:
void drawCache(QPainter& gc, const KisCoordinatesConverter *converter, bool assistantVisible=true) override;
private:
QPointF project(const QPointF& pt) const;
explicit EllipseAssistant(const EllipseAssistant &rhs, QMap<KisPaintingAssistantHandleSP, KisPaintingAssistantHandlebSP> &handleMap);
mutable Ellipse e;
};
......
......@@ -39,6 +39,18 @@ FisheyePointAssistant::FisheyePointAssistant()
{
}
FisheyePointAssistant::FisheyePointAssistant(const FisheyePointAssistant &rhs, QMap<KisPaintingAssistantHandleSP, KisPaintingAssistantHandlebSP> &handleMap)
: KisPaintingAssistant(rhs, handleMap)
, e(rhs.e)
, extraE(rhs.extraE)
{
}
KisPaintingAssistantSP FisheyePointAssistant::clone(QMap<KisPaintingAssistantHandleSP, KisPaintingAssistantHandleSP> &handleMap) const
{
return KisPaintingAssistantSP(new FisheyePointAssistant(*this, handleMap));
}
QPointF FisheyePointAssistant::project(const QPointF& pt, const QPointF& strokeBegin)
{
const static QPointF nullPoint(std::numeric_limits<qreal>::quiet_NaN(), std::numeric_limits<qreal>::quiet_NaN());
......
......@@ -33,6 +33,7 @@ class FisheyePointAssistant : public KisPaintingAssistant
{
public:
FisheyePointAssistant();
KisPaintingAssistantSP clone(QMap<KisPaintingAssistantHandleSP, KisPaintingAssistantHandleSP> &handleMap) const override;
QPointF adjustPosition(const QPointF& point, const QPointF& strokeBegin) override;
//virtual void endStroke();
QPointF buttonPosition() const override;
......@@ -46,6 +47,7 @@ protected:
void drawCache(QPainter& gc, const KisCoordinatesConverter *converter, bool assistantVisible=true) override;
private:
QPointF project(const QPointF& pt, const QPointF& strokeBegin);
explicit FisheyePointAssistant(const FisheyePointAssistant &rhs, QMap<KisPaintingAssistantHandleSP, KisPaintingAssistantHandlebSP> &handleMap);
mutable Ellipse e;
mutable Ellipse extraE;
};
......
......@@ -38,6 +38,16 @@ InfiniteRulerAssistant::InfiniteRulerAssistant()
{
}
InfiniteRulerAssistant::InfiniteRulerAssistant(const InfiniteRulerAssistant &rhs, QMap<KisPaintingAssistantHandleSP, KisPaintingAssistantHandleSP> &handleMap)
: KisPaintingAssistant(rhs, handleMap)
{
}
KisPaintingAssistantSP InfiniteRulerAssistant::clone(QMap<KisPaintingAssistantHandleSP, KisPaintingAssistantHandleSP> &handleMap) const
{
return KisPaintingAssistantSP(new InfiniteRulerAssistant(*this, handleMap));
}
QPointF InfiniteRulerAssistant::project(const QPointF& pt, const QPointF& strokeBegin)
{
Q_ASSERT(isAssistantComplete());
......
......@@ -34,6 +34,7 @@ class InfiniteRulerAssistant : public KisPaintingAssistant
{
public:
InfiniteRulerAssistant();
KisPaintingAssistantSP clone(QMap<KisPaintingAssistantHandleSP, KisPaintingAssistantHandleSP> &handleMap) const override;
QPointF adjustPosition(const QPointF& point, const QPointF& strokeBegin) override;
//virtual void endStroke();
QPointF buttonPosition() const override;
......@@ -45,6 +46,7 @@ protected:
void drawCache(QPainter& gc, const KisCoordinatesConverter *converter, bool assistantVisible=true) override;
private:
QPointF project(const QPointF& pt, const QPointF& strokeBegin);
explicit InfiniteRulerAssistant(const InfiniteRulerAssistant &rhs, QMap<KisPaintingAssistantHandleSP, KisPaintingAssistantHandleSP> &handleMap);
};
class InfiniteRulerAssistantFactory : public KisPaintingAssistantFactory
......
......@@ -38,6 +38,16 @@ ParallelRulerAssistant::ParallelRulerAssistant()
{
}
KisPaintingAssistantSP ParallelRulerAssistant::clone(QMap<KisPaintingAssistantHandleSP, KisPaintingAssistantHandleSP> &handleMap) const
{
return KisPaintingAssistantSP(new ParallelRulerAssistant(*this, handleMap));
}
ParallelRulerAssistant::ParallelRulerAssistant(const ParallelRulerAssistant &rhs, QMap<KisPaintingAssistantHandleSP, KisPaintingAssistantHandlebSP> &handleMap)
: KisPaintingAssistant(rhs, handleMap)
{
}
QPointF ParallelRulerAssistant::project(const QPointF& pt, const QPointF& strokeBegin)
{
Q_ASSERT(isAssistantComplete());
......
......@@ -34,6 +34,7 @@ class ParallelRulerAssistant : public KisPaintingAssistant
{
public:
ParallelRulerAssistant();
KisPaintingAssistantSP clone(QMap<KisPaintingAssistantHandleSP, KisPaintingAssistantHandleSP> &handleMap) const override;
QPointF adjustPosition(const QPointF& point, const QPointF& strokeBegin) override;
//virtual void endStroke();
QPointF buttonPosition() const override;
......@@ -45,6 +46,7 @@ protected:
void drawCache(QPainter& gc, const KisCoordinatesConverter *converter, bool assistantVisible=true) override;
private:
QPointF project(const QPointF& pt, const QPointF& strokeBegin);
explicit ParallelRulerAssistant(const ParallelRulerAssistant &rhs, QMap<KisPaintingAssistantHandleSP, KisPaintingAssistantHandlebSP> &handleMap);
};
class ParallelRulerAssistantFactory : public KisPaintingAssistantFactory
......
......@@ -39,6 +39,23 @@ PerspectiveAssistant::PerspectiveAssistant(QObject *parent)
{
}
PerspectiveAssistant::PerspectiveAssistant(const PerspectiveAssistant &rhs, QMap<KisPaintingAssistantHandleSP, KisPaintingAssistantHandlebSP> &handleMap)
: KisAbstractPerspectiveGrid(rhs.parent())
, KisPaintingAssistant(rhs, handleMap)
, m_snapLine(rhs.m_snapLine)
, m_cachedTransform(rhs.m_cachedTransform)
, m_cachedPolygon(rhs.m_cachedPolygon)
, m_cacheValid(rhs.m_cacheValid)
{
for (int i = 0; i < 4; ++i) {
m_cachedPoints[i] = rhs.m_cachedPoints[i];
}
}
KisPaintingAssistantSP PerspectiveAssistant::clone(QMap<KisPaintingAssistantHandleSP, KisPaintingAssistantHandleSP> &handleMap) const
{
return KisPaintingAssistantSP(new PerspectiveAssistant(*this, handleMap));
}
// squared distance from a point to a line
inline qreal distsqr(const QPointF& pt, const QLineF& line)
{
......
......@@ -32,6 +32,7 @@ class PerspectiveAssistant : public KisAbstractPerspectiveGrid, public KisPainti
Q_OBJECT
public:
PerspectiveAssistant(QObject * parent = 0);
KisPaintingAssistantSP clone(QMap<KisPaintingAssistantHandleSP, KisPaintingAssistantHandleSP> &handleMap) const override;
QPointF adjustPosition(const QPointF& point, const QPointF& strokeBegin) override;
void endStroke() override;
QPointF buttonPosition() const override;
......@@ -52,6 +53,7 @@ private:
bool quad(QPolygonF& out) const;
// finds the transform from perspective coordinates (a unit square) to the document
bool getTransform(QPolygonF& polyOut, QTransform& transformOut) const;
explicit PerspectiveAssistant(const PerspectiveAssistant &rhs, QMap<KisPaintingAssistantHandleSP, KisPaintingAssistantHandlebSP> &handleMap);
// which direction to snap to (in transformed coordinates)
QLineF m_snapLine;
......
......@@ -35,6 +35,16 @@ RulerAssistant::RulerAssistant()
{
}
KisPaintingAssistantSP RulerAssistant::clone(QMap<KisPaintingAssistantHandleSP, KisPaintingAssistantHandleSP> &handleMap) const
{
return KisPaintingAssistantSP(new RulerAssistant(*this, handleMap));
}
RulerAssistant::RulerAssistant(const RulerAssistant &rhs, QMap<KisPaintingAssistantHandleSP, KisPaintingAssistantHandleSP> &handleMap)
: KisPaintingAssistant(rhs, handleMap)
{
}
QPointF RulerAssistant::project(const QPointF& pt) const
{
Q_ASSERT(isAssistantComplete());
......
......@@ -19,6 +19,8 @@
#ifndef _RULER_ASSISTANT_H_
#define _RULER_ASSISTANT_H_
#include <QMap>
#include "kis_painting_assistant.h"
class Ruler;
......@@ -27,6 +29,7 @@ class RulerAssistant : public KisPaintingAssistant
{
public:
RulerAssistant();
KisPaintingAssistantSP clone(QMap<KisPaintingAssistantHandleSP, KisPaintingAssistantHandleSP> &handleMap) const override;
QPointF adjustPosition(const QPointF& point, const QPointF& strokeBegin) override;
QPointF buttonPosition() const override;
int numHandles() const override { return 2; }
......@@ -37,6 +40,7 @@ protected:
void drawCache(QPainter& gc, const KisCoordinatesConverter *converter, bool assistantVisible=true) override;
private:
QPointF project(const QPointF& pt) const;
explicit RulerAssistant(const RulerAssistant &rhs, QMap<KisPaintingAssistantHandleSP, KisPaintingAssistantHandleSP> &handleMap);
};
class RulerAssistantFactory : public KisPaintingAssistantFactory
......
......@@ -38,6 +38,17 @@ SplineAssistant::SplineAssistant()
{
}
SplineAssistant::SplineAssistant(const SplineAssistant &rhs, QMap<KisPaintingAssistantHandleSP, KisPaintingAssistantHandleSP> &handleMap)
: KisPaintingAssistant(rhs, handleMap)
, m_canvas(rhs.m_canvas)
{
}
KisPaintingAssistantSP SplineAssistant::clone(QMap<KisPaintingAssistantHandleSP, KisPaintingAssistantHandleSP> &handleMap) const
{
return KisPaintingAssistantSP(new SplineAssistant(*this, handleMap));
}
// parametric form of a cubic spline (B(t) = (1-t)^3 P0 + 3 (1-t)^2 t P1 + 3 (1-t) t^2 P2 + t^3 P3)
inline QPointF B(qreal t, const QPointF& P0, const QPointF& P1, const QPointF& P2, const QPointF& P3)
{
......
......@@ -27,6 +27,7 @@ class SplineAssistant : public KisPaintingAssistant
{
public:
SplineAssistant();
KisPaintingAssistantSP clone(QMap<KisPaintingAssistantHandleSP, KisPaintingAssistantHandleSP> &handleMap) const override;
QPointF adjustPosition(const QPointF& point, const QPointF& strokeBegin) override;
QPointF buttonPosition() const override;
int numHandles() const override { return 4; }
......@@ -37,6 +38,7 @@ protected:
void drawCache(QPainter& gc, const KisCoordinatesConverter *converter, bool assistantVisible=true) override;
private:
QPointF project(const QPointF& pt) const;
explicit SplineAssistant(const SplineAssistant &rhs, QMap<KisPaintingAssistantHandleSP, KisPaintingAssistantHandleSP> &handleMap);
/// used for getting the decoration so the bezier handles aren't drawn while editing
KisCanvas2* m_canvas;
......
......@@ -38,6 +38,18 @@ VanishingPointAssistant::VanishingPointAssistant()
{
}
VanishingPointAssistant::VanishingPointAssistant(const VanishingPointAssistant &rhs, QMap<KisPaintingAssistantHandleSP, KisPaintingAssistantHandlebSP> &handleMap)
: KisPaintingAssistant(rhs, handleMap)
, m_canvas(rhs.m_canvas)
, m_referenceLineDensity(rhs.referenceLineDensity)
{
}
KisPaintingAssistantSP VanishingPointAssistant::clone(QMap<KisPaintingAssistantHandleSP, KisPaintingAssistantHandleSP> &handleMap) const
{
return KisPaintingAssistantSP(new VanishingPointAssistant(*this, handleMap));
}
QPointF VanishingPointAssistant::project(const QPointF& pt, const QPointF& strokeBegin)
{
//Q_ASSERT(handles().size() == 1 || handles().size() == 5);
......
......@@ -45,6 +45,7 @@ class VanishingPointAssistant : public KisPaintingAssistant
{
public:
VanishingPointAssistant();
KisPaintingAssistantSP clone(QMap<KisPaintingAssistantHandleSP, KisPaintingAssistantHandleSP> &handleMap) const override;
QPointF adjustPosition(const QPointF& point, const QPointF& strokeBegin) override;
//virtual void endStroke();
QPointF buttonPosition() const override;
......@@ -63,6 +64,7 @@ protected:
void drawCache(QPainter& gc, const KisCoordinatesConverter *converter, bool assistantVisible=true) override;
private:
QPointF project(const QPointF& pt, const QPointF& strokeBegin);
explicit VanishingPointAssistant(const VanishingPointAssistant &rhs, QMap<KisPaintingAssistantHandleSP, KisPaintingAssistantHandlebSP> &handleMap);
KisCanvas2 *m_canvas;
float m_referenceLineDensity = 15.0;
......
......@@ -531,6 +531,16 @@ void KisAssistantTool::endPrimaryAction(KoPointerEvent *event)
m_canvas->updateCanvas(); // TODO update only the relevant part of the canvas
}
QList<KisPaintingAssistantSP> cloneAssistantList(const QList<KisPaintingAssistantSP> &list) const
{
QMap<KisPaintingAssistantHandleSP, KisPaintingAssistantHandleSP> handleMap;
QList<KisPaintingAssistantSP> clonedList;
for (auto i = list.begin(); i != list.end(); ++i) {
clonedList << i->clone(handleMap);
}
return clonedList;
}
void KisAssistantTool::addAssistant()
{
KUndo2Command *addAssistantCmd = new AddRemoveAssistantCommand(AddRemoveAssistantCommand::ADD, m_canvas, m_newAssistant);
......
......@@ -75,6 +75,11 @@ public:
QWidget *createOptionWidget() override;
/// clones the list of assistants
/// the originally shared handles will still be shared
/// the cloned assistants do not share any handle with the original assistants
QList<KisPaintingAssistantSP> cloneAssistantList(const QList<KisPaintingAssistantSP> &list) const;
private:
// adds and removes assistant.
// this is event is forwarded to the kis_painting_decoration class
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment