]> git.tdb.fi Git - r2c2.git/blob - source/engineer/routeselect.cpp
Store routes in a map by name rather than a set
[r2c2.git] / source / engineer / routeselect.cpp
1 /* $Id$
2
3 This file is part of the MSP Märklin suite
4 Copyright © 2009 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #include <msp/gltk/label.h>
9 #include "engineer.h"
10 #include "libmarklin/route.h"
11 #include "routeselect.h"
12
13 using namespace std;
14 using namespace Msp;
15 using namespace Marklin;
16
17 RouteSelect::RouteSelect(Engineer &e, const GLtk::Resources &r, Train &t):
18         GLtk::Widget(r),
19         Dialog(r),
20         engineer(e),
21         train(t)
22 {
23         set_size(200, 95);
24
25         GLtk::Label *label;
26         add(*(label = new GLtk::Label(res, "Select route")));
27         label->set_geometry(GLtk::Geometry(10, geom.h-25, geom.w-20, 20));
28
29         add(*(drp_route = new GLtk::Dropdown(res)));
30         drp_route->set_geometry(GLtk::Geometry(10, geom.h-50, geom.w-20, 20));
31         drp_route->append("(none)");
32         drp_route->set_selected_index(0);
33         const map<string, Route *> &routes = engineer.get_layout().get_routes();
34         unsigned n = 1;
35         for(map<string, Route *>::const_iterator i=routes.begin(); i!=routes.end(); ++i, ++n)
36         {
37                 drp_route->append(i->second->get_name());
38                 if(i->second==train.get_route())
39                         drp_route->set_selected_index(n);
40         }
41 }
42
43 void RouteSelect::on_ok_clicked()
44 {
45         if(drp_route->get_selected_index()>0)
46         {
47                 const map<string, Route *> &routes = engineer.get_layout().get_routes();
48                 map<string, Route *>::const_iterator i = routes.begin();
49                 advance(i, drp_route->get_selected_index()-1);
50                 
51                 train.set_route(i->second);
52         }
53         else
54                 train.set_route(0);
55 }