From: Mikko Rasa Date: Wed, 20 Nov 2013 08:03:29 +0000 (+0200) Subject: Generalize TrackProperties to all object types X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=b6af6301c76d3664d969a72d8d889020093ac848;p=r2c2.git Generalize TrackProperties to all object types --- diff --git a/source/designer/designer.cpp b/source/designer/designer.cpp index 88bdf8b..a59b3a1 100644 --- a/source/designer/designer.cpp +++ b/source/designer/designer.cpp @@ -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 @@ -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 &tracks = selection.get_objects(); for(set::const_iterator i=tracks.begin(); i!=tracks.end(); ++i) diff --git a/source/designer/designer.h b/source/designer/designer.h index 2c25085..c09feb9 100644 --- a/source/designer/designer.h +++ b/source/designer/designer.h @@ -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 index 0000000..af09033 --- /dev/null +++ b/source/designer/objectproperties.cpp @@ -0,0 +1,171 @@ +#include +#include +#include +#include +#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(object)) + { + if(track->get_type().is_turnout()) + { + if(selection.size()==1) + { + lbl_title->set_text("Turnout properties"); + properties = new TurnoutProperties(*this, *track); + } + } + else + { + set tracks = selection.get_objects(); + if(tracks.size()==selection.size()) + { + bool all_linear = true; + for(set::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(object)) + { + lbl_title->set_text("Signal properties"); + properties = new SignalProperties(*this, *signal); + } + else if(BeamGate *gate = dynamic_cast(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(track.get_turnout_address()), 5)) +{ } + +void ObjectProperties::TurnoutProperties::apply() +{ + track.set_turnout_address(lexical_cast(ent_address->get_text())); +} + + +ObjectProperties::TrackCircuitProperties::TrackCircuitProperties(ObjectProperties &p, const set &t): + tracks(t), + ent_address(p.add_property("Sensor address", string(), 5)) +{ + int addr = -1; + for(set::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(addr)); +} + +void ObjectProperties::TrackCircuitProperties::apply() +{ + const string &text = ent_address->get_text(); + if(!text.empty()) + { + unsigned addr = lexical_cast(text); + for(set::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(signal.get_address()), 5)) +{ } + +void ObjectProperties::SignalProperties::apply() +{ + signal.set_address(lexical_cast(ent_address->get_text())); +} + + +ObjectProperties::BeamGateProperties::BeamGateProperties(ObjectProperties &p, BeamGate &g): + gate(g), + ent_address(p.add_property("Sensor address", lexical_cast(gate.get_address()), 5)) +{ } + +void ObjectProperties::BeamGateProperties::apply() +{ + gate.set_address(lexical_cast(ent_address->get_text())); +} diff --git a/source/designer/objectproperties.h b/source/designer/objectproperties.h new file mode 100644 index 0000000..9180206 --- /dev/null +++ b/source/designer/objectproperties.h @@ -0,0 +1,85 @@ +#ifndef TRACKPROPERTIES_H_ +#define TRACKPROPERTIES_H_ + +#include +#include +#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 tracks; + Msp::GLtk::Entry *ent_address; + + public: + TrackCircuitProperties(ObjectProperties &, const std::set &); + + 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 diff --git a/source/designer/trackbar.cpp b/source/designer/trackbar.cpp index 4a79734..11dd1a6 100644 --- a/source/designer/trackbar.cpp +++ b/source/designer/trackbar.cpp @@ -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 index 59ce751..0000000 --- a/source/designer/trackproperties.cpp +++ /dev/null @@ -1,96 +0,0 @@ -#include -#include -#include -#include -#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()->get_turnout_address()) - ent_turnout_addr->set_text(lexical_cast(taddr)); - } - - const set &tracks = selection.get_objects(); - int sensor_addr = -1; - for(set::const_iterator i=tracks.begin(); i!=tracks.end(); ++i) - { - if(static_cast((*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(sensor_addr)); -} - -void TrackProperties::on_response(int code) -{ - if(code==1) - { - if(selection.size()==1) - { - Track *track = selection.get_object(); - if(track->get_type().is_turnout()) - track->set_turnout_address(lexical_cast(ent_turnout_addr->get_text())); - } - - string sensor_addr_text = ent_sensor_addr->get_text(); - if(!sensor_addr_text.empty()) - { - unsigned sensor_addr = lexical_cast(sensor_addr_text); - const set &tracks = selection.get_objects(); - for(set::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 index 7c29464..0000000 --- a/source/designer/trackproperties.h +++ /dev/null @@ -1,23 +0,0 @@ -#ifndef TRACKPROPERTIES_H_ -#define TRACKPROPERTIES_H_ - -#include -#include - -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 diff --git a/source/libr2c2/signal.h b/source/libr2c2/signal.h index 7f6e011..d774e76 100644 --- a/source/libr2c2/signal.h +++ b/source/libr2c2/signal.h @@ -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 &) { }