← Back to team overview

phatch-dev team mailing list archive

Re: conditionally showing & hiding fields

 

Hi,

This was a tough one, but I hope I succeeded. Now it is possible to
> show/hide fields conditionally. The method is very simple. Just add a
> get_relevant_field_labels() method to the Action. It is very important
> that the list of labels has EXACTLY the same order as defined in the
> interface method.
> I've implemented for these actions already:
> modified phatch/actions/color_to_alpha.py
> modified phatch/actions/crop.py
> modified phatch/actions/save.py
> modified phatch/actions/watermark.py

Thanks for doing this! this feature is very important for usability.


> Try them out and see if you can find any bugs! Note that the update
> will only take place after the field lost focus by clicking somewhere
> else or moving your mouse away over the toolbar.

I find this a bit confusing. Is there a technical reason to only change when
losing focus?


>
> This opens a lot of nice possibilities. Please go through the actions
> and see which ones could profit from this. From now on there should
> not any field be shown which is not relevant. This means that for some
> actions we need to add a ChoiceField which shows/hide the other
> fields. This is for example the case for the rounded corners action.
> It also might allow for more actions to add extra options.
>
> Nadia, the crop action still need some (important) work. I added to
> the white board of the blueprint:
> https://blueprints.launchpad.net/phatch/+spec/crop-action

No problem, check the attached action :)

Best Regards,
Nadia


>
>
> Best regards,
>
> Stani
>
> --
> Phatch Photo Batch Processor - http://photobatch.stani.be
> SPE Python IDE - http://pythonide.stani.be
>
> _______________________________________________
> Mailing list: https://launchpad.net/~phatch-dev<https://launchpad.net/%7Ephatch-dev>
> Post to     : phatch-dev@xxxxxxxxxxxxxxxxxxx
> Unsubscribe : https://launchpad.net/~phatch-dev<https://launchpad.net/%7Ephatch-dev>
> More help   : https://help.launchpad.net/ListHelp
>
# Phatch - Photo Batch Processor
# Copyright (C) 2007-2008 www.stani.be
#
# 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 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.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see http://www.gnu.org/licenses/
#
# Phatch recommends SPE (http://pythonide.stani.be) for editing python files.

# Embedded icon is taken from www.openclipart.org (public domain)

from core import models
from core.translation import _t,new

OPTIONS = [
    _t('Auto'),
    _t('All'),
    _t('Custom')
]
#---PIL
def init():
    global Image, ImageOps
    import Image, ImageOps
    
def crop(image, crop=False, all=0, left=0, right=0, top=0, bottom=0):
    if crop == OPTIONS[0]:
        if image.mode == 'RGBA':
            box = image.split()[3].getbbox()
        else:
            box = image.getbbox()
        image = image.crop(box)
    elif crop == OPTIONS[1]:
        image = ImageOps.crop(image, border=all)
    else:
        w, h = image.size
        box = (
            left,
            top,
            w - right,
            h - bottom,
        )
        image = image.crop(box)
    return image

CHOICES = ['0', '1', '2', '5', '10', '20']

#---Phatch
class Action(models.Action):
    label       = _t('Crop')
    author      = 'Nadia Alramli'
    email       = 'mail@xxxxxxxxxxx'
    init        = staticmethod(init)
    pil         = staticmethod(crop)
    version     = '0.1'
    tags        = [_t('size'),_t('border')]
    update_size = True
    __doc__     = _t('Crop the image')
    
    def interface(self,fields):
        fields[_t('Crop')]   = self.ChoiceField(OPTIONS[0], choices=OPTIONS)
        fields[_t('All')]    = self.PixelField('1px', choices=CHOICES)
        fields[_t('Left')]   = self.PixelField('0px', choices=CHOICES)
        fields[_t('Right')]  = self.PixelField('0px', choices=CHOICES)
        fields[_t('Top')]    = self.PixelField('0px', choices=CHOICES)
        fields[_t('Bottom')] = self.PixelField('0px', choices=CHOICES)

    def values(self, info):
        #pixel fields
        width, height   = info[new('Pil','Size')]
        # pass absolute reference for relative pixel values such as %
        return super(Action, self).values(info, pixel_fields={
            'All'   :(width+height) / 2,
            'Left'  : width,
            'Right' : width,
            'Top'   : height,
            'Bottom': height,
        })

    def get_relevant_field_labels(self):
        """If this method is present, Phatch will only show relevant
        fields. 
        
        :returns: list of the field labels which are relevant
        :rtype: list of strings
        
        .. note:: 
        
            It is very important that the list of labels has EXACTLY
            the same order as defined in the interface method.
        """
        relevant = ['Crop']
        if self.get_field_string('Crop') == OPTIONS[1]:
            relevant.extend(
                ['All']
            )
        elif self.get_field_string('Crop') == OPTIONS[2]:
            relevant.extend(
                ['Top','Left','Bottom','Right']
            )
        return relevant
    
    icon = \
