QTfrontend/ui/widget/selectWeapon.cpp
changeset 12665 5664ec82aed7
parent 12663 d7492247a368
child 12689 5b135c26f320
equal deleted inserted replaced
12664:943b258d5443 12665:5664ec82aed7
   187             return;
   187             return;
   188         }
   188         }
   189     setWeapons(*cDefaultAmmoStore);
   189     setWeapons(*cDefaultAmmoStore);
   190 }
   190 }
   191 
   191 
       
   192 //Save current weapons set.
   192 void SelWeaponWidget::save()
   193 void SelWeaponWidget::save()
   193 {
   194 {
       
   195     //The save() function is called by ANY change of the combo box.
       
   196     //If an entry is deleted, this code would just re-add the deleted
       
   197     //item. We use isDeleted to check if we are currently deleting to
       
   198     //prevent this.
       
   199     if (isDeleting)
       
   200         return;
   194     // TODO make this return if success or not, so that the page can react
   201     // TODO make this return if success or not, so that the page can react
   195     // properly and not goBack if saving failed
   202     // properly and not goBack if saving failed
   196     if (m_name->text() == "") return;
   203     if (m_name->text() == "")
       
   204         return;
   197 
   205 
   198     QString state1;
   206     QString state1;
   199     QString state2;
   207     QString state2;
   200     QString state3;
   208     QString state3;
   201     QString state4;
   209     QString state4;
   238     {
   246     {
   239         // remove old entry
   247         // remove old entry
   240         wconf->remove(curWeaponsName);
   248         wconf->remove(curWeaponsName);
   241     }
   249     }
   242     wconf->setValue(m_name->text(), stateFull);
   250     wconf->setValue(m_name->text(), stateFull);
   243     emit weaponsChanged();
   251     emit weaponsEdited(curWeaponsName, m_name->text(), stateFull);
   244 }
   252 }
   245 
   253 
   246 int SelWeaponWidget::operator [] (unsigned int weaponIndex) const
   254 int SelWeaponWidget::operator [] (unsigned int weaponIndex) const
   247 {
   255 {
   248     twi::const_iterator it = weaponItems.find(weaponIndex);
   256     twi::const_iterator it = weaponItems.find(weaponIndex);
   254     return wconf->value(name).toString();
   262     return wconf->value(name).toString();
   255 }
   263 }
   256 
   264 
   257 void SelWeaponWidget::deleteWeaponsName()
   265 void SelWeaponWidget::deleteWeaponsName()
   258 {
   266 {
   259     if (curWeaponsName == "") return;
   267     QString delWeaponsName = curWeaponsName;
       
   268     if (delWeaponsName == "") return;
   260 
   269 
   261     for(int i = 0; i < cDefaultAmmos.size(); i++)
   270     for(int i = 0; i < cDefaultAmmos.size(); i++)
   262         if (!cDefaultAmmos[i].first.compare(m_name->text()))
   271         if (!cDefaultAmmos[i].first.compare(delWeaponsName))
   263         {
   272         {
   264             QMessageBox deniedMsg(this);
   273             QMessageBox deniedMsg(this);
   265             deniedMsg.setIcon(QMessageBox::Warning);
   274             deniedMsg.setIcon(QMessageBox::Warning);
   266             deniedMsg.setWindowTitle(QMessageBox::tr("Weapons - Warning"));
   275             deniedMsg.setWindowTitle(QMessageBox::tr("Weapons - Warning"));
   267             deniedMsg.setText(QMessageBox::tr("Cannot delete default weapon set '%1'!").arg(cDefaultAmmos[i].first));
   276             deniedMsg.setText(QMessageBox::tr("Cannot delete default weapon set '%1'!").arg(cDefaultAmmos[i].first));
   271         }
   280         }
   272 
   281 
   273     QMessageBox reallyDeleteMsg(this);
   282     QMessageBox reallyDeleteMsg(this);
   274     reallyDeleteMsg.setIcon(QMessageBox::Question);
   283     reallyDeleteMsg.setIcon(QMessageBox::Question);
   275     reallyDeleteMsg.setWindowTitle(QMessageBox::tr("Weapons - Are you sure?"));
   284     reallyDeleteMsg.setWindowTitle(QMessageBox::tr("Weapons - Are you sure?"));
   276     reallyDeleteMsg.setText(QMessageBox::tr("Do you really want to delete the weapon set '%1'?").arg(curWeaponsName));
   285     reallyDeleteMsg.setText(QMessageBox::tr("Do you really want to delete the weapon set '%1'?").arg(delWeaponsName));
   277     reallyDeleteMsg.setWindowModality(Qt::WindowModal);
   286     reallyDeleteMsg.setWindowModality(Qt::WindowModal);
   278     reallyDeleteMsg.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
   287     reallyDeleteMsg.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
   279 
   288 
   280     if (reallyDeleteMsg.exec() == QMessageBox::Ok)
   289     if (reallyDeleteMsg.exec() == QMessageBox::Ok)
   281     {
   290     {
   282         wconf->remove(curWeaponsName);
   291         isDeleting = true;
   283         emit weaponsDeleted();
   292         wconf->remove(delWeaponsName);
       
   293         emit weaponsDeleted(delWeaponsName);
   284     }
   294     }
   285 }
   295 }
   286 
   296 
   287 void SelWeaponWidget::newWeaponsName()
   297 void SelWeaponWidget::newWeaponsName()
   288 {
   298 {
       
   299     save();
   289     QString newName = tr("New");
   300     QString newName = tr("New");
   290     if(wconf->contains(newName))
   301     if(wconf->contains(newName))
   291     {
   302     {
   292         //name already used -> look for an appropriate name:
   303         //name already used -> look for an appropriate name:
   293         int i=2;
   304         int i=2;
   294         while(wconf->contains(newName = tr("New (%1)").arg(i++))) ;
   305         while(wconf->contains(newName = tr("New (%1)").arg(i++))) ;
   295     }
   306     }
   296     setWeaponsName(newName);
   307     setWeaponsName(newName);
       
   308     wconf->setValue(newName, *cEmptyAmmoStore);
       
   309     emit weaponsAdded(newName, *cEmptyAmmoStore);
   297 }
   310 }
   298 
   311 
   299 void SelWeaponWidget::setWeaponsName(const QString& name)
   312 void SelWeaponWidget::setWeaponsName(const QString& name)
   300 {
   313 {
   301     m_name->setText(name);
   314     m_name->setText(name);
   310     {
   323     {
   311         setWeapons(*cEmptyAmmoStore);
   324         setWeapons(*cEmptyAmmoStore);
   312     }
   325     }
   313 }
   326 }
   314 
   327 
       
   328 void SelWeaponWidget::switchWeapons(const QString& name)
       
   329 {
       
   330     // Rescue old weapons set, then select new one
       
   331     save();
       
   332     setWeaponsName(name);
       
   333 }
       
   334 
   315 QStringList SelWeaponWidget::getWeaponNames() const
   335 QStringList SelWeaponWidget::getWeaponNames() const
   316 {
   336 {
   317     return wconf->allKeys();
   337     return wconf->allKeys();
   318 }
   338 }
   319 
   339 
   320 void SelWeaponWidget::copy()
   340 void SelWeaponWidget::copy()
   321 {
   341 {
       
   342     save();
   322     if(wconf->contains(curWeaponsName))
   343     if(wconf->contains(curWeaponsName))
   323     {
   344     {
   324         QString ammo = getWeaponsString(curWeaponsName);
   345         QString ammo = getWeaponsString(curWeaponsName);
   325         QString newName = tr("Copy of %1").arg(curWeaponsName);
   346         QString newName = tr("Copy of %1").arg(curWeaponsName);
   326         if(wconf->contains(newName))
   347         if(wconf->contains(newName))
   329             int i=2;
   350             int i=2;
   330             while(wconf->contains(newName = tr("Copy of %1 (%2)").arg(curWeaponsName).arg(i++)));
   351             while(wconf->contains(newName = tr("Copy of %1 (%2)").arg(curWeaponsName).arg(i++)));
   331         }
   352         }
   332         setWeaponsName(newName);
   353         setWeaponsName(newName);
   333         setWeapons(ammo);
   354         setWeapons(ammo);
       
   355         wconf->setValue(newName, ammo);
       
   356         emit weaponsAdded(newName, ammo);
   334     }
   357     }
   335 }
   358 }
   336 
   359 
   337 QString SelWeaponWidget::fixWeaponSet(const QString &s)
   360 QString SelWeaponWidget::fixWeaponSet(const QString &s)
   338 {
   361 {
   350     for(int i = sl.length() - 1; i >= 0; --i)
   373     for(int i = sl.length() - 1; i >= 0; --i)
   351         sl[i] = sl[i].leftJustified(neededLength, '0', true);
   374         sl[i] = sl[i].leftJustified(neededLength, '0', true);
   352 
   375 
   353     return sl.join(QString());
   376     return sl.join(QString());
   354 }
   377 }
       
   378 
       
   379 void SelWeaponWidget::deletionDone()
       
   380 {
       
   381     isDeleting = false;
       
   382 }