]> git.tdb.fi Git - r2c2.git/commitdiff
Implement an UI for importing locomotives from the driver
authorMikko Rasa <tdb@tdb.fi>
Fri, 15 Nov 2013 23:01:39 +0000 (01:01 +0200)
committerMikko Rasa <tdb@tdb.fi>
Fri, 15 Nov 2013 23:01:39 +0000 (01:01 +0200)
data/trainlistdialog.ui
source/engineer/engineer.cpp
source/engineer/engineer.h
source/engineer/trainlistdialog.cpp
source/engineer/trainlistdialog.h

index a1ebf792abc5c845ed03512d1b513aafd2c09257..0582066beb5ea4be7a5726b393bbbe9036e80f38 100644 (file)
@@ -25,9 +25,38 @@ column
                };
        };
 
-       list "lst_trains"
+       row
        {
-               view_size 10;
+               column
+               {
+                       toggle "tgl_trains"
+                       {
+                               text "Trains";
+                               style "pointer_left";
+                               exclusive true;
+                               value true;
+                       };
+                       toggle "tgl_import"
+                       {
+                               text "Import";
+                               style "pointer_left";
+                               exclusive true;
+                       };
+               };
+
+               stack
+               {
+                       list "lst_trains"
+                       {
+                               view_size 10;
+                       };
+
+                       list "lst_imported"
+                       {
+                               view_size 10;
+                               visible false;
+                       };
+               };
        };
 
        row
index 87e3dda10003c7fd917dec1a94b619f2911ffb93..d0c879c02067ebb260d841528c05cd26eafb5649 100644 (file)
@@ -27,6 +27,7 @@
 #include "3d/vehicle.h"
 #include "engineer.h"
 #include "mainwindow.h"
+#include "newtraindialog.h"
 #include "traindialog.h"
 #include "trainview.h"
 
@@ -40,6 +41,7 @@ Engineer::Engineer(int argc, char **argv):
        keyboard(window),
        mouse(window),
        ui_res("r2c2.res"),
+       import_active(false),
        layout(catalogue, (options.driver.empty() ? 0 : Driver::create(options.driver))),
        layout_3d(layout),
        server(0),
@@ -71,6 +73,8 @@ Engineer::Engineer(int argc, char **argv):
        DataFile::load(catalogue, "wagons.dat");
        DataFile::load(layout, options.layout_fn);
 
+       if(layout.has_driver())
+               layout.get_driver().signal_locomotive_detected.connect(sigc::mem_fun(this, &Engineer::locomotive_detected));
        layout.signal_train_added.connect(sigc::mem_fun(this, &Engineer::train_added));
        layout.signal_emergency.connect(sigc::mem_fun(this, &Engineer::set_status));
        const set<Block *> &blocks = layout.get_all<Block>();
@@ -296,6 +300,23 @@ Object *Engineer::pick_object(const Vector &p)
        return layout.pick<Track>(Ray(start, Vector(ray)));
 }
 
+void Engineer::locomotive_detected(const Driver::DetectedLocomotive &loco)
+{
+       if(!import_active)
+       {
+               NewTrainDialog *dlg = new NewTrainDialog(*this);
+               dlg->prefill(loco);
+               dlg->signal_response.connect(sigc::mem_fun(this, &Engineer::import_finished));
+               root->add(*dlg);
+               import_active = true;
+       }
+}
+
+void Engineer::import_finished(int)
+{
+       import_active = false;
+}
+
 void Engineer::process_new_train(Train &train)
 {
        Vehicle3D &loco3d = layout_3d.get_3d(train.get_vehicle(0));
index 418c0cdf730d9f4cfcee5e1bbd48320f69d581b1..d821e0581127ad28c315b4d6e61f8c47b02c653d 100644 (file)
@@ -10,6 +10,7 @@
 #include <msp/gltk/resources.h>
 #include <msp/gltk/root.h>
 #include "libr2c2/catalogue.h"
+#include "libr2c2/driver.h"
 #include "libr2c2/train.h"
 #include "3d/layout.h"
 #include "3d/overlay.h"
@@ -32,6 +33,7 @@ private:
        Msp::GLtk::Resources ui_res;
        Msp::GLtk::Root *root;
        Msp::GLtk::Arrangement *root_arrangement;
+       bool import_active;
 
        R2C2::Catalogue catalogue;
        R2C2::Layout layout;
@@ -72,6 +74,8 @@ private:
        void sensor_event(unsigned, bool);
        void block_reserved(const R2C2::Block &, const R2C2::Train *);
        R2C2::Object *pick_object(const R2C2::Vector &);
+       void locomotive_detected(const R2C2::Driver::DetectedLocomotive &);
+       void import_finished(int);
        void train_added(R2C2::Train &);
        void process_new_train(R2C2::Train &);
        virtual void sighandler(int);
index f4fbb102fe580b2f114ae0810f2852eb519aa07e..e84fc883df04252fd8dfd9d7378531bbb68a64b3 100644 (file)
@@ -20,6 +20,15 @@ public:
 };
 
 
