module.py 1.99 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
#!/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 import QtWidgets
12 13
from PyQt5.QtCore import QTime, QTimer, Qt
from PyQt5.QtWidgets import QTableWidgetItem, QComboBox, QApplication
14 15 16 17 18 19 20

from modules import *

class Module(QtWidgets.QWidget):
	def __init__(self, parent=None, widget=None):
		# UI Setup
		QtWidgets.QWidget.__init__(self, parent)
Antoine Lima's avatar
Antoine Lima committed
21
		self.mainwin = parent
22 23 24
		self.ui = widget
		self.ui.setupUi(self)

Antoine Lima's avatar
Antoine Lima committed
25 26 27 28
	def find(self, type):
		mod_idx = [i for i, x in enumerate(self.mainwin.modules) if isinstance(x, type)]
		return -1 if len(mod_idx)==0 else mod_idx[0]
	
29
	def switchModule(self, new_type):
Antoine Lima's avatar
Antoine Lima committed
30 31
		curmod_idx = self.find(type(self))
		newmod_idx = self.find(new_type)
32
		
Antoine Lima's avatar
Antoine Lima committed
33 34 35 36
		if curmod_idx<0:
			logging.error('Unknown panel {}'.format(type(self)))
		elif newmod_idx<0:
			logging.error('Unknown panel {}'.format(new_type))
37
		else:
Antoine Lima's avatar
Antoine Lima committed
38 39
			# Unfocus the current module
			self.mainwin.ui.panels.currentWidget().releaseKeyboard()
40 41 42
			if QApplication.focusWidget() != None:
				QApplication.focusWidget().clearFocus()
			
Antoine Lima's avatar
Antoine Lima committed
43 44 45 46
			# Swap modules by unloading, changing the ui then loading
			self.mainwin.modules[curmod_idx].unload()
			self.mainwin.ui.panels.setCurrentIndex(newmod_idx)
			self.mainwin.modules[newmod_idx].load()
47 48
			
			# Select first element of the Module
Antoine Lima's avatar
Antoine Lima committed
49 50 51
			self.mainwin.modules[newmod_idx].focusNextChild()
			self.mainwin.modules[newmod_idx].focusPreviousChild()
			self.mainwin.modules[newmod_idx].grabKeyboard()
52

Antoine Lima's avatar
Antoine Lima committed
53 54 55 56 57 58 59 60
	def send(self, to, **kwargs):
		mod_idx = self.find(to)
		
		if mod_idx<0:
			logging.error('Unknown panel {}'.format(to))
		else:
			self.mainwin.modules[mod_idx].other(**kwargs)
	
61 62 63 64 65 66 67 68
	def load(self):
		logging.warning('Unimplemented method "load" for {}'.format(self.__class__))

	def unload(self):
		logging.warning('Unimplemented method "unload" for {}'.format(self.__class__))

	def other(self, **kwargs):
		logging.warning('Unimplemented method "other" for {}'.format(self.__class__))