/*************************************************************************** * __________ __ ___. * Open \______ \ ____ ____ | | _\_ |__ _______ ___ * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ * \/ \/ \/ \/ \/ * $Id$ * * Copyright (C) 2010 Robert Bieber * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY * KIND, either express or implied. * ****************************************************************************/ #include "devicestate.h" #include #include #include #include #include #include #include #include #include #include #include DeviceState::DeviceState(QWidget *parent) : QWidget(parent), tabs(this) { /* UI stuff */ resize(500,400); setWindowIcon(QIcon(":/resources/windowicon.png")); setWindowTitle(tr("Device Settings")); QFormLayout* layout = new QFormLayout(this); layout->addWidget(&tabs); this->setLayout(layout); /* Loading the tabs */ QScrollArea* currentArea = 0; QWidget* panel; QFile fin(":/resources/deviceoptions"); fin.open(QFile::Text | QFile::ReadOnly); while(!fin.atEnd()) { QString line = QString(fin.readLine()); line = line.trimmed(); /* Continue on a comment or an empty line */ if(line[0] == '#' || line.length() == 0) continue; if(line[0] == '[') { QString buffer; for(int i = 1; line[i] != ']'; i++) buffer.append(line[i]); buffer = buffer.trimmed(); panel = new QWidget(); currentArea = new QScrollArea(); layout = new QFormLayout(panel); currentArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded); currentArea->setWidget(panel); currentArea->setWidgetResizable(true); tabs.addTab(currentArea, buffer); continue; } QStringList elements = line.split(";"); QString tag = elements[0].trimmed(); QString title = elements[1].trimmed(); QString type = elements[2].trimmed(); QString defVal = elements[3].trimmed(); elements = type.split("("); if(elements[0].trimmed() == "text") { QLineEdit* temp = new QLineEdit(defVal, currentArea); layout->addRow(title, temp); inputs.insert(tag, QPair(Text, temp)); temp->setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed)); QObject::connect(temp, SIGNAL(textChanged(QString)), this, SLOT(input())); } else if(elements[0].trimmed() == "check") { QCheckBox* temp = new QCheckBox(title, currentArea); layout->addRow(temp); if(defVal.toLower() == "true") temp->setChecked(true); else temp->setChecked(false); inputs.insert(tag, QPair(Check, temp)); QObject::connect(temp, SIGNAL(toggled(bool)), this, SLOT(input())); } else if(elements[0].trimmed() == "slider") { elements = elements[1].trimmed().split(","); int min = elements[0].trimmed().toInt(); QString maxS = elements[1].trimmed(); maxS.chop(1); int max = maxS.toInt(); QSlider* temp = new QSlider(Qt::Horizontal, currentArea); temp->setMinimum(min); temp->setMaximum(max); temp->setValue(defVal.toInt()); layout->addRow(title, temp); inputs.insert(tag, QPair(Slide, temp)); QObject::connect(temp, SIGNAL(valueChanged(int)), this, SLOT(input())); } else if(elements[0].trimmed() == "spin") { elements = elements[1].trimmed().split(","); int min = elements[0].trimmed().toInt(); QString maxS = elements[1].trimmed(); maxS.chop(1); int max = maxS.toInt(); QSpinBox* temp = new QSpinBox(currentArea); temp->setMinimum(min); temp->setMaximum(max); temp->setValue(defVal.toInt()); layout->addRow(title, temp); inputs.insert(tag, QPair(Spin, temp)); QObject::connect(temp, SIGNAL(valueChanged(int)), this, SLOT(input())); } else if(elements[0].trimmed() == "fspin") { elements = elements[1].trimmed().split(","); int min = elements[0].trimmed().toDouble(); QString maxS = elements[1].trimmed(); maxS.chop(1); int max = maxS.toDouble(); QDoubleSpinBox* temp = new QDoubleSpinBox(currentArea); temp->setMinimum(min); temp->setMaximum(max); temp->setValue(defVal.toDouble()); temp->setSingleStep(0.1); layout->addRow(title, temp); inputs.insert(tag, QPair(DSpin, temp)); QObject::connect(temp, SIGNAL(valueChanged(double)), this, SLOT(input())); } else if(elements[0].trimmed() == "combo") { elements = elements[1].trimmed().split(","); int defIndex = 0; QComboBox* temp = new QComboBox(currentArea); for(int i = 0; i < elements.count(); i++) { QString current = elements[i].trimmed(); if(i == elements.count() - 1) current.chop(1); temp->addItem(current, i); if(current == defVal) defIndex = i; } temp->setCurrentIndex(defIndex); layout->addRow(title, temp); inputs.insert(tag, QPair(Combo, temp)); QObject::connect(temp, SIGNAL(currentIndexChanged(int)), this, SLOT(input())); } } } DeviceState::~DeviceState() { } QVariant DeviceState::data(QString tag, int paramCount, skin_tag_parameter *params) { /* Handling special cases */ if(tag.toLower() == "fm") { QString path = tag[0].isLower() ? data("file").toString() : data("nextfile").toString(); return fileName(path, true); } else if(tag.toLower() == "fn") { QString path = tag[0].isLower() ? data("file").toString() : data("nextfile").toString(); return fileName(path, false); } else if(tag.toLower() == "fp") { if(tag[0].isLower()) return data("file").toString(); else return data("nextfile").toString(); } else if(tag.toLower() == "d") { QString path = tag[0].isLower() ? data("file").toString() : data("nextfile").toString(); if(paramCount > 0) return directory(path, params[0].data.numeric); else return QVariant(); } else if(tag == "pc") { int secs = data("?pc").toInt(); return secsToString(secs); } else if(tag == "pr") { int secs = data("?pt").toInt() - data("?pc").toInt(); if(secs < 0) secs = 0; return secsToString(secs); } else if(tag == "pt") { int secs = data("?pt").toInt(); return secsToString(secs); } else if(tag == "px") { int totalTime = data("?pt").toInt(); int currentTime = data("?pc").toInt(); return currentTime * 100 / totalTime; } else if(tag == "pS") { double threshhold = paramCount > 0 ? std::atof(params[0].data.text) : 10; if(data("?pc").toDouble() <= threshhold) return true; else return false; } else if(tag == "pE") { double threshhold = paramCount > 0 ? std::atof(params[0].data.text) : 10; if(data("?pt").toDouble() - data("?pc").toDouble() <= threshhold) return true; else return false; } QPair found = inputs.value(tag, QPair(Slide, 0)); if(found.second == 0 && tag[0] == '?') found = inputs.value(tag.right(2), QPair(Slide,0)); if(found.second == 0) return QVariant(); switch(found.first) { case Text: return dynamic_cast(found.second)->text(); case Slide: return dynamic_cast(found.second)->value(); case Spin: return dynamic_cast(found.second)->value(); case DSpin: return dynamic_cast(found.second)->value(); case Combo: if(tag[0] == '?') return dynamic_cast(found.second)->currentIndex(); else return dynamic_cast(found.second)->currentText(); case Check: return dynamic_cast(found.second)->isChecked(); } return QVariant(); } void DeviceState::setData(QString tag, QVariant data) { QPair found = inputs.value(tag, QPair(Slide, 0)); if(found.second == 0) return; switch(found.first) { case Text: dynamic_cast(found.second)->setText(data.toString()); break; case Slide: dynamic_cast(found.second)->setValue(data.toInt()); break; case Spin: dynamic_cast(found.second)->setValue(data.toInt()); break; case DSpin: dynamic_cast(found.second)->setValue(data.toDouble()); break; case Combo: dynamic_cast (found.second)-> setCurrentIndex(dynamic_cast (found.second)->findText(data.toString())); break; case Check: dynamic_cast(found.second)->setChecked(data.toBool()); break; } emit settingsChanged(); } void DeviceState::input() { emit settingsChanged(); } QString DeviceState::fileName(QString path, bool extension) { path = path.split("/").last(); if(!extension) { QString sum; QStringList name = path.split("."); for(int i = 0; i < name.count() - 1; i++) sum.append(name[i]); return sum; } else { return path; } } QString DeviceState::directory(QString path, int level) { QStringList dirs = path.split("/"); int index = dirs.count() - 1 - level; if(index < 0) index = 0; return dirs[index]; } QString DeviceState::secsToString(int secs) { int hours = 0; int minutes = 0; while(secs >= 60) { minutes++; secs -= 60; } while(minutes >= 60) { hours++; minutes -= 60; } QString retval; if(hours > 0) { retval += QString::number(hours); if(minutes < 10) retval += ":0"; else retval += ":"; } retval += QString::number(minutes); if(secs < 10) retval += ":0"; else retval += ":"; retval += QString::number(secs); return retval; }