Skip to content

Commit 704f756

Browse files
committed
For user convenience, allow clicking on action categories to tick / untick all actions within that category.
1 parent f11e430 commit 704f756

File tree

1 file changed

+43
-11
lines changed

1 file changed

+43
-11
lines changed

Form/formactions.py

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,10 @@ class FormActions(object):
7474
Form Action selector.
7575
"""
7676
RUN_ACTION_COL = 0
77-
ACTION_COL = 1
78-
DETAIL_COL = 2
79-
ACTION_COMMAND_COL = 3
77+
RUN_INCONSISTENT_COL = 1
78+
ACTION_COL = 2
79+
DETAIL_COL = 3
80+
ACTION_COMMAND_COL = 4
8081

8182
def __init__(self, dbstate, uistate, track, citation):
8283
self.dbstate = dbstate
@@ -121,17 +122,17 @@ def _create_dialog(self, title):
121122
box = Gtk.Box()
122123
top.vbox.pack_start(box, True, True, 5)
123124

124-
self.model = Gtk.TreeStore(bool, str, str, GObject.TYPE_PYOBJECT)
125+
self.model = Gtk.TreeStore(bool, bool, str, str, GObject.TYPE_PYOBJECT)
125126
self.tree = Gtk.TreeView(model=self.model)
126127
renderer_text = Gtk.CellRendererText()
127128
column1 = Gtk.TreeViewColumn(_("Action"))
128129
renderer_action_toggle = Gtk.CellRendererToggle()
129130
renderer_action_toggle.connect('toggled', self.on_action_toggled)
130131
column1.pack_start(renderer_action_toggle, False)
131132
column1.add_attribute(renderer_action_toggle, 'active', self.RUN_ACTION_COL)
133+
column1.add_attribute(renderer_action_toggle, 'inconsistent', self.RUN_INCONSISTENT_COL)
132134
column1.pack_start(renderer_text, True)
133135
column1.add_attribute(renderer_text, 'text', self.ACTION_COL)
134-
column1.set_cell_data_func(renderer_action_toggle, FormActions.action_data_func)
135136

136137
column2 = Gtk.TreeViewColumn(_("Detail"), renderer_text, text=self.DETAIL_COL)
137138
self.tree.append_column(column1)
@@ -153,10 +154,39 @@ def _create_dialog(self, title):
153154
return top
154155

155156
def on_action_toggled(self, widget, path):
156-
self.model[path][self.RUN_ACTION_COL] = not self.model[path][self.RUN_ACTION_COL]
157-
158-
def action_data_func(col, cell, model, iter, user_data):
159-
cell.set_property("visible", model.get_value(iter, FormActions.ACTION_COMMAND_COL))
157+
row_iter = self.model.get_iter(path)
158+
parent = self.model.iter_parent(row_iter)
159+
if not parent:
160+
# user clicked an action category row. toggle all children
161+
new_state = not self.model[row_iter][self.RUN_ACTION_COL]
162+
child = self.model.iter_children(row_iter)
163+
while child:
164+
self.model[child][self.RUN_ACTION_COL] = new_state
165+
child = self.model.iter_next(child)
166+
# all children are now consistent
167+
self.model[row_iter][self.RUN_INCONSISTENT_COL] = False
168+
# toggle RUN_ACTION_COL for the row that was clicked
169+
self.model[row_iter][self.RUN_ACTION_COL] = not self.model[row_iter][self.RUN_ACTION_COL]
170+
if parent:
171+
# update the status of the parent
172+
(consistent, value) = FormActions.all_children_consistent(self.model, parent, FormActions.RUN_ACTION_COL)
173+
self.model[parent][self.RUN_INCONSISTENT_COL] = not consistent
174+
self.model[parent][self.RUN_ACTION_COL] = consistent and value
175+
176+
def all_children_consistent(model, parent, col):
177+
consistent = True
178+
value = False
179+
child = model.iter_children(parent)
180+
if child: # handle case of no children
181+
# start with value of first child
182+
value = model.get_value(child, col)
183+
# advance to second child (if there is one)
184+
child = model.iter_next(child)
185+
# loop over all remaining children until we find an inconsistent value or reach the end
186+
while consistent and child:
187+
consistent = model.get_value(child, col) == value
188+
child = model.iter_next(child)
189+
return (consistent, value)
160190

161191
def _populate_model(self):
162192
form_id = get_form_id(self.source)
@@ -168,9 +198,11 @@ def _populate_model(self):
168198
action = (action_class[1])()
169199
(title, action_details) = action.get_actions(self.dbstate, self.citation, self.event)
170200
if action_details:
171-
parent = self.model.append(None, (False, title, None, None))
201+
# add the action category
202+
parent = self.model.append(None, (False, False, title, None, None))
172203
for action_detail in action_details:
173-
self.model.append(parent, (False, ) + action_detail)
204+
# add available actions within this category
205+
self.model.append(parent, (False, False) + action_detail)
174206

175207
def run(self):
176208
"""

0 commit comments

Comments
 (0)