Fix frontend displayed value of Sudden Death timeout being off-by-one
authorWuzzy <Wuzzy2@mail.ru>
Sun, 30 Sep 2018 01:41:12 +0200
changeset 13822 419de2dea82b
parent 13821 373813316812
child 13823 a8fd5490932f
Fix frontend displayed value of Sudden Death timeout being off-by-one
ChangeLog.txt
QTfrontend/ui/page/pagescheme.cpp
QTfrontend/ui/page/pagescheme.h
QTfrontend/ui/widget/SDTimeoutSpinBox.cpp
QTfrontend/ui/widget/SDTimeoutSpinBox.h
--- a/ChangeLog.txt	Sat Sep 29 23:13:02 2018 +0200
+++ b/ChangeLog.txt	Sun Sep 30 01:41:12 2018 +0200
@@ -44,6 +44,7 @@
  * Fix player rankings on round draw: Clans that died in the same turn now have the same rank
  * Fix rare crash when aborting video encoding in progress
  * Fix critical failure to cleanup teams list after rejoining game under certain conditions
+ * Fix displayed Sudden Death timeout being off by one
  * Controllers are detected again
  * Fix failure to shutdown game window properly after player got kicked
  * No longer allow having schemes with equal names (case-insensitive)
--- a/QTfrontend/ui/page/pagescheme.cpp	Sat Sep 29 23:13:02 2018 +0200
+++ b/QTfrontend/ui/page/pagescheme.cpp	Sun Sep 30 01:41:12 2018 +0200
@@ -30,6 +30,7 @@
 #include "gameSchemeModel.h"
 #include "pagescheme.h"
 #include "FreqSpinBox.h"
+#include "SDTimeoutSpinBox.h"
 #include "MinesTimeSpinBox.h"
 
 
@@ -251,9 +252,21 @@
     l->setWhatsThis(wtSuddenDeath);
     l->setPixmap(QPixmap(":/res/iconSuddenDeathTime.png"));
     glBSLayout->addWidget(l,3,1,1,1);
-    SB_SuddenDeath = new QSpinBox(gbBasicSettings);
+    /* NOTE:
+       The internally stored value for Sudden Death Timeout
+       is defined as
+       "number of full rounds to play till Sudden Death, minus one"
+       i.e. value 0 means Sudden Death starts in 2nd round.
+       The lowest possible internal value is 0.
+       The user-facing value is different, it's defined as
+       "number of full rounds to play till Sudden Death"
+       i.e. the user-facing value 1 is equivalent to internal value 0.
+       We use SDTimeoutSpinBox for the magic to happen. */
+    SB_SuddenDeath = new SDTimeoutSpinBox(gbBasicSettings);
     SB_SuddenDeath->setWhatsThis(wtSuddenDeath);
-    SB_SuddenDeath->setRange(0, 50);
+    // Will display as 1-52
+    SB_SuddenDeath->setRange(0, 51);
+    // Will display as 16
     SB_SuddenDeath->setValue(15);
     SB_SuddenDeath->setSingleStep(3);
     glBSLayout->addWidget(SB_SuddenDeath,3,2,1,1);
--- a/QTfrontend/ui/page/pagescheme.h	Sat Sep 29 23:13:02 2018 +0200
+++ b/QTfrontend/ui/page/pagescheme.h	Sun Sep 30 01:41:12 2018 +0200
@@ -23,6 +23,7 @@
 #include "togglebutton.h"
 
 class FreqSpinBox;
+class SDTimeoutSpinBox;
 class MinesTimeSpinBox;
 
 class PageScheme : public AbstractPage
@@ -80,7 +81,7 @@
         QSpinBox * SB_DamageModifier;
         QSpinBox * SB_TurnTime;
         QSpinBox * SB_InitHealth;
-        QSpinBox * SB_SuddenDeath;
+        SDTimeoutSpinBox * SB_SuddenDeath;
         QSpinBox * SB_WaterRise;
         QSpinBox * SB_HealthDecrease;
         FreqSpinBox * SB_CaseProb;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/QTfrontend/ui/widget/SDTimeoutSpinBox.cpp	Sun Sep 30 01:41:12 2018 +0200
@@ -0,0 +1,49 @@
+/*
+ * Hedgewars, a free turn based strategy game
+ * Copyright (c) 2004-2015 Andrey Korotaev <unC0Rr@gmail.com>
+ *
+ * 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; version 2 of the License
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+/**
+ * @file
+ * @brief SDTimeoutSpinBox class implementation
+ */
+
+#include "SDTimeoutSpinBox.h"
+
+SDTimeoutSpinBox::SDTimeoutSpinBox(QWidget* parent) : QSpinBox(parent)
+{
+    // do nothing
+};
+
+
+QString SDTimeoutSpinBox::textFromValue(int internalValue) const
+{
+    // user-facing value = internal value + 1
+    return QString::number(internalValue + 1);
+}
+
+int SDTimeoutSpinBox::valueFromText(const QString & userFacingString) const
+{
+    // internal value = user-facing value - 1
+    bool ok;
+    int value = userFacingString.toInt(&ok);
+
+    if (ok)
+        return value - 1;
+    // Fallback
+    else
+        return 15;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/QTfrontend/ui/widget/SDTimeoutSpinBox.h	Sun Sep 30 01:41:12 2018 +0200
@@ -0,0 +1,65 @@
+/*
+ * Hedgewars, a free turn based strategy game
+ * Copyright (c) 2004-2015 Andrey Korotaev <unC0Rr@gmail.com>
+ *
+ * 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; version 2 of the License
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+/**
+ * @file
+ * @brief SDTimeoutSpinBox class definition
+ */
+
+#ifndef HEDGEWARS_SDTIMEOUTSPINBOX_H
+#define HEDGEWARS_SDTIMEOUTSPINBOX_H
+
+#include <QObject>
+#include <QSpinBox>
+
+/**
+ * <code>SpinBox</code> for Sudden Death timeout.
+ * The internally stored Sudden Death timeout is different
+ * from the actual number of rounds it takes until SD starts.
+ * e.g. value 0 means SD starts in 2nd round
+ * @author Wuzzy
+ * @since  0.9.25
+ */
+class SDTimeoutSpinBox : public QSpinBox
+{
+        Q_OBJECT
+
+    public:
+        /**
+         * @brief Class constructor.
+         * @param parent parent widget.
+         */
+        SDTimeoutSpinBox(QWidget * parent);
+
+    protected:
+        /**
+         * Returns its value in real number of rounds.
+         * @param internal value integer value to be represented as string.
+         * @return the real number of rounds
+         */
+        QString textFromValue(int value) const;
+        /**
+         * Returns the internally-used value for SD timeout.
+         * @param user-facing string, i.e. real number of rounds
+         * @return internally-stored SD timeout value
+         */
+        int valueFromText(const QString & text) const;
+};
+
+
+#endif // HEDGEWARS_SDTIMEOUTSPINBOX_H