aboutsummaryrefslogtreecommitdiff
path: root/eindopdracht/applicatie/main.py
blob: 3327e07d59db39092c9784f8e9735d98f8837c2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/bin/python3

import os
import sys
import json
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():
  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 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_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)

    viewer_layout = QVBoxLayout()

    layout = QHBoxLayout()
    layout.addLayout(select_layout)
    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()
  win.show()
  app.exec()