1 #include <msp/core/maputils.h>
2 #include <msp/strings/format.h>
3 #include "libr2c2/catalogue.h"
4 #include "libr2c2/driver.h"
5 #include "libr2c2/layout.h"
6 #include "libr2c2/vehicletype.h"
8 #include "newtraindialog.h"
9 #include "traindialog.h"
15 static string vehicle_type_name(const VehicleType *const &vehtype)
17 return format("%s %s", vehtype->get_article_number(), vehtype->get_name());
20 NewTrainDialog::NewTrainDialog(Engineer &e):
22 loco_types(vehicle_type_name)
24 Loader::WidgetMap widgets;
25 DataFile::load(*this, "data/newtraindialog.ui", widgets);
27 drp_protocol = dynamic_cast<GLtk::Dropdown *>(get_item(widgets, "drp_protocol"));
28 drp_protocol->set_data(protocols);
29 ent_address = dynamic_cast<GLtk::Entry *>(get_item(widgets, "ent_address"));
30 ent_name = dynamic_cast<GLtk::Entry *>(get_item(widgets, "ent_name"));
31 drp_loco_type = dynamic_cast<GLtk::Dropdown *>(get_item(widgets, "drp_loco_type"));
32 drp_loco_type->set_data(loco_types);
34 const Driver &driver = engineer.get_layout().get_driver();
35 for(unsigned i=0;; ++i)
37 if(const char *proto = driver.enumerate_protocols(i))
38 protocols.append(proto);
43 drp_protocol->set_selected_index(0);
45 const Catalogue::VehicleMap &cat_vtypes = engineer.get_layout().get_catalogue().get_vehicles();
46 for(Catalogue::VehicleMap::const_iterator i=cat_vtypes.begin(); i!=cat_vtypes.end(); ++i)
47 if(i->second->is_locomotive())
48 loco_types.append(i->second);
51 void NewTrainDialog::on_response(int code)
55 int index = drp_protocol->get_selected_index();
58 string protocol = protocols.get(index);
60 unsigned address = lexical_cast<unsigned>(ent_address->get_text());
62 index = drp_loco_type->get_selected_index();
65 const VehicleType &type = *loco_types.get(index);
67 Train *train = new Train(engineer.get_layout(), type, address, protocol);
68 train->set_name(ent_name->get_text());
70 GLtk::Container *root = parent;
71 while(root->get_parent())
72 root = root->get_parent();
74 TrainDialog *dlg = new TrainDialog(engineer, *train);