]> git.tdb.fi Git - r2c2.git/commitdiff
Add Dialog class to handle common elements of dialogs
authorMikko Rasa <tdb@tdb.fi>
Sun, 13 Dec 2009 13:55:03 +0000 (13:55 +0000)
committerMikko Rasa <tdb@tdb.fi>
Sun, 13 Dec 2009 13:55:03 +0000 (13:55 +0000)
Implement TrainProperties in terms of Dialog
Delete dialogs automatically
Implement the edit button in TrainPanel

source/engineer/dialog.cpp [new file with mode: 0644]
source/engineer/dialog.h [new file with mode: 0644]
source/engineer/engineer.cpp
source/engineer/engineer.h
source/engineer/mainpanel.cpp
source/engineer/trainpanel.cpp
source/engineer/trainpanel.h
source/engineer/trainproperties.cpp
source/engineer/trainproperties.h

diff --git a/source/engineer/dialog.cpp b/source/engineer/dialog.cpp
new file mode 100644 (file)
index 0000000..c6389c9
--- /dev/null
@@ -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 (file)
index 0000000..f923bb8
--- /dev/null
@@ -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 <msp/gltk/button.h>
+#include <msp/gltk/panel.h>
+
+class Dialog: public Msp::GLtk::Panel
+{
+public:
+       sigc::signal<void> signal_ok;
+       sigc::signal<void> 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
index e0d45b0e00f7719cd3626a352533c6ea12227cc4..2c4e7263b4da113e3b304ab529e8229e5b47cebd 100644 (file)
@@ -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<TrainPanel *>::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<TrainPanel *>::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> Engineer::reg;
index c7c062fe2347331d41289a74f1378d862e84dee2..df45678306418c38e0ab9ffe905c89d567875d18 100644 (file)
@@ -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<TrainPanel *> 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<Engineer> reg;
 };
index 08348d713a91982902c9122067f6d6b017732801..8c3718b9176fa025bcda7087633cf8357a10522e 100644 (file)
@@ -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 <msp/gltk/button.h>
 #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()
index a8c0e90536580d3c7c40a27a32300d940446fb22..be675d6b8a2e4582c65390cc121d16acccf935f9 100644 (file)
@@ -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);
index d8b1ab0bae0b4f63ccdb62a4b6083deb714c2870..817ba18b238d84a998bd4a6ade21166e15a13e62 100644 (file)
@@ -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);
 };
index 67a957959fca96c04a8b8ab9338d7f62933b5e24..eb132c0e68f1eef1d9ea4a72e7280e430aa298a6 100644 (file)
@@ -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 <msp/gltk/button.h>
+#include <msp/gltk/label.h>
 #include <msp/strings/formatter.h>
 #include <msp/strings/lexicalcast.h>
 #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<unsigned, LocoType *> &locos = engineer.get_catalogue().get_locomotives();
-       for(map<unsigned, LocoType *>::const_iterator i=locos.begin(); i!=locos.end(); ++i)
+       unsigned n = 0;
+       for(map<unsigned, LocoType *>::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<unsigned, LocoType *> &locos = engineer.get_catalogue().get_locomotives();
                map<unsigned, LocoType *>::const_iterator i = locos.begin();
                advance(i, drp_type->get_selected_index());
-               train = engineer.add_train(*i->second, lexical_cast<unsigned>(ent_addr->get_text()));
+
+               unsigned addr = lexical_cast<unsigned>(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();
 }
index bcd0b1df921ff6f85dfe02491b9be5792f29874b..8048ee20aef71ecc964d6e357a381c57b0c19d26 100644 (file)
@@ -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 <msp/gltk/dropdown.h>
 #include <msp/gltk/entry.h>
-#include <msp/gltk/panel.h>
 #include <libmarklin/train.h>
+#include "dialog.h"
 
 class Engineer;
 
-class TrainProperties: public Msp::GLtk::Panel
+class TrainProperties: public Dialog
 {
-public:
-       sigc::signal<void> signal_ok;
-       sigc::signal<void> 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