-
Notifications
You must be signed in to change notification settings - Fork 0
/
activitytracker.py
525 lines (478 loc) · 20.8 KB
/
activitytracker.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
"""
Copyright (C) 2023 Craig S. Chisholm
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import sys
import json
import os
import datetime
import copy
import numpy as np
from PyQt5.QtCore import Qt, QDate
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QLineEdit, QPushButton, QHBoxLayout, QListWidget, QVBoxLayout, QLabel, QGridLayout, QScrollArea, QComboBox, QFileDialog, QDialog, QCalendarWidget
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
import matplotlib.pyplot as plt
def getVersion():
with open('helptext.txt','r') as f:
lines = f.readlines()
for line in lines:
if 'Version - ' in line:
return line.split('Version - ')[-1].strip('\n')
VERSION_STRING = getVersion()
#Set geometry
ITEMS_HEIGHT = 450
ITEMS_WIDTH = 300
FIELDS_WIDTH = 300
PLOT_WIDTH = 650
#Set default directory
defaultDirectory = f'{os.path.expanduser("~")}/Documents/'
#File IO function
def loadFile(fileName: str) -> dict:
with open(fileName,'r') as f:
items = json.loads(f.read())
return items
def saveFile(fileName: str, items: dict):
with open(fileName,'w') as f:
f.write(json.dumps(items))
def itemField(label: str, value: float, unit: str) -> dict:
return {label: {'value': value, 'unit': unit}}
#GUI classes
class mainWindow(QMainWindow):
'''Main window'''
def __init__(self):
super().__init__()
self.items = {}
self.itemsLoad = {}
self.currentDirectory = defaultDirectory
self.currentFile = ''
self._setTitle()
self.generalLayout = QHBoxLayout()
centralWidget = QWidget(self)
centralWidget.setLayout(self.generalLayout)
self.setCentralWidget(centralWidget)
self._createMenu()
self._createItemsBox()
self._displayItems()
self._createFieldsBox()
self._createPlotBox()
self._displayFields()
def _createMenu(self):
menu = self.menuBar().addMenu('&Menu')
menu.addAction('&New', self._new, shortcut='Ctrl+N')
menu.addAction('&Open', self._open, shortcut='Ctrl+O')
menu.addAction('&Save', self._save, shortcut='Ctrl+S')
menu.addAction('&Save As', self._saveAs,shortcut='Ctrl+Shift+S')
menu.addAction('&Exit', self.close, shortcut='Alt+F4')
helpMenu = self.menuBar().addMenu('&Help')
helpMenu.addAction('&Information', self._helpPopUp, shortcut='Ctrl+H')
def _createItemsBox(self):
itemsLayout = QVBoxLayout()
self.cal0 = QCalendarWidget()
itemsLayout.addWidget(self.cal0)
self.currentDay = str(self.cal0.selectedDate().toJulianDay())
self.itemsBox = QListWidget()
self.itemsBox.setFixedHeight(ITEMS_HEIGHT)
self.itemsBox.setFixedWidth(ITEMS_WIDTH)
itemsLayout.addWidget(self.itemsBox)
self.activityButton = QPushButton('Add activity')
itemsLayout.addWidget(self.activityButton)
self.generalLayout.addLayout(itemsLayout)
self._new()
def _createFieldsBox(self):
fieldsLayout = QVBoxLayout()
self.meanVal = QLabel('')
self.maxVal = QLabel('')
self.minVal = QLabel('')
self.medianVal = QLabel('')
self.stdVal = QLabel('')
self.totVal = QLabel('')
fieldsLayout.addWidget(self.meanVal)
fieldsLayout.addWidget(self.maxVal)
fieldsLayout.addWidget(self.minVal)
fieldsLayout.addWidget(self.medianVal)
fieldsLayout.addWidget(self.stdVal)
fieldsLayout.addWidget(self.totVal)
self.fieldFormScroll = QScrollArea(self)
self.fieldFormScroll.setFixedHeight(ITEMS_HEIGHT)
self.fieldFormScroll.setFixedWidth(FIELDS_WIDTH)
self.fieldFormScroll.setAlignment(Qt.AlignmentFlag.AlignLeft)
fieldsLayout.addWidget(self.fieldFormScroll)
fieldButtons = QHBoxLayout()
self.fieldButton = QPushButton('Add field')
fieldButtons.addWidget(self.fieldButton)
self.updateButton = QPushButton('Update fields')
fieldButtons.addWidget(self.updateButton)
fieldsLayout.addLayout(fieldButtons)
fieldsLayout.setAlignment(Qt.AlignmentFlag.AlignBottom)
self.generalLayout.addLayout(fieldsLayout)
self.editBoxes = {}
def _createPlotBox(self):
plotLayout = QVBoxLayout()
calLayout = QHBoxLayout()
self.cal1 = QCalendarWidget()
self.cal2 = QCalendarWidget()
calLayout.addWidget(self.cal1)
calLayout.addWidget(self.cal2)
plotLayout.addLayout(calLayout)
self.figure = plt.figure()
self.plotWidget = FigureCanvas(self.figure)
self.plotWidget.setFixedHeight(ITEMS_HEIGHT)
self.plotWidget.setFixedWidth(PLOT_WIDTH)
plotControlWidget = QHBoxLayout()
plotControlWidget.addWidget(QLabel('Plot Range:'))
self.plotRange = QComboBox()
plotControlWidget.addWidget(self.plotRange)
self.plotRange.addItems(['Last 7 days', 'Last 30 days', 'All time', 'Custom'])
plotControlWidget.addWidget(QLabel('Field:'))
self.plotField = QComboBox()
plotControlWidget.addWidget(self.plotField)
plotControlWidget.addWidget(QLabel('Plot type:'))
self.plotType = QComboBox()
plotControlWidget.addWidget(self.plotType)
self.plotType.addItems(['Bar','Line','Scatter'])
self.plotButton = QPushButton('Plot')
plotControlWidget.addWidget(self.plotButton)
self.savePlotButton = QPushButton('Save Plot')
plotControlWidget.addWidget(self.savePlotButton)
plotLayout.addWidget(self.plotWidget)
plotLayout.addLayout(plotControlWidget)
plotLayout.setAlignment(Qt.AlignmentFlag.AlignBottom)
self.generalLayout.addLayout(plotLayout)
def _displayItems(self):
self.itemsBox.clear()
for key in self.items[self.currentDay].keys():
self.itemsBox.addItem(key)
self.itemsBox.setCurrentRow(0)
def _displayFields(self):
fieldFormScrollContents = QWidget()
self.fieldForm = QGridLayout(fieldFormScrollContents)
currentItem = self.itemsBox.currentItem()
if currentItem is not None:
self.editBoxes = {key: QLineEdit() for key in self.items[self.currentDay][currentItem.text()].keys()}
for row, key in enumerate(self.items[self.currentDay][currentItem.text()].keys()):
self.editBoxes[key].setText(str(self.items[self.currentDay][currentItem.text()][key]['value']))
self.fieldForm.addWidget(QLabel(key),row,0)
self.fieldForm.addWidget(self.editBoxes[key],row,1)
self.fieldForm.addWidget(QLabel(self.items[self.currentDay][currentItem.text()][key]['unit']),row,2)
self.fieldFormScroll.setWidget(fieldFormScrollContents)
self.plotField.clear()
self.plotField.addItems(list(self.editBoxes.keys()))
def _generatePlot(self):
self.figure.clear()
rangeSetting = self.plotRange.currentText()
plotMode = self.plotType.currentText()
if rangeSetting=='All time':
plotRangeKeys = sorted(list(self.items.keys()))
else:
endDay = QDate.currentDate().toJulianDay()
if rangeSetting=='Last 7 days':
startDay = endDay - 7
elif rangeSetting=='Last 30 days':
startDay = endDay - 30
elif rangeSetting=='Custom':
startDay = self.cal1.selectedDate().toJulianDay()
endDay = self.cal2.selectedDate().toJulianDay()
plotRangeKeys = [str(x) for x in range(startDay,endDay+1)]
plotX = []
plotY = []
for key in plotRangeKeys:
try:
plotY.append(float(self.items[key][self.itemsBox.currentItem().text()][self.plotField.currentText()]['value']))
plotX.append(float(key)-float(QDate.currentDate().toJulianDay()))
except (KeyError,AttributeError):
pass
plotX = np.array(plotX)
plotY = np.array(plotY)
ax = self.figure.add_subplot(111)
if (plotMode=='Bar'):
ax.bar(plotX,plotY)
elif (plotMode=='Line'):
ax.plot(plotX[plotY>0],plotY[plotY>0],marker='o')
elif (plotMode=='Scatter'):
ax.scatter(plotX[plotY>0],plotY[plotY>0])
ax.set_xlabel('Day')
try:
ax.set_ylabel(f'{self.plotField.currentText()} ({self.items[self.currentDay][self.itemsBox.currentItem().text()][self.plotField.currentText()]["unit"]})')
except AttributeError:
self.figure.clear()
self.plotWidget.draw()
plotY = np.array([x for x in plotY if not x==0])
if len(plotY):
self.meanVal.setText(f'Mean: {np.mean(plotY):.1f} {self.items[self.currentDay][self.itemsBox.currentItem().text()][self.plotField.currentText()]["unit"]}')
self.maxVal.setText(f'Max: {max(plotY):.1f} {self.items[self.currentDay][self.itemsBox.currentItem().text()][self.plotField.currentText()]["unit"]}')
self.minVal.setText(f'Min: {min(plotY):.1f} {self.items[self.currentDay][self.itemsBox.currentItem().text()][self.plotField.currentText()]["unit"]}')
self.medianVal.setText(f'Median: {np.median(plotY):.1f} {self.items[self.currentDay][self.itemsBox.currentItem().text()][self.plotField.currentText()]["unit"]}')
self.stdVal.setText(f'Standard deviation: {np.std(plotY):.1f} {self.items[self.currentDay][self.itemsBox.currentItem().text()][self.plotField.currentText()]["unit"]}')
self.totVal.setText(f'Total: {np.sum(plotY):.1f} {self.items[self.currentDay][self.itemsBox.currentItem().text()][self.plotField.currentText()]["unit"]}')
else:
self.figure.clear()
self.plotWidget.draw()
def _exportPlot(self):
imName = QFileDialog.getSaveFileName(self,'',self.currentDirectory)[0]
if imName[-4:]!='.png':
imName+='.png'
plt.savefig(imName)
def _addActivity(self):
self.activityDialogue = activityDialogue(self)
self.activityDialogue.setWindowTitle('New Activity')
self.activityDialogue.setWindowModality(Qt.ApplicationModal)
self.activityDialogue.show()
def _addField(self):
self.fieldDialogue = fieldDialogue(self)
self.fieldDialogue.setWindowTitle('New Field')
self.fieldDialogue.setWindowModality(Qt.ApplicationModal)
self.fieldDialogue.show()
def _updateFields(self):
currentItem = self.itemsBox.currentItem().text()
for key in self.editBoxes.keys():
self.items[self.currentDay][currentItem][key]['value'] = float(self.editBoxes[key].text())
def _changeDay(self):
self.currentDay = str(self.cal0.selectedDate().toJulianDay())
if not self.currentDay in self.items.keys():
self.items[self.currentDay] = self._copyDay()
self._displayItems()
self._displayFields()
def _copyDay(self):
today = QDate.currentDate().toJulianDay()
mostRecentDay = str(max([int(key) for key in self.items.keys() if int(key)<=today]))
newDay = {}
for itemKey in self.items[mostRecentDay].keys():
newDay.update({itemKey: {}})
for fieldKey in self.items[mostRecentDay][itemKey].keys():
newDay[itemKey].update(itemField(fieldKey,0,self.items[mostRecentDay][itemKey][fieldKey]['unit']))
return newDay
def _new(self):
if self.itemsLoad!=self.items:
self._unsavedChanges()
else:
self.saveFirst = False
self.openOK = True
if self.saveFirst:
if not self.currentFile=='':
self._save()
else:
self._saveAs()
if self.openOK:
self.items = {self.currentDay: {}}
self.itemsLoad = copy.deepcopy(self.items)
try:
self._displayItems()
self._displayFields()
except AttributeError: #Still initialising
pass
def _save(self):
if not self.currentFile=='':
saveFile(self.currentFile,self.items)
else:
saveFile(f'{self.currentDirectory}activitytracker_{datetime.datetime.now().strftime("%Y-%m-%d_%I:%M%p")}.json',self.items)
self.itemsLoad = copy.deepcopy(self.items)
def _saveAs(self):
fileName = QFileDialog.getSaveFileName(self,'',self.currentDirectory)[0]
if not fileName=='':
self.currentFile = fileName
self.currentDirectory = fileName[:-len(fileName.split('/')[-1])]
saveFile(fileName,self.items)
self._setTitle()
self.itemsLoad = copy.deepcopy(self.items)
def _open(self):
if self.itemsLoad!=self.items:
self._unsavedChanges()
else:
self.saveFirst = False
self.openOK = True
if self.saveFirst:
if not self.currentFile=='':
self._save()
else:
self._saveAs()
if self.openOK:
fileName = QFileDialog.getOpenFileName(self,'',self.currentDirectory)[0]
if not fileName=='':
try:
self.cal0.setSelectedDate(QDate.currentDate())
self.currentDay = str(self.cal0.selectedDate().toJulianDay())
self.items = loadFile(fileName)
if not self.currentDay in self.items.keys():
self.items[self.currentDay] = self._copyDay()
self.itemsLoad = copy.deepcopy(self.items)
self._displayItems()
self._displayFields()
self.currentFile = fileName
self.currentDirectory = fileName[:-len(fileName.split('/')[-1])]
self._setTitle()
except Exception:
pass
def _helpPopUp(self):
self.hWindow = helpWindow()
self.hWindow.setWindowTitle(f'Activity Logger {VERSION_STRING} - Information')
self.hWindow.show()
def _unsavedChanges(self):
self.wWindow = warningWindow(self)
self.wWindow.setWindowTitle('Unsaved Changes')
self.wWindow.setWindowModality(Qt.ApplicationModal)
self.wWindow.exec_()
def _setTitle(self):
self.setWindowTitle(f'Activity Logger {VERSION_STRING} - {self.currentDirectory}')
def closeEvent(self, event):
self.saveFirst = False
self.openOK = True
if self.itemsLoad!=self.items:
event.ignore()
self._unsavedChanges()
if self.saveFirst:
if not self.currentFile=='':
self._save()
else:
self._saveAs()
if self.openOK:
QApplication.closeAllWindows()
event.accept()
class helpWindow(QWidget):
'''General information called by Help menu'''
def __init__(self):
super().__init__()
layout = QVBoxLayout()
helpText = self._helpText()
for line in helpText:
label = QLabel(line)
label.setOpenExternalLinks(True)
layout.addWidget(label)
self.closeButton = QPushButton('OK')
layout.addWidget(self.closeButton)
self.setLayout(layout)
self.closeButton.clicked.connect(self.close)
def _helpText(self):
with open('helptext.txt','r') as f:
lines = f.readlines()
return lines
class warningWindow(QDialog):
'''Project change warning dialogue'''
def __init__(self,parent):
super().__init__()
self.parent = parent
layout = QVBoxLayout()
layout.addWidget(QLabel('Warning, project has been modified. Save changes?'))
buttonsLayout = QHBoxLayout()
self.acceptButton = QPushButton('Yes')
self.noButton = QPushButton('No')
self.cancelButton = QPushButton('Cancel')
buttonsLayout.addWidget(self.acceptButton)
buttonsLayout.addWidget(self.noButton)
buttonsLayout.addWidget(self.cancelButton)
layout.addLayout(buttonsLayout)
self.setLayout(layout)
self.noButton.clicked.connect(self._noClick)
self.cancelButton.clicked.connect(self._cancelClick)
self.acceptButton.clicked.connect(self._acceptClick)
def _acceptClick(self):
self.parent.openOK = True
self.parent.saveFirst = True
self.close()
def _noClick(self):
self.parent.openOK = True
self.parent.saveFirst = False
self.close()
def _cancelClick(self):
self.parent.openOK = False
self.parent.saveFirst = False
self.close()
class activityDialogue(QDialog):
'''Add new Activity type'''
def __init__(self,parent):
super().__init__()
self.parent = parent
layout = QVBoxLayout()
entryFieldLayout = QHBoxLayout()
entryFieldLayout.addWidget(QLabel('Name:'))
self.textBox = QLineEdit()
entryFieldLayout.addWidget(self.textBox)
layout.addLayout(entryFieldLayout)
buttons = QHBoxLayout()
self.acceptButton = QPushButton('OK')
buttons.addWidget(self.acceptButton)
self.cancelButton = QPushButton('Cancel')
buttons.addWidget(self.cancelButton)
layout.addLayout(buttons)
self.setLayout(layout)
self.acceptButton.clicked.connect(self._accept)
self.cancelButton.clicked.connect(self.close)
def _accept(self):
qText = self.textBox.text()
if (qText in self.parent.items[self.parent.currentDay].keys() or not len(qText)):
return
else:
self.parent.items[self.parent.currentDay].update({qText: {}})
self.parent._displayItems()
self.close()
class fieldDialogue(QDialog):
'''Add fields to activity'''
def __init__(self,parent):
super().__init__()
self.parent = parent
layout = QVBoxLayout()
entryFieldLayout = QHBoxLayout()
entryFieldLayout.addWidget(QLabel('Label:'))
self.labelBox = QLineEdit()
entryFieldLayout.addWidget(self.labelBox)
entryFieldLayout.addWidget(QLabel('Value:'))
self.valueBox = QLineEdit()
entryFieldLayout.addWidget(self.valueBox)
entryFieldLayout.addWidget(QLabel('Unit:'))
self.unitBox = QLineEdit()
entryFieldLayout.addWidget(self.unitBox)
layout.addLayout(entryFieldLayout)
buttons = QHBoxLayout()
self.acceptButton = QPushButton('OK')
buttons.addWidget(self.acceptButton)
self.cancelButton = QPushButton('Cancel')
buttons.addWidget(self.cancelButton)
layout.addLayout(buttons)
self.setLayout(layout)
self.acceptButton.clicked.connect(self._accept)
self.cancelButton.clicked.connect(self.close)
def _accept(self):
labelText = self.labelBox.text()
valueText = self.valueBox.text()
unitText = self.unitBox.text()
itemKey = self.parent.itemsBox.currentItem().text()
if not labelText in self.parent.items[self.parent.currentDay][itemKey].keys():
try:
self.parent.items[self.parent.currentDay][itemKey].update(itemField(labelText,float(valueText),unitText))
self.parent._displayFields()
except Exception:
pass
self.close()
else:
return
class controller:
'''Controller module'''
def __init__(self,model,view):
self._evaluate = model
self._view = view
self._connectSignalsAndSlots()
def _connectSignalsAndSlots(self):
self._view.activityButton.clicked.connect(self._view._addActivity)
self._view.itemsBox.currentItemChanged.connect(self._view._displayFields)
self._view.fieldButton.clicked.connect(self._view._addField)
self._view.updateButton.clicked.connect(self._view._updateFields)
self._view.cal0.selectionChanged.connect(self._view._changeDay)
self._view.plotButton.clicked.connect(self._view._generatePlot)
self._view.savePlotButton.clicked.connect(self._view._exportPlot)
def main():
'''Main loop'''
elApp = QApplication([])
elWindow = mainWindow()
elWindow.show()
controller(model=None,view=elWindow)
sys.exit(elApp.exec())
if (__name__=='__main__'):
main()