]> git.tdb.fi Git - r2c2.git/blob - source/remote/trainpanel.cpp
Add framework for generating simple meshes for vehicles
[r2c2.git] / source / remote / trainpanel.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 <gtkmm/box.h>
9 #include <gtkmm/liststore.h>
10 #include "remote.h"
11 #include "trainpanel.h"
12
13 using namespace std;
14
15 TrainPanel::TrainPanel(Remote &r, Marklin::Client &c, Marklin::NetTrain &t):
16         remote(r),
17         client(c),
18         train(t)
19 {
20         train.signal_name_changed.connect(sigc::mem_fun(this, &TrainPanel::name_changed));
21         train.signal_control_changed.connect(sigc::mem_fun(this, &TrainPanel::control_changed));
22         train.signal_function_changed.connect(sigc::mem_fun(this, &TrainPanel::function_changed));
23         train.signal_route_changed.connect(sigc::mem_fun(this, &TrainPanel::route_changed));
24         train.signal_status_changed.connect(sigc::mem_fun(this, &TrainPanel::status_changed));
25
26         set_label(train.get_name());
27
28         Gtk::VBox *vbox = new Gtk::VBox(false, 5);
29         add(*manage(vbox));
30         vbox->set_border_width(5);
31
32         Gtk::HBox *hbox = new Gtk::HBox(false, 5);
33         vbox->add(*manage(hbox));
34
35         hbox->add(*manage(scl_speed = new Gtk::HScale));
36         scl_speed->set_digits(0);
37         scl_speed->set_range(0, 200);
38         scl_speed->set_increments(5, 5);
39         scl_speed->set_size_request(210, -1);
40         scl_speed->signal_value_changed().connect(sigc::mem_fun(this, &TrainPanel::ui_speed_changed));
41
42         hbox->add(*manage(chk_reverse = new Gtk::CheckButton("Rev")));
43         chk_reverse->signal_toggled().connect(sigc::mem_fun(this, &TrainPanel::ui_reverse_changed));
44
45         Gtk::HBox *func_box = new Gtk::HBox(false, 5);
46         vbox->add(*manage(func_box));
47         const std::map<unsigned, string> &funcs = train.get_loco_type().get_functions();
48         for(std::map<unsigned, string>::const_iterator i=funcs.begin(); i!=funcs.end(); ++i)
49         {
50                 Gtk::CheckButton *&chk = chk_funcs[i->first];
51                 chk = new Gtk::CheckButton(i->second);
52                 func_box->pack_start(*manage(chk), false, true);
53                 chk->signal_toggled().connect(sigc::bind(sigc::mem_fun(this, &TrainPanel::ui_function_changed), i->first));
54         }
55
56         Glib::RefPtr<Gtk::ListStore> route_store = Gtk::ListStore::create(route_columns);
57         vbox->add(*manage(cmb_route = new Gtk::ComboBox(route_store)));
58         cmb_route->pack_start(route_columns.name);
59         route_store->append();
60         const list<string> &routes = client.get_routes();
61         for(list<string>::const_iterator i=routes.begin(); i!=routes.end(); ++i)
62         {
63                 Gtk::TreeIter iter = route_store->append();
64                 (*iter)[route_columns.name] = *i;
65         }
66         cmb_route->signal_changed().connect(sigc::mem_fun(this, &TrainPanel::ui_route_changed));
67
68         vbox->add(*manage(lbl_status = new Gtk::Label));
69
70         show_all();
71 }
72
73 void TrainPanel::name_changed(const string &name)
74 {
75         set_label(name);
76 }
77
78 void TrainPanel::status_changed(const string &status)
79 {
80         lbl_status->set_text(status);
81 }
82
83 void TrainPanel::control_changed(const string &control, float value)
84 {
85         if(control=="speed")
86         {
87                 // XXX It would be better to make the VehicleType give us the catalogue
88                 scl_speed->set_value(value*3.6/remote.get_catalogue().get_scale());
89         }
90         else if(control=="reverse")
91                 chk_reverse->set_active(value);
92 }
93
94 void TrainPanel::function_changed(unsigned func, bool set)
95 {
96         std::map<unsigned, Gtk::CheckButton *>::iterator i = chk_funcs.find(func);
97         if(i!=chk_funcs.end())
98                 i->second->set_active(set);
99 }
100
101 void TrainPanel::route_changed(const string &route)
102 {
103         Gtk::TreeNodeChildren children = cmb_route->get_model()->children();
104         for(Gtk::TreeIter i=children.begin(); i!=children.end(); ++i)
105                 if((*i)[route_columns.name]==route)
106                 {
107                         cmb_route->set_active(i);
108                         break;
109                 }
110 }
111
112 void TrainPanel::ui_speed_changed()
113 {
114         float speed = scl_speed->get_value()/3.6*remote.get_catalogue().get_scale();
115         train.set_control("speed", speed);
116 }
117
118 void TrainPanel::ui_reverse_changed()
119 {
120         if(train.get_control("speed"))
121         {
122                 train.set_control("speed", 0);
123                 chk_reverse->set_active(train.get_control("reverse"));
124         }
125         else
126                 train.set_control("reverse", chk_reverse->get_active());
127 }
128
129 void TrainPanel::ui_function_changed(unsigned func)
130 {
131         std::map<unsigned, Gtk::CheckButton *>::iterator i = chk_funcs.find(func);
132         if(i!=chk_funcs.end())
133                 train.set_function(func, i->second->get_active());
134 }
135
136 void TrainPanel::ui_route_changed()
137 {
138         Gtk::TreeIter iter = cmb_route->get_active();
139         train.set_route(Glib::ustring((*iter)[route_columns.name]));
140 }
141
142
143 TrainPanel::RouteRecord::RouteRecord()
144 {
145         add(name);
146 }