]> git.tdb.fi Git - r2c2.git/commitdiff
Generalize TrackProperties to all object types
authorMikko Rasa <tdb@tdb.fi>
Wed, 20 Nov 2013 08:03:29 +0000 (10:03 +0200)
committerMikko Rasa <tdb@tdb.fi>
Wed, 20 Nov 2013 08:22:44 +0000 (10:22 +0200)
source/designer/designer.cpp
source/designer/designer.h
source/designer/objectproperties.cpp [new file with mode: 0644]
source/designer/objectproperties.h [new file with mode: 0644]
source/designer/trackbar.cpp
source/designer/trackproperties.cpp [deleted file]
source/designer/trackproperties.h [deleted file]
source/libr2c2/signal.h

index 88bdf8b75253f958e4493489f978cb3c5eda67b7..a59b3a12dd127839616a13190ceb51b7ee345b4f 100644 (file)
@@ -28,6 +28,7 @@
 #include "manipulator.h"
 #include "measure.h"
 #include "movetool.h"
+#include "objectproperties.h"
 #include "objectselecttool.h"
 #include "rotatetool.h"
 #include "routebar.h"
@@ -35,7 +36,6 @@
 #include "slopetool.h"
 #include "svgexporter.h"
 #include "trackbar.h"
-#include "trackproperties.h"
 #include "zonebar.h"
 #include "zoneproperties.h"
 
@@ -210,17 +210,17 @@ void Designer::erase_objects()
        }
 }
 
-void Designer::track_properties()
+void Designer::object_properties()
 {
        use_select_tool();
        if(selection.empty())
                return;
 
-       TrackProperties *track_prop = new TrackProperties(selection);
-       root.add(*track_prop);
-       root_layout->set_gravity(*track_prop, 0, 0);
+       ObjectProperties *dlg = new ObjectProperties(selection);
+       root.add(*dlg);
+       root_layout->set_gravity(*dlg, 0, 0);
 
-       track_prop->signal_response.connect(sigc::mem_fun(this, &Designer::track_properties_response));
+       dlg->signal_response.connect(sigc::mem_fun(this, &Designer::object_properties_response));
 }
 
 void Designer::extend_track()
@@ -427,7 +427,7 @@ void Designer::key_press(unsigned key)
        else if(key==Msp::Input::KEY_V)
                svg_export();
        else if(key==Msp::Input::KEY_P)
-               track_properties();
+               object_properties();
 }
 
 template<typename T>
@@ -536,7 +536,7 @@ void Designer::tool_status(const string &status)
        lbl_status->set_text(status);
 }
 
