]> git.tdb.fi Git - r2c2.git/blob - source/engineer/trainproperties.cpp
Fix a segfault in removing vehicles when creating a new train
[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/driver.h"
12 #include "libmarklin/vehicle.h"
13 #include "libmarklin/vehicletype.h"
14 #include "engineer.h"
15 #include "trainproperties.h"
16
17 using namespace std;
18 using namespace Msp;
19 using namespace Marklin;
20
21 TrainProperties::TrainProperties(Engineer &e, Train *t):
22         engineer(e),
23         train(t)
24 {
25         set_size(250, 305);
26
27         GLtk::Label *label;
28         add(*(label = new GLtk::Label("Train properties")));
29         label->set_geometry(GLtk::Geometry(10, geom.h-25, geom.w-20, 20));
30
31         add(*(ent_addr = new GLtk::Entry));
32         ent_addr->set_geometry(GLtk::Geometry(10, geom.h-50, 40, 20));
33
34         add(*(drp_protocol = new GLtk::Dropdown));
35         drp_protocol->set_geometry(GLtk::Geometry(60, geom.h-50, 60, 20));
36         for(unsigned i=0;; ++i)
37         {
38                 if(const char *proto = engineer.get_layout().get_driver().enumerate_protocols(i))
39                         drp_protocol->append(proto);
40                 else
41                         break;
42         }
43         drp_protocol->set_selected_index(0);
44
45         add(*(drp_type = new GLtk::Dropdown));
46         drp_type->set_geometry(GLtk::Geometry(130, geom.h-50, geom.w-140, 20));
47
48         const Catalogue::VehicleMap &vehs = engineer.get_catalogue().get_vehicles();
49         unsigned n = 0;
50         for(Catalogue::VehicleMap::const_iterator i=vehs.begin(); i!=vehs.end(); ++i)
51         {
52                 if(!i->second->is_locomotive())
53                         continue;
54
55                 drp_type->append(format("%d %s", i->second->get_article_number(), i->second->get_name()));
56                 if(train && i->second==&train->get_locomotive_type())
57                         drp_type->set_selected_index(n);
58
59                 ++n;
60         }
61
62         add(*(ent_name = new GLtk::Entry));
63         ent_name->set_geometry(GLtk::Geometry(10, geom.h-75, geom.w-20, 20));
64
65         add(*(drp_priority = new GLtk::Dropdown));
66         drp_priority->set_geometry(GLtk::Geometry(10, geom.h-100, geom.w-20, 20));
67         drp_priority->append("Standard freight");
68         drp_priority->append("Express freight");
69         drp_priority->append("Unspecified");
70         drp_priority->append("Standard passenger");
71         drp_priority->append("Express passenger");
72
73         add(*(lst_vehicles = new GLtk::List));
74         lst_vehicles->set_geometry(GLtk::Geometry(10, 100, geom.w-20, geom.h-205));
75
76         GLtk::Button *btn;
77
78         add(*(btn = new GLtk::Button("Rem")));
79         btn->set_geometry(GLtk::Geometry(10, 70, 40, 25));
80         btn->signal_clicked.connect(sigc::mem_fun(this, &TrainProperties::remove_vehicle_clicked));
81
82         add(*(drp_new_vehicle = new GLtk::Dropdown));
83         drp_new_vehicle->set_geometry(GLtk::Geometry(10, 45, geom.w-20, 20));
84         drp_new_vehicle->append("(new vehicle)");
85         drp_new_vehicle->set_selected_index(0);
86         for(Catalogue::VehicleMap::const_iterator i=vehs.begin(); i!=vehs.end(); ++i)
87         {
88                 if(i->second->is_locomotive())
89                         continue;
90
91                 drp_new_vehicle->append(format("%d %s", i->second->get_article_number(), i->second->get_name()));
92         }
93         drp_new_vehicle->signal_item_selected.connect(sigc::mem_fun(this, &TrainProperties::new_vehicle_selected));
94
95         if(train)
96         {
97                 ent_addr->set_text(lexical_cast(train->get_address()));
98                 ent_name->set_text(train->get_name());
99                 drp_priority->set_selected_index(train->get_priority()+2);
100
101                 unsigned n_vehicles = train->get_n_vehicles();
102                 for(unsigned i=1; i<n_vehicles; ++i)
103                 {
104                         const VehicleType &type = train->get_vehicle(i).get_type();
105                         lst_vehicles->append(format("%d %s", type.get_article_number(), type.get_name()));
106                 }
107         }
108         else
109         {
110                 ent_name->set_text(format("Train %d", engineer.get_layout().get_trains().size()+1));
111                 drp_priority->set_selected_index(2);
112         }
113 }
114
115 void TrainProperties::on_ok_clicked()
116 {
117         if(!train)
118         {
119                 const VehicleType &type = get_vehicle_type(drp_type->get_selected_index(), true);
120                 unsigned addr = lexical_cast<unsigned>(ent_addr->get_text());
121                 train = new Train(engineer.get_layout(), type, addr, drp_protocol->get_selected());
122         }
123
124         train->set_name(ent_name->get_text());
125         train->set_priority(drp_priority->get_selected_index()-2);
126
127         // The locomotive is vehicle 0 so we need to add 1
128         for(set<unsigned>::const_iterator i=rem_vehicles.end(); i!=rem_vehicles.begin();)
129                 train->remove_vehicle(*--i+1);
130         for(vector<const VehicleType *>::const_iterator i=add_vehicles.begin(); i!=add_vehicles.end(); ++i)
131                 train->add_vehicle(**i);
132 }
133
134 void TrainProperties::new_vehicle_selected(unsigned n, const string &)
135 {
136         if(n==0)
137                 return;
138
139         const VehicleType &type = get_vehicle_type(n-1, false);
140         add_vehicles.push_back(&type);
141         lst_vehicles->append(format("%d %s", type.get_article_number(), type.get_name()));
142
143         drp_new_vehicle->set_selected_index(0);
144 }
145
146 void TrainProperties::remove_vehicle_clicked()
147 {
148         int selected = lst_vehicles->get_selected_index();
149         if(selected<0)
150                 return;
151
152         lst_vehicles->remove(selected);
153
154         unsigned n_vehicles = (train ? train->get_n_vehicles()-rem_vehicles.size() : 0);
155         if(static_cast<unsigned>(selected)>=n_vehicles)
156                 add_vehicles.erase(add_vehicles.begin()+(selected-n_vehicles));
157         else
158         {
159                 for(set<unsigned>::const_iterator i=rem_vehicles.begin(); i!=rem_vehicles.end(); ++i)
160                         if(*i<=static_cast<unsigned>(selected))
161                                 ++selected;
162                 rem_vehicles.insert(selected);
163         }
164 }
165
166 const VehicleType &TrainProperties::get_vehicle_type(unsigned n, bool loco)
167 {
168         const Catalogue::VehicleMap &vehs = engineer.get_catalogue().get_vehicles();
169         Catalogue::VehicleMap::const_iterator i = vehs.begin();
170         while(i!=vehs.end())
171         {
172                 if(i->second->is_locomotive()==loco)
173                 {
174                         if(!n)
175                                 return *i->second;
176                         --n;
177                 }
178                 ++i;
179         }
180
181         throw InvalidParameterValue("Vehicle type index out of range");
182 }