author | unc0rr |
Tue, 07 Oct 2008 12:37:06 +0000 | |
changeset 1311 | 49beb1221c0b |
parent 1066 | 1f1b3686a2b0 |
child 1348 | 0eb0b3a955b8 |
permissions | -rw-r--r-- |
612
333d095319de
abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
598
diff
changeset
|
1 |
/* |
1066 | 2 |
* Hedgewars, a free turn based strategy game |
883 | 3 |
* Copyright (c) 2006-2008 Ulyanov Igor <iulyanov@gmail.com> |
612
333d095319de
abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
598
diff
changeset
|
4 |
* |
333d095319de
abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
598
diff
changeset
|
5 |
* This program is free software; you can redistribute it and/or modify |
333d095319de
abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
598
diff
changeset
|
6 |
* it under the terms of the GNU General Public License as published by |
333d095319de
abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
598
diff
changeset
|
7 |
* the Free Software Foundation; version 2 of the License |
333d095319de
abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
598
diff
changeset
|
8 |
* |
333d095319de
abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
598
diff
changeset
|
9 |
* This program is distributed in the hope that it will be useful, |
333d095319de
abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
598
diff
changeset
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
333d095319de
abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
598
diff
changeset
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
333d095319de
abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
598
diff
changeset
|
12 |
* GNU General Public License for more details. |
333d095319de
abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
598
diff
changeset
|
13 |
* |
333d095319de
abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
598
diff
changeset
|
14 |
* You should have received a copy of the GNU General Public License |
333d095319de
abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
598
diff
changeset
|
15 |
* along with this program; if not, write to the Free Software |
333d095319de
abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
598
diff
changeset
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
333d095319de
abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
598
diff
changeset
|
17 |
*/ |
624 | 18 |
|
19 |
#include "selectWeapon.h" |
|
629 | 20 |
#include "weaponItem.h" |
681 | 21 |
#include "hwconsts.h" |
612
333d095319de
abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
598
diff
changeset
|
22 |
|
624 | 23 |
#include <QPushButton> |
24 |
#include <QGridLayout> |
|
629 | 25 |
#include <QHBoxLayout> |
26 |
#include <QLabel> |
|
630 | 27 |
#include <QBitmap> |
694 | 28 |
#include <QLineEdit> |
29 |
#include <QSettings> |
|
724 | 30 |
#include <QMessageBox> |
624 | 31 |
|
629 | 32 |
QImage getAmmoImage(int num) |
33 |
{ |
|
34 |
static QImage ammo(":Ammos.png"); |
|
35 |
return ammo.copy(0, num*32, 32, 32); |
|
36 |
} |
|
37 |
||
681 | 38 |
SelWeaponItem::SelWeaponItem(int iconNum, int wNum, QWidget* parent) : |
629 | 39 |
QWidget(parent) |
40 |
{ |
|
41 |
QHBoxLayout* hbLayout = new QHBoxLayout(this); |
|
644 | 42 |
hbLayout->setSpacing(1); |
43 |
hbLayout->setMargin(1); |
|
629 | 44 |
|
639 | 45 |
QLabel* lbl = new QLabel(this); |
681 | 46 |
lbl->setPixmap(QPixmap::fromImage(getAmmoImage(iconNum))); |
639 | 47 |
lbl->setMaximumWidth(30); |
48 |
lbl->setGeometry(0, 0, 30, 30); |
|
629 | 49 |
hbLayout->addWidget(lbl); |
639 | 50 |
|
719 | 51 |
item=new WeaponItem(QImage(":/res/ammopic.png"), this); |
681 | 52 |
item->setItemsNum(wNum); |
640 | 53 |
item->setInfinityState(true); |
629 | 54 |
hbLayout->addWidget(item); |
639 | 55 |
|
56 |
hbLayout->setStretchFactor(lbl, 1); |
|
57 |
hbLayout->setStretchFactor(item, 99); |
|
644 | 58 |
hbLayout->setAlignment(lbl, Qt::AlignLeft | Qt::AlignVCenter); |
59 |
hbLayout->setAlignment(item, Qt::AlignLeft | Qt::AlignVCenter); |
|
629 | 60 |
} |
612
333d095319de
abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
598
diff
changeset
|
61 |
|
683 | 62 |
void SelWeaponItem::setItemsNum(const unsigned char num) |
63 |
{ |
|
64 |
item->setItemsNum(num); |
|
65 |
} |
|
66 |
||
681 | 67 |
unsigned char SelWeaponItem::getItemsNum() const |
68 |
{ |
|
69 |
return item->getItemsNum(); |
|
70 |
} |
|
71 |
||
683 | 72 |
SelWeaponWidget::SelWeaponWidget(int numItems, QWidget* parent) : |
73 |
m_numItems(numItems), |
|
74 |
QWidget(parent) |
|
612
333d095319de
abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
598
diff
changeset
|
75 |
{ |
694 | 76 |
wconf = new QSettings(cfgdir->absolutePath() + "/weapons.ini", QSettings::IniFormat, this); |
1004
4cbd91296df7
Don't let updated hedgewars version to make errors due to old weapons.ini
unc0rr
parents:
883
diff
changeset
|
77 |
|
4cbd91296df7
Don't let updated hedgewars version to make errors due to old weapons.ini
unc0rr
parents:
883
diff
changeset
|
78 |
if(wconf->value("Default", "").toString().size() != cDefaultAmmoStore->size() - 10) |
4cbd91296df7
Don't let updated hedgewars version to make errors due to old weapons.ini
unc0rr
parents:
883
diff
changeset
|
79 |
{ |
4cbd91296df7
Don't let updated hedgewars version to make errors due to old weapons.ini
unc0rr
parents:
883
diff
changeset
|
80 |
wconf->clear(); |
694 | 81 |
wconf->setValue("Default", cDefaultAmmoStore->mid(10)); |
82 |
} |
|
83 |
||
683 | 84 |
currentState=cDefaultAmmoStore->mid(10); |
85 |
||
624 | 86 |
pLayout=new QGridLayout(this); |
644 | 87 |
pLayout->setSpacing(1); |
88 |
pLayout->setMargin(1); |
|
629 | 89 |
|
90 |
int j=-1; |
|
694 | 91 |
int i=0, k=0; |
92 |
for(; i<m_numItems; ++i) { |
|
681 | 93 |
if(i==6) continue; |
94 |
if (k%4==0) ++j; |
|
683 | 95 |
weaponItems[i]=new SelWeaponItem(i, currentState[i].digitValue(), this); |
681 | 96 |
pLayout->addWidget(weaponItems[i], j, k%4); |
97 |
++k; |
|
629 | 98 |
} |
694 | 99 |
|
723 | 100 |
//pLayout->setRowStretch(5, 100); |
694 | 101 |
m_name = new QLineEdit(this); |
719 | 102 |
pLayout->addWidget(m_name, i, 0, 1, 5); |
612
333d095319de
abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
598
diff
changeset
|
103 |
} |
681 | 104 |
|
694 | 105 |
void SelWeaponWidget::setWeapons(const QString& ammo) |
683 | 106 |
{ |
107 |
for(int i=0; i<m_numItems; ++i) { |
|
108 |
twi::iterator it=weaponItems.find(i); |
|
109 |
if (it==weaponItems.end()) continue; |
|
110 |
it->second->setItemsNum(ammo[i].digitValue()); |
|
111 |
} |
|
112 |
update(); |
|
113 |
} |
|
114 |
||
115 |
void SelWeaponWidget::setDefault() |
|
116 |
{ |
|
117 |
setWeapons(cDefaultAmmoStore->mid(10)); |
|
118 |
} |
|
119 |
||
120 |
void SelWeaponWidget::save() |
|
121 |
{ |
|
725 | 122 |
if (m_name->text()=="Default") { |
123 |
QMessageBox impossible(QMessageBox::Warning, QMessageBox::tr("Weapons"), QMessageBox::tr("Can not edit default weapon set")); |
|
124 |
impossible.exec(); |
|
125 |
return; |
|
126 |
} |
|
694 | 127 |
if (m_name->text()=="") return; |
683 | 128 |
currentState=""; |
129 |
for(int i=0; i<m_numItems; ++i) { |
|
130 |
twi::const_iterator it=weaponItems.find(i); |
|
131 |
int num = it==weaponItems.end() ? 9 : (*this)[i]; |
|
132 |
currentState = QString("%1%2").arg(currentState).arg(num); |
|
133 |
} |
|
717 | 134 |
if (curWeaponsName!="") { |
135 |
// remove old entry |
|
136 |
wconf->remove(curWeaponsName); |
|
137 |
} |
|
694 | 138 |
wconf->setValue(m_name->text(), currentState); |
695 | 139 |
emit weaponsChanged(); |
683 | 140 |
} |
141 |
||
681 | 142 |
int SelWeaponWidget::operator [] (unsigned int weaponIndex) const |
143 |
{ |
|
683 | 144 |
twi::const_iterator it=weaponItems.find(weaponIndex); |
681 | 145 |
return it==weaponItems.end() ? 9 : it->second->getItemsNum(); |
146 |
} |
|
147 |
||
148 |
QString SelWeaponWidget::getWeaponsString() const |
|
149 |
{ |
|
683 | 150 |
return currentState; |
681 | 151 |
} |
694 | 152 |
|
696 | 153 |
QString SelWeaponWidget::getWeaponsString(const QString& name) const |
154 |
{ |
|
155 |
return wconf->value(name).toString(); |
|
156 |
} |
|
157 |
||
718 | 158 |
void SelWeaponWidget::deleteWeaponsName() |
159 |
{ |
|
160 |
if (curWeaponsName=="") return; |
|
724 | 161 |
|
162 |
if (curWeaponsName=="Default") { |
|
163 |
QMessageBox impossible(QMessageBox::Warning, QMessageBox::tr("Weapons"), QMessageBox::tr("Can not delete default weapon set")); |
|
164 |
impossible.exec(); |
|
165 |
return; |
|
166 |
} |
|
167 |
||
168 |
QMessageBox reallyDelete(QMessageBox::Question, QMessageBox::tr("Weapons"), QMessageBox::tr("Really delete this weapon set?"), |
|
169 |
QMessageBox::Ok | QMessageBox::Cancel); |
|
170 |
||
171 |
if (reallyDelete.exec()==QMessageBox::Ok) { |
|
172 |
wconf->remove(curWeaponsName); |
|
173 |
emit weaponsDeleted(); |
|
174 |
} |
|
718 | 175 |
} |
176 |
||
717 | 177 |
void SelWeaponWidget::setWeaponsName(const QString& name, bool editMode) |
694 | 178 |
{ |
179 |
if(name!="" && wconf->contains(name)) { |
|
180 |
setWeapons(wconf->value(name).toString()); |
|
181 |
} |
|
182 |
||
717 | 183 |
if(editMode) curWeaponsName=name; |
184 |
else curWeaponsName=""; |
|
185 |
||
694 | 186 |
m_name->setText(name); |
187 |
} |
|
188 |
||
189 |
QStringList SelWeaponWidget::getWeaponNames() const |
|
190 |
{ |
|
191 |
return wconf->allKeys(); |
|
192 |
} |