Baby Language Lab Scripts
A collection of data processing tools.
 All Classes Namespaces Files Functions Variables Pages
create_check_window.py
Go to the documentation of this file.
1 from gi.repository import Gtk as gtk
2 from gi.repository import Gdk as gdk
3 from gi.repository import GObject as gobject
4 import os
5 
6 from utils.ui_utils import UIUtils
7 from utils.progress_dialog import ProgressDialog
8 from parsers.reliability2_parser import Reliability2Parser
9 from db.bll_database import BLLDatabase
10 from data_structs.check2 import Check2
11 from ui.reliability2_app.test_window import TestWindow
12 
14  def __init__(self):
15  self.window = gtk.Window(gtk.WindowType.TOPLEVEL)
16  self.window.set_title('New')
17  self.window.connect('destroy', lambda w: self.window.destroy())
18  self.window.set_border_width(10)
19  self.window.set_default_size(210, 100)
20 
21  vbox = gtk.VBox()
22 
23  self.envs_treeview = None
24  self.acts_treeview = None
25  self.parser = None
26 
27  settings_grid = self._build_settings_grid()
28  vbox.pack_start(settings_grid, True, True, 0)
29  button_box = self._build_button_box()
30  #vbox.pack_start(gtk.HSeparator())
31  vbox.pack_start(button_box, True, True, 0)
32  self.window.add(vbox)
33 
34  self.window.show_all()
35 
37  #table = gtk.Table(5, 3)
38  grid = gtk.Grid()
39 
40  csv_label = gtk.Label('CSV File:')
41  csv_label.set_justify(gtk.JUSTIFY_RIGHT)
42  #table.attach(csv_label, 0, 1, 0, 1, gtk.EXPAND, gtk.EXPAND)
43  grid.attach(csv_label, 0, 0, 1, 1)
44 
45  csv_entry = gtk.Entry()
46  csv_entry.set_width_chars(60)
47  #table.attach(csv_entry, 1, 2, 0, 1, gtk.EXPAND, gtk.EXPAND)
48  grid.attach(csv_entry, 1, 0, 1, 1)
49  self.csv_entry = csv_entry
50 
51  csv_button = gtk.Button('Browse')
52  #table.attach(csv_button, 2, 3, 0, 1, gtk.EXPAND, gtk.EXPAND)
53  grid.attach(csv_button, 2, 0, 1, 1)
54  csv_button.connect('clicked', lambda w: UIUtils.browse_file('Select csv file', csv_entry, [UIUtils.CSV_FILE_FILTER, UIUtils.ALL_FILE_FILTER]))
55 
56  wav_label = gtk.Label('Wav Folder:')
57  wav_label.set_justify(gtk.JUSTIFY_RIGHT)
58  #table.attach(wav_label, 0, 1, 1, 2, gtk.EXPAND, gtk.EXPAND)
59  grid.attach(wav_label, 0, 1, 1, 1)
60 
61  wav_entry = gtk.Entry()
62  wav_entry.set_width_chars(60)
63  #table.attach(wav_entry, 1, 2, 1, 2, gtk.EXPAND, gtk.EXPAND)
64  grid.attach(wav_entry, 1, 1, 1, 1)
65  self.wav_entry = wav_entry
66 
67  wav_button = gtk.Button('Browse')
68  #table.attach(wav_button, 2, 3, 1, 2, gtk.EXPAND, gtk.EXPAND)
69  grid.attach(wav_button, 2, 1, 1, 1)
70  wav_button.connect('clicked', lambda w: UIUtils.browse_folder('Select parent wav folder', wav_entry))
71 
72  blocks_label = gtk.Label('Blocks per Activity:')
73  blocks_label.set_justify(gtk.JUSTIFY_RIGHT)
74  #table.attach(blocks_label, 0, 1, 2, 3, gtk.EXPAND, gtk.EXPAND)
75  grid.attach(blocks_label, 0, 2, 1, 1)
76 
77  spin_adj = gtk.Adjustment(value=3, lower=1, upper=1000, step_incr=1, page_incr=10)
78  blocks_spinner = gtk.SpinButton(adjustment=spin_adj)
79  #table.attach(blocks_spinner, 1, 2, 2, 3, gtk.EXPAND, gtk.EXPAND)
80  grid.attach(blocks_spinner, 1, 2, 1, 1)
81  self.blocks_spinner = blocks_spinner
82 
83  vbox = gtk.VBox(spacing=5)
84  #table.attach(vbox, 1, 2, 3, 5)
85  grid.attach(vbox, 1, 3, 1, 2)
86 
87  csv_entry.connect('changed', lambda w: self._build_treeviews(w.get_text(), vbox))
88 
89  return grid
90 
91  def _build_button_box(self):
92  button_box = gtk.HButtonBox()
93  button_box.set_layout(gtk.ButtonBoxStyle.EDGE)
94 
95  cancel_button = gtk.Button(stock=gtk.STOCK_CANCEL, label='Cancel')
96  cancel_button.connect('clicked', lambda w: self.window.destroy())
97  button_box.add(cancel_button)
98 
99  ok_button = gtk.Button(stock=gtk.STOCK_OK, label='Run')
100  ok_button.connect('clicked', lambda w: self.run())
101  button_box.add(ok_button)
102 
103  return button_box
104 
105  def _set_default_treeview_sel(self, treeview, defaults_dict):
106  it = treeview.get_model().get_iter_first()
107  while it:
108  if treeview.get_model().get_value(it, 0) in defaults_dict:
109  treeview.get_selection().select_iter(it)
110 
111  it = treeview.get_model().iter_next(it)
112 
113  def _build_treeviews(self, filename, vbox):
114  if os.path.exists(filename):
115  vbox.hide_all()
116  map(lambda child: vbox.remove(child), vbox.get_children())
117 
118  if self.parser:
119  self.parser.close()
120  self.parser = Reliability2Parser(filename)
121  envs_names = self.parser.get_envs_list()
122  envs_names.sort()
123  acts_names = self.parser.get_acts_list()
124  acts_names.sort()
125 
126  self.envs_treeview = UIUtils.build_multiselect_treeview(envs_names, envs_names, gobject.TYPE_STRING, 'Select Environments:')
127  self.acts_treeview = UIUtils.build_multiselect_treeview(acts_names, acts_names, gobject.TYPE_STRING, 'Select Activities:')
128 
129  #modify the selection colours that appear when the treeview is not in focus, to make them a bit easier to see
130  self.envs_treeview.modify_base(gtk.StateFlags.ACTIVE, gdk.Color.parse('#4285F4')[1])
131  self.acts_treeview.modify_base(gtk.StateFlags.ACTIVE, gdk.Color.parse('#4285F4')[1])
132 
133  #set default selection
134  envs_defaults = {
135  'daycare': True,
136  'home': True,
137  }
138  self._set_default_treeview_sel(self.envs_treeview, envs_defaults)
139 
140  acts_defaults = {
141  'mealtime',
142  'personal care',
143  'playtime - outside',
144  'playtime - general',
145  'playtime - organized'
146  }
147  self._set_default_treeview_sel(self.acts_treeview, acts_defaults)
148 
149  vbox.pack_start(self.envs_treeview, True, True, 0)
150  vbox.pack_start(self.acts_treeview, True, True, 0)
151  vbox.show_all()
152 
153  def run(self):
154  csv_path = self.csv_entry.get_text()
155  wav_path = self.wav_entry.get_text()
156  blocks_per_activity = self.blocks_spinner.get_value_as_int()
157 
158  activities = []
159  if self.acts_treeview and self.acts_treeview.get_selection():
160  model, sel_paths = self.acts_treeview.get_selection().get_selected_rows()
161 
162  for path in sel_paths:
163  it = model.get_iter(path)
164  activities.append(model.get_value(it, 0))
165 
166  environments = []
167  if self.envs_treeview and self.envs_treeview.get_selection():
168  model, sel_paths = self.envs_treeview.get_selection().get_selected_rows()
169 
170  for path in sel_paths:
171  it = model.get_iter(path)
172  environments.append(model.get_value(it, 0))
173 
174  is_valid = csv_path and wav_path and blocks_per_activity and len(activities) and len(environments)
175  if is_valid:
176  check2 = Check2(
177  csv_path,
178  wav_path,
179  activities,
180  environments,
181  blocks_per_activity,
182  )
183 
184  sel_test2s = None
185  enough_blocks, counts_str = self.parser.have_enough_blocks(check2, False)
186  if enough_blocks:
187  print counts_str
188  sel_test2s = self.parser.pick_rows(
189  check2,
190  lambda filename: UIUtils.open_file('Please locate %s' % (filename), filters=[UIUtils.WAV_FILE_FILTER], save_last_location=True, cur_location=wav_path),
191  False
192  )
193  else:
194  enough_blocks, counts_str = self.parser.have_enough_blocks(check2, True)
195  if enough_blocks:
196  print counts_str
197  if UIUtils.show_confirm_dialog('There are not enough unused rows left for some activities (see command window for row counts). If you proceed, the same row will be selected twice. Ok to continue?'):
198  sel_test2s = self.parser.pick_rows(
199  check2,
200  lambda filename: UIUtils.open_file('Please locate %s' % (filename), filters=[UIUtils.WAV_FILE_FILTER], save_last_location=True, cur_location=wav_path),
201  True
202  )
203  else:
204  print counts_str
205  UIUtils.show_message_dialog('The input file does not contain enough blocks of the specified types. Please refer to the command window for a printout of the activity counts.')
206 
207  if sel_test2s:
208  progress_dialog = ProgressDialog(title='Setting up...', phases=[''])
209  progress_dialog.show()
210 
211  db = BLLDatabase()
212  check2.db_insert(db)
213  check2.test2s = sel_test2s
214  for i in range(len(check2.test2s)):
215  test2 = check2.test2s[i]
216  test2.check2_id = check2.db_id
217  test2.db_insert(db)
218 
219  progress_dialog.set_fraction(float(i + 1) / float(len(check2.test2s)))
220  db.close()
221  progress_dialog.ensure_finish()
222 
223  TestWindow(check2)
224  if self.parser:
225  self.parser.close()
226  self.window.destroy()
227 
228  else:
229  UIUtils.show_empty_form_dialog()