2016-10-03 00:47:02 -04:00
|
|
|
#include "ui.h"
|
|
|
|
|
|
|
|
Ui::Ui(const QStringList &args, QWidget *parent) : QWidget(parent)
|
|
|
|
{
|
2016-10-23 21:26:58 -04:00
|
|
|
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);
|
2016-10-10 13:29:41 -04:00
|
|
|
fileName = new QLabel(this);
|
|
|
|
slider = new QSlider(this);
|
2016-10-16 17:04:21 -04:00
|
|
|
menu = new QMenu(this);
|
|
|
|
trayIcon = new QSystemTrayIcon(QIcon(":/logo"), this);
|
|
|
|
settings = new Icon(Icon::MENU, this);
|
2016-10-10 13:29:41 -04:00
|
|
|
pausePlay = new Icon(Icon::PAUSE_PLAY, this);
|
|
|
|
open = new Icon(Icon::OPEN, this);
|
2016-10-23 21:26:58 -04:00
|
|
|
stop = new Icon(Icon::STOP, this);
|
|
|
|
next = new Icon(Icon::NEXT, this);
|
|
|
|
prev = new Icon(Icon::PREV, this);
|
2016-10-10 13:29:41 -04:00
|
|
|
player = new QMediaPlayer(this);
|
|
|
|
ioDev = new AudFile(this);
|
2016-10-16 17:04:21 -04:00
|
|
|
conf = new Conf(this);
|
2016-10-10 13:29:41 -04:00
|
|
|
pressed = false;
|
2016-10-03 00:47:02 -04:00
|
|
|
|
2016-10-16 17:04:21 -04:00
|
|
|
QFont fnt = fileName->font();
|
2016-10-10 13:29:41 -04:00
|
|
|
|
|
|
|
fnt.setBold(true);
|
|
|
|
|
|
|
|
setStyleSheet("background-color:white;");
|
|
|
|
|
2016-10-16 17:04:21 -04:00
|
|
|
settings->setMenu(menu);
|
|
|
|
fileName->setText(tr("Ready"));
|
2016-10-10 13:29:41 -04:00
|
|
|
fileName->setFont(fnt);
|
2016-10-03 00:47:02 -04:00
|
|
|
slider->setOrientation(Qt::Horizontal);
|
2016-10-16 17:04:21 -04:00
|
|
|
trayIcon->show();
|
2016-10-23 21:26:58 -04:00
|
|
|
trayIcon->setContextMenu(trayMenu);
|
2016-10-16 17:04:21 -04:00
|
|
|
conf->populateOptionsMenu(menu, this);
|
|
|
|
player->setVolume(conf->getVolume());
|
2016-10-23 21:26:58 -04:00
|
|
|
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);
|
2016-10-16 17:04:21 -04:00
|
|
|
btnLayout->addWidget(pausePlay);
|
2016-10-23 21:26:58 -04:00
|
|
|
btnLayout->addWidget(stop);
|
|
|
|
btnLayout->addWidget(next);
|
2016-10-16 17:04:21 -04:00
|
|
|
btnLayout->addWidget(open);
|
|
|
|
btnLayout->addWidget(settings);
|
|
|
|
mainLayout->addWidget(fileName);
|
|
|
|
mainLayout->addWidget(slider);
|
2016-10-10 13:29:41 -04:00
|
|
|
mainLayout->addWidget(btnWid, 0, Qt::AlignCenter);
|
|
|
|
|
2016-10-23 21:26:58 -04:00
|
|
|
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)));
|
2016-10-10 13:29:41 -04:00
|
|
|
connect(pausePlay, SIGNAL(pause()), player, SLOT(pause()));
|
|
|
|
connect(pausePlay, SIGNAL(play()), player, SLOT(play()));
|
2016-10-23 21:26:58 -04:00
|
|
|
connect(next, SIGNAL(next()), this, SLOT(nextFile()));
|
|
|
|
connect(prev, SIGNAL(prev()), this, SLOT(prevFile()));
|
|
|
|
connect(stop, SIGNAL(stop()), this, SLOT(stopPlayer()));
|
2016-10-10 13:29:41 -04:00
|
|
|
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)));
|
2016-10-23 21:26:58 -04:00
|
|
|
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()));
|
2016-10-10 13:29:41 -04:00
|
|
|
connect(slider, SIGNAL(sliderPressed()), this, SLOT(sliderPressed()));
|
|
|
|
connect(slider, SIGNAL(sliderReleased()), this, SLOT(sliderReleased()));
|
2016-10-16 17:04:21 -04:00
|
|
|
connect(conf, SIGNAL(volume(int)), player, SLOT(setVolume(int)));
|
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)
|
|
|
|
{
|
2016-10-23 21:26:58 -04:00
|
|
|
stopPlayer();
|
2016-10-03 00:47:02 -04:00
|
|
|
|
2016-10-16 17:04:21 -04:00
|
|
|
if (ioDev->openFile(path))
|
|
|
|
{
|
|
|
|
QFileInfo info(path);
|
2016-10-03 00:47:02 -04:00
|
|
|
|
2016-10-16 17:04:21 -04:00
|
|
|
conf->setLastPath(info.path());
|
|
|
|
fileName->setText(info.fileName());
|
|
|
|
player->setMedia(0, ioDev);
|
2016-10-23 21:26:58 -04:00
|
|
|
slider->setMinimum(ioDev->getOffset());
|
|
|
|
slider->setMaximum(ioDev->size());
|
2016-10-16 17:04:21 -04:00
|
|
|
player->play();
|
|
|
|
|
|
|
|
adjustSize();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
error(QMediaPlayer::ResourceError);
|
|
|
|
}
|
2016-10-23 21:26:58 -04:00
|
|
|
}
|
2016-10-16 17:04:21 -04:00
|
|
|
|
2016-10-23 21:26:58 -04:00
|
|
|
void Ui::stopPlayer()
|
|
|
|
{
|
|
|
|
ioDev->blockSignals(true);
|
|
|
|
player->stop();
|
|
|
|
ioDev->blockSignals(false);
|
|
|
|
slider->setValue(ioDev->getOffset());
|
2016-10-10 13:29:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Ui::openDialog()
|
|
|
|
{
|
|
|
|
QFileDialog fileDialog(this);
|
|
|
|
|
|
|
|
fileDialog.setFileMode(QFileDialog::ExistingFile);
|
|
|
|
fileDialog.setNameFilter(tr("Audio Files (*.mp3 *.ogg *.wav *.flac)"));
|
2016-10-16 17:04:21 -04:00
|
|
|
fileDialog.setDirectory(conf->getLastPath());
|
2016-10-10 13:29:41 -04:00
|
|
|
|
|
|
|
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"));
|
2016-10-16 17:04:21 -04:00
|
|
|
box.setIcon(QMessageBox::Warning);
|
2016-10-10 13:29:41 -04:00
|
|
|
}
|
2016-10-16 17:04:21 -04:00
|
|
|
else if (error == QMediaPlayer::FormatError)
|
2016-10-10 13:29:41 -04:00
|
|
|
{
|
2016-10-16 17:04:21 -04:00
|
|
|
box.setText(tr("The format of the media resource isn't fully supported. Playback may still be possible."));
|
|
|
|
box.setIcon(QMessageBox::Warning);
|
2016-10-10 13:29:41 -04:00
|
|
|
}
|
2016-10-16 17:04:21 -04:00
|
|
|
else if (error == QMediaPlayer::AccessDeniedError)
|
2016-10-10 13:29:41 -04:00
|
|
|
{
|
2016-10-16 17:04:21 -04:00
|
|
|
box.setText(tr("The appropriate permissions to play the media resource are not present"));
|
|
|
|
box.setIcon(QMessageBox::Critical);
|
2016-10-03 00:47:02 -04:00
|
|
|
}
|
2016-10-16 17:04:21 -04:00
|
|
|
else if (error == QMediaPlayer::ServiceMissingError)
|
2016-10-03 00:47:02 -04:00
|
|
|
{
|
2016-10-16 17:04:21 -04:00
|
|
|
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."));
|
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;
|
|
|
|
|
2016-10-23 21:26:58 -04:00
|
|
|
float pos = (float) slider->value() - ioDev->getOffset();
|
|
|
|
float max = (float) slider->maximum() - ioDev->getOffset();
|
|
|
|
float mul = pos / max;
|
|
|
|
|
|
|
|
player->setPosition(mul * player->duration());
|
2016-10-10 13:29:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Ui::posChanged(qint64 pos)
|
|
|
|
{
|
|
|
|
if (!pressed) slider->setSliderPosition(pos);
|
2016-10-23 21:26:58 -04:00
|
|
|
}
|
2016-10-10 13:29:41 -04:00
|
|
|
|
2016-10-23 21:26:58 -04:00
|
|
|
void Ui::nextFile()
|
|
|
|
{
|
|
|
|
fileDir('+');
|
2016-10-10 13:29:41 -04:00
|
|
|
}
|
|
|
|
|
2016-10-23 21:26:58 -04:00
|
|
|
void Ui::prevFile()
|
2016-10-10 13:29:41 -04:00
|
|
|
{
|
2016-10-23 21:26:58 -04:00
|
|
|
fileDir('-');
|
2016-10-10 13:29:41 -04:00
|
|
|
}
|
|
|
|
|
2016-10-23 21:26:58 -04:00
|
|
|
void Ui::fileDir(char direction)
|
2016-10-10 13:29:41 -04:00
|
|
|
{
|
2016-10-16 17:04:21 -04:00
|
|
|
QDir dir(conf->getLastPath());
|
2016-10-10 13:29:41 -04:00
|
|
|
|
|
|
|
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-23 21:26:58 -04:00
|
|
|
if (direction == '+') pos++;
|
|
|
|
else if (direction == '-') pos--;
|
2016-10-10 13:29:41 -04:00
|
|
|
|
|
|
|
if (pos < list.size())
|
|
|
|
{
|
2016-10-16 17:04:21 -04:00
|
|
|
play(conf->getLastPath() + QDir::separator() + list[pos]);
|
2016-10-10 13:29:41 -04:00
|
|
|
}
|
2016-10-03 00:47:02 -04:00
|
|
|
}
|
|
|
|
}
|
2016-10-23 21:26:58 -04:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|