+class DetectedLocoItem: public GLtk::List::MultiColumnItem
+{
+public:
+       typedef const Driver::DetectedLocomotive *ValueType;
+
+       DetectedLocoItem(ValueType);
+};
+
+
 TrainListDialog::TrainListDialog(Engineer &e):
        engineer(e),
        layout(engineer.get_layout())
@@ -31,6 +40,13 @@ TrainListDialog::TrainListDialog(Engineer &e):
        lst_trains->set_data(trains);
        lst_trains->set_item_type<TrainItem>();
 
+       lst_imported = dynamic_cast<GLtk::List *>(get_item(widgets, "lst_imported"));
+       lst_imported->set_data(detected_locos);
+       lst_imported->set_item_type<DetectedLocoItem>();
+
+       dynamic_cast<GLtk::Toggle *>(get_item(widgets, "tgl_trains"))->signal_toggled.connect(sigc::bind(sigc::mem_fun(this, &TrainListDialog::tab_toggled), lst_trains));
+       dynamic_cast<GLtk::Toggle *>(get_item(widgets, "tgl_import"))->signal_toggled.connect(sigc::bind(sigc::mem_fun(this, &TrainListDialog::tab_toggled), lst_imported));
+
        dynamic_cast<GLtk::Button *>(get_item(widgets, "btn_new"))->signal_clicked.connect(sigc::mem_fun(this, &TrainListDialog::new_clicked));
        dynamic_cast<GLtk::Button *>(get_item(widgets, "btn_show"))->signal_clicked.connect(sigc::mem_fun(this, &TrainListDialog::show_clicked));
 
@@ -41,6 +57,12 @@ TrainListDialog::TrainListDialog(Engineer &e):
                i->second->signal_name_changed.connect(sigc::hide(sigc::bind(sigc::mem_fun(this, &TrainListDialog::train_name_changed), sigc::ref(*i->second))));
        }
 
+       if(layout.has_driver())
+       {
+               Driver &driver = layout.get_driver();
+               driver.signal_locomotive_detected.connect(sigc::mem_fun(this, &TrainListDialog::locomotive_detected));
+               driver.signal_locomotive_gone.connect(sigc::mem_fun(this, &TrainListDialog::locomotive_gone));
+       }
        layout.signal_train_added.connect(sigc::mem_fun(this, &TrainListDialog::train_added));
        layout.signal_train_removed.connect(sigc::mem_fun(this, &TrainListDialog::train_removed));
 }
@@ -48,6 +70,12 @@ TrainListDialog::TrainListDialog(Engineer &e):
 void TrainListDialog::new_clicked()
 {
        NewTrainDialog *dlg = new NewTrainDialog(engineer);
+       if(lst_imported->is_visible())
+       {
+               int selected = lst_imported->get_selected_index();
+               if(selected>=0)
+                       dlg->prefill(*detected_locos.get(selected));
+       }
        find_ancestor<GLtk::Root>()->add(*dlg);
        dlg->autosize();
 }
@@ -62,6 +90,23 @@ void TrainListDialog::show_clicked()
        }
 }
 
+void TrainListDialog::tab_toggled(bool value, GLtk::Widget *wdg)
+{
+       wdg->set_visible(value);
+}
+
+void TrainListDialog::locomotive_detected(const Driver::DetectedLocomotive &loco)
+{
+       detected_locos.append(&loco);
+}
+
+void TrainListDialog::locomotive_gone(const Driver::DetectedLocomotive &loco)
+{
+       int index = detected_locos.find(&loco);
+       if(index>=0)
+               detected_locos.remove(index);
+}
+
 void TrainListDialog::train_added(Train &train)
 {
        unsigned n_items = trains.size();
@@ -94,3 +139,10 @@ TrainItem::TrainItem(ValueType train)
        add(*new GLtk::Label(train->get_name()));
 }
 
+
+DetectedLocoItem::DetectedLocoItem(ValueType loco)
+{
+       add(*new GLtk::Label(loco->protocol));
+       add(*new GLtk::Label(lexical_cast<string>(loco->address)));
+       add(*new GLtk::Label(loco->name));
+}
index 5757c731b4ba96f35b61f2211f5518e7dff2f916..9403d2f3fe50ac2cb2ba1c9062eb2e3a55e5c8bd 100644 (file)
@@ -16,6 +16,8 @@ private:
        R2C2::Layout &layout;
        Msp::GLtk::BasicListData<R2C2::Train *> trains;
        Msp::GLtk::List *lst_trains;
+       Msp::GLtk::BasicListData<const R2C2::Driver::DetectedLocomotive *> detected_locos;
+       Msp::GLtk::List *lst_imported;
 
 public:
        TrainListDialog(Engineer &);
@@ -23,6 +25,9 @@ public:
 private:
        void new_clicked();
        void show_clicked();
+       void tab_toggled(bool, Msp::GLtk::Widget *);
+       void locomotive_detected(const R2C2::Driver::DetectedLocomotive &);
+       void locomotive_gone(const R2C2::Driver::DetectedLocomotive &);
        void train_added(R2C2::Train &);
        void train_removed(R2C2::Train &);
        void train_name_changed(R2C2::Train &);