1 #include <msp/gltk/button.h>
2 #include <msp/gltk/column.h>
3 #include <msp/gltk/label.h>
4 #include <msp/gltk/row.h>
5 #include "libr2c2/track.h"
6 #include "libr2c2/tracktype.h"
7 #include "objectproperties.h"
14 ObjectProperties::ObjectProperties(const Selection &selection):
18 set_layout(new GLtk::Layout);
19 GLtk::Column col(*layout);
21 GLtk::Label *lbl_title;
23 add(*(lbl_title = new GLtk::Label("Object properties")));
24 lbl_title->set_style("title");
25 layout->set_expand(*lbl_title, true, false);
27 Object *object = selection.get_object();
28 if(Track *track = dynamic_cast<Track *>(object))
30 if(track->get_type().is_turnout())
32 if(selection.size()==1)
34 lbl_title->set_text("Turnout properties");
35 properties = new TurnoutProperties(*this, *track);
40 set<Track *> tracks = selection.get_objects<Track>();
41 if(tracks.size()==selection.size())
43 bool all_linear = true;
44 for(set<Track *>::iterator i=tracks.begin(); (all_linear && i!=tracks.end()); ++i)
45 all_linear = !(*i)->get_type().is_turnout();
48 lbl_title->set_text("Track properties");
49 properties = new TrackCircuitProperties(*this, tracks);
54 else if(selection.size()==1)
56 if(Signal *signal = dynamic_cast<Signal *>(object))
58 lbl_title->set_text("Signal properties");
59 properties = new SignalProperties(*this, *signal);
61 else if(BeamGate *gate = dynamic_cast<BeamGate *>(object))
63 lbl_title->set_text("Beam gate properties");
64 properties = new BeamGateProperties(*this, *gate);
66 else if(Terrain *terrain = dynamic_cast<Terrain *>(object))
68 lbl_title->set_text("Terrain properties");
69 properties = new TerrainProperties(*this, *terrain);
74 add(*(new GLtk::Label("No properties available")));
79 GLtk::Row row(*layout);
81 add_button(*(btn = new GLtk::Button("Cncl")), 0);
82 btn->set_style("red");
84 add_button(*(btn = new GLtk::Button("OK")), 1);
85 btn->set_style("green");
89 GLtk::Entry *ObjectProperties::add_property(const string &label, const string &value, unsigned size)
91 GLtk::Row row(*layout);
92 add(*(new GLtk::Label(label)));
94 GLtk::Entry *entry = new GLtk::Entry(value);
97 entry->set_edit_size(size, 1);
100 layout->add_constraint(*entry, GLtk::Layout::ALIGN_LEFT, *prev_widget);
106 void ObjectProperties::on_response(int code)
108 if(code==1 && properties)
113 ObjectProperties::TurnoutProperties::TurnoutProperties(ObjectProperties &p, Track &t):
115 ent_address(p.add_property("Turnout address", lexical_cast<string>(track.get_turnout_address()), 5))
118 void ObjectProperties::TurnoutProperties::apply()
120 track.set_turnout_address(lexical_cast<unsigned>(ent_address->get_text()));
124 ObjectProperties::TrackCircuitProperties::TrackCircuitProperties(ObjectProperties &p, const set<Track *> &t):
126 ent_address(p.add_property("Sensor address", string(), 5))
129 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
131 int a = (*i)->get_sensor_address();
142 ent_address->set_text(lexical_cast<string>(addr));
145 void ObjectProperties::TrackCircuitProperties::apply()
147 const string &text = ent_address->get_text();
150 unsigned addr = lexical_cast<unsigned>(text);
151 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
152 (*i)->set_sensor_address(addr);
157 ObjectProperties::SignalProperties::SignalProperties(ObjectProperties &p, Signal &s):
159 ent_address(p.add_property("Signal address", lexical_cast<string>(signal.get_address()), 5))
162 void ObjectProperties::SignalProperties::apply()
164 signal.set_address(lexical_cast<unsigned>(ent_address->get_text()));
168 ObjectProperties::BeamGateProperties::BeamGateProperties(ObjectProperties &p, BeamGate &g):
170 ent_address(p.add_property("Sensor address", lexical_cast<string>(gate.get_address()), 5))
173 void ObjectProperties::BeamGateProperties::apply()
175 gate.set_address(lexical_cast<unsigned>(ent_address->get_text()));
179 ObjectProperties::TerrainProperties::TerrainProperties(ObjectProperties &p, Terrain &t):
181 ent_width(p.add_property("Width", lexical_cast<string>(terrain.get_width()), 4)),
182 ent_height(p.add_property("Height", lexical_cast<string>(terrain.get_height()), 4))
185 void ObjectProperties::TerrainProperties::apply()
187 unsigned width = lexical_cast<unsigned>(ent_width->get_text());
188 unsigned height = lexical_cast<unsigned>(ent_height->get_text());
189 terrain.set_size(width, height);