Subversion Repositories programming

Rev

Rev 344 | Rev 347 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
340 ira 1
 
2
#include "elevator_window.h"
3
 
4
Elevator_Window::Elevator_Window ()
5
    : m_table  (11, 8, true)
6
{
7
    int i, j;
8
    std::ostringstream strstrm;
9
    Gtk::ToggleButton *pToggleButton;
10
    Gtk::Label *pLabel;
11
    Gtk::Image *pImage;
12
    Button_Data bData;
13
 
14
    // Set up all of the buttons
15
    upbuttons.reserve (9);
16
    downbuttons.reserve (9);
17
 
18
    for (i=0; i<9; i++)
19
    {
20
        // Create an "up" button
21
        strstrm.str ("");
22
        strstrm << "Up"; // Button " << i;
23
        pToggleButton = new Gtk::ToggleButton (strstrm.str());
24
 
25
        bData.str = strstrm.str();
26
        bData.button_num = i;
27
        bData.direction = MOVE_UP;
28
        bData.theButton = pToggleButton;
29
 
30
        pToggleButton->signal_clicked ().connect (
31
                sigc::bind<Button_Data> (sigc::mem_fun (*this, &Elevator_Window::on_toggle_button_pressed), bData));
32
        upbuttons.push_back (pToggleButton);
33
 
34
        // Put it in the table
35
        m_table.attach (*pToggleButton, 1, 2, 10-(i+1), 10-i); //i+1, i+2);
36
 
37
        // Create a "down" button
38
        strstrm.str ("");
39
        strstrm << "Down"; // Button " << i;
40
        pToggleButton = new Gtk::ToggleButton (strstrm.str());
41
 
42
        bData.str = strstrm.str();
43
        bData.button_num = i;
44
        bData.direction = MOVE_DOWN;
45
        bData.theButton = pToggleButton;
46
 
47
        pToggleButton->signal_clicked ().connect (
48
                sigc::bind<Button_Data> (sigc::mem_fun (*this, &Elevator_Window::on_toggle_button_pressed), bData));
49
        downbuttons.push_back (pToggleButton);
50
 
51
        // Put it in the table
52
        m_table.attach (*pToggleButton, 2, 3, 10-(i+2), 10-(i+1)); //i, i+1);
53
    }
54
 
55
    // Labels
56
    for (i=0; i<10; i++)
57
    {
58
        strstrm.str ("");
59
        strstrm << "Floor " << i;
60
        pLabel = new Gtk::Label (strstrm.str ());
61
 
62
        m_table.attach (*pLabel, 0, 1, 10-(i+1), 10-i);
63
    }
64
 
65
    images.reserve (10*5);
66
 
67
    // Images
68
    for (i=0; i<10; i++)
69
    {
70
        for (j=3; j<8; j++)
71
        {
72
            pImage = new Gtk::Image ("eclose.png");
73
 
74
            images.push_back (pImage);
75
 
76
            m_table.attach (*pImage, j, j+1, i, i+1);
77
        }
78
    }
79
 
80
    // Bottom Row of Labels
81
    pLabel = new Gtk::Label ("Position:");
82
    m_table.attach (*pLabel, 2, 3, 10, 11);
83
 
84
    labels.reserve (5);
85
 
86
    for (i=3; i<8; i++)
87
    {
88
        pLabel = new Gtk::Label ("0");
89
        labels.push_back (pLabel);
90
        m_table.attach (*pLabel, i, i+1, 10, 11);
91
    }
92
 
93
    set_title ("GTK Elevator Simulator");
94
 
95
    // Set the border width of the window
96
    set_border_width (10);
97
 
98
    m_table.set_row_spacings (10);
99
    m_table.set_col_spacings (10);
100
 
101
    add (m_table);
102
 
103
    show_all_children ();
104
}
105
 
106
Elevator_Window::~Elevator_Window ()
107
{
108
 
109
}
110
 
