author | unc0rr |
Sat, 01 Aug 2009 13:59:41 +0000 | |
changeset 2292 | 87af4ad0ec98 |
parent 1967 | 213d368a002f |
child 2369 | c3eb11f1ab3a |
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 |
*/ |
1509 | 18 |
|
624 | 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 |
{ |
|
1509 | 34 |
static QImage ammo(":Ammos.png"); |
35 |
return ammo.copy(0, num*32, 32, 32); |
|
629 | 36 |
} |
37 |
||
681 | 38 |
SelWeaponItem::SelWeaponItem(int iconNum, int wNum, QWidget* parent) : |
1509 | 39 |
QWidget(parent) |
629 | 40 |
{ |
1509 | 41 |
QHBoxLayout* hbLayout = new QHBoxLayout(this); |
42 |
hbLayout->setSpacing(1); |
|
43 |
hbLayout->setMargin(1); |
|
44 |
||
45 |
QLabel* lbl = new QLabel(this); |
|
46 |
lbl->setPixmap(QPixmap::fromImage(getAmmoImage(iconNum))); |
|
47 |
lbl->setMaximumWidth(30); |
|
48 |
lbl->setGeometry(0, 0, 30, 30); |
|
49 |
hbLayout->addWidget(lbl); |
|
639 | 50 |
|
1509 | 51 |
item = new WeaponItem(QImage(":/res/ammopic.png"), this); |
52 |
item->setItemsNum(wNum); |
|
53 |
item->setInfinityState(true); |
|
54 |
hbLayout->addWidget(item); |
|
639 | 55 |
|
1509 | 56 |
hbLayout->setStretchFactor(lbl, 1); |
57 |
hbLayout->setStretchFactor(item, 99); |
|
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 |
{ |
|
1509 | 64 |
item->setItemsNum(num); |
683 | 65 |
} |
66 |
||
681 | 67 |
unsigned char SelWeaponItem::getItemsNum() const |
68 |
{ |
|
1509 | 69 |
return item->getItemsNum(); |
681 | 70 |
} |
71 |
||
683 | 72 |
SelWeaponWidget::SelWeaponWidget(int numItems, QWidget* parent) : |
1904 | 73 |
QFrame(parent), |
74 |
m_numItems(numItems) |
|
612
333d095319de
abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
598
diff
changeset
|
75 |
{ |
1509 | 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 |
|
1967 | 78 |
for(int i = 0; i < cDefaultAmmos.size(); ++i) |
79 |
wconf->setValue(cDefaultAmmos[i].first, cDefaultAmmos[i].second); |
|
694 | 80 |
|
1576
a02353129a41
Check for deprecated ammo schemes at startup and delete them
unc0rr
parents:
1509
diff
changeset
|
81 |
QStringList keys = wconf->allKeys(); |
a02353129a41
Check for deprecated ammo schemes at startup and delete them
unc0rr
parents:
1509
diff
changeset
|
82 |
for(int i = 0; i < keys.size(); i++) |
a02353129a41
Check for deprecated ammo schemes at startup and delete them
unc0rr
parents:
1509
diff
changeset
|
83 |
{ |
a02353129a41
Check for deprecated ammo schemes at startup and delete them
unc0rr
parents:
1509
diff
changeset
|
84 |
if (wconf->value(keys[i]).toString().size() != cDefaultAmmoStore->size()) |
a02353129a41
Check for deprecated ammo schemes at startup and delete them
unc0rr
parents:
1509
diff
changeset
|
85 |
wconf->remove(keys[i]); |
a02353129a41
Check for deprecated ammo schemes at startup and delete them
unc0rr
parents:
1509
diff
changeset
|
86 |
} |
a02353129a41
Check for deprecated ammo schemes at startup and delete them
unc0rr
parents:
1509
diff
changeset
|
87 |
|
a02353129a41
Check for deprecated ammo schemes at startup and delete them
unc0rr
parents:
1509
diff
changeset
|
88 |
QString currentState = *cDefaultAmmoStore; |
683 | 89 |
|
1509 | 90 |
pLayout = new QGridLayout(this); |
91 |
pLayout->setSpacing(1); |
|
92 |
pLayout->setMargin(1); |
|
629 | 93 |
|
1509 | 94 |
int j = -1; |
95 |
int i = 0, k = 0; |
|
96 |
for(; i < m_numItems; ++i) { |
|
97 |
if (i == 6) continue; |
|
98 |
if (k % 4 == 0) ++j; |
|
99 |
weaponItems[i] = new SelWeaponItem(i, currentState[i].digitValue(), this); |
|
100 |
pLayout->addWidget(weaponItems[i], j, k % 4); |
|
101 |
++k; |
|
102 |
} |
|
694 | 103 |
|
1509 | 104 |
//pLayout->setRowStretch(5, 100); |
105 |
m_name = new QLineEdit(this); |
|
106 |
pLayout->addWidget(m_name, i, 0, 1, 5); |
|
612
333d095319de
abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
598
diff
changeset
|
107 |
} |
681 | 108 |
|
694 | 109 |
void SelWeaponWidget::setWeapons(const QString& ammo) |
683 | 110 |
{ |
1509 | 111 |
for(int i = 0; i < m_numItems; ++i) { |
112 |
twi::iterator it = weaponItems.find(i); |
|
113 |
if (it == weaponItems.end()) continue; |
|
114 |
it->second->setItemsNum(ammo[i].digitValue()); |
|
115 |
} |
|
116 |
update(); |
|
683 | 117 |
} |
118 |
||
119 |
void SelWeaponWidget::setDefault() |
|
120 |
{ |
|
1576
a02353129a41
Check for deprecated ammo schemes at startup and delete them
unc0rr
parents:
1509
diff
changeset
|
121 |
setWeapons(*cDefaultAmmoStore); |
683 | 122 |
} |
123 |
||
124 |
void SelWeaponWidget::save() |
|
125 |
{ |
|
1509 | 126 |
if (m_name->text() == "Default") { |
127 |
QMessageBox impossible(QMessageBox::Warning, QMessageBox::tr("Weapons"), QMessageBox::tr("Can not edit default weapon set")); |
|
128 |
impossible.exec(); |
|
129 |
return; |
|
130 |
} |
|
131 |
||
132 |
if (m_name->text() == "") return; |
|
133 |
||
134 |
QString currentState; |
|
135 |
||
136 |
for(int i = 0; i < m_numItems; ++i) { |
|
137 |
twi::const_iterator it = weaponItems.find(i); |
|
138 |
int num = it == weaponItems.end() ? 9 : (*this)[i]; |
|
139 |
currentState = QString("%1%2").arg(currentState).arg(num); |
|
140 |
} |
|
141 |
if (curWeaponsName != "") { |
|
142 |
// remove old entry |
|
143 |
wconf->remove(curWeaponsName); |
|
144 |
} |
|
145 |
wconf->setValue(m_name->text(), currentState); |
|
146 |
emit weaponsChanged(); |
|
683 | 147 |
} |
148 |
||
681 | 149 |
int SelWeaponWidget::operator [] (unsigned int weaponIndex) const |
150 |
{ |
|
1509 | 151 |
twi::const_iterator it = weaponItems.find(weaponIndex); |
152 |
return it == weaponItems.end() ? 9 : it->second->getItemsNum(); |
|
681 | 153 |
} |
154 |
||
696 | 155 |
QString SelWeaponWidget::getWeaponsString(const QString& name) const |
156 |
{ |
|
1509 | 157 |
return wconf->value(name).toString(); |
696 | 158 |
} |
159 |
||
718 | 160 |
void SelWeaponWidget::deleteWeaponsName() |
161 |
{ |
|
1509 | 162 |
if (curWeaponsName == "") return; |
724 | 163 |
|
1509 | 164 |
if (curWeaponsName == "Default") { |
165 |
QMessageBox impossible(QMessageBox::Warning, QMessageBox::tr("Weapons"), QMessageBox::tr("Can not delete default weapon set")); |
|
166 |
impossible.exec(); |
|
167 |
return; |
|
168 |
} |
|
169 |
||
170 |
QMessageBox reallyDelete(QMessageBox::Question, QMessageBox::tr("Weapons"), QMessageBox::tr("Really delete this weapon set?"), QMessageBox::Ok | QMessageBox::Cancel); |
|
171 |
||
172 |
if (reallyDelete.exec() == QMessageBox::Ok) { |
|
173 |
wconf->remove(curWeaponsName); |
|
174 |
emit weaponsDeleted(); |
|
175 |
} |
|
718 | 176 |
} |
177 |
||
1509 | 178 |
void SelWeaponWidget::setWeaponsName(const QString& name) |
694 | 179 |
{ |
1509 | 180 |
if(name != "" && wconf->contains(name)) { |
181 |
setWeapons(wconf->value(name).toString()); |
|
182 |
} |
|
694 | 183 |
|
1509 | 184 |
curWeaponsName = name; |
717 | 185 |
|
1509 | 186 |
m_name->setText(name); |
694 | 187 |
} |
188 |
||
189 |
QStringList SelWeaponWidget::getWeaponNames() const |
|
190 |
{ |
|
1509 | 191 |
return wconf->allKeys(); |
694 | 192 |
} |