[graphics/krita] libs/resourcewidgets: Ask user on adding a tag with the same url in TagChooser

Agata Cacko null at kde.org
Fri Aug 6 15:20:56 BST 2021


Git commit 3f18671be4d2d773161776808cee776ade400254 by Agata Cacko.
Committed on 06/08/2021 at 14:03.
Pushed by tymond into branch 'master'.

Ask user on adding a tag with the same url in TagChooser

Before this commit, if the user tried to create a new tag
with the same url as an existing tag,
it would undelete the deleted tag, which means the assigned
resources would still be assigned.
This commit instead asks the user what they want to achieve,
and there are three options:
* Replace (overwrite) - this will reset assigned resources
* Undelete - this will cancel adding the tag and just
undelete the existing, deleted tag with all the assigned
resources it had before
* Cancel - no operation

This will be for both the tag tool button popup and
for the right-click in the resource chooser
(KisResourceItemContextMenu) because it uses the same
code.

BUG:439423

CCMAIL:kde-i18n-doc at kde.org

M  +39   -2    libs/resourcewidgets/KisTagChooserWidget.cpp
M  +8    -0    libs/resourcewidgets/KisTagChooserWidget.h

https://invent.kde.org/graphics/krita/commit/3f18671be4d2d773161776808cee776ade400254

diff --git a/libs/resourcewidgets/KisTagChooserWidget.cpp b/libs/resourcewidgets/KisTagChooserWidget.cpp
index 51f2d4c63c..07082d4ec7 100644
--- a/libs/resourcewidgets/KisTagChooserWidget.cpp
+++ b/libs/resourcewidgets/KisTagChooserWidget.cpp
@@ -17,6 +17,7 @@
 #include <QToolButton>
 #include <QGridLayout>
 #include <QComboBox>
+#include <QMessageBox>
 
 #include <klocalizedstring.h>
 #include <KisSqueezedComboBox.h>
@@ -26,6 +27,7 @@
 #include "KisResourceItemChooserContextMenu.h"
 #include "KisTagToolButton.h"
 #include "kis_debug.h"
+#include <KisTagResourceModel.h>
 
 class Q_DECL_HIDDEN KisTagChooserWidget::Private
 {
@@ -184,15 +186,50 @@ void KisTagChooserWidget::addTag(const QString &tag)
     addTag(tag, 0);
 }
 
+KisTagChooserWidget::OverwriteDialogOptions KisTagChooserWidget::overwriteTagDialog(KisTagChooserWidget* parent, bool tagIsActive)
+{
+    QString undeleteOption = !tagIsActive ? i18nc("Option in a dialog to undelete (reactivate) existing tag with its old assigned resources", "Restore previous tag")
+                                      : i18nc("Option in a dialog to use existing tag with its old assigned resources", "Use existing tag");
+    // if you use this simple cast, the order of buttons must match order of options in the enum
+    return (KisTagChooserWidget::OverwriteDialogOptions)QMessageBox::question(parent, i18nc("Dialog title", "Overwrite tag?"), i18nc("Question to the user in a dialog about creating a tag",
+                                                                                      "A tag with this unique name already exists. Do you want to replace it?"),
+                                       i18nc("Option in a dialog to discard the previously existing tag and creating a new one in its place", "Replace (overwrite) tag"),
+                                       undeleteOption, i18n("Cancel"));
+}
+
 void KisTagChooserWidget::addTag(const QString &tagName, KoResourceSP resource)
 {
-    d->model->addTag(tagName, false, {resource});
+    KisTagSP tagForUrl = d->model->tagForUrl(tagName);
+    if (!tagForUrl.isNull()) {
+        int response = overwriteTagDialog(this, tagForUrl->active());
+        if (response == Undelete) { // Undelete
+            d->model->setTagActive(tagForUrl);
+            KisTagResourceModel(d->resourceType).tagResource(tagForUrl, resource->resourceId());
+            d->model->sort(KisAllTagsModel::Name);
+            return;
+        } else if (response == Cancel) { // Cancel
+            return;
+        }
+    }
+    d->model->addTag(tagName, true, {resource}); // this will overwrite the tag
     d->model->sort(KisAllTagsModel::Name);
 }
 
 void KisTagChooserWidget::addTag(KisTagSP tag, KoResourceSP resource)
 {
-    d->model->addTag(tag, false, {resource});
+    KisTagSP tagForUrl = d->model->tagForUrl(tag->url());
+    if (!tagForUrl.isNull()) {
+        int response = overwriteTagDialog(this, tagForUrl->active());
+        if (response == Undelete) { // Undelete
+            d->model->setTagActive(tagForUrl);
+            KisTagResourceModel(d->resourceType).tagResource(tagForUrl, resource->resourceId());
+            d->model->sort(KisAllTagsModel::Name);
+            return;
+        } else if (response == Cancel) { // Cancel
+            return;
+        }
+    }
+    d->model->addTag(tag, true, {resource}); // this will overwrite the tag
     d->model->sort(KisAllTagsModel::Name);
 }
 
diff --git a/libs/resourcewidgets/KisTagChooserWidget.h b/libs/resourcewidgets/KisTagChooserWidget.h
index 122230a8e6..4147cd9dc3 100644
--- a/libs/resourcewidgets/KisTagChooserWidget.h
+++ b/libs/resourcewidgets/KisTagChooserWidget.h
@@ -114,6 +114,14 @@ private:
     /// \param index index is the index of the tag in the combobox
     void setCurrentIndex(int index);
 
+    enum OverwriteDialogOptions {
+        Replace,
+        Undelete,
+        Cancel
+    };
+
+    OverwriteDialogOptions overwriteTagDialog(KisTagChooserWidget* parent, bool undelete);
+
 private:
     class Private;
     Private* const d;



More information about the kde-i18n-doc mailing list