|
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 <QResizeEvent> |
|
21 #include <QCoreApplication> |
|
22 #include <QPalette> |
|
23 |
|
24 #include "frameTeam.h" |
|
25 #include "teamselhelper.h" |
|
26 #include "hwconsts.h" |
|
27 |
|
28 FrameTeams::FrameTeams(QWidget* parent) : |
|
29 QFrame(parent), maxHedgehogsPerGame(48), overallHedgehogs(0), mainLayout(this), nonInteractive(false) |
|
30 { |
|
31 QPalette newPalette = palette(); |
|
32 newPalette.setColor(QPalette::Window, QColor(0x00, 0x00, 0x00)); |
|
33 setPalette(newPalette); |
|
34 setAutoFillBackground(true); |
|
35 |
|
36 mainLayout.setSpacing(1); |
|
37 mainLayout.setContentsMargins(4, 4, 4, 4); |
|
38 |
|
39 int i = 0; |
|
40 while(colors[i] != 0) |
|
41 availableColors.push_back(QColor(colors[i++])); |
|
42 |
|
43 resetColors(); |
|
44 } |
|
45 |
|
46 void FrameTeams::setInteractivity(bool interactive) |
|
47 { |
|
48 nonInteractive = !interactive; |
|
49 for(tmapTeamToWidget::iterator it=teamToWidget.begin(); it!=teamToWidget.end(); ++it) { |
|
50 TeamShowWidget* pts = dynamic_cast<TeamShowWidget*>(it.value()); |
|
51 if(!pts) throw; |
|
52 pts->setInteractivity(interactive); |
|
53 } |
|
54 } |
|
55 |
|
56 void FrameTeams::resetColors() |
|
57 { |
|
58 currentColor=availableColors.end() - 1; // ensure next color is the first one |
|
59 } |
|
60 |
|
61 QColor FrameTeams::getNextColor() const |
|
62 { |
|
63 QList<QColor>::ConstIterator nextColor=currentColor; |
|
64 ++nextColor; |
|
65 if (nextColor==availableColors.end()) nextColor=availableColors.begin(); |
|
66 return *nextColor; |
|
67 } |
|
68 |
|
69 void FrameTeams::addTeam(HWTeam team, bool willPlay) |
|
70 { |
|
71 TeamShowWidget* pTeamShowWidget = new TeamShowWidget(team, willPlay, this); |
|
72 if(nonInteractive) pTeamShowWidget->setInteractivity(false); |
|
73 // int hght=teamToWidget.empty() ? 0 : teamToWidget.begin()->second->size().height(); |
|
74 mainLayout.addWidget(pTeamShowWidget); |
|
75 teamToWidget.insert(team, pTeamShowWidget); |
|
76 QResizeEvent* pevent=new QResizeEvent(parentWidget()->size(), parentWidget()->size()); |
|
77 QCoreApplication::postEvent(parentWidget(), pevent); |
|
78 } |
|
79 |
|
80 void FrameTeams::removeTeam(HWTeam team) |
|
81 { |
|
82 tmapTeamToWidget::iterator it=teamToWidget.find(team); |
|
83 if(it==teamToWidget.end()) return; |
|
84 mainLayout.removeWidget(it.value()); |
|
85 it.value()->deleteLater(); |
|
86 teamToWidget.erase(it); |
|
87 } |
|
88 |
|
89 void FrameTeams::resetTeams() |
|
90 { |
|
91 for(tmapTeamToWidget::iterator it=teamToWidget.begin(); it!=teamToWidget.end(); ) { |
|
92 mainLayout.removeWidget(it.value()); |
|
93 it.value()->deleteLater(); |
|
94 teamToWidget.erase(it++); |
|
95 } |
|
96 } |
|
97 |
|
98 void FrameTeams::setHHNum(const HWTeam& team) |
|
99 { |
|
100 TeamShowWidget* pTeamShowWidget = dynamic_cast<TeamShowWidget*>(getTeamWidget(team)); |
|
101 if(!pTeamShowWidget) return; |
|
102 pTeamShowWidget->setHHNum(team.numHedgehogs()); |
|
103 } |
|
104 |
|
105 void FrameTeams::setTeamColor(const HWTeam& team) |
|
106 { |
|
107 TeamShowWidget* pTeamShowWidget = dynamic_cast<TeamShowWidget*>(getTeamWidget(team)); |
|
108 if(!pTeamShowWidget) return; |
|
109 pTeamShowWidget->changeTeamColor(team.color()); |
|
110 } |
|
111 |
|
112 QWidget* FrameTeams::getTeamWidget(HWTeam team) |
|
113 { |
|
114 //qDebug() << "FrameTeams::getTeamWidget getNetID() = " << team.getNetID(); |
|
115 tmapTeamToWidget::iterator it=teamToWidget.find(team); |
|
116 QWidget* ret = it!=teamToWidget.end() ? it.value() : 0; |
|
117 return ret; |
|
118 } |
|
119 |
|
120 bool FrameTeams::isFullTeams() const |
|
121 { |
|
122 return overallHedgehogs==maxHedgehogsPerGame; |
|
123 } |
|
124 |
|
125 void FrameTeams::emitTeamColorChanged(const HWTeam& team) |
|
126 { |
|
127 emit teamColorChanged(team); |
|
128 } |