Bug 441704

Summary: Modified annotations are not saved
Product: [Applications] krita Reporter: grum999
Component: ScriptingAssignee: Krita Bugs <krita-bugs-null>
Status: RESOLVED FIXED    
Severity: normal    
Priority: NOR    
Version: 5.0.0-beta1   
Target Milestone: ---   
Platform: Other   
OS: Linux   
Latest Commit: Version Fixed In:
Sentry Crash Report:

Description grum999 2021-08-29 14:08:11 UTC
SUMMARY

Using the Python Scripting API, in some case the annotations are not saved.


STEPS TO REPRODUCE
1. Add annotation
2. Save document 
3. Modify an existing annotation
4. Save document 
5. Close document 
6. Load document, check annotation

OBSERVED RESULT
Added annotation from step 1 are here, but without modification made on step 3

EXPECTED RESULT
Modified annotation must be saved


ADDITIONAL INFORMATION
Here an example that can be executed in scripter:

```
from krita import *

fileName="/home/grum/test.kra"

doc = Krita.instance().createDocument(300, 300, "Test", "RGBA", "U8", "", 300.0)
#Krita.instance().activeWindow().addView(doc)

# -- add annotations --
for i in range(3):
    text=f"encoded data {i}={i*' ** '}"
    doc.setAnnotation(f'test{i}', f"added {i}", QByteArray(text.encode()))

print("-add------------")
for annotation in doc.annotationTypes():
    ba=doc.annotation(annotation)
    print("annotation: ", annotation, " / description: ", doc.annotationDescription(annotation), " / Content: ", bytes(ba).decode())
   
print("-save document---------")
doc.saveAs(fileName)


# -- update annotations --

for i in range(3):
    text=f"encoded data {i}={i*' ++ '}"
    doc.setAnnotation(f'test{i}', f"modified {i}", QByteArray(text.encode()))

print("-update---------")
for annotation in doc.annotationTypes():
    ba=doc.annotation(annotation)
    print("annotation: ", annotation, " / description: ", doc.annotationDescription(annotation), " / Content: ", bytes(ba).decode())

print("-save document---------")
doc.save()
doc.close()



doc=Krita.instance().openDocument(fileName)

print("-loaded------------")
for annotation in doc.annotationTypes():
    ba=doc.annotation(annotation)
    print("annotation: ", annotation, " / description: ", doc.annotationDescription(annotation), " / Content: ", bytes(ba).decode())

doc.close()
```



Result:
```
-add------------
annotation:  test0  / description:  added 0  / Content:  encoded data 0=
annotation:  test1  / description:  added 1  / Content:  encoded data 1= ** 
annotation:  test2  / description:  added 2  / Content:  encoded data 2= **  ** 
-save document---------
-update---------
annotation:  test0  / description:  modified 0  / Content:  encoded data 0=
annotation:  test1  / description:  modified 1  / Content:  encoded data 1= ++ 
annotation:  test2  / description:  modified 2  / Content:  encoded data 2= ++  ++ 
-save document---------
-loaded------------
annotation:  test0  / description:  added 0  / Content:  encoded data 0=
annotation:  test1  / description:  added 1  / Content:  encoded data 1= ** 
annotation:  test2  / description:  added 2  / Content:  encoded data 2= **  ** 
```
We can see that modification are not taken in account.


This is because when an annotation is added/removed, document is marked as modified.
But if document is in saved state (not modified), a modification of an existing annotation don't mark document as modified and save action does nothing (as krita consider document is unmodified)


In https://invent.kde.org/graphics/krita/-/blob/master/libs/image/kis_image.cc

```
void KisImage::addAnnotation(KisAnnotationSP annotation)
{
    // Find the icc annotation, if there is one
    vKisAnnotationSP_it it = m_d->annotations.begin();
    while (it != m_d->annotations.end()) {
        if ((*it)->type() == annotation->type()) {
            *it = annotation;
            emit sigImageModified();
            return;
        }
        ++it;
    }
    m_d->annotations.push_back(annotation);
    setModifiedWithoutUndo();
}
```

Line 1708, use of `setModifiedWithoutUndo();` instead of `emit sigImageModified();` will solve the problem.



Grum999
Comment 1 Halla Rempt 2022-06-28 08:05:21 UTC
Git commit d771a563238e6c708df13d12c802b6f583246fb8 by Halla Rempt.
Committed on 28/06/2022 at 08:05.
Pushed by rempt into branch 'master'.

Set the image to modified when changing an annotation

M  +1    -1    libs/image/kis_image.cc

https://invent.kde.org/graphics/krita/commit/d771a563238e6c708df13d12c802b6f583246fb8
Comment 2 Halla Rempt 2022-06-28 08:05:39 UTC
Git commit a40c05890cdce2685ff28cc1efcdb3b0886ab714 by Halla Rempt.
Committed on 28/06/2022 at 08:05.
Pushed by rempt into branch 'krita/5.1'.

Set the image to modified when changing an annotation
(cherry picked from commit 483cd34f0fe597c1f8dd533e7516fe75933badc7)

M  +1    -1    libs/image/kis_image.cc

https://invent.kde.org/graphics/krita/commit/a40c05890cdce2685ff28cc1efcdb3b0886ab714