]> git.tdb.fi Git - r2c2.git/blob - source/engineer/trainproperties.cpp
Support more complex article numbers
[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/label.h>
9 #include <msp/strings/formatter.h>
10 #include <msp/strings/lexicalcast.h>
11 #include "libmarklin/vehicle.h"
12 #include "libmarklin/vehicletype.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(250, 275);
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 Catalogue::VehicleMap &vehs = engineer.get_catalogue().get_vehicles();
39         unsigned n = 0;
40         for(Catalogue::VehicleMap::const_iterator i=vehs.begin(); i!=vehs.end(); ++i)
41         {
42                 if(!i->second->is_locomotive())
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                 ++n;
50         }
51
52         add(*(ent_name = new GLtk::Entry(res)));
53         ent_name->set_geometry(GLtk::Geometry(10, geom.h-75, geom.w-20, 20));
54
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");
62
63         add(*(lst_vehicles = new GLtk::List(res)));
64         lst_vehicles->set_geometry(GLtk::Geometry(10, geom.h-205, geom.w-20, 100));
65
66         add(*(drp_new_vehicle = new GLtk::Dropdown(res)));
67         drp_new_vehicle->set_geometry(GLtk::Geometry(10, geom.h-230, geom.w-20, 20));
68         drp_new_vehicle->append("(new vehicle)");
69         drp_new_vehicle->set_selected_index(0);
70         for(Catalogue::VehicleMap::const_iterator i=vehs.begin(); i!=vehs.end(); ++i)
71         {
72                 if(i->second->is_locomotive())
73                         continue;
74
75                 drp_new_vehicle->append(format("%d %s", i->second->get_article_number(), i->second->get_name()));
76         }
77         drp_new_vehicle->signal_item_selected.connect(sigc::mem_fun(this, &TrainProperties::new_vehicle_selected));
78
79         if(train)
80         {
81                 ent_addr->set_text(lexical_cast(train->get_address()));
82                 ent_name->set_text(train->get_name());
83                 drp_priority->set_selected_index(train->get_priority()+2);
84
85                 unsigned n_vehicles = train->get_n_vehicles();
86                 for(unsigned i=1; i<n_vehicles; ++i)
87                 {
88                         const VehicleType &type = train->get_vehicle(i).get_type();
89                         lst_vehicles->append(format("%d %s", type.get_article_number(), type.get_name()));
90                 }
91         }
92         else
93         {
94                 ent_name->set_text(format("Train %d", engineer.get_layout().get_trains().size()+1));
95                 drp_priority->set_selected_index(2);
96         }
97 }
98
99 void TrainProperties::on_ok_clicked()
100 {
101         if(!train)
102         {
103                 const VehicleType &type = get_vehicle_type(drp_type->get_selected_index(), true);
104                 unsigned addr = lexical_cast<unsigned>(ent_addr->get_text());
105                 train = new Train(engineer.get_layout(), type, addr);
106         }
107
108         train->set_name(ent_name->get_text());
109         train->set_priority(drp_priority->get_selected_index()-2);
110
111         for(vector<const VehicleType *>::const_iterator i=add_vehicles.begin(); i!=add_vehicles.end(); ++i)
112                 train->add_vehicle(**i);
113 }
114
115 void TrainProperties::new_vehicle_selected(unsigned n, const string &)
116 {
117         if(n==0)
118                 return;
119
120         const VehicleType &type = get_vehicle_type(n-1, false);
121         add_vehicles.push_back(&type);
122         lst_vehicles->append(format("%d %s", type.get_article_number(), type.get_name()));
123
124         drp_new_vehicle->set_selected_index(0);
125 }
126
127 const VehicleType &TrainProperties::get_vehicle_type(unsigned n, bool loco)
128 {
129         const Catalogue::VehicleMap &vehs = engineer.get_catalogue().get_vehicles();
130         Catalogue::VehicleMap::const_iterator i = vehs.begin();
131         while(i!=vehs.end())
132         {
133                 if(i->second->is_locomotive()==loco)
134                 {
135                         if(!n)
136                                 return *i->second;
137                         --n;
138                 }
139                 ++i;
140         }
141
142         throw InvalidParameterValue("Vehicle type index out of range");
143 }