-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
213 lines (163 loc) · 7.77 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import requests
from bs4 import BeautifulSoup
from PyQt5.QtChart import QChart, QChartView, QPieSeries, QPieSlice, QLegend
from PyQt5.QtGui import QFont, QIcon, QPalette, QColor, QPainter
from PyQt5.QtCore import Qt, QTranslator, QLocale, QLibraryInfo, pyqtSlot
from PyQt5.QtWidgets import (QApplication, QMainWindow, QWidget, QGridLayout, QVBoxLayout,
QLabel, QComboBox, QSizePolicy, QCheckBox)
def request(country="World"):
if country == "World":
url = "https://www.worldometers.info/coronavirus/"
elif country == "USA":
url = "https://www.worldometers.info/coronavirus/country/us"
else:
url = "https://www.worldometers.info/coronavirus/country/" + country
response = requests.get(url)
html_content = response.content
soup = BeautifulSoup(html_content, "html.parser")
worldCases = []
for i in soup.find_all("div", {"class": "maincounter-number"}):
worldCases.append(i.text)
return worldCases
class CircularGraphic(QWidget):
def __init__(self, parent=None):
super(CircularGraphic, self).__init__(parent)
self.initUI()
def initUI(self):
self.m_themaComboBox = self.addThemaItem()
self.m_labelComboBox = self.addLabelItem()
self.m_indicatorMarkerComboBox = self.addIndicatorItem()
self.m_countryComboBox = self.addCountryItem()
self.m_showLabelCheckBox = QCheckBox("Show Label")
chartView = QChartView(self.createGraphicCircular())
chartView.setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored)
chartView.setRenderHint(QPainter.Antialiasing, True)
self.m_chartView = chartView
designConfiguracion = QVBoxLayout()
designConfiguracion.addWidget(QLabel("Thema:"))
designConfiguracion.addWidget(self.m_themaComboBox)
designConfiguracion.addWidget(QLabel("Label:"))
designConfiguracion.addWidget(self.m_labelComboBox)
designConfiguracion.addWidget(QLabel("Indicator Marker:"))
designConfiguracion.addWidget(self.m_indicatorMarkerComboBox)
designConfiguracion.addWidget(QLabel("Country:"))
designConfiguracion.addWidget(self.m_countryComboBox)
designConfiguracion.addWidget(self.m_showLabelCheckBox)
designConfiguracion.addStretch()
baseDesign = QGridLayout()
baseDesign.addLayout(designConfiguracion, 0, 0, 0, 1)
baseDesign.addWidget(chartView, 0, 1, 0, 4)
self.setLayout(baseDesign)
self.m_themaComboBox.currentIndexChanged.connect(self.updateUI)
self.m_labelComboBox.currentIndexChanged.connect(self.updateUI)
self.m_indicatorMarkerComboBox.currentIndexChanged.connect(self.updateUI)
self.m_countryComboBox.currentIndexChanged.connect(self.updateChart)
self.m_showLabelCheckBox.toggled.connect(self.updateUI)
self.m_showLabelCheckBox.setChecked(True)
self.updateUI()
def addCountryItem(self):
country = QComboBox()
countryList = ["World", "Brazil", "Canada", "China", "France", "Germany", "India", "Iran", "Italy", "Russia",
"Turkey", "Spain", "USA"]
for option in countryList:
country.addItem(QIcon("images/" + option + ".png"), option)
return country
def addThemaItem(self):
thema = QComboBox()
thema.addItem("LightTheme", QChart.ChartThemeLight)
thema.addItem("DarkTheme", QChart.ChartThemeDark)
return thema
def addLabelItem(self):
labelComboBox = QComboBox()
labelComboBox.addItem("Top", Qt.AlignTop)
labelComboBox.addItem("Bottom", Qt.AlignBottom)
labelComboBox.addItem("Left", Qt.AlignLeft)
labelComboBox.addItem("Right", Qt.AlignRight)
return labelComboBox
def addIndicatorItem(self):
indicatorMarkerComboBox = QComboBox()
indicatorMarkerComboBox.addItem("Rectangle", QLegend.MarkerShapeRectangle)
indicatorMarkerComboBox.addItem("Circle", QLegend.MarkerShapeCircle)
indicatorMarkerComboBox.addItem("Series",
QLegend.MarkerShapeFromSeries)
return indicatorMarkerComboBox
def createGraphicCircular(self):
response = request("World")
graphic = QChart()
graphic.setTitle("World Corona Status")
cases = float(response[0].strip().replace(",", ""))
deaths = float(response[1].strip().replace(",", ""))
recovered = float(response[2].strip().replace(",", ""))
data_list = [("Cases:" + response[0], cases),
("Deaths:" + response[1], deaths),
("Recovered:" + response[2], recovered)]
series = QPieSeries(graphic)
for tag, valor in data_list:
series.append(tag, valor)
graphic.addSeries(series)
graphic.createDefaultAxes()
return graphic
def updateChart(self):
country = str(self.m_countryComboBox.currentText())
response = request(country)
cases = float(response[0].strip().replace(",", ""))
deaths = float(response[1].strip().replace(",", ""))
recovered = float(response[2].strip().replace(",", ""))
data_list = [("Cases:" + response[0], cases),
("Deaths:" + response[1], deaths),
("Recovered:" + response[2], recovered)]
self.m_chartView.chart().removeAllSeries()
series = QPieSeries(self.m_chartView.chart())
for tag, valor in data_list:
series.append(tag, valor)
self.m_chartView.chart().setTitle(country + " Corona Status")
self.m_chartView.chart().addSeries(series)
self.updateUI()
@pyqtSlot()
def updateUI(self):
thema = self.m_themaComboBox.itemData(self.m_themaComboBox.currentIndex())
if self.m_chartView.chart().theme() != thema:
self.m_chartView.chart().setTheme(thema)
pal = self.window().palette()
if thema == QChart.ChartThemeLight:
pal.setColor(QPalette.Window, QColor(0xf0f0f0))
pal.setColor(QPalette.WindowText, QColor(0x404044))
elif thema == QChart.ChartThemeDark:
pal.setColor(QPalette.Window, QColor(0x121218))
pal.setColor(QPalette.WindowText, QColor(0xd6d6d6))
self.window().setPalette(pal)
options = QChart.AnimationOptions(QChart.AllAnimations)
if self.m_chartView.chart().animationOptions() != options:
self.m_chartView.chart().setAnimationOptions(options)
label = self.m_chartView.chart().legend()
labelPosition = self.m_labelComboBox.itemData(self.m_labelComboBox.currentIndex())
if labelPosition == 0:
label.hide()
else:
label.setAlignment(Qt.Alignment(labelPosition))
label.show()
indicatorMarker = self.m_indicatorMarkerComboBox.itemData(
self.m_indicatorMarkerComboBox.currentIndex())
if label != indicatorMarker:
label.setMarkerShape(indicatorMarker)
showLabel = self.m_showLabelCheckBox.isChecked()
self.m_chartView.chart().series()[0].setLabelsVisible(showLabel)
# ==================================================================
if __name__ == "__main__":
import sys
application = QApplication(sys.argv)
translator = QTranslator(application)
locale = QLocale.system().name()
path = QLibraryInfo.location(QLibraryInfo.TranslationsPath)
translator.load("qtbase_%s" % locale, path)
application.installTranslator(translator)
font = QFont()
font.setPointSize(10)
application.setFont(font)
window = QMainWindow()
window.setWindowTitle("Corona App")
window.setMinimumSize(900, 500)
widget = CircularGraphic()
window.setCentralWidget(widget)
window.show()
sys.exit(application.exec_())