#include "musicwidget.h"
#include <QLayout>
#include <QMessageBox>
#include <stdlib.h>
#include <time.h>
MusicWidget::MusicWidget(QWidget *parent)
: QWidget(parent)
{
index = 0;
qsrand((unsigned)time(NULL));
QPalette palette;
QPalette labelPalette;
QPalette buttonPalette;
labelPalette.setColor(QPalette::WindowText, QColor(255, 255, 255));
buttonPalette.setColor(QPalette::ButtonText, QColor(255, 255, 255));
stop = new QPushButton(tr("stop"));
stop->setAutoFillBackground(true);
palette.setColor(QPalette::Button, QColor(128, 255, 0));
stop->setPalette(palette);
stop->setPalette(buttonPalette);
stop->setFlat(true);
pause = new QPushButton(tr("pause"));
pause->setAutoFillBackground(true);
palette.setColor(QPalette::Button, QColor(128, 255, 0));
pause->setPalette(palette);
pause->setPalette(buttonPalette);
pause->setFlat(true);
play = new QPushButton(tr("play"));
play->setAutoFillBackground(true);
palette.setColor(QPalette::Button, QColor(128, 255, 0));
play->setPalette(palette);
play->setPalette(buttonPalette);
play->setFlat(true);
next = new QPushButton(tr("next"));
next->setAutoFillBackground(true);
palette.setColor(QPalette::Button, QColor(128, 255, 0));
next->setPalette(palette);
next->setPalette(buttonPalette);
next->setFlat(true);
last = new QPushButton(tr("last"));
last->setAutoFillBackground(true);
palette.setColor(QPalette::Button, QColor(128, 255, 0));
last->setPalette(palette);
last->setPalette(buttonPalette);
last->setFlat(true);
volumeLabel = new QLabel;
volumeLabel->setText("Volume: ");
volumeLabel->setPalette(labelPalette);
timeLabel = new QLabel;
timeLabel->setText("Time: ");
timeLabel->setPalette(labelPalette);
showTime = new QLabel;
showTime->setText("00:00/00:00");
showTime->setPalette(labelPalette);
currentMusic = new QLabel;
currentMusic->setPalette(labelPalette);
currentMusic->setText("Name: ");
audioOutput = new Phonon::AudioOutput(Phonon::MusicCategory);
player = new Phonon::MediaObject;
seekSlider = new Phonon::SeekSlider;
volumeSlider = new Phonon::VolumeSlider;
Phonon::createPath(player, audioOutput);
seekSlider->setMediaObject(player);
volumeSlider->setAudioOutput(audioOutput);
playList = new PlayList;
playMode = new PlayMode;
mode = PlayMode::Sequence;
connect(play, SIGNAL(clicked()), player, SLOT(play()));
connect(stop, SIGNAL(clicked()), player, SLOT(stop()));
connect(pause, SIGNAL(clicked()), player, SLOT(pause()));
connect(player, SIGNAL( tick(qint64)), this, SLOT(changeText(qint64)));
connect(next, SIGNAL(clicked()), this, SLOT(nextMusic()));
connect(last, SIGNAL(clicked()), this, SLOT(lastMusic()));
connect(playList, SIGNAL(itemDoubleClicked(QTableWidgetItem*)), this, SLOT(selectMusic(QTableWidgetItem*)));
connect(playMode, SIGNAL(activated(int)), this, SLOT(changeMode(int)));
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(stop);
layout->addWidget(pause);
layout->addWidget(last);
layout->addWidget(play);
layout->addWidget(next);
QHBoxLayout *layoutTime = new QHBoxLayout;
layoutTime->addWidget(timeLabel);
layoutTime->addWidget(showTime);
layoutTime->addWidget(seekSlider);
QHBoxLayout *layoutVolume = new QHBoxLayout;
layoutVolume->addWidget(volumeLabel);
layoutVolume->addWidget(volumeSlider);
QHBoxLayout *layoutPlayList = new QHBoxLayout;
layoutPlayList->addWidget(playList);
QHBoxLayout *layoutMusic = new QHBoxLayout;
layoutMusic->addWidget(currentMusic);
layoutMusic->addWidget(playMode);
QVBoxLayout *mLayout = new QVBoxLayout;
mLayout->addLayout(layoutMusic);
mLayout->addLayout(layoutTime);
mLayout->addLayout(layoutVolume);
mLayout->addLayout(layout);
mLayout->addLayout(layoutPlayList);
setLayout(mLayout);
setAutoFillBackground(true);
//palette.setBrush(QPalette::Background, QBrush(QPixmap(":/23.png")));
palette.setColor(QPalette::Background, QColor(255, 128, 255));
setPalette(palette);
}
void MusicWidget::changeText(qint64 t)
{
__int64 minutie = t / 60000;
__int64 second = t / 1000 - minutie * 60;
QTime nowTime(0, minutie, second);
showTime->setText(nowTime.toString("mm:ss") + "/" + totlaTime->toString("mm:ss"));
if(t == player->totalTime())
{
switch(mode)
{
case PlayMode::Sequence: nextMusic(); break;
case PlayMode::Circular: {
if(index + 1 == vector.size())
{
index = 0;
}
playMusic(index);
break;
}
case PlayMode::Random: {
int temp = qrand() % vector.size();
while(temp == index)
{
temp = qrand() % vector.size();
}
index = temp;
playMusic(temp);
break;
}
case PlayMode::SingleCircular: {
playMusic(index);
break;
}
case PlayMode::SinglePlay: {
break;
}
}
}
}
void MusicWidget::setFileList(QStringList &list)
{
int i;
int currentSize = vector.size();
int nowSize;
QStringList fileList = list;
QList<QString>::Iterator iterator = fileList.begin();
for(; iterator != fileList.end(); ++iterator)
{
vector.push_back(*iterator);
}
nowSize = vector.size();
playList->setRows(nowSize - currentSize);
for(i = currentSize; i < nowSize; ++i)
{
QStringList string = vector[i].split("\\");
playList->setInformation(string[string.length() - 1], i);
//QMessageBox::information(NULL, tr("test"), time);
}
}
void MusicWidget::nextMusic()
{
if(mode != PlayMode::Random)
{
if(index + 1 < vector.size() && index + 1 >= 0)
{
playMusic(++index);
}
else if(index + 1 == vector.size())
{
if(mode != PlayMode::Sequence)
{
index = 0;
playMusic(index);
}
}
}
else
{
int temp = qrand() % vector.size();
while(temp == index)
{
temp = qrand() % vector.size();
}
index = temp;
playMusic(temp);
}
}
void MusicWidget::lastMusic()
{
if(mode != PlayMode::Random)
{
if(index - 1 >= 0 && index - 1 < vector.size())
{
playMusic(--index);
}
else if(index - 1 < 0)
{
index = 0;
playMusic(index);
}
}
else
{
int temp = qrand() % vector.size();
while(temp == index)
{
temp = qrand() % vector.size();
}
index = temp;
playMusic(temp);
}
}
void MusicWidget::playMusic(int i)
{
player->stop();
QStringList temp = vector[i].split("\\");
currentMusic->setText("Name : " + temp[temp.length() - 1]);
player->setCurrentSource(Phonon::MediaSource(vector[i]));
qint64 minutie = player->totalTime() / 60000;
qint64 second = player->totalTime() / 1000 - minutie * 60;
totlaTime = new QTime(0, minutie, second);
changeText((qint64)0);
player->play();
}
void MusicWidget::selectMusic(QTableWidgetItem *item)
{
index = item->row();
playMusic(index);
}
void MusicWidget::changeMode(int step)
{
switch(step)
{
case 0: mode = PlayMode::Sequence; break;
case 1: mode = PlayMode::Circular; break;
case 2: mode = PlayMode::Random; break;
case 3: mode = PlayMode::Singl
- 1
- 2
前往页