-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
132 lines (101 loc) · 3.87 KB
/
main.py
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
import sys
import json
import asyncio
from typing import List, Literal
from PyQt6.QtCore import QCoreApplication, pyqtSignal, QObject, Qt, pyqtSlot
from PyQt6.QtGui import QGuiApplication
from PyQt6.QtQml import QQmlApplicationEngine, qmlRegisterType
from PyQt6.QtCore import pyqtProperty # type: ignore
from PyQt6.QtQml import QQmlListProperty # type: ignore
from LightNetwork import get_sunlight
from LightParser import LightParser, LightType
WaterType = Literal["infrequent", "moderate", "evenly", "regular", "abundant"]
class PlantCare(QObject):
waterUpdated = pyqtSignal()
temperatureUpdated = pyqtSignal()
lightingUpdated = pyqtSignal()
def __init__(self, water: WaterType, temperature: str, light: LightType = "any") -> None:
super().__init__()
self.__water = water
self.__temperature = temperature
self.__light = light
@pyqtProperty(str, notify=waterUpdated)
def water(self) -> str:
return self.__water
@pyqtProperty(str, notify=temperatureUpdated)
def temperature(self) -> str:
return self.__temperature
@pyqtProperty(str, notify=lightingUpdated)
def light(self) -> LightType:
return self.__light
@light.setter
def light(self, light: LightType) -> None:
self.__light = light
self.lightingUpdated.emit()
class Plant(QObject):
nameUpdated = pyqtSignal()
pathUpdated = pyqtSignal()
descriptionUpdated = pyqtSignal()
careUpdated = pyqtSignal()
def __init__(self, name: str, botanical_name: str, path: str,
description: str, care: PlantCare) -> None:
super().__init__()
self.__name = name
self.__botanical_name = botanical_name
self.__path = path
self.__description = description
self.__care = care
@pyqtProperty(str, notify=nameUpdated)
def name(self) -> str:
return self.__name
@property
def botanical_name(self) -> str:
return self.__botanical_name
@pyqtProperty(str, notify=pathUpdated)
def path(self) -> str:
return self.__path
@pyqtProperty(str, notify=descriptionUpdated)
def description(self) -> str:
return self.__description
@pyqtProperty(PlantCare, notify=careUpdated)
def care(self) -> PlantCare:
return self.__care
class PlantModel(QObject):
plantsUpdated = pyqtSignal()
def __init__(self, plants: List[Plant]) -> None:
super().__init__()
self.__plants = plants
@pyqtProperty(QQmlListProperty, notify=plantsUpdated)
def plants(self) -> QQmlListProperty:
return QQmlListProperty(Plant, self, self.__plants)
async def update_light(self) -> None:
light = await get_sunlight([plant.botanical_name for plant in self.__plants])
for i in range(len(light)):
self.__plants[i].care.light = LightParser(light[i]).light
@pyqtSlot()
def update_care(self) -> None:
asyncio.run(self.update_light())
class ModelReader:
def __init__(self) -> None:
self.__path_to_model = "./plants/model.json"
def read(self) -> List[Plant]:
with open(self.__path_to_model) as f:
read_plants = json.load(f)["plants"]
plants = []
for plant in read_plants:
care = PlantCare(**plant["care"])
del plant["care"]
plants.append(Plant(**plant, care=care))
return plants
if __name__ == '__main__':
QCoreApplication.setAttribute(Qt.ApplicationAttribute.AA_ShareOpenGLContexts)
app = QGuiApplication(sys.argv)
qmlRegisterType(PlantCare, 'PlantCare', 1, 0, 'PlantCare')
engine = QQmlApplicationEngine()
engine.addImportPath("modules")
engine.quit.connect(app.quit) # type: ignore
plants = ModelReader().read()
plant_model = PlantModel(plants)
engine.setInitialProperties({"plantModel": plant_model})
engine.load('qml/main.qml')
sys.exit(app.exec())