]> git.tdb.fi Git - r2c2.git/blob - source/engineer/trainpanel.cpp
Code reformatting: add spaces around assignment operators
[r2c2.git] / source / engineer / trainpanel.cpp
1 /* $Id$
2
3 This file is part of the MSP Märklin suite
4 Copyright © 2006-2008 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 "libmarklin/locomotive.h"
11 #include "engineer.h"
12 #include "trainpanel.h"
13
14 using namespace std;
15 using namespace Msp;
16 using namespace Marklin;
17
18 TrainPanel::TrainPanel(Engineer &e, const GLtk::Resources &r, Train &t):
19         Panel(r),
20         engineer(e),
21         train(t)
22 {
23         set_size(200, 172);
24
25         add(*(lbl_addr=new GLtk::Label(res, format("%2d", train.get_locomotive().get_address()))));
26         lbl_addr->set_style("digital");
27         lbl_addr->set_geometry(GLtk::Geometry(10, geom.h-34, 35, 24));
28
29         add(*(lbl_name=new GLtk::Label(res, train.get_name())));
30         lbl_name->set_style("digital");
31         lbl_name->set_geometry(GLtk::Geometry(45, geom.h-34, geom.w-55, 24));
32         train.signal_name_changed.connect(sigc::mem_fun(lbl_name, &GLtk::Label::set_text));
33
34         add(*(sld_speed=new GLtk::HSlider(res)));
35         sld_speed->set_geometry(GLtk::Geometry(50, geom.h-56, geom.w-60, 10));
36         sld_speed->set_range(0, 14);
37         sld_speed->set_step(1);
38         sld_speed->signal_value_changed.connect(sigc::mem_fun(this, &TrainPanel::speed_slider_changed));
39
40         add(*(lbl_speed=new GLtk::Label(res, format("%2d", train.get_locomotive().get_speed()))));
41         lbl_speed->set_style("digital");
42         lbl_speed->set_geometry(GLtk::Geometry(10, geom.h-63, 35, 24));
43         train.get_locomotive().signal_speed_changed.connect(sigc::mem_fun(this, &TrainPanel::loco_speed_changed));
44
45         GLtk::Button *btn;
46
47         const map<unsigned, string> &funcs = train.get_locomotive().get_type().get_functions();
48         unsigned x = 10;
49         for(map<unsigned, string>::const_iterator i=funcs.begin(); i!=funcs.end(); ++i, x+=40)
50         {
51                 string fname = i->second;
52                 fname[0] = toupper(fname[0]);
53                 add(*(btn=new GLtk::Button(res, fname)));
54                 btn->set_geometry(GLtk::Geometry(x, 68, 40, 24));
55                 btn->signal_clicked.connect(sigc::bind(sigc::mem_fun(this, &TrainPanel::func_clicked), i->first));
56
57                 GLtk::Indicator *ind = new GLtk::Indicator(res);
58                 add(*ind);
59                 ind->set_geometry(GLtk::Geometry(x, 92, 40, 12));
60                 ind->set_active(train.get_locomotive().get_function(i->first));
61                 ind_funcs[i->first] = ind;
62         }
63         train.get_locomotive().signal_function_changed.connect(sigc::mem_fun(this, &TrainPanel::loco_function_changed));
64
65         add(*(lbl_status=new GLtk::Label(res, train.get_status())));
66         lbl_status->set_style("digital");
67         lbl_status->set_geometry(GLtk::Geometry(10, 39, geom.w-20, 24));
68         train.signal_status_changed.connect(sigc::mem_fun(this, &TrainPanel::train_status_changed));
69
70         add(*(btn=new GLtk::Button(res, "Edit")));
71         btn->set_geometry(GLtk::Geometry(geom.w-50, 10, 40, 24));
72
73         add(*(btn=new GLtk::Button(res, "Place")));
74         btn->set_geometry(GLtk::Geometry(geom.w-90, 10, 40, 24));
75         btn->signal_clicked.connect(sigc::mem_fun(this, &TrainPanel::place_clicked));
76 }
77
78 void TrainPanel::speed_slider_changed(double v)
79 {
80         train.set_speed(static_cast<unsigned>(v));
81 }
82
83 void TrainPanel::loco_speed_changed(unsigned speed)
84 {
85         lbl_speed->set_text(format("%2d", speed));
86 }
87
88 void TrainPanel::loco_function_changed(unsigned func, bool value)
89 {
90         map<unsigned, GLtk::Indicator *>::iterator i = ind_funcs.find(func);
91         if(i!=ind_funcs.end())
92                 i->second->set_active(value);
93 }
94
95 void TrainPanel::train_status_changed(const string &s)
96 {
97         lbl_status->set_text(s);
98 }
99
100 void TrainPanel::place_clicked()
101 {
102         engineer.place_train(train);
103 }
104
105 void TrainPanel::func_clicked(unsigned func)
106 {
107         train.get_locomotive().set_function(func, !train.get_locomotive().get_function(func));
108 }