Esempio n. 1
0
    def __init__(self, app, parent=None):
        super().__init__(parent)
        self._app = app

        class IconButton(QPushButton):
            def __init__(self, *args, **kwargs):
                super().__init__(*args, **kwargs)
                # 按钮文字一般是一个 symbol,长度控制为 40 是满足需求的
                self.setMaximumWidth(40)

        self._playback_modes = list(PlaybackMode.__members__.values())
        self._pm_alias_map = {
            PlaybackMode.one_loop: '单曲循环',
            PlaybackMode.sequential: '顺序播放',
            PlaybackMode.loop: '循环播放',
            PlaybackMode.random: '随机播放',
        }

        # initialize sub widgets
        self._layout = QHBoxLayout(self)
        self.previous_btn = IconButton(self)
        self.pp_btn = IconButton(self)
        self.next_btn = IconButton(self)
        self.pms_btn = QPushButton(self)
        self.volume_btn = VolumeButton(self)
        self.volume_btn.change_volume_needed.connect(
            lambda volume: setattr(self._app.player, 'volume', volume))
        self.playlist_btn = IconButton(parent=self)

        self.previous_btn.setObjectName('previous_btn')
        self.pp_btn.setObjectName('pp_btn')
        self.next_btn.setObjectName('next_btn')
        self.playlist_btn.setObjectName('playlist_btn')
        self.volume_btn.setObjectName('volume_btn')
        self.pms_btn.setObjectName('pms_btn')

        self.progress_slider = ProgressSlider(self)

        # TODO(simple): implementation
        self.pms_btn.setToolTip('修改播放模式(未实现,欢迎 PR)')
        self.volume_btn.setToolTip('调整音量(未实现,欢迎 PR)')
        self.playlist_btn.setToolTip('显示当前播放列表')
        self.progress_slider.setToolTip('拖动调节进度(未实现,欢迎 PR)')

        if not use_mac_theme():
            self.previous_btn.setIcon(self.style().standardIcon(
                QStyle.SP_MediaSkipBackward))
            self.pp_btn.setIcon(self.style().standardIcon(QStyle.SP_MediaPlay))
            self.next_btn.setIcon(self.style().standardIcon(
                QStyle.SP_MediaSkipForward))
            self.volume_btn.setIcon(self.style().standardIcon(
                QStyle.SP_MediaVolume))
            self.playlist_btn.setText('🎶')
        else:
            self.pp_btn.setCheckable(True)

        self.song_title_label = QLabel('No song is playing.', parent=self)
        self.song_title_label.setAlignment(Qt.AlignCenter)
        self.duration_label = QLabel('00:00', parent=self)
        self.position_label = QLabel('00:00', parent=self)

        self.next_btn.clicked.connect(self._app.player.play_next)
        self.previous_btn.clicked.connect(self._app.player.play_previous)
        self.pp_btn.clicked.connect(self._app.player.toggle)
        self.pms_btn.clicked.connect(self._switch_playback_mode)

        self._app.player.state_changed.connect(self._on_player_state_changed)
        self._app.player.playlist.playback_mode_changed.connect(
            self.on_playback_mode_changed)
        self._app.player.playlist.song_changed.connect(
            self.on_player_song_changed)
        self.progress_slider.resume_player_needed.connect(
            self._app.player.resume)
        self.progress_slider.pause_player_needed.connect(
            self._app.player.pause)
        self.progress_slider.change_position_needed.connect(
            lambda value: setattr(self._app.player, 'position', value))

        self._update_pms_btn_text()
        self._setup_ui()
Esempio n. 2
0
    def __init__(self, app, parent=None):
        super().__init__(parent)
        self._app = app

        class Button(QPushButton):
            def __init__(self, *args, **kwargs):
                super().__init__(*args, **kwargs)
                # 按钮文字一般是一个 symbol,长度控制为 40 是满足需求的
                self.setMaximumWidth(40)

        # initialize sub widgets
        self._layout = QHBoxLayout(self)
        self.previous_btn = Button(self)
        self.pp_btn = Button(self)
        self.next_btn = Button(self)
        self.pms_btn = Button(self)
        self.volume_btn = VolumeButton(self)
        self.volume_btn.change_volume_needed.connect(
            lambda volume: setattr(self._app.player, 'volume', volume))
        self.playlist_btn = Button(parent=self)

        self.previous_btn.setObjectName('previous_btn')
        self.pp_btn.setObjectName('pp_btn')
        self.next_btn.setObjectName('next_btn')
        self.playlist_btn.setObjectName('playlist_btn')
        self.volume_btn.setObjectName('volume_btn')

        self.progress_slider = ProgressSlider(self)

        # TODO(simple): implementation
        self.pms_btn.setToolTip('修改播放模式(未实现,欢迎 PR)')
        self.volume_btn.setToolTip('调整音量(未实现,欢迎 PR)')
        self.playlist_btn.setToolTip('显示当前播放列表')
        self.progress_slider.setToolTip('拖动调节进度(未实现,欢迎 PR)')

        if not use_mac_theme():
            self.previous_btn.setIcon(self.style().standardIcon(
                QStyle.SP_MediaSkipBackward))
            self.pp_btn.setIcon(self.style().standardIcon(QStyle.SP_MediaPlay))
            self.next_btn.setIcon(self.style().standardIcon(
                QStyle.SP_MediaSkipForward))
            self.volume_btn.setIcon(self.style().standardIcon(
                QStyle.SP_MediaVolume))
            self.playlist_btn.setText('🎶')
        else:
            self.pp_btn.setCheckable(True)

        self.song_title_label = QLabel('No song is playing.', parent=self)
        self.song_title_label.setAlignment(Qt.AlignCenter)
        self.duration_label = QLabel('00:00', parent=self)
        self.position_label = QLabel('00:00', parent=self)

        self.next_btn.clicked.connect(self._app.player.play_next)
        self.previous_btn.clicked.connect(self._app.player.play_previous)
        self.pp_btn.clicked.connect(self._app.player.toggle)

        # set widget layout
        self.progress_slider.setMinimumWidth(480)
        self.progress_slider.setMaximumWidth(600)
        self.progress_slider.setSizePolicy(QSizePolicy.Expanding,
                                           QSizePolicy.Preferred)
        self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Minimum)
        self._sub_layout = QVBoxLayout()
        self._sub_layout.addWidget(self.song_title_label)
        self._sub_layout.addWidget(self.progress_slider)

        self._layout.addSpacing(20)
        self._layout.addWidget(self.previous_btn)
        self._layout.addSpacing(8)
        self._layout.addWidget(self.pp_btn)
        self._layout.addSpacing(8)
        self._layout.addWidget(self.next_btn)
        self._layout.addSpacing(26)
        self._layout.addWidget(self.volume_btn)
        self._layout.addStretch(0)
        self._layout.addWidget(self.position_label)
        self._layout.addSpacing(7)
        self._layout.addLayout(self._sub_layout)
        self._layout.addSpacing(7)
        self._layout.addWidget(self.duration_label)
        self._layout.addSpacing(5)
        self._layout.addStretch(0)
        self._layout.addWidget(self.pms_btn)
        self._layout.addWidget(self.playlist_btn)
        self._layout.addSpacing(18)

        self._layout.setSpacing(0)
        self._layout.setContentsMargins(0, 0, 0, 0)