class InputDataLookup(GenericWidget): NO_FILENAME_TAG = '<no filename selected>' HEADERS_LOADED_EVENT = 'InputDataLookup.headers_loaded' SENTENCES_LOADED_EVENT = 'InputDataLookup.sentence_loaded' DATA_CONFIG_EXT = '*.inconf' def __init__(self, last_directory): super().__init__(Ui_InputDataLookupGen) self.last_directory = last_directory self._selected_filename = None self._selected_learning_filename = None self._selected_testing_filename = None self.symbol_translator = None self.headers = None self._bind_gui() self._bind_logic() self.worker = LoadInputWorker(self) self.async_progress_dialog = AsyncProgressDialog(self, self.worker) self.async_progress_dialog.make_lean() self.widget.connect( self.worker, QtCore.SIGNAL(self.HEADERS_LOADED_EVENT), self._handle_headers_loaded) self.widget.connect( self.worker, QtCore.SIGNAL(self.SENTENCES_LOADED_EVENT), self._handle_sentence_loaded) def _bind_gui(self): self.selected_filename = None self.selected_learning_filename = None self.selected_testing_filename = None def _bind_logic(self): self.ui.select_file_button.clicked.connect(self.select_input_action) self.ui.reload_content_button.clicked.connect(self.load_file_action) self.ui.update_learning_with_opened_button.clicked.connect(self.select_learning_set_action) self.ui.update_testing_with_opened_button.clicked.connect(self.select_testing_set_action) self.ui.save_configuration_button.clicked.connect(self.save_config_action) self.ui.load_configuration_button.clicked.connect(self.load_config_action) @property def selected_filename(self): return self._selected_filename @selected_filename.setter def selected_filename(self, val): self._selected_filename = val self.ui.filepath_line_edit.setText(self._selected_filename if self._selected_filename else InputDataLookup.NO_FILENAME_TAG) @property def selected_learning_filename(self): return self._selected_learning_filename @selected_learning_filename.setter def selected_learning_filename(self, val): self._selected_learning_filename = val self.ui.filepath_learning.setText( self._selected_learning_filename if self._selected_learning_filename else InputDataLookup.NO_FILENAME_TAG) self._update_saving_status() @property def selected_testing_filename(self): return self._selected_testing_filename @selected_testing_filename.setter def selected_testing_filename(self, val): self._selected_testing_filename = val self.ui.filepath_testing.setText( self._selected_testing_filename if self._selected_testing_filename else InputDataLookup.NO_FILENAME_TAG) self._update_saving_status() def _update_saving_status(self): status = all([self.selected_learning_filename, self.selected_testing_filename]) self.ui.save_configuration_button.setEnabled(status) def _handle_headers_loaded(self, headers): self.headers = headers self.ui.sentence_data_widget.setColumnCount(len(self.headers)) self.ui.sentence_data_widget.setHorizontalHeaderLabels( list(map(lambda x: x.name, self.headers))) def _handle_sentence_loaded(self, sentences, shift, total): for row, sentence in enumerate(sentences): if sentence is not None: for col, header in enumerate(self.headers): content = header.handle_cell(sentence, col) self.ui.sentence_data_widget.setItem(shift + row, col, QtGui.QTableWidgetItem(content)) if self.async_progress_dialog.dialog.maximum() != total: self.async_progress_dialog.dialog.setMaximum(total) new_value = min(self.async_progress_dialog.dialog.value() + len(sentences), total) self.async_progress_dialog.dialog.setValue(new_value) if new_value == total: self.async_progress_dialog.dialog.close() self.ui.reload_content_button.setEnabled(True) self.ui.update_learning_with_opened_button.setEnabled(True) self.ui.update_testing_with_opened_button.setEnabled(True) def select_input_action(self): # noinspection PyTypeChecker,PyCallByClass selected_filename = QtGui.QFileDialog.getOpenFileName( self.widget, 'Select input file...', self.last_directory) if selected_filename: self.selected_filename = selected_filename # noinspection PyTypeChecker self.last_directory = os.path.dirname(self.selected_filename) self.load_file_action() def load_file_action(self): self.worker.start() self.async_progress_dialog.show() def select_learning_set_action(self): self.selected_learning_filename = self.selected_filename def select_testing_set_action(self): self.selected_testing_filename = self.selected_filename def save_config_action(self): config = dict( learning=self.selected_learning_filename, testing=self.selected_testing_filename ) serialized = json.dumps(config) selected_filename = QtGui.QFileDialog.getSaveFileName( self.widget, 'Save config...', self.last_directory, self.DATA_CONFIG_EXT) if selected_filename: with open(selected_filename, 'w+') as file: file.write(serialized) def load_config_action(self): selected_filename = QtGui.QFileDialog.getOpenFileName( self.widget, 'Load config...', self.last_directory, self.DATA_CONFIG_EXT) if selected_filename: self.selected_learning_filename, self.selected_testing_filename = \ SimulationExecutor.load_input_config(selected_filename)
class PopulationEditor(GenericWidget): MAX_NUMBER_OF_RULES = 100 def __init__(self): super().__init__(Ui_populationEditor) self.logger = logging.getLogger(__name__) self.last_directory = '.' self.population_path = None self.rules = [] self.learning_set_line = self.ui.learningSetLineEdit self._load_learning_set_button = self.ui.loadLearningSetPushButton self._reset_button = self.ui.controlButtonBox.button(QtGui.QDialogButtonBox.Reset) self._save_button = self.ui.controlButtonBox.button(QtGui.QDialogButtonBox.Save) self._open_button = self.ui.controlButtonBox.button(QtGui.QDialogButtonBox.Open) self._add_terminal_button = self.ui.addTerminalPushButton self._add_nonterminal_button = self.ui.addNonTerminalPushButton self.worker = LoadPopulationWorker(self) self.async_progress_dialog = AsyncProgressDialog(self, self.worker) self.async_progress_dialog.make_lean() self.dynamic_nodes += [ DynamicNode( self._save_button, self._open_button, enabling_condition=PopulationEditor._is_learning_set_selected ), DynamicNode( self.learning_set_line, auto_updater=PopulationAutoUpdater(self) ) ] self._create_rule_views() self._bind() self.dynamic_gui_update() def _bind(self): self._load_learning_set_button.clicked.connect( self._on_load_learning_set_clicked ) self.widget.connect( self.worker, QtCore.SIGNAL(LoadPopulationWorker.TRANSLATOR_READY_SIGNAL), self._on_gui_invalidated ) self.widget.connect( self.worker, QtCore.SIGNAL(LoadPopulationWorker.POPULATION_LOADED_SIGNAL), self._on_gui_invalidated ) self._open_button.clicked.connect( self._on_open_population_clicked ) self._save_button.clicked.connect( self._on_save_population_clicked ) self._add_terminal_button.clicked.connect( self._add_new_terminal_rule ) self._add_nonterminal_button.clicked.connect( self._add_new_nonterminal_rule ) def _add_new_terminal_rule(self): self.rules.append(TerminalRuleModel()) self._on_gui_invalidated() def _add_new_nonterminal_rule(self): self.rules.append(NonTerminalRuleModel()) self._on_gui_invalidated() def _is_learning_set_selected(self): return self.worker.translator is not None def _create_rule_views(self): for i in range(self.MAX_NUMBER_OF_RULES): rule_view = RuleView(i, self) self.dynamic_nodes += rule_view.dynamic_nodes self.ui.rules_layout.addWidget(rule_view.groupbox) self.widget.connect( rule_view.groupbox, QtCore.SIGNAL(RuleView.REMOVE_RULE_CLICKED_SIGNAL), self._on_remove_rule ) def _on_remove_rule(self, index): del self.rules[index] self.dynamic_gui_update() def _on_load_learning_set_clicked(self): learning_set_path = QtGui.QFileDialog.getOpenFileName( self.widget, 'Select learning set...', self.last_directory) if learning_set_path: self.last_directory = os.path.dirname(learning_set_path) self.async_progress_dialog.show() self.population_path = learning_set_path self.worker.operation = self.worker.load_learning_set_operation self.worker.start() def _on_open_population_clicked(self): population_path = QtGui.QFileDialog.getOpenFileName( self.widget, 'Open population...', self.last_directory, '*' + SimulationExecutor.POPULATION_EXT) if population_path: self.last_directory = os.path.dirname(population_path) self.async_progress_dialog.show() self.population_path = population_path self.worker.operation = self.worker.load_population_operation self.worker.start() def _on_save_population_clicked(self): population_path = QtGui.QFileDialog.getSaveFileName( self.widget, 'Save population...', self.last_directory, '*' + SimulationExecutor.POPULATION_EXT) if population_path: self.last_directory = os.path.dirname(population_path) self.async_progress_dialog.show() self.population_path = population_path self.worker.operation = self.worker.save_population_operation self.worker.start() def _on_gui_invalidated(self): self.dynamic_gui_update()