]> git.tdb.fi Git - r2c2.git/blob - source/engineer/trainproperties.cpp
867e0a8d32c1e49c02f6cf1b61e59e9936bdf40e
[r2c2.git] / source / engineer / trainproperties.cpp
1 #include <msp/gltk/label.h>
2 #include <msp/strings/format.h>
3 #include <msp/strings/lexicalcast.h>
4 #include "libr2c2/driver.h"
5 #include "libr2c2/vehicle.h"
6 #include "libr2c2/vehicletype.h"
7 #include "engineer.h"
8 #include "trainproperties.h"
9
10 using namespace std;
11 using namespace Msp;
12 using namespace R2C2;
13
14 TrainProperties::TrainProperties(Engineer &e, Train *t):
15         engineer(e),
16         train(t)
17 {
18         set_size(250, 305);
19
20         GLtk::Label *label;
21         add(*(label = new GLtk::Label("Train properties")));
22         label->set_geometry(GLtk::Geometry(10, geom.h-25, geom.w-20, 20));
23
24         add(*(ent_addr = new GLtk::Entry));
25         ent_addr->set_geometry(GLtk::Geometry(10, geom.h-50, 40, 20));
26
27         add(*(drp_protocol = new GLtk::Dropdown));
28         drp_protocol->set_geometry(GLtk::Geometry(60, geom.h-50, 60, 20));
29         for(unsigned i=0;; ++i)
30         {
31                 if(const char *proto = engineer.get_layout().get_driver().enumerate_protocols(i))
32                         drp_protocol->append(proto);
33                 else
34                         break;
35         }
36         drp_protocol->set_selected_index(0);
37
38         add(*(drp_type = new GLtk::Dropdown));
39         drp_type->set_geometry(GLtk::Geometry(130, geom.h-50, geom.w-140, 20));
40
41         const Catalogue::VehicleMap &vehs = engineer.get_catalogue().get_vehicles();
42         unsigned n = 0;
43         for(Catalogue::VehicleMap::const_iterator i=vehs.begin(); i!=vehs.end(); ++i)
44         {
45                 if(!i->second->is_locomotive())
46                         continue;
47
48                 drp_type->append(format("%d %s", i->second->get_article_number(), i->second->get_name()));
49                 if(train && i->second==&train->get_locomotive_type())
50                         drp_type->set_selected_index(n);
51
52                 ++n;
53         }
54
55         add(*(ent_name = new GLtk::Entry));
56         ent_name->set_geometry(GLtk::Geometry(10, geom.h-75, geom.w-20, 20));
57
58         add(*(drp_priority = new GLtk::Dropdown));
59         drp_priority->set_geometry(GLtk::Geometry(10, geom.h-100, geom.w-20, 20));
60         drp_priority->append("Standard freight");
61         drp_priority->append("Express freight");
62         drp_priority->append("Unspecified");
63         drp_priority->append("Standard passenger");
64         drp_priority->append("Express passenger");
65
66         add(*(lst_vehicles = new GLtk::List));
67         lst_vehicles->set_geometry(GLtk::Geometry(10, 100, geom.w-20, geom.h-205));
68
69         GLtk::Button *btn;
70
71         add(*(btn = new GLtk::Button("Rem")));
72         btn->set_geometry(GLtk::Geometry(10, 70, 40, 25));
73         btn->signal_clicked.connect(sigc::mem_fun(this, &TrainProperties::remove_vehicle_clicked));
74
75         add(*(drp_new_vehicle = new GLtk::Dropdown));
76         drp_new_vehicle->set_geometry(GLtk::Geometry(10, 45, geom.w-20, 20));
77         drp_new_vehicle->append("(new vehicle)");
78         drp_new_vehicle->set_selected_index(0);
79         for(Catalogue::VehicleMap::const_iterator i=vehs.begin(); i!=vehs.end(); ++i)
80         {
81                 if(i->second->is_locomotive())
82                         continue;
83
84                 drp_new_vehicle->append(format("%d %s", i->second->get_article_number(), i->second->get_name()));
85         }
86         drp_new_vehicle->signal_item_selected.connect(sigc::mem_fun(this, &TrainProperties::new_vehicle_selected));
87
88         if(train)
89         {
90                 ent_addr->set_text(lexical_cast(train->get_address()));
91                 ent_name->set_text(train->get_name());
92                 drp_priority->set_selected_index(train->get_priority()+2);
93
94                 unsigned n_vehicles = train->get_n_vehicles();
95                 for(unsigned i=1; i<n_vehicles; ++i)
96                 {
97                         const VehicleType &type = train->get_vehicle(i).get_type();
98                         lst_vehicles->append(format("%d %s", type.get_article_number(), type.get_name()));
99                 }
100         }
101         else
102         {
103                 ent_name->set_text(format("Train %d", engineer.get_layout().get_trains().size()+1));
104                 drp_priority->set_selected_index(2);
105         }
106 }
107
108 void TrainProperties::on_ok_clicked()
109 {
110         if(!train)
111         {
112                 const VehicleType &type = get_vehicle_type(drp_type->get_selected_index(), true);
113                 unsigned addr = lexical_cast<unsigned>(ent_addr->get_text());
114                 train = new Train(engineer.get_layout(), type, addr, drp_protocol->get_selected());
115         }
116
117         train->set_name(ent_name->get_text());
118         train->set_priority(drp_priority->get_selected_index()-2);
119
120         // The locomotive is vehicle 0 so we need to add 1
121         for(set<unsigned>::const_iterator i=rem_vehicles.end(); i!=rem_vehicles.begin();)
122                 train->remove_vehicle(*--i+1);
123         for(vector<const VehicleType *>::const_iterator i=add_vehicles.begin(); i!=add_vehicles.end(); ++i)
124                 train->add_vehicle(**i);
125 }
126
127 void TrainProperties::new_vehicle_selected(unsigned n, const string &)
128 {
129         if(n==0)
130                 return;
131
132         const VehicleType &type = get_vehicle_type(n-1, false);
133         add_vehicles.push_back(&type);
134         lst_vehicles->append(format("%d %s", type.get_article_number(), type.get_name()));
135
136         drp_new_vehicle->set_selected_index(0);
137 }
138
139 void TrainProperties::remove_vehicle_clicked()
140 {
141         int selected = lst_vehicles->get_selected_index();
142         if(selected<0)
143                 return;
144
145         lst_vehicles->remove(selected);
146
147         unsigned n_vehicles = (train ? train->get_n_vehicles()-rem_vehicles.size() : 0);
148         if(static_cast<unsigned>(selected)>=n_vehicles)
149                 add_vehicles.erase(add_vehicles.begin()+(selected-n_vehicles));
150         else
151         {
152                 for(set<unsigned>::const_iterator i=rem_vehicles.begin(); i!=rem_vehicles.end(); ++i)
153                         if(*i<=static_cast<unsigned>(selected))
154                                 ++selected;
155                 rem_vehicles.insert(selected);
156         }
157 }
158
159 const VehicleType &TrainProperties::get_vehicle_type(unsigned n, bool loco)
160 {
161         const Catalogue::VehicleMap &vehs = engineer.get_catalogue().get_vehicles();
162         Catalogue::VehicleMap::const_iterator i = vehs.begin();
163         while(i!=vehs.end())
164         {
165                 if(i->second->is_locomotive()==loco)
166                 {
167                         if(!n)
168                                 return *i->second;
169                         --n;
170                 }
171                 ++i;
172         }
173
174         throw InvalidParameterValue("Vehicle type index out of range");
175 }