def __init__(self, data, header, title, parent=None):
        super().__init__(parent)

        layout = QVBoxLayout(self)

        table_model = CSVTableModel(datalist=[], header=[], parent=self)
        table_model.set_list(data, 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(title)
        self.resize(850,120)
        self.setModal(True)
        # self.show()
        self.exec_()
Example #2
0
    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.")
Example #3
0
class Controller(QtGui.QMainWindow):
    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.")

    def show_window(self):
        self.show()
        self.raise_()

    def connect_elements(self):
        self.view.actionNew.triggered.connect(self.new)
        self.view.actionOpen.triggered.connect(self.open)
        self.view.actionSave.triggered.connect(self.save)
        self.view.actionSave_As.triggered.connect(self.save_as)
        self.view.actionAdd_Row.triggered.connect(self.add_rows)
        self.view.actionRemove_Row_s.triggered.connect(self.remove_rows)
        self.view.actionWrite.triggered.connect(self.write)
        self.view.actionLoad.triggered.connect(self.load)
        self.view.actionUndo.triggered.connect(self.undo)
        self.view.actionRedo.triggered.connect(self.redo)
        self.view.actionCopy.triggered.connect(self.copy)
        self.view.actionPaste.triggered.connect(self.paste)
        self.view.actionDuplicate_Row.triggered.connect(self.duplicate)
        self.view.actionCut.triggered.connect(self.cut)
        self.view.actionShow_Results.triggered.connect(self.show_results)

    def set_undo_redo_text(self):
        undo = "Undo"
        redo = "Redo"
        undo_text = self.undoStack.undoText()
        redo_text = self.undoStack.redoText()
        if undo_text:
            undo += " \"" + undo_text + "\""
        if redo_text:
            redo += " \"" + redo_text + "\""
        self.view.actionUndo.setText(undo)
        self.view.actionRedo.setText(redo)

    def show_results(self):
        # try:
        datalist, header = self.db.create_results()

        ResultsController(datalist, header, "Hochrechnungsergebnis")
        """
        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 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 cut(self):
        self.copy()
        index = self.view.tableView.selectionModel().selectedIndexes()[0]
        command = EditCommand(self.table_model, index)
        command.newValue("")
        self.undoStack.beginMacro("Cut")
        self.undoStack.push(command)
        self.undoStack.endMacro()
        self.set_undo_redo_text()
        self.view.tableView.reset()

    def copy(self):
        if len(self.view.tableView.selectionModel().selectedIndexes()) == 0:
            return

        clipboard = QApplication.clipboard()
        selected_index = self.view.tableView.selectionModel().selectedIndexes()[0]
        selected_text = str(self.table_model.data(selected_index))
        clipboard.setText(selected_text)

    def paste(self):
        if len(self.view.tableView.selectionModel().selectedIndexes()) == 0:
            return

        clipboard = QApplication.clipboard()
        index = self.view.tableView.selectionModel().selectedIndexes()[0]
        command = EditCommand(self.table_model, index)
        command.newValue(str(clipboard.text()))

        self.undoStack.beginMacro("Paste")
        self.undoStack.push(command)
        self.undoStack.endMacro()
        self.set_undo_redo_text()
        self.view.tableView.reset()

    def undo(self):
        self.undoStack.undo()
        self.set_undo_redo_text()
        self.view.tableView.reset()

    def redo(self):
        self.undoStack.redo()
        self.set_undo_redo_text()
        self.view.tableView.reset()

    def update_table_model(self, data, header):
        self.table_model.set_list(data, header)
        self.view.tableView.reset()
        self.view.tableView.setModel(self.table_model)

    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 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 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 get_zero_column_selected_indexes(self):
        selected_indexes = self.view.tableView.selectedIndexes()
        if not selected_indexes:
            return
        return [index for index in selected_indexes if not index.column()]

    def get_selection(self):
        zero_column_selected_indexes = self.get_zero_column_selected_indexes()
        if not zero_column_selected_indexes:
            return self.table_model.rowCount(self), 1
        first_zero_column_selected_index = zero_column_selected_indexes[0]
        zero_column_selected_indexes = self.get_zero_column_selected_indexes()

        if not first_zero_column_selected_index or not first_zero_column_selected_index.isValid():
            return False
        startingrow = first_zero_column_selected_index.row()

        return startingrow, len(zero_column_selected_indexes)

    def new(self):
        self.filename = None
        self.table_model.set_list([], [])

    def open(self):
        filename = QFileDialog.getOpenFileName(self, caption="Open CSV-File", filter="CSV-File (*.csv)")[0]
        if len(filename) > 0:
            self.filename = filename
            datalist, header = CSV.read(self.filename)
            self.update_table_model(datalist, header)

    def save(self):
        if self.filename is not None:
            CSV.write(self.filename, self.table_model.get_list())
        else:
            self.save_as()

    def save_as(self):
        filename = QFileDialog.getSaveFileName(self, caption="Save CSV-File", dir=self.filename,
                                               filter="CSV-File (*.csv)")[0]
        if len(filename) > 0:
            self.filename = filename
            self.save()