diff --git a/src/features/progress_bar_pallete.py b/src/features/progress_bar_pallete.py new file mode 100644 index 0000000000000000000000000000000000000000..f0f9aaa4c2fcae0ac6ac5dd7682c271354ce0c56 --- /dev/null +++ b/src/features/progress_bar_pallete.py @@ -0,0 +1,27 @@ +default = """ +QProgressBar{ + border: 2px solid grey; + border-radius: 5px; + text-align: center +} + +QProgressBar::chunk { + background-color: green; + width: 10px; + margin: 1px; +} +""" + +overload = """ +QProgressBar{ + border: 2px solid grey; + border-radius: 5px; + text-align: center +} + +QProgressBar::chunk { + background-color: red; + width: 10px; + margin: 1px; +} +""" \ No newline at end of file diff --git a/src/features/summary_data.py b/src/features/summary_data.py index dc80e212a02d994fca762c2f754d287b91e5d19d..a6893a908a434d4b1ca5687fca27d8e19f774dc3 100644 --- a/src/features/summary_data.py +++ b/src/features/summary_data.py @@ -2,11 +2,12 @@ from PyQt5 import QtCore, QtGui, QtWidgets from db import db, mycursor, name_cred from datetime import date, datetime from input_data import * +from progress_bar_pallete import * performance = { - "Buruk": "You can try to do better, upgrade to premium and see our tips and trick", - "Menengah": "You are doing great, but you can do better by upgrade to premium and see the tips and trick", - "Baik": "You are doing excellent job to decrease the emission" + "Buruk": f"You can try to do better, upgrade to\npremium and see our tips and trick", + "Menengah": f"You are doing great, but you can do better\nby upgrade to premium and see the tips and trick", + "Baik": f"You are doing excellent job\nto decrease the emission" } class summary_data(QtWidgets.QWidget): @@ -48,7 +49,7 @@ class summary_data(QtWidgets.QWidget): def setupLabelTotal(self): self.label_total = QtWidgets.QLabel(self.Frame) self.label_total.setObjectName("label_total") - self.label_total.setGeometry(QtCore.QRect(186, 130, 281, 121)) + self.label_total.setGeometry(QtCore.QRect(30, 130, 621, 121)) self.label_total.setFont(self.createFont("Lucida Console", 10)) self.label_total.setAlignment(QtCore.Qt.AlignHCenter|QtCore.Qt.AlignTop) self.label_total.setWordWrap(False) @@ -70,20 +71,18 @@ class summary_data(QtWidgets.QWidget): self.label_motorbike.setWordWrap(False) def setupProgressBar1(self): - self.progressBar = QtWidgets.QProgressBar(self.Frame) - self.progressBar.setGeometry(QtCore.QRect(120, 370, 151, 31)) - self.progressBar.setFont(self.createFont("Lucida Console", 10)) - self.progressBar.setStyleSheet("") - self.progressBar.setProperty("value", 40) - self.progressBar.setObjectName("progressBar") + self.progressBarElectric = QtWidgets.QProgressBar(self.Frame) + self.progressBarElectric.setGeometry(QtCore.QRect(120, 370, 151, 31)) + self.progressBarElectric.setFont(self.createFont("Lucida Console", 10)) + self.progressBarElectric.setStyleSheet(default) + self.progressBarElectric.setObjectName("progressBarElectric") def setupProgressBar2(self): - self.progressBar_2 = QtWidgets.QProgressBar(self.Frame) - self.progressBar_2.setGeometry(QtCore.QRect(430, 370, 151, 31)) - self.progressBar_2.setFont(self.createFont("Lucida Console", 10)) - self.progressBar_2.setStyleSheet("") - self.progressBar_2.setProperty("value", 10) - self.progressBar_2.setObjectName("progressBar_2") + self.progressBarMotor = QtWidgets.QProgressBar(self.Frame) + self.progressBarMotor.setGeometry(QtCore.QRect(430, 370, 151, 31)) + self.progressBarMotor.setFont(self.createFont("Lucida Console", 10)) + self.progressBarMotor.setStyleSheet(default) + self.progressBarMotor.setObjectName("progressBarMotor") def setupPushButton(self): self.pushButton = QtWidgets.QPushButton(self.Frame) @@ -97,25 +96,25 @@ class summary_data(QtWidgets.QWidget): self.pushButton_weekly.setGeometry(QtCore.QRect(210, 80, 91, 31)) self.pushButton_weekly.setStyleSheet("background-color: rgb(41, 255, 148);") self.pushButton_weekly.clicked.connect(lambda: self.updateContent("Weekly")) + self.pushButton_weekly.clicked.connect(lambda: self.setGreen("Weekly")) def setupPushButtonMonthly(self): self.pushButton_monthly = QtWidgets.QPushButton(self.Frame) self.pushButton_monthly.setObjectName("pushButton_monthly") self.pushButton_monthly.setGeometry(QtCore.QRect(340, 80, 91, 31)) self.pushButton_monthly.clicked.connect(lambda: self.updateContent("Monthly")) + self.pushButton_monthly.clicked.connect(lambda: self.setGreen("Monthly")) # * Set the initial filling def retranslateUi(self): _translate = QtCore.QCoreApplication.translate self.Frame.setWindowTitle(_translate("Frame", "Frame")) - self.progressBar.setFormat(_translate("Frame", "%p%")) + self.progressBarElectric.setFormat(_translate("Frame", "%p%")) self.label.setText(_translate("Frame", f"{date.today().strftime('%d %B %Y')}\nHello, {name_cred}")) self.pushButton.setText(_translate("Frame", "Catat Emisi [+]")) self.pushButton_weekly.setText(_translate("Frame", "Weekly")) self.pushButton_monthly.setText(_translate("Frame", "Monthly")) - self.label_electricity.setText(_translate("Frame", "Electricity summary")) - self.label_motorbike.setText(f"Motorbike summary\n{self.sumMotorbikeEmission('Weekly')} km") - self.label_electricity.setText(f"Electricity summary\n{self.sumElectricityEmission('Weekly')} kWh") + self.updateContent("Weekly") # * Utility for filling def createFont(self, fontName, fontSize): @@ -127,7 +126,44 @@ class summary_data(QtWidgets.QWidget): def updateContent(self, period): self.label_motorbike.setText(f"Motorbike summary\n{self.sumMotorbikeEmission(period)} km") self.label_electricity.setText(f"Electricity summary\n{self.sumElectricityEmission(period)} kWh") + self.label_total.setText(f"Carbon dioxide total\n{self.sumCarbonDioxide(period)} gram CO2\n\n{self.getPerformanceText(period)}") + + p, q = self.getPercentValue(period) + self.progressBarElectric.setValue(int(p)) + self.progressBarMotor.setValue(int(q)) + + if p == 100: + self.progressBarElectric.setStyleSheet(overload) + else: + self.progressBarElectric.setStyleSheet(default) + + if q == 100: + self.progressBarMotor.setStyleSheet(overload) + else: + self.progressBarMotor.setStyleSheet(default) + def getPercentValue(self, period): + if period == "Monthly": + percentValElectric = self.sumElectricityEmission("Weekly") / 127 * 100 + percentValMotor = self.sumMotorbikeEmission("Weekly") / 50 * 100 + p = min(100, percentValElectric) + q = min(100, percentValMotor) + return p,q + elif period == "Weekly": + percentValElectric = self.sumElectricityEmission("Monthly") / 893 * 100 + percentValMotor = self.sumMotorbikeEmission("Monthly") / 300 * 100 + p = min(100, percentValElectric) + q = min(100, percentValMotor) + return p,q + + def setGreen(self, period): + if period == "Weekly": + self.pushButton_weekly.setStyleSheet("background-color: rgb(41, 255, 148);") + self.pushButton_monthly.setStyleSheet("background-color: rgb(238, 238, 238);") + elif period == "Monthly": + self.pushButton_monthly.setStyleSheet("background-color: rgb(41, 255, 148);") + self.pushButton_weekly.setStyleSheet("background-color: rgb(238, 238, 238);") + # * Measurement utility def sumMotorbikeEmission(self, period): if(period == "Weekly"): @@ -135,6 +171,7 @@ class summary_data(QtWidgets.QWidget): mycursor.execute(f"SELECT SUM(KuantitasJarakMotor) FROM carbon_trace WHERE ID_Customer = {id_cred} AND TanggalInput BETWEEN '{dateBegin}' AND '{dateLast}'") elif(period == "Monthly"): today = date.today() + # today = datetime.date(2022, 1, 14) # ! For testing mycursor.execute(f"SELECT SUM(KuantitasJarakMotor) FROM carbon_trace WHERE ID_Customer = {id_cred} AND MONTH(TanggalInput) = {today.month} AND YEAR(TanggalInput) = {today.year}") val = mycursor.fetchall() if val[0][0] is None: @@ -147,14 +184,40 @@ class summary_data(QtWidgets.QWidget): mycursor.execute(f"SELECT SUM(KuantitasListrik) FROM carbon_trace WHERE ID_Customer = {id_cred} AND TanggalInput BETWEEN '{dateBegin}' AND '{dateLast}'") elif(period == "Monthly"): today = date.today() + # today = datetime.date(2022, 1, 14) # ! For testing mycursor.execute(f"SELECT SUM(KuantitasListrik) FROM carbon_trace WHERE ID_Customer = {id_cred} AND MONTH(TanggalInput) = {today.month} AND YEAR(TanggalInput) = {today.year}") val = mycursor.fetchall() if val[0][0] is None: return 0 return val[0][0] + + def sumCarbonDioxide(self, period): + if(period == "Weekly"): + return (self.sumElectricityEmission("Weekly") * 385) + (self.sumMotorbikeEmission("Weekly") * 132) + elif(period == "Monthly"): + return (self.sumElectricityEmission("Monthly") * 385) + (self.sumMotorbikeEmission("Monthly") * 132) + + def getPerformanceText(self, period): + if period == "Weekly": + totalEmission = self.sumCarbonDioxide("Weekly") + if totalEmission <= 3000: + return performance["Baik"] + elif totalEmission > 3000 and totalEmission < 7000: + return performance["Menengah"] + else: + return performance["Buruk"] + elif period == "Monthly": + totalEmission = self.sumCarbonDioxide("Monthly") + if totalEmission <= 13000: + return performance["Baik"] + elif totalEmission > 13000 and totalEmission < 30000: + return performance["Menengah"] + else: + return performance["Buruk"] def getRangeDate(self): today = date.today() + # today = datetime.date(2022, 1, 14) # ! For testing if today.strftime("%A") == "Monday": lastMonday = today else: