]> git.tdb.fi Git - r2c2.git/commitdiff
Implement removing vehicles from a train
authorMikko Rasa <tdb@tdb.fi>
Mon, 8 Nov 2010 14:29:23 +0000 (14:29 +0000)
committerMikko Rasa <tdb@tdb.fi>
Mon, 8 Nov 2010 14:29:23 +0000 (14:29 +0000)
source/3d/layout.cpp
source/3d/layout.h
source/engineer/trainproperties.cpp
source/engineer/trainproperties.h

index 05c398d302a33b3fb22bcc968d82d90f21409abe..05aaa4205bf85053d7a0c1513253fddf5cef87b7 100644 (file)
@@ -29,6 +29,7 @@ Layout3D::Layout3D(Layout &l):
        layout.signal_track_added.connect(sigc::mem_fun(this, &Layout3D::track_added));
        layout.signal_track_removed.connect(sigc::mem_fun(this, &Layout3D::track_removed));
        layout.signal_vehicle_added.connect(sigc::mem_fun(this, &Layout3D::vehicle_added));
+       layout.signal_vehicle_removed.connect(sigc::mem_fun(this, &Layout3D::vehicle_removed));
 
        const set<Track *> &ltracks = layout.get_tracks();
        for(set<Track *>::iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
@@ -140,4 +141,11 @@ void Layout3D::vehicle_added(Vehicle &v)
        new Vehicle3D(*this, v);
 }
 
+void Layout3D::vehicle_removed(Vehicle &v)
+{
+       VehicleMap::iterator i = vehicles.find(&v);
+       if(i!=vehicles.end())
+               delete i->second;
+}
+
 } // namespace Marklin
index f2de127a0cc19d96117b4a63f3fb3be8c042921c..54269bc8b42e1d43e32bb56a9e41591a5d313a89 100644 (file)
@@ -57,6 +57,7 @@ private:
        void track_added(Track &);
        void track_removed(Track &);
        void vehicle_added(Vehicle &);
+       void vehicle_removed(Vehicle &);
 };
 
 } // namespace Marklin
index efbf842c4ee10cc84368a850f42ff0738f443f8f..bb8860b96ff12ffc8253344a88a168e484a1da35 100644 (file)
@@ -23,7 +23,7 @@ TrainProperties::TrainProperties(Engineer &e, const GLtk::Resources &r, Train *t
        engineer(e),
        train(t)
 {
-       set_size(250, 275);
+       set_size(250, 305);
 
        GLtk::Label *label;
        add(*(label = new GLtk::Label(res, "Train properties")));
@@ -61,10 +61,16 @@ TrainProperties::TrainProperties(Engineer &e, const GLtk::Resources &r, Train *t
        drp_priority->append("Express passenger");
 
        add(*(lst_vehicles = new GLtk::List(res)));
-       lst_vehicles->set_geometry(GLtk::Geometry(10, geom.h-205, geom.w-20, 100));
+       lst_vehicles->set_geometry(GLtk::Geometry(10, 100, geom.w-20, geom.h-205));
+
+       GLtk::Button *btn;
+
+       add(*(btn = new GLtk::Button(res, "Rem")));
+       btn->set_geometry(GLtk::Geometry(10, 70, 40, 25));
+       btn->signal_clicked.connect(sigc::mem_fun(this, &TrainProperties::remove_vehicle_clicked));
 
        add(*(drp_new_vehicle = new GLtk::Dropdown(res)));
-       drp_new_vehicle->set_geometry(GLtk::Geometry(10, geom.h-230, geom.w-20, 20));
+       drp_new_vehicle->set_geometry(GLtk::Geometry(10, 45, geom.w-20, 20));
        drp_new_vehicle->append("(new vehicle)");
        drp_new_vehicle->set_selected_index(0);
        for(Catalogue::VehicleMap::const_iterator i=vehs.begin(); i!=vehs.end(); ++i)
@@ -108,6 +114,9 @@ void TrainProperties::on_ok_clicked()
        train->set_name(ent_name->get_text());
        train->set_priority(drp_priority->get_selected_index()-2);
 
+       // The locomotive is vehicle 0 so we need to add 1
+       for(set<unsigned>::const_iterator i=rem_vehicles.end(); i!=rem_vehicles.begin();)
+               train->remove_vehicle(*--i+1);
        for(vector<const VehicleType *>::const_iterator i=add_vehicles.begin(); i!=add_vehicles.end(); ++i)
                train->add_vehicle(**i);
 }
@@ -124,6 +133,26 @@ void TrainProperties::new_vehicle_selected(unsigned n, const string &)
        drp_new_vehicle->set_selected_index(0);
 }
 
+void TrainProperties::remove_vehicle_clicked()
+{
+       int selected = lst_vehicles->get_selected_index();
+       if(selected<0)
+               return;
+
+       lst_vehicles->remove(selected);
+
+       unsigned n_vehicles = train->get_n_vehicles()-rem_vehicles.size();
+       if(static_cast<unsigned>(selected)>=n_vehicles)
+               add_vehicles.erase(add_vehicles.begin()+(selected-n_vehicles));
+       else
+       {
+               for(set<unsigned>::const_iterator i=rem_vehicles.begin(); i!=rem_vehicles.end(); ++i)
+                       if(*i<=static_cast<unsigned>(selected))
+                               ++selected;
+               rem_vehicles.insert(selected);
+       }
+}
+
 const VehicleType &TrainProperties::get_vehicle_type(unsigned n, bool loco)
 {
        const Catalogue::VehicleMap &vehs = engineer.get_catalogue().get_vehicles();
index c75015b08f4b27f8c5b009af0af17aa572352713..b69569f77dd47eddd2fca0435e3be3a4c8478ec7 100644 (file)
@@ -27,12 +27,14 @@ private:
        Msp::GLtk::List *lst_vehicles;
        Msp::GLtk::Dropdown *drp_new_vehicle;
        std::vector<const Marklin::VehicleType *> add_vehicles;
+       std::set<unsigned> rem_vehicles;
 
 public:
        TrainProperties(Engineer &, const Msp::GLtk::Resources &, Marklin::Train *);
 private:
        virtual void on_ok_clicked();
        void new_vehicle_selected(unsigned, const std::string &);
+       void remove_vehicle_clicked();
        const Marklin::VehicleType &get_vehicle_type(unsigned, bool);
 };