covid-19-analysis/UserInterface.py

195 lines
7.8 KiB
Python

# -*- coding: utf-8 -*-
"""
Project: Analyse worldwide COVID-19 Data and provide graphs etc.
@author Patrick Müller
"""
import sys
from datetime import datetime
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import Analyser as ana
class UserInterface(QWidget):
def __init__(self, app):
super().__init__()
# Get Analyser instance and required stuff from it
self.analyser = ana.Analyser()
countries = self.analyser.getAvailableCountries()
dates = self.analyser.getAvailableDates()
dates.sort()
# PyQT Setup
self.setWindowTitle('COVID-19 Analytics')
screen_width, screen_height = app.desktop().screenGeometry().width(), app.desktop().screenGeometry().height()
self.setGeometry(((screen_width / 2) - 500), ((screen_height / 2) - 250), 1000, 500)
self.setMinimumWidth(1000)
self.setMinimumHeight(500)
# Plot DPI Settings
self.plotDpi = screen_width / 20
# Layout boxes
self.header_box = QHBoxLayout()
self.h2box1 = QHBoxLayout()
self.labelBox = QHBoxLayout()
self.picklistBox = QHBoxLayout()
self.buttonBox = QHBoxLayout()
self.caseNumberBox = QHBoxLayout()
self.graphBox = QHBoxLayout()
# Heading
heading = QLabel('<h1>COVID-19 Analytics</h1>')
self.header_box.addWidget(heading)
# Sub-Header
subheader1 = QLabel('<h2>Statistics about one country</h2>')
self.h2box1.addWidget(subheader1)
# Country Picker
countryPickerLabel = QLabel('<h3>Pick a country:</h3>')
self.labelBox.addWidget(countryPickerLabel)
# self.countryPicker = QComboBox()
# self.countryPicker.addItems(countries)
# self.countryPicker.setCurrentText('Germany')
# picklistBox.addWidget(self.countryPicker, 1)
self.countryPicker = QLineEdit()
self.countryPicker.setCompleter(QCompleter(countries))
self.countryPicker.setText('Germany')
self.picklistBox.addWidget(self.countryPicker, 1)
# Start Date Picker
startDatePickerLabel = QLabel('<h3>Pick a start date:</h3>')
self.labelBox.addWidget(startDatePickerLabel)
self.startDatePicker = QComboBox()
self.startDatePicker.addItems(dates)
self.startDatePicker.setCurrentText('2019-12-31')
self.picklistBox.addWidget(self.startDatePicker, 1)
# End Date Picker
endDatePickerLabel = QLabel('<h3>Pick an end date:</h3>')
self.labelBox.addWidget(endDatePickerLabel)
self.endDatePicker = QComboBox()
self.endDatePicker.addItems(dates)
self.endDatePicker.setCurrentText(dates[len(dates) - 1])
self.picklistBox.addWidget(self.endDatePicker, 1)
# Graph Type Picker
graphTypePickerLabel = QLabel('<h3>Pick a graph type:</h3>')
self.labelBox.addWidget(graphTypePickerLabel)
self.graphTypePicker = QComboBox()
self.graphTypePicker.addItems(
['Total Cases', 'Case Increase', 'Increase Percentage', 'Cases per Million', 'Total Deaths',
'Death Increase', 'Death Rate', 'Deaths per Million', 'Is it going to end soon?',
'Double Rate Compare'])
self.picklistBox.addWidget(self.graphTypePicker, 1)
# Calculate Button
self.calculateSingleCountryStats = QPushButton('Calculate')
self.buttonBox.addWidget(self.calculateSingleCountryStats)
self.calculateSingleCountryStats.setCheckable(True)
self.calculateSingleCountryStats.clicked.connect(self.btnstate)
# Total cases number
self.casesNumber = QLabel('<h4>Daily Statistics appear here once you click "Calculate"</h4>')
self.caseNumberBox.addWidget(self.casesNumber)
# Generate Layout
layout = QVBoxLayout()
layout.addLayout(self.header_box)
layout.addSpacing(50)
layout.addLayout(self.h2box1)
layout.addSpacing(10)
layout.addLayout(self.labelBox)
layout.addLayout(self.picklistBox)
layout.addSpacing(10)
layout.addLayout(self.buttonBox)
layout.addSpacing(10)
layout.addLayout(self.caseNumberBox)
layout.addSpacing(10)
layout.addLayout(self.graphBox)
layout.addStretch(1)
# self.scrollArea = QScrollArea()
# self.scrollArea.setLayout(layout)
# self.scrollArea.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
#
# self.setCentralWidget(self.scrollArea)
self.setLayout(layout)
self.show()
def btnstate(self):
if self.calculateSingleCountryStats.isChecked():
# To reset the button
self.calculateSingleCountryStats.toggle()
if self.countryPicker.text() in self.analyser.getAvailableCountries():
country = self.countryPicker.text()
else:
self.casesNumber.setText(('<h4>Unknown country <font color="red">' + self.countryPicker.text() + ''
+ '</font>. Please try again with the given options</h4>'))
return
startDate = self.startDatePicker.currentText()
endDate = self.endDatePicker.currentText()
self.casesNumber.setText(
('<h4>Statistics for <font color="red">' + country
+ '</font> as of ' + endDate + ': <font color="red">Total Cases:</font> '
+ str(self.analyser.getTotalCases(country, endDate))
+ ', <font color="red">Total Deaths:</font> ' + str(self.analyser.getTotalDeaths(country, endDate))
+ ', <font color="red">Death Rate:</font> ' + str(self.analyser.getDeathRate(country, endDate))[:4]
+ '%</h4>'))
self.graphPlaceHolder = QLabel(self)
self.clearLayout(self.graphBox)
self.graphBox.addWidget(self.graphPlaceHolder)
if self.graphTypePicker.currentText() == 'Total Cases':
casesGraphPath = self.analyser.getCasesGraph(country, startDate, endDate, plotDpi=self.plotDpi)
self.graphPlaceHolder.setPixmap(QPixmap(casesGraphPath))
elif self.graphTypePicker.currentText() == 'Case Increase':
caseIncreaseGraphPath = self.analyser.getCaseIncreaseGraph(country, startDate, endDate,
plotDpi=self.plotDpi)
self.graphPlaceHolder.setPixmap(QPixmap(caseIncreaseGraphPath))
elif self.graphTypePicker.currentText() == 'Increase Percentage':
increasePercentageGraphPath = self.analyser.getIncreasePercentageGraph(country, startDate, endDate,
plotDpi=self.plotDpi)
self.graphPlaceHolder.setPixmap(QPixmap(increasePercentageGraphPath))
elif self.graphTypePicker.currentText() == 'Total Deaths':
deathGraphPath = self.analyser.getDeathGraph(country, startDate, endDate, plotDpi=self.plotDpi)
self.graphPlaceHolder.setPixmap(QPixmap(deathGraphPath))
elif self.graphTypePicker.currentText() == 'Death Increase':
deathIncreaseGraphPath = self.analyser.getDeathIncreaseGraph(country, startDate, endDate,
plotDpi=self.plotDpi)
self.graphPlaceHolder.setPixmap(QPixmap(deathIncreaseGraphPath))
elif self.graphTypePicker.currentText() == 'Death Rate':
deathRateGraphPath = self.analyser.getDailyDeathRateGraph(country, startDate, endDate,
plotDpi=self.plotDpi)
self.graphPlaceHolder.setPixmap(QPixmap(deathRateGraphPath))
elif self.graphTypePicker.currentText() == 'Is it going to end soon?':
isItOverGraphPath = self.analyser.getIsItOverGraph(country, plotDpi=self.plotDpi)
self.graphPlaceHolder.setPixmap(QPixmap(isItOverGraphPath))
elif self.graphTypePicker.currentText() == 'Cases per Million':
casesPerMillionGraphPath = self.analyser.getCasesPerMillionGraph(country, plotDpi=self.plotDpi)
self.graphPlaceHolder.setPixmap(QPixmap(casesPerMillionGraphPath))
elif self.graphTypePicker.currentText() == 'Deaths per Million':
deathsPerMillionGraphPath = self.analyser.getDeathsPerMillionGraph(country, plotDpi=self.plotDpi)
self.graphPlaceHolder.setPixmap(QPixmap(deathsPerMillionGraphPath))
elif self.graphTypePicker.currentText() == 'Double Rate Compare':
doubleRateCompareGraphPath = self.analyser.getDoubleRateCompareGraph(country, plotDpi=self.plotDpi)
self.graphPlaceHolder.setPixmap(QPixmap(doubleRateCompareGraphPath))
def clearLayout(self, layout):
for i in reversed(range(layout.count())):
widgetToRemove = layout.itemAt(i).widget()
# remove it from the layout list
layout.removeWidget(widgetToRemove)
# remove it from the gui
widgetToRemove.setParent(None)
def main():
app = QApplication(sys.argv)
ex = UserInterface(app)
sys.exit(app.exec_())