#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# GIMP plugin to list guides in current image
# (c) Ofnuts 2020-2025
#
#   History:
#
#   v0.0: 2020-11-04 First (re-?)published version
#   v1.0: 2025-03-18 Convert to Gimp3
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published
#   by the Free Software Foundation; either version 3 of the License, or
#   (at your option) any later version.
#
#   This very file is the complete source code to the program.
#
#   If you make and redistribute changes to this code, please mark it
#   in reasonable ways as different from the original version. 
#   
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   The GPL v3 licence is available at: https://www.gnu.org/licenses/gpl-3.0.en.html

import gi

gi.require_version('Gimp', '3.0')
from gi.repository import Gimp
gi.require_version('Gegl', '0.4')
from gi.repository import Gegl
from gi.repository import GLib
import sys
from gimphelpers import *

def _(msg): return GLib.dgettext(None, msg)

def getGuides(image):
    guides=[[],[]] # Gimp.OrientationType.HORIZONTAL (0), Gimp.OrientationType.VERTICAL (1)
    gid=0
    while gid := image.find_next_guide(gid):
        guides[image.get_guide_orientation(gid)].append(image.get_guide_position(gid))
    return [sorted(gl) for gl in guides]

def listGuides(procedure: Gimp.ImageProcedure,
                run_mode: Gimp.RunMode,
                image: Gimp.Image,
                drawables: list[Gimp.Drawable],
                config: Gimp.ProcedureConfig,
                data: Any) -> Gimp.ValueArray:
    guides=getGuides(image)
    Gimp.message(_("Horizontal guides: {}\nVertical guides: {}").format(*guides))
    return successReturn(procedure)

### Registration
class AlignLayers(HelpedPlugin):
    author='Ofnuts'
    copyright='Ofnuts'
    year='2016-2025'
    procedures = [
        ProcedureDescription(
            'ofn3-list-guides',
            '*', Gimp.ProcedureSensitivityMask.ALWAYS,
            listGuides,
            menuLabel=_('List guides'),
            menuPath='<Image>/'+_('Image/Guides'),
            descShort=_('List existing guides in image'),
            descLong=_('List existing guides in image'),
        ),
    ]

Gimp.main(AlignLayers.__gtype__, sys.argv)
