def __init__(self, data, header, parent=None): super().__init__(parent) layout = QVBoxLayout(self) table_model = CSVFileTableModel(parent=self) table_model.set_list(data, header_list=header) self.view = QtGui.QTableView(self) self.view.setModel(table_model) self.view.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers) self.view.resizeColumnsToContents() layout.addWidget(self.view) self.setWindowTitle("Generated Predictions") self.resize(925, 100) self.setModal(True) # self.show() self.exec_()
def __init__(self, parent=None): super().__init__(parent) self.view = WahlAnalyseView() self.model = WahlAnalyseModel() self.table_model = CSVFileTableModel() self.view.setupUi(self) self.view.tableView.setModel(self.table_model) self.view.tableView.setItemDelegate(CSVItemDelegate(self.model.undo_stack)) self.connect_elements()
class WahlAnalyseController(QMainWindow): def __init__(self, parent=None): super().__init__(parent) self.view = WahlAnalyseView() self.model = WahlAnalyseModel() self.table_model = CSVFileTableModel() self.view.setupUi(self) self.view.tableView.setModel(self.table_model) self.view.tableView.setItemDelegate(CSVItemDelegate(self.model.undo_stack)) self.connect_elements() def connect_elements(self): """ Maps the graphical elements to the related callback function :return: None """ # file menu self.view.actionNew.triggered.connect(self.action_new) self.view.actionOpen.triggered.connect(self.action_open) self.view.actionSave.triggered.connect(self.action_save) self.view.actionSaveAs.triggered.connect(self.action_save_as) # edit menu self.view.actionRedo.triggered.connect(self.action_redo) self.view.actionUndo.triggered.connect(self.action_undo) self.view.actionCopyCell.triggered.connect(self.action_copy_cell) self.view.actionPasteCell.triggered.connect(self.action_paste_cell) self.view.actionCutCell.triggered.connect(self.action_cut_cell) self.view.actionInsertRow.triggered.connect(self.action_insert_row) self.view.actionDuplicateRow.triggered.connect(self.action_duplicate_row) self.view.actionDeleteRow.triggered.connect(self.action_delete_row) # window menu self.view.actionSaveDatabase.triggered.connect(self.action_save_to_database) self.view.actionLoadDatabase.triggered.connect(self.action_load_from_database) self.view.actionGenerate_Computer_Prediction.triggered.connect(self.action_prediction) self.view.menuHelp.triggered.connect(self.menu_help) def action_new(self): self.model.filename = None self.table_model.set_list([], []) def action_open(self): filename = QFileDialog.getOpenFileName( self, caption="Open CSV-File", filter="CSV-File (*.csv)" )[0] if len(filename) > 0: self.model.filename = filename self.update_table(CSVUtil.read_content(self.model.filename)[::1]) def action_save(self): if self.model.filename is not None: CSVUtil.write_list(self.model.filename, self.table_model.get_list()) else: self.action_save_as() def action_save_as(self): filename = QFileDialog.getSaveFileName( self, caption="Save to CSV-File", dir=self.model.filename, filter="CSV-File (*.csv)" )[0] if len(filename) > 0: self.model.filename = filename self.action_save() def action_insert_row(self): if self.cell_selected(): # insert row below the selected one first, amount = self.get_selected_indexes() command = InsertRowCommand(self.table_model, first) else: # insert row on top of the table command = InsertRowCommand(self.table_model, 0) self.model.undo_stack.beginMacro("Insert Row") self.model.undo_stack.push(command) self.model.undo_stack.endMacro() self.view.tableView.reset() def action_duplicate_row(self): if self.cell_selected(): first, amount = self.get_selected_indexes() command = DuplicateRowCommand(self.table_model, first) self.model.undo_stack.beginMacro("Duplicate Row") self.model.undo_stack.push(command) self.model.undo_stack.endMacro() self.view.tableView.reset() def action_delete_row(self): if self.cell_selected(): first, amount = self.get_selected_indexes() command = DeleteRowCommand(self.table_model, first) self.model.undo_stack.beginMacro("Delete Row") self.model.undo_stack.push(command) self.model.undo_stack.endMacro() self.view.tableView.reset() def action_save_to_database(self): election_date = self.get_election_date() if election_date is not None: Thread(target=database.WahlAnalyseAccessor( host='localhost', database='wahlanalyse', username='******', password='******' ).save( datetime.date(election_date[0], election_date[1], election_date[2]), self.table_model.get_list() )).start() else: return def action_load_from_database(self): election_date = self.get_election_date() if election_date is not None: accessor = database.WahlAnalyseAccessor( host='localhost', database='wahlanalyse', username='******', password='******' ) data, header = accessor.load(datetime.date(election_date[0], election_date[1], election_date[2])) self.table_model.set_list(data, header) else: return def action_prediction(self): election_date = self.get_election_date() data, header = database.WahlAnalyseAccessor( host='localhost', database='wahlanalyse', username='******', password='******' ).\ generate_prediction(datetime.date(election_date[0], election_date[1], election_date[2])) PredictionsController(data, header) def action_undo(self): self.model.undo_stack.undo() self.update_undo_actions() self.view.tableView.reset() def action_redo(self): self.model.undo_stack.redo() self.update_undo_actions() self.view.tableView.reset() # noinspection PyArgumentList def action_copy_cell(self): if self.cell_selected(): clipboard = QApplication.clipboard() index = self.first_selected_index() text = self.table_model.data(index) clipboard.setText(str(text)) return True else: # TODO return False # noinspection PyArgumentList def action_paste_cell(self): if self.cell_selected(): clipboard = QApplication.clipboard() index = self.view.tableView.selectionModel().selectedIndexes()[0] command = EditCommand(self.table_model, index) command.newValue(clipboard.text()) self.model.undo_stack.beginMacro("Paste Cell") self.model.undo_stack.push(command) self.model.undo_stack.endMacro() self.update_undo_actions() self.view.tableView.reset() return True else: # TODO return False def action_cut_cell(self): if self.action_copy_cell(): # no else case since it was handled in the copy callback index = self.first_selected_index() command = EditCommand(self.table_model, index) command.newValue("0") self.model.undo_stack.beginMacro("Cut Cell") self.model.undo_stack.push(command) self.model.undo_stack.endMacro() self.update_undo_actions() self.view.tableView.reset() def menu_help(self): pass def update_table(self, data): self.table_model.set_list(data) self.view.tableView.reset() self.view.tableView.setModel(self.table_model) def get_selected_indexes(self): selected_indexes = self.view.tableView.selectedIndexes() if not selected_indexes: return self.table_model.rowCount(self), 1 unique_rows = set() rows = [x for x in selected_indexes if not (x.row() in unique_rows or unique_rows.add(x.row()))] return rows[0].row(), len(rows) def get_election_date(self): """ Open a new date selector and return the result if there is one :return: The selected date """ dialog = DateSelectorDialogController(self) dialog.show() if dialog.exec_(): return dialog.get_date() else: return None def cell_selected(self): return len(self.view.tableView.selectionModel().selectedIndexes()) > 0 def first_selected_index(self): return self.view.tableView.selectionModel().selectedIndexes()[0] def update_undo_actions(self): undo_text = "Undo " + self.model.undo_stack.undoText() redo_text = "Redo " + self.model.undo_stack.redoText() self.view.actionUndo.setText(undo_text) self.view.actionRedo.setText(redo_text)