]> git.tdb.fi Git - r2c2.git/blob - source/designer/toolbar.cpp
Don't leave removed tracks in routes
[r2c2.git] / source / designer / toolbar.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/button.h>
9 #include <msp/strings/formatter.h>
10 #include "designer.h"
11 #include "toolbar.h"
12
13 using namespace std;
14 using namespace Msp;
15 using namespace Marklin;
16
17 Toolbar::Toolbar(Designer &d):
18         GLtk::Widget(d.get_ui_resources()),
19         GLtk::Panel(d.get_ui_resources()),
20         designer(d)
21 {
22         set_size(370, 40);
23
24         GLtk::Button *btn;
25         GLtk::Label *lbl;
26
27         add(*(btn=new GLtk::Button(res, "Load")));
28         btn->set_geometry(GLtk::Geometry(5, 10, 40, 24));
29
30         add(*(btn=new GLtk::Button(res, "Save")));
31         btn->set_geometry(GLtk::Geometry(45, 10, 40, 24));
32         btn->signal_clicked.connect(sigc::mem_fun(&designer, &Designer::save));
33
34         add(*(btn=new GLtk::Button(res, "Quit")));
35         btn->set_geometry(GLtk::Geometry(85, 10, 40, 24));
36         btn->set_style("red");
37         btn->signal_clicked.connect(sigc::mem_fun(&designer, &Designer::quit));
38
39         add(*(btn=new GLtk::Button(res, "Trnt")));
40         btn->set_geometry(GLtk::Geometry(135, 10, 40, 24));
41
42         add(*(btn=new GLtk::Button(res, "Sens")));
43         btn->set_geometry(GLtk::Geometry(175, 10, 40, 24));
44
45         add(*(lbl=new GLtk::Label(res, "Routes:")));
46         lbl->set_geometry(GLtk::Geometry(225, 22, 40, 13));
47
48         add(*(drp_routes=new GLtk::Dropdown(res)));
49         drp_routes->set_geometry(GLtk::Geometry(225, 5, 100, 17));
50         drp_routes->append("(new route)");
51         drp_routes->signal_item_selected.connect(sigc::mem_fun(this, &Toolbar::route_selected));
52
53         add(*(btn=new GLtk::Button(res, "AddT")));
54         btn->set_geometry(GLtk::Geometry(325, 10, 40, 24));
55         btn->signal_clicked.connect(sigc::mem_fun(&designer, &Designer::add_selection_to_route));
56
57         designer.get_layout()->signal_route_added.connect(sigc::hide(sigc::mem_fun(this, &Toolbar::update_routes)));
58         designer.get_layout()->signal_route_removed.connect(sigc::hide(sigc::mem_fun(this, &Toolbar::update_routes)));
59         update_routes();
60 }
61
62 void Toolbar::route_selected(unsigned index, const string &)
63 {
64         if(index==drp_routes->get_n_items()-1)
65         {
66                 Layout &layout = *designer.get_layout();
67                 Route *route = new Route(layout, format("Route %d", layout.get_routes().size()+1));
68                 designer.get_layout()->add_route(*route);
69                 designer.edit_route(*route);
70
71                 const set<Route *> &routes = designer.get_layout()->get_routes();
72                 int selected = -1;
73                 unsigned n = 0;
74                 for(set<Route *>::const_iterator i=routes.begin(); (selected<0 && i!=routes.end()); ++i, ++n)
75                         if(*i==route)
76                                 selected = n;
77                 drp_routes->set_selected_index(selected);
78         }
79         else
80         {
81                 const set<Route *> &routes = designer.get_layout()->get_routes();
82                 set<Route *>::const_iterator i = routes.begin();
83                 advance(i, index);
84                 designer.edit_route(**i);
85         }
86 }
87
88 void Toolbar::update_routes()
89 {
90         drp_routes->clear();
91         const set<Route *> &routes = designer.get_layout()->get_routes();
92         int selected = -1;
93         unsigned n = 0;
94         for(set<Route *>::const_iterator i=routes.begin(); i!=routes.end(); ++i, ++n)
95         {
96                 drp_routes->append((*i)->get_name());
97                 if(*i==designer.get_current_route())
98                         selected = n;
99         }
100         drp_routes->append("(new route)");
101         drp_routes->set_selected_index(selected);
102 }