]> git.tdb.fi Git - r2c2.git/commitdiff
Keep the train list dialog up to date whem trains are added or removed
authorMikko Rasa <tdb@tdb.fi>
Wed, 6 Nov 2013 12:59:02 +0000 (14:59 +0200)
committerMikko Rasa <tdb@tdb.fi>
Wed, 6 Nov 2013 12:59:02 +0000 (14:59 +0200)
source/engineer/trainlistdialog.cpp
source/engineer/trainlistdialog.h

index 5d0c9fec9d85402f94dd16c068c8947ed3bab551..ce1bcb8902f27ca0ed6688755f7abb651c601059 100644 (file)
@@ -43,7 +43,13 @@ TrainListDialog::TrainListDialog(Engineer &e):
 
        const map<unsigned, Train *> &ltrains = layout.get_trains();
        for(map<unsigned, Train *>::const_iterator i=ltrains.begin(); i!=ltrains.end(); ++i)
+       {
                trains.append(i->second);
+               i->second->signal_name_changed.connect(sigc::hide(sigc::bind(sigc::mem_fun(this, &TrainListDialog::train_name_changed), sigc::ref(*i->second))));
+       }
+
+       layout.signal_train_added.connect(sigc::mem_fun(this, &TrainListDialog::train_added));
+       layout.signal_train_removed.connect(sigc::mem_fun(this, &TrainListDialog::train_removed));
 }
 
 void TrainListDialog::new_clicked()
@@ -64,6 +70,30 @@ void TrainListDialog::show_clicked()
        }
 }
 
+void TrainListDialog::train_added(Train &train)
+{
+       unsigned n_items = trains.size();
+       unsigned i;
+       for(i=0; i<n_items; ++i)
+               if(trains.get(i)->get_address()>train.get_address())
+                       break;
+       trains.insert(i, &train);
+
+       train.signal_name_changed.connect(sigc::hide(sigc::bind(sigc::mem_fun(this, &TrainListDialog::train_name_changed), sigc::ref(train))));
+}
+
+void TrainListDialog::train_removed(Train &train)
+{
+       int i = trains.find(&train);
+       if(i>=0)
+               trains.remove(i);
+}
+
+void TrainListDialog::train_name_changed(Train &train)
+{
+       trains.refresh(&train);
+}
+
 
 TrainItem::TrainItem(ValueType train):
        address(lexical_cast<string>(train->get_address())),
index c12bcd4fc72c8a6bfb95f348f3605a2192c86f7e..5757c731b4ba96f35b61f2211f5518e7dff2f916 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef TRAINLISTDIALOG_H_
 #define TRAINLISTDIALOG_H_
 
+#include <sigc++/trackable.h>
 #include <msp/gltk/dialog.h>
 #include <msp/gltk/list.h>
 #include <msp/gltk/listdata.h>
@@ -8,7 +9,7 @@
 
 class Engineer;
 
-class TrainListDialog: public Msp::GLtk::Dialog
+class TrainListDialog: public Msp::GLtk::Dialog, public sigc::trackable
 {
 private:
        Engineer &engineer;
@@ -22,6 +23,9 @@ public:
 private:
        void new_clicked();
        void show_clicked();
+       void train_added(R2C2::Train &);
+       void train_removed(R2C2::Train &);
+       void train_name_changed(R2C2::Train &);
 };
 
 #endif