This repository has been archived by the owner on Nov 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
147 lines (102 loc) · 4.23 KB
/
app.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
import sys
import random
import signal
from PySide2 import Qt, QtCore, QtWidgets, QtGui
import serial
from serial.tools import list_ports
from tools import set_background_color
from views import MainView
from comports import SerialConnection, SerialParser
from analysis import KeyPress
import status
FPS = 20
class PianoApp(QtWidgets.QApplication):
def __init__(self):
super(PianoApp, self).__init__()
self.SerialConnection = SerialConnection()
self.window = MainWindow()
self.window.setWindowTitle("Piano Sensor")
self.window.show()
signal.signal(signal.SIGINT, self.window.quit)
self.window.closeSignal.connect(self.quit)
self.toolbar = QtWidgets.QToolBar()
self.window.addToolBar(self.toolbar)
self.mainView = MainView(self.toolbar, self.SerialConnection.getDropdownWidget())
self.window.setCentralWidget(self.mainView)
self.mainView.refresh.connect(self.SerialConnection.refresh)
self.mainView.resetEncoders.connect(lambda: self.SerialConnection.sendCmd('reset'))
self.mainView.resetSystem.connect(lambda: self.SerialConnection.sendCmd('sysreset'))
self.mainView.getPositions.connect(lambda: self.SerialConnection.sendCmd('pos'))
self.parser = SerialParser()
self.SerialConnection.textStream.connect(self.parser.parse_line)
# self.SerialConnection.textStream.connect(self.mainView.textOutputView.addText)
self.parser.comment.connect(self.mainView.textOutputView.addComment)
self.parser.newDataSet.connect(lambda i, t, p: self.mainView.resultsView.new_results(KeyPress(i, t,p)))
self.parser.newDataSet.connect(lambda i, t, p: self.mainView.textOutputView.new_results(KeyPress(i, t,p)))
status.set_status_logger(self.set_status_message)
status.set_status('Piano Sensor Ready..')
def quit(self):
self.mainView.textOutputView.quit()
def set_status_message(self, msg: str):
self.window.statusBar().showMessage(msg)
class MainWindow(QtWidgets.QMainWindow):
"""
Class docstring
"""
closeSignal = QtCore.Signal()
def __init__(self):
super(MainWindow, self).__init__()
self._running = True
exitShortcut = QtWidgets.QShortcut("CTRL+Q", self)
exitShortcut.activated.connect(self.close) # calls closeEvent
self._setupView()
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self._update)
self.timer.start(1000/FPS)
self.popup = None
def _setupView(self):
"""Initialize Main Window"""
self.setWindowIcon(QtGui.QIcon('assets/icon.jpeg'))
self.setGeometry(50, 50, 1600, 900)
# set_background_color(self, '#5a5d73')
# set_background_color(self, 'gray')
self.statusBar().showMessage('')
self._center()
self.raise_()
self.activateWindow()
def closeEvent(self, event):
"""Handle window close event"""
if event:
self.closeSignal.emit()
event.accept()
if not self._running:
return
print('shutting down..')
self._running = False
def quit(self, _signal=None, _=None):
""" Signal Handler to quit the program """
self.close()
def _error_popup(self, msg):
QtWidgets.QMessageBox.critical(self, "Unexpected Error", msg)
def _center(self):
""" Center Window on current display """
frameGm = self.frameGeometry()
screen = QtWidgets.QApplication.desktop().screenNumber(QtWidgets.QApplication.desktop().cursor().pos())
centerPoint = QtWidgets.QApplication.desktop().screenGeometry(screen).center()
frameGm.moveCenter(centerPoint)
self.move(frameGm.topLeft())
# qRect = self.frameGeometry()
# centerPoint = QtWidgets.QDesktopWidget().availableGeometry().center()
# qRect.moveCenter(centerPoint)
# self.move(qRect.topLeft())
def _update(self):
""" Gui Thread poll """
if not self._running:
# not running, pass update
self.close()
return
pass
if __name__ == "__main__":
app = PianoApp()
if sys.flags.interactive != 1:
sys.exit(app.exec_())