def load(self):
     try:
         datalist, header = self.db.load_into_csv_list()
         self.update_table_model(datalist, header)
     except Exception as e:
         print(e)
         Message.error("Database Error", "Error while accessing the database. Perhaps the schema in the database "
                                         "does not match the ddl-script.")
    def add_rows(self):
        if len(self.table_model.get_header()) == 0:
            Message.error("Error", "Adding rows to an empty table without a header is not possible.")
            return
        start, amount = self.get_selection()

        self.undoStack.beginMacro("Add Row")
        self.undoStack.push(InsertRowsCommand(self.table_model, start, 1))
        self.undoStack.endMacro()
        self.set_undo_redo_text()
    def duplicate(self):
        if len(self.view.tableView.selectionModel().selectedIndexes()) == 0:
            Message.error("Error", "You must select the first column of the row you want to duplicate")
            return

        start, amount = self.get_selection()
        self.undoStack.beginMacro("Duplicate Row")
        self.undoStack.push(DuplicateRowCommand(self.table_model, start))
        self.undoStack.endMacro()
        self.set_undo_redo_text()
        self.view.tableView.reset()
 def remove_rows(self):
     if len(self.table_model.get_list()) == 0:
         Message.error("Error", "Removing rows from an empty table is not possible.")
         return
     start, amount = self.get_selection()
     if start != len(self.table_model.get_list()):
         self.undoStack.beginMacro("Remove Row(s)")
         self.undoStack.push(RemoveRowsCommand(self.table_model, start, amount))
         self.undoStack.endMacro()
         self.set_undo_redo_text()
     else:
         Message.error("Error", "You need to choose the rows you want to remove by selecting the cells in the "
                                "first column")
 def write(self):
     if self.filename is None or len(self.table_model.get_list()) == 0:
         Message.error("Writing to database not possible", "The table needs to contain of the same column headers "
                                                           "as the original 'wahl.csv'.")
         return
     try:
         current_list = self.table_model.get_list()
         self.db.write_from_csv_list(current_list)
     except Exception as e:
         print(e)
         Message.error("Database Error", "Error while accessing the database. Perhaps the schema in the database "
                                         "does not match the ddl-script or the required table headers (see "
                                         "original 'wahl.csv') are not existing.")
    def __init__(self, parent=None):

        super().__init__(parent)

        self.undoStack = QUndoStack()

        self.view = Ui_MainWindow()
        self.view.setupUi(self)
        self.view.tableView.setSortingEnabled(True)
        self.view.tableView.setItemDelegate(ItemDelegate(self.undoStack, self.set_undo_redo_text))

        self.filename = None
        self.table_model = CSVTableModel(datalist=[], header=[], parent=self)

        self.connect_elements()

        try:
            self.db = DBAccess(DBConfig.database, DBConfig.username, DBConfig.password, DBConfig.wahltermin)
        except Exception as e:
            print(e)
            Message.error("Database Error", "Error while trying to establish a database connection.")