authquick.py 3.97 KB
Newer Older
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 18 18:34:40 2018

@author: Antoine Lima, Leo Reynaert, Domitille Jehenne
"""

import logging

from PyQt5.QtWidgets import QSizePolicy

import modules
from module import Module
from modules.auth import AuthModuleBase
from player import Side, Player, PlayerGuest
from ui.authquick_ui import Ui_Form as AuthQuickWidget

class AuthQuickModule(AuthModuleBase):
	def __init__(self, parent):
		super().__init__(parent, AuthQuickWidget())
		
		self.smallPicSize = 200
		self.bigPicSize = 300
		self.leftSpacerWidth  = self.ui.widgetLayoutP1.layout().itemAt(0).spacerItem().geometry().width()
		self.rightSpacerWidth = self.ui.widgetLayoutP3.layout().itemAt(0).spacerItem().geometry().width()

	def load(self):
		logging.debug('Loading AuthQuickModule')
		super().load()
		
		for side in [Side.Left, Side.Right]:
			if len(self.players[side])==0:
				self.addPlayer(side,  PlayerGuest)
				
		self.updateSides()
		
	def unload(self):
		logging.debug('Unloading AuthQuickModule')
		super().unload()
		#self.updateSides()

Antoine Lima's avatar
Antoine Lima committed
43 44 45
	def createPlayerList(self):
		self.players = {Side.Left: list(), Side.Right: list()}
	
46
	def addPlayer(self, side, player):
Antoine Lima's avatar
Antoine Lima committed
47
		# If there is a placeholder Guest, clear it from the list, we don't need it anymore
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
		if len(self.players[side])>0 and self.players[side][0]==PlayerGuest:
			self.players[side].clear()
		
		if len(self.players[side])<2:
			self.players[side].append(player)
			
			if len(self.players[Side.Left])>1 and len(self.players[Side.Right])>1:
				self.handleDone()
			else:
				self.updateSides()
	
	def updateSides(self):
		'''
		There might be issues with this function in the future:
		Unfortunatly, Qt does not allow naming spacers, we can only access them indirectly,
		via the layout's itemAt function. The problem with this approach is that if a spacer
		is for a reason or another placed out of order by pyuic5, this won't work. Take a look to
		ui/auth4p_ui.py to adapt if this ever occurs
		'''

		spacerLeft  = self.ui.widgetLayoutP1.layout().itemAt(0).spacerItem()
		spacerRight = self.ui.widgetLayoutP3.layout().itemAt(0).spacerItem()
		
		# Update Left P1
		if len(self.players[Side.Left])>0:
			self.players[Side.Left][0].displayImg(self.ui.imgP1)
			self.ui.lblP1.setText(self.players[Side.Left][0].name)
		else:
			PlayerGuest.displayImg(self.ui.imgP1)
			self.ui.lblP1.setText('')
		
		# Update Left P2
		if len(self.players[Side.Left])>1:
			self.ui.imgP1.setMaximumSize(self.smallPicSize, self.smallPicSize)
			self.players[Side.Left][1].displayImg(self.ui.imgP2)
			self.ui.lblP2.setText(self.players[Side.Left][1].name)
			self.ui.widgetLayoutP2.setVisible(True)
			spacerLeft.changeSize(self.leftSpacerWidth, spacerLeft.geometry().height(), QSizePolicy.Expanding, QSizePolicy.Expanding)
		else:
			self.ui.imgP1.setMaximumSize(self.bigPicSize, self.bigPicSize)
			PlayerGuest.displayImg(self.ui.imgP2)
			self.ui.lblP2.setText('')
			self.ui.widgetLayoutP2.setVisible(False)
			spacerLeft.changeSize(0, spacerLeft.geometry().height(), QSizePolicy.Ignored, QSizePolicy.Expanding)
		
		# Update Right P1
		if len(self.players[Side.Right])>0:
			self.players[Side.Right][0].displayImg(self.ui.imgP3)
			self.ui.lblP3.setText(self.players[Side.Right][0].name)
		else:
			PlayerGuest.displayImg(self.ui.imgP3)
			self.ui.lblP3.setText('')
		
		# Update Right P2
		if len(self.players[Side.Right])>1:
			self.ui.imgP3.setMaximumSize(self.smallPicSize, self.smallPicSize)
			self.players[Side.Right][1].displayImg(self.ui.imgP4)
			self.ui.lblP4.setText(self.players[Side.Right][1].name)
			self.ui.widgetLayoutP4.setVisible(True)
			spacerRight.changeSize(self.rightSpacerWidth, spacerRight.geometry().height(), QSizePolicy.Expanding, QSizePolicy.Expanding)
		else:
			self.ui.imgP3.setMaximumSize(self.bigPicSize, self.bigPicSize)
			PlayerGuest.displayImg(self.ui.imgP4)
			self.ui.lblP4.setText('')
			self.ui.widgetLayoutP4.setVisible(False)
			spacerRight.changeSize(0, spacerRight.geometry().height(), QSizePolicy.Ignored, QSizePolicy.Expanding)