'x\xda\x01\x0f\t\xf0\xf6\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x000\x00\
\x00\x000\x08\x06\x00\x00\x00W\x02\xf9\x87\x00\x00\x00\x04sBIT\x08\x08\x08\
\x08|\x08d\x88\x00\x00\x08\xc6IDATh\x81\xed\x99ml[\xd5\x19\xc7\xff\xe7\x9c{\
\xaf}\xed&\xae\xed\xd4I\x1cRX\x1a\x9a\x86\xb5\xa1*[\xd3\x97\xa1\x82B\xb5}\
\xd9\x97\xbd|\xa1b\x13\xe2\x03SU6\xd1M\xac\x03M\x8c\x82\x04\x88\xa1U\x9b\xd4\
\x15\xb5\xa2\xbc\x08\xb4\xb4\xd3\x18]\xa0\xdb\x8a\nk\xd3\xb2\x90\xa2TaDj\x8b\
\x1d\x9a8ql\xc7\x8d\x13\xfb\xbe\xf9\xde{\xce>\\\x07\x15\xd6\xb4I!\t\x93\xf8}\
\xb1\xe4{\xef\xd1\xffy\xces\x9e\xf3\xbf\xe7\x02_\xf1\xc5p\xe8\xd0\xa1pggg\
\xeb\\\x9f\xa3\xf3!f\xae\xbc\xfa\xea\xab\x9b\x1d\xc7\xf9\xd8q\x9c\x9b\xe7\
\xfa\xec\xa2\x07\xf0\xd2K/5\x9b\xa6y\xd4q\x9cg\xb7m\xdbv\xa4\xa2i\xd6\xba\
\xc8\xfcI\xbb6===\xd1|>\xff\xef\xc6\xc6\xc6\x8bk\xd6\xac\xb9\x0b\x80\x04@\
\x06`\x03p\x16S\xdb5)\x97\xcb\x91K\x97.\xf5MLLd\x85\x10\xcb\xe1\t\x0f\x02\
\xf0\xcde\x1ci^\xd4]\x83\xc9\xc9I\x9fa\x18\xcfs\xce\xd7\n!\xee!\x84\xa4\x00T\
\xc1\xcb\xbc\xb5\x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d5\xb2e\xcb\x10\x0e\x87\x91H$PWW\'2\x99\x0c8\
\xe7\xf0\xfb\xfd\xa4P(@\xd34\x94J\xa5\xe2\xd4\xd4T\xff\xf8\xf8xO*\x95:\xff\
\xde{\xef}\x94H$R\x00\x8c\x8aX\x03\xde\xc2\x9e\x93\x9d\x90\x00 \x10\x08\xdc\
\xe2\xba\xee\x1b\xc9d\xb2O\x96\xe5\x9f\xc0\xb3\xb6\xd5\x00\x9ch4\xba\x841\
\xb6\x921\x06\xc6X\x841F\x18c\x90$\t\x94R\xd1\xda\xda\xfa\xd1\x9a5kn\xee\xea\
\xeaBCC\x03\xb2\xd9,\xb1m[\x84B!Q,\x16K###\'\xfb\xfa\xfa\x8e\x1c9rd\xa0P(h\
\x00\xca\xf0\xfa\xffT%\xdb\x9f\xcb\xf7\x13\x00\x92,\xcb\xdd\x8e\xe34\x13B\
\x1a8\xe7\x1c\x80\xac\xaajTU\xd5o\xca\xb2\xbc]\x96\xe5\x0eUU\xa1(\n(\xa5\x10\
Bp\xc6\xd8\x90\x10\xa2/\x99L>\xbdg\xcf\x9enUU%\xc6\x98 \x84\xf0\xa1\xa1\xa1\
\x97\xcf\x9f?\x7fb\xff\xfe\xfd\xfd\xf0j\xd8\x01\xa0_\x96\xe9/\xeceE\x02p\x93\
\xe38\xed\x92$\xfd\xc7\xb6m\xae(\xca\xb7eY\xbe7\x10\x08\xdc\x1e\x8dF#555LQ\
\x14X\x96U\xd04\xed\x94\xa6ioMLL\xf4\x99\xa6\xa9i\x9aVnoo\x0fp\xce%\x9f\xcf\
\'(\xa5$\x95J\x9dx\xe4\x91G\xf6\x97\xcb\xe5K\x00r\x95\x00l|\xce\x1dw&\x08\
\x00H\x92\x94\x0f\x87\xc3\x91H$\x82H$\x82\x9a\x9a\x1aTWW\xa7,\xcbJd2\x99S\
\x83\x83\x83\xc7S\xa9\xd4Ex\xa6\xcbWy\xae\x0c/\x9b\xb9\xbd{\xf7\xfev\xd9\xb2\
e?\xd7u=\xb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x8ce\x01\xdc\t\xe0\x16\xc6\xd8=\
\xf1x\xfc\xfd\x96\x96\x16\x11\x8f\xc7\'\xbf\xcc\xe2\x01\x00\x84\x90\t\xc6\
\x98P\x14%\xef\xf7\xfb\xd3\xa1PH\xd4\xd6\xd6fC\xa1\xd0S\x8b\xadm&>{6\xdaD)\
\xbd\x871\x16e\x8ce(\xa5\x1f\xd8\xb6\xddm\xdb\xf6\xa5EQ\xf7\x15\xff\x07\xfc\
\x17\tF\x13\xe0\x81\x7f;\x1a\x00\x00\x00\x00IEND\xaeB`\x82[\t[T'

Follow ups

References