From d4f5a2078027e614de4d1be76ed15550bb1b280c Mon Sep 17 00:00:00 2001 From: "Ira W. Snyder" Date: Tue, 9 Oct 2007 13:42:46 -0700 Subject: [PATCH] Make PositionLabels have direction status indicators Signed-off-by: Ira W. Snyder --- TODO | 3 --- elevator.cpp | 6 ++++-- elevatorgui.cpp | 10 ++-------- elevatorgui.hpp | 2 +- main.cpp | 6 +++--- main.hpp | 2 +- positionlabel.cpp | 37 +++++++++++++++++++++++++++++++++++-- positionlabel.hpp | 9 ++++++++- 8 files changed, 54 insertions(+), 21 deletions(-) diff --git a/TODO b/TODO index 47d2a3a..d7cfff2 100644 --- a/TODO +++ b/TODO @@ -1,6 +1,3 @@ -Make PositionLabels inherit from Gtk::Table and have them display an arrow -with their direction, as well as just an integer version of their position. - Move the PositionLabels to the top of the screen. Make the input of floors and elevators GUI based (dialog boxes) diff --git a/elevator.cpp b/elevator.cpp index f2be628..cbf9775 100644 --- a/elevator.cpp +++ b/elevator.cpp @@ -126,7 +126,7 @@ void Elevator::transition_move_up () position_ += ELEVATOR_STEP; // TODO: Call into the GUI to update the position - gui_update_position_label (number_, (float)position_); + gui_update_position_label (number_, (float)position_, direction_); std::cout << "Updating the GUI with our position: " << position_ << std::endl; } @@ -136,13 +136,15 @@ void Elevator::transition_move_down () position_ -= ELEVATOR_STEP; // TODO: Call into the GUI to update the position - gui_update_position_label (number_, (float)position_); + gui_update_position_label (number_, (float)position_, direction_); std::cout << "Updating the GUI with our position: " << position_ << std::endl; } void Elevator::transition_move_idle () { direction_ = IDLE; + // TODO: Call into the GUI to update the position + gui_update_position_label (number_, (float)position_, direction_); // do not change position while IDLE } diff --git a/elevatorgui.cpp b/elevatorgui.cpp index 630a855..2c82772 100644 --- a/elevatorgui.cpp +++ b/elevatorgui.cpp @@ -229,20 +229,14 @@ void ElevatorGUI::on_call_button_toggled (CallButton *button) } } -void ElevatorGUI::gui_update_position_label (int elevator, float new_position) +void ElevatorGUI::gui_update_position_label (int elevator, float new_position, Direction direction) { - std::ostringstream str; - - // Generate the text - str << std::setiosflags (std::ios_base::showpoint | std::ios_base::fixed) - << std::setprecision(1) << new_position; - // Find the correct label and set it PositionLabelVector::iterator it; for (it=position_labels_.begin(); it!=position_labels_.end(); it++) if ((*it)->getElevatorNumber() == elevator) - (*it)->set_text (str.str()); + (*it)->update_position (new_position, direction); } void ElevatorGUI::gui_unpress_call_button (int floor, Direction direction) diff --git a/elevatorgui.hpp b/elevatorgui.hpp index 77f4d58..3a4b1f3 100644 --- a/elevatorgui.hpp +++ b/elevatorgui.hpp @@ -27,7 +27,7 @@ class ElevatorGUI : public Gtk::Window ElevatorGUI (int floors, int elevators); /* Functions to be called from Elevator to change GUI status */ - void gui_update_position_label (int elevator, float new_position); + void gui_update_position_label (int elevator, float new_position, Direction direction); void gui_unpress_call_button (int floor, Direction direction); void gui_unpress_request_button (int elevator, int floor); void gui_open_door (int elevator, int floor); diff --git a/main.cpp b/main.cpp index f49b107..5ac3fe9 100644 --- a/main.cpp +++ b/main.cpp @@ -8,7 +8,7 @@ int main (int argc, char *argv[]) int floors = 7; int elevators = 3; -#define USE_STATIC_FLOORS 1 +//#define USE_STATIC_FLOORS 1 #ifndef USE_STATIC_FLOORS do { @@ -64,9 +64,9 @@ int main (int argc, char *argv[]) } -void gui_update_position_label (int elevator, float new_position) +void gui_update_position_label (int elevator, float new_position, Direction direction) { - thegui->gui_update_position_label (elevator, new_position); + thegui->gui_update_position_label (elevator, new_position, direction); } void gui_unpress_call_button (int floor, Direction direction) diff --git a/main.hpp b/main.hpp index 124c63e..fde418f 100644 --- a/main.hpp +++ b/main.hpp @@ -6,7 +6,7 @@ #include #include -void gui_update_position_label (int elevator, float new_position); +void gui_update_position_label (int elevator, float new_position, Direction direction); void gui_unpress_call_button (int floor, Direction direction); void gui_unpress_request_button (int elevator, int floor); void gui_open_door (int elevator, int floor); diff --git a/positionlabel.cpp b/positionlabel.cpp index bdbe24d..97a2654 100644 --- a/positionlabel.cpp +++ b/positionlabel.cpp @@ -1,10 +1,14 @@ #include "positionlabel.hpp" PositionLabel::PositionLabel (int elevator, const std::string text) - : Gtk::Label (text) + : Gtk::Table (1, 2) + , label_(text) + , direction_img_(Gtk::Stock::YES, Gtk::ICON_SIZE_BUTTON) , elevator_(elevator) { - // Intentionally Left Empty + attach (label_, 0, 1, 0, 1); + attach (direction_img_, 1, 2, 0, 1); + show (); } int PositionLabel::getElevatorNumber () const @@ -12,4 +16,33 @@ int PositionLabel::getElevatorNumber () const return elevator_; } +void PositionLabel::update_position (float floor, Direction direction) +{ + switch (direction) + { + case UP: + direction_img_.set(Gtk::Stock::GO_UP, Gtk::ICON_SIZE_BUTTON); + break; + case DOWN: + direction_img_.set(Gtk::Stock::GO_DOWN, Gtk::ICON_SIZE_BUTTON); + break; + case IDLE: + direction_img_.set(Gtk::Stock::YES, Gtk::ICON_SIZE_BUTTON); + break; + default: + std::cout << "Bad direction in PositionLabel->update_position(" << floor + << ", " << direction << ")" << std::endl; + break; + } + + std::ostringstream str; + + // Generate the text + str << std::setiosflags (std::ios_base::showpoint | std::ios_base::fixed) + << std::setprecision(1) << floor; + + label_.set_text (str.str()); +} + + /* vim: set ts=4 sts=4 sw=4 noet tw=112: */ diff --git a/positionlabel.hpp b/positionlabel.hpp index bd90449..e5bfd69 100644 --- a/positionlabel.hpp +++ b/positionlabel.hpp @@ -1,17 +1,24 @@ #ifndef POSITIONLABEL_HPP #define POSITIONLABEL_HPP +#include "direction.hpp" #include #include +#include +#include +#include -class PositionLabel : public Gtk::Label +class PositionLabel : public Gtk::Table { public: PositionLabel (int elevator, const std::string text="0.0"); int getElevatorNumber() const; + void update_position (float floor, Direction direction); private: + Gtk::Label label_; + Gtk::Image direction_img_; int elevator_; }; -- 2.34.1