]> git.tdb.fi Git - r2c2.git/blob - trackproperties.cpp
ce806c5d64dacbfbda94c637b5e3097c74570a49
[r2c2.git] / trackproperties.cpp
1 /* $Id$
2
3 This file is part of R²C²
4 Copyright © 2010 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #include <msp/gltk/button.h>
9 #include <msp/gltk/label.h>
10 #include "libr2c2/tracktype.h"
11 #include "selection.h"
12 #include "trackproperties.h"
13
14 using namespace std;
15 using namespace Msp;
16 using namespace R2C2;
17
18 TrackProperties::TrackProperties(const Selection &s):
19         selection(s)
20 {
21         set_size(300, 110);
22
23         GLtk::Label *lbl;
24
25         add(*(lbl = new GLtk::Label("Track properties")));
26         lbl->set_geometry(GLtk::Geometry(10, geom.h-30, geom.w-20, 20));
27
28         add(*(lbl = new GLtk::Label("Turnout ID")));
29         lbl->set_geometry(GLtk::Geometry(10, geom.h-65, 70, 20));
30
31         add(*(ent_turnout_id = new GLtk::Entry));
32         ent_turnout_id->set_geometry(GLtk::Geometry(80, geom.h-65, 50, 20));
33
34         add(*(lbl = new GLtk::Label("Sensor ID")));
35         lbl->set_geometry(GLtk::Geometry(150, geom.h-65, 70, 20));
36
37         add(*(ent_sensor_id = new GLtk::Entry));
38         ent_sensor_id->set_geometry(GLtk::Geometry(220, geom.h-65, 50, 20));
39
40         GLtk::Button *btn;
41
42         add_button(*(btn = new GLtk::Button("Cncl")), 0);
43         btn->set_geometry(GLtk::Geometry(geom.w-90, 10, 40, 24));
44         btn->set_style("red");
45
46         add_button(*(btn = new GLtk::Button("OK")), 1);
47         btn->set_geometry(GLtk::Geometry(geom.w-50, 10, 40, 24));
48         btn->set_style("green");
49
50         if(selection.size()==1)
51         {
52                 if(unsigned tid = selection.get_track()->get_turnout_id())
53                         ent_turnout_id->set_text(lexical_cast(tid));
54         }
55
56         const set<Track *> &tracks = selection.get_tracks();
57         int sensor_id = -1;
58         for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
59         {
60                 if(static_cast<int>((*i)->get_sensor_id())!=sensor_id)
61                 {
62                         if(sensor_id==-1)
63                                 sensor_id = (*i)->get_sensor_id();
64                         else
65                                 sensor_id = -2;
66                 }
67         }
68
69         if(sensor_id>=0)
70                 ent_sensor_id->set_text(lexical_cast(sensor_id));
71 }
72
73 void TrackProperties::on_response(int code)
74 {
75         if(code==1)
76         {
77                 if(selection.size()==1)
78                 {
79                         Track *track = selection.get_track();
80                         if(track->get_type().is_turnout())
81                                 track->set_turnout_id(lexical_cast<unsigned>(ent_turnout_id->get_text()));
82                 }
83
84                 string sensor_id_text = ent_sensor_id->get_text();
85                 if(!sensor_id_text.empty())
86                 {
87                         unsigned sensor_id = lexical_cast<unsigned>(sensor_id_text);
88                         const set<Track *> &tracks = selection.get_tracks();
89                         for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
90                                 if(!(*i)->get_type().is_turnout())
91                                         (*i)->set_sensor_id(sensor_id);
92                 }
93         }
94 }