Source

VPlayer / minigui.py

# -*- coding: utf-8 -*-

from PyQt4 import QtGui
from PyQt4 import QtCore, Qt

from minigui_qt import Ui_Form
from length import Length


class ComboModel(QtCore.QAbstractItemModel):
    tracks = []
    def data(self, index, role):

        if role == QtCore.Qt.BackgroundRole:
            if self.tracks[index.row()].playing:
                return QtCore.QVariant(QtGui.QColor(130,140,150))
            elif index.row()%2==1:
                return QtCore.QVariant(QtGui.QColor(230,240,250))

        if role == QtCore.Qt.FontRole:
            if self.tracks[index.row()].playing:
                font = QtGui.QFont()
                font.setBold(True)
                return QtCore.QVariant(font)
        if role != QtCore.Qt.DisplayRole :
            return QtCore.QVariant()

        track = self.tracks[index.row()]

        return QtCore.QVariant('%s - %s' % (track.artist, track.title))

    def index(self, row, column, parent = QtCore.QModelIndex()):
        if row>len(self.tracks)-1 or len(self.tracks)==0:
            return QtCore.QModelIndex()
        track = self.tracks[row]
        item = '%s - %s' % (track.artist, track.title)
        return self.createIndex(row, column, item)

    def parent(self, index):
        return QtCore.QModelIndex()

    def rowCount(self, parent = QtCore.QModelIndex()):
        return len(self.tracks)

    def columnCount(self, parent = QtCore.QModelIndex()):
        return 1

class MiniGui(QtGui.QWidget):
    offset = QtCore.QPoint()
    def __init__(self, parent):
        QtGui.QWidget.__init__(self)
        self.ui = Ui_Form()
        self.ui.setupUi(self)
        #self.setWindowFlags(Qt.Qt.WindowStaysOnTopHint|Qt.Qt.FramelessWindowHint|Qt.Qt.Tool)
#        self.set_icons()
#        logo = QtGui.QPixmap()
#        logo.load('icon.png')
#        self.ui.label.setPixmap(logo)

        self.ui.label.mousePressEvent = self.labelMPE
        self.ui.label.mouseMoveEvent = self.labelMME
        self.ui.label.mouseReleaseEvent = self.labelMRE

        self.setParent(parent)

        QtCore.QObject.connect(self.parent().player, QtCore.SIGNAL('status'),
                                self.player_processor)

        QtCore.QObject.connect(self.ui.full,
                               QtCore.SIGNAL('clicked()'), self.show_full)
        QtCore.QObject.connect(self.ui.prev,
                               QtCore.SIGNAL('clicked()'),
                               lambda: self.parent().control.control("previous"))
        QtCore.QObject.connect(self.ui.playpause,
                               QtCore.SIGNAL('clicked()'), self.play_button)
        QtCore.QObject.connect(self.ui.next,
                               QtCore.SIGNAL('clicked()'),
                               lambda: self.parent().control.control("next"))

        self.combo_model = ComboModel()
        self.combo_model.tracks = parent.playlistwidget.playlist_model.tracks

        self.ui.combo.setModel(self.combo_model)
        tr = None
        for track in parent.playlistwidget.playlist_model.tracks:
            if track.is_playing():
                tr = track
                break
        if tr:
            index = parent.playlist_model.tracks.index(tr)
            self.ui.combo.setCurrentIndex(index)

        QtCore.QObject.connect(self.ui.combo,
        QtCore.SIGNAL('activated(int)'), self.combo_select)

        #if self.parent.player:
            #self.set_state(self.parent.player.state)
        #else:
            #self.set_state('none')

    def play_button(self):
        if self.parent().player:
            self.parent().control.pause()

    def show_full(self):
        self.parent().show()
        self.hide()

    def player_processor(self, status):
        try: status = status[0]
        except: return
        icon_image = QtGui.QIcon()
        if status not in ('start', 'unpause'):
            icon_image.addPixmap(QtGui.QPixmap(":/icons/images/pause.png"))
        else:
            icon_image.addPixmap(QtGui.QPixmap(":/icons/images/play.png"))
        self.ui.playpause.setIcon(icon_image)

    def labelMPE(self, event):
        event.accept()
        if (self.isWindow()):
            self.offset = event.globalPos() - self.pos()
        else:
            self.offset = event.pos()

    def labelMME(self, event):
        event.accept()
        if self.isWindow():
            self.move(event.globalPos() - self.offset)
        else:
            self.move(self.mapToParent(event.pos() - self.offset))

    def labelMRE(self, event):
        event.accept()
        self.offset = QtCore.QPoint();

    def combo_select(self, index):
        track = self.parent().playlistwidget.playlist_model.tracks[index.row()]
        self.parent().playlistwidget.emit_play(track)