From 1124a98ce5e1edee1904eeec089976d446601afd Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sun, 23 Mar 2014 15:30:33 +0200 Subject: [PATCH] Unify TrackChain names --- source/designer/zonebar.cpp | 3 ++- source/designer/zonebar.h | 2 +- source/libr2c2/block.cpp | 19 +++++++++++++++++++ source/libr2c2/block.h | 2 ++ source/libr2c2/route.cpp | 13 ++++++------- source/libr2c2/route.h | 6 +----- source/libr2c2/trackchain.cpp | 8 ++++++++ source/libr2c2/trackchain.h | 5 +++++ source/libr2c2/zone.cpp | 26 ++++++++++++++++---------- source/libr2c2/zone.h | 4 +--- 10 files changed, 61 insertions(+), 27 deletions(-) diff --git a/source/designer/zonebar.cpp b/source/designer/zonebar.cpp index f97554c..3b6b212 100644 --- a/source/designer/zonebar.cpp +++ b/source/designer/zonebar.cpp @@ -92,8 +92,9 @@ void Zonebar::track_chain_removed(TrackChain &chain) } } -void Zonebar::zone_renamed(Zone &zone, const string &group, const string &, unsigned) +void Zonebar::zone_renamed(Zone &zone, const string &) { + const string &group = zone.get_group(); const set &lzones = designer.get_layout().get_all(); set group_names; for(set::const_iterator i=lzones.begin(); i!=lzones.end(); ++i) diff --git a/source/designer/zonebar.h b/source/designer/zonebar.h index 13ffbe2..c9f0d62 100644 --- a/source/designer/zonebar.h +++ b/source/designer/zonebar.h @@ -21,7 +21,7 @@ public: private: void track_chain_added(R2C2::TrackChain &); void track_chain_removed(R2C2::TrackChain &); - void zone_renamed(R2C2::Zone &, const std::string &, const std::string &, unsigned); + void zone_renamed(R2C2::Zone &, const std::string &); void group_selected(unsigned); void number_selected(unsigned); void delete_zone_clicked(); diff --git a/source/libr2c2/block.cpp b/source/libr2c2/block.cpp index 59ad3a3..7c0409d 100644 --- a/source/libr2c2/block.cpp +++ b/source/libr2c2/block.cpp @@ -1,4 +1,5 @@ #include +#include #include #include "block.h" #include "driver.h" @@ -74,6 +75,11 @@ Block::~Block() delete sensor; } +void Block::set_name(const string &) +{ + throw logic_error("Block names can't be set"); +} + void Block::on_track_added(Track &track) { track.set_block(this); @@ -174,10 +180,17 @@ bool Block::reserve(Train *t) void Block::determine_id() { + string n; if(sensor_addr) + { id = 0x1000|sensor_addr; + n = format("Sensor %d", sensor_addr); + } else if(turnout_addr) + { id = 0x2000|turnout_addr; + n = format("Turnout %d", turnout_addr); + } else if(endpoints.size()==2) { unsigned id1 = endpoints[0].link ? endpoints[0].link->get_id() : 1; @@ -191,6 +204,12 @@ void Block::determine_id() unsigned id1 = endpoints[0].link ? endpoints[0].link->get_id() : 1; id = 0x10000 | id1; } + + if(n.empty()) + n = format("Block %x", id); + + name = n; + signal_name_changed.emit(name); } diff --git a/source/libr2c2/block.h b/source/libr2c2/block.h index 2f898b0..86ad6ce 100644 --- a/source/libr2c2/block.h +++ b/source/libr2c2/block.h @@ -43,6 +43,8 @@ public: Block(Layout &, Track &); ~Block(); + virtual void set_name(const std::string &); + private: virtual void on_track_added(Track &); virtual Validity check_validity(Track &) const; diff --git a/source/libr2c2/route.cpp b/source/libr2c2/route.cpp index a2e7ac8..7458cc6 100644 --- a/source/libr2c2/route.cpp +++ b/source/libr2c2/route.cpp @@ -140,12 +140,6 @@ Route::~Route() layout.remove(*this); } -void Route::set_name(const string &n) -{ - name = n; - signal_name_changed.emit(name); -} - void Route::set_temporary(bool t) { temporary = t; @@ -287,7 +281,7 @@ Route *Route::find(const TrackIter &from, const set &to) Route::Loader::Loader(Route &r): DataFile::ObjectLoader(r) { - add("name", &Route::name); + add("name", &Loader::name); add("turnout", &Loader::turnout); } @@ -316,6 +310,11 @@ void Route::Loader::finish() } } +void Route::Loader::name(const string &n) +{ + obj.name = n; +} + void Route::Loader::turnout(unsigned id, unsigned path) { turnouts[id] = path; diff --git a/source/libr2c2/route.h b/source/libr2c2/route.h index 3bd4a44..6ada850 100644 --- a/source/libr2c2/route.h +++ b/source/libr2c2/route.h @@ -31,13 +31,11 @@ public: Loader(Route &); private: virtual void finish(); + void name(const std::string &); void turnout(unsigned, unsigned); }; - sigc::signal signal_name_changed; - private: - std::string name; bool temporary; TurnoutMap turnouts; @@ -45,8 +43,6 @@ public: Route(Layout &); ~Route(); - void set_name(const std::string &); - const std::string &get_name() const { return name; } void set_temporary(bool); bool is_temporary() const { return temporary; } void update_turnouts(); diff --git a/source/libr2c2/trackchain.cpp b/source/libr2c2/trackchain.cpp index bb4e77b..b256b70 100644 --- a/source/libr2c2/trackchain.cpp +++ b/source/libr2c2/trackchain.cpp @@ -3,6 +3,8 @@ #include "track.h" #include "trackchain.h" +using namespace std; + namespace R2C2 { TrackChain::TrackChain(Layout &l): @@ -11,6 +13,12 @@ TrackChain::TrackChain(Layout &l): layout.signal_object_removed.connect(sigc::mem_fun(this, &TrackChain::object_removed)); } +void TrackChain::set_name(const string &n) +{ + name = n; + signal_name_changed.emit(name); +} + void TrackChain::add_track(Track &track) { if(tracks.count(&track)) diff --git a/source/libr2c2/trackchain.h b/source/libr2c2/trackchain.h index b4704f0..0338bab 100644 --- a/source/libr2c2/trackchain.h +++ b/source/libr2c2/trackchain.h @@ -34,10 +34,12 @@ protected: }; public: + sigc::signal signal_name_changed; sigc::signal signal_track_added; sigc::signal signal_track_removed; Layout &layout; + std::string name; TrackSet tracks; TrackIter ends[2]; @@ -45,6 +47,9 @@ public: public: virtual ~TrackChain() { } + virtual void set_name(const std::string &); + const std::string &get_name() const { return name; } + void add_track(Track &); void add_tracks(const TrackSet &); protected: diff --git a/source/libr2c2/zone.cpp b/source/libr2c2/zone.cpp index 23b0ac9..dc60a3a 100644 --- a/source/libr2c2/zone.cpp +++ b/source/libr2c2/zone.cpp @@ -1,4 +1,5 @@ #include +#include #include "block.h" #include "layout.h" #include "track.h" @@ -21,26 +22,31 @@ Zone::~Zone() layout.remove(*this); } +void Zone::set_name(const string &n) +{ + Regex r_name("(.+) ([^ ]+) ([1-9][0-9]*)"); + RegMatch m = r_name.match(n); + if(!m) + throw invalid_argument("Zone::set_name"); + + set_name(m[1].str, m[2].str, lexical_cast(m[3].str)); +} + void Zone::set_name(const string &g, const string &q, unsigned n) { group = g; qualifier = q; number = n; - signal_name_changed.emit(group, qualifier, number); -} - -string Zone::get_name() const -{ - string result = group; + string full_name = group; if(!qualifier.empty()) { - result += ' '; - result += qualifier; + full_name += ' '; + full_name += qualifier; } if(number) - result += format(" %d", number); - return result; + full_name += format(" %d", number); + TrackChain::set_name(full_name); } void Zone::save(list &st) const diff --git a/source/libr2c2/zone.h b/source/libr2c2/zone.h index 7b1dd9f..d8488d7 100644 --- a/source/libr2c2/zone.h +++ b/source/libr2c2/zone.h @@ -22,8 +22,6 @@ public: void block(unsigned); }; - sigc::signal signal_name_changed; - private: std::string group; std::string qualifier; @@ -33,11 +31,11 @@ public: Zone(Layout &); ~Zone(); + virtual void set_name(const std::string &); void set_name(const std::string &, const std::string &, unsigned); const std::string &get_group() const { return group; } const std::string &get_qualifier() const { return qualifier; } unsigned get_number() const { return number; } - std::string get_name() const; void save(std::list &) const; }; -- 2.45.2