JustAudio/io/conf.cpp
Maurice O'Neal 73a8c03fa1 Added a logo to the project and changed the "settings" button to "menu"
and added some options to the menu that changes the behaviour of the
app. such as turning on/off the auto playing of files and the directory
and volume control. this is all kept persistent using an IDM formated
file in the app's config directory. still having issues with it not
calculating the durations properly but i now know why. it's because
QMediaPlayer can't calculate the durations of VBR files (variable bit-
rate) properly. other than that, everything seems to be working
perfectly. i'll add more feasures such as a next/previous file button
and a stop button.
2016-10-16 17:04:21 -04:00

151 lines
3.3 KiB
C++

#include "io/conf.h"
MenuItem::MenuItem(const QString &label, QWidget *widget, QObject *parent) : QWidgetAction(parent)
{
str = label;
wid = widget;
}
QWidget *MenuItem::createWidget(QWidget *parent)
{
QWidget *widget = new QWidget(parent);
QHBoxLayout *layout = new QHBoxLayout(widget);
layout->addWidget(new QLabel(str, parent));
layout->addStretch();
layout->addWidget(wid, 0, Qt::AlignRight);
return widget;
}
Conf::Conf(QObject *parent) : QObject(parent)
{
QFile file(confPath(), this);
QByteArray data;
if (file.open(QFile::ReadOnly))
{
data = file.readAll();
}
file.close();
Idm idm(data, this);
idm.rdHeader();
QByteArray pathBa = idm.value(LAST_PATH);
QByteArray nextBa = idm.value(NEXT_FILE);
QByteArray volBa = idm.value(VOLUME);
if (!pathBa.isEmpty()) lastPath = pathBa;
else lastPath = QDir::homePath();
if (!nextBa.isEmpty()) nextFileState = (nextBa == "Y");
else nextFileState = true;
if (!volBa.isEmpty()) volumeValue = rdInt(volBa);
else volumeValue = 50;
}
void Conf::sync()
{
Idm idm(this);
idm.wrHeader();
idm.insert(LAST_PATH, lastPath.toUtf8());
idm.insert(VOLUME, wrInt(volumeValue));
if (nextFileState) idm.insert(NEXT_FILE, "Y");
else idm.insert(NEXT_FILE, "N");
QFile file(confPath(), this);
file.open(QFile::WriteOnly | QFile::Truncate);
file.write(idm.getBuff());
}
void Conf::populateOptionsMenu(QMenu *menu, QWidget *parent)
{
QWidget *volWidget = new QWidget(parent);
QHBoxLayout *volLayout = new QHBoxLayout(volWidget);
QSlider *volSlider = new QSlider(parent);
QCheckBox *nextBox = new QCheckBox(parent);
volSlider->setMinimum(0);
volSlider->setMaximum(100);
volSlider->setValue(getVolume());
volSlider->setOrientation(Qt::Horizontal);
volLayout->addWidget(new QLabel("-"));
volLayout->addWidget(volSlider);
volLayout->addWidget(new QLabel("+"));
nextBox->setText("");
nextBox->setChecked(nextFile());
connect(volSlider, SIGNAL(valueChanged(int)), this, SLOT(setVolume(int)));
connect(volSlider, SIGNAL(valueChanged(int)), this, SIGNAL(volume(int)));
connect(nextBox, SIGNAL(clicked(bool)), this, SLOT(setNextFile(bool)));
menu->addAction(new MenuItem(tr("Volume"), volWidget, this));
menu->addAction(new MenuItem(tr("Auto play next file in directory"), nextBox, this));
}
void Conf::setLastPath(const QString &path)
{
lastPath = path; sync();
}
void Conf::setNextFile(bool state)
{
nextFileState = state; sync();
}
void Conf::setVolume(int value)
{
emit volume(value);
volumeValue = value; sync();
}
QString Conf::getLastPath()
{
if (!QDir(lastPath).exists())
{
lastPath = QDir::homePath();
}
return lastPath;
}
bool Conf::nextFile()
{
return nextFileState;
}
int Conf::getVolume()
{
return volumeValue;
}
QString Conf::confPath()
{
QString path = QDir::homePath() + '/';
#ifndef Q_OS_WIN32
path.append("/.config/");
#else
path.append("AppData/Local/");
#endif
QDir dir;
path.append(QApplication::applicationName() + '/');
dir.mkpath(path);
return path + "conf.idm";
}