]> git.tdb.fi Git - r2c2.git/blobdiff - source/engineer/trainproperties.cpp
Match the API change in GLtk
[r2c2.git] / source / engineer / trainproperties.cpp
index bdd39fb97622f2e3c2f707ec892796abc0958ea2..bf0b270ca3de41556e3219fd81ea3ee73f70dc71 100644 (file)
@@ -17,27 +17,25 @@ using namespace std;
 using namespace Msp;
 using namespace Marklin;
 
-TrainProperties::TrainProperties(Engineer &e, const GLtk::Resources &r, Train *t):
-       Widget(r),
-       Dialog(r),
+TrainProperties::TrainProperties(Engineer &e, Train *t):
        engineer(e),
        train(t)
 {
-       set_size(200, 275);
+       set_size(250, 305);
 
        GLtk::Label *label;
-       add(*(label = new GLtk::Label(res, "Train properties")));
+       add(*(label = new GLtk::Label("Train properties")));
        label->set_geometry(GLtk::Geometry(10, geom.h-25, geom.w-20, 20));
 
-       add(*(ent_addr = new GLtk::Entry(res)));
+       add(*(ent_addr = new GLtk::Entry));
        ent_addr->set_geometry(GLtk::Geometry(10, geom.h-50, 40, 20));
 
-       add(*(drp_type = new GLtk::Dropdown(res)));
+       add(*(drp_type = new GLtk::Dropdown));
        drp_type->set_geometry(GLtk::Geometry(60, geom.h-50, geom.w-70, 20));
 
-       const map<unsigned, VehicleType *> &vehs = engineer.get_catalogue().get_vehicles();
+       const Catalogue::VehicleMap &vehs = engineer.get_catalogue().get_vehicles();
        unsigned n = 0;
-       for(map<unsigned, VehicleType *>::const_iterator i=vehs.begin(); i!=vehs.end(); ++i, ++n)
+       for(Catalogue::VehicleMap::const_iterator i=vehs.begin(); i!=vehs.end(); ++i)
        {
                if(!i->second->is_locomotive())
                        continue;
@@ -45,12 +43,14 @@ TrainProperties::TrainProperties(Engineer &e, const GLtk::Resources &r, Train *t
                drp_type->append(format("%d %s", i->second->get_article_number(), i->second->get_name()));
                if(train && i->second==&train->get_locomotive_type())
                        drp_type->set_selected_index(n);
+
+               ++n;
        }
 
-       add(*(ent_name = new GLtk::Entry(res)));
+       add(*(ent_name = new GLtk::Entry));
        ent_name->set_geometry(GLtk::Geometry(10, geom.h-75, geom.w-20, 20));
 
-       add(*(drp_priority = new GLtk::Dropdown(res)));
+       add(*(drp_priority = new GLtk::Dropdown));
        drp_priority->set_geometry(GLtk::Geometry(10, geom.h-100, geom.w-20, 20));
        drp_priority->append("Standard freight");
        drp_priority->append("Express freight");
@@ -58,14 +58,20 @@ TrainProperties::TrainProperties(Engineer &e, const GLtk::Resources &r, Train *t
        drp_priority->append("Standard passenger");
        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));
+       add(*(lst_vehicles = new GLtk::List));
+       lst_vehicles->set_geometry(GLtk::Geometry(10, 100, geom.w-20, geom.h-205));
+
+       GLtk::Button *btn;
+
+       add(*(btn = new GLtk::Button("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));
+       add(*(drp_new_vehicle = new GLtk::Dropdown));
+       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(map<unsigned, VehicleType *>::const_iterator i=vehs.begin(); i!=vehs.end(); ++i)
+       for(Catalogue::VehicleMap::const_iterator i=vehs.begin(); i!=vehs.end(); ++i)
        {
                if(i->second->is_locomotive())
                        continue;
@@ -106,6 +112,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);
 }
@@ -122,10 +131,30 @@ 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 map<unsigned, VehicleType *> &vehs = engineer.get_catalogue().get_vehicles();
-       map<unsigned, VehicleType *>::const_iterator i = vehs.begin();
+       const Catalogue::VehicleMap &vehs = engineer.get_catalogue().get_vehicles();
+       Catalogue::VehicleMap::const_iterator i = vehs.begin();
        while(i!=vehs.end())
        {
                if(i->second->is_locomotive()==loco)