JustAudio/gui/ui.cpp

249 lines
7.5 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);
Icon *trayPausePlay = new Icon(Icon::PAUSE_PLAY, this);
Icon *trayOpen = new Icon(Icon::OPEN, this);
Icon *trayStop = new Icon(Icon::STOP, this);
Icon *trayNext = new Icon(Icon::NEXT, this);
Icon *trayPrev = new Icon(Icon::PREV, this);
Icon *trayRestore = new Icon(Icon::RESTORE, this);
trayMenu = new QMenu(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);
stop = new Icon(Icon::STOP, this);
next = new Icon(Icon::NEXT, this);
prev = new Icon(Icon::PREV, 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();
trayIcon->setContextMenu(trayMenu);
conf->populateOptionsMenu(menu, this);
player->setVolume(conf->getVolume());
trayMenu->addAction(new MenuItem(trayPausePlay->toolTip(), trayPausePlay, this));
trayMenu->addAction(new MenuItem(trayOpen->toolTip(), trayOpen, this));
trayMenu->addAction(new MenuItem(trayStop->toolTip(), trayStop, this));
trayMenu->addAction(new MenuItem(trayNext->toolTip(), trayNext, this));
trayMenu->addAction(new MenuItem(trayPrev->toolTip(), trayPrev, this));
trayMenu->addAction(new MenuItem(trayRestore->toolTip(), trayRestore, this));
btnLayout->addWidget(prev);
btnLayout->addWidget(pausePlay);
btnLayout->addWidget(stop);
btnLayout->addWidget(next);
btnLayout->addWidget(open);
btnLayout->addWidget(settings);
mainLayout->addWidget(fileName);
mainLayout->addWidget(slider);
mainLayout->addWidget(btnWid, 0, Qt::AlignCenter);
connect(trayRestore, SIGNAL(restore()), this, SLOT(showNormal()));
connect(trayPausePlay, SIGNAL(pause()), player, SLOT(pause()));
connect(trayPausePlay, SIGNAL(play()), player, SLOT(play()));
connect(trayNext, SIGNAL(next()), this, SLOT(nextFile()));
connect(trayPrev, SIGNAL(prev()), this, SLOT(prevFile()));
connect(trayStop, SIGNAL(stop()), this, SLOT(stopPlayer()));
connect(trayOpen, SIGNAL(open()), this, SLOT(openDialog()));
connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(trayActivated(QSystemTrayIcon::ActivationReason)));
connect(pausePlay, SIGNAL(pause()), player, SLOT(pause()));
connect(pausePlay, SIGNAL(play()), player, SLOT(play()));
connect(next, SIGNAL(next()), this, SLOT(nextFile()));
connect(prev, SIGNAL(prev()), this, SLOT(prevFile()));
connect(stop, SIGNAL(stop()), this, SLOT(stopPlayer()));
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(stateChanged(QMediaPlayer::State)), trayPausePlay, SLOT(stateChanged(QMediaPlayer::State)));
connect(ioDev, SIGNAL(posChanged(qint64)), this, SLOT(posChanged(qint64)));
connect(ioDev, SIGNAL(endOfPlayback()), this, SLOT(nextFile()));
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)
{
stopPlayer();
if (ioDev->openFile(path))
{
QFileInfo info(path);
conf->setLastPath(info.path());
fileName->setText(info.fileName());
player->setMedia(0, ioDev);
slider->setMinimum(ioDev->getOffset());
slider->setMaximum(ioDev->size());
player->play();
adjustSize();
}
else
{
error(QMediaPlayer::ResourceError);
}
}
void Ui::stopPlayer()
{
ioDev->blockSignals(true);
player->stop();
ioDev->blockSignals(false);
slider->setValue(ioDev->getOffset());
}
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;
float pos = (float) slider->value() - ioDev->getOffset();
float max = (float) slider->maximum() - ioDev->getOffset();
float mul = pos / max;
player->setPosition(mul * player->duration());
}
void Ui::posChanged(qint64 pos)
{
if (!pressed) slider->setSliderPosition(pos);
}
void Ui::nextFile()
{
fileDir('+');
}
void Ui::prevFile()
{
fileDir('-');
}
void Ui::fileDir(char direction)
{
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)
{
if (direction == '+') pos++;
else if (direction == '-') pos--;
if (pos < list.size())
{
play(conf->getLastPath() + QDir::separator() + list[pos]);
}
}
}
void Ui::trayActivated(QSystemTrayIcon::ActivationReason reason)
{
Q_UNUSED(reason);
trayMenu->popup(trayIcon->geometry().center());
}
void Ui::changeEvent(QEvent *event)
{
if (event->type() == QEvent::WindowStateChange)
{
if (windowState() & Qt::WindowMinimized)
{
hide();
}
}
}
void Ui::closeEvent(QCloseEvent *event)
{
Q_UNUSED(event);
QCoreApplication::exit();
}