示例#1
0
class PlayAudioMenu(QWidget):
    """
    This class creates a custom widget with the Play Audio Menu elements and layout.
    """

    __metaclass__ = ABCMeta

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

        :param controller: GUIController object.
        """

        super(PlayAudioMenu, self).__init__(parent)

        self.controller = controller
        self.db = RAM_DB()

        backButton = Button_Back_PAM(self.controller).createButton(269, 100)
        self.playButton = Button_Play_PAM(self.controller).createButton(60, 60)
        self.pauseButton = Button_Pause_PAM(self.controller).createButton(
            60, 60)
        nextButton = Button_Next_PAM(self.controller).createButton(60, 60)
        previousButton = Button_Previous_PAM(self.controller).createButton(
            60, 60)

        (self.fileName, self.pathFiles,
         self.metaDataList) = self.db.getAudioDB()
        path = self.pathFiles[self.db.getSelection()]

        audioController = AudioController()
        self.audioObject = audioController.getAudioObject()

        self.textLabel = CustomLabel().createLabel(path, Qt.AlignCenter)
        self.actualTimeLabel = CustomLabel().createLabel(
            "00:00", Qt.AlignCenter)
        self.totalTimeLabel = CustomLabel().createLabel(
            "00:00", Qt.AlignCenter)
        self.timeSlider = TimeSlider(0, 100, 0, self.sliderValueChangedByUser)

        verticalBoxLayout = QVBoxLayout()
        hRepTimeBox = QHBoxLayout()
        hRepButtonsBox = QHBoxLayout()
        hImageBox = QHBoxLayout()
        hButtonsMenuBox = QHBoxLayout()

        verticalBoxLayout.setContentsMargins(0, 0, 0, 0)
        verticalBoxLayout.addStretch()

        verticalBoxLayout.addStretch()
        verticalBoxLayout.addWidget(self.textLabel)
        verticalBoxLayout.addStretch()

        hImageBox.addStretch()
        self.imgQLabel = QLabel()
        self.imgQLabel.setMaximumHeight(150)
        self.imgQLabel.setMaximumWidth(150)

        self.pixmapCover = QPixmap()
        loaded = self.pixmapCover.load(
            getArtworkPath(self.metaDataList[self.db.getSelection()]))

        if (loaded == False):
            self.pixmapCover.load(self.db.getArtworkNotFoundPath())
        self.imgQLabel.setPixmap(self.pixmapCover.scaled(150, 150))

        hImageBox.addWidget(self.imgQLabel)
        hImageBox.addStretch()
        verticalBoxLayout.addLayout(hImageBox)

        verticalBoxLayout.addStretch()

        hRepTimeBox.addWidget(self.actualTimeLabel)
        hRepTimeBox.addStretch()
        hRepTimeBox.addWidget(self.totalTimeLabel)
        verticalBoxLayout.addLayout(hRepTimeBox)
        verticalBoxLayout.addWidget(self.timeSlider)
        verticalBoxLayout.addStretch()

        hRepButtonsBox.addStretch()
        if (self.audioObject.getStatus() == AudioStatus.PAUSED):
            self.pauseButton.hide()
        else:
            self.playButton.hide()
        hRepButtonsBox.addWidget(previousButton)
        hRepButtonsBox.addStretch()
        hRepButtonsBox.addWidget(self.playButton)
        hRepButtonsBox.addWidget(self.pauseButton)
        hRepButtonsBox.addStretch()
        hRepButtonsBox.addWidget(nextButton)
        hRepButtonsBox.addStretch()

        verticalBoxLayout.addLayout(hRepButtonsBox)
        verticalBoxLayout.addStretch()

        hButtonsMenuBox.addWidget(backButton)
        hButtonsMenuBox.addStretch()
        verticalBoxLayout.addLayout(hButtonsMenuBox)

        self.setLayout(verticalBoxLayout)

    def sliderValueChangedByUser(self):
        """
        Method when the slider notifies a change in his value made by the user.
        """

        self.audioObject.changeAudioSecond(self.timeSlider.getValue())

    @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] == "NewMetaData"):
            minutes = round((arg2[16] // 1000.0) // 60.0)
            seconds = round((arg2[16] // 1000.0) % 60.0)

            if minutes < 10:
                strMinutes = "0" + str(minutes)
            else:
                strMinutes = str(minutes)
            if seconds < 10:
                strSeconds = "0" + str(seconds)
            else:
                strSeconds = str(seconds)

            if arg2[1] == None:
                titleText = "Artista desconocido" + " - " + arg2[0]
            else:
                titleText = arg2[1] + " - " + arg2[0]

            self.textLabel.setText(titleText)
            self.totalTimeLabel.setText(strMinutes + ":" + strSeconds)
            self.timeSlider.setMaximum(arg2[16])
            loaded = self.pixmapCover.load(
                getArtworkPath(self.metaDataList[self.db.getSelection()]))

            if (loaded == False):
                self.pixmapCover.load(self.db.getArtworkNotFoundPath())
            self.imgQLabel.setPixmap(self.pixmapCover.scaled(150, 150))

            if (self.audioObject.getStatus() == AudioStatus.PAUSED):
                self.pauseButton.hide()
                self.playButton.show()
            else:
                self.playButton.hide()
                self.pauseButton.show()

        elif (args[0] == "AudioPaused"):
            self.playButton.show()
            self.pauseButton.hide()

        elif (args[0] == "AudioResumed"):
            self.playButton.hide()
            self.pauseButton.show()

        elif (args[0] == "UpdateReproductionSecond"):
            minutes = round((arg1 // 1000) // 60)
            seconds = round((arg1 // 1000)) % 60

            if minutes < 10:
                strMinutes = "0" + str(minutes)
            else:
                strMinutes = str(minutes)
            if seconds < 10:
                strSeconds = "0" + str(seconds)
            else:
                strSeconds = str(seconds)

            self.actualTimeLabel.setText(strMinutes + ":" + strSeconds)
            self.timeSlider.setValue(arg1)
class CustomListItemWidget(QWidget):
    """
    This class provides customized widgets for each element of a list.
    """
    def __init__(self, parent=None):
        """
        Constructor of the CustomListItemWidget class.
        """

        super(CustomListItemWidget, self).__init__(parent)

        self.db = RAM_DB()
        self.textQVBoxLayout = QVBoxLayout()
        self.textUpQLabel = QLabel()
        self.textDownQLabel = QLabel()
        self.path = ""
        self.textQVBoxLayout.addWidget(self.textUpQLabel)
        self.textQVBoxLayout.addWidget(self.textDownQLabel)
        self.allQHBoxLayout = QHBoxLayout()
        self.iconQLabel = QLabel()
        self.iconQLabel.setMaximumHeight(70)
        self.iconQLabel.setMaximumWidth(70)

        self.allQHBoxLayout.addWidget(self.iconQLabel, 0)
        self.allQHBoxLayout.addLayout(self.textQVBoxLayout, 1)
        self.setLayout(self.allQHBoxLayout)

        font1 = QFont('Myriada', 10)
        font1.setBold(True)
        font2 = QFont('Myriada', 9)
        font2.setBold(True)

        self.textUpQLabel.setFont(font1)
        self.textDownQLabel.setFont(font2)

        # setStyleSheet
        self.textUpQLabel.setStyleSheet('''
            color: rgb(10, 0, 0);
        ''')
        self.textDownQLabel.setStyleSheet('''
            color: rgb(128, 128, 128);
        ''')

    def setTextUp(self, text):
        """
        Sets the text of the label on the top of the item.

        :param text: Text of the label.
        """

        self.textUpQLabel.setText(text)

    def setPath(self, pathFile):
        """
        Sets the path to the song that is listed in the item.

        :param pathFile: Path to the file.
        """

        self.path = pathFile

    def getPath(self):
        """
        Returns the path to the song that is listed in the item.

        :return: Path to the song.
        """

        return self.path

    def setTextDown(self, text):
        """
        Sets the text of the label on the bottom of the item.

        :param text: Text of the label.
        """

        self.textDownQLabel.setText(text)

    def setIcon(self, imagePath):
        """
        Sets the icon of the item.

        :param imagePath: Full path to the icon.
        """

        pixmapIcon = QPixmap()

        loaded = pixmapIcon.load(imagePath)

        if (loaded == False):
            pixmapIcon.load(self.db.getArtworkNotFoundPath())

        self.iconQLabel.setPixmap(pixmapIcon.scaled(70, 70))

        return loaded