#!/bin/python3 import os import sys import json import mariadb from PyQt6.QtGui import QPixmap from PyQt6.QtWidgets import QApplication, QDialog, QComboBox, QLabel, QPlainTextEdit, QRadioButton, QHBoxLayout, QVBoxLayout db = mariadb.connect(host="localhost", user=os.getlogin(), database="mysql") cursor = db.cursor(buffered=True) def get_car_brands(): cursor.execute("select Naam, ID from Merk") return cursor.fetchall() def get_brand_info(brand): 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, Logo from Merk where ID = %s", (brand,)) name_logo_results = cursor.fetchone() return_dict["name"] = None if brand == None else name_logo_results[0] return_dict["logo_image_path"] = None if brand == None else name_logo_results[1] return return_dict def get_model_info(model): 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.model = None self.units_metric = True self.brand_dropdown = QComboBox() self.brand_dropdown.activated.connect(self.switch_brand) brand_dropdown_label = QLabel("Merk:") brand_dropdown_label.setBuddy(self.brand_dropdown) self.brand_facts = QPlainTextEdit() self.brand_facts.setReadOnly(True) brand_facts_label = QLabel("Merkinfo:") brand_facts_label.setBuddy(self.brand_facts) self.model_dropdown = QComboBox() self.model_dropdown.activated.connect(self.switch_model) model_dropdown_label = QLabel("Model:") model_dropdown_label.setBuddy(self.model_dropdown) self.model_facts = QPlainTextEdit() self.model_facts.setReadOnly(True) model_facts_label = QLabel("Modelinfo:") 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(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(self.brand_dropdown) select_layout.addWidget(brand_facts_label) select_layout.addWidget(self.brand_facts) select_layout.addWidget(model_dropdown_label) select_layout.addWidget(self.model_dropdown) select_layout.addLayout(model_facts_units_layout) select_layout.addWidget(model_facts_label) select_layout.addWidget(self.model_facts) self.viewer_widget = QLabel() self.viewer_widget.setScaledContents(True) self.viewer_widget.setMinimumHeight(100) self.viewer_widget.setMinimumWidth(100) layout = QHBoxLayout() layout.addLayout(select_layout) layout.addWidget(self.viewer_widget) layout.setStretch(0, 1) 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()) self.viewer_widget.setPixmap(QPixmap(self.brand_info["logo_image_path"])) 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() win.show() app.exec()