3 This file is part of the MSP Märklin suite
4 Copyright © 2009-2010 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
9 #include <gtkmm/liststore.h>
11 #include "trainpanel.h"
15 TrainPanel::TrainPanel(Remote &r, Marklin::Client &c, Marklin::NetTrain &t):
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));
26 set_label(train.get_name());
28 Gtk::VBox *vbox = new Gtk::VBox(false, 5);
30 vbox->set_border_width(5);
32 Gtk::HBox *hbox = new Gtk::HBox(false, 5);
33 vbox->add(*manage(hbox));
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));
42 hbox->add(*manage(chk_reverse = new Gtk::CheckButton("Rev")));
43 chk_reverse->signal_toggled().connect(sigc::mem_fun(this, &TrainPanel::ui_reverse_changed));
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)
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));
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)
63 Gtk::TreeIter iter = route_store->append();
64 (*iter)[route_columns.name] = *i;
66 cmb_route->signal_changed().connect(sigc::mem_fun(this, &TrainPanel::ui_route_changed));
68 vbox->add(*manage(lbl_status = new Gtk::Label));
73 void TrainPanel::name_changed(const string &name)
78 void TrainPanel::status_changed(const string &status)
80 lbl_status->set_text(status);
83 void TrainPanel::control_changed(const string &control, float value)
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());
90 else if(control=="reverse")
91 chk_reverse->set_active(value);
94 void TrainPanel::function_changed(unsigned func, bool set)
96 std::map<unsigned, Gtk::CheckButton *>::iterator i = chk_funcs.find(func);
97 if(i!=chk_funcs.end())
98 i->second->set_active(set);
101 void TrainPanel::route_changed(const string &route)
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)
107 cmb_route->set_active(i);
112 void TrainPanel::ui_speed_changed()
114 float speed = scl_speed->get_value()/3.6*remote.get_catalogue().get_scale();
115 train.set_control("speed", speed);
118 void TrainPanel::ui_reverse_changed()
120 if(train.get_control("speed"))
122 train.set_control("speed", 0);
123 chk_reverse->set_active(train.get_control("reverse"));
126 train.set_control("reverse", chk_reverse->get_active());
129 void TrainPanel::ui_function_changed(unsigned func)
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());
136 void TrainPanel::ui_route_changed()
138 Gtk::TreeIter iter = cmb_route->get_active();
139 train.set_route(Glib::ustring((*iter)[route_columns.name]));
143 TrainPanel::RouteRecord::RouteRecord()