Package jazzparser :: Package gui :: Module windows
[hide private]
[frames] | no frames]

Source Code for Module jazzparser.gui.windows

  1  """Graphical interface for the Jazz Parser. 
  2   
  3  Very much not finished yet, but would be nice to finish in the future.  
  4  Just makes taking input and choosing parameters easier. 
  5   
  6  """ 
  7  """ 
  8  ============================== License ======================================== 
  9   Copyright (C) 2008, 2010-12 University of Edinburgh, Mark Granroth-Wilding 
 10    
 11   This file is part of The Jazz Parser. 
 12    
 13   The Jazz Parser is free software: you can redistribute it and/or modify 
 14   it under the terms of the GNU General Public License as published by 
 15   the Free Software Foundation, either version 3 of the License, or 
 16   (at your option) any later version. 
 17    
 18   The Jazz Parser is distributed in the hope that it will be useful, 
 19   but WITHOUT ANY WARRANTY; without even the implied warranty of 
 20   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 21   GNU General Public License for more details. 
 22    
 23   You should have received a copy of the GNU General Public License 
 24   along with The Jazz Parser.  If not, see <http://www.gnu.org/licenses/>. 
 25   
 26  ============================ End license ====================================== 
 27   
 28  """ 
 29  __author__ = "Mark Granroth-Wilding <mark.granroth-wilding@ed.ac.uk>"  
 30   
 31  # Check we've got the right version of PyGtk 
 32  # This check is made when this module first gets loaded 
 33  import pygtk 
 34  pygtk.require('2.0') 
 35   
 36  import gtk, pango, sys, gobject 
 37  from jazzparser.data.input import INPUT_TYPES, BULK_INPUT_TYPES, get_input_type 
 38  from jazzparser.utils.options import file_option, new_file_option, \ 
 39                      zero_to_one_float, choose_from_dict, choose_from_list 
 40   
