Fixed BUG with inner timers.

This commit is contained in:
Claudio Maradonna 2017-08-29 23:03:22 +02:00
parent a111de5426
commit ebdcd75860
15 changed files with 208 additions and 8 deletions

View File

@ -3,5 +3,6 @@
<file>resources/play.wav</file>
<file>resources/rest.wav</file>
<file>resources/stop.wav</file>
<file>resources/inner.wav</file>
</qresource>
</RCC>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 7.3 KiB

After

Width:  |  Height:  |  Size: 6.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 8.8 KiB

After

Width:  |  Height:  |  Size: 8.4 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.7 KiB

After

Width:  |  Height:  |  Size: 5.5 KiB

View File

@ -47,9 +47,22 @@ Page {
property int mRoundMs: settings.roundsMs
property int mRestMs: settings.restMs
property var mListOfInnerTimers: []
onStatusChanged: {
if (status === PageStatus.Active) {
Helper.pushPresetListPage();
Helper.pushPresetListPage()
var counter = 0
for (var i = 0; i < settingsColumn.children.length; i++) {
if (settingsColumn.children[i].objectName === 'innerSlider') {
// if (counter in mListOfInnerTimers) {
// counter++
// } else {
// settingsColumn.children[i].destroy()
// }
}
}
}
}
@ -65,7 +78,8 @@ Page {
presetName.text,
roundsSlider.value,
roundsTimeSlider.value,
Converter.sToMs(restSlider.value)
Converter.sToMs(restSlider.value),
Helper.getListOfInnerTimers()
)
boxingTimer.loadPreset(presetName.text)
@ -75,6 +89,20 @@ Page {
}
}
PushUpMenu {
MenuItem {
text: qsTr("Add inner time")
onClicked: Helper.addSliderForInnerTime()
}
MenuItem {
text: qsTr("Remove last inner time")
onClicked: Helper.removeLastSliderFromInnerTimers()
visible: mListOfInnerTimers.length
}
}
contentHeight: settingsColumn.height
Column {
@ -136,6 +164,12 @@ Page {
label: qsTr("Total rounds")
}
SectionHeader {
text: qsTr("Inner timers")
visible: mListOfInnerTimers.length
}
}
}
}

View File

@ -11,3 +11,64 @@ function playPauseTimer() {
boxingTimer.restore()
}
}
function addSliderForInnerTime(value) {
if (value === undefined) value = 5
var remainingTimeForInner = settingsPage.mRoundMs
var numberOfInnerTimers = settingsPage.mListOfInnerTimers.length
for (var i = 0; i < numberOfInnerTimers; i++) {
remainingTimeForInner -= settingsPage.mListOfInnerTimers[i].value
}
if (remainingTimeForInner > 0) {
var number = parseInt(numberOfInnerTimers) + 1
var id = "innerTime_" + number
var newSlider = Qt.createQmlObject(
"import QtQuick 2.2;" +
"import Sailfish.Silica 1.0;" +
"import 'converter.js' as Converter;" +
"Slider {" +
"id: " + id + ";" +
"objectName: 'innerSlider';" +
"width: parent.width;" +
"minimumValue: Converter.sToMs(5);" +
"maximumValue: "+ remainingTimeForInner +";" +
"stepSize: Converter.sToMs(5);" +
"value: "+ value +";" +
"valueText: Converter.msToTime(value);" +
"label: qsTr('Inner time "+ number +"');" +
"}",
settingsColumn,
"dynamicInnerTimerSlider"
);
var newArray = settingsPage.mListOfInnerTimers
newArray.push(newSlider);
settingsPage.mListOfInnerTimers = newArray;
}
}
function removeLastSliderFromInnerTimers() {
var newArray = settingsPage.mListOfInnerTimers
var slider = newArray.pop()
slider.destroy()
settingsPage.mListOfInnerTimers = newArray
}
function getListOfInnerTimers() {
var list = []
var timers = settingsPage.mListOfInnerTimers.length
for (var i = 0; i < timers; i++) {
list.push(settingsPage.mListOfInnerTimers[i].value)
}
return list;
}

BIN
resources/inner.wav Normal file

Binary file not shown.

View File

