]> ruin.nu Git - hbs.git/blob - bs/bsview.cpp
initial commit of the BSConf class.
[hbs.git] / bs / bsview.cpp
1 /***************************************************************************
2                           bcview.cpp  -  description
3                              -------------------
4     begin                : Sun Jun 17 19:19:58 CEST 2001
5     copyright            : (C) 2001 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 "bsview.h"
19
20 //standard library
21 #include <iostream>
22 #include <map>
23 #include <vector>
24 #include <string>
25
26 using namespace std;
27
28 //QT includes
29 #include <qlistview.h>
30 #include <qwidgetstack.h>
31
32 #include "battlesum.h"
33 #include "scanview.h"
34 #include "ui/infoview.h"
35 #include "tickview.h"
36 #include "ui/fleetviewbase.h"
37 #include "fleet.h"
38
39 BSView::BSView(QWidget *parent, BSDoc *doc) : QSplitter(parent)
40 {
41   /** connect doc with the view*/
42   connect(doc, SIGNAL(documentChanged()), this, SLOT(slotDocumentChanged()));
43         
44         m_doc = doc;
45         
46         m_LeftSplit = new QSplitter(QSplitter::Vertical, this);
47         m_RightSplit = new QSplitter(QSplitter::Vertical, this);
48
49         //setting up the listview
50         m_NumberView = new QListView(m_LeftSplit);
51         m_NumberView->setRootIsDecorated(true);  
52         m_NumberView->addColumn("Name");
53         m_NumberView->addColumn("Number");
54         m_NumberView->addColumn("ETA");
55         //addBattle("NO BATTLES");
56
57         m_InfoView = new InfoView(m_LeftSplit);
58         
59         //the right side
60         m_TickView = new TickView(m_RightSplit);
61         m_FleetViews = new QWidgetStack(m_RightSplit);
62         m_ScanView = new ScanView(m_RightSplit);
63         
64         //the widget stack
65         m_BattleSum = new BattleSum();
66         m_FleetViews->addWidget(m_BattleSum, 0);
67         m_FleetView = new FleetViewBase();
68         m_FleetViews->addWidget(m_FleetView, 1);
69
70         //m_FleetViews->raiseWidget(0);
71         connect(m_NumberView, SIGNAL(selectionChanged(QListViewItem *)), SLOT(slotFleetSelection(QListViewItem *))); 
72 }
73
74 BSView::~BSView()
75 {
76 }
77
78 //////////////////////////////////////////////////////////////////////////
79 //
80 void BSView::addBattle(QString name)
81 {
82         QListViewItem* battle = new QListViewItem(m_NumberView, name);
83         QListViewItem* def = new QListViewItem(battle, tr("Friendly"),"", "","1");
84         (void) new QListViewItem(battle, tr("Hostile"),"", "","2");
85         (void) new QListViewItem(def, tr("Home Planet"), "","","1");
86 }
87
88 ///////////////////////////////////////////////////////////////////////////////
89 //
90 /***This function clears the listview and then iterates through the battles
91  * and recreates the listview hierarchy.
92  */
93 void BSView::slotDocumentChanged()
94 {
95         m_NumberView->clear();
96         
97         map<QString, map<QString, map<QString, Fleet> > >& battles = m_doc->Battles();  
98
99         for (map<QString, map<QString, map<QString, Fleet> > >::iterator i = battles.begin(); i != battles.end(); i++)
100         {
101                 QString b = (*i).first;
102                 QListViewItem* battle = new QListViewItem(m_NumberView, b);
103
104                 for (map<QString, map<QString, Fleet> >::iterator j = battles[b].begin(); j != battles[b].end(); j++)
105                 {
106                         QString g = (*j).first;
107                         QListViewItem* group = new QListViewItem(battle, g);
108                         int groupShips = 0;
109
110                         for (map<QString, Fleet>::iterator k = battles[b][g].begin(); k != battles[b][g].end(); k++)
111                         {       
112                                 int ships = battles[b][g][(*k).first].NumberOfShips();
113                                 groupShips += ships;
114                                 (void) new QListViewItem(group, (*k).first, QString("%1").arg(ships), QString("%1").arg(battles[b][g][(*k).first].ETA()));
115                         }
116                         group->setText(1, QString("%1").arg(groupShips));
117                 }
118         }
119 }
120
121 //////////////////////////////////////////////////////////////////////////////
122 //
123 void BSView::slotFleetSelection(QListViewItem *lvi)
124 {
125         //cout << lvi->parent()->text(2).toLocal8bit() << endl;
126 //      for (int i = 0; i < 5; i++)
127 //              cout << lvi->text(i).local8Bit() << endl;
128         
129
130         if (lvi->parent() == '\0')
131         {
132                 m_FleetViews->raiseWidget(0);
133         }
134         else if (lvi->parent()->parent() == '\0')
135         {
136                         m_FleetView->slotHomePlanet(false);
137                 if (lvi->text(0) == tr("Friendly"))
138                 {
139                         m_FleetView->slotAttacker(false);
140                 }
141                 else
142                 {
143                         m_FleetView->slotAttacker(true);
144                 }
145                 m_FleetViews->raiseWidget(1);
146                 
147         }
148         else
149         {
150                 if (lvi->parent()->text(0) == tr("Friendly"))
151                 {
152                         m_FleetView->slotAttacker(false);
153                         m_FleetView->slotHomePlanet(false);
154                         if (lvi->text(0) == tr("Home Planet"))
155                         {
156                                 m_FleetView->slotHomePlanet(true);
157                         }
158                 }
159                 else
160                 {
161                         m_FleetView->slotAttacker(true);
162                         m_FleetView->slotHomePlanet(false);
163                 }
164                 m_FleetViews->raiseWidget(1);
165                                 
166         }
167
168         
169 }