Skip to content

Commit

Permalink
Merge pull request #5 from markferry/issue594
Browse files Browse the repository at this point in the history
trivial: fix flake8 whitespace
  • Loading branch information
mwilck authored Jul 6, 2020
2 parents 55d5e84 + f2339e3 commit def1c58
Showing 1 changed file with 39 additions and 54 deletions.
93 changes: 39 additions & 54 deletions src/hamster/widgets/facttree.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,36 +44,39 @@ def __init__(self):
y=4)
self.add_child(self.restart)

self.width = 50 # Simon says
self.width = 50 # Simon says


class TotalFact(Fact):
"""An extension of Fact that is used for daily totals.
Instances of this class are rendered differently than instances
of Fact.
A TotalFact doesn't have a meaningful start and an end, but a
total duration (delta).
FIXME: Ideally, we should have a common parent for Fact and Total Fact
so we don't need to have nonsensical start and end properties here.
"""
def __init__(self, activity, duration):
"""An extension of Fact that is used for daily totals.
Instances of this class are rendered differently than instances
of Fact.
A TotalFact doesn't have a meaningful start and an end, but a
total duration (delta).
FIXME: Ideally, we should have a common parent for Fact and Total Fact
so we don't need to have nonsensical start and end properties here.
"""

def __init__(self, activity, duration):
super().__init__(activity=activity, start=dt.datetime.now(), end=dt.datetime.now())
self.duration = duration

@property
def delta(self):
@property
def delta(self):
return self.duration


class Label(object):
"""a much cheaper label that would be suitable for cellrenderer"""

def __init__(self, x=0, y=0, color=None):
self.x = x
self.y = y
self.color = color
self._label_context = cairo.Context(cairo.ImageSurface(cairo.FORMAT_A1, 0, 0))
self.layout = pangocairo.create_layout(self._label_context)
self.layout.set_font_description(pango.FontDescription(graphics._font_desc))
self.set_text("Hamster") # dummy
self.set_text("Hamster") # dummy

@property
def height(self):
Expand Down Expand Up @@ -117,6 +120,7 @@ def show(self, g, text=None, x=None, y=None):

class TagLabel(Label):
"""Tag label, with small text."""

def set_text(self, text):
Label.set_text(self, "<small>{}</small>".format(text))

Expand Down Expand Up @@ -144,7 +148,7 @@ def __init__(self):
self.inter_tag_margin = 4
self.row_margin_H = 5
self.row_margin_V = 2
self.category_offset_V = self.category_label.height * 0.1;
self.category_offset_V = self.category_label.height * 0.1

@property
def height(self):
Expand All @@ -161,7 +165,6 @@ def height(self):

return res


def set_fact(self, fact):
"""Set current fact."""

Expand All @@ -186,7 +189,6 @@ def set_fact(self, fact):
# The first one is enough to determine the height.
self.tag_label.set_text(stuff.escape_pango(fact.tags[0]))


def _show_tags(self, g, color, bg):
label = self.tag_label
label.color = bg
Expand All @@ -206,7 +208,6 @@ def _show_tags(self, g, color, bg):

g.restore_context()


