]> git.tdb.fi Git - r2c2.git/blob - source/designer/toolbar.cpp
Halt all trains in various unexpected situations
[r2c2.git] / source / designer / toolbar.cpp
1 /* $Id$
2
3 This file is part of the MSP Märklin suite
4 Copyright © 2009-2010  Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #include <msp/gltk/button.h>
9 #include <msp/strings/formatter.h>
10 #include "libmarklin/route.h"
11 #include "designer.h"
12 #include "toolbar.h"
13
14 using namespace std;
15 using namespace Msp;
16 using namespace Marklin;
17
18 Toolbar::Toolbar(Designer &d):
19         GLtk::Widget(d.get_ui_resources()),
20         GLtk::Panel(d.get_ui_resources()),
21         designer(d)
22 {
23         set_size(410, 40);
24
25         GLtk::Button *btn;
26         GLtk::Label *lbl;
27
28         add(*(btn=new GLtk::Button(res, "Load")));
29         btn->set_geometry(GLtk::Geometry(5, 10, 40, 24));
30         btn->set_tooltip("Load layout (not implemented)");
31
32         add(*(btn=new GLtk::Button(res, "Save")));
33         btn->set_geometry(GLtk::Geometry(45, 10, 40, 24));
34         btn->set_tooltip("Save current layout");
35         btn->signal_clicked.connect(sigc::mem_fun(&designer, &Designer::save));
36
37         add(*(btn=new GLtk::Button(res, "Quit")));
38         btn->set_geometry(GLtk::Geometry(85, 10, 40, 24));
39         btn->set_style("red");
40         btn->set_tooltip("Exit Designer");
41         btn->signal_clicked.connect(sigc::mem_fun(&designer, &Designer::quit));
42
43         add(*(btn=new GLtk::Button(res, "NewT")));
44         btn->set_geometry(GLtk::Geometry(135, 10, 40, 24));
45         btn->set_tooltip("Add a track piece");
46         btn->signal_clicked.connect(sigc::mem_fun(&designer, &Designer::new_track));
47
48         add(*(btn=new GLtk::Button(res, "Trnt")));
49         btn->set_geometry(GLtk::Geometry(175, 10, 40, 24));
50         btn->set_tooltip("Set turnout ID of selected track");
51         btn->signal_clicked.connect(sigc::mem_fun(&designer, &Designer::set_turnout_id));
52
53         add(*(btn=new GLtk::Button(res, "Sens")));
54         btn->set_geometry(GLtk::Geometry(215, 10, 40, 24));
55         btn->set_tooltip("Set sensor ID of selected tracks");
56         btn->signal_clicked.connect(sigc::mem_fun(&designer, &Designer::set_sensor_id));
57
58         add(*(lbl=new GLtk::Label(res, "Routes:")));
59         lbl->set_geometry(GLtk::Geometry(265, 22, 40, 13));
60
61         add(*(drp_routes=new GLtk::Dropdown(res)));
62         drp_routes->set_geometry(GLtk::Geometry(265, 5, 100, 17));
63         drp_routes->set_tooltip("Select route to edit");
64         drp_routes->append("(new route)");
65         drp_routes->signal_item_selected.connect(sigc::mem_fun(this, &Toolbar::route_selected));
66
67         add(*(btn=new GLtk::Button(res, "AddT")));
68         btn->set_geometry(GLtk::Geometry(365, 10, 40, 24));
69         btn->set_tooltip("Add selected tracks to current route");
70         btn->signal_clicked.connect(sigc::mem_fun(&designer, &Designer::add_selection_to_route));
71
72         designer.get_layout()->signal_route_added.connect(sigc::hide(sigc::mem_fun(this, &Toolbar::update_routes)));
73         designer.get_layout()->signal_route_removed.connect(sigc::hide(sigc::mem_fun(this, &Toolbar::update_routes)));
74         update_routes();
75 }
76
77 void Toolbar::route_selected(unsigned index, const string &)
78 {
79         if(index==drp_routes->get_n_items()-1)
80         {
81                 Layout &layout = *designer.get_layout();
82                 Route *route = new Route(layout, format("Route %d", layout.get_routes().size()+1));
83                 designer.get_layout()->add_route(*route);
84                 designer.edit_route(*route);
85
86                 const map<string, Route *> &routes = designer.get_layout()->get_routes();
87                 int selected = -1;
88                 unsigned n = 0;
89                 for(map<string, Route *>::const_iterator i=routes.begin(); (selected<0 && i!=routes.end()); ++i, ++n)
90                         if(i->second==route)
91                                 selected = n;
92                 drp_routes->set_selected_index(selected);
93         }
94         else
95         {
96                 const map<string, Route *> &routes = designer.get_layout()->get_routes();
97                 map<string, Route *>::const_iterator i = routes.begin();
98                 advance(i, index);
99                 designer.edit_route(*i->second);
100         }
101 }
102
103 void Toolbar::update_routes()
104 {
105         drp_routes->clear();
106         const map<string, Route *> &routes = designer.get_layout()->get_routes();
107         int selected = -1;
108         unsigned n = 0;
109         for(map<string, Route *>::const_iterator i=routes.begin(); i!=routes.end(); ++i, ++n)
110         {
111                 drp_routes->append(i->second->get_name());
112                 if(i->second==designer.get_current_route())
113                         selected = n;
114         }
115         drp_routes->append("(new route)");
116         drp_routes->set_selected_index(selected);
117 }