Made GUIs accesible, currently only saving and testing the API key is working. Nothing else.

This commit is contained in:
crennis 2023-05-04 15:56:48 +02:00
parent b1100e8338
commit d62abec3b8

View File

@ -1,34 +1,144 @@
import json
import os
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QDialog
from mainWindow import Ui_MainWindow
from connection import Ui_Form
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
configfolder = "config"
configfile = "config.json"
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
# Open Connection Window
self.ui.actionConnect_DB.triggered.connect(self.open_connection)
# Open API Key Window
self.ui.actionConnect_API_Key.triggered.connect(self.open_apikey)
# Pressed Convert Button
self.ui.convertButton.clicked.connect(self.on_convert_button_clicked)
def open_connection(self):
self.connection_window = ConnectionWindow(self)
self.connection_window.show()
def open_apikey(self):
self.apikey_window = ApiKeyWindow(self)
self.apikey_window.show()
def on_convert_button_clicked(self):
print("Convert Button Clicked")
print(self.ui.textInput.text())
class ConnectionWindow(QDialog):
def __init__(self, parent=None):
super().__init__(parent)
self.ui = Ui_Form()
self.ui = ConnectionForm()
self.ui.setupUi(self)
self.ui.saveButton.setEnabled(False)
print('1')
# 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"]
def customSlot(self, text):
if self.ui.IP.text():
self.ui.saveButton.setEnabled(True)
else:
print('2')
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)
print('3')
# If IP Port and Database are empty, disable Buttons
if self.db_ip == "" or self.db_port == "" or self.db_name == "":
self.ui.saveButton.setEnabled(False)
self.ui.testButton.setEnabled(False)
else:
self.ui.saveButton.setEnabled(True)
self.ui.testButton.setEnabled(True)
print('4')
class ApiKeyWindow(QDialog):
def __init__(self, parent=None):
super().__init__(parent)
self.ui = ApiKeyForm()
self.ui.setupUi(self)
self.ui.outputLabel.setText("")
# Load API Key from file
with open(os.path.join(configfolder, configfile), "r") as f:
self.config = json.load(f)
self.apikey = self.config["apikey"]
self.ui.apikeyInput.setText(self.apikey)
if self.apikey == "":
self.ui.saveButton.setEnabled(False)
self.ui.testButton.setEnabled(False)
else:
self.ui.saveButton.setEnabled(True)
self.ui.testButton.setEnabled(True)
# Unlock Buttons if text is entered
self.ui.apikeyInput.textChanged.connect(self.on_text_changed)
# 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.apikeyInput.text() == "":
self.ui.saveButton.setEnabled(False)
self.ui.testButton.setEnabled(False)
else:
self.ui.saveButton.setEnabled(True)
self.ui.testButton.setEnabled(True)
def on_save_button_clicked(self):
self.apikey = self.ui.apikeyInput.text()
with open(os.path.join(configfolder, configfile), "w") as f:
self.config["apikey"] = self.apikey
json.dump(self.config, f)
# Close Window
self.close()
def on_test_button_clicked(self):
test_key = self.ui.apikeyInput.text()
ai = aisql.AI(test_key)
if ai.test_key():
self.ui.outputLabel.setText("API Key is valid")
self.ui.outputLabel.setStyleSheet("color: green")
else:
self.ui.outputLabel.setText("API Key is invalid")
self.ui.outputLabel.setStyleSheet("color: red")
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
sys.exit(app.exec_())