Example #1
0
class Button_Memory_PRM():

    def __init__(self, buttonId):
        self.buttonId = buttonId
        self.db = RAM_DB()
        self.radioChannels = self.db.getRadioChannels()
        self.audioController = AudioController()

    def onClick(self, isLongClick = False):
        if(self.audioController.getGUICoolDown() == False and isLongClick == False):
            self.audioController.startGUICoolDown(1)
            # If there's an entry in the XML with memorized channel, we assign it to this button/memory bank
            if(self.radioChannels[self.buttonId] != None):
                self.audioController.setCurrentFMFrequency(self.radioChannels[self.buttonId][0])
        elif ((self.audioController.getGUICoolDown() == False and isLongClick == True)):
            # On long click, we memorize the current radio channel to this memory bank :)
            self.db.setRadioChannel(self.buttonId, self.audioController.getCurrentFMFrequency(), self.audioController.getCurrentFMStationName())
            self.radioChannels = self.db.getRadioChannels()
            self.audioController.updateRadioObservers()



    def createButton(self, sizeX, sizeY):
        button = PicButton(QPixmap("themes/default/img/MemoryButton.png"), QPixmap("themes/default/img/MemoryButton_Pressed.png"), sizeX, sizeY, str(self.buttonId), self.onClick)

        return button
class RadioMenuChannelMemory(QWidget):
    """
    This class provides a customized widget for control the memory banks of the radio channels.
    """

    __metaclass__ = ABCMeta

    def __init__(self, parent=None):
        """
        Constructor of the RadioMenuChannelMemory class.
        """

        super(RadioMenuChannelMemory, self).__init__(parent)

        self.db = RAM_DB()
        audioController = AudioController()
        radioChannels = self.db.getRadioChannels()

        self.memoryButtons = []
        self.labelsMemoryButtons = []
        for i in range(0, 9):
            self.memoryButtons.append(
                Button_Memory_PRM(i).createButton(60, 60))
            if (audioController.getPlayingRadio() == False
                    or audioController.getGUICoolDown() == True):
                self.memoryButtons[i].setOppacity(0.3)

        for i in range(0, 9):
            if (radioChannels[i] == None):
                memoryButtonLabel = "VacĂ­o"
            else:
                memoryButtonLabel = radioChannels[i][1]

            self.labelsMemoryButtons.append(CustomLabel().createLabel(
                memoryButtonLabel, Qt.AlignCenter, 7))

        hButtonBarBox = QHBoxLayout()

        hButtonBarBox.addStretch()

        for i in range(0, len(self.memoryButtons)):
            vButtonBox = QVBoxLayout()
            hMemoryButtonBox = QHBoxLayout()

            hMemoryButtonBox.addStretch()
            hMemoryButtonBox.addWidget(self.memoryButtons[i])
            hMemoryButtonBox.addStretch()
            vButtonBox.addLayout(hMemoryButtonBox)

            hMemoryLabelBox = QHBoxLayout()
            hMemoryLabelBox.addStretch()
            hMemoryLabelBox.addWidget(self.labelsMemoryButtons[i])
            hMemoryLabelBox.addStretch()
            vButtonBox.addLayout(hMemoryLabelBox)

            hButtonBarBox.addLayout(vButtonBox)
            hButtonBarBox.addStretch()

        self.setLayout(hButtonBarBox)

    @abstractmethod
    def update(self, *args, **kwargs):
        """
        Update method of the observer pattern.

        :param args: args
        :param kwargs: kwargs
        """

        self.updateView(*args, **kwargs)

    def updateView(self, *args, arg1, arg2):
        """
        Update view method of the observer pattern.

        :param args: Name of the notification.
        :param arg1: Other data.
        :param arg2: Other data.
        """

        if (args[0] == "UpdateRadioChannelData"):
            radioChannels = self.db.getRadioChannels()
            for i in range(0, len(self.labelsMemoryButtons)):
                self.labelsMemoryButtons[i].setText(radioChannels[i][1])

        elif (args[0] == "CoolDownStarted"):
            for i in range(0, len(self.memoryButtons)):
                self.memoryButtons[i].setOppacity(0.3)

        elif (args[0] == "CoolDownEnded"):
            for i in range(0, len(self.memoryButtons)):
                self.memoryButtons[i].setOppacity(1)