class QueryWidget(QWidget): def __init__(self, info : tuple, conn): super().__init__() self.conn = conn self.cursor = conn.cursor() self.fun_name = info[0] self.param = info[1] self.labels = [] self.values = [] self.gen_layout = QVBoxLayout() self.table = TableWidget() self.input = None if self.param : self.label = QLabel(self.param[0]) self.input = self.param[1]() if self.param[1] == QDateEdit : self.input.setDisplayFormat("MM/dd/yyyy") self.input.setCalendarPopup(True) if self.param[1] == QSpinBox : self.input.setMaximum(999999999) self.button = QPushButton('Esegui') self.button.clicked.connect(self._run_query) self.row = QHBoxLayout() self.row.addWidget(self.label) self.row.addWidget(self.input) self.row.addWidget(self.button) self.gen_layout.addLayout(self.row) self.gen_layout.addWidget(self.table) self.setLayout(self.gen_layout) if not self.param : self._run_query() def _get_param(self): if not self.input : return None if type(self.input) == QDateEdit : return self.input.date().toString('MM/dd/yyyy') if type(self.input) == QLineEdit : return self.input.text() if type(self.input) == QSpinBox : return self.input.value() return None def _run_query(self): param = self._get_param() if param : self.cursor.callproc(self.fun_name, (self._get_param(),)) else : self.cursor.callproc(self.fun_name) labels = [item[0] for item in self.cursor.description] self.table.populate(labels, self.cursor.fetchall()) self.conn.commit()
class DbMainWindow(QMainWindow): def __init__(self, connection): ''' Main window for the program, handles the basic table/view queries ''' super().__init__() # Param refs self.connection = connection self.cursor = self.connection.cursor() # Get db meta self.cursor.execute(GET_TABLES_QUERY) self.table_list = [item[0] for item in self.cursor.fetchall()] self.cursor.execute(GET_VIEWS_QUERY) self.view_list = [item[0] for item in self.cursor.fetchall()] # Menu init self.menu = self.menuBar() self.table_menu = self.menu.addMenu('Tabelle') for table in self.table_list: action = QAction(' '.join([t.capitalize() for t in table.replace('_', ' ').split()]), self) callback = self._make_show_table(table) action.triggered.connect(callback) self.table_menu.addAction(action) self.view_menu = self.menu.addMenu('Viste') for view in self.view_list: action = QAction(' '.join([v.capitalize() for v in view.replace('_', ' ').split()]), self) callback = self._make_show_table(view) action.triggered.connect(callback) self.view_menu.addAction(action) self.query_menu = self.menu.addMenu('Interrogazioni') for label, info in QUERIES.items(): action = QAction(label, self) callback = self._make_show_query(info) action.triggered.connect(callback) self.query_menu.addAction(action) self.insert_menu = self.menu.addMenu('Inserimenti') for label, dialogclass in INSERTIONS.items(): action = QAction(label, self) callback = self._make_show_dialog(dialogclass) action.triggered.connect(callback) self.insert_menu.addAction(action) self.connection.commit() # GUI init self.setWindowTitle('DbApp') self.table_widget = TableWidget() self.setCentralWidget(self.table_widget) def _make_show_table(self, table): def show_table(): ''' Display the content of a table in the main table view Parameters: table : str -- The table name to show, will be directly inserted in the query ''' self.cursor.execute(GET_ALL_FROM_X.format(table)) labels = [item[0] for item in self.cursor.description] self.table_widget = TableWidget() self.setCentralWidget(self.table_widget) self.table_widget.populate(labels, self.cursor.fetchall()) self.connection.commit() return show_table def _make_show_query(self, info): def show_query(): self.query_widget = QueryWidget(info, self.connection) self.setCentralWidget(self.query_widget) return show_query def _make_show_dialog(self, dialogclass): def show_dialog(): self.last_dialog = dialogclass(self.connection) self.last_dialog.show() return show_dialog def _show_error(self, msg=''): dialog = QMessageBox() dialog.setWindowTitle('ERRORE') dialog.setText(msg) dialog.exec_()