Skip to content

Commit b655741

Browse files
author
Zerline
committed
Flipping dominos example interactivity: override Button to skip propagation in certain cases.
1 parent c8239ea commit b655741

File tree

1 file changed

+32
-10
lines changed

1 file changed

+32
-10
lines changed

examples/dominos/flipping_dominos.py

+32-10
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from .flipping_aztecdiamond import *
55
from sage_widget_adapters.graphs.graph_grid_view_adapter import GraphGridViewAdapter
6-
from sage_combinat_widgets.grid_view_widget import GridViewWidget, ButtonCell, BlankButton, styled_button_cell
6+
from sage_combinat_widgets.grid_view_widget import GridViewWidget, ButtonCell, BlankButton, StyledButtonCell
77
from ipywidgets import Layout
88
from traitlets import dlink, HasTraits, Bool, observe, All
99
from contextlib import contextmanager
@@ -143,6 +143,32 @@ def unlink(self):
143143
source[0].unobserve(self._update, names=source[1])
144144
self.double_source, self.target = None, None
145145

146+
147+
class MyStyledButtonCell(StyledButtonCell):
148+
def _handle_msg(self, msg):
149+
r"""
150+
Override needed to prevent propagation
151+
when a domino is pressed, in some cases.
152+
"""
153+
data = msg['content']['data']
154+
if data['method'] != 'update' or not 'state' in data or ('buffer_paths' in data and data['buffer_paths']):
155+
super(FlippinDominosWidget)._handle_msg(msg)
156+
state = data['state']
157+
try:
158+
self.set_state(state)
159+
except:
160+
pass
161+
162+
163+
def my_styled_button_cell(disabled=False, style_name='', addable=False):
164+
class_name = "{}Button".format(style_name.capitalize())
165+
if disabled:
166+
class_name = "Disabled" + class_name
167+
elif addable:
168+
class_name = "Addable" + class_name
169+
return type(class_name, (MyStyledButtonCell,), {'disable': disabled, 'css_class': style_name, 'addable': addable})
170+
171+
146172
class Domino(HasTraits):
147173
r"""Objet non représenté en lui-même, les 2
148174
boutons qu'il contient étant, eux, des widgets"""
@@ -250,19 +276,16 @@ def __init__(self, g, css_classes=['b0', 'b1', 'b2', 'b3', 'b4']):
250276
with flipping aztec diamond graph `g`
251277
"""
252278
self.css_classes = css_classes
253-
#self.resetting = None
254-
#self.resetting = True
255279
super(FlippingDominosWidget, self).__init__(g, adapter = FlippingDominosAdapter(),
256280
cell_layout = smallblyt,
257-
cell_widget_classes=[styled_button_cell(),
258-
styled_button_cell(style_name='b1'),
259-
styled_button_cell(style_name='b2'),
260-
styled_button_cell(style_name='b3'),
261-
styled_button_cell(style_name='b4'),
281+
cell_widget_classes=[my_styled_button_cell(),
282+
my_styled_button_cell(style_name='b1'),
283+
my_styled_button_cell(style_name='b2'),
284+
my_styled_button_cell(style_name='b3'),
285+
my_styled_button_cell(style_name='b4'),
262286
],
263287
cell_widget_class_index=make_cell_widget_class_index(g),
264288
blank_widget_class = BlankButton)
265-
self.resetting_cells = []
266289

267290
def draw(self):
268291
self.dominos = {}
@@ -312,7 +335,6 @@ def set_cell(self, change):
312335
print("set_cell()", change.name, change.old, change.new)
313336
else:
314337
print("set_cell()", change.name)
315-
# Try to reset everything right now to avoid unwanted propagations
316338
click_pos = extract_coordinates(change.name)
317339
domino = self.domino_for_position(click_pos)
318340
if not domino: # or domino.donottrack:

0 commit comments

Comments
 (0)