Source code for tagit.program.bfs
"""Breadth-first-search for Tagging.
Goes through the images in a breadth-first manner. When a tag is
added to some selected images, they are removed from the view.
When no more images can be removed, the process repeates with
the all of the groups selected before. This continues until the
user stops the program.
This program enables the user to create hierarchies quickly.
Part of the tagit module.
A copy of the license is provided with the project.
Author: Matthias Baumgartner, 2016
"""
# IMPORTS
# INNER-MODULE IMPORTS
from program import Program
from ..basics import subset
from ..bindings import Binding
# EXPORTS
__all__ = ('Pgm_BFS', )
## CODE ##
[docs]class Pgm_BFS(Program):
def do_save(self, path):
import json
json.dump(self.sets, open(path, 'w'))
def do_load(self, path):
import json
self.sets = json.load(open(path))
self._filter.selected_show(self.sets[0]) # Add exclusive token
def decorate(self, wx):
self._add_tag = wx.browser.controller.add_tag
wx.browser.controller.add_tag = self.on_add_tag
self._browser = wx.browser.controller
self._filter = wx.filter.controller
def undecorate(self, wx):
wx.browser.controller.add_tag = self._add_tag
def on_start(self):
self.base_filter = self._filter.active[:], self._filter.inactive[:]
#self.base = self._browser.images[:] # DEBUG
self.sets = [self._browser.images[:]]
if len(self.sets[0]) == 0:
self.stop()
#print [sorted([self.base.index(im) for im in s]) for s in self.sets] # DEBUG
def on_stop(self):
self._filter.active, self._filter.inactive = self.base_filter
self._filter.redraw()
self._filter.apply()
def on_pause(self):
self.filter_paused = self._filter.active
def on_resume(self):
self._filter.active = self.filter_paused
self._filter.inactive = []
self._filter.redraw()
self._filter.apply()
def on_add_tag(self, text, *args, **kwargs):
#print "Tag added!", len(self.sets), len(self.sets[0])
#print [sorted([self.base.index(im) for im in s]) for s in self.sets] # DEBUG
# Allows empty tag on purpose as a way to skip
sel = self._browser.selection
if len(sel) > 0:
vis = self.sets.pop(0)
diff = [img for img in vis if img not in sel]
if len(text.strip()) > 0:
self.sets.append(sel) # Add new set
if len(diff) > 0:
self.sets.insert(0, diff) # Replace front with remaining images
#print [sorted([self.base.index(im) for im in s]) for s in self.sets] # DEBUG
# Actually add the tag
r_value = self._add_tag(text, *args, **kwargs)
# Continue
if len(sel) > 0:
if len(self.sets) == 0:
self.stop()
elif subset(self.sets[0], vis):
# Shortcut: delete images
self._browser.on_keyboard(self._browser.widget, ((Binding.DEL, Binding.DEL), []))
else:
# Temporarily reroute filter fu's
apply_, self._filter.apply = self._filter.apply, lambda: False
redraw_, self._filter.redraw = self._filter.redraw, lambda: True
self._filter.go_back(len(self._filter.active)) # Remove all filters
# Revert filter activity
self._filter.apply = apply_
self._filter.redraw = redraw_
self._filter.selected_show(self.sets[0]) # Add exclusive token
#self._filter.parent.dispatch('on_show_selected', self.sets[0]) # FIXME: Could use event
return r_value
## EOF ##