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/vehicle.h"
12 #include "libmarklin/vehicletype.h"
14 #include "trainproperties.h"
18 using namespace Marklin;
20 TrainProperties::TrainProperties(Engineer &e, const GLtk::Resources &r, Train *t):
29 add(*(label = new GLtk::Label(res, "Train properties")));
30 label->set_geometry(GLtk::Geometry(10, geom.h-25, geom.w-20, 20));
32 add(*(ent_addr = new GLtk::Entry(res)));
33 ent_addr->set_geometry(GLtk::Geometry(10, geom.h-50, 40, 20));
35 add(*(drp_type = new GLtk::Dropdown(res)));
36 drp_type->set_geometry(GLtk::Geometry(60, geom.h-50, geom.w-70, 20));
38 const Catalogue::VehicleMap &vehs = engineer.get_catalogue().get_vehicles();
40 for(Catalogue::VehicleMap::const_iterator i=vehs.begin(); i!=vehs.end(); ++i)
42 if(!i->second->is_locomotive())
45 drp_type->append(format("%d %s", i->second->get_article_number(), i->second->get_name()));
46 if(train && i->second==&train->get_locomotive_type())
47 drp_type->set_selected_index(n);
52 add(*(ent_name = new GLtk::Entry(res)));
53 ent_name->set_geometry(GLtk::Geometry(10, geom.h-75, geom.w-20, 20));
55 add(*(drp_priority = new GLtk::Dropdown(res)));
56 drp_priority->set_geometry(GLtk::Geometry(10, geom.h-100, geom.w-20, 20));
57 drp_priority->append("Standard freight");
58 drp_priority->append("Express freight");
59 drp_priority->append("Unspecified");
60 drp_priority->append("Standard passenger");
61 drp_priority->append("Express passenger");
63 add(*(lst_vehicles = new GLtk::List(res)));
64 lst_vehicles->set_geometry(GLtk::Geometry(10, 100, geom.w-20, geom.h-205));
68 add(*(btn = new GLtk::Button(res, "Rem")));
69 btn->set_geometry(GLtk::Geometry(10, 70, 40, 25));
70 btn->signal_clicked.connect(sigc::mem_fun(this, &TrainProperties::remove_vehicle_clicked));
72 add(*(drp_new_vehicle = new GLtk::Dropdown(res)));
73 drp_new_vehicle->set_geometry(GLtk::Geometry(10, 45, geom.w-20, 20));
74 drp_new_vehicle->append("(new vehicle)");
75 drp_new_vehicle->set_selected_index(0);
76 for(Catalogue::VehicleMap::const_iterator i=vehs.begin(); i!=vehs.end(); ++i)
78 if(i->second->is_locomotive())
81 drp_new_vehicle->append(format("%d %s", i->second->get_article_number(), i->second->get_name()));
83 drp_new_vehicle->signal_item_selected.connect(sigc::mem_fun(this, &TrainProperties::new_vehicle_selected));
87 ent_addr->set_text(lexical_cast(train->get_address()));
88 ent_name->set_text(train->get_name());
89 drp_priority->set_selected_index(train->get_priority()+2);
91 unsigned n_vehicles = train->get_n_vehicles();
92 for(unsigned i=1; i<n_vehicles; ++i)
94 const VehicleType &type = train->get_vehicle(i).get_type();
95 lst_vehicles->append(format("%d %s", type.get_article_number(), type.get_name()));
100 ent_name->set_text(format("Train %d", engineer.get_layout().get_trains().size()+1));
101 drp_priority->set_selected_index(2);
105 void TrainProperties::on_ok_clicked()
109 const VehicleType &type = get_vehicle_type(drp_type->get_selected_index(), true);
110 unsigned addr = lexical_cast<unsigned>(ent_addr->get_text());
111 train = new Train(engineer.get_layout(), type, addr);
114 train->set_name(ent_name->get_text());
115 train->set_priority(drp_priority->get_selected_index()-2);
117 // The locomotive is vehicle 0 so we need to add 1
118 for(set<unsigned>::const_iterator i=rem_vehicles.end(); i!=rem_vehicles.begin();)
119 train->remove_vehicle(*--i+1);
120 for(vector<const VehicleType *>::const_iterator i=add_vehicles.begin(); i!=add_vehicles.end(); ++i)
121 train->add_vehicle(**i);
124 void TrainProperties::new_vehicle_selected(unsigned n, const string &)
129 const VehicleType &type = get_vehicle_type(n-1, false);
130 add_vehicles.push_back(&type);
131 lst_vehicles->append(format("%d %s", type.get_article_number(), type.get_name()));
133 drp_new_vehicle->set_selected_index(0);
136 void TrainProperties::remove_vehicle_clicked()
138 int selected = lst_vehicles->get_selected_index();
142 lst_vehicles->remove(selected);
144 unsigned n_vehicles = train->get_n_vehicles()-rem_vehicles.size();
145 if(static_cast<unsigned>(selected)>=n_vehicles)
146 add_vehicles.erase(add_vehicles.begin()+(selected-n_vehicles));
149 for(set<unsigned>::const_iterator i=rem_vehicles.begin(); i!=rem_vehicles.end(); ++i)
150 if(*i<=static_cast<unsigned>(selected))
152 rem_vehicles.insert(selected);
156 const VehicleType &TrainProperties::get_vehicle_type(unsigned n, bool loco)
158 const Catalogue::VehicleMap &vehs = engineer.get_catalogue().get_vehicles();
159 Catalogue::VehicleMap::const_iterator i = vehs.begin();
162 if(i->second->is_locomotive()==loco)
171 throw InvalidParameterValue("Vehicle type index out of range");