def show(self, g, colors, fact=None, is_selected=False):
"""Display the fact row.
Expand All @@ -227,7 +228,7 @@ def show(self, g, colors, fact=None, is_selected=False):

g.set_color(color)

#Do not show the start/end time for Totals
# Do not show the start/end time for Totals
if not isinstance(self.fact, TotalFact):
self.time_label.show(g)
self.activity_label.show(g, self.activity_label.get_text() if not isinstance(self.fact, TotalFact) else "<b>{}</b>".format(self.activity_label.get_text()))
Expand Down Expand Up @@ -290,8 +291,6 @@ class FactTree(graphics.Scene, gtk.Scrollable):
'on-delete-called': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_PYOBJECT,)),
}



hadjustment = gobject.property(type=gtk.Adjustment, default=None)
hscroll_policy = gobject.property(type=gtk.ScrollablePolicy, default=gtk.ScrollablePolicy.MINIMUM)
vadjustment = gobject.property(type=gtk.Adjustment, default=None)
Expand All @@ -310,7 +309,7 @@ def __init__(self):
self.fact_row = FactRow()

self.action_row = ActionRow()
#self.add_child(self.action_row)
# self.add_child(self.action_row)

self.row_positions = []
self.row_heights = []
Expand All @@ -337,7 +336,6 @@ def __init__(self):
self.connect("on-enter-frame", self.on_enter_frame)
self.connect("on-double-click", self.on_double_click)


@property
def current_fact_index(self):
"""Current fact index in the self.facts list."""
Expand All @@ -350,13 +348,13 @@ def on_mouse_down(self, scene, event):
if self.hover_fact:
# match either content or id
if (self.hover_fact == self.current_fact
or (self.hover_fact
and self.current_fact
and self.hover_fact.id == self.current_fact.id)
):
or (self.hover_fact
and self.current_fact
and self.hover_fact.id == self.current_fact.id)
):
self.unset_current_fact()
#Totals can't be selected
elif not isinstance(self.hover_fact,TotalFact):
# Totals can't be selected
elif not isinstance(self.hover_fact, TotalFact):
self.set_current_fact(self.hover_fact)

def activate_row(self, day, fact):
Expand All @@ -366,7 +364,7 @@ def delete_row(self, fact):
self.emit("on-delete-called", fact)

def on_double_click(self, scene, event):
if self.hover_fact and not isinstance(self.hover_fact,TotalFact):
if self.hover_fact and not isinstance(self.hover_fact, TotalFact):
self.activate_row(self.hover_day, self.hover_fact)

def on_key_press(self, scene, event):
Expand Down Expand Up @@ -414,7 +412,6 @@ def on_key_press(self, scene, event):
if self.current_fact:
self.delete_row(self.current_fact)


def set_current_fact(self, fact):
self.current_fact = fact

Expand All @@ -436,10 +433,9 @@ def get_visible_range(self):

y = self.y
return [{"i": start + i, "y": pos - y, "h": height, "day": day, "facts": facts}
for i, (pos, height, (day, facts)) in enumerate(zip(self.row_positions[start:end],
self.row_heights[start:end],
self.days[start:end]))]

for i, (pos, height, (day, facts)) in enumerate(zip(self.row_positions[start:end],
self.row_heights[start:end],
self.days[start:end]))]

def on_mouse_move(self, tree, event):
hover_day, hover_fact = None, None
Expand All @@ -464,9 +460,9 @@ def on_mouse_move(self, tree, event):
break

if (hover_fact
and self.hover_fact
and hover_fact.id != self.hover_fact.id
):
and self.hover_fact
and hover_fact.id != self.hover_fact.id
):
self.move_actions()
# idem, always update hover_fact, not just if they appear different
self.hover_fact = hover_fact
Expand All @@ -479,14 +475,12 @@ def move_actions(self):
else:
self.action_row.visible = False


def _on_vadjustment_change(self, scene, vadjustment):
if not self.vadjustment:
return
self.vadjustment.connect("value_changed", self.on_scroll_value_changed)
self.set_size_request(500, 300)


def set_facts(self, facts):
# FactTree adds attributes to its facts. isolate these side effects
# copy the id too; most of the checks are based on id here.
Expand All @@ -509,15 +503,15 @@ def set_facts(self, facts):
by_date[fact.date].append(fact)
delta_by_date[fact.date] += fact.delta

#Add a TotalFact at the end of each day if we are
#displaying more than one day.
if len(by_date) > 1 :
# Add a TotalFact at the end of each day if we are
# displaying more than one day.
if len(by_date) > 1:
for key in by_date:
total_by_date = TotalFact(_("Total"), delta_by_date[key])
by_date[key].append(total_by_date)

days = []
for i in range((end-start).days + 1):
for i in range((end - start).days + 1):
current_date = start + dt.timedelta(days=i)
days.append((current_date, by_date[current_date]))

Expand All @@ -526,8 +520,8 @@ def set_facts(self, facts):
self.set_row_heights()

if (self.current_fact
and self.current_fact.id in (fact.id for fact in self.facts)
):
and self.current_fact.id in (fact.id for fact in self.facts)
):
self.on_scroll()
else:
# will also trigger an on_scroll
Expand Down Expand Up @@ -568,7 +562,6 @@ def set_row_heights(self):
heights.append(height)
y += height


self.row_positions, self.row_heights = pos, heights

maxy = max(y, 1)
Expand All @@ -578,18 +571,15 @@ def set_row_heights(self):
self.vadjustment.set_upper(max(maxy, self.height))
self.vadjustment.set_page_size(self.height)


def on_resize(self, scene, event):
self.set_row_heights()
self.fact_row.width = self.width - 105
self.on_scroll()


def on_scroll_value_changed(self, scroll):
self.y = int(scroll.get_value())
self.on_scroll()


def on_scroll(self, scene=None, event=None):
if not self.height:
return
Expand All @@ -611,7 +601,6 @@ def on_scroll(self, scene=None, event=None):

self.visible_range = self.get_visible_range()


def on_enter_frame(self, scene, context):
has_focus = self.get_toplevel().has_toplevel_focus()
if has_focus:
Expand All @@ -629,7 +618,6 @@ def on_enter_frame(self, scene, context):
"selected_bg": self.style.get_background_color(gtk.StateFlags.BACKDROP),
}


if not self.height:
return

Expand All @@ -641,7 +629,6 @@ def on_enter_frame(self, scene, context):
date_bg_color = self.colors.mix(colors["normal_bg"], colors["normal"], 0.15)
g.fill_area(0, 0, 105, self.height, date_bg_color)


y = int(self.y)

for rec in self.visible_range:
Expand All @@ -664,7 +651,6 @@ def on_enter_frame(self, scene, context):
g.restore_context()
continue


g.set_color(colors["normal"])
self.date_label.show(g, rec['day'].strftime("%A\n%b %d"))

Expand All @@ -676,5 +662,4 @@ def on_enter_frame(self, scene, context):
self.fact_row.show(g, colors, is_selected=is_selected)
g.translate(0, self.fact_row.height)


g.restore_context()

0 comments on commit def1c58

Please sign in to comment.