Rework weapon scheme handling in frontend and fix a couple of bugs
authorWuzzy <almikes@aol.com>
Sat, 07 Oct 2017 03:43:06 +0200
changeset 12670 5664ec82aed7
parent 12669 943b258d5443
child 12671 1d25f5555c38
Rework weapon scheme handling in frontend and fix a couple of bugs There were many oddities in weapon scheme editing which are fixed now: * When you left the weapon scheme editor, everything flashed briefly * When you leaft the weapon scheme editr, the weapon scheme selection always reverted to Default. Very annoying * Clicking on New or Copy now instantly creates and save the new weapons set
ChangeLog.txt
QTfrontend/hwform.cpp
QTfrontend/hwform.h
QTfrontend/ui/page/pageselectweapon.cpp
QTfrontend/ui/widget/selectWeapon.cpp
QTfrontend/ui/widget/selectWeapon.h
--- a/ChangeLog.txt	Sat Oct 07 01:05:55 2017 +0200
+++ b/ChangeLog.txt	Sat Oct 07 03:43:06 2017 +0200
@@ -276,6 +276,7 @@
  * Fix the key for toggling team bars being incorrectly described
  * Fix caption of stats screen showing only one winner if multiple teams have won
  * Remove broken “Play again” button appearing in stats page after an online game
+ * Weapons scheme editor: When leaving, it no longer flickers and the selection is not reset to Default
 
 Content Creation:
  + Theme objects can now have more than 1 in-land rect specified. You can specify the amount in theme.cfg by adding another number (and ,) before the first rect
--- a/QTfrontend/hwform.cpp	Sat Oct 07 01:05:55 2017 +0200
+++ b/QTfrontend/hwform.cpp	Sat Oct 07 03:43:06 2017 +0200
@@ -265,7 +265,7 @@
             ui.pageMultiplayer->BtnStartMPGame, SLOT(setEnabled(bool)));
     connect(ui.pageMultiplayer, SIGNAL(SetupClicked()), this, SLOT(IntermediateSetup()));
     connect(ui.pageMultiplayer->gameCFG, SIGNAL(goToSchemes(int)), this, SLOT(GoToScheme(int)));
-    connect(ui.pageMultiplayer->gameCFG, SIGNAL(goToWeapons(int)), this, SLOT(GoToSelectWeaponSet(int)));
+    connect(ui.pageMultiplayer->gameCFG, SIGNAL(goToWeapons(int)), this, SLOT(GoToWeapons(int)));
     connect(ui.pageMultiplayer->gameCFG, SIGNAL(goToDrawMap()), pageSwitchMapper, SLOT(map()));
     pageSwitchMapper->setMapping(ui.pageMultiplayer->gameCFG, ID_PAGE_DRAWMAP);
 
@@ -279,14 +279,13 @@
     connect(ui.pageOptions, SIGNAL(goBack()), config, SLOT(SaveOptions()));
     connect(ui.pageOptions->BtnAssociateFiles, SIGNAL(clicked()), this, SLOT(AssociateFiles()));
 
-    connect(ui.pageOptions->WeaponEdit, SIGNAL(clicked()), this, SLOT(GoToSelectWeapon()));
-    connect(ui.pageOptions->WeaponNew, SIGNAL(clicked()), this, SLOT(GoToSelectNewWeapon()));
+    connect(ui.pageOptions->WeaponEdit, SIGNAL(clicked()), this, SLOT(GoToEditWeapons()));
+    connect(ui.pageOptions->WeaponNew, SIGNAL(clicked()), this, SLOT(GoToNewWeapons()));
     connect(ui.pageOptions->WeaponDelete, SIGNAL(clicked()), this, SLOT(DeleteWeaponSet()));
     connect(ui.pageOptions->SchemeEdit, SIGNAL(clicked()), this, SLOT(GoToEditScheme()));
     connect(ui.pageOptions->SchemeNew, SIGNAL(clicked()), this, SLOT(GoToNewScheme()));
     connect(ui.pageOptions->SchemeDelete, SIGNAL(clicked()), this, SLOT(DeleteScheme()));
     connect(ui.pageOptions->CBFrontendEffects, SIGNAL(toggled(bool)), this, SLOT(onFrontendEffects(bool)) );
-    connect(ui.pageSelectWeapon->pWeapons, SIGNAL(weaponsChanged()), this, SLOT(UpdateWeapons()));
 
     connect(ui.pageNet->BtnSpecifyServer, SIGNAL(clicked()), this, SLOT(NetConnect()));
     connect(ui.pageNet->BtnNetSvrStart, SIGNAL(clicked()), pageSwitchMapper, SLOT(map()));