111
void Elevator_Window::on_toggle_button_pressed (Button_Data data)
112
{
113
    std::string s;
114
    bool active = false;
115
 
116
    if (data.direction == MOVE_UP)
117
    {
118
        s = "MOVE_UP";
119
        active = data.theButton->get_active ();
120
 
121
        if (active)
122
            controller->request_elevator (data.button_num, MOVE_UP);
123
    }
124
    else if (data.direction == MOVE_DOWN)
125
    {
126
        s = "MOVE_DOWN";
127
        active = data.theButton->get_active ();
128
 
129
        if (active)
130
            controller->request_elevator (data.button_num+1, MOVE_DOWN);
131
    }
132
    else
133
        s = "NOT VALID";
134
}
135
 
136
void Elevator_Window::open_elevator_at (int row, int col)
137
{
138
    assert (row >= 0);
139
    assert (row < 10);
140
    assert (col >= 0);
141
    assert (col < 5);
142
 
345 ira 143
    const int numRows = 10; // FIXME
340 ira 144
    const int numCols = 5;
145
 
146
    images.at (row*numCols+col)->set ("eopen2.png");
147
}
148
 
149
void Elevator_Window::close_elevator_at (int row, int col)
150
{
151
    assert (row >= 0);
152
    assert (row < 10);
153
    assert (col >= 0);
154
    assert (col < 5);
155
 
345 ira 156
    const int numRows = 10; // FIXME
340 ira 157
    const int numCols = 5;
344 ira 158
 
159
    images.at (row*numCols+col)->clear ();
340 ira 160
    images.at (row*numCols+col)->set ("eclose.png");
161
}
162
 
163
void Elevator_Window::unset_up_button (int button_num)
164
{
165
    assert (button_num >= 0);
166
    assert (button_num < 9);
167
 
168
    upbuttons.at (button_num)->set_active (false);
169
}
170
 
171
void Elevator_Window::unset_down_button (int button_num)
172
{
173
    assert (button_num >= 0);
174
    assert (button_num < 9);
175
 
176
    downbuttons.at (button_num)->set_active (false);
177
}
178
 
179
void Elevator_Window::set_label (int label_num, std::string new_text)
180
{
181
    assert (label_num >= 0);
182
    assert (label_num < 5);
183
 
184
    labels.at (label_num)->set_text (new_text);
185
}
186
 
187
void Elevator_Window::set_controller (Controller *controller)
188
{
189
    this->controller = controller;
190
}
191
 
344 ira 192
void Elevator_Window::get_floors_from_user (int elevator_num)
340 ira 193
{
194
    int res;
195
 
344 ira 196
    Gtk::Dialog d ("Floor Dialog", true, true);
340 ira 197
    Gtk::Entry entry;
345 ira 198
    Gtk::Label label1 ("Enter floors to stop at (space seperated): ");
199
    Gtk::Label label2 ("Enter -1 for none");
340 ira 200
 
344 ira 201
    Gtk::Box *pBox = d.get_vbox();
340 ira 202
 
344 ira 203
    pBox->set_spacing (10);
204
 
345 ira 205
    pBox->pack_start (label1);
206
    pBox->pack_start (label2);
344 ira 207
    pBox->pack_start (entry);
208
 
345 ira 209
    label1.show ();
210
    label2.show ();
344 ira 211
    entry.show ();
212
 
213
    d.add_button ("gtk-ok", 7);
214
 
215
    // Run the Dialog
340 ira 216
    res = d.run ();
217
 
344 ira 218
    // Parse the entry
219
    std::string s = entry.get_text();
220
    std::vector<std::string> SplitVec;
221
    boost::split (SplitVec, s, boost::is_any_of(" "));
222
 
223
    const int num_floors = 10; // FIXME
224
 
225
    int i, val;
226
    for (i=0; i<SplitVec.size(); i++)
227
    {
228
        val = atoi (SplitVec[i].c_str());
229
 
230
        // Ignore values that are outside of range
231
        if (val >= 0 && val < num_floors)
232
        {
233
            controller->push_button_in_elevator (elevator_num, val);
234
            std::cout << "Pushing button: " << val << std::endl;
235
        }
236
    }
340 ira 237
}
238