2016-10-03 00:47:02 -04:00
|
|
|
#include "ui.h"
|
|
|
|
|
|
|
|
Ui::Ui(const QStringList &args, QWidget *parent) : QWidget(parent)
|
|
|
|
{
|
2016-10-10 13:29:41 -04:00
|
|
|
QWidget *btnWid = new QWidget(this);
|
|
|
|
QHBoxLayout *btnLayout = new QHBoxLayout(btnWid);
|
2016-10-03 00:47:02 -04:00
|
|
|
QVBoxLayout *mainLayout = new QVBoxLayout(this);
|
|
|
|
|
2016-10-10 13:29:41 -04:00
|
|
|
fileName = new QLabel(this);
|
|
|
|
slider = new QSlider(this);
|
|
|
|
pausePlay = new Icon(Icon::PAUSE_PLAY, this);
|
|
|
|
settings = new Icon(Icon::SETTINGS, this);
|
|
|
|
open = new Icon(Icon::OPEN, this);
|
|
|
|
player = new QMediaPlayer(this);
|
|
|
|
ioDev = new AudFile(this);
|
|
|
|
pressed = false;
|
2016-10-03 00:47:02 -04:00
|
|
|
|
2016-10-10 13:29:41 -04:00
|
|
|
QFont fnt = fileName->font();
|
|
|
|
QRect rect = QApplication::desktop()->screenGeometry();
|
|
|
|
|
|
|
|
fnt.setBold(true);
|
|
|
|
|
|
|
|
setMinimumHeight(rect.height() / 6);
|
|
|
|
setMinimumWidth(rect.width() / 5);
|
|
|
|
setMaximumHeight(rect.height() / 6);
|
|
|
|
setMaximumWidth(rect.width() / 5);
|
|
|
|
setStyleSheet("background-color:white;");
|
|
|
|
|
|
|
|
fileName->setText(QApplication::applicationName());
|
|
|
|
fileName->setFont(fnt);
|
2016-10-03 00:47:02 -04:00
|
|
|
slider->setOrientation(Qt::Horizontal);
|
2016-10-10 13:29:41 -04:00
|
|
|
slider->setMinimumWidth((rect.width() / 5) - 100);
|
|
|
|
btnLayout->addWidget(pausePlay, 0, Qt::AlignCenter);
|
|
|
|
btnLayout->addSpacing(rect.height() / 40);
|
|
|
|
btnLayout->addWidget(open, 0, Qt::AlignCenter);
|
|
|
|
btnLayout->addSpacing(rect.height() / 40);
|
|
|
|
btnLayout->addWidget(settings, 0, Qt::AlignCenter);
|
|
|
|
mainLayout->addWidget(fileName, 0, Qt::AlignCenter);
|
|
|
|
mainLayout->addWidget(slider, 0, Qt::AlignCenter);
|
|
|
|
mainLayout->addWidget(btnWid, 0, Qt::AlignCenter);
|
|
|
|
|
|
|
|
connect(pausePlay, SIGNAL(pause()), player, SLOT(pause()));
|
|
|
|
connect(pausePlay, SIGNAL(play()), player, SLOT(play()));
|
|
|
|
connect(open, SIGNAL(open()), this, SLOT(openDialog()));
|
|
|
|
connect(player, SIGNAL(error(QMediaPlayer::Error)), this, SLOT(error(QMediaPlayer::Error)));
|
|
|
|
connect(player, SIGNAL(stateChanged(QMediaPlayer::State)), pausePlay, SLOT(stateChanged(QMediaPlayer::State)));
|
|
|
|
connect(player, SIGNAL(durationChanged(qint64)), this, SLOT(durationChanged(qint64)));
|
|
|
|
connect(player, SIGNAL(positionChanged(qint64)), this, SLOT(posChanged(qint64)));
|
|
|
|
connect(slider, SIGNAL(sliderPressed()), this, SLOT(sliderPressed()));
|
|
|
|
connect(slider, SIGNAL(sliderReleased()), this, SLOT(sliderReleased()));
|
2016-10-03 00:47:02 -04:00
|
|
|
|
|
|
|
if (args.size() > 1)
|
|
|
|
{
|
2016-10-10 13:29:41 -04:00
|
|
|
play(args[1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Ui::play(const QString &path)
|
|
|
|
{
|
|
|
|
QFileInfo info(path);
|
2016-10-03 00:47:02 -04:00
|
|
|
|
2016-10-10 13:29:41 -04:00
|
|
|
fileDir = info.path();
|
2016-10-03 00:47:02 -04:00
|
|
|
|
2016-10-10 13:29:41 -04:00
|
|
|
slider->setMinimum(0);
|
|
|
|
fileName->setText(info.fileName());
|
|
|
|
player->stop();
|
|
|
|
ioDev->close();
|
|
|
|
ioDev->openFile(path);
|
|
|
|
player->setMedia(0, ioDev);
|
|
|
|
player->play();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Ui::openDialog()
|
|
|
|
{
|
|
|
|
QFileDialog fileDialog(this);
|
|
|
|
|
|
|
|
fileDialog.setFileMode(QFileDialog::ExistingFile);
|
|
|
|
fileDialog.setNameFilter(tr("Audio Files (*.mp3 *.ogg *.wav *.flac)"));
|
|
|
|
|
|
|
|
if (fileDialog.exec())
|
|
|
|
{
|
|
|
|
play(fileDialog.selectedFiles()[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Ui::error(QMediaPlayer::Error error)
|
|
|
|
{
|
|
|
|
QMessageBox box(this);
|
|
|
|
|
|
|
|
if (error == QMediaPlayer::NoError)
|
|
|
|
{
|
|
|
|
box.setText(tr("An unknown error has occured"));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
box.setText(player->errorString());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error == QMediaPlayer::FormatError)
|
|
|
|
{
|
|
|
|
box.setIcon(QMessageBox::Warning);
|
2016-10-03 00:47:02 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-10-10 13:29:41 -04:00
|
|
|
box.setIcon(QMessageBox::Critical);
|
2016-10-03 00:47:02 -04:00
|
|
|
}
|
2016-10-10 13:29:41 -04:00
|
|
|
|
|
|
|
box.exec();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Ui::sliderPressed()
|
|
|
|
{
|
|
|
|
pressed = true;
|
2016-10-03 00:47:02 -04:00
|
|
|
}
|
|
|
|
|
2016-10-10 13:29:41 -04:00
|
|
|
void Ui::sliderReleased()
|
2016-10-03 00:47:02 -04:00
|
|
|
{
|
2016-10-10 13:29:41 -04:00
|
|
|
pressed = false;
|
|
|
|
|
|
|
|
player->setPosition(slider->value());
|
|
|
|
}
|
|
|
|
|
|
|
|
void Ui::posChanged(qint64 pos)
|
|
|
|
{
|
|
|
|
if (!pressed) slider->setSliderPosition(pos);
|
|
|
|
|
|
|
|
// if (slider->sliderPosition() == slider->maximum())
|
|
|
|
// {
|
|
|
|
// QTimer::singleShot(750, this, SLOT(nextFile()));
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
void Ui::durationChanged(qint64 len)
|
|
|
|
{
|
|
|
|
slider->setMaximum(len);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Ui::nextFile()
|
|
|
|
{
|
|
|
|
QDir dir(fileDir);
|
|
|
|
|
|
|
|
QStringList filterList;
|
|
|
|
|
|
|
|
filterList.append("*.mp3");
|
|
|
|
filterList.append("*.ogg");
|
|
|
|
filterList.append("*.flac");
|
|
|
|
filterList.append("*.wav");
|
|
|
|
|
|
|
|
QStringList list = dir.entryList(filterList, QDir::Files | QDir::Readable, QDir::Name);
|
|
|
|
int pos = list.indexOf(fileName->text());
|
|
|
|
|
|
|
|
if (pos != -1)
|
2016-10-03 00:47:02 -04:00
|
|
|
{
|
2016-10-10 13:29:41 -04:00
|
|
|
pos++;
|
|
|
|
|
|
|
|
if (pos < list.size())
|
|
|
|
{
|
|
|
|
play(fileDir + list[pos]);
|
|
|
|
}
|
2016-10-03 00:47:02 -04:00
|
|
|
}
|
|
|
|
}
|