Subversion Repositories programming

Rev

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

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