@@ -300,7 +299,7 @@
             ui.pageNetGame->BtnStart, SLOT(setEnabled(bool)));
     connect(ui.pageNetGame, SIGNAL(SetupClicked()), this, SLOT(IntermediateSetup()));
     connect(ui.pageNetGame->pGameCFG, SIGNAL(goToSchemes(int)), this, SLOT(GoToScheme(int)));
-    connect(ui.pageNetGame->pGameCFG, SIGNAL(goToWeapons(int)), this, SLOT(GoToSelectWeaponSet(int)));
+    connect(ui.pageNetGame->pGameCFG, SIGNAL(goToWeapons(int)), this, SLOT(GoToWeapons(int)));
     connect(ui.pageNetGame->pGameCFG, SIGNAL(goToDrawMap()), pageSwitchMapper, SLOT(map()));
     pageSwitchMapper->setMapping(ui.pageNetGame->pGameCFG, ID_PAGE_DRAWMAP);
 
@@ -334,13 +333,12 @@
     connect(ui.pageCampaign->CBCampaign, SIGNAL(currentIndexChanged(int)), this, SLOT(UpdateCampaignPage(int)));
     connect(ui.pageCampaign->CBMission, SIGNAL(currentIndexChanged(int)), this, SLOT(UpdateCampaignPageMission(int)));
 
-    connect(ui.pageSelectWeapon->BtnDelete, SIGNAL(clicked()),
-            ui.pageSelectWeapon->pWeapons, SLOT(deleteWeaponsName())); // executed first
-    connect(ui.pageSelectWeapon->pWeapons, SIGNAL(weaponsDeleted()),
-            this, SLOT(UpdateWeapons())); // executed second
-    //connect(ui.pageSelectWeapon->pWeapons, SIGNAL(weaponsDeleted()),
-    //    this, SLOT(GoBack())); // executed third
-
+    connect(ui.pageSelectWeapon->pWeapons, SIGNAL(weaponsDeleted(QString)),
+             this, SLOT(DeleteWeapons(QString)));
+    connect(ui.pageSelectWeapon->pWeapons, SIGNAL(weaponsAdded(QString, QString)),
+             this, SLOT(AddWeapons(QString, QString)));
+    connect(ui.pageSelectWeapon->pWeapons, SIGNAL(weaponsEdited(QString, QString, QString)),
+             this, SLOT(EditWeapons(QString, QString, QString)));
 
     connect(ui.pageMain->BtnNetLocal, SIGNAL(clicked()), this, SLOT(GoToNet()));
     connect(ui.pageMain->BtnNetOfficial, SIGNAL(clicked()), this, SLOT(NetConnectOfficialServer()));
@@ -453,6 +451,62 @@
     }
 }
 
+void HWForm::AddWeapons(QString weaponsName, QString ammo)
+{
+    QVector<QComboBox*> combos;
+    combos.push_back(ui.pageOptions->WeaponsName);
+    combos.push_back(ui.pageMultiplayer->gameCFG->WeaponsName);
+    combos.push_back(ui.pageNetGame->pGameCFG->WeaponsName);
+    combos.push_back(ui.pageSelectWeapon->selectWeaponSet);
+
+    QStringList names = ui.pageSelectWeapon->pWeapons->getWeaponNames();
+
+    for(QVector<QComboBox*>::iterator it = combos.begin(); it != combos.end(); ++it)
+    {
+        (*it)->addItem(weaponsName, QVariant(ammo));
+    }
+    ui.pageSelectWeapon->selectWeaponSet->setCurrentIndex(ui.pageSelectWeapon->selectWeaponSet->count()-1);
+}
+
+void HWForm::DeleteWeapons(QString weaponsName)
+{
+    QVector<QComboBox*> combos;
+    combos.push_back(ui.pageOptions->WeaponsName);
+    combos.push_back(ui.pageMultiplayer->gameCFG->WeaponsName);
+    combos.push_back(ui.pageNetGame->pGameCFG->WeaponsName);
+    combos.push_back(ui.pageSelectWeapon->selectWeaponSet);
+
+    QStringList names = ui.pageSelectWeapon->pWeapons->getWeaponNames();
+
+    for(QVector<QComboBox*>::iterator it = combos.begin(); it != combos.end(); ++it)
+    {
+        int pos = (*it)->findText(weaponsName);
+        if (pos != -1)
+        {
+            (*it)->removeItem(pos);
+        }
+    }
+    ui.pageSelectWeapon->pWeapons->deletionDone();
+}
+
+void HWForm::EditWeapons(QString oldWeaponsName, QString newWeaponsName, QString ammo)
+{
+    QVector<QComboBox*> combos;
+    combos.push_back(ui.pageOptions->WeaponsName);
+    combos.push_back(ui.pageMultiplayer->gameCFG->WeaponsName);
+    combos.push_back(ui.pageNetGame->pGameCFG->WeaponsName);
+    combos.push_back(ui.pageSelectWeapon->selectWeaponSet);
+
+    QStringList names = ui.pageSelectWeapon->pWeapons->getWeaponNames();
+
+    for(QVector<QComboBox*>::iterator it = combos.begin(); it != combos.end(); ++it)
+    {
+        int pos = (*it)->findText(oldWeaponsName);
+        (*it)->setItemText(pos, newWeaponsName);
+        (*it)->setItemData(pos, ammo);
+    }
+}
+
 void HWForm::UpdateTeamsLists()
 {
     QStringList teamslist = config->GetTeamsList();
@@ -527,24 +581,25 @@
     }
 }
 
