Baby Language Lab Scripts
A collection of data processing tools.
 All Classes Namespaces Files Functions Variables Pages
filters_frame.py
Go to the documentation of this file.
1 ## @package utils.filters_frame
2 
3 from gi.repository import Gtk as gtk
4 from gi.repository import GObject as gobject
5 
6 from utils.add_filter_window import AddFilterWindow
7 from utils.ui_utils import UIUtils
8 from data_structs.seg_filters import *
9 
10 ## This is frame that you can embed in a window to allow the user to create segment filters.
11 # The frame contains a treeview with rows for each of the filters that have been added so far.
12 # Each row contains an English description of an existing filter.
13 # Two buttons just below the treeview allow the user to add and remove filters.
14 # A list of SegFilter objects can be retrieved using the get_filters() method.
15 # The machinery for adding and removing filters is provided by the utils.add_filter_window.AddFilterWindow class.
16 # Here is a screenshot of what this class looks like in the UI:
17 # <img src="../images/filters_frame.png">
18 class FiltersFrame(gtk.Frame):
19  ## Constructor
20  # @param self
21  # @param label (string='Filters') a title string to display along the top of the frame
22  # @param existing_filters (list=[]) a list of SegFilter objects that should be programatically added to the list upon creation
23  def __init__(self, label='Filters', existing_filters=[]):
24  super(FiltersFrame, self).__init__(label=label)
25 
26  vbox = gtk.VBox()
27  #build the treeview (list of current filters)
28  list_store = gtk.ListStore(gobject.TYPE_STRING,
29  gobject.TYPE_STRING,
30  gobject.TYPE_PYOBJECT)
31 
32  self.treeview = gtk.TreeView(list_store)
33 
34  col_names = ['Type', 'Description']
35  for i in range(len(col_names)):
36  col = gtk.TreeViewColumn(col_names[i], gtk.CellRendererText(), text=i)
37  col.set_resizable(True)
38  self.treeview.append_column(col)
39 
40  #if we have any already-existing filters, add them to the treeview model so that they are displayed
41  model = self.treeview.get_model()
42  for cur_filter in existing_filters:
43  model.append([cur_filter.get_filter_type_str(), cur_filter.get_filter_desc_str(), cur_filter])
44 
45  vbox.pack_start(self.treeview, True, True, 0)
46 
47  #create buttons for adding and removing filters
48  button_box = gtk.HButtonBox()
49  add_button = UIUtils.create_button('Add Filter', UIUtils.BUTTON_ICONS.ADD, UIUtils.BUTTON_ICON_SIZES.PX22)
50  add_button.connect('clicked', lambda w: AddFilterWindow(self._add_filter))
51  remove_button = UIUtils.create_button('Remove Filter', UIUtils.BUTTON_ICONS.REMOVE, UIUtils.BUTTON_ICON_SIZES.PX22)
52  remove_button.connect('clicked', lambda w: self._remove_filter())
53  button_box.add(add_button)
54  button_box.add(remove_button)
55 
56  vbox.pack_start(button_box, False, False, 0)
57  self.add(vbox)
58 
59  ## Retrieves a list of SegFilter objects that represent the filters the user has created.
60  # @param self
61  # @returns (list) list of SegFilter objects - empty list if user has not created any
62  def get_filters(self):
63  tree_model = self.treeview.get_model()
64  filters_iter = tree_model.get_iter_first()
65  filters = []
66  while filters_iter:
67  filters.append(tree_model.get_value(filters_iter, 2))
68  filters_iter = tree_model.iter_next(filters_iter)
69 
70  return filters
71 
72  ## Internal method to add a new filter to the treeview model. This makes the UI display a list row for the filter.
73  # @param self
74  # @param new_filter (SegFilter) a SegFilter object representing the newly created filter to add to the UI list.
75  def _add_filter(self, new_filter):
76  filter_list_store = self.treeview.get_model()
77  filter_list_store.append([new_filter.get_filter_type_str(), new_filter.get_filter_desc_str(), new_filter])
78 
79  ## Internal method to remove the currently selected filter from the list. This effectively removes all record of the filter from this instance's memory (i.e. it will no longer be
80  # returned by get_filters()). If no UI row is selected, nothing happens.
81  # @param self
82  def _remove_filter(self):
83  model, it = self.treeview.get_selection().get_selected()
84  if it:
85  model.remove(it)