]> git.tdb.fi Git - r2c2.git/blob - source/engineer/trainproperties.cpp
67a957959fca96c04a8b8ab9338d7f62933b5e24
[r2c2.git] / source / engineer / trainproperties.cpp
1 /* $Id$
2
3 This file is part of the MSP Märklin suite
4 Copyright © 2006-2008 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #include <msp/gltk/button.h>
9 #include <msp/strings/formatter.h>
10 #include <msp/strings/lexicalcast.h>
11 #include "engineer.h"
12 #include "trainproperties.h"
13
14 using namespace std;
15 using namespace Msp;
16 using namespace Marklin;
17
18 TrainProperties::TrainProperties(Engineer &e, GLtk::Resources &r, Train *t):
19         Widget(r),
20         Panel(r),
21         engineer(e),
22         train(t)
23 {
24         set_size(200, 95);
25
26         add(*(ent_addr=new GLtk::Entry(res)));
27         ent_addr->set_geometry(GLtk::Geometry(10, geom.h-30, 40, 20));
28
29         add(*(drp_type=new GLtk::Dropdown(res)));
30         drp_type->set_geometry(GLtk::Geometry(60, geom.h-30, geom.w-70, 20));
31
32         const map<unsigned, LocoType *> &locos = engineer.get_catalogue().get_locomotives();
33         for(map<unsigned, LocoType *>::const_iterator i=locos.begin(); i!=locos.end(); ++i)
34                 drp_type->append(format("%d %s", i->second->get_article_number(), i->second->get_name()));
35
36         add(*(ent_name=new GLtk::Entry(res, "Train")));
37         ent_name->set_geometry(GLtk::Geometry(10, geom.h-55, geom.w-20, 20));
38
39         GLtk::Button *btn;
40
41         add(*(btn=new GLtk::Button(res, "OK")));
42         btn->set_style("green");
43         btn->set_geometry(GLtk::Geometry(geom.w-40, 10, 30, 25));
44         btn->signal_clicked.connect(sigc::mem_fun(this, &TrainProperties::ok_clicked));
45
46         add(*(btn=new GLtk::Button(res, "Cncl")));
47         btn->set_style("red");
48         btn->set_geometry(GLtk::Geometry(geom.w-80, 10, 30, 25));
49         btn->signal_clicked.connect(sigc::mem_fun(this, &TrainProperties::cancel_clicked));
50 }
51
52 void TrainProperties::ok_clicked()
53 {
54         if(!train)
55         {
56                 const map<unsigned, LocoType *> &locos = engineer.get_catalogue().get_locomotives();
57                 map<unsigned, LocoType *>::const_iterator i = locos.begin();
58                 advance(i, drp_type->get_selected_index());
59                 train = engineer.add_train(*i->second, lexical_cast<unsigned>(ent_addr->get_text()));
60         }
61
62         train->set_name(ent_name->get_text());
63         signal_ok.emit();
64 }
65
66 void TrainProperties::cancel_clicked()
67 {
68         signal_cancel.emit();
69 }