QTfrontend/ui/widget/selectWeapon.cpp
changeset 12923 305f3667af20
parent 12922 c0320d66310d
child 13195 7d9462d49d4c
equal deleted inserted replaced
12922:c0320d66310d 12923:305f3667af20
    83 
    83 
    84 SelWeaponWidget::SelWeaponWidget(int numItems, QWidget* parent) :
    84 SelWeaponWidget::SelWeaponWidget(int numItems, QWidget* parent) :
    85     QFrame(parent),
    85     QFrame(parent),
    86     m_numItems(numItems)
    86     m_numItems(numItems)
    87 {
    87 {
    88     if (!QDir(cfgdir->absolutePath() + "/Weapon Settings").exists()) {
    88     if (!QDir(cfgdir->absolutePath() + "/Schemes").exists()) QDir().mkdir(cfgdir->absolutePath() + "/Schemes");
    89         QDir().mkdir(cfgdir->absolutePath() + "/Weapon Settings");
    89     if (!QDir(cfgdir->absolutePath() + "/Schemes/Ammo").exists()) {
    90         wconf = new QSettings(cfgdir->absolutePath() + "/weapons.ini", QSettings::IniFormat, this);
    90         QDir().mkdir(cfgdir->absolutePath() + "/Schemes/Ammo");
    91 
    91         wconf = new QSettings("Hedgewars", "Hedgewars");
    92         for(int i = 0; i < cDefaultAmmos.size(); ++i)
    92         wconf->clear();
       
    93         QSettings old_wconf(cfgdir->absolutePath() + "/weapons.ini", QSettings::IniFormat);
       
    94 
       
    95         QList<QVariant> defaultAmmos;
       
    96         for(int i = 0; i < cDefaultAmmos.size(); ++i) {
    93             wconf->setValue(cDefaultAmmos[i].first, cDefaultAmmos[i].second);
    97             wconf->setValue(cDefaultAmmos[i].first, cDefaultAmmos[i].second);
    94 
    98             defaultAmmos.append(cDefaultAmmos[i].first);
    95         QStringList keys = wconf->allKeys();
    99         }
       
   100 
       
   101         QStringList keys = old_wconf.allKeys();
    96         for(int i = 0; i < keys.size(); i++)
   102         for(int i = 0; i < keys.size(); i++)
    97         {
   103         {
    98             if (wconf->value(keys[i]).toString().size() != cDefaultAmmoStore->size())
   104             wconf->setValue(keys[i], fixWeaponSet(old_wconf.value(keys[i]).toString()));
    99                 wconf->setValue(keys[i], fixWeaponSet(wconf->value(keys[i]).toString()));
   105 
   100 
   106             if (!defaultAmmos.contains(keys[i])) {
   101             QFile file(cfgdir->absolutePath() + "/Weapon Settings/" + keys[i] + ".hww");
   107                 QFile file(cfgdir->absolutePath() + "/Schemes/Ammo/" + keys[i] + ".hwa");
   102             if (file.open(QIODevice::WriteOnly)) {
   108                 if (file.open(QIODevice::WriteOnly)) {
   103                 QTextStream stream( &file );
   109                     QTextStream stream( &file );
   104                 stream << wconf->value(keys[i]).toString() << endl;
   110                     stream << old_wconf.value(keys[i]).toString() << endl;
       
   111                 }
   105             }
   112             }
   106         }
   113         }
   107     } else {
   114     } else {
   108         wconf = new QSettings("Hedgewars", "Hedgewars");
   115         wconf = new QSettings("Hedgewars", "Hedgewars");
   109         wconf->clear();
   116         wconf->clear();
   110 
   117 
   111         QStringList schemes = QDir(cfgdir->absolutePath() + "/Weapon Settings").entryList();
   118         for(int i = 0; i < cDefaultAmmos.size(); ++i)
       
   119             wconf->setValue(cDefaultAmmos[i].first, cDefaultAmmos[i].second);
       
   120 
       
   121         QStringList schemes = QDir(cfgdir->absolutePath() + "/Schemes/Ammo").entryList();
   112 
   122 
   113         for(int i = 0; i < schemes.size(); i++)
   123         for(int i = 0; i < schemes.size(); i++)
   114         {
   124         {
   115             if (schemes[i] == "." || schemes[i] == "..") continue;
   125             if (schemes[i] == "." || schemes[i] == "..") continue;
   116 
   126 
   117             QFile file(cfgdir->absolutePath() + "/Weapon Settings/" + schemes[i]);
   127             QFile file(cfgdir->absolutePath() + "/Schemes/Ammo/" + schemes[i]);
   118             QString config;
   128             QString config;
   119 
   129 
   120             if (file.open(QIODevice::ReadOnly)) {
   130             if (file.open(QIODevice::ReadOnly)) {
   121                 QTextStream stream( &file );
   131                 QTextStream stream( &file );
   122                 stream >> config;
   132                 stream >> config;
   123             }
   133             }
   124 
   134 
   125             wconf->setValue(schemes[i].remove(".hww"), fixWeaponSet(config));
   135             wconf->setValue(schemes[i].remove(".hwa"), fixWeaponSet(config));
   126         }
   136         }
   127     }
   137     }
   128 
   138 
   129     QString currentState = *cDefaultAmmoStore;
   139     QString currentState = *cDefaultAmmoStore;
   130 
   140 
   279     {
   289     {
   280         // remove old entry
   290         // remove old entry
   281         wconf->remove(curWeaponsName);
   291         wconf->remove(curWeaponsName);
   282     }
   292     }
   283     wconf->setValue(m_name->text(), stateFull);
   293     wconf->setValue(m_name->text(), stateFull);
   284     QFile file(cfgdir->absolutePath() + "/Weapon Settings/" + m_name->text()+ ".hww");
   294     QFile file(cfgdir->absolutePath() + "/Schemes/Ammo/" + m_name->text()+ ".hwa");
   285     if (file.open(QIODevice::WriteOnly)) {
   295     if (file.open(QIODevice::WriteOnly)) {
   286         QTextStream stream( &file );
   296         QTextStream stream( &file );
   287         stream << stateFull << endl;
   297         stream << stateFull << endl;
   288     }
   298     }
   289     emit weaponsEdited(curWeaponsName, m_name->text(), stateFull);
   299     emit weaponsEdited(curWeaponsName, m_name->text(), stateFull);
   326 
   336 
   327     if (reallyDeleteMsg.exec() == QMessageBox::Ok)
   337     if (reallyDeleteMsg.exec() == QMessageBox::Ok)
   328     {
   338     {
   329         isDeleting = true;
   339         isDeleting = true;
   330         wconf->remove(delWeaponsName);
   340         wconf->remove(delWeaponsName);
   331         QFile(cfgdir->absolutePath() + "/Weapon Settings/" + curWeaponsName + ".hww").remove();
   341         QFile(cfgdir->absolutePath() + "/Schemes/Ammo/" + curWeaponsName + ".hwa").remove();
   332         emit weaponsDeleted(delWeaponsName);
   342         emit weaponsDeleted(delWeaponsName);
   333     }
   343     }
   334 }
   344 }
   335 
   345 
   336 void SelWeaponWidget::newWeaponsName()
   346 void SelWeaponWidget::newWeaponsName()