]> git.tdb.fi Git - r2c2.git/blob - source/designer/objectproperties.cpp
Generalize TrackProperties to all object types
[r2c2.git] / source / designer / objectproperties.cpp
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"
8 #include "selection.h"
9
10 using namespace std;
11 using namespace Msp;
12 using namespace R2C2;
13
14 ObjectProperties::ObjectProperties(const Selection &selection):
15         properties(0),
16         prev_widget(0)
17 {
18         set_layout(new GLtk::Layout);
19         GLtk::Column col(*layout);
20
21         GLtk::Label *lbl_title;
22
23         add(*(lbl_title = new GLtk::Label("Object properties")));
24         lbl_title->set_style("title");
25         layout->set_expand(*lbl_title, true, false);
26
27         Object *object = selection.get_object();
28         if(Track *track = dynamic_cast<Track *>(object))
29         {
30                 if(track->get_type().is_turnout())
31                 {
32                         if(selection.size()==1)
33                         {
34                                 lbl_title->set_text("Turnout properties");
35                                 properties = new TurnoutProperties(*this, *track);
36                         }
37                 }
38                 else
39                 {
40                         set<Track *> tracks = selection.get_objects<Track>();
41                         if(tracks.size()==selection.size())
42                         {
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();
46                                 if(all_linear)
47                                 {
48                                         lbl_title->set_text("Track properties");
49                                         properties = new TrackCircuitProperties(*this, tracks);
50                                 }
51                         }
52                 }
53         }
54         else if(selection.size()==1)
55         {
56                 if(Signal *signal = dynamic_cast<Signal *>(object))
57                 {
58                         lbl_title->set_text("Signal properties");
59                         properties = new SignalProperties(*this, *signal);
60                 }
61                 else if(BeamGate *gate = dynamic_cast<BeamGate *>(object))
62                 {
63                         lbl_title->set_text("Beam gate properties");
64                         properties = new BeamGateProperties(*this, *gate);
65                 }
66         }
67
68         if(!properties)
69                 add(*(new GLtk::Label("No properties available")));
70
71         GLtk::Button *btn;
72
73         {
74                 GLtk::Row row(*layout);
75                 row.split();
76                 add_button(*(btn = new GLtk::Button("Cncl")), 0);
77                 btn->set_style("red");
78
79                 add_button(*(btn = new GLtk::Button("OK")), 1);
80                 btn->set_style("green");
81         }
82 }
83
84 GLtk::Entry *ObjectProperties::add_property(const string &label, const string &value, unsigned size)
85 {
86         GLtk::Row row(*layout);
87         add(*(new GLtk::Label(label)));
88
89         GLtk::Entry *entry = new GLtk::Entry(value);
90         add(*entry);
91         if(size)
92                 entry->set_edit_size(size, 1);
93
94         if(prev_widget)
95                 layout->add_constraint(*entry, GLtk::Layout::ALIGN_LEFT, *prev_widget);
96         prev_widget = entry;
97
98         return entry;
99 }
100
101 void ObjectProperties::on_response(int code)
102 {
103         if(code==1 && properties)
104                 properties->apply();
105 }
106
107
108 ObjectProperties::TurnoutProperties::TurnoutProperties(ObjectProperties &p, Track &t):
109         track(t),
110         ent_address(p.add_property("Turnout address", lexical_cast<string>(track.get_turnout_address()), 5))
111 { }
112
113 void ObjectProperties::TurnoutProperties::apply()
114 {
115         track.set_turnout_address(lexical_cast<unsigned>(ent_address->get_text()));
116 }
117
118
119 ObjectProperties::TrackCircuitProperties::TrackCircuitProperties(ObjectProperties &p, const set<Track *> &t):
120         tracks(t),
121         ent_address(p.add_property("Sensor address", string(), 5))
122 {
123         int addr = -1;
124         for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
125         {
126                 int a = (*i)->get_sensor_address();
127                 if(a!=addr)
128                 {
129                         if(addr==-1)
130                                 addr = a;
131                         else
132                                 addr = -2;
133                 }
134         }
135
136         if(addr>=0)
137                 ent_address->set_text(lexical_cast<string>(addr));
138 }
139
140 void ObjectProperties::TrackCircuitProperties::apply()
141 {
142         const string &text = ent_address->get_text();
143         if(!text.empty())
144         {
145                 unsigned addr = lexical_cast<unsigned>(text);
146                 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
147                         (*i)->set_sensor_address(addr);
148         }
149 }
150
151
152 ObjectProperties::SignalProperties::SignalProperties(ObjectProperties &p, Signal &s):
153         signal(s),
154         ent_address(p.add_property("Signal address", lexical_cast<string>(signal.get_address()), 5))
155 { }
156
157 void ObjectProperties::SignalProperties::apply()
158 {
159         signal.set_address(lexical_cast<unsigned>(ent_address->get_text()));
160 }
161
162
163 ObjectProperties::BeamGateProperties::BeamGateProperties(ObjectProperties &p, BeamGate &g):
164         gate(g),
165         ent_address(p.add_property("Sensor address", lexical_cast<string>(gate.get_address()), 5))
166 { }
167
168 void ObjectProperties::BeamGateProperties::apply()
169 {
170         gate.set_address(lexical_cast<unsigned>(ent_address->get_text()));
171 }