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"
8 #include "trainproperties.h"
14 TrainProperties::TrainProperties(Engineer &e, Train *t):
21 add(*(label = new GLtk::Label("Train properties")));
22 label->set_geometry(GLtk::Geometry(10, geom.h-25, geom.w-20, 20));
24 add(*(ent_addr = new GLtk::Entry));
25 ent_addr->set_geometry(GLtk::Geometry(10, geom.h-50, 40, 20));
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)
31 if(const char *proto = engineer.get_layout().get_driver().enumerate_protocols(i))
32 drp_protocol->append(proto);
36 drp_protocol->set_selected_index(0);
38 add(*(drp_type = new GLtk::Dropdown));
39 drp_type->set_geometry(GLtk::Geometry(130, geom.h-50, geom.w-140, 20));
41 const Catalogue::VehicleMap &vehs = engineer.get_catalogue().get_vehicles();
43 for(Catalogue::VehicleMap::const_iterator i=vehs.begin(); i!=vehs.end(); ++i)
45 if(!i->second->is_locomotive())
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);
55 add(*(ent_name = new GLtk::Entry));
56 ent_name->set_geometry(GLtk::Geometry(10, geom.h-75, geom.w-20, 20));
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");
66 add(*(lst_vehicles = new GLtk::List));
67 lst_vehicles->set_geometry(GLtk::Geometry(10, 100, geom.w-20, geom.h-205));
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));
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)
81 if(i->second->is_locomotive())
84 drp_new_vehicle->append(format("%d %s", i->second->get_article_number(), i->second->get_name()));
86 drp_new_vehicle->signal_item_selected.connect(sigc::mem_fun(this, &TrainProperties::new_vehicle_selected));
90 ent_addr->set_text(lexical_cast<string>(train->get_address()));
91 ent_name->set_text(train->get_name());
92 drp_priority->set_selected_index(train->get_priority()+2);
94 unsigned n_vehicles = train->get_n_vehicles();
95 for(unsigned i=1; i<n_vehicles; ++i)
97 const VehicleType &type = train->get_vehicle(i).get_type();
98 lst_vehicles->append(format("%d %s", type.get_article_number(), type.get_name()));
103 ent_name->set_text(format("Train %d", engineer.get_layout().get_trains().size()+1));
104 drp_priority->set_selected_index(2);
108 void TrainProperties::on_ok_clicked()
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());
117 train->set_name(ent_name->get_text());
118 train->set_priority(drp_priority->get_selected_index()-2);
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);
127 void TrainProperties::new_vehicle_selected(unsigned n, const string &)
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()));
136 drp_new_vehicle->set_selected_index(0);
139 void TrainProperties::remove_vehicle_clicked()
141 int selected = lst_vehicles->get_selected_index();
145 lst_vehicles->remove(selected);
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));
152 for(set<unsigned>::const_iterator i=rem_vehicles.begin(); i!=rem_vehicles.end(); ++i)
153 if(*i<=static_cast<unsigned>(selected))
155 rem_vehicles.insert(selected);
159 const VehicleType &TrainProperties::get_vehicle_type(unsigned n, bool loco)
161 const Catalogue::VehicleMap &vehs = engineer.get_catalogue().get_vehicles();
162 Catalogue::VehicleMap::const_iterator i = vehs.begin();
165 if(i->second->is_locomotive()==loco)
174 throw out_of_range("TrainProperties::get_vehicle_type");