3 This file is part of the MSP Märklin suite
4 Copyright © 2006-2010 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
8 #include <msp/gltk/label.h>
9 #include <msp/strings/formatter.h>
10 #include <msp/strings/lexicalcast.h>
11 #include "libmarklin/driver.h"
12 #include "libmarklin/vehicle.h"
13 #include "libmarklin/vehicletype.h"
15 #include "trainproperties.h"
19 using namespace Marklin;
21 TrainProperties::TrainProperties(Engineer &e, Train *t):
28 add(*(label = new GLtk::Label("Train properties")));
29 label->set_geometry(GLtk::Geometry(10, geom.h-25, geom.w-20, 20));
31 add(*(ent_addr = new GLtk::Entry));
32 ent_addr->set_geometry(GLtk::Geometry(10, geom.h-50, 40, 20));
34 add(*(drp_protocol = new GLtk::Dropdown));
35 drp_protocol->set_geometry(GLtk::Geometry(60, geom.h-50, 60, 20));
36 for(unsigned i=0;; ++i)
38 if(const char *proto = engineer.get_layout().get_driver().enumerate_protocols(i))
39 drp_protocol->append(proto);
43 drp_protocol->set_selected_index(0);
45 add(*(drp_type = new GLtk::Dropdown));
46 drp_type->set_geometry(GLtk::Geometry(130, geom.h-50, geom.w-140, 20));
48 const Catalogue::VehicleMap &vehs = engineer.get_catalogue().get_vehicles();
50 for(Catalogue::VehicleMap::const_iterator i=vehs.begin(); i!=vehs.end(); ++i)
52 if(!i->second->is_locomotive())
55 drp_type->append(format("%d %s", i->second->get_article_number(), i->second->get_name()));
56 if(train && i->second==&train->get_locomotive_type())
57 drp_type->set_selected_index(n);
62 add(*(ent_name = new GLtk::Entry));
63 ent_name->set_geometry(GLtk::Geometry(10, geom.h-75, geom.w-20, 20));
65 add(*(drp_priority = new GLtk::Dropdown));
66 drp_priority->set_geometry(GLtk::Geometry(10, geom.h-100, geom.w-20, 20));
67 drp_priority->append("Standard freight");
68 drp_priority->append("Express freight");
69 drp_priority->append("Unspecified");
70 drp_priority->append("Standard passenger");
71 drp_priority->append("Express passenger");
73 add(*(lst_vehicles = new GLtk::List));
74 lst_vehicles->set_geometry(GLtk::Geometry(10, 100, geom.w-20, geom.h-205));
78 add(*(btn = new GLtk::Button("Rem")));
79 btn->set_geometry(GLtk::Geometry(10, 70, 40, 25));
80 btn->signal_clicked.connect(sigc::mem_fun(this, &TrainProperties::remove_vehicle_clicked));
82 add(*(drp_new_vehicle = new GLtk::Dropdown));
83 drp_new_vehicle->set_geometry(GLtk::Geometry(10, 45, geom.w-20, 20));
84 drp_new_vehicle->append("(new vehicle)");
85 drp_new_vehicle->set_selected_index(0);
86 for(Catalogue::VehicleMap::const_iterator i=vehs.begin(); i!=vehs.end(); ++i)
88 if(i->second->is_locomotive())
91 drp_new_vehicle->append(format("%d %s", i->second->get_article_number(), i->second->get_name()));
93 drp_new_vehicle->signal_item_selected.connect(sigc::mem_fun(this, &TrainProperties::new_vehicle_selected));
97 ent_addr->set_text(lexical_cast(train->get_address()));
98 ent_name->set_text(train->get_name());
99 drp_priority->set_selected_index(train->get_priority()+2);
101 unsigned n_vehicles = train->get_n_vehicles();
102 for(unsigned i=1; i<n_vehicles; ++i)
104 const VehicleType &type = train->get_vehicle(i).get_type();
105 lst_vehicles->append(format("%d %s", type.get_article_number(), type.get_name()));
110 ent_name->set_text(format("Train %d", engineer.get_layout().get_trains().size()+1));
111 drp_priority->set_selected_index(2);
115 void TrainProperties::on_ok_clicked()
119 const VehicleType &type = get_vehicle_type(drp_type->get_selected_index(), true);
120 unsigned addr = lexical_cast<unsigned>(ent_addr->get_text());
121 train = new Train(engineer.get_layout(), type, addr, drp_protocol->get_selected());
124 train->set_name(ent_name->get_text());
125 train->set_priority(drp_priority->get_selected_index()-2);
127 // The locomotive is vehicle 0 so we need to add 1
128 for(set<unsigned>::const_iterator i=rem_vehicles.end(); i!=rem_vehicles.begin();)
129 train->remove_vehicle(*--i+1);
130 for(vector<const VehicleType *>::const_iterator i=add_vehicles.begin(); i!=add_vehicles.end(); ++i)
131 train->add_vehicle(**i);
134 void TrainProperties::new_vehicle_selected(unsigned n, const string &)
139 const VehicleType &type = get_vehicle_type(n-1, false);
140 add_vehicles.push_back(&type);
141 lst_vehicles->append(format("%d %s", type.get_article_number(), type.get_name()));
143 drp_new_vehicle->set_selected_index(0);
146 void TrainProperties::remove_vehicle_clicked()
148 int selected = lst_vehicles->get_selected_index();
152 lst_vehicles->remove(selected);
154 unsigned n_vehicles = (train ? train->get_n_vehicles()-rem_vehicles.size() : 0);
155 if(static_cast<unsigned>(selected)>=n_vehicles)
156 add_vehicles.erase(add_vehicles.begin()+(selected-n_vehicles));
159 for(set<unsigned>::const_iterator i=rem_vehicles.begin(); i!=rem_vehicles.end(); ++i)
160 if(*i<=static_cast<unsigned>(selected))
162 rem_vehicles.insert(selected);
166 const VehicleType &TrainProperties::get_vehicle_type(unsigned n, bool loco)
168 const Catalogue::VehicleMap &vehs = engineer.get_catalogue().get_vehicles();
169 Catalogue::VehicleMap::const_iterator i = vehs.begin();
172 if(i->second->is_locomotive()==loco)
181 throw InvalidParameterValue("Vehicle type index out of range");