]> git.tdb.fi Git - r2c2.git/blob - source/engineer/trainproperties.cpp
Support trains with multiple vehicles
[r2c2.git] / source / engineer / trainproperties.cpp
1 /* $Id$
2
3 This file is part of the MSP Märklin suite
4 Copyright © 2006-2010  Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #include <msp/gltk/button.h>
9 #include <msp/gltk/label.h>
10 #include <msp/strings/formatter.h>
11 #include <msp/strings/lexicalcast.h>
12 #include "libmarklin/locotype.h"
13 #include "engineer.h"
14 #include "trainproperties.h"
15
16 using namespace std;
17 using namespace Msp;
18 using namespace Marklin;
19
20 TrainProperties::TrainProperties(Engineer &e, const GLtk::Resources &r, Train *t):
21         Widget(r),
22         Dialog(r),
23         engineer(e),
24         train(t)
25 {
26         set_size(200, 120);
27
28         GLtk::Label *label;
29         add(*(label=new GLtk::Label(res, "Train properties")));
30         label->set_geometry(GLtk::Geometry(10, geom.h-25, geom.w-20, 20));
31
32         add(*(ent_addr=new GLtk::Entry(res)));
33         ent_addr->set_geometry(GLtk::Geometry(10, geom.h-50, 40, 20));
34
35         add(*(drp_type=new GLtk::Dropdown(res)));
36         drp_type->set_geometry(GLtk::Geometry(60, geom.h-50, geom.w-70, 20));
37
38         const map<unsigned, VehicleType *> &vehs = engineer.get_catalogue().get_vehicles();
39         unsigned n = 0;
40         for(map<unsigned, VehicleType *>::const_iterator i=vehs.begin(); i!=vehs.end(); ++i, ++n)
41         {
42                 if(!dynamic_cast<LocoType *>(i->second))
43                         continue;
44
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);
48         }
49
50         add(*(ent_name=new GLtk::Entry(res)));
51         ent_name->set_geometry(GLtk::Geometry(10, geom.h-75, geom.w-20, 20));
52
53         if(train)
54         {
55                 ent_addr->set_text(lexical_cast(train->get_address()));
56                 ent_name->set_text(train->get_name());
57         }
58         else
59                 ent_name->set_text(format("Train %d", engineer.get_layout().get_trains().size()+1));
60 }
61
62 void TrainProperties::on_ok_clicked()
63 {
64         if(!train)
65         {
66                 const map<unsigned, VehicleType *> &vehs = engineer.get_catalogue().get_vehicles();
67                 map<unsigned, VehicleType *>::const_iterator i = vehs.begin();
68                 unsigned n = drp_type->get_selected_index();
69                 while(!dynamic_cast<LocoType *>(i->second))
70                         ++i;
71                 while(n)
72                 {
73                         if(dynamic_cast<LocoType *>(i->second))
74                                 --n;
75                         ++i;
76                 }
77
78                 unsigned addr = lexical_cast<unsigned>(ent_addr->get_text());
79                 train = new Train(engineer.get_layout(), *dynamic_cast<LocoType *>(i->second), addr);
80         }
81
82         train->set_name(ent_name->get_text());
83 }