move redundant code to find pointer to main widget to helper function

This commit is contained in:
Axel Kohlmeyer
2025-06-14 19:04:43 -04:00
parent 90df96b16f
commit c5c1f47c20
10 changed files with 31 additions and 37 deletions

View File

@ -13,6 +13,7 @@
#include "chartviewer.h"
#include "helpers.h"
#include "lammpsgui.h"
#include <QAction>
@ -221,9 +222,7 @@ void ChartWindow::add_data(int step, double data, int index)
void ChartWindow::quit()
{
LammpsGui *main = nullptr;
for (QWidget *widget : QApplication::topLevelWidgets())
if (widget->objectName() == "LammpsGui") main = dynamic_cast<LammpsGui *>(widget);
auto *main = dynamic_cast<LammpsGui *>(get_main_widget());
if (main) main->quit();
}
@ -238,9 +237,7 @@ void ChartWindow::reset_zoom()
void ChartWindow::stop_run()
{
LammpsGui *main = nullptr;
for (QWidget *widget : QApplication::topLevelWidgets())
if (widget->objectName() == "LammpsGui") main = dynamic_cast<LammpsGui *>(widget);
auto *main = dynamic_cast<LammpsGui *>(get_main_widget());
if (main) main->stop_run();
}

View File

@ -13,6 +13,7 @@
#include "fileviewer.h"
#include "helpers.h"
#include "lammpsgui.h"
#include <QApplication>
@ -107,17 +108,13 @@ FileViewer::FileViewer(const QString &_filename, QString title, QWidget *parent)
void FileViewer::quit()
{
LammpsGui *main = nullptr;
for (QWidget *widget : QApplication::topLevelWidgets())
if (widget->objectName() == "LammpsGui") main = dynamic_cast<LammpsGui *>(widget);
auto *main = dynamic_cast<LammpsGui *>(get_main_widget());
if (main) main->quit();
}
void FileViewer::stop_run()
{
LammpsGui *main = nullptr;
for (QWidget *widget : QApplication::topLevelWidgets())
if (widget->objectName() == "LammpsGui") main = dynamic_cast<LammpsGui *>(widget);
auto *main = dynamic_cast<LammpsGui *>(get_main_widget());
if (main) main->stop_run();
}

View File

@ -14,6 +14,7 @@
#include "findandreplace.h"
#include "codeeditor.h"
#include "helpers.h"
#include "lammpsgui.h"
#include <QApplication>
@ -137,9 +138,7 @@ void FindAndReplace::replace_all()
void FindAndReplace::quit()
{
LammpsGui *main = nullptr;
for (QWidget *widget : QApplication::topLevelWidgets())
if (widget->objectName() == "LammpsGui") main = dynamic_cast<LammpsGui *>(widget);
auto *main = dynamic_cast<LammpsGui *>(get_main_widget());
if (main) main->quit();
}

View File

@ -13,6 +13,7 @@
#include "helpers.h"
#include <QApplication>
#include <QBrush>
#include <QColor>
#include <QDir>
@ -21,6 +22,7 @@
#include <QPalette>
#include <QProcess>
#include <QStringList>
#include <QWidget>
// duplicate string, STL version
char *mystrdup(const std::string &text)
@ -42,6 +44,15 @@ char *mystrdup(const QString &text)
return mystrdup(text.toStdString());
}
// get pointer to LAMMPS-GUI main widget
QWidget *get_main_widget()
{
for (QWidget *widget : QApplication::topLevelWidgets())
if (widget->objectName() == "LammpsGui") return widget;
return nullptr;
}
// find if executable is in path
// https://stackoverflow.com/a/51041497

View File

@ -22,6 +22,9 @@ extern char *mystrdup(const std::string &text);
extern char *mystrdup(const char *text);
extern char *mystrdup(const QString &text);
// get pointer to LAMMPS-GUI main widget
extern class QWidget *get_main_widget();
// find if executable is in path
extern bool has_exe(const QString &exe);

View File

@ -13,6 +13,7 @@
#include "imageviewer.h"
#include "helpers.h"
#include "lammpsgui.h"
#include "lammpswrapper.h"
@ -769,9 +770,7 @@ void ImageViewer::copy() {}
void ImageViewer::quit()
{
LammpsGui *main = nullptr;
for (QWidget *widget : QApplication::topLevelWidgets())
if (widget->objectName() == "LammpsGui") main = dynamic_cast<LammpsGui *>(widget);
auto *main = dynamic_cast<LammpsGui *>(get_main_widget());
if (main) main->quit();
}

View File

@ -2215,9 +2215,7 @@ void TutorialWizard::accept()
// get hold of LAMMPS-GUI main widget
if (dirname) {
LammpsGui *main = nullptr;
for (QWidget *widget : QApplication::topLevelWidgets())
if (widget->objectName() == "LammpsGui") main = dynamic_cast<LammpsGui *>(widget);
auto *main = dynamic_cast<LammpsGui *>(get_main_widget());
if (main) main->setup_tutorial(_ntutorial, curdir, purgedir, getsolution, openwebpage);
}
}

View File

@ -14,6 +14,7 @@
#include "logwindow.h"
#include "flagwarnings.h"
#include "helpers.h"
#include "lammpsgui.h"
#include <QAction>
@ -107,17 +108,13 @@ void LogWindow::closeEvent(QCloseEvent *event)
void LogWindow::quit()
{
LammpsGui *main = nullptr;
for (QWidget *widget : QApplication::topLevelWidgets())
if (widget->objectName() == "LammpsGui") main = dynamic_cast<LammpsGui *>(widget);
auto *main = dynamic_cast<LammpsGui *>(get_main_widget());
if (main) main->quit();
}
void LogWindow::stop_run()
{
LammpsGui *main = nullptr;
for (QWidget *widget : QApplication::topLevelWidgets())
if (widget->objectName() == "LammpsGui") main = dynamic_cast<LammpsGui *>(widget);
auto *main = dynamic_cast<LammpsGui *>(get_main_widget());
if (main) main->stop_run();
}

View File

@ -337,10 +337,7 @@ GeneralTab::GeneralTab(QSettings *_settings, LammpsWrapper *_lammps, QWidget *pa
void GeneralTab::updatefonts(const QFont &all, const QFont &text)
{
LammpsGui *main = nullptr;
for (QWidget *widget : QApplication::topLevelWidgets())
if (widget->objectName() == "LammpsGui") main = dynamic_cast<LammpsGui *>(widget);
auto *main = dynamic_cast<LammpsGui *>(get_main_widget());
if (main) {
main->setFont(all);
main->ui->textEdit->document()->setDefaultFont(text);

View File

@ -212,17 +212,13 @@ void SlideShow::loadImage(int idx)
void SlideShow::quit()
{
LammpsGui *main = nullptr;
for (QWidget *widget : QApplication::topLevelWidgets())
if (widget->objectName() == "LammpsGui") main = dynamic_cast<LammpsGui *>(widget);
auto *main = dynamic_cast<LammpsGui *>(get_main_widget());
if (main) main->quit();
}
void SlideShow::stop_run()
{
LammpsGui *main = nullptr;
for (QWidget *widget : QApplication::topLevelWidgets())
if (widget->objectName() == "LammpsGui") main = dynamic_cast<LammpsGui *>(widget);
auto *main = dynamic_cast<LammpsGui *>(get_main_widget());
if (main) main->stop_run();
}