-void Designer::track_properties_response(int)
+void Designer::object_properties_response(int)
 {
        const set<Track *> &tracks = selection.get_objects<Track>();
        for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
index 2c25085ddb85201b75c8d57535a2b3bb60de7f24..c09feb9ca2218276f57f5a0e0f3fb8209fe14e5b 100644 (file)
@@ -86,7 +86,7 @@ public:
 
        void new_object();
        void erase_objects();
-       void track_properties();
+       void object_properties();
        void extend_track();
        void connect_tracks();
        void flatten_tracks();
@@ -118,7 +118,7 @@ private:
        R2C2::Object *pick_object(const R2C2::Vector &);
        void update_track_icon(R2C2::Track3D &);
        void tool_status(const std::string &);
-       void track_properties_response(int);
+       void object_properties_response(int);
        void route_name_accept(const std::string &);
        void svg_export_accept(const std::string &);
        std::string tooltip(int, int);
diff --git a/source/designer/objectproperties.cpp b/source/designer/objectproperties.cpp
new file mode 100644 (file)
index 0000000..af09033
--- /dev/null
@@ -0,0 +1,171 @@
+#include <msp/gltk/button.h>
+#include <msp/gltk/column.h>
+#include <msp/gltk/label.h>
+#include <msp/gltk/row.h>
+#include "libr2c2/track.h"
+#include "libr2c2/tracktype.h"
+#include "objectproperties.h"
+#include "selection.h"
+
+using namespace std;
+using namespace Msp;
+using namespace R2C2;
+
+ObjectProperties::ObjectProperties(const Selection &selection):
+       properties(0),
+       prev_widget(0)
+{
+       set_layout(new GLtk::Layout);
+       GLtk::Column col(*layout);
+
+       GLtk::Label *lbl_title;
+
+       add(*(lbl_title = new GLtk::Label("Object properties")));
+       lbl_title->set_style("title");
+       layout->set_expand(*lbl_title, true, false);
+
+       Object *object = selection.get_object();
+       if(Track *track = dynamic_cast<Track *>(object))
+       {
+               if(track->get_type().is_turnout())
+               {
+                       if(selection.size()==1)
+                       {
+                               lbl_title->set_text("Turnout properties");
+                               properties = new TurnoutProperties(*this, *track);
+                       }
+               }
+               else
+               {
+                       set<Track *> tracks = selection.get_objects<Track>();
+                       if(tracks.size()==selection.size())
+                       {
+                               bool all_linear = true;
+                               for(set<Track *>::iterator i=tracks.begin(); (all_linear && i!=tracks.end()); ++i)
+                                       all_linear = !(*i)->get_type().is_turnout();
+                               if(all_linear)
+                               {
+                                       lbl_title->set_text("Track properties");
+                                       properties = new TrackCircuitProperties(*this, tracks);
+                               }
+                       }
+               }
+       }
+       else if(selection.size()==1)
+       {
+               if(Signal *signal = dynamic_cast<Signal *>(object))
+               {
+                       lbl_title->set_text("Signal properties");
+                       properties = new SignalProperties(*this, *signal);
+               }
+               else if(BeamGate *gate = dynamic_cast<BeamGate *>(object))
+               {
+                       lbl_title->set_text("Beam gate properties");
+                       properties = new BeamGateProperties(*this, *gate);
+               }
+       }
+
+       if(!properties)
+               add(*(new GLtk::Label("No properties available")));
+
+       GLtk::Button *btn;
+
+       {
+               GLtk::Row row(*layout);
+               row.split();
+               add_button(*(btn = new GLtk::Button("Cncl")), 0);
+               btn->set_style("red");
+
+               add_button(*(btn = new GLtk::Button("OK")), 1);
+               btn->set_style("green");
+       }
+}
+
+GLtk::Entry *ObjectProperties::add_property(const string &label, const string &value, unsigned size)
+{
+       GLtk::Row row(*layout);
+       add(*(new GLtk::Label(label)));
+
+       GLtk::Entry *entry = new GLtk::Entry(value);
+       add(*entry);
+       if(size)
+               entry->set_edit_size(size, 1);
+
+       if(prev_widget)
+               layout->add_constraint(*entry, GLtk::Layout::ALIGN_LEFT, *prev_widget);
+       prev_widget = entry;
+
+       return entry;
+}
+
+void ObjectProperties::on_response(int code)
+{
+       if(code==1 && properties)
+               properties->apply();
+}
+
+
+ObjectProperties::TurnoutProperties::TurnoutProperties(ObjectProperties &p, Track &t):
+       track(t),
+       ent_address(p.add_property("Turnout address", lexical_cast<string>(track.get_turnout_address()), 5))
+{ }
+
+void ObjectProperties::TurnoutProperties::apply()
+{
+       track.set_turnout_address(lexical_cast<unsigned>(ent_address->get_text()));
+}
+
+
+ObjectProperties::TrackCircuitProperties::TrackCircuitProperties(ObjectProperties &p, const set<Track *> &t):
+       tracks(t),
+       ent_address(p.add_property("Sensor address", string(), 5))
+{
+       int addr = -1;
+       for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
+       {
+               int a = (*i)->get_sensor_address();
+               if(a!=addr)
+               {
+                       if(addr==-1)
+                               addr = a;
+                       else
+                               addr = -2;
+               }
+       }
+
+       if(addr>=0)
+               ent_address->set_text(lexical_cast<string>(addr));
+}
+
+void ObjectProperties::TrackCircuitProperties::apply()
+{
+       const string &text = ent_address->get_text();
+       if(!text.empty())
+       {
+               unsigned addr = lexical_cast<unsigned>(text);
+               for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
+                       (*i)->set_sensor_address(addr);
+       }
+}
+
+
+ObjectProperties::SignalProperties::SignalProperties(ObjectProperties &p, Signal &s):
+       signal(s),
+       ent_address(p.add_property("Signal address", lexical_cast<string>(signal.get_address()), 5))
+{ }
+
+void ObjectProperties::SignalProperties::apply()
+{
+       signal.set_address(lexical_cast<unsigned>(ent_address->get_text()));
+}
+
+
+ObjectProperties::BeamGateProperties::BeamGateProperties(ObjectProperties &p, BeamGate &g):
+       gate(g),
+       ent_address(p.add_property("Sensor address", lexical_cast<string>(gate.get_address()), 5))
+{ }
+
+void ObjectProperties::BeamGateProperties::apply()
+{
+       gate.set_address(lexical_cast<unsigned>(ent_address->get_text()));
+}
diff --git a/source/designer/objectproperties.h b/source/designer/objectproperties.h
new file mode 100644 (file)
index 0000000..9180206
--- /dev/null
@@ -0,0 +1,85 @@
+#ifndef TRACKPROPERTIES_H_
+#define TRACKPROPERTIES_H_
+
+#include <msp/gltk/dialog.h>
+#include <msp/gltk/entry.h>
+#include "libr2c2/beamgate.h"
+#include "libr2c2/signal.h"
+#include "libr2c2/track.h"
+
+class Selection;
+
+class ObjectProperties: public Msp::GLtk::Dialog
+{
+private:
+       class Properties
+       {
+       protected:
+               Properties() { }
+       public:
+               virtual ~Properties() { }
+
+               virtual void apply() = 0;
+       };
+
+       class TurnoutProperties: public Properties
+       {
+       private:
+               R2C2::Track &track;
+               Msp::GLtk::Entry *ent_address;
+
+       public:
+               TurnoutProperties(ObjectProperties &, R2C2::Track &);
+
+               virtual void apply();
+       };
+
+       class TrackCircuitProperties: public Properties
+       {
+       private:
+               std::set<R2C2::Track *> tracks;
+               Msp::GLtk::Entry *ent_address;
+
+       public:
+               TrackCircuitProperties(ObjectProperties &, const std::set<R2C2::Track *> &);
+
+               virtual void apply();
+       };
+
+       class SignalProperties: public Properties
+       {
+       private:
+               R2C2::Signal &signal;
+               Msp::GLtk::Entry *ent_address;
+
+       public:
+               SignalProperties(ObjectProperties &, R2C2::Signal &);
+
+               virtual void apply();
+       };
+
+       class BeamGateProperties: public Properties
+       {
+       private:
+               R2C2::BeamGate &gate;
+               Msp::GLtk::Entry *ent_address;
+
+       public:
+               BeamGateProperties(ObjectProperties &, R2C2::BeamGate &);
+
+               virtual void apply();
+       };
+
+       Properties *properties;
+       Msp::GLtk::Widget *prev_widget;
+
+public:
+       ObjectProperties(const Selection &);
+
+private:
+       Msp::GLtk::Entry *add_property(const std::string &, const std::string &, unsigned = 0);
+
+       virtual void on_response(int);
+};
+
+#endif
index 4a797348c72a58e6133db74eef52d2178607fab5..11dd1a6556dfba7e7619219b82aa3eda0c3e2fe8 100644 (file)
@@ -20,7 +20,7 @@ Trackbar::Trackbar(Designer &designer):
 
        pnl_content->add(*(btn = new GLtk::Button("Prop")));
        btn->set_tooltip("Change properties of selected tracks (P)");
-       btn->signal_clicked.connect(sigc::mem_fun(&designer, &Designer::track_properties));
+       btn->signal_clicked.connect(sigc::mem_fun(&designer, &Designer::object_properties));
 
        pnl_content->add(*(btn = new GLtk::Button("Xtnd")));
        btn->set_tooltip("Extend straight track out of free endpoint (Shift+N)");
diff --git a/source/designer/trackproperties.cpp b/source/designer/trackproperties.cpp
deleted file mode 100644 (file)
index 59ce751..0000000
+++ /dev/null
@@ -1,96 +0,0 @@
-#include <msp/gltk/button.h>
-#include <msp/gltk/column.h>
-#include <msp/gltk/label.h>
-#include <msp/gltk/row.h>
-#include "libr2c2/track.h"
-#include "libr2c2/tracktype.h"
-#include "selection.h"
-#include "trackproperties.h"
-
-using namespace std;
-using namespace Msp;
-using namespace R2C2;
-
-TrackProperties::TrackProperties(const Selection &s):
-       selection(s)
-{
-       set_layout(new GLtk::Layout);
-       GLtk::Column col(*layout);
-
-       GLtk::Label *lbl1, *lbl2;
-
-       add(*(lbl1 = new GLtk::Label("Track properties")));
-       lbl1->set_style("title");
-
-       {
-               GLtk::Row row(*layout);
-               add(*(lbl1 = new GLtk::Label("Turnout address")));
-               add(*(ent_turnout_addr = new GLtk::Entry));
-               ent_turnout_addr->set_edit_size(5, 1);
-       }
-
-       {
-               GLtk::Row row(*layout);
-               add(*(lbl2 = new GLtk::Label("Sensor address")));
-               layout->add_constraint(*lbl1, GLtk::Layout::COPY_WIDTH, *lbl2);
-               add(*(ent_sensor_addr = new GLtk::Entry));
-               ent_sensor_addr->set_edit_size(5, 1);
-       }
-
-       GLtk::Button *btn;
-
-       {
-               GLtk::Row row(*layout);
-               row.split();
-               add_button(*(btn = new GLtk::Button("Cncl")), 0);
-               btn->set_style("red");
-
-               add_button(*(btn = new GLtk::Button("OK")), 1);
-               btn->set_style("green");
-       }
-
-       if(selection.size()==1)
-       {
-               if(unsigned taddr = selection.get_object<Track>()->get_turnout_address())
-                       ent_turnout_addr->set_text(lexical_cast<string>(taddr));
-       }
-
-       const set<Track *> &tracks = selection.get_objects<Track>();
-       int sensor_addr = -1;
-       for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
-       {
-               if(static_cast<int>((*i)->get_sensor_address())!=sensor_addr)
-               {
-                       if(sensor_addr==-1)
-                               sensor_addr = (*i)->get_sensor_address();
-                       else
-                               sensor_addr = -2;
-               }
-       }
-
-       if(sensor_addr>=0)
-               ent_sensor_addr->set_text(lexical_cast<string>(sensor_addr));
-}
-
-void TrackProperties::on_response(int code)
-{
-       if(code==1)
-       {
-               if(selection.size()==1)
-               {
-                       Track *track = selection.get_object<Track>();
-                       if(track->get_type().is_turnout())
-                               track->set_turnout_address(lexical_cast<unsigned>(ent_turnout_addr->get_text()));
-               }
-
-               string sensor_addr_text = ent_sensor_addr->get_text();
-               if(!sensor_addr_text.empty())
-               {
-                       unsigned sensor_addr = lexical_cast<unsigned>(sensor_addr_text);
-                       const set<Track *> &tracks = selection.get_objects<Track>();
-                       for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
-                               if(!(*i)->get_type().is_turnout())
-                                       (*i)->set_sensor_address(sensor_addr);
-               }
-       }
-}
diff --git a/source/designer/trackproperties.h b/source/designer/trackproperties.h
deleted file mode 100644 (file)
index 7c29464..0000000
+++ /dev/null
@@ -1,23 +0,0 @@
-#ifndef TRACKPROPERTIES_H_
-#define TRACKPROPERTIES_H_
-
-#include <msp/gltk/dialog.h>
-#include <msp/gltk/entry.h>
-
-class Selection;
-
-class TrackProperties: public Msp::GLtk::Dialog
-{
-private:
-       const Selection &selection;
-       Msp::GLtk::Entry *ent_turnout_addr;
-       Msp::GLtk::Entry *ent_sensor_addr;
-
-public:
-       TrackProperties(const Selection &);
-
-private:
-       virtual void on_response(int);
-};
-
-#endif
index 7f6e011d148c55add40d610a0ae9124695d89ee0..d774e76b424057d864fc59f16f2550bde2f258ae 100644 (file)
@@ -45,6 +45,7 @@ public:
        virtual const SignalType &get_type() const { return type; }
 
        void set_address(unsigned);
+       unsigned get_address() const { return address; }
        virtual void set_position(const Vector &);
        virtual void set_rotation(const Angle &);
        virtual void set_tilt(const Angle &) { }