113 lines
2.0 KiB
C++
Raw Normal View History

2018-04-11 20:27:22 +03:00
#include "backEnd.h"
#include <cmath>
2018-04-13 22:24:52 +03:00
#include <QDataStream>
2019-07-04 18:06:25 +03:00
#include <QDir>
2018-01-20 17:44:28 +03:00
2018-04-11 20:27:22 +03:00
BackEnd::BackEnd():
2018-01-20 17:44:28 +03:00
QObject()
{
2018-07-03 17:45:02 +03:00
reset();
readCnfig();
}
void BackEnd::reset(){
2018-04-11 22:13:34 +03:00
isFirstStart = true;
lvl = 1;
2018-07-03 17:45:02 +03:00
_animation = true;
_randomColor = false;
2018-04-11 20:27:22 +03:00
}
2019-07-04 18:06:25 +03:00
void BackEnd::writeConfig() const {
QDir dir(QDir::rootPath());
if (!QFileInfo::exists(MAIN_FOLDER) &&
!dir.mkpath(MAIN_FOLDER)) {
return;
}
QFile f(MAIN_SETINGS_FILE);
2018-01-20 17:44:28 +03:00
if(f.open(QIODevice::WriteOnly|QIODevice::Truncate)){
2018-04-13 22:24:52 +03:00
QDataStream stream(&f);
stream << lvl;
stream << isFirstStart;
2018-07-03 17:45:02 +03:00
stream << _animation;
stream << _randomColor;
2018-01-20 17:44:28 +03:00
f.close();
}
}
2018-04-11 20:27:22 +03:00
2018-07-03 17:45:02 +03:00
bool BackEnd::randomColor() const {
return _randomColor;
}
void BackEnd::setRandomColor(bool random) {
2019-07-04 16:26:45 +03:00
if (_randomColor != random) {
_randomColor = random;
writeConfig();
emit randomColorChanged();
}
2018-07-03 17:45:02 +03:00
}
bool BackEnd::animation() const{
return _animation;
}
void BackEnd::setAnimation(bool name) {
2019-07-04 16:26:45 +03:00
if (_animation != name) {
_animation = name;
writeConfig();
emit animationChanged();
}
2018-07-03 17:45:02 +03:00
}
2018-04-11 22:13:34 +03:00
void BackEnd::readCnfig() {
2019-07-04 18:06:25 +03:00
QFile f(MAIN_SETINGS_FILE);
2018-04-13 22:24:52 +03:00
if(f.exists() && f.open(QIODevice::ReadOnly)){
QDataStream stream(&f);
stream >> lvl;
stream >> isFirstStart;
2018-07-03 17:45:02 +03:00
stream >> _animation;
stream >> _randomColor;
if (f.size() <= 3) {
reset();
}
2018-01-20 17:44:28 +03:00
f.close();
2018-04-12 23:18:39 +03:00
if(lvl < 1 || lvl > 99) {
lvl = 1;
}
2018-07-03 17:45:02 +03:00
emit firstChanged();
2018-01-20 17:44:28 +03:00
}
2018-04-11 22:13:34 +03:00
}
unsigned short BackEnd::getMinSteps(const unsigned short lvl) const{
return pow(2, lvl) - 1;
}
void BackEnd::save(const short &lvl){
this->lvl = lvl;
writeConfig();
}
bool BackEnd::isFirst()const{
return isFirstStart;
}
void BackEnd::setShowHelp(bool state){
isFirstStart = state;
2018-07-03 17:45:02 +03:00
emit firstChanged();
2018-04-11 22:13:34 +03:00
writeConfig();
}
short BackEnd::read()const{
return lvl;
}
BackEnd::~BackEnd(){
writeConfig();
2018-01-20 17:44:28 +03:00
}