@ -66,6 +66,9 @@ desktop-file-install --delete-original \
%{_bindir}
%{_datadir}/%{name}
%{_datadir}/applications/%{name}.desktop
%{_datadir}/icons/hicolor/*/apps/%{name}.png
%{_datadir}/icons/hicolor/86x86/apps/%{name}.png
%{_datadir}/icons/hicolor/108x108/apps/%{name}.png
%{_datadir}/icons/hicolor/128x128/apps/%{name}.png
%{_datadir}/icons/hicolor/256x256/apps/%{name}.png
# >> files
# << files

View File

@ -39,7 +39,10 @@ Files:
- '%{_bindir}'
- '%{_datadir}/%{name}'
- '%{_datadir}/applications/%{name}.desktop'
- '%{_datadir}/icons/hicolor/*/apps/%{name}.png'
- '%{_datadir}/icons/hicolor/86x86/apps/%{name}.png'
- '%{_datadir}/icons/hicolor/108x108/apps/%{name}.png'
- '%{_datadir}/icons/hicolor/128x128/apps/%{name}.png'
- '%{_datadir}/icons/hicolor/256x256/apps/%{name}.png'
# For more information about yaml and what's supported in Sailfish OS
# build system, please see https://wiki.merproject.org/wiki/Spectacle

View File

