]> git.tdb.fi Git - r2c2.git/blob - source/designer/zoneproperties.cpp
b1380c5d25f779de3ee627ea4225c2843995ae17
[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[] = { "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
56         {
57                 GLtk::Row row(*layout);
58                 add(*(lbl1 = new GLtk::Label("Number")));
59                 layout->add_constraint(*lbl1, GLtk::Layout::COPY_WIDTH, *lbl2);
60                 add(*(ent_number = new GLtk::Entry(lexical_cast<string>(zone.get_number()))));
61                 ent_number->set_edit_size(4, 1);
62         }
63
64         {
65                 GLtk::Row row(*layout);
66                 add(*(lbl2 = new GLtk::Label("Up towards")));
67                 layout->add_constraint(*lbl1, GLtk::Layout::COPY_WIDTH, *lbl2);
68                 add(*(drp_up_direction = new GLtk::Dropdown(up_directions)));
69
70                 up_directions.append(0);
71
72                 Track *up = zone.get_end(TrackChain::UP).next().track();
73                 if(!up)
74                         drp_up_direction->set_selected_index(0);
75
76                 for(unsigned i=0; i<2; ++i)
77                         if(TrackIter iter = zone.get_end(i).next())
78                         {
79                                 up_directions.append(iter.track());
80                                 if(iter.track()==up)
81                                         drp_up_direction->set_selected_index(up_directions.size()-1);
82                         }
83         }
84
85         {
86                 GLtk::Row row(*layout);
87                 add(*(lbl1 = new GLtk::Label("Direction")));
88                 layout->add_constraint(*lbl1, GLtk::Layout::COPY_WIDTH, *lbl2);
89
90                 add(*(drp_preferred_dir = new GLtk::Dropdown));
91                 GLtk::ListDataStore<string> &data = dynamic_cast<GLtk::ListDataStore<string> &>(drp_preferred_dir->get_data());
92                 const char *directions[] = { "either", "up", "down", 0 };
93                 for(unsigned i=0; directions[i]; ++i)
94                 {
95                         data.append(directions[i]);
96                         if(zone.get_preferred_direction()==i)
97                                 drp_preferred_dir->set_selected_index(i);
98                 }
99         }
100
101         GLtk::Button *btn;
102
103         {
104                 GLtk::Row row(*layout);
105                 row.split();
106                 add_button(*(btn = new GLtk::Button("Cncl")), 0);
107                 btn->set_style("red");
108
109                 add_button(*(btn = new GLtk::Button("OK")), 1);
110                 btn->set_style("green");
111         }
112 }
113
114 void ZoneProperties::on_response(int code)
115 {
116         if(code==1)
117         {
118                 string qualifier;
119                 int sel = drp_qualifier->get_selected_index();
120                 if(sel>=0)
121                         qualifier = drp_qualifier->get_data().get_string(sel);
122                 unsigned number = lexical_cast<unsigned>(ent_number->get_text());
123                 zone.set_name(ent_group->get_text(), qualifier, number);
124
125                 sel = drp_up_direction->get_selected_index();
126                 if(sel==0)
127                         zone.clear_direction();
128                 else
129                         zone.set_direction_towards(*up_directions.get(sel), TrackChain::UP);
130
131                 if(zone.has_direction())
132                 {
133                         sel = drp_preferred_dir->get_selected_index();
134                         zone.set_preferred_direction(static_cast<TrackChain::Direction>(sel));
135                 }
136         }
137 }