2016-10-16 17:04:21 -04:00
|
|
|
#include "io/conf.h"
|
|
|
|
|
|
|
|
MenuItem::MenuItem(const QString &label, QWidget *widget, QObject *parent) : QWidgetAction(parent)
|
|
|
|
{
|
|
|
|
str = label;
|
|
|
|
wid = widget;
|
|
|
|
}
|
|
|
|
|
|
|
|
QWidget *MenuItem::createWidget(QWidget *parent)
|
|
|
|
{
|
2016-10-23 21:26:58 -04:00
|
|
|
if (str.isEmpty())
|
|
|
|
{
|
|
|
|
wid->setParent(parent);
|
|
|
|
|
|
|
|
return wid;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QWidget *widget = new QWidget(parent);
|
|
|
|
QHBoxLayout *layout = new QHBoxLayout(widget);
|
|
|
|
|
|
|
|
layout->addWidget(wid);
|
|
|
|
layout->addWidget(new QLabel(str, parent));
|
2016-10-16 17:04:21 -04:00
|
|
|
|
2016-10-23 21:26:58 -04:00
|
|
|
return widget;
|
2016-10-16 17:04:21 -04:00
|
|
|
|
2016-10-23 21:26:58 -04:00
|
|
|
}
|
2016-10-16 17:04:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2016-10-23 21:26:58 -04:00
|
|
|
Icon *volUp = new Icon(Icon::VOL_UP, parent);
|
|
|
|
Icon *volDown = new Icon(Icon::VOL_DOWN, parent);
|
2016-10-16 17:04:21 -04:00
|
|
|
|
2016-10-23 21:26:58 -04:00
|
|
|
volUp->setSlider(volSlider);
|
|
|
|
volDown->setSlider(volSlider);
|
2016-10-16 17:04:21 -04:00
|
|
|
volSlider->setMinimum(0);
|
|
|
|
volSlider->setMaximum(100);
|
|
|
|
volSlider->setValue(getVolume());
|
|
|
|
volSlider->setOrientation(Qt::Horizontal);
|
2016-10-23 21:26:58 -04:00
|
|
|
volLayout->addWidget(volDown);
|
2016-10-16 17:04:21 -04:00
|
|
|
volLayout->addWidget(volSlider);
|
2016-10-23 21:26:58 -04:00
|
|
|
volLayout->addWidget(volUp);
|
|
|
|
nextBox->setText(tr("Auto play next file in directory"));
|
2016-10-16 17:04:21 -04:00
|
|
|
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)));
|
|
|
|
|
2016-10-23 21:26:58 -04:00
|
|
|
menu->addAction(new MenuItem("", volWidget, this));
|
|
|
|
menu->addAction(new MenuItem("", nextBox, this));
|
2016-10-16 17:04:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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";
|
|
|
|
}
|