]> git.tdb.fi Git - r2c2.git/blob - source/designer/zoneproperties.cpp
Allow zones with no qualifier or no number
[r2c2.git] / source / designer / zoneproperties.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/block.h"
6 #include "libr2c2/track.h"
7 #include "zoneproperties.h"
8
9 using namespace std;
10 using namespace Msp;
11 using namespace R2C2;
12
13 string track_block_name(Track *const &track)
14 {
15         if(track)
16                 return track->get_block().get_name();
17         else
18                 return "(unspecified)";
19 }
20
21 ZoneProperties::ZoneProperties(Zone &z):
22         zone(z),
23         up_directions(&track_block_name)
24 {
25         set_layout(new GLtk::Layout);
26         GLtk::Column col(*layout);
27
28         GLtk::Label *lbl1, *lbl2;
29
30         add(*(lbl1 = new GLtk::Label("Zone properties")));
31         lbl1->set_style("title");
32
33         {
34                 GLtk::Row row(*layout);
35                 add(*(lbl1 = new GLtk::Label("Group")));
36                 add(*(ent_group = new GLtk::Entry(zone.get_group())));
37                 ent_group->set_edit_size(30, 1);
38         }
39
40         {
41                 GLtk::Row row(*layout);
42                 add(*(lbl2 = new GLtk::Label("Qualifier")));
43                 layout->add_constraint(*lbl1, GLtk::Layout::COPY_WIDTH, *lbl2);
44
45                 add(*(drp_qualifier = new GLtk::Dropdown));
46                 GLtk::ListDataStore<string> &data = dynamic_cast<GLtk::ListDataStore<string> &>(drp_qualifier->get_data());
47                 const char *qualifiers[] = { "(none)", "track", "platform", "siding", 0 };
48                 for(unsigned i=0; qualifiers[i]; ++i)
49                 {
50                         data.append(qualifiers[i]);
51                         if(zone.get_qualifier()==qualifiers[i])
52                                 drp_qualifier->set_selected_index(i);
53                 }
54
55                 if(zone.get_qualifier().empty())
56                         drp_qualifier->set_selected_index(0);
57         }
58
59         {
60                 GLtk::Row row(*layout);
61                 add(*(lbl1 = new GLtk::Label("Number")));
62                 layout->add_constraint(*lbl1, GLtk::Layout::COPY_WIDTH, *lbl2);
63                 unsigned n = zone.get_number();
64                 add(*(ent_number = new GLtk::Entry(n ? lexical_cast<string>(zone.get_number()) : string())));
65                 ent_number->set_edit_size(4, 1);
66         }
67
68         {
69                 GLtk::Row row(*layout);
70                 add(*(lbl2 = new GLtk::Label("Up towards")));
71                 layout->add_constraint(*lbl1, GLtk::Layout::COPY_WIDTH, *lbl2);
72                 add(*(drp_up_direction = new GLtk::Dropdown(up_directions)));
73
74                 up_directions.append(0);
75
76                 Track *up = zone.get_end(TrackChain::UP).next().track();
77                 if(!up)
78                         drp_up_direction->set_selected_index(0);
79
80                 for(unsigned i=0; i<2; ++i)
81                         if(TrackIter iter = zone.get_end(i).next())
82                         {
83                                 up_directions.append(iter.track());
84                                 if(iter.track()==up)
85                                         drp_up_direction->set_selected_index(up_directions.size()-1);
86                         }
87         }
88
89         {
90                 GLtk::Row row(*layout);
91                 add(*(lbl1 = new GLtk::Label("Direction")));
92                 layout->add_constraint(*lbl1, GLtk::Layout::COPY_WIDTH, *lbl2);
93
94                 add(*(drp_preferred_dir = new GLtk::Dropdown));
95                 GLtk::ListDataStore<string> &data = dynamic_cast<GLtk::ListDataStore<string> &>(drp_preferred_dir->get_data());
96                 const char *directions[] = { "either", "up", "down", 0 };
97                 for(unsigned i=0; directions[i]; ++i)
98                 {
99                         data.append(directions[i]);
100                         if(zone.get_preferred_direction()==i)
101                                 drp_preferred_dir->set_selected_index(i);
102                 }
103         }
104
105         GLtk::Button *btn;
106
107         {
108                 GLtk::Row row(*layout);
109                 row.split();
110                 add_button(*(btn = new GLtk::Button("Cncl")), 0);
111                 btn->set_style("red");
112
113                 add_button(*(btn = new GLtk::Button("OK")), 1);
114                 btn->set_style("green");
115         }
116 }
117
118 void ZoneProperties::on_response(int code)
119 {
120         if(code==1)
121         {
122                 string qualifier;
123                 int sel = drp_qualifier->get_selected_index();
124                 if(sel>0)
125                         qualifier = drp_qualifier->get_data().get_string(sel);
126
127                 unsigned number = 0;
128                 string num_str = ent_number->get_text();
129                 if(!num_str.empty())
130                         number = lexical_cast<unsigned>(ent_number->get_text());
131
132                 zone.set_name(ent_group->get_text(), qualifier, number);
133
134                 sel = drp_up_direction->get_selected_index();
135                 if(sel==0)
136                         zone.clear_direction();
137                 else
138                         zone.set_direction_towards(*up_directions.get(sel), TrackChain::UP);
139
140                 if(zone.has_direction())
141                 {
142                         sel = drp_preferred_dir->get_selected_index();
143                         zone.set_preferred_direction(static_cast<TrackChain::Direction>(sel));
144                 }
145         }
146 }