41 -class GraphicalJazzParserWindow(gtk.Window):
42 """ 43 Main top-level interface window. 44 45 """
46 - def __init__(self, *args, **kwargs):
47 gtk.Window.__init__(self, gtk.WINDOW_TOPLEVEL, *args, **kwargs) 48 # Window setup 49 self.set_border_width(5) 50 self.set_title("Jazz Parser") 51 52 ### Layout ### 53 self._vbox = gtk.VBox(spacing=5) 54 self.add(self._vbox) 55 ### Input box 56 self._input_frame = gtk.Frame(label="Input") 57 self._input_box = gtk.VBox(spacing=4) 58 self._input_box.set_border_width(5) 59 self._input_frame.add(self._input_box) 60 self._vbox.add(self._input_frame) 61 # Input type selector 62 input_type_box = gtk.HBox(spacing=3) 63 input_type_label = gtk.Label("Input type:") 64 input_type_box.add(input_type_label) 65 input_type_selector = gtk.combo_box_new_text() 66 for (itype_name,itype) in INPUT_TYPES+BULK_INPUT_TYPES: 67 input_type_selector.append_text(itype_name) 68 input_type_box.add(input_type_selector) 69 self._input_type_selector = input_type_selector 70 self._input_box.add(input_type_box) 71 # Input file selector 72 input_file_box = gtk.HBox(spacing=3) 73 self._filename_label = gtk.Label("No file selected") 74 input_file_box.add(self._filename_label) 75 file_button = gtk.Button("Choose file") 76 input_file_box.add(file_button) 77 file_button.connect("clicked", self.select_input_file) 78 self._input_box.add(input_file_box) 79 # Input options editor 80 input_options_box = gtk.HBox(spacing=3) 81 # TODO: display the current options in here 82 input_options_button = gtk.Button("Input options") 83 input_options_box.add(input_options_button) 84 input_options_button.connect("clicked", self.edit_input_options) 85 self._input_box.add(input_options_box) 86 ### Tagger box 87 self._tagger_frame = gtk.Frame(label="Supertagger") 88 self._tagger_hbox = gtk.HBox() 89 self._tagger_frame.add(self._tagger_hbox) 90 self._vbox.add(self._tagger_frame) 91 ### Parser box 92 self._parser_frame = gtk.Frame(label="Parser") 93 self._parser_hbox = gtk.HBox() 94 self._parser_frame.add(self._parser_hbox) 95 self._vbox.add(self._parser_frame) 96 97 ## Events 98 # Stop PyGtk when the window is closed 99 self.connect("destroy", self.destroy)
100
101 - def select_input_file(self, widget, data=None):
102 # Prepare and display a file chooser dialog 103 chooser = gtk.FileChooserDialog("Select input file", self, 104 gtk.FILE_CHOOSER_ACTION_OPEN, 105 (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, 106 gtk.STOCK_OPEN, gtk.RESPONSE_OK)) 107 response = chooser.run() 108 # If a file was selected, update the display 109 if response == gtk.RESPONSE_OK: 110 filename = chooser.get_filename() 111 self._filename_label.set_text(filename) 112 113 chooser.destroy()
114
115 - def edit_input_options(self, widget, data=None):
116 # Get the appropriate options for the input type 117 type_name = self._input_type_selector.get_active_text() 118 if type_name is None: 119 show_error("Select an input type", self) 120 else: 121 input_type = get_input_type(type_name) 122 # Display an edition for the input options 123 editor = ModuleOptionsWindow(input_type.FILE_INPUT_OPTIONS, 124 self, "Input options") 125 editor.show_all()
126
127 - def update_input_options(self, values):
128 ## TODO 129 print values
130
131 - def destroy(self, obj=None):
132 gtk.main_quit()
133
134 -class ModuleOptionsWindow(gtk.Window):
135 """ 136 Generic editing window for module options. 137 138 """
139 - def __init__(self, options, parent, title="Edit options", *args, **kwargs):
140 gtk.Window.__init__(self, *args, **kwargs) 141 self.set_title(title) 142 self.set_modal(True) 143 self.set_transient_for(parent) 144 self.set_border_width(5) 145 self.main_window = parent 146 147 # Add widgets for each option 148 if len(options) == 0: 149 self.add(gtk.Label("No options for this input type")) 150 else: 151 vbox = gtk.VBox(spacing=3) 152 for option in options: 153 hbox = gtk.HBox(spacing=5) 154 # The input type depends on the filter 155 if option.filter == int: 156 # Use an integer selector 157 hbox.add(gtk.Label(option.name)) 158 hbox.add(gtk.SpinButton( 159 gtk.Adjustment(value=0, lower=0, upper=9999, 160 step_incr=1), 161 0, 0)) 162 elif option.filter == file_option: 163 # TODO: use a file selector 164 raise NotImplementedError 165 elif option.filter == new_file_option: 166 # TODO: do something clever 167 raise NotImplementedError 168 elif option.filter == float: 169 # TODO: use a float spinner 170 raise NotImplementedError 171 elif option.filter == zero_to_one_float: 172 # TODO: use a float spinner 173 raise NotImplementedError 174 elif option.filter == choose_from_dict: 175 # TODO: use a combobox on the keys 176 raise NotImplementedError 177 elif option.filter == choose_from_list: 178 # TODO: use a combobox 179 raise NotImplementedError 180 else: 181 # Just take string input 182 hbox.add(gtk.Label(option.name)) 183 hbox.add(gtk.Entry()) 184 vbox.add(hbox) 185 186 self.add(vbox) 187 188 self.connect("destroy", self.destroy) 189 self.show_all()
190
191 - def destroy(self, widget):
192 self.main_window.update_input_options({}) 193 super(ModuleOptionsWindow, self).destroy()
194
195 -def show_error(message, parent=None):
196 dlg = gtk.MessageDialog(parent=parent, 197 type=gtk.MESSAGE_ERROR, 198 message_format=message, 199 buttons=gtk.BUTTONS_OK) 200 dlg.run() 201 dlg.destroy()
202