Commit 9d099549 authored by Tusooa Zhu's avatar Tusooa Zhu 🔼
Browse files

Add undo commands for adding/removing assistants

parent b1f02c76
/*
* Copyright (c) 2008 Cyrille Berger <cberger@cberger.net>
* Copyright (c) 2010 Geoffry Song <goffrie@gmail.com>
* Copyright (c) 2017 Scott Petrovic <scottpetrovic@gmail.com>
* Copyright (c) 2019 Tusooa Zhu <tusooa@vista.aero>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "AddRemoveAssistantCommand.h"
#include <kis_canvas2.h>
#include <kis_abstract_perspective_grid.h>
#include <KisViewManager.h>
#include <kis_canvas_resource_provider.h>
#include <kis_painting_assistants_decoration.h>
AddRemoveAssistantCommand::AddRemoveAssistantCommand(Type type, QPointer<KisCanvas2> canvas, KisPaintingAssistantSP assistant, KUndo2Command *parent)
: KUndo2Command((type == ADD ? kundo2_i18n("Add Assistant") : kundo2_i18n("Remove Assistant")), parent)
, m_type(type)
, m_canvas(canvas.data())
, m_assistant(assistant)
{
}
AddRemoveAssistantCommand::~AddRemoveAssistantCommand()
{
}
void AddRemoveAssistantCommand::undo()
{
if (m_type == ADD) {
removeAssistant();
} else {
addAssistant();
}
}
void AddRemoveAssistantCommand::redo()
{
if (m_type == ADD) {
addAssistant();
} else {
removeAssistant();
}
}
void AddRemoveAssistantCommand::addAssistant()
{
m_canvas->paintingAssistantsDecoration()->addAssistant(m_assistant);
KisAbstractPerspectiveGrid* grid = dynamic_cast<KisAbstractPerspectiveGrid*>(m_assistant.data());
if (grid) {
m_canvas->viewManager()->canvasResourceProvider()->addPerspectiveGrid(grid);
}
}
void AddRemoveAssistantCommand::removeAssistant()
{
KisAbstractPerspectiveGrid* grid = dynamic_cast<KisAbstractPerspectiveGrid*>(m_assistant.data());
if (grid) {
m_canvas->viewManager()->canvasResourceProvider()->removePerspectiveGrid(grid);
}
m_canvas->paintingAssistantsDecoration()->removeAssistant(m_assistant);
}
/*
* Copyright (c) 2019 Tusooa Zhu <tusooa@vista.aero>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef ADD_REMOVE_ASSISTANT_COMMAND_H_
#define ADD_REMOVE_ASSISTANT_COMMAND_H_
#include <kundo2command.h>
#include <kis_painting_assistant.h>
#include <kis_types.h>
#include <QPointer>
class KisCanvas2;
class AddRemoveAssistantCommand : public KUndo2Command
{
public:
enum Type {
ADD,
REMOVE
};
AddRemoveAssistantCommand(Type type, QPointer<KisCanvas2> canvas, KisPaintingAssistantSP assistant, KUndo2Command *parent = 0);
~AddRemoveAssistantCommand() override;
void undo() override;
void redo() override;
private:
void addAssistant();
void removeAssistant();
Type m_type;
QPointer<KisCanvas2> m_canvas;
KisPaintingAssistantSP m_assistant;
};
#endif
......@@ -12,6 +12,7 @@ set(kritaassistanttool_SOURCES
RulerAssistant.cc
SplineAssistant.cc
VanishingPointAssistant.cc
AddRemoveAssistantCommand.cpp
)
ki18n_wrap_ui(kritaassistanttool_SOURCES AssistantsToolOptions.ui )
......
......@@ -48,6 +48,8 @@
#include <kis_painting_assistants_decoration.h>
#include "kis_global.h"
#include "VanishingPointAssistant.h"
#include "AddRemoveAssistantCommand.h"
#include <kis_undo_adapter.h>
#include <math.h>
......@@ -531,27 +533,22 @@ void KisAssistantTool::endPrimaryAction(KoPointerEvent *event)
void KisAssistantTool::addAssistant()
{
m_canvas->paintingAssistantsDecoration()->addAssistant(m_newAssistant);
KUndo2Command *addAssistantCmd = new AddRemoveAssistantCommand(AddRemoveAssistantCommand::ADD, m_canvas, m_newAssistant);
m_canvas->viewManager()->undoAdapter()->addCommand(addAssistantCmd);
m_handles = m_canvas->paintingAssistantsDecoration()->handles();
m_canvas->paintingAssistantsDecoration()->setSelectedAssistant(m_newAssistant);
updateToolOptionsUI(); // vanishing point assistant will get an extra option
KisAbstractPerspectiveGrid* grid = dynamic_cast<KisAbstractPerspectiveGrid*>(m_newAssistant.data());
if (grid) {
m_canvas->viewManager()->canvasResourceProvider()->addPerspectiveGrid(grid);
}
m_newAssistant.clear();
}
void KisAssistantTool::removeAssistant(KisPaintingAssistantSP assistant)
{
KisAbstractPerspectiveGrid* grid = dynamic_cast<KisAbstractPerspectiveGrid*>(assistant.data());
if (grid) {
m_canvas->viewManager()->canvasResourceProvider()->removePerspectiveGrid(grid);
}
m_canvas->paintingAssistantsDecoration()->removeAssistant(assistant);
m_handles = m_canvas->paintingAssistantsDecoration()->handles();
KUndo2Command *removeAssistantCmd = new AddRemoveAssistantCommand(AddRemoveAssistantCommand::REMOVE, m_canvas, assistant);
m_canvas->viewManager()->undoAdapter()->addCommand(removeAssistantCmd);
m_handles = m_canvas->paintingAssistantsDecoration()->handles();
m_canvas->paintingAssistantsDecoration()->deselectAssistant();
updateToolOptionsUI();
}
......
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