]> git.tdb.fi Git - r2c2.git/blobdiff - source/engineer/trainproperties.cpp
Maintain a Block pointer in Track
[r2c2.git] / source / engineer / trainproperties.cpp
index be3ba8daa410091746b9efb3cde1f7407b30aa1b..81463d28498a6493281cecbf5558ac8fc927f5a2 100644 (file)
@@ -1,13 +1,15 @@
 /* $Id$
 
 This file is part of the MSP Märklin suite
-Copyright © 2006-2008 Mikkosoft Productions, Mikko Rasa
+Copyright © 2006-2010  Mikkosoft Productions, Mikko Rasa
 Distributed under the GPL
 */
 
-#include <msp/gltk/button.h>
+#include <msp/gltk/label.h>
 #include <msp/strings/formatter.h>
 #include <msp/strings/lexicalcast.h>
+#include "libmarklin/vehicle.h"
+#include "libmarklin/vehicletype.h"
 #include "engineer.h"
 #include "trainproperties.h"
 
@@ -15,53 +17,127 @@ using namespace std;
 using namespace Msp;
 using namespace Marklin;
 
-TrainProperties::TrainProperties(Engineer &e, GLtk::Resources &r, Train *t):
-       Panel(r),
+TrainProperties::TrainProperties(Engineer &e, const GLtk::Resources &r, Train *t):
+       Widget(r),
+       Dialog(r),
        engineer(e),
        train(t)
 {
-       set_size(200, 95);
+       set_size(250, 275);
 
-       add(*(ent_addr=new GLtk::Entry(res)));
-       ent_addr->set_geometry(GLtk::Geometry(10, geom.h-30, 40, 20));
+       GLtk::Label *label;
+       add(*(label = new GLtk::Label(res, "Train properties")));
+       label->set_geometry(GLtk::Geometry(10, geom.h-25, geom.w-20, 20));
 
-       add(*(drp_type=new GLtk::Dropdown(res)));
-       drp_type->set_geometry(GLtk::Geometry(60, geom.h-30, geom.w-70, 20));
+       add(*(ent_addr = new GLtk::Entry(res)));
+       ent_addr->set_geometry(GLtk::Geometry(10, geom.h-50, 40, 20));
+
+       add(*(drp_type = new GLtk::Dropdown(res)));
+       drp_type->set_geometry(GLtk::Geometry(60, geom.h-50, geom.w-70, 20));
+
+       const map<unsigned, VehicleType *> &vehs = engineer.get_catalogue().get_vehicles();
+       unsigned n = 0;
+       for(map<unsigned, VehicleType *>::const_iterator i=vehs.begin(); i!=vehs.end(); ++i)
+       {
+               if(!i->second->is_locomotive())
+                       continue;
 
-       const map<unsigned, LocoType *> &locos = engineer.get_catalogue().get_locomotives();
-       for(map<unsigned, LocoType *>::const_iterator i=locos.begin(); i!=locos.end(); ++i)
                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)));
+       ent_name->set_geometry(GLtk::Geometry(10, geom.h-75, geom.w-20, 20));
 
-       add(*(ent_name=new GLtk::Entry(res, "Train")));
-       ent_name->set_geometry(GLtk::Geometry(10, geom.h-55, geom.w-20, 20));
+       add(*(drp_priority = new GLtk::Dropdown(res)));
+       drp_priority->set_geometry(GLtk::Geometry(10, geom.h-100, geom.w-20, 20));
+       drp_priority->append("Standard freight");
+       drp_priority->append("Express freight");
+       drp_priority->append("Unspecified");
+       drp_priority->append("Standard passenger");
+       drp_priority->append("Express passenger");
 
-       GLtk::Button *btn;
+       add(*(lst_vehicles = new GLtk::List(res)));
+       lst_vehicles->set_geometry(GLtk::Geometry(10, geom.h-205, geom.w-20, 100));
 
-       add(*(btn=new GLtk::Button(res, "OK")));
-       btn->set_style("green");
-       btn->set_geometry(GLtk::Geometry(geom.w-40, 10, 30, 25));
-       btn->signal_clicked.connect(sigc::mem_fun(this, &TrainProperties::ok_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->append("(new vehicle)");
+       drp_new_vehicle->set_selected_index(0);
+       for(map<unsigned, VehicleType *>::const_iterator i=vehs.begin(); i!=vehs.end(); ++i)
+       {
+               if(i->second->is_locomotive())
+                       continue;
+
+               drp_new_vehicle->append(format("%d %s", i->second->get_article_number(), i->second->get_name()));
+       }
+       drp_new_vehicle->signal_item_selected.connect(sigc::mem_fun(this, &TrainProperties::new_vehicle_selected));
 
-       add(*(btn=new GLtk::Button(res, "Cncl")));
-       btn->set_style("red");
-       btn->set_geometry(GLtk::Geometry(geom.w-80, 10, 30, 25));
-       btn->signal_clicked.connect(sigc::mem_fun(this, &TrainProperties::cancel_clicked));
+       if(train)
+       {
+               ent_addr->set_text(lexical_cast(train->get_address()));
+               ent_name->set_text(train->get_name());
+               drp_priority->set_selected_index(train->get_priority()+2);
+
+               unsigned n_vehicles = train->get_n_vehicles();
+               for(unsigned i=1; i<n_vehicles; ++i)
+               {
+                       const VehicleType &type = train->get_vehicle(i).get_type();
+                       lst_vehicles->append(format("%d %s", type.get_article_number(), type.get_name()));
+               }
+       }
+       else
+       {
+               ent_name->set_text(format("Train %d", engineer.get_layout().get_trains().size()+1));
+               drp_priority->set_selected_index(2);
+       }
 }
 
-void TrainProperties::ok_clicked()
+void TrainProperties::on_ok_clicked()
 {
        if(!train)
        {
-               const map<unsigned, LocoType *> &locos = engineer.get_catalogue().get_locomotives();
-               map<unsigned, LocoType *>::const_iterator i = locos.begin();
-               advance(i, drp_type->get_selected_index());
-               train = engineer.add_train(*i->second, lexical_cast<unsigned>(ent_addr->get_text()));
+               const VehicleType &type = get_vehicle_type(drp_type->get_selected_index(), true);
+               unsigned addr = lexical_cast<unsigned>(ent_addr->get_text());
+               train = new Train(engineer.get_layout(), type, addr);
        }
 
        train->set_name(ent_name->get_text());
-       signal_ok.emit();
+       train->set_priority(drp_priority->get_selected_index()-2);
+
+       for(vector<const VehicleType *>::const_iterator i=add_vehicles.begin(); i!=add_vehicles.end(); ++i)
+               train->add_vehicle(**i);
 }
 
-void TrainProperties::cancel_clicked()
+void TrainProperties::new_vehicle_selected(unsigned n, const string &)
 {
+       if(n==0)
+               return;
+
+       const VehicleType &type = get_vehicle_type(n-1, false);
+       add_vehicles.push_back(&type);
+       lst_vehicles->append(format("%d %s", type.get_article_number(), type.get_name()));
+
+       drp_new_vehicle->set_selected_index(0);
+}
+
+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();
+       while(i!=vehs.end())
+       {
+               if(i->second->is_locomotive()==loco)
+               {
+                       if(!n)
+                               return *i->second;
+                       --n;
+               }
+               ++i;
+       }
+
+       throw InvalidParameterValue("Vehicle type index out of range");
 }