]> git.tdb.fi Git - r2c2.git/commitdiff
Use Widget::find_ancestor to reduce code duplication
authorMikko Rasa <tdb@tdb.fi>
Fri, 9 Aug 2013 10:54:57 +0000 (13:54 +0300)
committerMikko Rasa <tdb@tdb.fi>
Fri, 9 Aug 2013 13:32:30 +0000 (16:32 +0300)
source/engineer/controlpanel.cpp
source/engineer/mainwindow.cpp
source/engineer/newtraindialog.cpp
source/engineer/trainlistdialog.cpp
source/engineer/vehiclespanel.cpp

index 050b37623fb98c472294c75078ea325f39e98312..07651c07929fdf5f21d8d65f873f5f997c191911 100644 (file)
@@ -75,12 +75,8 @@ void ControlPanel::take_clicked()
 
 void ControlPanel::view_clicked()
 {
-       GLtk::Container *root = parent;
-       while(root->get_parent())
-               root = root->get_parent();
-
        TrainView *dlg = new TrainView(engineer, train);
-       root->add(*dlg);
+       find_ancestor<GLtk::Root>()->add(*dlg);
        dlg->autosize();
 }
 
index 6e3888c19b81978f476e5f2441fe335723914975..f5c8df73bd15b57d375819bef8b29fbf358cff23 100644 (file)
@@ -57,12 +57,8 @@ void MainWindow::halt_clicked()
 
 void MainWindow::trains_clicked()
 {
-       GLtk::Container *root = parent;
-       while(root->get_parent())
-               root = root->get_parent();
-
        TrainListDialog *dlg = new TrainListDialog(engineer);
-       root->add(*dlg);
+       find_ancestor<GLtk::Root>()->add(*dlg);
        dlg->autosize();
 }
 
index 11fd5c04d6f1331c607926c8d84256216d9e44df..78a3f113b6b0a161ed1ea96e2d6ddddb83a28f60 100644 (file)
@@ -67,12 +67,8 @@ void NewTrainDialog::on_response(int code)
                Train *train = new Train(engineer.get_layout(), type, address, protocol);
                train->set_name(ent_name->get_text());
 
-               GLtk::Container *root = parent;
-               while(root->get_parent())
-                       root = root->get_parent();
-
                TrainDialog *dlg = new TrainDialog(engineer, *train);
-               root->add(*dlg);
+               find_ancestor<GLtk::Root>()->add(*dlg);
                dlg->autosize();
        }
 }
index 0a31d67808c7a72a275fd41136d01c4bc0c87d67..5d0c9fec9d85402f94dd16c068c8947ed3bab551 100644 (file)
@@ -48,12 +48,8 @@ TrainListDialog::TrainListDialog(Engineer &e):
 
 void TrainListDialog::new_clicked()
 {
-       GLtk::Container *root = parent;
-       while(root->get_parent())
-               root = root->get_parent();
-
        NewTrainDialog *dlg = new NewTrainDialog(engineer);
-       root->add(*dlg);
+       find_ancestor<GLtk::Root>()->add(*dlg);
        dlg->autosize();
 }
 
@@ -62,12 +58,8 @@ void TrainListDialog::show_clicked()
        int index = lst_trains->get_selected_index();
        if(index>=0)
        {
-               GLtk::Container *root = parent;
-               while(root->get_parent())
-                       root = root->get_parent();
-
                TrainDialog *dlg = new TrainDialog(engineer, *trains.get(index));
-               root->add(*dlg);
+               find_ancestor<GLtk::Root>()->add(*dlg);
                dlg->autosize();
        }
 }
index d906f6cb4218e5976a0b0d468941a12553c1d049..cd3b7cf25fa3be207edaa6c6c1db80f9ab887c34 100644 (file)
@@ -1,5 +1,6 @@
 #include <msp/core/maputils.h>
 #include <msp/gltk/button.h>
+#include <msp/gltk/root.h>
 #include <msp/strings/format.h>
 #include "libr2c2/layout.h"
 #include "libr2c2/vehicle.h"
@@ -40,12 +41,8 @@ VehiclesPanel::VehiclesPanel(Train &t):
 
 void VehiclesPanel::add_clicked()
 {
-       GLtk::Container *root = parent;
-       while(root->get_parent())
-               root = root->get_parent();
-
        NewVehicleDialog *dlg = new NewVehicleDialog(train);
-       root->add(*dlg);
+       find_ancestor<GLtk::Root>()->add(*dlg);
        dlg->autosize();
 }