From: Mikko Rasa Date: Fri, 9 Aug 2013 10:54:57 +0000 (+0300) Subject: Use Widget::find_ancestor to reduce code duplication X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=b8cf5d2f394c151c6b095396dafdc66162cb060e;p=r2c2.git Use Widget::find_ancestor to reduce code duplication --- diff --git a/source/engineer/controlpanel.cpp b/source/engineer/controlpanel.cpp index 050b376..07651c0 100644 --- a/source/engineer/controlpanel.cpp +++ b/source/engineer/controlpanel.cpp @@ -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()->add(*dlg); dlg->autosize(); } diff --git a/source/engineer/mainwindow.cpp b/source/engineer/mainwindow.cpp index 6e3888c..f5c8df7 100644 --- a/source/engineer/mainwindow.cpp +++ b/source/engineer/mainwindow.cpp @@ -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()->add(*dlg); dlg->autosize(); } diff --git a/source/engineer/newtraindialog.cpp b/source/engineer/newtraindialog.cpp index 11fd5c0..78a3f11 100644 --- a/source/engineer/newtraindialog.cpp +++ b/source/engineer/newtraindialog.cpp @@ -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()->add(*dlg); dlg->autosize(); } } diff --git a/source/engineer/trainlistdialog.cpp b/source/engineer/trainlistdialog.cpp index 0a31d67..5d0c9fe 100644 --- a/source/engineer/trainlistdialog.cpp +++ b/source/engineer/trainlistdialog.cpp @@ -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()->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()->add(*dlg); dlg->autosize(); } } diff --git a/source/engineer/vehiclespanel.cpp b/source/engineer/vehiclespanel.cpp index d906f6c..cd3b7cf 100644 --- a/source/engineer/vehiclespanel.cpp +++ b/source/engineer/vehiclespanel.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #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()->add(*dlg); dlg->autosize(); }