QTfrontend/selectWeapon.cpp
changeset 1509 34f7dd4efe84
parent 1456 2539dd079be1
child 1576 a02353129a41
equal deleted inserted replaced
1508:ef093f31ced1 1509:34f7dd4efe84
    13  *
    13  *
    14  * You should have received a copy of the GNU General Public License
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program; if not, write to the Free Software
    15  * along with this program; if not, write to the Free Software
    16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
    16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
    17  */
    17  */
    18  
    18 
    19 #include "selectWeapon.h"
    19 #include "selectWeapon.h"
    20 #include "weaponItem.h"
    20 #include "weaponItem.h"
    21 #include "hwconsts.h"
    21 #include "hwconsts.h"
    22 
    22 
    23 #include <QPushButton>
    23 #include <QPushButton>
    29 #include <QSettings>
    29 #include <QSettings>
    30 #include <QMessageBox>
    30 #include <QMessageBox>
    31 
    31 
    32 QImage getAmmoImage(int num)
    32 QImage getAmmoImage(int num)
    33 {
    33 {
    34   static QImage ammo(":Ammos.png");
    34 	static QImage ammo(":Ammos.png");
    35   return ammo.copy(0, num*32, 32, 32);
    35 	return ammo.copy(0, num*32, 32, 32);
    36 }
    36 }
    37 
    37 
    38 SelWeaponItem::SelWeaponItem(int iconNum, int wNum, QWidget* parent) :
    38 SelWeaponItem::SelWeaponItem(int iconNum, int wNum, QWidget* parent) :
    39   QWidget(parent)
    39 	QWidget(parent)
    40 {
    40 {
    41   QHBoxLayout* hbLayout = new QHBoxLayout(this);
    41 	QHBoxLayout* hbLayout = new QHBoxLayout(this);
    42   hbLayout->setSpacing(1);
    42 	hbLayout->setSpacing(1);
    43   hbLayout->setMargin(1);
    43 	hbLayout->setMargin(1);
    44   
    44 	
    45   QLabel* lbl = new QLabel(this);
    45 	QLabel* lbl = new QLabel(this);
    46   lbl->setPixmap(QPixmap::fromImage(getAmmoImage(iconNum)));
    46 	lbl->setPixmap(QPixmap::fromImage(getAmmoImage(iconNum)));
    47   lbl->setMaximumWidth(30);
    47 	lbl->setMaximumWidth(30);
    48   lbl->setGeometry(0, 0, 30, 30);
    48 	lbl->setGeometry(0, 0, 30, 30);
    49   hbLayout->addWidget(lbl);
    49 	hbLayout->addWidget(lbl);
    50 
    50 
    51   item=new WeaponItem(QImage(":/res/ammopic.png"), this);
    51 	item = new WeaponItem(QImage(":/res/ammopic.png"), this);
    52   item->setItemsNum(wNum);
    52 	item->setItemsNum(wNum);
    53   item->setInfinityState(true);
    53 	item->setInfinityState(true);
    54   hbLayout->addWidget(item);
    54 	hbLayout->addWidget(item);
    55 
    55 
    56   hbLayout->setStretchFactor(lbl, 1);
    56 	hbLayout->setStretchFactor(lbl, 1);
    57   hbLayout->setStretchFactor(item, 99);
    57 	hbLayout->setStretchFactor(item, 99);
    58   hbLayout->setAlignment(lbl, Qt::AlignLeft | Qt::AlignVCenter);
    58 	hbLayout->setAlignment(lbl, Qt::AlignLeft | Qt::AlignVCenter);
    59   hbLayout->setAlignment(item, Qt::AlignLeft | Qt::AlignVCenter);
    59 	hbLayout->setAlignment(item, Qt::AlignLeft | Qt::AlignVCenter);
    60 }
    60 }
    61 
    61 
    62 void SelWeaponItem::setItemsNum(const unsigned char num)
    62 void SelWeaponItem::setItemsNum(const unsigned char num)
    63 {
    63 {
    64   item->setItemsNum(num);
    64 	item->setItemsNum(num);
    65 }
    65 }
    66 
    66 
    67 unsigned char SelWeaponItem::getItemsNum() const
    67 unsigned char SelWeaponItem::getItemsNum() const
    68 {
    68 {
    69   return item->getItemsNum();
    69 	return item->getItemsNum();
    70 }
    70 }
    71 
    71 
    72 SelWeaponWidget::SelWeaponWidget(int numItems, QWidget* parent) :
    72 SelWeaponWidget::SelWeaponWidget(int numItems, QWidget* parent) :
    73   m_numItems(numItems),
    73   m_numItems(numItems),
    74   QFrame(parent)
    74   QFrame(parent)
    75 {
    75 {
    76   wconf = new QSettings(cfgdir->absolutePath() + "/weapons.ini", QSettings::IniFormat, this);
    76 	wconf = new QSettings(cfgdir->absolutePath() + "/weapons.ini", QSettings::IniFormat, this);
    77 
    77 
    78   wconf->setValue("Default", cDefaultAmmoStore->mid(10));
    78 	wconf->setValue("Default", cDefaultAmmoStore->mid(10));
    79 
    79 
    80   currentState=cDefaultAmmoStore->mid(10);
    80 	QString currentState = cDefaultAmmoStore->mid(10);
    81 
    81 
    82   pLayout=new QGridLayout(this);
    82 	pLayout = new QGridLayout(this);
    83   pLayout->setSpacing(1);
    83 	pLayout->setSpacing(1);
    84   pLayout->setMargin(1);
    84 	pLayout->setMargin(1);
    85 
    85 
    86   int j=-1;
    86 	int j = -1;
    87   int i=0, k=0;
    87 	int i = 0, k = 0;
    88   for(; i<m_numItems; ++i) {
    88 	for(; i < m_numItems; ++i) {
    89     if(i==6) continue;
    89 		if (i == 6) continue;
    90     if (k%4==0) ++j;
    90 		if (k % 4 == 0) ++j;
    91     weaponItems[i]=new SelWeaponItem(i, currentState[i].digitValue(), this);
    91 		weaponItems[i] = new SelWeaponItem(i, currentState[i].digitValue(), this);
    92     pLayout->addWidget(weaponItems[i], j, k%4);
    92 		pLayout->addWidget(weaponItems[i], j, k % 4);
    93     ++k;
    93 		++k;
    94   }
    94 	}
    95 
    95 
    96   //pLayout->setRowStretch(5, 100);
    96 	//pLayout->setRowStretch(5, 100);
    97   m_name = new QLineEdit(this);
    97 	m_name = new QLineEdit(this);
    98   pLayout->addWidget(m_name, i, 0, 1, 5);
    98 	pLayout->addWidget(m_name, i, 0, 1, 5);
    99 }
    99 }
   100 
   100 
   101 void SelWeaponWidget::setWeapons(const QString& ammo)
   101 void SelWeaponWidget::setWeapons(const QString& ammo)
   102 {
   102 {
   103   for(int i=0; i<m_numItems; ++i) {
   103 	for(int i = 0; i < m_numItems; ++i) {
   104     twi::iterator it=weaponItems.find(i);
   104 		twi::iterator it = weaponItems.find(i);
   105     if (it==weaponItems.end()) continue;
   105 		if (it == weaponItems.end()) continue;
   106     it->second->setItemsNum(ammo[i].digitValue());
   106 		it->second->setItemsNum(ammo[i].digitValue());
   107   }
   107 	}
   108   update();
   108 	update();
   109 }
   109 }
   110 
   110 
   111 void SelWeaponWidget::setDefault()
   111 void SelWeaponWidget::setDefault()
   112 {
   112 {
   113   setWeapons(cDefaultAmmoStore->mid(10));
   113 	setWeapons(cDefaultAmmoStore->mid(10));
   114 }
   114 }
   115 
   115 
   116 void SelWeaponWidget::save()
   116 void SelWeaponWidget::save()
   117 {
   117 {
   118   if (m_name->text()=="Default") {
   118 	if (m_name->text() == "Default") {
   119     QMessageBox impossible(QMessageBox::Warning, QMessageBox::tr("Weapons"), QMessageBox::tr("Can not edit default weapon set"));
   119 		QMessageBox impossible(QMessageBox::Warning, QMessageBox::tr("Weapons"), QMessageBox::tr("Can not edit default weapon set"));
   120     impossible.exec();
   120 		impossible.exec();
   121     return;
   121 		return;
   122   }
   122 	}
   123   if (m_name->text()=="") return;
   123 	
   124   currentState="";
   124 	if (m_name->text() == "") return;
   125   for(int i=0; i<m_numItems; ++i) {
   125 	
   126     twi::const_iterator it=weaponItems.find(i);
   126 	QString currentState;
   127     int num = it==weaponItems.end() ? 9 : (*this)[i];
   127 	
   128     currentState = QString("%1%2").arg(currentState).arg(num);
   128 	for(int i = 0; i < m_numItems; ++i) {
   129   }
   129 		twi::const_iterator it = weaponItems.find(i);
   130   if (curWeaponsName!="") {
   130 		int num = it == weaponItems.end() ? 9 : (*this)[i];
   131     // remove old entry
   131 		currentState = QString("%1%2").arg(currentState).arg(num);
   132     wconf->remove(curWeaponsName);
   132 	}
   133   }
   133 	if (curWeaponsName != "") {
   134   wconf->setValue(m_name->text(), currentState);
   134 		// remove old entry
   135   emit weaponsChanged();
   135 		wconf->remove(curWeaponsName);
       
   136 	}
       
   137 	wconf->setValue(m_name->text(), currentState);
       
   138 	emit weaponsChanged();
   136 }
   139 }
   137 
   140 
   138 int SelWeaponWidget::operator [] (unsigned int weaponIndex) const
   141 int SelWeaponWidget::operator [] (unsigned int weaponIndex) const
   139 {
   142 {
   140   twi::const_iterator it=weaponItems.find(weaponIndex);
   143 	twi::const_iterator it = weaponItems.find(weaponIndex);
   141   return it==weaponItems.end() ? 9 : it->second->getItemsNum();
   144 	return it == weaponItems.end() ? 9 : it->second->getItemsNum();
   142 }
   145 }
   143 
   146 
   144 QString SelWeaponWidget::getWeaponsString(const QString& name) const
   147 QString SelWeaponWidget::getWeaponsString(const QString& name) const
   145 {
   148 {
   146   return wconf->value(name).toString();
   149 	return wconf->value(name).toString();
   147 }
   150 }
   148 
   151 
   149 void SelWeaponWidget::deleteWeaponsName()
   152 void SelWeaponWidget::deleteWeaponsName()
   150 {
   153 {
   151   if (curWeaponsName=="") return;
   154 	if (curWeaponsName == "") return;
   152 
   155 
   153   if (curWeaponsName=="Default") {
   156 	if (curWeaponsName == "Default") {
   154     QMessageBox impossible(QMessageBox::Warning, QMessageBox::tr("Weapons"), QMessageBox::tr("Can not delete default weapon set"));
   157 		QMessageBox impossible(QMessageBox::Warning, QMessageBox::tr("Weapons"), QMessageBox::tr("Can not delete default weapon set"));
   155     impossible.exec();
   158 		impossible.exec();
   156     return;
   159 		return;
   157   }
   160 	}
   158 
   161 
   159   QMessageBox reallyDelete(QMessageBox::Question, QMessageBox::tr("Weapons"), QMessageBox::tr("Really delete this weapon set?"),
   162 	QMessageBox reallyDelete(QMessageBox::Question, QMessageBox::tr("Weapons"), QMessageBox::tr("Really delete this weapon set?"), QMessageBox::Ok | QMessageBox::Cancel);
   160 			   QMessageBox::Ok | QMessageBox::Cancel);
   163 	
   161   
   164 	if (reallyDelete.exec() == QMessageBox::Ok) {
   162   if (reallyDelete.exec()==QMessageBox::Ok) {
   165 		wconf->remove(curWeaponsName);
   163     wconf->remove(curWeaponsName);
   166 		emit weaponsDeleted();
   164     emit weaponsDeleted();
   167 	}
   165   }
       
   166 }
   168 }
   167 
   169 
   168 void SelWeaponWidget::setWeaponsName(const QString& name, bool editMode)
   170 void SelWeaponWidget::setWeaponsName(const QString& name)
   169 {
   171 {
   170   if(name!="" && wconf->contains(name)) {
   172 	if(name != "" && wconf->contains(name)) {
   171     setWeapons(wconf->value(name).toString());
   173 		setWeapons(wconf->value(name).toString());
   172   }
   174 	}
   173 
   175 
   174   if(editMode) curWeaponsName=name;
   176 	curWeaponsName = name;
   175   else curWeaponsName="";
       
   176 
   177 
   177   m_name->setText(name);
   178 	m_name->setText(name);
   178 }
   179 }
   179 
   180 
   180 QStringList SelWeaponWidget::getWeaponNames() const
   181 QStringList SelWeaponWidget::getWeaponNames() const
   181 {
   182 {
   182   return wconf->allKeys();
   183 	return wconf->allKeys();
   183 }
   184 }