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>
10 #include "libmarklin/locotype.h"
12 #include "trainpanel.h"
16 TrainPanel::TrainPanel(Remote &r, Marklin::Client &c, Marklin::NetTrain &t):
21 train.signal_name_changed.connect(sigc::mem_fun(this, &TrainPanel::name_changed));
22 train.signal_control_changed.connect(sigc::mem_fun(this, &TrainPanel::control_changed));
23 train.signal_function_changed.connect(sigc::mem_fun(this, &TrainPanel::function_changed));
24 train.signal_route_changed.connect(sigc::mem_fun(this, &TrainPanel::route_changed));
25 train.signal_status_changed.connect(sigc::mem_fun(this, &TrainPanel::status_changed));
27 set_label(train.get_name());
29 Gtk::VBox *vbox = new Gtk::VBox(false, 5);
31 vbox->set_border_width(5);
33 Gtk::HBox *hbox = new Gtk::HBox(false, 5);
34 vbox->add(*manage(hbox));
36 hbox->add(*manage(scl_speed = new Gtk::HScale));
37 scl_speed->set_digits(0);
38 scl_speed->set_range(0, 200);
39 scl_speed->set_increments(5, 5);
40 scl_speed->set_size_request(210, -1);
41 scl_speed->signal_value_changed().connect(sigc::mem_fun(this, &TrainPanel::ui_speed_changed));
43 hbox->add(*manage(chk_reverse = new Gtk::CheckButton("Rev")));
44 chk_reverse->signal_toggled().connect(sigc::mem_fun(this, &TrainPanel::ui_reverse_changed));
46 Gtk::HBox *func_box = new Gtk::HBox(false, 5);
47 vbox->add(*manage(func_box));
48 const std::map<unsigned, string> &funcs = train.get_loco_type().get_functions();
49 for(std::map<unsigned, string>::const_iterator i=funcs.begin(); i!=funcs.end(); ++i)
51 Gtk::CheckButton *&chk = chk_funcs[i->first];
52 chk = new Gtk::CheckButton(i->second);
53 func_box->pack_start(*manage(chk), false, true);
54 chk->signal_toggled().connect(sigc::bind(sigc::mem_fun(this, &TrainPanel::ui_function_changed), i->first));
57 Glib::RefPtr<Gtk::ListStore> route_store = Gtk::ListStore::create(route_columns);
58 vbox->add(*manage(cmb_route = new Gtk::ComboBox(route_store)));
59 cmb_route->pack_start(route_columns.name);
60 route_store->append();
61 const list<string> &routes = client.get_routes();
62 for(list<string>::const_iterator i=routes.begin(); i!=routes.end(); ++i)
64 Gtk::TreeIter iter = route_store->append();
65 (*iter)[route_columns.name] = *i;
67 cmb_route->signal_changed().connect(sigc::mem_fun(this, &TrainPanel::ui_route_changed));
69 vbox->add(*manage(lbl_status = new Gtk::Label));
74 void TrainPanel::name_changed(const string &name)
79 void TrainPanel::status_changed(const string &status)
81 lbl_status->set_text(status);
84 void TrainPanel::control_changed(const string &control, float value)
88 // XXX It would be better to make the LocoType give us the catalogue
89 scl_speed->set_value(abs(value)*3.6/remote.get_catalogue().get_scale());
90 chk_reverse->set_active(value<0);
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 if(chk_reverse->get_active())
117 train.set_control("speed", speed);
120 void TrainPanel::ui_reverse_changed()
122 train.set_control("speed", 0);
125 void TrainPanel::ui_function_changed(unsigned func)
127 std::map<unsigned, Gtk::CheckButton *>::iterator i = chk_funcs.find(func);
128 if(i!=chk_funcs.end())
129 train.set_function(func, i->second->get_active());
132 void TrainPanel::ui_route_changed()
134 Gtk::TreeIter iter = cmb_route->get_active();
135 train.set_route(Glib::ustring((*iter)[route_columns.name]));
139 TrainPanel::RouteRecord::RouteRecord()