def __init__(self, config_view, datagen_view, questions_view): QObject.__init__(self) self.view_config = config_view self.view_data_gen = datagen_view self.view_questions = questions_view self.model = Scene() self.manager = Manager.singleton_instance self.current_scenario = self.manager.get_databases()[0] self.view_config.scenario_changed.connect(self.update_current_scenario) self.view_config.delete_data_button.clicked.connect(self.delete_data) self.view_config.delete_scenario_button.clicked.connect( self.delete_scenario) self.view_data_gen.scenario_changed.connect( self.update_current_scenario) self.view_data_gen.comboBox.currentTextChanged.connect( self.load_datagen_data) self.view_data_gen.generate_data_button.clicked.connect( self.generate_data) self.view_questions.add_question_button.clicked.connect( self.add_question) self.view_questions.keywords_edition_popped_up.connect( self.keywords_edition) self.view_questions.scenario_changed.connect( self.update_current_scenario) self.view_questions.comboBox.currentTextChanged.connect( self.load_questions_data) self.view_questions.save_questions_button.clicked.connect( self.save_questions)
def on_init(self): self.scene = Scene(Vector(0, 0), Vector(70, 0), View.resolution[0], View.resolution[1]) self.scene.start() self.game_objects.append(self.scene) gameover_image = Image( Vector(View.resolution) / 2, "gameover.png", 0.5) sound = pg.mixer.Sound(os.path.join(SOUND_ROOT, "gameover.wav")) that = self def on_gameover_set(): sound.play() self.game_objects.append(gameover_image) pg.time.set_timer(that.GAMEOVER_EVENT, 2000) that.scene.on_die = lambda: None pass def on_gameover_end(): pg.time.set_timer(that.GAMEOVER_EVENT, 0) self.running = False self.gameover_event.addHandler(on_gameover_end) self.scene.on_die = on_gameover_set return True
def __init__(self, view_schema, view_data_gen, view_questions): super().__init__() self.view_schema = view_schema self.view_data_gen = view_data_gen self.view_questions = view_questions self.manager = Manager.singleton_instance self.model = Scene() self.working_thread = None self.view_schema.load_file_button.clicked.connect(self.file_load) self.view_schema.confirm_button.clicked.connect(self.create_new_scene) self.view_questions.add_question_button.clicked.connect( self.add_question) self.view_questions.keywords_edition_popped_up.connect( self.keywords_edition) self.new_scene_event()
def new_scene_event(self): self.view_schema.clear() self.view_schema.navbar_gen_data.setEnabled(False) self.model = Scene()
class New_scene_controller(QObject): finished_scene = pyqtSignal() def __init__(self, view_schema, view_data_gen, view_questions): super().__init__() self.view_schema = view_schema self.view_data_gen = view_data_gen self.view_questions = view_questions self.manager = Manager.singleton_instance self.model = Scene() self.working_thread = None self.view_schema.load_file_button.clicked.connect(self.file_load) self.view_schema.confirm_button.clicked.connect(self.create_new_scene) self.view_questions.add_question_button.clicked.connect( self.add_question) self.view_questions.keywords_edition_popped_up.connect( self.keywords_edition) self.new_scene_event() def new_scene_event(self): self.view_schema.clear() self.view_schema.navbar_gen_data.setEnabled(False) self.model = Scene() # SCHEMA @pyqtSlot(bool) def create_new_scene(self): scene_name = self.view_schema.get_scene_name_text() scene_ddl = self.view_schema.get_ddl_text() self.model.name = scene_name self.model.loaded_sql = scene_ddl self.manager.create_database(scene_name) self.manager.select_database(scene_name) self.manager.execute_sql(scene_ddl) self.manager.selected_db_connection.commit() self.view_schema.navbar_gen_data.setEnabled(True) @pyqtSlot(bool) def file_load(self): file_selector = QFileDialog(self.view_schema, 'Open DDL file', "/", "Sql files (*.sql)") if file_selector.exec_(): file_names = file_selector.selectedFiles() base_names = [] for file_name in file_names: base_names.append(os.path.basename(file_name)) with open(file_name) as sql: for line in sql.readlines(): self.view_schema.text_editor.appendPlainText(line) if base_names: label_text = ', '.join(base_names) else: label_text = 'Ningún archivo seleccionado' self.view_schema.set_files_selected(label_text) # DATA GENERATION @pyqtSlot(str, str, str, tuple) def update_type(self, table_name, column_name, fake_name, extra_data): table = self.model.context.get_table_by_name(table_name) column = table.columns[column_name] del column.fake_type column.max_char_len = None fake_type = None if fake_name == 'regex': fake_type = Regex(extra_data[0]) column.max_char_len = int(extra_data[1]) if extra_data[1] else None elif fake_name == 'custom': fake_type = Custom(extra_data[0]) else: fake_type = Default() max_value = extra_data[0] min_value = extra_data[1] max_char_len = extra_data[2] column.max_value = max_value if max_value else None column.min_value = min_value if min_value else None column.max_char_len = max_char_len if max_char_len else None column.fake_type = fake_type def load_scene_data(self): manager = Manager.singleton_instance manager.select_database(self.model.name) conn = manager.selected_db_connection if not self.model.context: self.model.context = squema_recollector.create_context(conn) self.view_data_gen.tabWidget.clear() custom_types = self.manager.get_fake_types() for _, table in enumerate(self.model.context.tables): new_tab_page = Table_data_generation_tab(table, custom_types) new_tab_page.table_data_modified.connect(self.update_type) self.view_data_gen.tabWidget.addTab(new_tab_page, table.name) # QUESTIONS def add_question(self): question = self.view_questions.get_question_text() answer = self.view_questions.get_answer_text() self.model.add_question(question, answer) self.view_questions.add_question(question, answer) def finish_scene_creation(self): self.manager.select_database(self.model.name) progress_popup = ProgressDialog() self.working_thread = FinishSceneThread(self.model, self.manager) self.working_thread.progression_made.connect(progress_popup.setValue) self.working_thread.category_update.connect( progress_popup.setLabelText) progress_popup.canceled.connect(self.working_thread.quit) self.working_thread.start() if progress_popup.exec(): self.finished_scene.emit() @pyqtSlot(int) def keywords_edition(self, index): keyword_options = ['Obligatorio', 'Prohibido'] selectable_keywords = self.manager.get_all_keywords() popup = KeywordsPopup(keyword_options, selectable_keywords) if popup.exec(): self.model.update_keywords(index, popup.model.return_data())
class Edit_scene_controller(New_scene_controller, QObject): def __init__(self, config_view, datagen_view, questions_view): QObject.__init__(self) self.view_config = config_view self.view_data_gen = datagen_view self.view_questions = questions_view self.model = Scene() self.manager = Manager.singleton_instance self.current_scenario = self.manager.get_databases()[0] self.view_config.scenario_changed.connect(self.update_current_scenario) self.view_config.delete_data_button.clicked.connect(self.delete_data) self.view_config.delete_scenario_button.clicked.connect( self.delete_scenario) self.view_data_gen.scenario_changed.connect( self.update_current_scenario) self.view_data_gen.comboBox.currentTextChanged.connect( self.load_datagen_data) self.view_data_gen.generate_data_button.clicked.connect( self.generate_data) self.view_questions.add_question_button.clicked.connect( self.add_question) self.view_questions.keywords_edition_popped_up.connect( self.keywords_edition) self.view_questions.scenario_changed.connect( self.update_current_scenario) self.view_questions.comboBox.currentTextChanged.connect( self.load_questions_data) self.view_questions.save_questions_button.clicked.connect( self.save_questions) def load_scenarios(self): combo_list = self.manager.get_databases() self.view_config.load_scenarios(combo_list) self.view_data_gen.load_scenarios(combo_list) self.view_questions.load_scenarios(combo_list) @pyqtSlot(str) def update_current_scenario(self, scenario): self.current_scenario = scenario self.model.name = scenario self.view_config.setCurrentScenario(scenario) self.view_data_gen.setCurrentScenario(scenario) self.view_questions.setCurrentScenario(scenario) # CONFIG def delete_scenario(self): self.manager.selected_db_connection.close() self.manager.selected_db_connection = None self.manager.delete_database(self.current_scenario) self.load_scenarios() def delete_data(self): self.manager.delete_database_data() # DATAGEN @pyqtSlot(str, str, str, tuple) def update_type(self, table_name, column_name, fake_name, extra_data): table = self.model.context.get_table_by_name(table_name) column = table.columns[column_name] del column.fake_type column.max_char_len = None fake_type = None if fake_name == 'regex': fake_type = Regex(extra_data[0]) column.max_char_len = int(extra_data[1]) if extra_data[1] else None elif fake_name == 'custom': fake_type = Custom(extra_data[0]) else: fake_type = Default() max_value = extra_data[0] min_value = extra_data[1] max_char_len = extra_data[2] column.max_value = max_value if max_value else None column.min_value = min_value if min_value else None column.max_char_len = max_char_len if max_char_len else None column.fake_type = fake_type @pyqtSlot(bool) def generate_data(self): manager = Manager.singleton_instance manager.select_database(self.model.name) filler.generate_fake_data( self.model.context, manager.selected_db_connection) def load_datagen_data(self): if self.model.name: manager = Manager.singleton_instance manager.select_database(self.model.name) conn = manager.selected_db_connection self.model.context = squema_recollector.create_context(conn) self.view_data_gen.tabWidget.clear() custom_types = self.manager.get_fake_types() for _, table in enumerate(self.model.context.tables): new_tab_page = Table_data_generation_tab( table, custom_types) new_tab_page.table_data_modified.connect(self.update_type) self.view_data_gen.tabWidget.addTab(new_tab_page, table.name) # QUESTIONS def add_question(self, question_input=None, answer_input=None): question = question_input if question_input else self.view_questions.get_question_text() answer = answer_input if answer_input else self.view_questions.get_answer_text() self.model.add_question(question, answer) self.view_questions.add_question(question, answer) def load_questions_data(self): if self.model.name: self.model.questions = [] self.model.keywords = [] self.view_questions.reset_questions() self.manager.select_database(self.model.name) questions = self.manager.get_questions() for question in questions: answer = self.manager.get_correct_answer(question) self.add_question(question, answer) def save_questions(self): for index, (question, query) in enumerate(self.model.questions): generator.create_question( self.model.name, query, question, self.model.context, self.model.get_formatted_keywords(index))
class StageController(Controller): scene: Scene GAMEOVER_EVENT = pg.USEREVENT game_objects = List[GameObject] def __init__(self): self.clock = pg.time.Clock() self.FPS = 30 self.game_objects = [] self.running = False self.gameover_event = Event() def on_init(self): self.scene = Scene(Vector(0, 0), Vector(70, 0), View.resolution[0], View.resolution[1]) self.scene.start() self.game_objects.append(self.scene) gameover_image = Image( Vector(View.resolution) / 2, "gameover.png", 0.5) sound = pg.mixer.Sound(os.path.join(SOUND_ROOT, "gameover.wav")) that = self def on_gameover_set(): sound.play() self.game_objects.append(gameover_image) pg.time.set_timer(that.GAMEOVER_EVENT, 2000) that.scene.on_die = lambda: None pass def on_gameover_end(): pg.time.set_timer(that.GAMEOVER_EVENT, 0) self.running = False self.gameover_event.addHandler(on_gameover_end) self.scene.on_die = on_gameover_set return True def on_render(self): for obj in self.game_objects: View.blit(View.window, obj.display(), obj.get_cords(), "top left") View.update() def on_loop(self): self.scene.step(1 / self.FPS) def run(self): self.running = True if not (self.on_init()): self.running = False jump_sound = pg.mixer.Sound(os.path.join(SOUND_ROOT, "jump.wav")) while self.running: self.clock.tick(self.FPS) self.on_render() self.on_loop() for event in pg.event.get(): if event.type == self.GAMEOVER_EVENT: self.gameover_event.throw() if event.type == pg.KEYDOWN: if event.key == pg.K_SPACE: jump_sound.play() self.scene.heap_jump() if event.key == pg.K_F10: View.toggle_fullscreen() if event.key == pg.K_ESCAPE: self.running = False if event.type == pg.QUIT: self.running = False
def main(): scene = Scene() done = False clock = pygame.time.Clock() while done == False: clock.tick(20) scene.update() scene.drawMe() for event in pygame.event.get(): if event.type == pygame.QUIT: done = True if event.type == pygame.KEYDOWN: done = True if event.type == pygame.MOUSEWHEEL: scene.recordVehiculeSize(event.y) elif event.type == pygame.MOUSEBUTTONDOWN: if event.button == MOUSE_LEFT: scene.eventClic(event.dict['pos'], event.dict['button']) elif event.type == pygame.MOUSEMOTION: scene.recordMouseMove(event.dict['pos']) pygame.quit()