-void HWForm::GoToSelectNewWeapon()
+void HWForm::GoToNewWeapons()
 {
     ui.pageSelectWeapon->pWeapons->newWeaponsName();
     GoToPage(ID_PAGE_SELECTWEAPON);
 }
 
-void HWForm::GoToSelectWeapon()
+void HWForm::GoToEditWeapons()
 {
     ui.pageSelectWeapon->selectWeaponSet->setCurrentIndex(ui.pageOptions->WeaponsName->currentIndex());
     GoToPage(ID_PAGE_SELECTWEAPON);
 }
 
-void HWForm::GoToSelectWeaponSet(int index)
+void HWForm::GoToWeapons(int index)
 {
     ui.pageSelectWeapon->selectWeaponSet->setCurrentIndex(index);
     GoToPage(ID_PAGE_SELECTWEAPON);
 }
 
+
 void HWForm::GoToSaves()
 {
     ui.pagePlayDemo->FillFromDir(PagePlayDemo::RT_Save);
--- a/QTfrontend/hwform.h	Sat Oct 07 01:05:55 2017 +0200
+++ b/QTfrontend/hwform.h	Sat Oct 07 03:43:06 2017 +0200
@@ -74,9 +74,9 @@
         void GoToSaves();
         void GoToDemos();
         void GoToNet();
-        void GoToSelectWeapon();
-        void GoToSelectWeaponSet(int index);
-        void GoToSelectNewWeapon();
+        void GoToEditWeapons();
+        void GoToNewWeapons();
+        void GoToWeapons(int index);
         void GoToScheme(int index);
         void GoToEditScheme();
         void GoToNewScheme();
@@ -123,6 +123,9 @@
         void GetRecord(RecordType type, const QByteArray & record);
         void CreateNetGame();
         void UpdateWeapons();
+        void DeleteWeapons(QString weaponsName);
+        void AddWeapons(QString weaponsName, QString ammo);
+        void EditWeapons(QString oldWeaponsName, QString newWeaponsName, QString ammo);
         void onFrontendFullscreen(bool value);
         void onFrontendEffects(bool value);
         void Music(bool checked);
--- a/QTfrontend/ui/page/pageselectweapon.cpp	Sat Oct 07 01:05:55 2017 +0200
+++ b/QTfrontend/ui/page/pageselectweapon.cpp	Sat Oct 07 03:43:06 2017 +0200
@@ -59,11 +59,12 @@
 
 void PageSelectWeapon::connectSignals()
 {
+    connect(selectWeaponSet, SIGNAL(currentIndexChanged(const QString&)), pWeapons, SLOT(switchWeapons(const QString&)));
     connect(BtnDefault, SIGNAL(clicked()), pWeapons, SLOT(setDefault()));
     connect(this, SIGNAL(goBack()), pWeapons, SLOT(save()));
     connect(BtnNew, SIGNAL(clicked()), pWeapons, SLOT(newWeaponsName()));
     connect(BtnCopy, SIGNAL(clicked()), pWeapons, SLOT(copy()));
-    connect(selectWeaponSet, SIGNAL(currentIndexChanged(const QString&)), pWeapons, SLOT(setWeaponsName(const QString&)));
+    connect(BtnDelete, SIGNAL(clicked()), pWeapons, SLOT(deleteWeaponsName()));
 }
 
 PageSelectWeapon::PageSelectWeapon(QWidget* parent) :  AbstractPage(parent)
--- a/QTfrontend/ui/widget/selectWeapon.cpp	Sat Oct 07 01:05:55 2017 +0200
+++ b/QTfrontend/ui/widget/selectWeapon.cpp	Sat Oct 07 03:43:06 2017 +0200
@@ -189,11 +189,19 @@
     setWeapons(*cDefaultAmmoStore);
 }
 
+//Save current weapons set.
 void SelWeaponWidget::save()
 {
+    //The save() function is called by ANY change of the combo box.
+    //If an entry is deleted, this code would just re-add the deleted
+    //item. We use isDeleted to check if we are currently deleting to
+    //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;
+    if (m_name->text() == "")
+        return;
 
     QString state1;
     QString state2;
@@ -240,7 +248,7 @@
         wconf->remove(curWeaponsName);
     }
     wconf->setValue(m_name->text(), stateFull);
