]> git.tdb.fi Git - r2c2.git/blob - source/engineer/trainproperties.cpp
004747cdd1224456ecf0493e1d175e64823160ed
[r2c2.git] / source / engineer / trainproperties.cpp
1 /* $Id$
2
3 This file is part of the MSP Märklin suite
4 Copyright © 2006-2009 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 "engineer.h"
13 #include "trainproperties.h"
14
15 using namespace std;
16 using namespace Msp;
17 using namespace Marklin;
18
19 TrainProperties::TrainProperties(Engineer &e, const GLtk::Resources &r, Train *t):
20         Widget(r),
21         Dialog(r),
22         engineer(e),
23         train(t)
24 {
25         set_size(200, 120);
26
27         GLtk::Label *label;
28         add(*(label=new GLtk::Label(res, "Train properties")));
29         label->set_geometry(GLtk::Geometry(10, geom.h-25, geom.w-20, 20));
30
31         add(*(ent_addr=new GLtk::Entry(res)));
32         ent_addr->set_geometry(GLtk::Geometry(10, geom.h-50, 40, 20));
33
34         add(*(drp_type=new GLtk::Dropdown(res)));
35         drp_type->set_geometry(GLtk::Geometry(60, geom.h-50, geom.w-70, 20));
36
37         const map<unsigned, LocoType *> &locos = engineer.get_catalogue().get_locomotives();
38         unsigned n = 0;
39         for(map<unsigned, LocoType *>::const_iterator i=locos.begin(); i!=locos.end(); ++i, ++n)
40         {
41                 drp_type->append(format("%d %s", i->second->get_article_number(), i->second->get_name()));
42                 if(train && i->second==&train->get_locomotive().get_type())
43                         drp_type->set_selected_index(n);
44         }
45
46         add(*(ent_name=new GLtk::Entry(res)));
47         ent_name->set_geometry(GLtk::Geometry(10, geom.h-75, geom.w-20, 20));
48
49         if(train)
50         {
51                 ent_addr->set_text(lexical_cast(train->get_locomotive().get_address()));
52                 ent_name->set_text(train->get_name());
53         }
54         else
55                 ent_name->set_text(format("Train %d", engineer.get_traffic_manager().get_trains().size()+1));
56 }
57
58 void TrainProperties::on_ok_clicked()
59 {
60         if(!train)
61         {
62                 const map<unsigned, LocoType *> &locos = engineer.get_catalogue().get_locomotives();
63                 map<unsigned, LocoType *>::const_iterator i = locos.begin();
64                 advance(i, drp_type->get_selected_index());
65
66                 unsigned addr = lexical_cast<unsigned>(ent_addr->get_text());
67                 Locomotive *loco = new Locomotive(*i->second, engineer.get_control(), addr);
68                 train = new Train(engineer.get_traffic_manager(), *loco);
69                 engineer.place_train(*train);
70         }
71
72         train->set_name(ent_name->get_text());
73 }