main.py 3.25 KB
Newer Older
Antoine Lima's avatar
Base UI  
Antoine Lima committed
1
2
3
4
5
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 18 18:34:40 2018

Antoine Lima's avatar
UI v0.3    
Antoine Lima committed
6
@author: Antoine Lima, Leo Reynaert, Domitille Jehenne
Antoine Lima's avatar
Base UI  
Antoine Lima committed
7
8
"""

9
10
11
import os
OnRasp = os.uname()[1] == 'raspberrypi'

Antoine Lima's avatar
UI v0.3    
Antoine Lima committed
12
13
import sys
import logging
Antoine Lima's avatar
Antoine Lima committed
14
import threading
Antoine Lima's avatar
Antoine Lima committed
15
from os.path import dirname, abspath, join
Antoine Lima's avatar
Base UI  
Antoine Lima committed
16
17

from PyQt5 import QtWidgets
18
from PyQt5.QtWidgets import QGraphicsBlurEffect, QApplication
19
from PyQt5.QtCore import QTime, Qt
Antoine Lima's avatar
Antoine Lima committed
20

21
22
from ui.main_ui import Ui_MainWindow
from modules import *
Antoine Lima's avatar
Antoine Lima committed
23
from player import Side
24
from input import GPIOThread
Antoine Lima's avatar
Antoine Lima committed
25
from database import Database
Antoine Lima's avatar
Base UI  
Antoine Lima committed
26
27
28
29
30
31

class MainWin(QtWidgets.QMainWindow):
	def __init__(self, parent=None):
		QtWidgets.QWidget.__init__(self, parent)
		self.ui = Ui_MainWindow()
		self.ui.setupUi(self)
32
		
33
34
35
36
37
38
39
		#Background blur
		bgBlur = QGraphicsBlurEffect()
		bgBlur.setBlurHints(QGraphicsBlurEffect.QualityHint)
		#bgBlur.setBlurRadius(5)
		#self.ui.panels.setGraphicsEffect(bgBlur)
		
		# Module loading
Antoine Lima's avatar
Antoine Lima committed
40
41
		self.modules = [
			MenuModule(self),
42
43
			AuthQuickModule(self),
			AuthLeagueModule(self),
Antoine Lima's avatar
Antoine Lima committed
44
45
46
47
48
			GameModule(self),
			EndGameModule(self),
			LeaderboardModule(self),
			OptionsModule(self)
		]
49
50
		
		for mod in self.modules:
Antoine Lima's avatar
Antoine Lima committed
51
			self.ui.panels.addWidget(mod)
52
		
Antoine Lima's avatar
UI v0.3    
Antoine Lima committed
53
		self.ui.panels.setCurrentIndex(0)
54
		self.ui.panels.currentWidget().setFocus()
Antoine Lima's avatar
Antoine Lima committed
55
		self.ui.panels.setFocusProxy(self.ui.panels.currentWidget())
56
		self.ui.panels.currentWidget().load()
Antoine Lima's avatar
UI v0.3    
Antoine Lima committed
57
58
		self.displaySystemTime()
		self.startTimer(1000)
Antoine Lima's avatar
Antoine Lima committed
59
60
		
		self._loadSettings()
61
62
63
	
	#def eventFilter(target, event):
	#	return event.type()==QEvent.KeyPress and event.key() not in acceptedKeys
Antoine Lima's avatar
Antoine Lima committed
64

Antoine Lima's avatar
UI v0.3    
Antoine Lima committed
65
66
	def timerEvent(self, e):
		self.displaySystemTime()
Antoine Lima's avatar
Antoine Lima committed
67

Antoine Lima's avatar
UI v0.3    
Antoine Lima committed
68
69
	def displaySystemTime(self):
		self.ui.lcdTime.display(QTime.currentTime().toString("hh:mm:ss"))
Antoine Lima's avatar
Antoine Lima committed
70
71
72
73
74

	def findMod(self, type):
		mod_idx = [i for i, x in enumerate(self.modules) if isinstance(x, type)]
		return -1 if len(mod_idx)==0 else mod_idx[0]
	
75
76
77
78
79
80
	def dispatchMessage(self, msg, toType=None, toAll=False):
		if toType!=None:
                    modulesIdx = [self.findMod(toType)]
		else:
                    modulesIdx = self.modules if toAll else [self.findMod(type(self.ui.panels.currentWidget()))]			
		
Antoine Lima's avatar
Antoine Lima committed
81
82
		for modIdx in modulesIdx:
			self.modules[modIdx].other(**msg)
83
84
85
86
87
	
	@staticmethod
	def getContent(path):
		contentFolder = join(dirname(dirname(abspath(__file__))), 'content')
		return join(contentFolder, path)
Antoine Lima's avatar
Base UI  
Antoine Lima committed
88

Antoine Lima's avatar
Antoine Lima committed
89
	def _loadSettings(self):
90
91
		from settings import Settings
		
Antoine Lima's avatar
Antoine Lima committed
92
		if Settings['ui.fullscreen']:
93
94
95
96
97
			self.showFullScreen()
			QApplication.setOverrideCursor(Qt.BlankCursor);
		else:
			self.showNormal()
			QApplication.setOverrideCursor(Qt.ArrowCursor);
Antoine Lima's avatar
Antoine Lima committed
98

Antoine Lima's avatar
Base UI  
Antoine Lima committed
99
if __name__=='__main__':
Antoine Lima's avatar
Antoine Lima committed
100
	from settings import Settings
101
	from replay import Replay as ReplayThread
Antoine Lima's avatar
Antoine Lima committed
102
	
103
104
105
106
107
108
109
	try:
		#logging.basicConfig(filename='babyfoot.log', level=logging.DEBUG)
		logging.basicConfig(level=logging.DEBUG)
		
		app = QtWidgets.QApplication(sys.argv)
		myapp = MainWin()
		
110
111
112
113
		if ReplayThread.isCamAvailable():
			threadReplay = ReplayThread(Side.Left)
			threadReplay.start()
			myapp.dispatchMessage({'replayThread': threadReplay}, toType=GameModule)
114
115
116
117
118
119
120
121
122
		
		threadGPIO = GPIOThread(myapp)
		threadGPIO.start()
		
		myapp.show()
		app.exec_()
		
		threadGPIO.stop()
		
123
124
125
		if ReplayThread.isCamAvailable():
			threadReplay.stop()
			threadReplay.join()
126
127
128
129
130
		
		threadGPIO.join()
		
	finally:
		GPIOThread.clean()
Antoine Lima's avatar
Antoine Lima committed
131
		Database.instance().close()