File indexing completed on 2024-06-23 04:28:11

0001 /*
0002  *  SPDX-FileCopyrightText: 2017 Dmitry Kazakov <dimula73@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "ShapeGradientEditStrategy.h"
0008 
0009 #include <KoToolBase.h>
0010 #include <KoCanvasBase.h>
0011 #include <KoCanvasResourceProvider.h>
0012 #include <KoShapeManager.h>
0013 #include <KoShape.h>
0014 #include "kis_assert.h"
0015 #include "SelectionDecorator.h"
0016 #include <kundo2command.h>
0017 #include <KoSnapGuide.h>
0018 #include <KisSnapPointStrategy.h>
0019 
0020 #include "kis_debug.h"
0021 
0022 
0023 struct ShapeGradientEditStrategy::Private
0024 {
0025     Private(const QPointF &_start, KoShape *shape, KoFlake::FillVariant fillVariant)
0026         : start(_start),
0027           gradientHandles(fillVariant, shape)
0028     {
0029         previous = start;
0030     }
0031 
0032     QPointF start;
0033     QPointF previous;
0034     QPointF initialOffset;
0035     KoShapeGradientHandles gradientHandles;
0036     KoShapeGradientHandles::Handle::Type handleType {KoShapeGradientHandles::Handle::Type::None};
0037     QScopedPointer<KUndo2Command> intermediateCommand;
0038 };
0039 
0040 
0041 ShapeGradientEditStrategy::ShapeGradientEditStrategy(KoToolBase *tool,
0042                                                      KoFlake::FillVariant fillVariant,
0043                                                      KoShape *shape,
0044                                                      KoShapeGradientHandles::Handle::Type startHandleType,
0045                                                      const QPointF &clicked)
0046     : KoInteractionStrategy(tool)
0047     , m_d(new Private(clicked, shape, fillVariant))
0048 {
0049     KIS_SAFE_ASSERT_RECOVER_RETURN(shape);
0050 
0051     m_d->handleType = startHandleType;
0052 
0053     KoShapeGradientHandles::Handle handle = m_d->gradientHandles.getHandle(m_d->handleType);
0054     m_d->initialOffset = handle.pos - clicked;
0055 
0056     KisSnapPointStrategy *strategy = new KisSnapPointStrategy();
0057     Q_FOREACH (const KoShapeGradientHandles::Handle &h, m_d->gradientHandles.handles()) {
0058         strategy->addPoint(h.pos);
0059     }
0060     tool->canvas()->snapGuide()->addCustomSnapStrategy(strategy);
0061 }
0062 
0063 ShapeGradientEditStrategy::~ShapeGradientEditStrategy()
0064 {
0065 }
0066 
0067 void ShapeGradientEditStrategy::handleMouseMove(const QPointF &mouseLocation, Qt::KeyboardModifiers modifiers)
0068 {
0069     const QPointF snappedPosition = tool()->canvas()->snapGuide()->snap(mouseLocation, m_d->initialOffset, modifiers);
0070     const QPointF diff = snappedPosition - m_d->previous;
0071     m_d->previous = snappedPosition;
0072     KUndo2Command *cmd = m_d->gradientHandles.moveGradientHandle(m_d->handleType, diff);
0073     cmd->redo();
0074     if (cmd) {
0075         if (m_d->intermediateCommand) {
0076             m_d->intermediateCommand->mergeWith(cmd);
0077         } else {
0078             m_d->intermediateCommand.reset(cmd);
0079         }
0080     }
0081 }
0082 
0083 KUndo2Command *ShapeGradientEditStrategy::createCommand()
0084 {
0085     return m_d->intermediateCommand.take();
0086 }
0087 
0088 void ShapeGradientEditStrategy::finishInteraction(Qt::KeyboardModifiers modifiers)
0089 {
0090     Q_UNUSED(modifiers);
0091     tool()->canvas()->snapGuide()->reset();
0092 }
0093 
0094 void ShapeGradientEditStrategy::paint(QPainter &painter, const KoViewConverter &converter)
0095 {
0096     Q_UNUSED(painter);
0097     Q_UNUSED(converter);
0098 }
0099