QTfrontend/teamselect.cpp
author unc0rr
Thu, 26 Oct 2006 16:08:29 +0000
changeset 205 8d9aff55e6ab
parent 184 f97a7a3dc8f6
child 207 287077789132
permissions -rw-r--r--
- Set version to 0.8 - Check protocol version to ensure clients compatibility in further versions
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
184
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
     1
/*
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
     2
 * Hedgewars, a worms-like game
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
     3
 * Copyright (c) 2006 Ulyanov Igor <iulyanov@gmail.com>
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
     4
 *
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
     8
 *
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    12
 * GNU General Public License for more details.
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    13
 *
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    14
 * You should have received a copy of the GNU General Public License
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    15
 * along with this program; if not, write to the Free Software
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    17
 */
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    18
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    19
#include <QLabel>
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    20
#include <QPixmap>
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    21
#include <QPushButton>
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    22
#include <QFrame>
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    23
#include <QDebug>
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    24
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    25
#include <vertScrollArea.h>
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    26
#include "teamselect.h"
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    27
#include "teamselhelper.h"
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    28
#include "frameTeam.h"
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    29
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    30
void TeamSelWidget::addTeam(HWTeam team)
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    31
{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    32
  frameDontPlaying->addTeam(team, false);
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    33
  curDontPlayingTeams.push_back(team);
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    34
  QObject::connect(frameDontPlaying->getTeamWidget(team), SIGNAL(teamStatusChanged(HWTeam)),
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    35
		   this, SLOT(changeTeamStatus(HWTeam)));
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    36
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    37
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    38
//void TeamSelWidget::removeTeam(__attribute__ ((unused)) HWTeam team)
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    39
//{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    40
  //curDontPlayingTeams.erase(std::find(curDontPlayingTeams.begin(), curDontPlayingTeams.end(), team));
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    41
//}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    42
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    43
void TeamSelWidget::changeTeamStatus(HWTeam team)
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    44
{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    45
  list<HWTeam>::iterator itDontPlay=std::find(curDontPlayingTeams.begin(), curDontPlayingTeams.end(), team);
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    46
  list<HWTeam>::iterator itPlay=std::find(curPlayingTeams.begin(), curPlayingTeams.end(), team);
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    47
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    48
  bool willBePlaying=itDontPlay!=curDontPlayingTeams.end();
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    49
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    50
  if(!willBePlaying) {
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    51
    // playing team => dont playing
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    52
    curDontPlayingTeams.push_back(*itPlay);
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    53
    curPlayingTeams.erase(itPlay);
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    54
  } else {
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    55
    // return if max playing teams reached
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    56
    if(framePlaying->isFullTeams()) return;
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    57
    // dont playing team => playing
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    58
    curPlayingTeams.push_back(*itDontPlay);
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    59
    curDontPlayingTeams.erase(itDontPlay);
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    60
  }
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    61
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    62
  FrameTeams* pRemoveTeams;
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    63
  FrameTeams* pAddTeams;
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    64
  if(!willBePlaying) {
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    65
    pRemoveTeams=framePlaying;
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    66
    pAddTeams=frameDontPlaying;
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    67
  } else {
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    68
    pRemoveTeams=frameDontPlaying;
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    69
    pAddTeams=framePlaying;
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    70
  }
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    71
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    72
  pAddTeams->addTeam(team, willBePlaying);
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    73
  pRemoveTeams->removeTeam(team);
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    74
  QObject::connect(pAddTeams->getTeamWidget(team), SIGNAL(teamStatusChanged(HWTeam)),
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    75
		   this, SLOT(changeTeamStatus(HWTeam)));
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    76
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    77
  QSize szh=pAddTeams->sizeHint();
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    78
  QSize szh1=pRemoveTeams->sizeHint();
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    79
  if(szh.isValid() && szh1.isValid()) {
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    80
    pAddTeams->resize(pAddTeams->size().width(), szh.height());
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    81
    pRemoveTeams->resize(pRemoveTeams->size().width(), szh1.height());
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    82
  }
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    83
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    84
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    85
void TeamSelWidget::addScrArea(FrameTeams* pfteams, QColor color)
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    86
{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    87
  VertScrArea* area=new VertScrArea(color);
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    88
  area->setWidget(pfteams);
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    89
  mainLayout.addWidget(area, 30);
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    90
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    91
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    92
TeamSelWidget::TeamSelWidget(QWidget* parent) :
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    93
  QWidget(parent), mainLayout(this)
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    94
{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    95
  framePlaying=new FrameTeams();
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    96
  frameDontPlaying=new FrameTeams();
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    97
  addScrArea(framePlaying, QColor("DarkTurquoise"));
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    98
  addScrArea(frameDontPlaying, QColor("LightGoldenrodYellow"));
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
    99
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   100
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   101
void TeamSelWidget::resetPlayingTeams(const QStringList& teamslist)
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   102
{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   103
  list<HWTeam>::iterator it;
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   104
  for(it=curPlayingTeams.begin(); it!=curPlayingTeams.end(); it++) {
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   105
    framePlaying->removeTeam(*it);
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   106
  }
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   107
  curPlayingTeams.clear();
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   108
  for(it=curDontPlayingTeams.begin(); it!=curDontPlayingTeams.end(); it++) {
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   109
    frameDontPlaying->removeTeam(*it);
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   110
  }
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   111
  curDontPlayingTeams.clear();
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   112
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   113
  for (QStringList::ConstIterator it = teamslist.begin(); it != teamslist.end(); ++it ) {
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   114
    addTeam(*it);
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   115
  }
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   116
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   117
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   118
bool TeamSelWidget::isPlaying(HWTeam team) const
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   119
{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   120
  return std::find(curPlayingTeams.begin(), curPlayingTeams.end(), team)!=curPlayingTeams.end();
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   121
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   122
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   123
list<HWTeam> TeamSelWidget::getPlayingTeams() const
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   124
{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   125
  return curPlayingTeams;
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   126
}
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   127
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   128
unsigned char TeamSelWidget::numHedgedogs(HWTeam team) const
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   129
{
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   130
  const TeamShowWidget* tsw=dynamic_cast<TeamShowWidget*>(framePlaying->getTeamWidget(team));
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   131
  if(!tsw) return 0;
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   132
  return tsw->getHedgehogsNum();
f97a7a3dc8f6 - Update more headers
unc0rr
parents: 183
diff changeset
   133
}