JustAudio/gui/ui.cpp

184 lines
5.0 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);
menu = new QMenu(this);
trayIcon = new QSystemTrayIcon(QIcon(":/logo"), this);
settings = new Icon(Icon::MENU, this);
pausePlay = new Icon(Icon::PAUSE_PLAY, this);
open = new Icon(Icon::OPEN, this);
player = new QMediaPlayer(this);
ioDev = new AudFile(this);
conf = new Conf(this);
pressed = false;
QFont fnt = fileName->font();
fnt.setBold(true);
setStyleSheet("background-color:white;");
settings->setMenu(menu);
fileName->setText(tr("Ready"));
fileName->setFont(fnt);
slider->setOrientation(Qt::Horizontal);
trayIcon->show();
conf->populateOptionsMenu(menu, this);
player->setVolume(conf->getVolume());
btnLayout->addWidget(pausePlay);
btnLayout->addWidget(open);
btnLayout->addWidget(settings);
mainLayout->addWidget(fileName);
mainLayout->addWidget(slider);
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(positionChanged(qint64)), this, SLOT(posChanged(qint64)));
connect(player, SIGNAL(durationChanged(qint64)), this, SLOT(durChanged(qint64)));
connect(slider, SIGNAL(sliderPressed()), this, SLOT(sliderPressed()));
connect(slider, SIGNAL(sliderReleased()), this, SLOT(sliderReleased()));
connect(conf, SIGNAL(volume(int)), player, SLOT(setVolume(int)));
if (args.size() > 1)
{
play(args[1]);
}
}
void Ui::play(const QString &path)
{
player->blockSignals(true);
player->stop();
pausePlay->stateChanged(QMediaPlayer::StoppedState);
if (ioDev->openFile(path))
{
QFileInfo info(path);
conf->setLastPath(info.path());
fileName->setText(info.fileName());
player->setMedia(0, ioDev);
slider->setValue(0);
slider->setMinimum(0);
pausePlay->stateChanged(QMediaPlayer::PlayingState);
player->play();
adjustSize();
}
else
{
error(QMediaPlayer::ResourceError);
}
player->blockSignals(false);
}
void Ui::openDialog()
{
QFileDialog fileDialog(this);
fileDialog.setFileMode(QFileDialog::ExistingFile);
fileDialog.setNameFilter(tr("Audio Files (*.mp3 *.ogg *.wav *.flac)"));
fileDialog.setDirectory(conf->getLastPath());
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"));
box.setIcon(QMessageBox::Warning);
}
else if (error == QMediaPlayer::FormatError)
{
box.setText(tr("The format of the media resource isn't fully supported. Playback may still be possible."));
box.setIcon(QMessageBox::Warning);
}
else if (error == QMediaPlayer::AccessDeniedError)
{
box.setText(tr("The appropriate permissions to play the media resource are not present"));
box.setIcon(QMessageBox::Critical);
}
else if (error == QMediaPlayer::ServiceMissingError)
{
box.setText(tr("A valid playback service was not found, playback cannot proceed."));
box.setIcon(QMessageBox::Critical);
}
else if (error == QMediaPlayer::ResourceError)
{
box.setText(tr("Media resource couldn't be resolved."));
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()) && conf->nextFile())
{
QTimer::singleShot(1000, this, SLOT(nextFile()));
}
}
void Ui::durChanged(qint64 len)
{
slider->setMaximum(len);
}
void Ui::nextFile()
{
QDir dir(conf->getLastPath());
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(conf->getLastPath() + QDir::separator() + list[pos]);
}
}
}