Make PositionLabels have direction status indicators
authorIra W. Snyder <devel@irasnyder.com>
Tue, 9 Oct 2007 20:42:46 +0000 (13:42 -0700)
committerIra W. Snyder <devel@irasnyder.com>
Tue, 9 Oct 2007 20:42:46 +0000 (13:42 -0700)
Signed-off-by: Ira W. Snyder <devel@irasnyder.com>
TODO
elevator.cpp
elevatorgui.cpp
elevatorgui.hpp
main.cpp
main.hpp
positionlabel.cpp
positionlabel.hpp

diff --git a/TODO b/TODO
index 47d2a3a..d7cfff2 100644 (file)
--- 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)
index f2be628..cbf9775 100644 (file)
@@ -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
 }
 
index 630a855..2c82772 100644 (file)
@@ -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)
index 77f4d58..3a4b1f3 100644 (file)
@@ -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);
index f49b107..5ac3fe9 100644 (file)
--- 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)
index 124c63e..fde418f 100644 (file)
--- a/main.hpp
+++ b/main.hpp
@@ -6,7 +6,7 @@
 #include <iostream>
 #include <gtkmm/main.h>
 
-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);
index bdbe24d..97a2654 100644 (file)
@@ -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: */
index bd90449..e5bfd69 100644 (file)
@@ -1,17 +1,24 @@
 #ifndef POSITIONLABEL_HPP
 #define POSITIONLABEL_HPP
 
+#include "direction.hpp"
 #include <gtkmm.h>
 #include <string>
+#include <sstream>
+#include <iostream>
+#include <iomanip>
 
-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_;
 };