Skip to content

armgit / krita

Public Imported from GitHub
0

Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.

C++

Fix renaming of File, Filter and Generator Layers to be undoable

Dmitry Kazakov72af5096bbc6
diff --git a/libs/command/kis_command_utils.cpp b/libs/command/kis_command_utils.cpp
index 4ef2b0cbf8..a415c6b0e4 100644
--- a/libs/command/kis_command_utils.cpp
+++ b/libs/command/kis_command_utils.cpp
@@ -198,6 +198,29 @@ namespace KisCommandUtils
KUndo2Command::undo();
}
+ KUndo2Command* composeCommands(KUndo2Command *parent, KUndo2Command *cmd) {
+ KIS_SAFE_ASSERT_RECOVER(cmd) {
+ cmd = new KUndo2Command(kundo2_noi18n("failed"));
+ }
+
+ KIS_SAFE_ASSERT_RECOVER_NOOP(!cmd->hasParent());
+ KIS_SAFE_ASSERT_RECOVER_NOOP(!parent || !parent->hasParent());
+
+ if (!parent) return cmd;
+
+ if (CompositeCommand *compositeParent = dynamic_cast<CompositeCommand*>(parent)) {
+ compositeParent->addCommand(cmd);
+ return parent;
+ }
+
+ CompositeCommand *newCompositeParent = new CompositeCommand();
+ newCompositeParent->setText(parent->text());
+ newCompositeParent->addCommand(parent);
+ newCompositeParent->addCommand(cmd);
+
+ return newCompositeParent;
+ }
+
void redoAndMergeIntoAccumulatingCommand(KUndo2Command *cmd, QScopedPointer<KUndo2Command> &accumulatingCommand)
{
cmd->redo();
diff --git a/libs/command/kis_command_utils.h b/libs/command/kis_command_utils.h
index 53c9c02802..7ecfee8102 100644
--- a/libs/command/kis_command_utils.h
+++ b/libs/command/kis_command_utils.h
@@ -131,6 +131,43 @@ namespace KisCommandUtils
QVector<KUndo2Command*> m_commands;
};
+ /**
+ * A simple helper function for monadic composition of undo commands
+ *
+ * \p parent and \p cmd commands are composed into a single CompositeCommand
+ * command. The name of the new command is inherited from \p parent.
+ *
+ * If \p parent is already a CompositeCommand, then \p cmd is just
+ * appended to the end of of it.
+ *
+ * If \p parent is nullptr, then a mere \p cmd is returned
+ *
+ * WARNIGN: neither of the commands should have a parent! We simply
+ * cannot compose a command with a parent!
+ *
+ * Usage:
+ *
+ * \code{.cpp}
+ *
+ * KUndo2Command *cmd = nullptr;
+ * if (propertyXChanged) {
+ * cmd = composeCommands(cmd, new ChangePropertyXCommand(...));
+ * }
+ * if (propertyYChanged) {
+ * cmd = composeCommands(cmd, new ChangePropertyYCommand(...));
+ * }
+ * if (propertyZChanged) {
+ * cmd = composeCommands(cmd, new ChangePropertyZCommand(...));
+ * }
+ * if (cmd) {
+ * m_view->undoAdapter()->addCommand(cmd);
+ * }
+ *
+ * \endcode
+ */
+ KRITACOMMAND_EXPORT
+ KUndo2Command* composeCommands(KUndo2Command *parent, KUndo2Command *cmd);
+
/**
* A simple function to merge down commands in iterative actions, like the
* ones we use in shape manipulations. The function takes ownership of \p cmd
diff --git a/libs/ui/dialogs/kis_dlg_generator_layer.cpp b/libs/ui/dialogs/kis_dlg_generator_layer.cpp
index 6ef334ddde..f03f77b833 100644
--- a/libs/ui/dialogs/kis_dlg_generator_layer.cpp
+++ b/libs/ui/dialogs/kis_dlg_generator_layer.cpp
@@ -19,10 +19,12 @@
#include <kis_paint_device.h>
#include <kis_transaction.h>
#include <commands/kis_change_filter_command.h>
+#include <commands/KisNodeRenameCommand.h>
#include <kis_generator_layer.h>
#include <KisViewManager.h>
#include <KisDocument.h>
#include <KisGlobalResourcesInterface.h>
+#include <kis_command_utils.h>
#include <kis_icon_utils.h>
#define UPDATE_DELAY 100 /*ms */
@@ -79,7 +81,7 @@ void KisDlgGeneratorLayer::saveLayer()
* the user has accepted the changes.
*/
if (isEditing) {
- layer->setName(layerName());
+ KUndo2Command *cmd = nullptr;
KisFilterConfigurationSP configAfter(configuration());
Q_ASSERT(configAfter);
@@ -87,13 +89,22 @@ void KisDlgGeneratorLayer::saveLayer()
QString xmlAfter = configAfter->toXML();
if (xmlBefore != xmlAfter) {
- KisChangeFilterCmd *cmd
- = new KisChangeFilterCmd(layer,
- configBefore,
- configAfter->cloneWithResourcesSnapshot());
+ cmd = KisCommandUtils::composeCommands(cmd,
+ new KisChangeFilterCmd(layer,
+ configBefore,
+ configAfter->cloneWithResourcesSnapshot()));
+ }
+
+ if (layer->name() != layerName()) {
+ cmd = KisCommandUtils::composeCommands(cmd,
+ new KisNodeRenameCommand(layer,
+ layer->name(),
+ layerName()));
+ }
+
+ if (cmd) {
m_view->undoAdapter()->addCommand(cmd);
- m_view->document()->setModified(true);
}
} else {
KIS_ASSERT_RECOVER_RETURN(layer);
diff --git a/libs/ui/kis_layer_manager.cc b/libs/ui/kis_layer_manager.cc
index c8d1101a56..0db2d7b62c 100644
--- a/libs/ui/kis_layer_manager.cc
+++ b/libs/ui/kis_layer_manager.cc
@@ -261,35 +261,38 @@ void KisLayerManager::layerProperties()
KisFileLayerSP fileLayer = KisFileLayerSP(dynamic_cast<KisFileLayer*>(layer.data()));
if (adjustmentLayer && !multipleLayersSelected) {
-
KisPaintDeviceSP dev = adjustmentLayer->projection();
KisDlgAdjLayerProps dlg(adjustmentLayer, adjustmentLayer.data(), dev, m_view, adjustmentLayer->filter().data(), adjustmentLayer->name(), i18n("Filter Layer Properties"), m_view->mainWindow(), "dlgadjlayerprops");
dlg.resize(dlg.minimumSizeHint());
-
KisFilterConfigurationSP configBefore(adjustmentLayer->filter());
KIS_ASSERT_RECOVER_RETURN(configBefore);
QString xmlBefore = configBefore->toXML();
-
if (dlg.exec() == QDialog::Accepted) {
-
- adjustmentLayer->setName(dlg.layerName());
+ KUndo2Command *cmd = nullptr;
KisFilterConfigurationSP configAfter(dlg.filterConfiguration());
Q_ASSERT(configAfter);
QString xmlAfter = configAfter->toXML();
if(xmlBefore != xmlAfter) {
- KisChangeFilterCmd *cmd
- = new KisChangeFilterCmd(adjustmentLayer,
- configBefore->cloneWithResourcesSnapshot(),
- configAfter->cloneWithResourcesSnapshot());
- // FIXME: check whether is needed
- cmd->redo();
+ cmd = KisCommandUtils::composeCommands(cmd,
+ new KisChangeFilterCmd(adjustmentLayer,
+ configBefore->cloneWithResourcesSnapshot(),
+ configAfter->cloneWithResourcesSnapshot()));
+ }
+
+ if (adjustmentLayer->name() != dlg.layerName()) {
+ cmd = KisCommandUtils::composeCommands(cmd,
+ new KisNodeRenameCommand(adjustmentLayer,
+ adjustmentLayer->name(),
+ dlg.layerName()));
+ }
+
+ if (cmd) {
m_view->undoAdapter()->addCommand(cmd);
- m_view->document()->setModified(true);
}
}
else {
@@ -323,19 +326,30 @@ void KisLayerManager::layerProperties()
QMessageBox::critical(m_view->mainWindow(), i18nc("@title:window", "Krita"), i18n("No file name specified"));
return;
}
- fileLayer->setName(dlg.layerName());
-
- if (fileNameOld!= fileNameNew || scalingMethodOld != scalingMethodNew || scalingFilterOld != scalingFilterNew) {
- KisChangeFileLayerCmd *cmd
- = new KisChangeFileLayerCmd(fileLayer,
- basePath,
- fileNameOld,
- scalingMethodOld,
- scalingFilterOld,
- basePath,
- fileNameNew,
- scalingMethodNew,
- scalingFilterNew);
+
+ KUndo2Command *cmd = nullptr;
+
+ if (fileNameOld != fileNameNew || scalingMethodOld != scalingMethodNew
+ || scalingFilterOld != scalingFilterNew) {
+
+ cmd = KisCommandUtils::composeCommands(cmd,
+ new KisChangeFileLayerCmd(fileLayer,
+ basePath,
+ fileNameOld,
+ scalingMethodOld,
+ scalingFilterOld,
+ basePath,
+ fileNameNew,
+ scalingMethodNew,
+ scalingFilterNew));
+ }
+
+ if (fileLayer->name() != dlg.layerName()) {
+ cmd = KisCommandUtils::composeCommands(cmd,
+ new KisNodeRenameCommand(fileLayer, fileLayer->name(), dlg.layerName()));
+ }
+
+ if (cmd) {
m_view->undoAdapter()->addCommand(cmd);
}
}