diff options
author | lonkaars <loek@pipeframe.xyz> | 2022-10-09 16:08:30 +0200 |
---|---|---|
committer | lonkaars <loek@pipeframe.xyz> | 2022-10-09 16:08:30 +0200 |
commit | cbd5af58165bb2a05dcb8153804c9a9261be89b9 (patch) | |
tree | 4b7369861c79c3f67dbba3d8ff7991f5546207c8 | |
parent | 61646ba1b0894b31510bc73bb284b5506828330e (diff) |
model selection working
-rwxr-xr-x | eindopdracht/applicatie/main.py | 125 |
1 files changed, 96 insertions, 29 deletions
diff --git a/eindopdracht/applicatie/main.py b/eindopdracht/applicatie/main.py index 222d785..3327e07 100755 --- a/eindopdracht/applicatie/main.py +++ b/eindopdracht/applicatie/main.py @@ -1,67 +1,93 @@ #!/bin/python3 +import os import sys import json -from PyQt6.QtCore import QDateTime, Qt, QTimer -from PyQt6.QtWidgets import * +from mysql.connector import MySQLConnection, Error +import mysql.connector +from PyQt6.QtWidgets import QApplication, QDialog, QComboBox, QLabel, QPlainTextEdit, QRadioButton, QHBoxLayout, QVBoxLayout + +db = mysql.connector.connect(host="localhost", user=os.getlogin(), database="mysql") +cursor = db.cursor(buffered=True) def get_car_brands(): - return ["brand a", "brand b"] + cursor.execute("select Naam, ID from Merk") + return cursor.fetchall() def get_brand_info(brand): - return { "key": "value", "models": ["model a", "model b"] } + return_dict = {} + cursor.execute("select Naam, ID from `Type` where MerkID = %s", (brand,)) + return_dict["models"] = cursor.fetchall() + cursor.execute( "select " + "Locatie.Naam as Locatie, " + "Land.Naam as Land " + "from Hoofdkantoor " + "left join Locatie on Locatie.ID = Hoofdkantoor.LocatieID " + "left join Land on Land.ID = Locatie.LandID " + "where Hoofdkantoor.MerkID = %s", (brand,)) + location_country = cursor.fetchone() + return_dict["location"] = None if brand == None else location_country[0] + return_dict["country"] = None if brand == None else location_country[1] + cursor.execute("select Naam from Merk where ID = %s", (brand,)) + return_dict["name"] = None if brand == None else cursor.fetchone()[0] + return return_dict def get_model_info(model): - return { "power": 123 } + return_dict = {} + cursor.execute("select Vermogen, round(Vermogen * 1.3637) from `Type` where ID = %s", (model,)) + power_values = cursor.fetchone() + return_dict["power_kw"] = None if model == None else power_values[0] + return_dict["power_hp"] = None if model == None else power_values[1] + return return_dict class MainWindow(QDialog): def __init__(self, parent=None): super(MainWindow, self).__init__(parent) self.brand = None - self.brand_info = get_brand_info(self.brand) self.model = None - self.model_info = get_model_info(self.model) + self.units_metric = True - brand_dropdown = QComboBox() - brand_dropdown.addItems(get_car_brands()) + self.brand_dropdown = QComboBox() + self.brand_dropdown.activated.connect(self.switch_brand) brand_dropdown_label = QLabel("Merk:") - brand_dropdown_label.setBuddy(brand_dropdown) + brand_dropdown_label.setBuddy(self.brand_dropdown) - brand_facts = QPlainTextEdit() - brand_facts.setReadOnly(True) - brand_facts.setPlainText(json.dumps(self.brand_info)) + self.brand_facts = QPlainTextEdit() + self.brand_facts.setReadOnly(True) brand_facts_label = QLabel("Merkinfo:") - brand_facts_label.setBuddy(brand_facts) + brand_facts_label.setBuddy(self.brand_facts) - model_dropdown = QComboBox() - model_dropdown.addItems(self.brand_info["models"]) + self.model_dropdown = QComboBox() + self.model_dropdown.activated.connect(self.switch_model) model_dropdown_label = QLabel("Model:") - model_dropdown_label.setBuddy(model_dropdown) + model_dropdown_label.setBuddy(self.model_dropdown) - model_facts = QPlainTextEdit() - model_facts.setPlainText(json.dumps(self.model_info)) - model_facts.setReadOnly(True) + self.model_facts = QPlainTextEdit() + self.model_facts.setReadOnly(True) model_facts_label = QLabel("Modelinfo:") - model_facts_label.setBuddy(model_facts) - model_facts_units_kw = QRadioButton("kW") - model_facts_units_hp = QRadioButton("PK") + model_facts_label.setBuddy(self.model_facts) + self.model_facts_units_kw = QRadioButton("kW") + self.model_facts_units_hp = QRadioButton("PK") + self.model_facts_units_kw.setChecked(True) + self.model_facts_units_kw.toggled.connect(self.switch_units) + self.model_facts_units_hp.toggled.connect(self.switch_units) model_facts_units_label = QLabel("Eenheden:") model_facts_units_layout = QHBoxLayout() model_facts_units_layout.addWidget(model_facts_units_label) - model_facts_units_layout.addWidget(model_facts_units_hp) - model_facts_units_layout.addWidget(model_facts_units_kw) + model_facts_units_layout.addWidget(self.model_facts_units_hp) + model_facts_units_layout.addWidget(self.model_facts_units_kw) select_layout = QVBoxLayout() select_layout.addWidget(brand_dropdown_label) - select_layout.addWidget(brand_dropdown) + select_layout.addWidget(self.brand_dropdown) select_layout.addWidget(brand_facts_label) - select_layout.addWidget(brand_facts) + select_layout.addWidget(self.brand_facts) select_layout.addWidget(model_dropdown_label) - select_layout.addWidget(model_dropdown) + select_layout.addWidget(self.model_dropdown) select_layout.addLayout(model_facts_units_layout) select_layout.addWidget(model_facts_label) - select_layout.addWidget(model_facts) + select_layout.addWidget(self.model_facts) viewer_layout = QVBoxLayout() @@ -70,8 +96,49 @@ class MainWindow(QDialog): layout.addLayout(viewer_layout) layout.setStretch(1, 2) + self.brand_dropdown.clear() + for brand in get_car_brands(): self.brand_dropdown.addItem(*brand) + + self.update() self.setLayout(layout) + def update(self): + self.brand_info = get_brand_info(self.brand) + self.brand_facts.setPlainText(self.get_brand_info_human_readable()) + + self.model_info = get_model_info(self.model) + self.model_facts.setPlainText(self.get_model_info_human_readable()) + + def switch_brand(self, index): + self.brand = self.brand_dropdown.itemData(index) + self.update() + + self.model_dropdown.clear() + for model in self.brand_info["models"]: self.model_dropdown.addItem(*model) + + def switch_model(self, index): + self.model = self.model_dropdown.itemData(index) + self.update() + return + + def switch_units(self): + self.units_metric = self.model_facts_units_kw.isChecked() + self.update() + return + + def get_brand_info_human_readable(self): + return ( + f"Naam: {self.brand_info['name']}\n" + f"Land: {self.brand_info['country']}\n" + f"Locatie hoofdkantoor: {self.brand_info['location']}\n" + f"Aantal modellen: {len(self.brand_info['models'])}\n" + ) + + def get_model_info_human_readable(self): + return ( + f"Vermogen: {self.model_info['power_kw' if self.units_metric else 'power_hp']} {'kW' if self.units_metric else 'PK'}\n" + ) + if __name__ == '__main__': app = QApplication(sys.argv) win = MainWindow() |