QTfrontend/selectWeapon.cpp
changeset 694 436045756181
parent 683 57d624f71e65
child 695 34a93d123712
equal deleted inserted replaced
693:32a546d1eb3e 694:436045756181
    23 #include <QPushButton>
    23 #include <QPushButton>
    24 #include <QGridLayout>
    24 #include <QGridLayout>
    25 #include <QHBoxLayout>
    25 #include <QHBoxLayout>
    26 #include <QLabel>
    26 #include <QLabel>
    27 #include <QBitmap>
    27 #include <QBitmap>
       
    28 #include <QLineEdit>
       
    29 #include <QSettings>
    28 
    30 
    29 QImage getAmmoImage(int num)
    31 QImage getAmmoImage(int num)
    30 {
    32 {
    31   static QImage ammo(":Ammos.png");
    33   static QImage ammo(":Ammos.png");
    32   return ammo.copy(0, num*32, 32, 32);
    34   return ammo.copy(0, num*32, 32, 32);
    68 
    70 
    69 SelWeaponWidget::SelWeaponWidget(int numItems, QWidget* parent) :
    71 SelWeaponWidget::SelWeaponWidget(int numItems, QWidget* parent) :
    70   m_numItems(numItems),
    72   m_numItems(numItems),
    71   QWidget(parent)
    73   QWidget(parent)
    72 {
    74 {
       
    75   wconf = new QSettings(cfgdir->absolutePath() + "/weapons.ini", QSettings::IniFormat, this);
       
    76   if (wconf->allKeys().empty()) {
       
    77     wconf->setValue("Default", cDefaultAmmoStore->mid(10));
       
    78   }
       
    79 
    73   currentState=cDefaultAmmoStore->mid(10);
    80   currentState=cDefaultAmmoStore->mid(10);
    74 
    81 
    75   pLayout=new QGridLayout(this);
    82   pLayout=new QGridLayout(this);
    76   pLayout->setSpacing(1);
    83   pLayout->setSpacing(1);
    77   pLayout->setMargin(1);
    84   pLayout->setMargin(1);
    78 
    85 
    79   int j=-1;
    86   int j=-1;
    80   for(int i=0, k=0; i<m_numItems; ++i) {
    87   int i=0, k=0;
       
    88   for(; i<m_numItems; ++i) {
    81     if(i==6) continue;
    89     if(i==6) continue;
    82     if (k%4==0) ++j;
    90     if (k%4==0) ++j;
    83     weaponItems[i]=new SelWeaponItem(i, currentState[i].digitValue(), this);
    91     weaponItems[i]=new SelWeaponItem(i, currentState[i].digitValue(), this);
    84     pLayout->addWidget(weaponItems[i], j, k%4);
    92     pLayout->addWidget(weaponItems[i], j, k%4);
    85     ++k;
    93     ++k;
    86   }
    94   }
       
    95 
       
    96   m_name = new QLineEdit(this);
       
    97   pLayout->addWidget(m_name, i, 0, 1, 4);
    87 }
    98 }
    88 
    99 
    89 void SelWeaponWidget::setWeapons(QString ammo)
   100 void SelWeaponWidget::setWeapons(const QString& ammo)
    90 {
   101 {
    91   for(int i=0; i<m_numItems; ++i) {
   102   for(int i=0; i<m_numItems; ++i) {
    92     twi::iterator it=weaponItems.find(i);
   103     twi::iterator it=weaponItems.find(i);
    93     if (it==weaponItems.end()) continue;
   104     if (it==weaponItems.end()) continue;
    94     it->second->setItemsNum(ammo[i].digitValue());
   105     it->second->setItemsNum(ammo[i].digitValue());
   101   setWeapons(cDefaultAmmoStore->mid(10));
   112   setWeapons(cDefaultAmmoStore->mid(10));
   102 }
   113 }
   103 
   114 
   104 void SelWeaponWidget::save()
   115 void SelWeaponWidget::save()
   105 {
   116 {
       
   117   if (m_name->text()=="") return;
   106   currentState="";
   118   currentState="";
   107   for(int i=0; i<m_numItems; ++i) {
   119   for(int i=0; i<m_numItems; ++i) {
   108     twi::const_iterator it=weaponItems.find(i);
   120     twi::const_iterator it=weaponItems.find(i);
   109     int num = it==weaponItems.end() ? 9 : (*this)[i];
   121     int num = it==weaponItems.end() ? 9 : (*this)[i];
   110     currentState = QString("%1%2").arg(currentState).arg(num);
   122     currentState = QString("%1%2").arg(currentState).arg(num);
   111   }
   123   }
       
   124   wconf->setValue(m_name->text(), currentState);
   112 }
   125 }
   113 
   126 
   114 int SelWeaponWidget::operator [] (unsigned int weaponIndex) const
   127 int SelWeaponWidget::operator [] (unsigned int weaponIndex) const
   115 {
   128 {
   116   twi::const_iterator it=weaponItems.find(weaponIndex);
   129   twi::const_iterator it=weaponItems.find(weaponIndex);
   119 
   132 
   120 QString SelWeaponWidget::getWeaponsString() const
   133 QString SelWeaponWidget::getWeaponsString() const
   121 {
   134 {
   122   return currentState;
   135   return currentState;
   123 }
   136 }
       
   137 
       
   138 void SelWeaponWidget::setWeaponsName(const QString& name)
       
   139 {
       
   140   if(name!="" && wconf->contains(name)) {
       
   141     setWeapons(wconf->value(name).toString());
       
   142   }
       
   143 
       
   144   curWeaponsName=name;
       
   145   m_name->setText(name);
       
   146 }
       
   147 
       
   148 QStringList SelWeaponWidget::getWeaponNames() const
       
   149 {
       
   150   return wconf->allKeys();
       
   151 }