Skip to content

Commit

Permalink
Clear selections in a collection of files.
Browse files Browse the repository at this point in the history
  • Loading branch information
mathiasburger committed Jun 17, 2019
1 parent 3ee6f36 commit 5348d48
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
28 changes: 28 additions & 0 deletions pgimp/GimpFileCollection.py
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,34 @@ def merge_mask_layer_from(
)
return self

def clear_selection(
self,
timeout_in_seconds: float = None
):
"""
Clears active selections.
"""
script = textwrap.dedent(
"""
import gimp
from pgimp.gimp.parameter import get_json, return_json
from pgimp.gimp.file import XcfFile
files = get_json('__files__')
for file in files:
with XcfFile(file, save=True) as image:
gimp.pdb.gimp_selection_none(image)
return_json(None)
"""
)
self.execute_script_and_return_json(
script,
timeout_in_seconds=timeout_in_seconds
)



@classmethod
def create_from_pathname(cls, pathname: str):
"""
Expand Down
40 changes: 40 additions & 0 deletions pgimp/GimpFileCollectionTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# SPDX-License-Identifier: MIT

import os
import shutil
import tempfile
import textwrap

Expand Down Expand Up @@ -507,3 +508,42 @@ def test_merge_mask_layer_from_with_mask_not_available_in_files_in_both_collecti

assert np.all(dst_1.layer_to_numpy('Mask') == [[255], [255]])
assert ['Mask'] == dst_1.layer_names()


def test_clear_selection():
file_with_selection_original = file.relative_to(__file__, 'test-resources/selection.xcf')
with TempFile('.xcf') as file_with_selection:
shutil.copyfile(file_with_selection_original, file_with_selection)
collection = GimpFileCollection([file_with_selection])

selections_before = _has_selections(collection)
assert selections_before[file_with_selection]

collection.clear_selection(timeout_in_seconds=10)

selections_after = _has_selections(collection)
assert not selections_after[file_with_selection]

assert True


def _has_selections(collection):
result = collection.execute_script_and_return_json(
textwrap.dedent(
"""
import gimp
from pgimp.gimp.parameter import get_json, return_json
from pgimp.gimp.file import XcfFile
files = get_json('__files__')
selections = {}
for file in files:
with XcfFile(file, save=True) as image:
selections[file] = not gimp.pdb.gimp_selection_is_empty(image)
return_json(selections)
"""
),
timeout_in_seconds=10
)
return result

0 comments on commit 5348d48

Please sign in to comment.