QTfrontend/selectWeapon.cpp
changeset 2369 c3eb11f1ab3a
parent 1967 213d368a002f
child 2371 a20b6e26044b
equal deleted inserted replaced
2368:e0750b23c9e6 2369:c3eb11f1ab3a
    26 #include <QLabel>
    26 #include <QLabel>
    27 #include <QBitmap>
    27 #include <QBitmap>
    28 #include <QLineEdit>
    28 #include <QLineEdit>
    29 #include <QSettings>
    29 #include <QSettings>
    30 #include <QMessageBox>
    30 #include <QMessageBox>
       
    31 #include <QTabWidget>
    31 
    32 
    32 QImage getAmmoImage(int num)
    33 QImage getAmmoImage(int num)
    33 {
    34 {
    34 	static QImage ammo(":Ammos.png");
    35 	static QImage ammo(":Ammos.png");
    35 	return ammo.copy(0, num*32, 32, 32);
    36 	return ammo.copy(0, num*32, 32, 32);
    36 }
    37 }
    37 
    38 
    38 SelWeaponItem::SelWeaponItem(int iconNum, int wNum, QWidget* parent) :
    39 SelWeaponItem::SelWeaponItem(bool allowInfinite, int iconNum, int wNum, QWidget* parent) :
    39 	QWidget(parent)
    40 	QWidget(parent)
    40 {
    41 {
    41 	QHBoxLayout* hbLayout = new QHBoxLayout(this);
    42 	QHBoxLayout* hbLayout = new QHBoxLayout(this);
    42 	hbLayout->setSpacing(1);
    43 	hbLayout->setSpacing(1);
    43 	hbLayout->setMargin(1);
    44 	hbLayout->setMargin(1);
    48 	lbl->setGeometry(0, 0, 30, 30);
    49 	lbl->setGeometry(0, 0, 30, 30);
    49 	hbLayout->addWidget(lbl);
    50 	hbLayout->addWidget(lbl);
    50 
    51 
    51 	item = new WeaponItem(QImage(":/res/ammopic.png"), this);
    52 	item = new WeaponItem(QImage(":/res/ammopic.png"), this);
    52 	item->setItemsNum(wNum);
    53 	item->setItemsNum(wNum);
    53 	item->setInfinityState(true);
    54 	item->setInfinityState(allowInfinite);
    54 	hbLayout->addWidget(item);
    55 	hbLayout->addWidget(item);
    55 
    56 
    56 	hbLayout->setStretchFactor(lbl, 1);
    57 	hbLayout->setStretchFactor(lbl, 1);
    57 	hbLayout->setStretchFactor(item, 99);
    58 	hbLayout->setStretchFactor(item, 99);
    58 	hbLayout->setAlignment(lbl, Qt::AlignLeft | Qt::AlignVCenter);
    59 	hbLayout->setAlignment(lbl, Qt::AlignLeft | Qt::AlignVCenter);
    85 			wconf->remove(keys[i]);
    86 			wconf->remove(keys[i]);
    86 	}
    87 	}
    87 
    88 
    88 	QString currentState = *cDefaultAmmoStore;
    89 	QString currentState = *cDefaultAmmoStore;
    89 
    90 
    90 	pLayout = new QGridLayout(this);
    91 	QTabWidget * tbw = new QTabWidget(this);
    91 	pLayout->setSpacing(1);
    92 	QWidget * page1 = new QWidget(this);
    92 	pLayout->setMargin(1);
    93 	p1Layout = new QGridLayout(page1);
       
    94 	p1Layout->setSpacing(1);
       
    95 	p1Layout->setMargin(1);
       
    96 	QWidget * page2 = new QWidget(this);
       
    97 	p2Layout = new QGridLayout(page2);
       
    98 	p2Layout->setSpacing(1);
       
    99 	p2Layout->setMargin(1);
       
   100 	
       
   101 	tbw->addTab(page1, tr("Weapon set"));
       
   102 	tbw->addTab(page2, tr("Probabilities"));
       
   103 	
       
   104 	QGridLayout * pageLayout = new QGridLayout(this);
       
   105 	pageLayout->addWidget(tbw);
       
   106 
    93 
   107 
    94 	int j = -1;
   108 	int j = -1;
    95 	int i = 0, k = 0;
   109 	int i = 0, k = 0;
    96 	for(; i < m_numItems; ++i) {
   110 	for(; i < m_numItems; ++i) {
    97 		if (i == 6) continue;
   111 		if (i == 6) continue;
    98 		if (k % 4 == 0) ++j;
   112 		if (k % 4 == 0) ++j;
    99 		weaponItems[i] = new SelWeaponItem(i, currentState[i].digitValue(), this);
   113 		SelWeaponItem * swi = new SelWeaponItem(true, i, currentState[i].digitValue(), this);
   100 		pLayout->addWidget(weaponItems[i], j, k % 4);
   114 		weaponItems[i].append(swi);
       
   115 		p1Layout->addWidget(swi, j, k % 4);
       
   116 		
       
   117 		SelWeaponItem * pwi = new SelWeaponItem(false, i, currentState[numItems + i].digitValue(), this);
       
   118 		weaponItems[i].append(pwi);
       
   119 		p2Layout->addWidget(pwi, j, k % 4);
       
   120 		
   101 		++k;
   121 		++k;
   102 	}
   122 	}
   103 
   123 
   104 	//pLayout->setRowStretch(5, 100);
   124 	//pLayout->setRowStretch(5, 100);
   105 	m_name = new QLineEdit(this);
   125 	m_name = new QLineEdit(this);
   106 	pLayout->addWidget(m_name, i, 0, 1, 5);
   126 	pageLayout->addWidget(m_name, i, 0, 1, 5);
   107 }
   127 }
   108 
   128 
   109 void SelWeaponWidget::setWeapons(const QString& ammo)
   129 void SelWeaponWidget::setWeapons(const QString& ammo)
   110 {
   130 {
   111 	for(int i = 0; i < m_numItems; ++i) {
   131 	for(int i = 0; i < m_numItems; ++i) {
   112 		twi::iterator it = weaponItems.find(i);
   132 		twi::iterator it = weaponItems.find(i);
   113 		if (it == weaponItems.end()) continue;
   133 		if (it == weaponItems.end()) continue;
   114 		it->second->setItemsNum(ammo[i].digitValue());
   134 		it.value()[0]->setItemsNum(ammo[i].digitValue());
       
   135 		it.value()[1]->setItemsNum(ammo[m_numItems + i].digitValue());
   115 	}
   136 	}
   116 	update();
   137 	update();
   117 }
   138 }
   118 
   139 
   119 void SelWeaponWidget::setDefault()
   140 void SelWeaponWidget::setDefault()
   129 		return;
   150 		return;
   130 	}
   151 	}
   131 	
   152 	
   132 	if (m_name->text() == "") return;
   153 	if (m_name->text() == "") return;
   133 	
   154 	
   134 	QString currentState;
   155 	QString state1;
       
   156 	QString state2;
   135 	
   157 	
   136 	for(int i = 0; i < m_numItems; ++i) {
   158 	for(int i = 0; i < m_numItems; ++i) {
   137 		twi::const_iterator it = weaponItems.find(i);
   159 		twi::const_iterator it = weaponItems.find(i);
   138 		int num = it == weaponItems.end() ? 9 : (*this)[i];
   160 		int num = it == weaponItems.end() ? 0 : it.value()[0]->getItemsNum();
   139 		currentState = QString("%1%2").arg(currentState).arg(num);
   161 		state1.append(QString::number(num));
       
   162 		int prob = it == weaponItems.end() ? 0 : it.value()[1]->getItemsNum();
       
   163 		state2.append(QString::number(prob));
   140 	}
   164 	}
   141 	if (curWeaponsName != "") {
   165 	if (curWeaponsName != "") {
   142 		// remove old entry
   166 		// remove old entry
   143 		wconf->remove(curWeaponsName);
   167 		wconf->remove(curWeaponsName);
   144 	}
   168 	}
   145 	wconf->setValue(m_name->text(), currentState);
   169 	wconf->setValue(m_name->text(), state1 + state2);
   146 	emit weaponsChanged();
   170 	emit weaponsChanged();
   147 }
   171 }
   148 
   172 
   149 int SelWeaponWidget::operator [] (unsigned int weaponIndex) const
   173 int SelWeaponWidget::operator [] (unsigned int weaponIndex) const
   150 {
   174 {
   151 	twi::const_iterator it = weaponItems.find(weaponIndex);
   175 	twi::const_iterator it = weaponItems.find(weaponIndex);
   152 	return it == weaponItems.end() ? 9 : it->second->getItemsNum();
   176 	return it == weaponItems.end() ? 9 : it.value()[0]->getItemsNum();
   153 }
   177 }
   154 
   178 
   155 QString SelWeaponWidget::getWeaponsString(const QString& name) const
   179 QString SelWeaponWidget::getWeaponsString(const QString& name) const
   156 {
   180 {
   157 	return wconf->value(name).toString();
   181 	return wconf->value(name).toString();