QTfrontend/selectWeapon.cpp
author unc0rr
Sat, 18 Oct 2008 17:01:56 +0000
changeset 1379 39cfee7a5be5
parent 1348 0eb0b3a955b8
child 1455 7d0a4ecd8210
permissions -rw-r--r--
Fix formatting
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
612
333d095319de abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents: 598
diff changeset
     1
/*
1066
1f1b3686a2b0 Update copyright headers a bit
unc0rr
parents: 1004
diff changeset
     2
 * Hedgewars, a free turn based strategy game
883
07a568ba44e0 Update copyright info in source files headers
unc0rr
parents: 725
diff changeset
     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
e7673b036db5 weaponItem added
displacer
parents: 612
diff changeset
    18
 
e7673b036db5 weaponItem added
displacer
parents: 612
diff changeset
    19
#include "selectWeapon.h"
629
fb03a39a10ff more icons at weapon select
displacer
parents: 624
diff changeset
    20
#include "weaponItem.h"
681
7a20c50988ec working weapons select
displacer
parents: 644
diff changeset
    21
#include "hwconsts.h"
612
333d095319de abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents: 598
diff changeset
    22
624
e7673b036db5 weaponItem added
displacer
parents: 612
diff changeset
    23
#include <QPushButton>
e7673b036db5 weaponItem added
displacer
parents: 612
diff changeset
    24
#include <QGridLayout>
629
fb03a39a10ff more icons at weapon select
displacer
parents: 624
diff changeset
    25
#include <QHBoxLayout>
fb03a39a10ff more icons at weapon select
displacer
parents: 624
diff changeset
    26
#include <QLabel>
630
38338573e09a mask applied
displacer
parents: 629
diff changeset
    27
#include <QBitmap>
694
436045756181 working save weapons to file
displacer
parents: 683
diff changeset
    28
#include <QLineEdit>
436045756181 working save weapons to file
displacer
parents: 683
diff changeset
    29
#include <QSettings>
724
21706280d913 delete weapon set should work fine now
displacer
parents: 723
diff changeset
    30
#include <QMessageBox>
624
e7673b036db5 weaponItem added
displacer
parents: 612
diff changeset
    31
629
fb03a39a10ff more icons at weapon select
displacer
parents: 624
diff changeset
    32
QImage getAmmoImage(int num)
fb03a39a10ff more icons at weapon select
displacer
parents: 624
diff changeset
    33
{
fb03a39a10ff more icons at weapon select
displacer
parents: 624
diff changeset
    34
  static QImage ammo(":Ammos.png");
fb03a39a10ff more icons at weapon select
displacer
parents: 624
diff changeset
    35
  return ammo.copy(0, num*32, 32, 32);
fb03a39a10ff more icons at weapon select
displacer
parents: 624
diff changeset
    36
}
fb03a39a10ff more icons at weapon select
displacer
parents: 624
diff changeset
    37
681
7a20c50988ec working weapons select
displacer
parents: 644
diff changeset
    38
SelWeaponItem::SelWeaponItem(int iconNum, int wNum, QWidget* parent) :
629
fb03a39a10ff more icons at weapon select
displacer
parents: 624
diff changeset
    39
  QWidget(parent)
fb03a39a10ff more icons at weapon select
displacer
parents: 624
diff changeset
    40
{
fb03a39a10ff more icons at weapon select
displacer
parents: 624
diff changeset
    41
  QHBoxLayout* hbLayout = new QHBoxLayout(this);
644
45454744f240 some more layout tune
displacer
parents: 643
diff changeset
    42
  hbLayout->setSpacing(1);
45454744f240 some more layout tune
displacer
parents: 643
diff changeset
    43
  hbLayout->setMargin(1);
629
fb03a39a10ff more icons at weapon select
displacer
parents: 624
diff changeset
    44
  
639
69d7ff3ab6f0 some layout tune
displacer
parents: 631
diff changeset
    45
  QLabel* lbl = new QLabel(this);
681
7a20c50988ec working weapons select
displacer
parents: 644
diff changeset
    46
  lbl->setPixmap(QPixmap::fromImage(getAmmoImage(iconNum)));
639
69d7ff3ab6f0 some layout tune
displacer
parents: 631
diff changeset
    47
  lbl->setMaximumWidth(30);
69d7ff3ab6f0 some layout tune
displacer
parents: 631
diff changeset
    48
  lbl->setGeometry(0, 0, 30, 30);
629
fb03a39a10ff more icons at weapon select
displacer
parents: 624
diff changeset
    49
  hbLayout->addWidget(lbl);
639
69d7ff3ab6f0 some layout tune
displacer
parents: 631
diff changeset
    50
719
bea32693bd7a - Fix layout on options page
unc0rr
parents: 718
diff changeset
    51
  item=new WeaponItem(QImage(":/res/ammopic.png"), this);
681
7a20c50988ec working weapons select
displacer
parents: 644
diff changeset
    52
  item->setItemsNum(wNum);
640
e69b651bb336 first buggy infinity state for weapons
displacer
parents: 639
diff changeset
    53
  item->setInfinityState(true);
629
fb03a39a10ff more icons at weapon select
displacer
parents: 624
diff changeset
    54
  hbLayout->addWidget(item);
639
69d7ff3ab6f0 some layout tune
displacer
parents: 631
diff changeset
    55
69d7ff3ab6f0 some layout tune
displacer
parents: 631
diff changeset
    56
  hbLayout->setStretchFactor(lbl, 1);
69d7ff3ab6f0 some layout tune
displacer
parents: 631
diff changeset
    57
  hbLayout->setStretchFactor(item, 99);
644
45454744f240 some more layout tune
displacer
parents: 643
diff changeset
    58
  hbLayout->setAlignment(lbl, Qt::AlignLeft | Qt::AlignVCenter);
45454744f240 some more layout tune
displacer
parents: 643
diff changeset
    59
  hbLayout->setAlignment(item, Qt::AlignLeft | Qt::AlignVCenter);
629
fb03a39a10ff more icons at weapon select
displacer
parents: 624
diff changeset
    60
}
612
333d095319de abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents: 598
diff changeset
    61
683
57d624f71e65 select default and save weapons added
displacer
parents: 682
diff changeset
    62
void SelWeaponItem::setItemsNum(const unsigned char num)
57d624f71e65 select default and save weapons added
displacer
parents: 682
diff changeset
    63
{
57d624f71e65 select default and save weapons added
displacer
parents: 682
diff changeset
    64
  item->setItemsNum(num);
57d624f71e65 select default and save weapons added
displacer
parents: 682
diff changeset
    65
}
57d624f71e65 select default and save weapons added
displacer
parents: 682
diff changeset
    66
681
7a20c50988ec working weapons select
displacer
parents: 644
diff changeset
    67
unsigned char SelWeaponItem::getItemsNum() const
7a20c50988ec working weapons select
displacer
parents: 644
diff changeset
    68
{
7a20c50988ec working weapons select
displacer
parents: 644
diff changeset
    69
  return item->getItemsNum();
7a20c50988ec working weapons select
displacer
parents: 644
diff changeset
    70
}
7a20c50988ec working weapons select
displacer
parents: 644
diff changeset
    71
683
57d624f71e65 select default and save weapons added
displacer
parents: 682
diff changeset
    72
SelWeaponWidget::SelWeaponWidget(int numItems, QWidget* parent) :
57d624f71e65 select default and save weapons added
displacer
parents: 682
diff changeset
    73
  m_numItems(numItems),
57d624f71e65 select default and save weapons added
displacer
parents: 682
diff changeset
    74
  QWidget(parent)
612
333d095319de abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents: 598
diff changeset
    75
{
694
436045756181 working save weapons to file
displacer
parents: 683
diff changeset
    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
436045756181 working save weapons to file
displacer
parents: 683
diff changeset
    81
    wconf->setValue("Default", cDefaultAmmoStore->mid(10));
436045756181 working save weapons to file
displacer
parents: 683
diff changeset
    82
  }
436045756181 working save weapons to file
displacer
parents: 683
diff changeset
    83
683
57d624f71e65 select default and save weapons added
displacer
parents: 682
diff changeset
    84
  currentState=cDefaultAmmoStore->mid(10);
57d624f71e65 select default and save weapons added
displacer
parents: 682
diff changeset
    85
624
e7673b036db5 weaponItem added
displacer
parents: 612
diff changeset
    86
  pLayout=new QGridLayout(this);
644
45454744f240 some more layout tune
displacer
parents: 643
diff changeset
    87
  pLayout->setSpacing(1);
45454744f240 some more layout tune
displacer
parents: 643
diff changeset
    88
  pLayout->setMargin(1);
629
fb03a39a10ff more icons at weapon select
displacer
parents: 624
diff changeset
    89
fb03a39a10ff more icons at weapon select
displacer
parents: 624
diff changeset
    90
  int j=-1;
694
436045756181 working save weapons to file
displacer
parents: 683
diff changeset
    91
  int i=0, k=0;
436045756181 working save weapons to file
displacer
parents: 683
diff changeset
    92
  for(; i<m_numItems; ++i) {
681
7a20c50988ec working weapons select
displacer
parents: 644
diff changeset
    93
    if(i==6) continue;
7a20c50988ec working weapons select
displacer
parents: 644
diff changeset
    94
    if (k%4==0) ++j;
683
57d624f71e65 select default and save weapons added
displacer
parents: 682
diff changeset
    95
    weaponItems[i]=new SelWeaponItem(i, currentState[i].digitValue(), this);
681
7a20c50988ec working weapons select
displacer
parents: 644
diff changeset
    96
    pLayout->addWidget(weaponItems[i], j, k%4);
7a20c50988ec working weapons select
displacer
parents: 644
diff changeset
    97
    ++k;
629
fb03a39a10ff more icons at weapon select
displacer
parents: 624
diff changeset
    98
  }
694
436045756181 working save weapons to file
displacer
parents: 683
diff changeset
    99
723
35f7bdb93268 Switch back to SelWeaponWidgets everywhere
unc0rr
parents: 719
diff changeset
   100
  //pLayout->setRowStretch(5, 100);
694
436045756181 working save weapons to file
displacer
parents: 683
diff changeset
   101
  m_name = new QLineEdit(this);
719
bea32693bd7a - Fix layout on options page
unc0rr
parents: 718
diff changeset
   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
7a20c50988ec working weapons select
displacer
parents: 644
diff changeset
   104
694
436045756181 working save weapons to file
displacer
parents: 683
diff changeset
   105
void SelWeaponWidget::setWeapons(const QString& ammo)
683
57d624f71e65 select default and save weapons added
displacer
parents: 682
diff changeset
   106
{
57d624f71e65 select default and save weapons added
displacer
parents: 682
diff changeset
   107
  for(int i=0; i<m_numItems; ++i) {
57d624f71e65 select default and save weapons added
displacer
parents: 682
diff changeset
   108
    twi::iterator it=weaponItems.find(i);
57d624f71e65 select default and save weapons added
displacer
parents: 682
diff changeset
   109
    if (it==weaponItems.end()) continue;
57d624f71e65 select default and save weapons added
displacer
parents: 682
diff changeset
   110
    it->second->setItemsNum(ammo[i].digitValue());
57d624f71e65 select default and save weapons added
displacer
parents: 682
diff changeset
   111
  }
57d624f71e65 select default and save weapons added
displacer
parents: 682
diff changeset
   112
  update();
57d624f71e65 select default and save weapons added
displacer
parents: 682
diff changeset
   113
}
57d624f71e65 select default and save weapons added
displacer
parents: 682
diff changeset
   114
57d624f71e65 select default and save weapons added
displacer
parents: 682
diff changeset
   115
void SelWeaponWidget::setDefault()
57d624f71e65 select default and save weapons added
displacer
parents: 682
diff changeset
   116
{
57d624f71e65 select default and save weapons added
displacer
parents: 682
diff changeset
   117
  setWeapons(cDefaultAmmoStore->mid(10));
57d624f71e65 select default and save weapons added
displacer
parents: 682
diff changeset
   118
}
57d624f71e65 select default and save weapons added
displacer
parents: 682
diff changeset
   119
57d624f71e65 select default and save weapons added
displacer
parents: 682
diff changeset
   120
void SelWeaponWidget::save()
57d624f71e65 select default and save weapons added
displacer
parents: 682
diff changeset
   121
{
725
a7a8eef5bbb6 can not save Default weapon set
displacer
parents: 724
diff changeset
   122
  if (m_name->text()=="Default") {
a7a8eef5bbb6 can not save Default weapon set
displacer
parents: 724
diff changeset
   123
    QMessageBox impossible(QMessageBox::Warning, QMessageBox::tr("Weapons"), QMessageBox::tr("Can not edit default weapon set"));
a7a8eef5bbb6 can not save Default weapon set
displacer
parents: 724
diff changeset
   124
    impossible.exec();
a7a8eef5bbb6 can not save Default weapon set
displacer
parents: 724
diff changeset
   125
    return;
a7a8eef5bbb6 can not save Default weapon set
displacer
parents: 724
diff changeset
   126
  }
694
436045756181 working save weapons to file
displacer
parents: 683
diff changeset
   127
  if (m_name->text()=="") return;
683
57d624f71e65 select default and save weapons added
displacer
parents: 682
diff changeset
   128
  currentState="";
57d624f71e65 select default and save weapons added
displacer
parents: 682
diff changeset
   129
  for(int i=0; i<m_numItems; ++i) {
57d624f71e65 select default and save weapons added
displacer
parents: 682
diff changeset
   130
    twi::const_iterator it=weaponItems.find(i);
57d624f71e65 select default and save weapons added
displacer
parents: 682
diff changeset
   131
    int num = it==weaponItems.end() ? 9 : (*this)[i];
57d624f71e65 select default and save weapons added
displacer
parents: 682
diff changeset
   132
    currentState = QString("%1%2").arg(currentState).arg(num);
57d624f71e65 select default and save weapons added
displacer
parents: 682
diff changeset
   133
  }
717
490dc8bb5b87 edit weapons is really edit now
displacer
parents: 696
diff changeset
   134
  if (curWeaponsName!="") {
490dc8bb5b87 edit weapons is really edit now
displacer
parents: 696
diff changeset
   135
    // remove old entry
490dc8bb5b87 edit weapons is really edit now
displacer
parents: 696
diff changeset
   136
    wconf->remove(curWeaponsName);
490dc8bb5b87 edit weapons is really edit now
displacer
parents: 696
diff changeset
   137
  }
694
436045756181 working save weapons to file
displacer
parents: 683
diff changeset
   138
  wconf->setValue(m_name->text(), currentState);
695
34a93d123712 update weapons combo fixed
displacer
parents: 694
diff changeset
   139
  emit weaponsChanged();
683
57d624f71e65 select default and save weapons added
displacer
parents: 682
diff changeset
   140
}
57d624f71e65 select default and save weapons added
displacer
parents: 682
diff changeset
   141
681
7a20c50988ec working weapons select
displacer
parents: 644
diff changeset
   142
int SelWeaponWidget::operator [] (unsigned int weaponIndex) const
7a20c50988ec working weapons select
displacer
parents: 644
diff changeset
   143
{
683
57d624f71e65 select default and save weapons added
displacer
parents: 682
diff changeset
   144
  twi::const_iterator it=weaponItems.find(weaponIndex);
681
7a20c50988ec working weapons select
displacer
parents: 644
diff changeset
   145
  return it==weaponItems.end() ? 9 : it->second->getItemsNum();
7a20c50988ec working weapons select
displacer
parents: 644
diff changeset
   146
}
7a20c50988ec working weapons select
displacer
parents: 644
diff changeset
   147
696
d6f32ed6edc8 working multiplayer weapons combo
displacer
parents: 695
diff changeset
   148
QString SelWeaponWidget::getWeaponsString(const QString& name) const
d6f32ed6edc8 working multiplayer weapons combo
displacer
parents: 695
diff changeset
   149
{
d6f32ed6edc8 working multiplayer weapons combo
displacer
parents: 695
diff changeset
   150
  return wconf->value(name).toString();
d6f32ed6edc8 working multiplayer weapons combo
displacer
parents: 695
diff changeset
   151
}
d6f32ed6edc8 working multiplayer weapons combo
displacer
parents: 695
diff changeset
   152
718
f93a38d2c982 delete weapon button added
displacer
parents: 717
diff changeset
   153
void SelWeaponWidget::deleteWeaponsName()
f93a38d2c982 delete weapon button added
displacer
parents: 717
diff changeset
   154
{
f93a38d2c982 delete weapon button added
displacer
parents: 717
diff changeset
   155
  if (curWeaponsName=="") return;
724
21706280d913 delete weapon set should work fine now
displacer
parents: 723
diff changeset
   156
21706280d913 delete weapon set should work fine now
displacer
parents: 723
diff changeset
   157
  if (curWeaponsName=="Default") {
21706280d913 delete weapon set should work fine now
displacer
parents: 723
diff changeset
   158
    QMessageBox impossible(QMessageBox::Warning, QMessageBox::tr("Weapons"), QMessageBox::tr("Can not delete default weapon set"));
21706280d913 delete weapon set should work fine now
displacer
parents: 723
diff changeset
   159
    impossible.exec();
21706280d913 delete weapon set should work fine now
displacer
parents: 723
diff changeset
   160
    return;
21706280d913 delete weapon set should work fine now
displacer
parents: 723
diff changeset
   161
  }
21706280d913 delete weapon set should work fine now
displacer
parents: 723
diff changeset
   162
21706280d913 delete weapon set should work fine now
displacer
parents: 723
diff changeset
   163
  QMessageBox reallyDelete(QMessageBox::Question, QMessageBox::tr("Weapons"), QMessageBox::tr("Really delete this weapon set?"),
21706280d913 delete weapon set should work fine now
displacer
parents: 723
diff changeset
   164
			   QMessageBox::Ok | QMessageBox::Cancel);
21706280d913 delete weapon set should work fine now
displacer
parents: 723
diff changeset
   165
  
21706280d913 delete weapon set should work fine now
displacer
parents: 723
diff changeset
   166
  if (reallyDelete.exec()==QMessageBox::Ok) {
21706280d913 delete weapon set should work fine now
displacer
parents: 723
diff changeset
   167
    wconf->remove(curWeaponsName);
21706280d913 delete weapon set should work fine now
displacer
parents: 723
diff changeset
   168
    emit weaponsDeleted();
21706280d913 delete weapon set should work fine now
displacer
parents: 723
diff changeset
   169
  }
718
f93a38d2c982 delete weapon button added
displacer
parents: 717
diff changeset
   170
}
f93a38d2c982 delete weapon button added
displacer
parents: 717
diff changeset
   171
717
490dc8bb5b87 edit weapons is really edit now
displacer
parents: 696
diff changeset
   172
void SelWeaponWidget::setWeaponsName(const QString& name, bool editMode)
694
436045756181 working save weapons to file
displacer
parents: 683
diff changeset
   173
{
436045756181 working save weapons to file
displacer
parents: 683
diff changeset
   174
  if(name!="" && wconf->contains(name)) {
436045756181 working save weapons to file
displacer
parents: 683
diff changeset
   175
    setWeapons(wconf->value(name).toString());
436045756181 working save weapons to file
displacer
parents: 683
diff changeset
   176
  }
436045756181 working save weapons to file
displacer
parents: 683
diff changeset
   177
717
490dc8bb5b87 edit weapons is really edit now
displacer
parents: 696
diff changeset
   178
  if(editMode) curWeaponsName=name;
490dc8bb5b87 edit weapons is really edit now
displacer
parents: 696
diff changeset
   179
  else curWeaponsName="";
490dc8bb5b87 edit weapons is really edit now
displacer
parents: 696
diff changeset
   180
694
436045756181 working save weapons to file
displacer
parents: 683
diff changeset
   181
  m_name->setText(name);
436045756181 working save weapons to file
displacer
parents: 683
diff changeset
   182
}
436045756181 working save weapons to file
displacer
parents: 683
diff changeset
   183
436045756181 working save weapons to file
displacer
parents: 683
diff changeset
   184
QStringList SelWeaponWidget::getWeaponNames() const
436045756181 working save weapons to file
displacer
parents: 683
diff changeset
   185
{
436045756181 working save weapons to file
displacer
parents: 683
diff changeset
   186
  return wconf->allKeys();
436045756181 working save weapons to file
displacer
parents: 683
diff changeset
   187
}