]> git.tdb.fi Git - r2c2.git/blob - source/libmarklin/layout.cpp
1fe06c7e4f5aed39948ae18e6ca9a46482545474
[r2c2.git] / source / libmarklin / layout.cpp
1 #include <algorithm>
2 #include <fstream>
3 #include <msp/parser/parser.h>
4 #include "catalogue.h"
5 #include "layout.h"
6
7 using namespace std;
8 using namespace Msp;
9
10 namespace Marklin {
11
12 Layout::Layout(Catalogue &c):
13         catalogue(c)
14 { }
15
16 void Layout::add_track(Track *t)
17 {
18         if(find(tracks.begin(), tracks.end(), t)==tracks.end())
19         {
20                 tracks.push_back(t);
21                 signal_track_added.emit(t);
22         }
23 }
24
25 void Layout::remove_track(Track *t)
26 {
27         TrackSeq::iterator i=remove_if(tracks.begin(), tracks.end(), bind1st(equal_to<Track *>(), t));
28         if(i!=tracks.end())
29         {
30                 tracks.erase(i, tracks.end());
31                 signal_track_removed.emit(t);
32         }
33 }
34
35 void Layout::check_links()
36 {
37         for(TrackSeq::iterator i=tracks.begin(); i!=tracks.end(); ++i)
38                 (*i)->break_links();
39
40         for(TrackSeq::iterator i=tracks.begin(); i!=tracks.end(); ++i)
41                 for(TrackSeq::iterator j=i; j!=tracks.end(); ++j)
42                         if(j!=i)
43                                 (*i)->snap_to(**j, true);
44 }
45
46 void Layout::load(const string &fn)
47 {
48         ifstream in(fn.c_str());
49         if(!in)
50                 throw Exception("Couldn't open file");
51
52         filename=fn;
53         Parser::Parser parser(in, fn);
54         Loader loader(*this);
55         loader.load(parser);
56
57         check_links();
58
59         for(TrackSeq::iterator i=tracks.begin(); i!=tracks.end(); ++i)
60                 (*i)->check_slope();
61 }
62
63 int Layout::save(const string &fn)
64 {
65         ofstream out(fn.c_str());
66         if(!out) return -1;
67
68         filename=fn;
69
70         if(base.size())
71                 out<<"base \""<<base<<"\";\n";
72         for(TrackSeq::iterator i=tracks.begin(); i!=tracks.end(); ++i)
73         {
74                 out<<"track "<<(*i)->get_article_number()<<"\n{\n";
75                 const Point &p=(*i)->get_position();
76                 out<<"\tposition "<<p.x<<' '<<p.y<<' '<<p.z<<";\n";
77                 out<<"\trotation "<<(*i)->get_rotation()<<";\n";
78                 out<<"\tslope "<<(*i)->get_slope()<<";\n";
79
80                 unsigned id=(*i)->get_turnout_id();
81                 if(id)
82                         out<<"\tturnout_id "<<id<<";\n";
83
84                 id=(*i)->get_sensor_id();
85                 if(id)
86                         out<<"\tsensor_id "<<id<<";\n";
87
88                 if((*i)->get_flex())
89                         out<<"\tflex true;\n";
90
91                 out<<"};\n";
92         }
93
94         return 0;
95 }
96
97 Layout::~Layout()
98 {
99         for(TrackSeq::iterator i=tracks.begin(); i!=tracks.end(); ++i)
100                 delete *i;
101 }
102
103 /*******************
104 ** Layout::Loader
105 */
106
107 Layout::Loader::Loader(Layout &l):
108         layout(l)
109 {
110         add("base",  &Layout::base);
111         add("track", &Loader::track);
112 }
113
114 void Layout::Loader::track(unsigned art_nr)
115 {
116         Track *tmpl=layout.catalogue.get_track(art_nr);
117         if(!tmpl)
118                 throw Exception("Unknown track");
119
120         Track *trk=tmpl->copy();
121         try
122         {
123                 load_sub(*trk);
124         }
125         catch(Msp::Exception &e)
126         {
127                 delete trk;
128                 throw;
129         }
130         layout.tracks.push_back(trk);
131         layout.signal_track_added.emit(trk);
132 }
133
134 } // namespace Marklin