Subversion Repositories programming

Rev

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