diff --git a/gui/config/config.json.sample b/gui/config/config.json.sample index 5e4c6f5..5dccfdc 100644 --- a/gui/config/config.json.sample +++ b/gui/config/config.json.sample @@ -5,8 +5,8 @@ "ip": "localhost", "password": "password", "user": "admin", - "port": "5432" - "database": "PostgreSQL", + "port": "5432", + "database": "PostgreSQL" }, "apikey": "sk-abc***" } diff --git a/gui/database/postgresql.py b/gui/database/postgresql.py index 2aae6c6..156c66c 100644 --- a/gui/database/postgresql.py +++ b/gui/database/postgresql.py @@ -67,6 +67,19 @@ class Postgres: self.conn.commit() cur.close() + def test_connection(self) -> bool: + try: + self.conn = psycopg2.connect( + host=self.db_ip, + port=self.db_port, + user=self.db_username, + password=self.db_password, + database=self.db_name + ) + return True + except: + return False + def close(self): self.conn.close() diff --git a/gui/gui/Window.py b/gui/gui/Window.py index 6544eb7..0e72770 100644 --- a/gui/gui/Window.py +++ b/gui/gui/Window.py @@ -55,6 +55,9 @@ class Ui_MainWindow(object): self.statementOutput = QtWidgets.QTextEdit(self.centralwidget) self.statementOutput.setEnabled(True) self.statementOutput.setGeometry(QtCore.QRect(20, 80, 375, 111)) + self.statementOutput.setFrameShape(QtWidgets.QFrame.StyledPanel) + self.statementOutput.setFrameShadow(QtWidgets.QFrame.Sunken) + self.statementOutput.setTextInteractionFlags(QtCore.Qt.TextSelectableByKeyboard|QtCore.Qt.TextSelectableByMouse) self.statementOutput.setObjectName("statementOutput") MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtWidgets.QMenuBar(MainWindow) diff --git a/gui/gui/Window.ui b/gui/gui/Window.ui index 982e399..c96be20 100644 --- a/gui/gui/Window.ui +++ b/gui/gui/Window.ui @@ -160,6 +160,12 @@ 111 + + QFrame::StyledPanel + + + QFrame::Sunken + <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd"> <html><head><meta name="qrichtext" content="1" /><style type="text/css"> @@ -167,6 +173,9 @@ p, li { white-space: pre-wrap; } </style></head><body style=" font-family:'MS Shell Dlg 2'; font-size:7.8pt; font-weight:400; font-style:normal;"> <p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br /></p></body></html> + + Qt::TextSelectableByKeyboard|Qt::TextSelectableByMouse + diff --git a/gui/gui/connection.py b/gui/gui/connection.py index fecd934..165b06e 100644 --- a/gui/gui/connection.py +++ b/gui/gui/connection.py @@ -66,7 +66,6 @@ class Ui_Connection(object): self.dbtypeCombo = QtWidgets.QComboBox(Connection) self.dbtypeCombo.setGeometry(QtCore.QRect(100, 10, 131, 22)) self.dbtypeCombo.setObjectName("dbtypeCombo") - self.dbtypeCombo.addItem("") self.retranslateUi(Connection) QtCore.QMetaObject.connectSlotsByName(Connection) @@ -83,4 +82,3 @@ class Ui_Connection(object): self.saveButton.setText(_translate("Connection", "Save")) self.returnLabel.setText(_translate("Connection", "Connection ... / Saved...")) self.label_6.setText(_translate("Connection", "IP")) - self.dbtypeCombo.setItemText(0, _translate("Connection", "PostgrSQL")) diff --git a/gui/gui/connection.ui b/gui/gui/connection.ui index 03b9ca3..2716bb3 100644 --- a/gui/gui/connection.ui +++ b/gui/gui/connection.ui @@ -207,11 +207,6 @@ 22 - - - PostgrSQL - - diff --git a/gui/main.py b/gui/main.py index 50b146a..7278d02 100644 --- a/gui/main.py +++ b/gui/main.py @@ -5,6 +5,7 @@ import sys from PyQt5.QtWidgets import QApplication, QMainWindow, QDialog import modules.aisql as aisql + from gui.apikey import Ui_ApiKey as ApiKeyForm from gui.connection import Ui_Connection as ConnectionForm from gui.gui.Window import Ui_MainWindow @@ -12,6 +13,12 @@ from gui.gui.Window import Ui_MainWindow configfolder = "config" configfile = "config.json" +dbtypes = { + 0: "PostgreSQL", + 1: "MySQL", + 2: "SQLite" +} + class MainWindow(QMainWindow): def __init__(self): @@ -41,44 +48,94 @@ class MainWindow(QMainWindow): print(self.ui.textInput.text()) +### Connection Window ### class ConnectionWindow(QDialog): def __init__(self, parent=None): super().__init__(parent) self.ui = ConnectionForm() self.ui.setupUi(self) - print('1') + self.ui.saveButton.setEnabled(False) + self.ui.testButton.setEnabled(False) + self.ui.returnLabel.setText("") + # Load DB Config from file with open(os.path.join(configfolder, configfile), "r") as f: self.config = json.load(f) - self.db_type = self.config["db_type"] - self.db_ip = self.config["db_ip"] - self.db_port = self.config["db_port"] - self.db_username = self.config["db_username"] - self.db_password = self.config["db_password"] - self.db_name = self.config["db_name"] + self.dbtype = self.config["database"]["dbtype"] + self.dbip = self.config["database"]["ip"] + self.dbport = self.config["database"]["port"] + self.dbuser = self.config["database"]["user"] + self.dbpass = self.config["database"]["password"] + self.dbname = self.config["database"]["database"] - print('2') + self.ui.dbtypeCombo.addItems(dbtypes.values()) + self.ui.dbtypeCombo.setCurrentIndex(self.dbtype) + self.ui.ipInput.setText(self.dbip) + self.ui.portInput.setText(self.dbport) + self.ui.usernameInput.setText(self.dbuser) + self.ui.passwordInput.setText(self.dbpass) + self.ui.databaseInput.setText(self.dbname) - self.ui.dbtypeCombo.setText(self.db_type) - self.ui.ipInput.setText(self.db_ip) - self.ui.portInput.setText(self.db_port) - self.ui.usernameInput.setText(self.db_username) - self.ui.passwordInput.setText(self.db_password) - self.ui.databaseInput.setText(self.db_name) + # Unlock Buttons if ip, port and database is not empty - print('3') + self.ui.ipInput.textChanged.connect(self.on_text_changed) + self.ui.portInput.textChanged.connect(self.on_text_changed) + self.ui.databaseInput.textChanged.connect(self.on_text_changed) - # If IP Port and Database are empty, disable Buttons - if self.db_ip == "" or self.db_port == "" or self.db_name == "": + # Pressed Save Button + self.ui.saveButton.clicked.connect(self.on_save_button_clicked) + + # Pressed Test Button + self.ui.testButton.clicked.connect(self.on_test_button_clicked) + + def on_text_changed(self): + if self.ui.ipInput.text() == "" or self.ui.portInput.text() == "" or self.ui.databaseInput.text() == "": self.ui.saveButton.setEnabled(False) self.ui.testButton.setEnabled(False) else: self.ui.saveButton.setEnabled(True) self.ui.testButton.setEnabled(True) - print('4') + def on_save_button_clicked(self): + self.dbtype = self.ui.dbtypeCombo.currentIndex() + self.dbip = self.ui.ipInput.text() + self.dbport = self.ui.portInput.text() + self.dbuser = self.ui.usernameInput.text() + self.dbpass = self.ui.passwordInput.text() + self.dbname = self.ui.databaseInput.text() + self.config["database"]["dbtype"] = self.dbtype + self.config["database"]["ip"] = self.dbip + self.config["database"]["port"] = self.dbport + self.config["database"]["user"] = self.dbuser + self.config["database"]["password"] = self.dbpass + self.config["database"]["database"] = self.dbname + with open(os.path.join(configfolder, configfile), "w") as f: + json.dump(self.config, f, indent=4) + + self.close() + + # FIXME: Crash when testing connection + def on_test_button_clicked(self): + self.dbtype = self.ui.dbtypeCombo.currentIndex() + self.dbip = self.ui.ipInput.text() + self.dbport = self.ui.portInput.text() + self.dbuser = self.ui.usernameInput.text() + self.dbpass = self.ui.passwordInput.text() + self.dbname = self.ui.databaseInput.text() + + if self.dbtype == 0: # PostgreSQL + import database.postgresql as postgresql + db = postgresql.Postgre(self.dbip, self.dbport, self.dbuser, self.dbpass, self.dbname) + if db.test_connection(): + self.ui.returnLabel.setText("Connection successful") + self.ui.returnLabel.setStyleSheet("color: green") + else: + self.ui.returnLabel.setText("Connection failed") + self.ui.returnLabel.setStyleSheet("color: red") + +### Api Key Window ### class ApiKeyWindow(QDialog): def __init__(self, parent=None):