-    emit weaponsChanged();
+    emit weaponsEdited(curWeaponsName, m_name->text(), stateFull);
 }
 
 int SelWeaponWidget::operator [] (unsigned int weaponIndex) const
@@ -256,10 +264,11 @@
 
 void SelWeaponWidget::deleteWeaponsName()
 {
-    if (curWeaponsName == "") return;
+    QString delWeaponsName = curWeaponsName;
+    if (delWeaponsName == "") return;
 
     for(int i = 0; i < cDefaultAmmos.size(); i++)
-        if (!cDefaultAmmos[i].first.compare(m_name->text()))
+        if (!cDefaultAmmos[i].first.compare(delWeaponsName))
         {
             QMessageBox deniedMsg(this);
             deniedMsg.setIcon(QMessageBox::Warning);
@@ -273,19 +282,21 @@
     QMessageBox reallyDeleteMsg(this);
     reallyDeleteMsg.setIcon(QMessageBox::Question);
     reallyDeleteMsg.setWindowTitle(QMessageBox::tr("Weapons - Are you sure?"));
-    reallyDeleteMsg.setText(QMessageBox::tr("Do you really want to delete the weapon set '%1'?").arg(curWeaponsName));
+    reallyDeleteMsg.setText(QMessageBox::tr("Do you really want to delete the weapon set '%1'?").arg(delWeaponsName));
     reallyDeleteMsg.setWindowModality(Qt::WindowModal);
     reallyDeleteMsg.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
 
     if (reallyDeleteMsg.exec() == QMessageBox::Ok)
     {
-        wconf->remove(curWeaponsName);
-        emit weaponsDeleted();
+        isDeleting = true;
+        wconf->remove(delWeaponsName);
+        emit weaponsDeleted(delWeaponsName);
     }
 }
 
 void SelWeaponWidget::newWeaponsName()
 {
+    save();
     QString newName = tr("New");
     if(wconf->contains(newName))
     {
@@ -294,6 +305,8 @@
         while(wconf->contains(newName = tr("New (%1)").arg(i++))) ;
     }
     setWeaponsName(newName);
+    wconf->setValue(newName, *cEmptyAmmoStore);
+    emit weaponsAdded(newName, *cEmptyAmmoStore);
 }
 
 void SelWeaponWidget::setWeaponsName(const QString& name)
@@ -312,6 +325,13 @@
     }
 }
 
+void SelWeaponWidget::switchWeapons(const QString& name)
+{
+    // Rescue old weapons set, then select new one
+    save();
+    setWeaponsName(name);
+}
+
 QStringList SelWeaponWidget::getWeaponNames() const
 {
     return wconf->allKeys();
@@ -319,6 +339,7 @@
 
 void SelWeaponWidget::copy()
 {
+    save();
     if(wconf->contains(curWeaponsName))
     {
         QString ammo = getWeaponsString(curWeaponsName);
@@ -331,6 +352,8 @@
         }
         setWeaponsName(newName);
         setWeapons(ammo);
+        wconf->setValue(newName, ammo);
+        emit weaponsAdded(newName, ammo);
     }
 }
 
@@ -352,3 +375,8 @@
 
     return sl.join(QString());
 }
+
+void SelWeaponWidget::deletionDone()
+{
+    isDeleting = false;
+}
--- a/QTfrontend/ui/widget/selectWeapon.h	Sat Oct 07 01:05:55 2017 +0200
+++ b/QTfrontend/ui/widget/selectWeapon.h	Sat Oct 07 03:43:06 2017 +0200
@@ -52,24 +52,29 @@
         SelWeaponWidget(int numItems, QWidget* parent=0);
         QString getWeaponsString(const QString& name) const;
         QStringList getWeaponNames() const;
+        void deletionDone();
 
     public slots:
         void setDefault();
         void setWeapons(const QString& ammo);
         //sets the name of the current set
         void setWeaponsName(const QString& name);
+        void switchWeapons(const QString& name);
         void deleteWeaponsName();
         void newWeaponsName();
         void save();
         void copy();
 
     signals:
-        void weaponsChanged();
-        void weaponsDeleted();
+        void weaponsDeleted(QString weaponsName);
+        void weaponsAdded(QString weaponsName, QString ammo);
+        void weaponsEdited(QString oldWeaponsName, QString newWeaponsName, QString ammo);
 
     private:
         //the name of the current weapon set
         QString curWeaponsName;
+        //set to true while an entry is deleted. Used to avoid duplicate saving due to combobox change
+        bool isDeleting = false;
 
         QLineEdit* m_name;