babyfut.py 1.83 KB
Newer Older
Antoine Lima's avatar
Antoine Lima committed
1 2 3
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Antoine Lima's avatar
Antoine Lima committed
4
@author: Antoine Lima, Leo Reynaert, Domitille Jehenne
Antoine Lima's avatar
Antoine Lima committed
5 6
"""

7 8 9
import os
OnRasp = os.uname()[1] == 'raspberrypi'

Antoine Lima's avatar
Antoine Lima committed
10 11
import sys
import logging
Antoine Lima's avatar
Antoine Lima committed
12
from os.path import dirname, abspath, join
Antoine Lima's avatar
Antoine Lima committed
13

Antoine Lima's avatar
Antoine Lima committed
14
from PyQt5 import QtWidgets, QtCore
Antoine Lima's avatar
Antoine Lima committed
15
from PyQt5.QtWidgets import QMainWindow, QApplication
Antoine Lima's avatar
Antoine Lima committed
16

Antoine Lima's avatar
Antoine Lima committed
17 18 19
def getContent(path):
	contentFolder = join(dirname(dirname(abspath(__file__))), 'content')
	return join(contentFolder, path)
20

Antoine Lima's avatar
Antoine Lima committed
21
def getMainWin():
22 23
	from Babyfut.ui.mainwin import MainWin

Antoine Lima's avatar
Antoine Lima committed
24 25 26 27 28 29
	# Global function to find the (open) QMainWindow in application
	for widget in QApplication.instance().topLevelWidgets():
		if isinstance(widget, QMainWindow):
			return widget
	return None

Antoine Lima's avatar
Antoine Lima committed
30
if __name__=='__main__':
31 32 33 34 35 36 37 38 39
	print(__package__)
	from Babyfut.ui.mainwin import MainWin
	from Babyfut.modules import GameModule
	from Babyfut.core.player import Side
	from Babyfut.core.settings import Settings
	from Babyfut.core.input import GPIOThread
	from Babyfut.core.database import Database
	from Babyfut.core.replay import Replay as ReplayThread

40 41 42
	try:
		#logging.basicConfig(filename='babyfoot.log', level=logging.DEBUG)
		logging.basicConfig(level=logging.DEBUG)
43

44
		app = QtWidgets.QApplication(sys.argv)
Antoine Lima's avatar
Antoine Lima committed
45 46 47 48
		lang = Settings['ui.language']
		qtTranslator = QtCore.QTranslator()
		if lang!='en' and qtTranslator.load("translations/babyfut_{}.qm".format(lang)):
			app.installTranslator(qtTranslator)
49

50
		myapp = MainWin()
51

52 53 54 55
		if ReplayThread.isCamAvailable():
			threadReplay = ReplayThread(Side.Left)
			threadReplay.start()
			myapp.dispatchMessage({'replayThread': threadReplay}, toType=GameModule)
56

57 58
		threadGPIO = GPIOThread(myapp)
		threadGPIO.start()
59

60 61
		myapp.show()
		app.exec_()
62

63
		threadGPIO.stop()
64

65 66 67
		if ReplayThread.isCamAvailable():
			threadReplay.stop()
			threadReplay.join()
68

69
		threadGPIO.join()
70

71 72
	finally:
		GPIOThread.clean()
Antoine Lima's avatar
Antoine Lima committed
73
		Database.instance().close()