]> ruin.nu Git - hbs.git/blob - bs/fleetview.cpp
started to work on the battle report.. almost there =)
[hbs.git] / bs / fleetview.cpp
1 /***************************************************************************
2                           fleetview.cpp  -  description
3                              -------------------
4     begin                : Sun Mar 10 2002
5     copyright            : (C) 2002 by Michael Andreen
6     email                : whale@linux.nu
7  ***************************************************************************/
8
9 /***************************************************************************
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the GNU General Public License as published by  *
13  *   the Free Software Foundation; either version 2 of the License, or     *
14  *   (at your option) any later version.                                   *
15  *                                                                         *
16  ***************************************************************************/
17
18 #include "fleetview.h"
19
20 #include "ui/roidseditview.h"
21 #include "ui/resourceview.h"
22 #include "planet.h"
23
24 #include <qvalidator.h>
25
26 #include <iostream>
27 #include <map>
28
29 using namespace std;
30
31 FleetView::FleetView(const Fleet* fleet, bool friendly, QWidget *parent, const char *name ) : QWidget(parent,name)
32 {
33         m_iMinTick = 0;
34         m_iMaxTick = 0;
35         m_iResultMode = 0;
36
37         m_Modified = false;
38         const Planet* planet = 0;
39         if((planet = dynamic_cast<const Planet*>(fleet)))
40         {
41                         m_Fleet = new Planet(*planet);
42                         m_bHome = true;
43         }
44         else
45         {
46                 m_Fleet = new Fleet(*fleet);
47                 m_bHome = false;
48         }
49         
50         m_Fleet->printFleet();
51         m_bFriendly = friendly;
52
53         //m_ChangesMapper = new QSignalMapper(this);
54
55         
56         findNames();
57         int rows = m_Names.size() / 3 + m_Names.size() % 3;
58         m_MainLayout = new QVBoxLayout(this,2);
59         m_MainLayout->addSpacing(2);
60         m_UnitsLayout = new QGridLayout(m_MainLayout, rows, 6);
61         buildUnitTable();
62         //QSpacerItem* space = new QSpacerItem(1, 1);
63         
64
65         m_MainLayout->addStretch(10);
66
67         if (m_bHome)
68         {
69                 m_RoidsEditView = new RoidsEditView(this);
70                 m_MainLayout->addWidget(m_RoidsEditView);
71                 connect(m_RoidsEditView, SIGNAL(changed(const QString&, int)), SLOT(slotRoidsChanged(const QString&, int)));
72         }
73         m_ResourceView = new ResourceView(this);
74         m_MainLayout->addWidget(m_ResourceView);
75         fillTable();
76 }
77
78 FleetView::~FleetView(){
79 }
80
81 /////////////////////////////////////////////////////////////////////
82 //
83 void FleetView::viewFleet(const Fleet* fleet, bool friendly)
84 {
85         friendly = true;
86         if (m_Fleet->race() != fleet->race())
87         {
88                 findNames();
89         }
90         delete m_Fleet;
91         if(m_bHome)
92         {
93                 m_Fleet = new Planet(*dynamic_cast<const Planet*>(fleet));
94         }
95         else
96         {
97                 m_Fleet = new Fleet(*fleet);
98         }
99         //m_Fleet->printFleet();
100         fillTable();
101 }
102
103 /////////////////////////////////////////////////////////////////////
104 //
105 void FleetView::findNames()
106 {
107         m_Names.clear();
108
109         const UnitList& units = Fleet::Units(); 
110         vector<int> races = m_Fleet->RacesAllowed();
111
112         for (UnitList::const_iterator i = units.begin(); i != units.end(); ++i)
113         {
114                 for (vector<int>::iterator j = races.begin(); j != races.end(); ++j)
115                 {
116                         if ((*j) == i->second.race())
117                         {
118                                 m_Names.push_back(i->first.c_str());
119                                 break;
120                         }
121                 }
122         }
123 }
124
125 /////////////////////////////////////////////////////////////////////
126 //
127 void FleetView::buildUnitTable()
128 {
129         int rows = 0;
130         QIntValidator* val = new QIntValidator(this);
131         val->setBottom(0);
132
133         for (vector<QString>::iterator i = m_Names.begin(); i != m_Names.end(); ++rows)
134         {
135                 for ( int cols = 0; i != m_Names.end() && cols < 3; ++i, ++cols)
136                 {
137                         m_UnitsLabel[(*i)] = new QLabel((*i), this);
138                         m_UnitsLayout->addWidget(m_UnitsLabel[(*i)], rows, cols*3);
139
140                         m_UnitsEdit[(*i)] = new QLineEdit(this,(*i));
141       m_UnitsEdit[(*i)]->setValidator(val);
142                         m_UnitsLayout->addWidget(m_UnitsEdit[(*i)], rows, cols*3+1);
143
144                         m_UnitsLostSurvivedView[(*i)] = new QLineEdit(this);
145                         m_UnitsLostSurvivedView[(*i)]->setFocusPolicy(QWidget::NoFocus);
146                         m_UnitsLostSurvivedView[(*i)]->setReadOnly(true);
147                         m_UnitsLayout->addWidget(m_UnitsLostSurvivedView[(*i)], rows, cols*3+2);
148
149                         connect(m_UnitsEdit[(*i)], SIGNAL(textChanged(const QString&)),this, SLOT(unitsChanged(const QString&)));
150                 }
151         }
152
153 }
154
155 /////////////////////////////////////////////////////////////////////
156 //
157 const Fleet* FleetView::fleet()
158 {
159         return m_Fleet;
160 }
161
162 /////////////////////////////////////////////////////////////////////
163 //
164 bool FleetView::isHome()
165 {
166         return m_bHome;
167 }
168
169 /////////////////////////////////////////////////////////////////////
170 //
171 bool FleetView::isFriendly()
172 {
173         return m_bFriendly;
174 }
175
176 /////////////////////////////////////////////////////////////////////
177 //
178 void FleetView::unitsChanged(const QString& number)
179 {
180         m_Fleet->setFleet(sender()->name(), number.toInt());
181         m_Modified = true;
182         emit fleetChanged(m_Fleet);
183 }
184
185 /////////////////////////////////////////////////////////////////////
186 //
187 bool FleetView::isModified()
188 {
189         return m_Modified;
190 }
191
192 /////////////////////////////////////////////////////////////////////
193 //
194 void FleetView::fillTable()
195 {
196         for(vector<QString>::iterator i = m_Names.begin(); i != m_Names.end(); ++i)
197         {
198                 m_UnitsEdit[(*i)]->setText(QString("%1").arg(m_Fleet->fleet(i->latin1(), 0)));
199         }
200         Planet* pl;
201         if ((pl = dynamic_cast<Planet*>(m_Fleet)) && m_bHome)
202         {
203                 m_RoidsEditView->setValue(tr("score"),pl->planetScore());
204                 m_RoidsEditView->setValue(tr("metal"),pl->roids(tr("metal").latin1()));
205                 m_RoidsEditView->setValue(tr("crystal"),pl->roids(tr("crystal").latin1()));
206                 m_RoidsEditView->setValue(tr("eonium"),pl->roids(tr("eonium").latin1()));
207                 m_RoidsEditView->setValue(tr("uninit"),pl->roids(tr("uninit").latin1()));
208         }
209         slotViewTickRange();
210 }
211
212 /////////////////////////////////////////////////////////////////////
213 //
214 void FleetView::slotRoidsChanged(const QString& type, int value)
215 {
216         Planet* pl;
217         if ((pl = dynamic_cast<Planet*>(m_Fleet)))
218         {
219                 if (type == tr("score"))
220                         pl->setPlanetScore(value);
221                 else
222                 {
223                         pl->setRoids(type.latin1(), value);
224                 }
225                 emit fleetChanged(pl);
226         }
227 }
228
229 /////////////////////////////////////////////////////////////////////
230 //
231 void FleetView::slotViewTickRange(int min, int max)
232 {
233         if (min > -1)
234                 m_iMinTick = min;
235         if (max > -1)
236                 m_iMaxTick = max;
237
238         int before = 0;
239         int after  = 0;
240         for(vector<QString>::iterator i = m_Names.begin(); i != m_Names.end(); ++i)
241         {
242                 int show;
243                 if (m_iResultMode == 1) //Blocked
244                 {
245                         show = m_Fleet->blockedFleet(i->latin1(), m_iMaxTick);
246                 }
247                 else if (m_iResultMode == 2) //Survived
248                 {
249                         show = m_Fleet->fleet(i->latin1(), m_iMaxTick);
250                 }
251                 else
252                 {
253                         before = m_Fleet->fleet(i->latin1(), m_iMinTick);
254                         after = m_Fleet->fleet(i->latin1(), m_iMaxTick);
255                         show = after - before;
256                 }
257                 m_UnitsLostSurvivedView[(*i)]->setText(QString("%1").arg( show ));
258         }
259
260         int lost;
261         
262         int init = 0;
263
264         before = m_Fleet->resource(tr("metal").latin1(),m_iMinTick);
265         after = m_Fleet->resource(tr("metal").latin1(),m_iMaxTick);
266         lost = after - before;
267         init += lost;
268         m_ResourceView->setLines(tr("metal"),lost);
269
270         before = m_Fleet->resource(tr("crystal").latin1(),m_iMinTick);
271         after = m_Fleet->resource(tr("crystal").latin1(),m_iMaxTick);
272         lost = after - before;
273         init += lost;
274         m_ResourceView->setLines(tr("crystal"),lost);
275
276         before = m_Fleet->resource(tr("eonium").latin1(),m_iMinTick);
277         after = m_Fleet->resource(tr("eonium").latin1(),m_iMaxTick);
278         lost = after - before;
279         init += lost;
280         m_ResourceView->setLines(tr("eonium"),lost);
281
282         before = m_Fleet->resource(tr("uninit").latin1(),m_iMinTick);
283         after = m_Fleet->resource(tr("uninit").latin1(),m_iMaxTick);
284         lost = after - before;
285         m_ResourceView->setLines(tr("uninit"),lost);
286
287         before = m_Fleet->score(m_iMinTick);
288         after = m_Fleet->score(m_iMaxTick);
289         lost = after - before;
290         m_ResourceView->setLines(tr("score"),after);
291         m_ResourceView->setLines(tr("lost"),lost);
292         float cost =  (before - after) / float(init) / 1000;
293         m_ResourceView->setLines(tr("cost"),cost);
294         
295
296
297         Planet* pl;
298         if ((pl = dynamic_cast<Planet*>(m_Fleet)))
299         {
300
301                 before = pl->roids(tr("metal").latin1(),m_iMinTick);
302                 after = pl->roids(tr("metal").latin1(),m_iMaxTick);
303                 lost = after - before;
304                 m_RoidsEditView->slotSetLost(tr("metal"),lost);
305
306                 before = pl->roids(tr("crystal").latin1(),m_iMinTick);
307                 after = pl->roids(tr("crystal").latin1(),m_iMaxTick);
308                 lost = after - before;
309                 m_RoidsEditView->slotSetLost(tr("crystal"),lost);
310
311                 before = pl->roids(tr("eonium").latin1(),m_iMinTick);
312                 after = pl->roids(tr("eonium").latin1(),m_iMaxTick);
313                 lost = after - before;
314                 m_RoidsEditView->slotSetLost(tr("eonium"),lost);
315
316                 before = pl->roids(tr("uninit").latin1(),m_iMinTick);
317                 after = pl->roids(tr("uninit").latin1(),m_iMaxTick);
318                 lost = after - before;
319                 m_RoidsEditView->slotSetLost(tr("uninit"),lost);
320
321                 before = pl->planetScore(m_iMinTick);
322                 after = pl->planetScore(m_iMaxTick);
323                 lost = after - before;
324                 m_RoidsEditView->slotSetLost(tr("scorelost"),lost);
325
326                 m_RoidsEditView->slotSetLost(tr("capping"),pl->capping(m_iMaxTick));
327
328         }
329 }
330
331 //////////////////////////////////////////////////////////////////////////
332 //
333 void FleetView::slotSetResultMode(int i)
334 {
335         m_iResultMode = i;
336         slotViewTickRange();
337 }