QTfrontend/teamselect.cpp
changeset 6060 fdfc01419815
parent 6059 ddf020d0941a
child 6061 15b4b485a1c5
equal deleted inserted replaced
6059:ddf020d0941a 6060:fdfc01419815
     1 /*
       
     2  * Hedgewars, a free turn based strategy game
       
     3  * Copyright (c) 2006-2007 Igor Ulyanov <iulyanov@gmail.com>
       
     4  * Copyright (c) 2007-2011 Andrey Korotaev <unC0Rr@gmail.com>
       
     5  *
       
     6  * This program is free software; you can redistribute it and/or modify
       
     7  * it under the terms of the GNU General Public License as published by
       
     8  * the Free Software Foundation; version 2 of the License
       
     9  *
       
    10  * This program is distributed in the hope that it will be useful,
       
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    13  * GNU General Public License for more details.
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License
       
    16  * along with this program; if not, write to the Free Software
       
    17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
       
    18  */
       
    19 
       
    20 #include <algorithm>
       
    21 
       
    22 #include <QLabel>
       
    23 #include <QPixmap>
       
    24 #include <QPushButton>
       
    25 #include <QFrame>
       
    26 #include <QDebug>
       
    27 
       
    28 #include "vertScrollArea.h"
       
    29 #include "teamselect.h"
       
    30 #include "teamselhelper.h"
       
    31 #include "frameTeam.h"
       
    32 
       
    33 void TeamSelWidget::addTeam(HWTeam team)
       
    34 {
       
    35   if(team.isNetTeam()) {
       
    36     framePlaying->addTeam(team, true);
       
    37     curPlayingTeams.push_back(team);
       
    38     connect(framePlaying->getTeamWidget(team), SIGNAL(teamStatusChanged(HWTeam)),
       
    39              this, SLOT(netTeamStatusChanged(const HWTeam&)));
       
    40     connect(framePlaying->getTeamWidget(team), SIGNAL(hhNmChanged(const HWTeam&)),
       
    41                 this, SLOT(hhNumChanged(const HWTeam&)));
       
    42     dynamic_cast<TeamShowWidget*>(framePlaying->getTeamWidget(team))->hhNumChanged();
       
    43     connect(framePlaying->getTeamWidget(team), SIGNAL(teamColorChanged(const HWTeam&)),
       
    44                 this, SLOT(proxyTeamColorChanged(const HWTeam&)));
       
    45   } else {
       
    46     frameDontPlaying->addTeam(team, false);
       
    47     m_curNotPlayingTeams.push_back(team);
       
    48     if(m_acceptOuter) {
       
    49       connect(frameDontPlaying->getTeamWidget(team), SIGNAL(teamStatusChanged(HWTeam)),
       
    50           this, SLOT(pre_changeTeamStatus(HWTeam)));
       
    51     } else {
       
    52       connect(frameDontPlaying->getTeamWidget(team), SIGNAL(teamStatusChanged(HWTeam)),
       
    53           this, SLOT(changeTeamStatus(HWTeam)));
       
    54     }
       
    55   }
       
    56   emit setEnabledGameStart(curPlayingTeams.size()>1);
       
    57 }
       
    58 
       
    59 void TeamSelWidget::setInteractivity(bool interactive)
       
    60 {
       
    61     framePlaying->setInteractivity(interactive);
       
    62 }
       
    63 
       
    64 void TeamSelWidget::hhNumChanged(const HWTeam& team)
       
    65 {
       
    66     QList<HWTeam>::iterator itPlay=std::find(curPlayingTeams.begin(), curPlayingTeams.end(), team);
       
    67     if(itPlay==curPlayingTeams.end())
       
    68     {
       
    69         qWarning() << QString("hhNumChanged: team '%1' not found").arg(team.name());
       
    70         return;
       
    71     }
       
    72     itPlay->setNumHedgehogs(team.numHedgehogs());
       
    73     emit hhogsNumChanged(team);
       
    74 }
       
    75 
       
    76 void TeamSelWidget::proxyTeamColorChanged(const HWTeam& team)
       
    77 {
       
    78     QList<HWTeam>::iterator itPlay=std::find(curPlayingTeams.begin(), curPlayingTeams.end(), team);
       
    79     if(itPlay==curPlayingTeams.end())
       
    80     {
       
    81         qWarning() << QString("proxyTeamColorChanged: team '%1' not found").arg(team.name());
       
    82         return;
       
    83     }
       
    84     itPlay->setColor(team.color());
       
    85     emit teamColorChanged(team);
       
    86 }
       
    87 
       
    88 void TeamSelWidget::changeHHNum(const HWTeam& team)
       
    89 {
       
    90   QList<HWTeam>::iterator itPlay=std::find(curPlayingTeams.begin(), curPlayingTeams.end(), team);
       
    91     if(itPlay==curPlayingTeams.end())
       
    92     {
       
    93         qWarning() << QString("changeHHNum: team '%1' not found").arg(team.name());
       
    94         return;
       
    95     }
       
    96   itPlay->setNumHedgehogs(team.numHedgehogs());
       
    97 
       
    98   framePlaying->setHHNum(team);
       
    99 }
       
   100 
       
   101 void TeamSelWidget::changeTeamColor(const HWTeam& team)
       
   102 {
       
   103     QList<HWTeam>::iterator itPlay=std::find(curPlayingTeams.begin(), curPlayingTeams.end(), team);
       
   104     if(itPlay==curPlayingTeams.end())
       
   105     {
       
   106         qWarning() << QString("changeTeamColor: team '%1' not found").arg(team.name());
       
   107         return;
       
   108     }
       
   109     itPlay->setColor(team.color());
       
   110 
       
   111     framePlaying->setTeamColor(team);
       
   112 }
       
   113 
       
   114 void TeamSelWidget::removeNetTeam(const HWTeam& team)
       
   115 {
       
   116     //qDebug() << QString("removeNetTeam: removing team '%1'").arg(team.TeamName);
       
   117     for(;;) {
       
   118         QList<HWTeam>::iterator itPlay=std::find(curPlayingTeams.begin(), curPlayingTeams.end(), team);
       
   119         if(itPlay==curPlayingTeams.end())
       
   120         {
       
   121             qWarning() << QString("removeNetTeam: team '%1' not found").arg(team.name());
       
   122             break;
       
   123         }
       
   124         if(itPlay->isNetTeam()) {
       
   125             QObject::disconnect(framePlaying->getTeamWidget(*itPlay), SIGNAL(teamStatusChanged(HWTeam)));
       
   126             framePlaying->removeTeam(team);
       
   127             curPlayingTeams.erase(itPlay);
       
   128             break;
       
   129         }
       
   130     }
       
   131     emit setEnabledGameStart(curPlayingTeams.size()>1);
       
   132 }
       
   133 
       
   134 void TeamSelWidget::netTeamStatusChanged(const HWTeam& team)
       
   135 {
       
   136   QList<HWTeam>::iterator itPlay=std::find(curPlayingTeams.begin(), curPlayingTeams.end(), team);
       
   137 
       
   138 }
       
   139 
       
   140 //void TeamSelWidget::removeTeam(__attribute__ ((unused)) HWTeam team)
       
   141 //{
       
   142   //curDontPlayingTeams.erase(std::find(curDontPlayingTeams.begin(), curDontPlayingTeams.end(), team));
       
   143 //}
       
   144 
       
   145 void TeamSelWidget::changeTeamStatus(HWTeam team)
       
   146 {
       
   147   QList<HWTeam>::iterator itDontPlay=std::find(m_curNotPlayingTeams.begin(), m_curNotPlayingTeams.end(), team);
       
   148   QList<HWTeam>::iterator itPlay=std::find(curPlayingTeams.begin(), curPlayingTeams.end(), team);
       
   149 
       
   150   bool willBePlaying=itDontPlay!=m_curNotPlayingTeams.end();
       
   151 
       
   152   if(!willBePlaying) {
       
   153     // playing team => dont playing
       
   154     m_curNotPlayingTeams.push_back(*itPlay);
       
   155     emit teamNotPlaying(*itPlay);
       
   156     curPlayingTeams.erase(itPlay);
       
   157   } else {
       
   158     // return if max playing teams reached
       
   159     if(framePlaying->isFullTeams()) return;
       
   160     // dont playing team => playing
       
   161     team=*itDontPlay; // for net team info saving in framePlaying (we have only name with netID from network)
       
   162     itDontPlay->setColor(framePlaying->getNextColor());
       
   163     curPlayingTeams.push_back(*itDontPlay);
       
   164     if(!m_acceptOuter) emit teamWillPlay(*itDontPlay);
       
   165     m_curNotPlayingTeams.erase(itDontPlay);
       
   166   }
       
   167 
       
   168   FrameTeams* pRemoveTeams;
       
   169   FrameTeams* pAddTeams;
       
   170   if(!willBePlaying) {
       
   171     pRemoveTeams=framePlaying;
       
   172     pAddTeams=frameDontPlaying;
       
   173   } else {
       
   174     pRemoveTeams=frameDontPlaying;
       
   175     pAddTeams=framePlaying;
       
   176   }
       
   177 
       
   178   pAddTeams->addTeam(team, willBePlaying);
       
   179   pRemoveTeams->removeTeam(team);
       
   180   if(!team.isNetTeam() && m_acceptOuter && !willBePlaying) {
       
   181     connect(frameDontPlaying->getTeamWidget(team), SIGNAL(teamStatusChanged(HWTeam)),
       
   182         this, SLOT(pre_changeTeamStatus(HWTeam)));
       
   183   } else {
       
   184     connect(pAddTeams->getTeamWidget(team), SIGNAL(teamStatusChanged(HWTeam)),
       
   185         this, SLOT(changeTeamStatus(HWTeam)));
       
   186   }
       
   187   if(willBePlaying) {
       
   188     connect(framePlaying->getTeamWidget(team), SIGNAL(hhNmChanged(const HWTeam&)),
       
   189         this, SLOT(hhNumChanged(const HWTeam&)));
       
   190     dynamic_cast<TeamShowWidget*>(framePlaying->getTeamWidget(team))->hhNumChanged();
       
   191     connect(framePlaying->getTeamWidget(team), SIGNAL(teamColorChanged(const HWTeam&)),
       
   192         this, SLOT(proxyTeamColorChanged(const HWTeam&)));
       
   193     emit teamColorChanged(((TeamShowWidget*)framePlaying->getTeamWidget(team))->getTeam());
       
   194   }
       
   195 
       
   196   QSize szh=pAddTeams->sizeHint();
       
   197   QSize szh1=pRemoveTeams->sizeHint();
       
   198   if(szh.isValid() && szh1.isValid()) {
       
   199     pAddTeams->resize(pAddTeams->size().width(), szh.height());
       
   200     pRemoveTeams->resize(pRemoveTeams->size().width(), szh1.height());
       
   201   }
       
   202 
       
   203   emit setEnabledGameStart(curPlayingTeams.size()>1);
       
   204 }
       
   205 
       
   206 void TeamSelWidget::addScrArea(FrameTeams* pfteams, QColor color, int fixedHeight)
       
   207 {
       
   208     VertScrArea* area = new VertScrArea(color);
       
   209     area->setWidget(pfteams);
       
   210     mainLayout.addWidget(area, 30);
       
   211     if (fixedHeight > 0)
       
   212     {
       
   213         area->setMinimumHeight(fixedHeight);
       
   214         area->setMaximumHeight(fixedHeight);
       
   215         area->setStyleSheet(
       
   216                 "FrameTeams{"
       
   217                     "border: solid;"
       
   218                     "border-width: 1px;"
       
   219                     "border-radius: 16px;"
       
   220                     "border-color: #ffcc00;"
       
   221                     "}"
       
   222         );
       
   223     }
       
   224 }
       
   225 
       
   226 TeamSelWidget::TeamSelWidget(QWidget* parent) :
       
   227   QGroupBox(parent), mainLayout(this), m_acceptOuter(false)
       
   228 {
       
   229     setTitle(QGroupBox::tr("Playing teams"));
       
   230     framePlaying = new FrameTeams();
       
   231     frameDontPlaying = new FrameTeams();
       
   232 
       
   233     QPalette p;
       
   234     p.setColor(QPalette::Window, QColor(0x00, 0x00, 0x00));
       
   235     addScrArea(framePlaying, p.color(QPalette::Window).light(105), 250);
       
   236     addScrArea(frameDontPlaying, p.color(QPalette::Window).dark(105), 0);
       
   237 }
       
   238 
       
   239 void TeamSelWidget::setAcceptOuter(bool acceptOuter)
       
   240 {
       
   241   m_acceptOuter=acceptOuter;
       
   242 }
       
   243 
       
   244 void TeamSelWidget::resetPlayingTeams(const QList<HWTeam>& teamslist)
       
   245 {
       
   246   //for(it=curPlayingTeams.begin(); it!=curPlayingTeams.end(); it++) {
       
   247   //framePlaying->removeTeam(*it);
       
   248   //}
       
   249   framePlaying->resetTeams();
       
   250   framePlaying->resetColors();
       
   251   curPlayingTeams.clear();
       
   252   //for(it=curDontPlayingTeams.begin(); it!=curDontPlayingTeams.end(); it++) {
       
   253   //frameDontPlaying->removeTeam(*it);
       
   254   //}
       
   255   frameDontPlaying->resetTeams();
       
   256   m_curNotPlayingTeams.clear();
       
   257 
       
   258   foreach(HWTeam team, teamslist)
       
   259     addTeam(team);
       
   260 }
       
   261 
       
   262 bool TeamSelWidget::isPlaying(HWTeam team) const
       
   263 {
       
   264   return std::find(curPlayingTeams.begin(), curPlayingTeams.end(), team)!=curPlayingTeams.end();
       
   265 }
       
   266 
       
   267 QList<HWTeam> TeamSelWidget::getPlayingTeams() const
       
   268 {
       
   269   return curPlayingTeams;
       
   270 }
       
   271 
       
   272 QList<HWTeam> TeamSelWidget::getNotPlayingTeams() const
       
   273 {
       
   274   return m_curNotPlayingTeams;
       
   275 }
       
   276 
       
   277 void TeamSelWidget::pre_changeTeamStatus(HWTeam team)
       
   278 {
       
   279   team.setColor(framePlaying->getNextColor());
       
   280   emit acceptRequested(team);
       
   281 }