"""
Part of the tagit module.
A copy of the license is provided with the project.
Author: Matthias Baumgartner, 2016
"""
# IMPORTS
import pylab as plt
# INNER-MODULE IMPORTS
from tags import CSidebox_Tags
# EXPORTS
__all__ = ('CSidebox_Tags_Browser', )
## CODE ##
[docs]class CSidebox_Tags_Browser(CSidebox_Tags):
"""Show tags if images displayed. Highlight tags of selected images and the cursor.
"""
def __init__(self, widget, model, settings, parent=None):
super(CSidebox_Tags_Browser, self).__init__(widget, model, settings, parent)
# Event bindings
self.parent.bind(on_cursor=self._upd_cursor)
self.parent.bind(on_selection=self._upd_selection)
self.parent.bind(on_results_change=self._upd_results)
#self.parent.bind(on_images_change=self._upd_images)
self.tags_cursor = []
self.tags_selection = []
self.tags_results = []
def __del__(self):
self.parent.unbind(on_cursor=self._upd_cursor)
self.parent.unbind(on_selection=self._upd_selection)
self.parent.unbind(on_results_change=self._upd_results)
#self.parent.unbind(on_images_change=self._upd_images)
def _upd_cursor(self, browser, cursor):
"""When the cursor has changed."""
self.tags_cursor = self.model.tags.get(cursor)
self.update()
return False
def _upd_selection(self, browser, selection):
"""When the selection has changed."""
self.tags_selection = self._tags_from_images(selection)
self.update()
return False
def _upd_results(self, filter_, images):
"""When results have changes (i.e. filter was applied)."""
self.tags_results = self._tags_from_images(images)
self._upd_hist()
self.update()
return False
def _upd_hist(self):
# Helper functions
median = lambda lst: sorted(lst)[int(len(lst) / 2.0)]
to_string = lambda c: str(hex(int(255.0 * c)))[2:].ljust(2, '0')
# Histogram and range values
tags_hist = self.model.tags.histogram()
if len(self.tags_results) > 0:
h_values = [tags_hist[t] for t in self.tags_results]
#v_lo, v_hi = min(hist.values()), max(hist.values())
v_lo, v_hi = min(h_values), median(h_values)
s_lo, s_hi = 0.0, 0.875
def to_col(h_value):
cnt = float(h_value)
freq = min((cnt - v_lo) / (v_hi - v_lo), 1.0)
#freq = (1.0 - freq) * (s_hi - s_lo) + s_lo # Alternative: Reverse color map
freq = (freq) * (s_hi - s_lo) + s_lo
red, green, blue = plt.cm.YlOrRd(freq)[:3]
return '#' + to_string(red) + to_string(green) + to_string(blue)
self.tags_col = dict([(tag, to_col(tags_hist[tag])) for tag in self.tags_results])
[docs] def update(self):
"""Update the sidebox widget."""
self.widget.update(
self.tags_results
, self.tags_selection
, self.tags_cursor
, self.tags_col
)
## EOF ##