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

Fixed a bug where adding a guide may create two cmds

When KisGuidesManager's setUnitType() is called when the mouse is
dragged onto the canvas, slotUploadConfigToDocument() will upload
the guides config that includes the newly created guide, resulting
one additional unexpected "Add Guides" command. The current
implementation will guarantee one undo command is created only
after the release of the mouse/finger/pen.
parent 52533ef6
......@@ -112,11 +112,11 @@ void KisChangeGuidesCommand::Private::switchTo(const KisGuidesConfig &config)
doc->setGuidesConfig(curConfig);
}
KisChangeGuidesCommand::KisChangeGuidesCommand(KisDocument *doc, const KisGuidesConfig &newGuides)
KisChangeGuidesCommand::KisChangeGuidesCommand(KisDocument *doc, const KisGuidesConfig &oldGuides, const KisGuidesConfig &newGuides)
: KUndo2Command(kundo2_i18n("Edit Guides")),
m_d(new Private(doc, this))
{
m_d->oldGuides = doc->guidesConfig();
m_d->oldGuides = oldGuides;
m_d->newGuides = newGuides;
// update the undo command text
m_d->sameOrOnlyMovedOneGuideBetween(m_d->oldGuides, m_d->newGuides);
......
......@@ -30,8 +30,7 @@ class KisGuidesConfig;
class KisChangeGuidesCommand : public KUndo2Command
{
public:
KisChangeGuidesCommand(KisDocument *doc, const KisGuidesConfig &newGuides);
KisChangeGuidesCommand(KisDocument *document);
KisChangeGuidesCommand(KisDocument *doc, const KisGuidesConfig &oldGuides, const KisGuidesConfig &newGuides);
~KisChangeGuidesCommand() override;
void undo() override;
......
......@@ -84,6 +84,7 @@ struct KisGuidesManager::Private
QAction* createShortenedAction(const QString &text, const QString &parentId, QObject *parent);
void syncAction(const QString &actionName, bool value);
bool needsUndoCommand();
void createUndoCommandIfNeeded();
GuideHandle currentGuide;
......@@ -130,10 +131,6 @@ void KisGuidesManager::slotUploadConfigToDocument()
if (doc) {
KisSignalsBlocker b(doc);
if (m_d->shouldSetModified && m_d->needsUndoCommand()) {
KUndo2Command *cmd = new KisChangeGuidesCommand(doc, value);
doc->addCommand(cmd);
}
// we've made KisChangeGuidesCommand post-exec, so in all situations
// we will replace the whole config
doc->setGuidesConfig(value);
......@@ -198,13 +195,16 @@ void KisGuidesManager::Private::syncAction(const QString &actionName, bool value
bool KisGuidesManager::Private::needsUndoCommand()
{
const KisGuidesConfig &value = guidesConfig;
return !(oldGuidesConfig.hasSamePositionAs(guidesConfig));
}
void KisGuidesManager::Private::createUndoCommandIfNeeded()
{
KisDocument *doc = view ? view->document() : 0;
if (!doc) {
return false;
if (doc && needsUndoCommand()) {
KUndo2Command *cmd = new KisChangeGuidesCommand(doc, oldGuidesConfig, guidesConfig);
doc->addCommand(cmd);
}
return !(doc->guidesConfig().hasSamePositionAs(value));
}
void KisGuidesManager::syncActionsStatus()
......@@ -504,7 +504,6 @@ void KisGuidesManager::Private::initDragStart(const GuideHandle &guide,
qreal guideValue,
bool snapToStart)
{
oldGuidesConfig = guidesConfig;
currentGuide = guide;
dragStartDoc = dragStart;
dragStartGuidePos = guideValue;
......@@ -585,6 +584,7 @@ bool KisGuidesManager::Private::mouseReleaseHandler(const QPointF &docPos)
}
q->slotUploadConfigToDocument();
createUndoCommandIfNeeded();
return updateCursor(docPos) | result;
}
......@@ -673,6 +673,7 @@ bool KisGuidesManager::eventFilter(QObject *obj, QEvent *event)
const bool guideValid = m_d->isGuideValid(guide);
if (guideValid) {
m_d->oldGuidesConfig = m_d->guidesConfig;
m_d->initDragStart(guide, docPos, m_d->guideValue(guide), true);
}
......@@ -711,6 +712,8 @@ void KisGuidesManager::slotGuideCreationInProgress(Qt::Orientation orientation,
} else {
m_d->guidesConfig.setShowGuides(true);
m_d->oldGuidesConfig = m_d->guidesConfig;
if (orientation == Qt::Horizontal) {
QList<qreal> guides = m_d->guidesConfig.horizontalGuideLines();
guides.append(docPos.y());
......
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