]> git.tdb.fi Git - r2c2.git/blob - source/libmarklin/catalogue.cpp
beab69a62ae387cf37b54745518472ac14ee0ef9
[r2c2.git] / source / libmarklin / catalogue.cpp
1 /* $Id$
2
3 This file is part of the MSP Märklin suite
4 Copyright © 2006-2010  Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #include <msp/core/refptr.h>
9 #include <msp/datafile/parser.h>
10 #include "catalogue.h"
11 #include "tracktype.h"
12 #include "vehicletype.h"
13
14 using namespace std;
15 using namespace Msp;
16
17 namespace Marklin {
18
19 Catalogue::Catalogue():
20         scale(1),
21         gauge(1.524),
22         layout(*this)
23 { }
24
25 Catalogue::~Catalogue()
26 {
27         for(map<unsigned, TrackType *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
28                 delete i->second;
29         for(map<unsigned, VehicleType *>::iterator i=vehicles.begin(); i!=vehicles.end(); ++i)
30                 delete i->second;
31 }
32
33 float Catalogue::get_rail_elevation() const
34 {
35         return ballast_profile.get_height()+rail_profile.get_height();
36 }
37
38 void Catalogue::add_track(TrackType &track)
39 {
40         if(tracks.count(track.get_article_number()))
41                 throw Exception("Duplicate track type");
42
43         tracks[track.get_article_number()] = &track;
44         signal_track_added.emit(track);
45 }
46
47 const TrackType &Catalogue::get_track(unsigned art_nr) const
48 {
49         map<unsigned, TrackType *>::const_iterator i=tracks.find(art_nr);
50         if(i==tracks.end())
51                 throw KeyError("Unknown track type");
52
53         return *i->second;
54 }
55
56 void Catalogue::add_vehicle(VehicleType &veh)
57 {
58         if(vehicles.count(veh.get_article_number()))
59                 throw Exception("Duplicate vehicle type");
60
61         vehicles[veh.get_article_number()] = &veh;
62         signal_vehicle_added.emit(veh);
63 }
64
65 const VehicleType &Catalogue::get_vehicle(unsigned art_nr) const
66 {
67         map<unsigned, VehicleType *>::const_iterator i = vehicles.find(art_nr);
68         if(i==vehicles.end())
69                 throw KeyError("Unknown vehicle type");
70
71         return *i->second;
72 }
73
74
75 Catalogue::Loader::Loader(Catalogue &c):
76         DataFile::BasicLoader<Catalogue>(c)
77 {
78         add("ballast_profile", &Loader::ballast_profile);
79         add("gauge", &Loader::gauge);
80         add("layout", &Loader::layout);
81         add("rail_profile", &Loader::rail_profile);
82         add("scale", &Loader::scale);
83         add("track", &Loader::track);
84         add("vehicle", &Loader::vehicle);
85 }
86
87 void Catalogue::Loader::ballast_profile()
88 {
89         load_sub(obj.ballast_profile);
90 }
91
92 void Catalogue::Loader::gauge(float g)
93 {
94         obj.gauge = g/1000;
95         obj.path_profile = Profile();
96         obj.path_profile.append_point(Point(0.1*obj.gauge, 0));
97         obj.path_profile.append_point(Point(-0.1*obj.gauge, 0));
98 }
99
100 void Catalogue::Loader::layout()
101 {
102         load_sub(obj.layout);
103 }
104
105 void Catalogue::Loader::rail_profile()
106 {
107         load_sub(obj.rail_profile);
108 }
109
110 void Catalogue::Loader::scale(float n, float d)
111 {
112         obj.scale = n/d;
113 }
114
115 void Catalogue::Loader::track(unsigned art_nr)
116 {
117         RefPtr<TrackType> trk = new TrackType(art_nr);
118         load_sub(*trk);
119         obj.add_track(*trk);
120         trk.release();
121 }
122
123 void Catalogue::Loader::vehicle(unsigned art_nr)
124 {
125         RefPtr<VehicleType> veh = new VehicleType(art_nr);
126         load_sub(*veh);
127         obj.add_vehicle(*veh);
128         veh.release();
129 }
130
131 } // namespace Marklin