]> git.tdb.fi Git - r2c2.git/blob - source/engineer/newtraindialog.cpp
ef9fbf82b6dfe79a58924f65f5e34b610b31acd2
[r2c2.git] / source / engineer / newtraindialog.cpp
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"
7 #include "engineer.h"
8 #include "newtraindialog.h"
9 #include "traindialog.h"
10
11 using namespace std;
12 using namespace Msp;
13 using namespace R2C2;
14
15 static string vehicle_type_name(const VehicleType *const &vehtype)
16 {
17         return format("%s %s", vehtype->get_article_number(), vehtype->get_name());
18 }
19
20 NewTrainDialog::NewTrainDialog(Engineer &e):
21         engineer(e),
22         loco_types(vehicle_type_name)
23 {
24         Loader::WidgetMap widgets;
25         DataFile::load(*this, "data/newtraindialog.ui", widgets);
26
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);
33
34         const Driver &driver = engineer.get_layout().get_driver();
35         for(unsigned i=0;; ++i)
36         {
37                 if(const char *proto = driver.enumerate_protocols(i))
38                         protocols.append(proto);
39                 else
40                         break;
41         }
42
43         drp_protocol->set_selected_index(0);
44
45         const Catalogue::ObjectMap &obj_types = engineer.get_layout().get_catalogue().get_all();
46         for(Catalogue::ObjectMap::const_iterator i=obj_types.begin(); i!=obj_types.end(); ++i)
47                 if(const VehicleType *vt = dynamic_cast<const VehicleType *>(i->second))
48                         if(vt->is_locomotive())
49                                 loco_types.append(vt);
50 }
51
52 void NewTrainDialog::prefill(const Driver::DetectedLocomotive &loco)
53 {
54         drp_protocol->set_selected_index(protocols.find(loco.protocol));
55         ent_address->set_text(lexical_cast<string>(loco.address));
56         ent_name->set_text(loco.name);
57 }
58
59 void NewTrainDialog::on_response(int code)
60 {
61         if(code)
62         {
63                 int index = drp_protocol->get_selected_index();
64                 if(index<0)
65                         return;
66                 string protocol = protocols.get(index);
67
68                 unsigned address = lexical_cast<unsigned>(ent_address->get_text());
69
70                 index = drp_loco_type->get_selected_index();
71                 if(index<0)
72                         return;
73                 const VehicleType &type = *loco_types.get(index);
74
75                 Train *train = new Train(engineer.get_layout(), type, address, protocol);
76                 train->set_name(ent_name->get_text());
77
78                 TrainDialog *dlg = new TrainDialog(engineer, *train);
79                 find_ancestor<GLtk::Root>()->add(*dlg);
80                 dlg->autosize();
81         }
82 }