3 This file is part of R²C²
4 Copyright © 2006-2010 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
8 #include <msp/core/refptr.h>
9 #include <msp/datafile/parser.h>
10 #include "catalogue.h"
11 #include "tracktype.h"
12 #include "vehicletype.h"
19 Catalogue::Catalogue():
25 Catalogue::~Catalogue()
27 for(TrackMap::iterator i=tracks.begin(); i!=tracks.end(); ++i)
29 for(VehicleMap::iterator i=vehicles.begin(); i!=vehicles.end(); ++i)
33 float Catalogue::get_rail_elevation() const
35 return ballast_profile.get_height()+rail_profile.get_height();
38 void Catalogue::add_track(TrackType &track)
40 if(tracks.count(track.get_article_number()))
41 throw Exception("Duplicate track type");
43 tracks[track.get_article_number()] = &track;
44 signal_track_added.emit(track);
47 const TrackType &Catalogue::get_track(const ArticleNumber &art_nr) const
49 TrackMap::const_iterator i=tracks.find(art_nr);
51 throw KeyError("Unknown track type");
56 void Catalogue::add_vehicle(VehicleType &veh)
58 if(vehicles.count(veh.get_article_number()))
59 throw Exception("Duplicate vehicle type");
61 vehicles[veh.get_article_number()] = &veh;
62 signal_vehicle_added.emit(veh);
65 const VehicleType &Catalogue::get_vehicle(const ArticleNumber &art_nr) const
67 VehicleMap::const_iterator i = vehicles.find(art_nr);
69 throw KeyError("Unknown vehicle type");
75 Catalogue::Loader::Loader(Catalogue &c):
76 DataFile::BasicLoader<Catalogue>(c)
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", static_cast<void (Loader::*)(unsigned)>(&Loader::track));
84 add("track", static_cast<void (Loader::*)(ArticleNumber)>(&Loader::track));
85 add("vehicle", static_cast<void (Loader::*)(unsigned)>(&Loader::vehicle));
86 add("vehicle", static_cast<void (Loader::*)(ArticleNumber)>(&Loader::vehicle));
89 void Catalogue::Loader::ballast_profile()
91 load_sub(obj.ballast_profile);
94 void Catalogue::Loader::gauge(float g)
97 obj.path_profile = Profile();
98 obj.path_profile.append_point(Point(0.1*obj.gauge, 0));
99 obj.path_profile.append_point(Point(-0.1*obj.gauge, 0));
102 void Catalogue::Loader::layout()
104 load_sub(obj.layout);
107 void Catalogue::Loader::rail_profile()
109 load_sub(obj.rail_profile);
112 void Catalogue::Loader::scale(float n, float d)
117 void Catalogue::Loader::track(unsigned art_nr)
119 track(ArticleNumber(art_nr));
122 void Catalogue::Loader::track(ArticleNumber art_nr)
124 if(obj.tracks.count(art_nr))
125 throw KeyError("Duplicate track type", art_nr.str());
127 RefPtr<TrackType> trk = new TrackType(art_nr);
129 obj.add_track(*trk.release());
132 void Catalogue::Loader::vehicle(unsigned art_nr)
134 vehicle(ArticleNumber(art_nr));
137 void Catalogue::Loader::vehicle(ArticleNumber art_nr)
139 if(obj.vehicles.count(art_nr))
140 throw KeyError("Duplicate vehicle type", art_nr.str());
142 RefPtr<VehicleType> veh = new VehicleType(art_nr);
144 obj.add_vehicle(*veh.release());