Prevent saving weapon scheme if name was already taken 0.9.24
authorWuzzy <Wuzzy2@mail.ru>
Thu, 26 Jul 2018 14:18:23 +0200
branch0.9.24
changeset 13562 ea05d4b0b6b0
parent 13561 0c8deb338990
child 13568 5a1e4796e431
Prevent saving weapon scheme if name was already taken This is case-insensitive.
ChangeLog.txt
QTfrontend/ui/widget/selectWeapon.cpp
--- a/ChangeLog.txt	Thu Jul 26 14:18:23 2018 +0200
+++ b/ChangeLog.txt	Thu Jul 26 14:18:23 2018 +0200
@@ -5,6 +5,7 @@
  * Fix crash when starting game with 2 controllers or more
  * Fix insane amount of droplets appearing when shooting minigun into ocean world edge
  * Limit number of droplets to 50 (temporary bugfix)
+ * Prevent creation of schemes with same name as an existing scheme
  * Fix teleport tooltip claiming it doesn't end turn in hog placing phase with inf. attack
  + Add default controls for controllers (see README)
 
--- a/QTfrontend/ui/widget/selectWeapon.cpp	Thu Jul 26 14:18:23 2018 +0200
+++ b/QTfrontend/ui/widget/selectWeapon.cpp	Thu Jul 26 14:18:23 2018 +0200
@@ -251,8 +251,6 @@
     //prevent this.
     if (isDeleting)
         return;
-    // TODO make this return if success or not, so that the page can react
-    // properly and not goBack if saving failed
     if (m_name->text() == "")
         return;
 
@@ -277,21 +275,24 @@
 
     stateFull = state1 + state2 + state3 + state4;
 
-    for(int i = 0; i < cDefaultAmmos.size(); i++)
+    // Check for duplicates
+    QString inputNameLower = m_name->text().toLower();
+    QString curWeaponsNameLower = curWeaponsName.toLower();
+    QStringList keys = wconf->keys();
+    for(int i = 0; i < keys.size(); i++)
     {
-        // Don't allow same name as default weapon set, even case-insensitively.
+        QString compName = keys[i];
+        QString compNameLower = compName.toLower();
+        // Don't allow same name as other weapon set, even case-insensitively.
         // This prevents some problems with saving/loading.
-        if (cDefaultAmmos[i].first.toLower().compare(m_name->text().toLower()) == 0)
+        if ((compNameLower == inputNameLower) && (compNameLower != curWeaponsNameLower))
         {
-            // don't show warning if no change
-            if (cDefaultAmmos[i].second.compare(stateFull) == 0)
-                return;
-
+            // Discard changed made to current weapon scheme if there's a duplicate
             m_name->setText(curWeaponsName);
             QMessageBox deniedMsg(this);
             deniedMsg.setIcon(QMessageBox::Warning);
             deniedMsg.setWindowTitle(QMessageBox::tr("Weapons - Warning"));
-            deniedMsg.setText(QMessageBox::tr("Cannot overwrite default weapon set '%1'!").arg(cDefaultAmmos[i].first));
+            deniedMsg.setText(QMessageBox::tr("A weapon scheme with the name '%1' already exists. Changes made to the weapon scheme have been discarded.").arg(compName));
             deniedMsg.setWindowModality(Qt::WindowModal);
             deniedMsg.exec();
             return;