3 This file is part of the MSP Märklin suite
4 Copyright © 2006-2010 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
9 #include <msp/strings/formatter.h>
10 #include "libmarklin/timetable.h"
11 #include "libmarklin/trackiter.h"
12 #include "libmarklin/vehicletype.h"
14 #include "routeselect.h"
15 #include "timetabledialog.h"
16 #include "trainpanel.h"
17 #include "trainproperties.h"
18 #include "trainview.h"
22 using namespace Marklin;
24 TrainPanel::TrainPanel(Engineer &e, const GLtk::Resources &r, Train &t):
33 train.signal_control_changed.connect(sigc::mem_fun(this, &TrainPanel::train_control_changed));
35 add(*(pnl_basic = new GLtk::Panel(res)));
36 pnl_basic->set_style("group");
37 pnl_basic->set_geometry(GLtk::Geometry(0, geom.h-58, geom.w, 45));
39 pnl_basic->add(*(lbl_addr = new GLtk::Label(res, format("%2d", train.get_address()))));
40 lbl_addr->set_style("digital");
41 lbl_addr->set_geometry(GLtk::Geometry(10, 28, 35, 20));
43 pnl_basic->add(*(lbl_name = new GLtk::Label(res, train.get_name())));
44 lbl_name->set_style("digital");
45 lbl_name->set_geometry(GLtk::Geometry(50, 28, geom.w-77, 20));
46 train.signal_name_changed.connect(sigc::mem_fun(lbl_name, &GLtk::Label::set_text));
48 pnl_basic->add(*(lbl_speed = new GLtk::Label(res, " 0")));
49 lbl_speed->set_style("digital");
50 lbl_speed->set_geometry(GLtk::Geometry(10, 3, 35, 20));
52 pnl_basic->add(*(sld_speed = new GLtk::HSlider(res)));
53 sld_speed->set_geometry(GLtk::Geometry(50, 8, geom.w-80, 10));
54 sld_speed->set_range(0, 200);
55 sld_speed->set_step(5);
56 sld_speed->signal_value_changed.connect(sigc::mem_fun(this, &TrainPanel::speed_slider_changed));
58 pnl_basic->add(*(tgl_forward = new GLtk::Toggle(res)));
59 tgl_forward->set_text("Fwd");
60 tgl_forward->set_geometry(GLtk::Geometry(geom.w-30, 0, 20, 27));
61 tgl_forward->set_value(true);
62 tgl_forward->signal_toggled.connect(sigc::mem_fun(this, &TrainPanel::forward_toggled));
64 pnl_basic->add(*(btn_expand = new GLtk::Button(res)));
65 btn_expand->set_style("arrow_down");
66 btn_expand->set_geometry(GLtk::Geometry(geom.w-22, 28, 12, 20));
67 btn_expand->signal_clicked.connect(sigc::mem_fun(this, &TrainPanel::expand_clicked));
69 add(*(pnl_extra = new GLtk::Panel(res)));
70 pnl_extra->set_style("group");
71 pnl_extra->set_geometry(GLtk::Geometry(0, 10, geom.w, 135));
72 pnl_extra->set_visible(false);
74 const Route *route = train.get_route();
75 pnl_extra->add(*(lbl_route = new GLtk::Label(res, (route ? route->get_name() : "Free run"))));
76 lbl_route->set_style("digital");
77 lbl_route->set_geometry(GLtk::Geometry(10, 85, geom.w-20, 20));
78 train.signal_route_changed.connect(sigc::mem_fun(this, &TrainPanel::train_route_changed));
80 pnl_extra->add(*(lbl_status = new GLtk::Label(res, train.get_status())));
81 lbl_status->set_style("digital");
82 lbl_status->set_geometry(GLtk::Geometry(10, 60, geom.w-20, 20));
83 train.signal_status_changed.connect(sigc::mem_fun(this, &TrainPanel::train_status_changed));
85 const map<unsigned, string> &funcs = train.get_locomotive_type().get_functions();
87 for(map<unsigned, string>::const_iterator i=funcs.begin(); i!=funcs.end(); ++i, x+=36)
89 string fname = i->second;
90 fname[0] = toupper(fname[0]);
92 pnl_extra->add(*(tgl = new GLtk::Toggle(res)));
94 tgl->set_geometry(GLtk::Geometry(x, 108, 36, 27));
95 tgl->set_value(train.get_function(i->first));
96 tgl->signal_toggled.connect(sigc::bind(sigc::mem_fun(this, &TrainPanel::func_toggled), i->first));
98 tgl_funcs[i->first] = tgl;
100 train.signal_function_changed.connect(sigc::mem_fun(this, &TrainPanel::train_function_changed));
104 pnl_extra->add(*(btn = new GLtk::Button(res, "Edit")));
105 btn->set_geometry(GLtk::Geometry(10, 30, 36, 25));
106 btn->signal_clicked.connect(sigc::mem_fun(this, &TrainPanel::edit_clicked));
108 pnl_extra->add(*(btn = new GLtk::Button(res, "Place")));
109 btn->set_geometry(GLtk::Geometry(10, 0, 36, 25));
110 btn->signal_clicked.connect(sigc::mem_fun(this, &TrainPanel::place_clicked));
112 pnl_extra->add(*(btn = new GLtk::Button(res, "Take")));
113 btn->set_geometry(GLtk::Geometry(46, 0, 36, 25));
114 btn->signal_clicked.connect(sigc::mem_fun(this, &TrainPanel::take_clicked));
116 pnl_extra->add(*(btn = new GLtk::Button(res, "GoTo")));
117 btn->set_geometry(GLtk::Geometry(100, 0, 36, 25));
118 btn->signal_clicked.connect(sigc::mem_fun(this, &TrainPanel::goto_clicked));
120 pnl_extra->add(*(btn = new GLtk::Button(res, "Route")));
121 btn->set_geometry(GLtk::Geometry(100, 30, 36, 25));
122 btn->signal_clicked.connect(sigc::mem_fun(this, &TrainPanel::route_clicked));
124 pnl_extra->add(*(btn = new GLtk::Button(res, "TTbl")));
125 btn->set_geometry(GLtk::Geometry(46, 30, 36, 25));
126 btn->signal_clicked.connect(sigc::mem_fun(this, &TrainPanel::timetable_clicked));
128 pnl_extra->add(*(btn = new GLtk::Button(res, "View")));
129 btn->set_geometry(GLtk::Geometry(geom.w-46, 30, 36, 25));
130 btn->signal_clicked.connect(sigc::mem_fun(this, &TrainPanel::view_clicked));
133 void TrainPanel::expand(bool e)
136 pnl_extra->set_visible(expanded);
139 set_size(geom.w, 205);
140 btn_expand->set_style("arrow_up");
144 set_size(geom.w, 65);
145 btn_expand->set_style("arrow_down");
147 pnl_basic->set_geometry(GLtk::Geometry(0, geom.h-58, geom.w, 45));
148 engineer.rearrange_panels();
151 void TrainPanel::train_control_changed(const string &control, float value)
155 float speed = value/engineer.get_layout().get_catalogue().get_scale()*3.6;
156 sld_speed->set_value(speed);
157 lbl_speed->set_text(format("%3.0f", speed));
159 else if(control=="reverse")
160 tgl_forward->set_value(value==0);
163 void TrainPanel::train_function_changed(unsigned func, bool value)
165 map<unsigned, GLtk::Toggle *>::iterator i = tgl_funcs.find(func);
166 if(i!=tgl_funcs.end())
167 i->second->set_value(value);
170 void TrainPanel::train_route_changed(const Route *r)
173 lbl_route->set_text(r->get_name());
175 lbl_route->set_text("Free run");
178 void TrainPanel::train_status_changed(const string &s)
180 lbl_status->set_text(s);
183 void TrainPanel::place_clicked()
186 pick_conn = engineer.signal_pick_done.connect(sigc::mem_fun(this, &TrainPanel::place));
189 void TrainPanel::take_clicked()
194 void TrainPanel::edit_clicked()
196 TrainProperties *dialog = new TrainProperties(engineer, res, &train);
197 engineer.get_root().add(*dialog);
198 dialog->set_position(geom.x+geom.w, geom.y+geom.h-dialog->get_geometry().h);
201 void TrainPanel::route_clicked()
203 RouteSelect *dialog = new RouteSelect(engineer, res, train);
204 engineer.get_root().add(*dialog);
205 dialog->set_position(geom.x+geom.w, geom.y+geom.h-dialog->get_geometry().h);
208 void TrainPanel::goto_clicked()
210 engineer.pick(false);
211 pick_conn = engineer.signal_pick_done.connect(sigc::mem_fun(this, &TrainPanel::go_to));
214 void TrainPanel::timetable_clicked()
216 Timetable *timetable = train.get_timetable();
219 timetable = new Timetable(train);
220 train.set_timetable(timetable);
223 TimetableDialog *dialog = new TimetableDialog(res, *timetable);
224 engineer.get_root().add(*dialog);
225 dialog->set_position(geom.x+geom.w, geom.y+geom.h-dialog->get_geometry().h);
228 void TrainPanel::view_clicked()
230 TrainView *dialog = new TrainView(engineer, train);
231 engineer.get_root().add(*dialog);
232 dialog->set_position(geom.x+geom.w, geom.y+geom.h-dialog->get_geometry().h);
235 void TrainPanel::expand_clicked()
240 void TrainPanel::speed_slider_changed(double value)
242 float speed = value/3.6*engineer.get_layout().get_catalogue().get_scale();
243 train.set_control("speed", speed);
246 void TrainPanel::forward_toggled(bool value)
248 if(train.get_speed() || sld_speed->get_value())
250 train.set_control("speed", 0);
251 tgl_forward->set_value(!train.get_control("reverse"));
254 train.set_control("reverse", !value);
257 void TrainPanel::func_toggled(bool value, unsigned func)
259 train.set_function(func, value);
262 void TrainPanel::place(Track *track, unsigned ep)
264 pick_conn.disconnect();
266 Block &block = track->get_block();
267 TrackIter iter(track, ep);
269 while(block.has_track(*iter))
271 const vector<Block::Endpoint> &eps = block.get_endpoints();
273 for(unsigned i=0; (!ok && i<eps.size()); ++i)
274 if(eps[i].track==iter.track() && eps[i].track_ep==iter.entry())
276 train.place(block, i);
283 iter = iter.flip().reverse();
287 void TrainPanel::go_to(Track *track, unsigned)
289 pick_conn.disconnect();
291 if(!train.go_to(*track))
292 engineer.set_status("Could not set route");