From 44eca07b154eeb412041fd2ff75251afcdcc204b Mon Sep 17 00:00:00 2001 From: Gaetan Delannay Date: Wed, 25 Mar 2015 15:13:32 +0100 Subject: [PATCH] [gen] Calendar field: added the possibility to define the part of the legend that is specific to every layer. --- fields/calendar.py | 39 ++++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/fields/calendar.py b/fields/calendar.py index ae06178..12c2a74 100644 --- a/fields/calendar.py +++ b/fields/calendar.py @@ -146,7 +146,7 @@ class Totals: class Layer: '''A layer is a set of additional data that can be activated or not on top of calendar data. Currently available for timelines only.''' - def __init__(self, name, label, onCell, activeByDefault=False): + def __init__(self, name, label, onCell, activeByDefault=False, legend=None): # "name" must hold a short name or acronym, unique among all layers self.name = name # "label" is a i18n label that will be used to produce the layer name in @@ -168,6 +168,14 @@ class Layer: self.onCell = onCell # Is this layer activated by default ? self.activeByDefault = activeByDefault + # "legend" is a method that must produce legend items that are specific + # to this layer. The method must accept no arg and must return a list of + # objects (you can use class appy.Object) having these attributes: + # * name - the legend item name as shown in the calendar + # * style - the content of the "style" attribute that will be + # applied to the little square ("td" tag) for this item; + # * content - the content of this "td" (if any). + self.legend = legend # Layers will be chained: one layer will access the previous one in the # stack via attribute "previous". "previous" fields will automatically # be filled by the Calendar. @@ -185,6 +193,11 @@ class Layer: return self.previous.getCellInfo(obj, activeLayers, date, other, events, preComputed) + def getLegendItems(self, obj): + '''Returns the legend items by calling method in self.legend''' + if not self.legend: return + return self.legend(obj) + # ------------------------------------------------------------------------------ class Event(Persistent): '''An event as will be stored in the database''' @@ -271,12 +284,14 @@ class Calendar(Field): # Legend for a timeline calendar pxTimelineLegend = Px(''' - + var="items=field.getLegendItems(obj, allEventTypes, allEventNames, \ + colors, url, _, activeLayers)"> + @@ -979,18 +994,24 @@ class Calendar(Field): content = events[0].symbol or '' return '%s' % (style, title, content) - def getLegendItems(self, allEventTypes, allEventNames, colors, url, _): - '''Gets information needed to produce the legend for a timeline.''' + def getLegendItems(self, obj, allEventTypes, allEventNames, colors, url, _, + activeLayers): + '''Gets information needed to produce the legend for a timeline''' # Produce one legend item by event type shown and colored res = [] for eventType in allEventTypes: if eventType not in colors: continue - res.append(Object(name=allEventNames[eventType], + res.append(Object(name=allEventNames[eventType], content='', style='background-color: %s' % colors[eventType])) # Add the background indicating that several events are hidden behind # the timeline cell - res.append(Object(name=_('several_events'), + res.append(Object(name=_('several_events'), content='', style=url('angled', bg=True))) + # Add layer-specific items + for layer in self.layers: + if layer.name not in activeLayers: continue + items = layer.getLegendItems(obj) + if items: res += items return res def getTimelineMonths(self, grid, obj):
- + +
 
:item.content or ' '
:item.name