author | displacer |
Sat, 12 Jan 2008 22:41:03 +0000 | |
changeset 703 | 424297e3165a |
parent 696 | d6f32ed6edc8 |
child 717 | 490dc8bb5b87 |
permissions | -rw-r--r-- |
612
333d095319de
abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
598
diff
changeset
|
1 |
/* |
333d095319de
abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
598
diff
changeset
|
2 |
* Hedgewars, a worms-like game |
333d095319de
abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
598
diff
changeset
|
3 |
* Copyright (c) 2006, 2007 Ulyanov Igor <iulyanov@gmail.com> |
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> |
|
624 | 30 |
|
629 | 31 |
QImage getAmmoImage(int num) |
32 |
{ |
|
33 |
static QImage ammo(":Ammos.png"); |
|
34 |
return ammo.copy(0, num*32, 32, 32); |
|
35 |
} |
|
36 |
||
681 | 37 |
SelWeaponItem::SelWeaponItem(int iconNum, int wNum, QWidget* parent) : |
629 | 38 |
QWidget(parent) |
39 |
{ |
|
40 |
QHBoxLayout* hbLayout = new QHBoxLayout(this); |
|
644 | 41 |
hbLayout->setSpacing(1); |
42 |
hbLayout->setMargin(1); |
|
629 | 43 |
|
639 | 44 |
QLabel* lbl = new QLabel(this); |
681 | 45 |
lbl->setPixmap(QPixmap::fromImage(getAmmoImage(iconNum))); |
639 | 46 |
lbl->setMaximumWidth(30); |
47 |
lbl->setGeometry(0, 0, 30, 30); |
|
629 | 48 |
hbLayout->addWidget(lbl); |
639 | 49 |
|
681 | 50 |
item=new WeaponItem(QImage(":/res/hh25x25.png"), this); |
51 |
item->setItemsNum(wNum); |
|
640 | 52 |
item->setInfinityState(true); |
629 | 53 |
hbLayout->addWidget(item); |
639 | 54 |
|
55 |
hbLayout->setStretchFactor(lbl, 1); |
|
56 |
hbLayout->setStretchFactor(item, 99); |
|
644 | 57 |
hbLayout->setAlignment(lbl, Qt::AlignLeft | Qt::AlignVCenter); |
58 |
hbLayout->setAlignment(item, Qt::AlignLeft | Qt::AlignVCenter); |
|
629 | 59 |
} |
612
333d095319de
abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
598
diff
changeset
|
60 |
|
683 | 61 |
void SelWeaponItem::setItemsNum(const unsigned char num) |
62 |
{ |
|
63 |
item->setItemsNum(num); |
|
64 |
} |
|
65 |
||
681 | 66 |
unsigned char SelWeaponItem::getItemsNum() const |
67 |
{ |
|
68 |
return item->getItemsNum(); |
|
69 |
} |
|
70 |
||
683 | 71 |
SelWeaponWidget::SelWeaponWidget(int numItems, QWidget* parent) : |
72 |
m_numItems(numItems), |
|
73 |
QWidget(parent) |
|
612
333d095319de
abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
598
diff
changeset
|
74 |
{ |
694 | 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 |
||
683 | 80 |
currentState=cDefaultAmmoStore->mid(10); |
81 |
||
624 | 82 |
pLayout=new QGridLayout(this); |
644 | 83 |
pLayout->setSpacing(1); |
84 |
pLayout->setMargin(1); |
|
629 | 85 |
|
86 |
int j=-1; |
|
694 | 87 |
int i=0, k=0; |
88 |
for(; i<m_numItems; ++i) { |
|
681 | 89 |
if(i==6) continue; |
90 |
if (k%4==0) ++j; |
|
683 | 91 |
weaponItems[i]=new SelWeaponItem(i, currentState[i].digitValue(), this); |
681 | 92 |
pLayout->addWidget(weaponItems[i], j, k%4); |
93 |
++k; |
|
629 | 94 |
} |
694 | 95 |
|
96 |
m_name = new QLineEdit(this); |
|
97 |
pLayout->addWidget(m_name, i, 0, 1, 4); |
|
612
333d095319de
abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
598
diff
changeset
|
98 |
} |
681 | 99 |
|
694 | 100 |
void SelWeaponWidget::setWeapons(const QString& ammo) |
683 | 101 |
{ |
102 |
for(int i=0; i<m_numItems; ++i) { |
|
103 |
twi::iterator it=weaponItems.find(i); |
|
104 |
if (it==weaponItems.end()) continue; |
|
105 |
it->second->setItemsNum(ammo[i].digitValue()); |
|
106 |
} |
|
107 |
update(); |
|
108 |
} |
|
109 |
||
110 |
void SelWeaponWidget::setDefault() |
|
111 |
{ |
|
112 |
setWeapons(cDefaultAmmoStore->mid(10)); |
|
113 |
} |
|
114 |
||
115 |
void SelWeaponWidget::save() |
|
116 |
{ |
|
694 | 117 |
if (m_name->text()=="") return; |
683 | 118 |
currentState=""; |
119 |
for(int i=0; i<m_numItems; ++i) { |
|
120 |
twi::const_iterator it=weaponItems.find(i); |
|
121 |
int num = it==weaponItems.end() ? 9 : (*this)[i]; |
|
122 |
currentState = QString("%1%2").arg(currentState).arg(num); |
|
123 |
} |
|
694 | 124 |
wconf->setValue(m_name->text(), currentState); |
695 | 125 |
emit weaponsChanged(); |
683 | 126 |
} |
127 |
||
681 | 128 |
int SelWeaponWidget::operator [] (unsigned int weaponIndex) const |
129 |
{ |
|
683 | 130 |
twi::const_iterator it=weaponItems.find(weaponIndex); |
681 | 131 |
return it==weaponItems.end() ? 9 : it->second->getItemsNum(); |
132 |
} |
|
133 |
||
134 |
QString SelWeaponWidget::getWeaponsString() const |
|
135 |
{ |
|
683 | 136 |
return currentState; |
681 | 137 |
} |
694 | 138 |
|
696 | 139 |
QString SelWeaponWidget::getWeaponsString(const QString& name) const |
140 |
{ |
|
141 |
return wconf->value(name).toString(); |
|
142 |
} |
|
143 |
||
694 | 144 |
void SelWeaponWidget::setWeaponsName(const QString& name) |
145 |
{ |
|
146 |
if(name!="" && wconf->contains(name)) { |
|
147 |
setWeapons(wconf->value(name).toString()); |
|
148 |
} |
|
149 |
||
150 |
curWeaponsName=name; |
|
151 |
m_name->setText(name); |
|
152 |
} |
|
153 |
||
154 |
QStringList SelWeaponWidget::getWeaponNames() const |
|
155 |
{ |
|
156 |
return wconf->allKeys(); |
|
157 |
} |