JustAudio/gui/ui.cpp
Maurice O'Neal 10e5af0588 I've fully written out code for the base of the app and tested to to be
working, also added svg icons to the user interface. the settings button
currently does nothing (still work in progress). during testing, i
discovered QMediaPlayer would have trouble playing some music files that
have an ID3 tag so AudFile was created as a way to read the size of the
ID3 tag and step over it when QMediaPlayer reads from the file. although
it fixed the playback issue, QMediaPlayer is now having trouble
calculating the duration properly without the tag. i already have a
solution to the porblem in mind, more to come in the next commit.
2016-10-10 13:29:41 -04:00

162 lines
4.1 KiB
C++

#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]);
}
}
}