]> git.tdb.fi Git - r2c2.git/commitdiff
Unify TrackChain names
authorMikko Rasa <tdb@tdb.fi>
Sun, 23 Mar 2014 13:30:33 +0000 (15:30 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sun, 23 Mar 2014 13:30:33 +0000 (15:30 +0200)
source/designer/zonebar.cpp
source/designer/zonebar.h
source/libr2c2/block.cpp
source/libr2c2/block.h
source/libr2c2/route.cpp
source/libr2c2/route.h
source/libr2c2/trackchain.cpp
source/libr2c2/trackchain.h
source/libr2c2/zone.cpp
source/libr2c2/zone.h

index f97554c8048c6937e87ded369fe5d11023acc2de..3b6b2125804b996467e8b0d03230ff43748ee3f1 100644 (file)
@@ -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<Zone *> &lzones = designer.get_layout().get_all<Zone>();
        set<string> group_names;
        for(set<Zone *>::const_iterator i=lzones.begin(); i!=lzones.end(); ++i)
index 13ffbe2619b109f18d5828aca266d3bb320c5630..c9f0d62cadd164fff11a5bcdbd232fc17b3c2d8a 100644 (file)
@@ -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();
index 59ad3a3c990965ac67b9e0ccad5602aff5c35abb..7c0409d94588caa787f1c73d440189c69de68a0b 100644 (file)
@@ -1,4 +1,5 @@
 #include <algorithm>
+#include <msp/strings/format.h>
 #include <msp/time/units.h>
 #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);
 }
 
 
index 2f898b0992af7e14799fc576532265f8b808bff8..86ad6ce593e0204ee7708fd3ad0a26560547e709 100644 (file)
@@ -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;
index a2e7ac8c4c2c5d00a18597185377e12e70a89861..7458cc66e2eb87f57a08373d6dfc04041bc5680b 100644 (file)
@@ -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<Track *> &to)
 Route::Loader::Loader(Route &r):
        DataFile::ObjectLoader<Route>(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;
index 3bd4a445e5ecc6ffcddd096b828cdfa42f700d0d..6ada85078e19f18afbc72ab5d595776a34df925b 100644 (file)
@@ -31,13 +31,11 @@ public:
                Loader(Route &);
        private:
                virtual void finish();
+               void name(const std::string &);
                void turnout(unsigned, unsigned);
        };
 
-       sigc::signal<void, const std::string &> 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();
index bb4e77b6321a93998b3f1232f1f49ec00c2befde..b256b705f95aa5bc21dceca556b767f3d962c676 100644 (file)
@@ -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))
index b4704f0bd9f145394035376b5abada7719b23789..0338bab2d9d0e2dfca079b4039cbcdb9e2f51cb6 100644 (file)
@@ -34,10 +34,12 @@ protected:
        };
 
 public:
+       sigc::signal<void, const std::string &> signal_name_changed;
        sigc::signal<void, Track &> signal_track_added;
        sigc::signal<void, Track &> 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:
index 23b0ac9f093f3962507473f6258c082d3bb3245a..dc60a3a2630f13ccb3b02cd2d72ef8d722412799 100644 (file)
@@ -1,4 +1,5 @@
 #include <msp/strings/format.h>
+#include <msp/strings/regex.h>
 #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<unsigned>(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<DataFile::Statement> &st) const
index 7b1dd9fb30d9bf29266fbfbef3cc5a5cbd50d000..d8488d7da1f58990e3c79f88e189b31fe4bb3f33 100644 (file)
@@ -22,8 +22,6 @@ public:
                void block(unsigned);
        };
 
-       sigc::signal<void, const std::string &, const std::string &, unsigned> 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<Msp::DataFile::Statement> &) const;
 };