Subversion Repositories programming

Rev

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


#include "elevator_window.h"

Elevator_Window::Elevator_Window ()
    : m_table  (11, 8, true)
{
    int i, j;
    std::ostringstream strstrm;
    Gtk::ToggleButton *pToggleButton;
    Gtk::Label *pLabel;
    Gtk::Image *pImage;
    Button_Data bData;

    // Set up all of the buttons
    upbuttons.reserve (9);
    downbuttons.reserve (9);

    for (i=0; i<9; i++)
    {
        // Create an "up" button
        strstrm.str ("");
        strstrm << "Up"; // Button " << i;
        pToggleButton = new Gtk::ToggleButton (strstrm.str());

        bData.str = strstrm.str();
        bData.button_num = i;
        bData.direction = MOVE_UP;
        bData.theButton = pToggleButton;

        pToggleButton->signal_clicked ().connect (
                sigc::bind<Button_Data> (sigc::mem_fun (*this, &Elevator_Window::on_toggle_button_pressed), bData));
        upbuttons.push_back (pToggleButton);

        // Put it in the table
        m_table.attach (*pToggleButton, 1, 2, 10-(i+1), 10-i); //i+1, i+2);

        // Create a "down" button
        strstrm.str ("");
        strstrm << "Down"; // Button " << i;
        pToggleButton = new Gtk::ToggleButton (strstrm.str());

        bData.str = strstrm.str();
        bData.button_num = i;
        bData.direction = MOVE_DOWN;
        bData.theButton = pToggleButton;

        pToggleButton->signal_clicked ().connect (
                sigc::bind<Button_Data> (sigc::mem_fun (*this, &Elevator_Window::on_toggle_button_pressed), bData));
        downbuttons.push_back (pToggleButton);

        // Put it in the table
        m_table.attach (*pToggleButton, 2, 3, 10-(i+2), 10-(i+1)); //i, i+1);
    }

    // Labels
    for (i=0; i<10; i++)
    {
        strstrm.str ("");
        strstrm << "Floor " << i;
        pLabel = new Gtk::Label (strstrm.str ());

        m_table.attach (*pLabel, 0, 1, 10-(i+1), 10-i);
    }

    images.reserve (10*5);

    // Images
    for (i=0; i<10; i++)
    {
        for (j=3; j<8; j++)
        {
            pImage = new Gtk::Image ("eclose.png");

            images.push_back (pImage);

            m_table.attach (*pImage, j, j+1, i, i+1);
        }
    }

    // Bottom Row of Labels
    pLabel = new Gtk::Label ("Position:");
    m_table.attach (*pLabel, 2, 3, 10, 11);

    labels.reserve (5);

    for (i=3; i<8; i++)
    {
        pLabel = new Gtk::Label ("0");
        labels.push_back (pLabel);
        m_table.attach (*pLabel, i, i+1, 10, 11);
    }

    set_title ("GTK Elevator Simulator");

    // Set the border width of the window
    set_border_width (10);

    m_table.set_row_spacings (10);
    m_table.set_col_spacings (10);

    add (m_table);

    show_all_children ();
}

Elevator_Window::~Elevator_Window ()
{

}

void Elevator_Window::on_toggle_button_pressed (Button_Data data)
{
    std::string s;
    bool active = false;

    if (data.direction == MOVE_UP)
    {
        s = "MOVE_UP";
        active = data.theButton->get_active ();

        if (active)
            controller->request_elevator (data.button_num, MOVE_UP);
    }
    else if (data.direction == MOVE_DOWN)
    {
        s = "MOVE_DOWN";
        active = data.theButton->get_active ();

        if (active)
            controller->request_elevator (data.button_num+1, MOVE_DOWN);
    }
    else
        s = "NOT VALID";
}

void Elevator_Window::open_elevator_at (int row, int col)
{
    assert (row >= 0);
    assert (row < 10);
    assert (col >= 0);
    assert (col < 5);

    const int numRows = 10; // FIXME
    const int numCols = 5;

    images.at (row*numCols+col)->set ("eopen2.png");
}

void Elevator_Window::close_elevator_at (int row, int col)
{
    assert (row >= 0);
    assert (row < 10);
    assert (col >= 0);
    assert (col < 5);

    const int numRows = 10; // FIXME
    const int numCols = 5;
    
    images.at (row*numCols+col)->clear ();
    images.at (row*numCols+col)->set ("eclose.png");
}

void Elevator_Window::unset_up_button (int button_num)
{
    assert (button_num >= 0);
    assert (button_num < 9);

    upbuttons.at (button_num)->set_active (false);
}

void Elevator_Window::unset_down_button (int button_num)
{
    assert (button_num >= 0);
    assert (button_num < 9);

    downbuttons.at (button_num)->set_active (false);
}

void Elevator_Window::set_label (int label_num, std::string new_text)
{
    assert (label_num >= 0);
    assert (label_num < 5);

    labels.at (label_num)->set_text (new_text);
}

void Elevator_Window::set_controller (Controller *controller)
{
    this->controller = controller;
}

void Elevator_Window::get_floors_from_user (int elevator_num)
{
    int res;

    Gtk::Dialog d ("Floor Dialog", true, true);
    Gtk::Entry entry;
    Gtk::Label label1 ("Enter floors to stop at (space seperated): ");
    Gtk::Label label2 ("Enter -1 for none");

    Gtk::Box *pBox = d.get_vbox();

    pBox->set_spacing (10);

    pBox->pack_start (label1);
    pBox->pack_start (label2);
    pBox->pack_start (entry);

    label1.show ();
    label2.show ();
    entry.show ();

    d.add_button ("gtk-ok", 7);

    // Run the Dialog
    res = d.run ();

    // Parse the entry
    std::string s = entry.get_text();
    std::vector<std::string> SplitVec;
    boost::split (SplitVec, s, boost::is_any_of(" "));

    const int num_floors = 10; // FIXME

    int i, val;
    for (i=0; i<SplitVec.size(); i++)
    {
        val = atoi (SplitVec[i].c_str());

        // Ignore values that are outside of range
        if (val >= 0 && val < num_floors)
        {
            controller->push_button_in_elevator (elevator_num, val);
            std::cout << "Pushing button: " << val << std::endl;
        }
    }
}