From e375f8bf215058398795be1010ffec89d0150114 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sun, 13 Dec 2009 13:55:03 +0000 Subject: [PATCH] Add Dialog class to handle common elements of dialogs Implement TrainProperties in terms of Dialog Delete dialogs automatically Implement the edit button in TrainPanel --- source/engineer/dialog.cpp | 47 ++++++++++++++++++++++ source/engineer/dialog.h | 34 ++++++++++++++++ source/engineer/engineer.cpp | 60 +++++++---------------------- source/engineer/engineer.h | 12 +++--- source/engineer/mainpanel.cpp | 8 +++- source/engineer/trainpanel.cpp | 12 +++++- source/engineer/trainpanel.h | 3 +- source/engineer/trainproperties.cpp | 59 ++++++++++++++-------------- source/engineer/trainproperties.h | 15 +++----- 9 files changed, 154 insertions(+), 96 deletions(-) create mode 100644 source/engineer/dialog.cpp create mode 100644 source/engineer/dialog.h diff --git a/source/engineer/dialog.cpp b/source/engineer/dialog.cpp new file mode 100644 index 0000000..c6389c9 --- /dev/null +++ b/source/engineer/dialog.cpp @@ -0,0 +1,47 @@ +/* $Id$ + +This file is part of the MSP Märklin suite +Copyright © 2009 Mikkosoft Productions, Mikko Rasa +Distributed under the GPL +*/ + +#include "dialog.h" + +using namespace Msp; + +Dialog::Dialog(const GLtk::Resources &r): + GLtk::Widget(r), + GLtk::Panel(r), + stale(false) +{ + add(*(btn_ok=new GLtk::Button(res, "OK"))); + btn_ok->set_style("green"); + btn_ok->set_geometry(GLtk::Geometry(geom.w-40, 10, 30, 25)); + btn_ok->signal_clicked.connect(sigc::mem_fun(this, &Dialog::on_ok_clicked)); + btn_ok->signal_clicked.connect(signal_ok); + btn_ok->signal_clicked.connect(sigc::mem_fun(this, &Dialog::dismiss)); + + add(*(btn_cancel=new GLtk::Button(res, "Cncl"))); + btn_cancel->set_style("red"); + btn_cancel->set_geometry(GLtk::Geometry(geom.w-80, 10, 30, 25)); + btn_cancel->signal_clicked.connect(signal_cancel); + btn_cancel->signal_clicked.connect(sigc::mem_fun(this, &Dialog::dismiss)); +} + +void Dialog::button_release(int x, int y, unsigned btn) +{ + Panel::button_release(x, y, btn); + if(stale) + delete this; +} + +void Dialog::on_geometry_change() +{ + btn_ok->set_geometry(GLtk::Geometry(geom.w-40, 10, 30, 25)); + btn_cancel->set_geometry(GLtk::Geometry(geom.w-80, 10, 30, 25)); +} + +void Dialog::dismiss() +{ + stale = true; +} diff --git a/source/engineer/dialog.h b/source/engineer/dialog.h new file mode 100644 index 0000000..f923bb8 --- /dev/null +++ b/source/engineer/dialog.h @@ -0,0 +1,34 @@ +/* $Id$ + +This file is part of the MSP Märklin suite +Copyright © 2009 Mikkosoft Productions, Mikko Rasa +Distributed under the GPL +*/ + +#ifndef DIALOG_H_ +#define DIALOG_H_ + +#include +#include + +class Dialog: public Msp::GLtk::Panel +{ +public: + sigc::signal signal_ok; + sigc::signal signal_cancel; + +protected: + Msp::GLtk::Button *btn_ok; + Msp::GLtk::Button *btn_cancel; + bool stale; + + Dialog(const Msp::GLtk::Resources &); + + virtual void button_release(int, int, unsigned); + virtual void on_geometry_change(); + + virtual void on_ok_clicked() { } + void dismiss(); +}; + +#endif diff --git a/source/engineer/engineer.cpp b/source/engineer/engineer.cpp index e0d45b0..2c4e726 100644 --- a/source/engineer/engineer.cpp +++ b/source/engineer/engineer.cpp @@ -39,8 +39,6 @@ Engineer::Engineer(int argc, char **argv): layout(catalogue), layout_3d(layout), server(0), - train_prop(0), - train_prop_stale(false), placing_train(0), placing_block(0), placing_entry(0), @@ -91,9 +89,10 @@ Engineer::Engineer(int argc, char **argv): DataFile::load(layout, args.front()); trfc_mgr = new TrafficManager(control, layout); - trfc_mgr->signal_block_reserved.connect(sigc::mem_fun(this, &Engineer::block_reserved)); if(FS::exists("engineer.state")) DataFile::load(*trfc_mgr, "engineer.state"); + trfc_mgr->signal_train_added.connect(sigc::mem_fun(this, &Engineer::train_added)); + trfc_mgr->signal_block_reserved.connect(sigc::mem_fun(this, &Engineer::block_reserved)); if(network) { @@ -115,36 +114,6 @@ Engineer::~Engineer() delete trfc_mgr; } -void Engineer::add_train() -{ - train_prop = new TrainProperties(*this, ui_res, 0); - root->add(*train_prop); - train_prop->signal_ok.connect(sigc::mem_fun(this, &Engineer::dismiss_train_prop)); - train_prop->signal_cancel.connect(sigc::mem_fun(this, &Engineer::dismiss_train_prop)); - train_prop_stale = false; - train_prop->set_visible(true); -} - -Train *Engineer::add_train(const LocoType &type, unsigned addr) -{ - Locomotive *loco = new Locomotive(type, control, addr); - Train *train = new Train(*trfc_mgr, *loco); - train->set_name(format("Train %d", trfc_mgr->get_trains().size())); - - TrainPanel *tpanel = new TrainPanel(*this, ui_res, *train); - root->add(*tpanel); - int y = main_panel->get_geometry().y; - for(list::iterator i=train_panels.begin(); i!=train_panels.end(); ++i) - y -= (*i)->get_geometry().h; - tpanel->set_position(0, y-tpanel->get_geometry().h); - train_panels.push_back(tpanel); - tpanel->set_visible(true); - - place_train(*train); - - return train; -} - void Engineer::place_train(Train &train) { placing_train = &train; @@ -325,21 +294,9 @@ void Engineer::tick() root->render(); - if(train_prop_stale) - { - delete train_prop; - train_prop = 0; - } - glc->swap_buffers(); } -void Engineer::key_press(unsigned key, unsigned mod, wchar_t ch) -{ - if(train_prop) - train_prop->key_press(key, mod, ch); -} - void Engineer::button_press(int x, int y, unsigned btn, unsigned) { if(placing_train) @@ -521,9 +478,18 @@ Track3D *Engineer::pick_track(int x, int y) return layout_3d.pick_track(xx, yy, size); } -void Engineer::dismiss_train_prop() +void Engineer::train_added(Train &train) { - train_prop_stale = true; + TrainPanel *tpanel = new TrainPanel(*this, ui_res, train); + root->add(*tpanel); + int y = main_panel->get_geometry().y; + for(list::iterator i=train_panels.begin(); i!=train_panels.end(); ++i) + y -= (*i)->get_geometry().h; + tpanel->set_position(0, y-tpanel->get_geometry().h); + train_panels.push_back(tpanel); + tpanel->set_visible(true); + + place_train(train); } Application::RegApp Engineer::reg; diff --git a/source/engineer/engineer.h b/source/engineer/engineer.h index c7c062f..df45678 100644 --- a/source/engineer/engineer.h +++ b/source/engineer/engineer.h @@ -1,7 +1,7 @@ /* $Id$ This file is part of the MSP Märklin suite -Copyright © 2006-2008 Mikkosoft Productions, Mikko Rasa +Copyright © 2006-2009 Mikkosoft Productions, Mikko Rasa Distributed under the GPL */ @@ -50,8 +50,6 @@ private: MainPanel *main_panel; std::list train_panels; - TrainProperties *train_prop; - bool train_prop_stale; Marklin::Train *placing_train; Marklin::Block *placing_block; unsigned placing_entry; @@ -63,16 +61,16 @@ public: Engineer(int argc, char **argv); ~Engineer(); + const Msp::GLtk::Resources &get_ui_resources() const { return ui_res; } + Msp::GLtk::Root &get_root() const { return *root; } const Marklin::Catalogue &get_catalogue() const { return catalogue; } Marklin::Control &get_control() { return control; } - void add_train(); - Marklin::Train *add_train(const Marklin::LocoType &, unsigned); + Marklin::TrafficManager &get_traffic_manager() { return *trfc_mgr; } void place_train(Marklin::Train &); int main(); void quit() { exit(0); } private: void tick(); - void key_press(unsigned, unsigned, wchar_t); void button_press(int, int, unsigned, unsigned); void button_release(int, int, unsigned, unsigned); void pointer_motion(int, int); @@ -83,7 +81,7 @@ private: void block_reserved(const Marklin::Block &, const Marklin::Train *); void project_3d(); Marklin::Track3D *pick_track(int, int); - void dismiss_train_prop(); + void train_added(Marklin::Train &); static Msp::Application::RegApp reg; }; diff --git a/source/engineer/mainpanel.cpp b/source/engineer/mainpanel.cpp index 08348d7..8c3718b 100644 --- a/source/engineer/mainpanel.cpp +++ b/source/engineer/mainpanel.cpp @@ -1,13 +1,14 @@ /* $Id$ This file is part of the MSP Märklin suite -Copyright © 2006-2008 Mikkosoft Productions, Mikko Rasa +Copyright © 2006-2009 Mikkosoft Productions, Mikko Rasa Distributed under the GPL */ #include #include "engineer.h" #include "mainpanel.h" +#include "trainproperties.h" using namespace std; using namespace Msp; @@ -81,7 +82,10 @@ void MainPanel::power_off() void MainPanel::new_loc() { - engineer.add_train(); + TrainProperties *dialog = new TrainProperties(engineer, res, 0); + engineer.get_root().add(*dialog); + dialog->set_position(geom.x+geom.w, geom.y+geom.h-dialog->get_geometry().h); + dialog->set_visible(true); } void MainPanel::quit() diff --git a/source/engineer/trainpanel.cpp b/source/engineer/trainpanel.cpp index a8c0e90..be675d6 100644 --- a/source/engineer/trainpanel.cpp +++ b/source/engineer/trainpanel.cpp @@ -1,7 +1,7 @@ /* $Id$ This file is part of the MSP Märklin suite -Copyright © 2006-2008 Mikkosoft Productions, Mikko Rasa +Copyright © 2006-2009 Mikkosoft Productions, Mikko Rasa Distributed under the GPL */ @@ -10,6 +10,7 @@ Distributed under the GPL #include "libmarklin/locomotive.h" #include "engineer.h" #include "trainpanel.h" +#include "trainproperties.h" using namespace std; using namespace Msp; @@ -75,6 +76,7 @@ TrainPanel::TrainPanel(Engineer &e, const GLtk::Resources &r, Train &t): add(*(btn=new GLtk::Button(res, "Edit"))); btn->set_geometry(GLtk::Geometry(geom.w-50, 10, 40, 24)); + btn->signal_clicked.connect(sigc::mem_fun(this, &TrainPanel::edit_clicked)); add(*(btn=new GLtk::Button(res, "Place"))); btn->set_geometry(GLtk::Geometry(geom.w-90, 10, 40, 24)); @@ -112,6 +114,14 @@ void TrainPanel::place_clicked() engineer.place_train(train); } +void TrainPanel::edit_clicked() +{ + TrainProperties *dialog = new TrainProperties(engineer, res, &train); + engineer.get_root().add(*dialog); + dialog->set_position(geom.x+geom.w, geom.y+geom.h-dialog->get_geometry().h); + dialog->set_visible(true); +} + void TrainPanel::forward_toggled(bool value) { train.set_reverse(!value); diff --git a/source/engineer/trainpanel.h b/source/engineer/trainpanel.h index d8b1ab0..817ba18 100644 --- a/source/engineer/trainpanel.h +++ b/source/engineer/trainpanel.h @@ -1,7 +1,7 @@ /* $Id$ This file is part of the MSP Märklin suite -Copyright © 2006-2008 Mikkosoft Productions, Mikko Rasa +Copyright © 2006-2009 Mikkosoft Productions, Mikko Rasa Distributed under the GPL */ @@ -37,6 +37,7 @@ private: void loco_function_changed(unsigned, bool); void train_status_changed(const std::string &); void place_clicked(); + void edit_clicked(); void forward_toggled(bool); void func_toggled(bool, unsigned); }; diff --git a/source/engineer/trainproperties.cpp b/source/engineer/trainproperties.cpp index 67a9579..eb132c0 100644 --- a/source/engineer/trainproperties.cpp +++ b/source/engineer/trainproperties.cpp @@ -1,11 +1,12 @@ /* $Id$ This file is part of the MSP Märklin suite -Copyright © 2006-2008 Mikkosoft Productions, Mikko Rasa +Copyright © 2006-2009 Mikkosoft Productions, Mikko Rasa Distributed under the GPL */ #include +#include #include #include #include "engineer.h" @@ -15,55 +16,57 @@ using namespace std; using namespace Msp; using namespace Marklin; -TrainProperties::TrainProperties(Engineer &e, GLtk::Resources &r, Train *t): +TrainProperties::TrainProperties(Engineer &e, const GLtk::Resources &r, Train *t): Widget(r), - Panel(r), + Dialog(r), engineer(e), train(t) { - set_size(200, 95); + set_size(200, 120); + + GLtk::Label *label; + add(*(label=new GLtk::Label(res, "Train properties"))); + label->set_geometry(GLtk::Geometry(10, geom.h-25, geom.w-20, 20)); add(*(ent_addr=new GLtk::Entry(res))); - ent_addr->set_geometry(GLtk::Geometry(10, geom.h-30, 40, 20)); + ent_addr->set_geometry(GLtk::Geometry(10, geom.h-50, 40, 20)); add(*(drp_type=new GLtk::Dropdown(res))); - drp_type->set_geometry(GLtk::Geometry(60, geom.h-30, geom.w-70, 20)); + drp_type->set_geometry(GLtk::Geometry(60, geom.h-50, geom.w-70, 20)); const map &locos = engineer.get_catalogue().get_locomotives(); - for(map::const_iterator i=locos.begin(); i!=locos.end(); ++i) + unsigned n = 0; + for(map::const_iterator i=locos.begin(); i!=locos.end(); ++i, ++n) + { drp_type->append(format("%d %s", i->second->get_article_number(), i->second->get_name())); + if(train && i->second==&train->get_locomotive().get_type()) + drp_type->set_selected_index(n); + } - add(*(ent_name=new GLtk::Entry(res, "Train"))); - ent_name->set_geometry(GLtk::Geometry(10, geom.h-55, geom.w-20, 20)); - - GLtk::Button *btn; - - add(*(btn=new GLtk::Button(res, "OK"))); - btn->set_style("green"); - btn->set_geometry(GLtk::Geometry(geom.w-40, 10, 30, 25)); - btn->signal_clicked.connect(sigc::mem_fun(this, &TrainProperties::ok_clicked)); + add(*(ent_name=new GLtk::Entry(res))); + ent_name->set_geometry(GLtk::Geometry(10, geom.h-75, geom.w-20, 20)); - add(*(btn=new GLtk::Button(res, "Cncl"))); - btn->set_style("red"); - btn->set_geometry(GLtk::Geometry(geom.w-80, 10, 30, 25)); - btn->signal_clicked.connect(sigc::mem_fun(this, &TrainProperties::cancel_clicked)); + if(train) + { + ent_addr->set_text(lexical_cast(train->get_locomotive().get_address())); + ent_name->set_text(train->get_name()); + } + else + ent_name->set_text(format("Train %d", engineer.get_traffic_manager().get_trains().size()+1)); } -void TrainProperties::ok_clicked() +void TrainProperties::on_ok_clicked() { if(!train) { const map &locos = engineer.get_catalogue().get_locomotives(); map::const_iterator i = locos.begin(); advance(i, drp_type->get_selected_index()); - train = engineer.add_train(*i->second, lexical_cast(ent_addr->get_text())); + + unsigned addr = lexical_cast(ent_addr->get_text()); + Locomotive *loco = new Locomotive(*i->second, engineer.get_control(), addr); + train = new Train(engineer.get_traffic_manager(), *loco); } train->set_name(ent_name->get_text()); - signal_ok.emit(); -} - -void TrainProperties::cancel_clicked() -{ - signal_cancel.emit(); } diff --git a/source/engineer/trainproperties.h b/source/engineer/trainproperties.h index bcd0b1d..8048ee2 100644 --- a/source/engineer/trainproperties.h +++ b/source/engineer/trainproperties.h @@ -1,7 +1,7 @@ /* $Id$ This file is part of the MSP Märklin suite -Copyright © 2006-2008 Mikkosoft Productions, Mikko Rasa +Copyright © 2006-2009 Mikkosoft Productions, Mikko Rasa Distributed under the GPL */ @@ -10,17 +10,13 @@ Distributed under the GPL #include #include -#include #include +#include "dialog.h" class Engineer; -class TrainProperties: public Msp::GLtk::Panel +class TrainProperties: public Dialog { -public: - sigc::signal signal_ok; - sigc::signal signal_cancel; - private: Engineer &engineer; Marklin::Train *train; @@ -29,10 +25,9 @@ private: Msp::GLtk::Entry *ent_name; public: - TrainProperties(Engineer &, Msp::GLtk::Resources &, Marklin::Train *); + TrainProperties(Engineer &, const Msp::GLtk::Resources &, Marklin::Train *); private: - void ok_clicked(); - void cancel_clicked(); + virtual void on_ok_clicked(); }; #endif -- 2.45.2