@ -1,6 +1,7 @@
#include "boxingsettings.h"
#include <QString>
#include <QVariant>
BoxingSettings::BoxingSettings() :
mRounds(DefaultRounds),
@ -46,17 +47,30 @@ void BoxingSettings::loadPreset() {
this->mRounds = DefaultRounds;
this->mRoundMilliseconds = DefaultRoundMilliseconds;
this->mRestMilliseconds = DefaultRestMilliseconds;
this->mInnerTimers.clear();
} else {
this->beginGroup(this->mPreset);
this->mPresetName = this->value("name").toString();
this->mRounds = this->value("rounds").toInt();
this->mRoundMilliseconds = this->value("roundmilliseconds").toInt();
this->mRestMilliseconds = this->value("restmilliseconds").toInt();
// QList<int> list = this->value("innerTimers").value<QList<int>>();
this->mInnerTimers = this->value("innerTimers").value<QList<int>>();
//#ifdef QT_DEBUG
// qDebug() << list;
//#endif
// foreach(QVariant v, list) {
// this->mInnerTimers.append(v.toInt());
// }
this->endGroup();
}
}
void BoxingSettings::savePreset(const QString &name, const int &rounds, const int &roundMilliseconds, const int &restMilliseconds) {
void BoxingSettings::savePreset(const QString &name, const int &rounds, const int &roundMilliseconds, const int &restMilliseconds, const QList<int> &innerTimers) {
QString strippedName = this->convertRichNameToGroup(name);
this->beginGroup(strippedName);
@ -64,6 +78,12 @@ void BoxingSettings::savePreset(const QString &name, const int &rounds, const in
this->setValue("rounds", rounds);
this->setValue("roundmilliseconds", roundMilliseconds);
this->setValue("restmilliseconds", restMilliseconds);
#ifdef QT_DEBUG
qDebug() << innerTimers;
#endif
this->setValue("innerTimers", QVariant::fromValue(innerTimers));
this->endGroup();
this->setPreset(strippedName);

View File

@ -4,6 +4,7 @@
#include <QString>
#include <QStringList>
#include <QSettings>
#include <QList>
#ifdef QT_DEBUG
#include <QDebug>
@ -25,6 +26,7 @@ class BoxingSettings : public QSettings {
Q_PROPERTY(int rounds READ getRounds WRITE setRounds NOTIFY presetChanged)
Q_PROPERTY(int roundsMs READ getRoundMilliseconds WRITE setRoundMilliseconds NOTIFY presetChanged)
Q_PROPERTY(int restMs READ getRestMilliseconds WRITE setRestMilliseconds NOTIFY presetChanged)
Q_PROPERTY(QList<int> innerTimers READ getInnerTimers NOTIFY presetChanged)
/** @endcond */
/**
@ -78,7 +80,7 @@ class BoxingSettings : public QSettings {
*
* @brief Save or override settings about specific preset
*/
Q_INVOKABLE void savePreset(const QString &name, const int &rounds, const int &roundMilliseconds, const int &restMilliseconds);
Q_INVOKABLE void savePreset(const QString &name, const int &rounds, const int &roundMilliseconds, const int &restMilliseconds, const QList<int> &innerTimers);
/**
* @addtogroup QML
@ -115,6 +117,20 @@ class BoxingSettings : public QSettings {
*/
inline int getRestMilliseconds() { return this->mRestMilliseconds; }
/**
* @brief gerInnerTimers()
*
* @return the list of inner timers
*/
inline QList<int> getInnerTimers() { return this->mInnerTimers; }
/**
* @brief getInnerTimer(const int &index)
*
* @return the specific timer in milliseconds
*/
inline int getInnerTimer(const int &index) { return this->mInnerTimers.at(index); }
/**
* @fn QString getPreset()
*
@ -166,6 +182,8 @@ class BoxingSettings : public QSettings {
int mRoundMilliseconds;
int mRestMilliseconds;
QList<int> mInnerTimers;
QString mPreset;
QString mPresetName;

View File

@ -8,16 +8,20 @@
BoxingTimer::BoxingTimer() :
mRound(0),
mInnerRemainingMilliseconds(0),
mStatus(Status::Stop),
mOldStatus(Status::Default) {
this->setTimerType(Qt::VeryCoarseTimer);
this->settings = new BoxingSettings();
this->mRemainingMilliseconds = settings->getRoundMilliseconds();
this->mRemainingMilliseconds = this->settings->getRoundMilliseconds();
this->setInnerTimers();
this->playBell = new QSound(":/audio/resources/play.wav");
this->restBell = new QSound(":/audio/resources/rest.wav");
this->stopBell = new QSound(":/audio/resources/stop.wav");
this->innerBell = new QSound(":/audio/resources/inner.wav");
QObject::connect(this, SIGNAL(statusChanged()), this, SLOT(applyStatus()));
QObject::connect(this, SIGNAL(timeout()), this, SLOT(updateRemainingMilliseconds()));
@ -68,8 +72,10 @@ void BoxingTimer::applyStatus() {
case Status::Reset:
if (this->mOldStatus == Status::Start) {
this->setRemainingMilliseconds(this->settings->getRoundMilliseconds());
this->setInnerTimers();
} else if (this->mOldStatus == Status::Rest) {
this->setRemainingMilliseconds(this->settings->getRestMilliseconds());
this->setInnerTimers();
}
if (this->isActive()) {
@ -109,11 +115,41 @@ void BoxingTimer::updateRemainingMilliseconds() {
this->mStatus = Status::Start;
this->setRemainingMilliseconds(this->settings->getRoundMilliseconds());
this->setInnerTimers();
this->nextRound();
}
this->songsBell();
}
this->updateInnerRemainingMilliseconds();
}
void BoxingTimer::updateInnerRemainingMilliseconds() {
if (this->mInnerTime >= 0 && this->mStatus == Status::Start) {
this->mInnerRemainingMilliseconds -= 1000;
if (this->mInnerRemainingMilliseconds <= 0) {
if (++this->mInnerTime >= this->mNumberOfInnerTimers) {
this->mInnerTime = 0;
}
this->mInnerRemainingMilliseconds = this->settings->getInnerTimer(this->mInnerTime);
this->innerBell->play();
}
}
}
void BoxingTimer::setInnerTimers() {
this->mNumberOfInnerTimers = this->settings->getInnerTimers().size();
this->mInnerTime = -1;
if (this->mNumberOfInnerTimers > 0) {
this->mInnerTime = 0;
this->mInnerRemainingMilliseconds = this->settings->getInnerTimer(this->mInnerTime);
}
}
const QString BoxingTimer::remainingTimeToString() {
@ -156,6 +192,8 @@ void BoxingTimer::loadPreset(const QString &preset) {
this->mRemainingMilliseconds = settings->getRoundMilliseconds();
this->mStatus = Status::Stop;
this->setInnerTimers();
emit presetChanged();
emit roundsToStringChanged();
emit remainingTimeToStringChanged();

View File

@ -155,17 +155,22 @@ class BoxingTimer : public QTimer {
int mRoundsMilliseconds;
int mRemainingMilliseconds;
int mInnerTime;
int mNumberOfInnerTimers;
int mInnerRemainingMilliseconds;
void setRemainingMilliseconds(const int &milliseconds);
void setRound(const int &round);
QSound *playBell;
QSound *restBell;
QSound *stopBell;
QSound *innerBell;
/**
* @fn void songsBell()
*
* @brief Play the bell check actual status
* @brief Play the bell if status changes
*/
void songsBell();
@ -181,6 +186,20 @@ class BoxingTimer : public QTimer {
*/
void nextRound();
/**
* @fn void updateInnerRemainingMilliseconds()
*
* @brief Update remaining milliseconds of inner timer
*/
void updateInnerRemainingMilliseconds();
/**
* @fn void setInnerTimers()
*
* @brief Setup inner timers from settings
*/
void setInnerTimers();
signals:
void remainingTimeToStringChanged();
void roundsToStringChanged();

View File

@ -29,6 +29,7 @@
*/
#include <QtQuick>
#include <QList>
#include <sailfishapp.h>
@ -46,6 +47,8 @@ int main(int argc, char *argv[])
//
// To display the view, call "show()" (will show fullscreen on device).
qRegisterMetaTypeStreamOperators<QList<int>>("QList<int>");
qmlRegisterType<BoxingTimer>("Pw.Unitoo.Backend", 1, 0, "BoxingTimer");
qmlRegisterType<BoxingSettings>("Pw.Unitoo.Backend", 1, 0, "BoxingSettings");