JustAudio/gui/ui.cpp

162 lines
4.1 KiB
C++
Raw Normal View History

#include "ui.h"
Ui::Ui(const QStringList &args, QWidget *parent) : QWidget(parent)
{
QWidget *btnWid = new QWidget(this);
QHBoxLayout *btnLayout = new QHBoxLayout(btnWid);
QVBoxLayout *mainLayout = new QVBoxLayout(this);
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;
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);
slider->setOrientation(Qt::Horizontal);
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()));
if (args.size() > 1)
{
play(args[1]);
}
}
void Ui::play(const QString &path)
{
QFileInfo info(path);
fileDir = info.path();
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);
}
else
{
box.setIcon(QMessageBox::Critical);
}
box.exec();
}
void Ui::sliderPressed()
{
pressed = true;
}
void Ui::sliderReleased()
{
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)
{
pos++;
if (pos < list.size())
{
play(fileDir + list[pos]);
}
}
}