port LAMMPS GUI to Qt6 while still supporting Qt5

This commit is contained in:
Axel Kohlmeyer
2023-10-12 23:24:11 -04:00
parent 1a9dac83d7
commit 6ad5c0eced
5 changed files with 71 additions and 24 deletions

View File

@ -11,6 +11,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
option(LAMMPS_GUI_USE_PLUGIN "Load LAMMPS library dynamically at runtime" OFF) option(LAMMPS_GUI_USE_PLUGIN "Load LAMMPS library dynamically at runtime" OFF)
mark_as_advanced(LAMMPS_GUI_USE_PLUGIN) mark_as_advanced(LAMMPS_GUI_USE_PLUGIN)
option(LAMMPS_GUI_USE_QT5 "Prefer using Qt5 over Qt6" OFF)
# checks # checks
# when this file is included as subdirectory in the LAMMPS build, many settings are directly imported # when this file is included as subdirectory in the LAMMPS build, many settings are directly imported
@ -73,7 +74,15 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
endif() endif()
# we require Qt 5 and at least version 5.12 at that. # we require Qt 5 and at least version 5.12 at that.
find_package(Qt5 5.12 REQUIRED COMPONENTS Widgets Charts) if(NOT LAMMPS_GUI_USE_QT5)
find_package(Qt6 6.2 COMPONENTS Widgets Charts)
endif()
if(NOT Qt6_FOUND)
find_package(Qt5 5.12 REQUIRED COMPONENTS Widgets Charts)
set(QT_VERSION_MAJOR "5")
else()
set(QT_VERSION_MAJOR "6")
endif()
set(PROJECT_SOURCES set(PROJECT_SOURCES
main.cpp main.cpp
@ -105,7 +114,11 @@ set(PROJECT_SOURCES
${PLUGIN_LOADER_SRC} ${PLUGIN_LOADER_SRC}
${ICON_RC_FILE} ${ICON_RC_FILE}
) )
qt5_add_resources(PROJECT_SOURCES lammpsgui.qrc) if(QT_VERSION_MAJOR EQUAL 6)
qt6_add_resources(PROJECT_SOURCES lammpsgui.qrc)
else()
qt5_add_resources(PROJECT_SOURCES lammpsgui.qrc)
endif()
if(APPLE) if(APPLE)
set(MACOSX_ICON_FILE ${LAMMPS_DIR}/cmake/packaging/lammps.icns) set(MACOSX_ICON_FILE ${LAMMPS_DIR}/cmake/packaging/lammps.icns)
@ -113,10 +126,22 @@ if(APPLE)
set(MACOSX_BACKGROUND_FILE ${LAMMPS_DIR}/cmake/packaging/LAMMPS_DMG_Background.png) set(MACOSX_BACKGROUND_FILE ${LAMMPS_DIR}/cmake/packaging/LAMMPS_DMG_Background.png)
endif() endif()
add_executable(lammps-gui if(QT_VERSION_MAJOR EQUAL 6)
${MACOSX_ICON_FILE} qt_add_executable(lammps-gui
${PROJECT_SOURCES} MANUAL_FINALIZATION
) ${MACOSX_ICON_FILE}
${PROJECT_SOURCES}
)
else()
add_executable(lammps-gui
${MACOSX_ICON_FILE}
${PROJECT_SOURCES}
)
endif()
if(QT_VERSION_MAJOR EQUAL 6)
qt_finalize_executable(lammps-gui)
endif()
# compilation settings # compilation settings
if(LAMMPS_GUI_USE_PLUGIN) if(LAMMPS_GUI_USE_PLUGIN)
@ -128,7 +153,7 @@ else()
endif() endif()
target_include_directories(lammps-gui PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) target_include_directories(lammps-gui PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
target_compile_definitions(lammps-gui PRIVATE LAMMPS_GUI_VERSION="${PROJECT_VERSION}") target_compile_definitions(lammps-gui PRIVATE LAMMPS_GUI_VERSION="${PROJECT_VERSION}")
target_link_libraries(lammps-gui PRIVATE Qt5::Widgets Qt5::Charts) target_link_libraries(lammps-gui PRIVATE Qt${QT_VERSION_MAJOR}::Widgets Qt${VERSION_MAJOR}::Charts)
if(BUILD_OMP) if(BUILD_OMP)
find_package(OpenMP COMPONENTS CXX REQUIRED) find_package(OpenMP COMPONENTS CXX REQUIRED)
target_link_libraries(lammps-gui PRIVATE OpenMP::OpenMP_CXX) target_link_libraries(lammps-gui PRIVATE OpenMP::OpenMP_CXX)
@ -209,7 +234,7 @@ elseif((CMAKE_SYSTEM_NAME STREQUAL "Windows") AND CMAKE_CROSSCOMPILING)
COMMENT "Create zip file with windows binaries" COMMENT "Create zip file with windows binaries"
BYPRODUCT LAMMPS-Win10-amd64.zip BYPRODUCT LAMMPS-Win10-amd64.zip
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}) WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux") elseif((CMAKE_SYSTEM_NAME STREQUAL "Linux") AND NOT LAMMPS_GUI_USE_PLUGIN)
install(TARGETS lammps-gui DESTINATION ${CMAKE_INSTALL_BINDIR}) install(TARGETS lammps-gui DESTINATION ${CMAKE_INSTALL_BINDIR})
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/lammps-gui.desktop DESTINATION ${CMAKE_INSTALL_DATADIR}/applications/) install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/lammps-gui.desktop DESTINATION ${CMAKE_INSTALL_DATADIR}/applications/)
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/lammps-input.xml DESTINATION ${CMAKE_INSTALL_DATADIR}/mime/packages/) install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/lammps-input.xml DESTINATION ${CMAKE_INSTALL_DATADIR}/mime/packages/)

View File

@ -15,11 +15,19 @@
#include "lammpsgui.h" #include "lammpsgui.h"
#include <QAction>
#include <QApplication>
#include <QFileDialog>
#include <QHBoxLayout> #include <QHBoxLayout>
#include <QLabel>
#include <QLayout>
#include <QLineSeries> #include <QLineSeries>
#include <QMenu>
#include <QMenuBar>
#include <QPushButton> #include <QPushButton>
#include <QSettings> #include <QSettings>
#include <QSpacerItem> #include <QSpacerItem>
#include <QTextStream>
#include <QVBoxLayout> #include <QVBoxLayout>
using namespace QtCharts; using namespace QtCharts;
@ -288,7 +296,7 @@ void ChartViewer::add_data(int step, double data)
if (last_step < step) { if (last_step < step) {
last_step = step; last_step = step;
series->append(step, data); series->append(step, data);
auto points = series->pointsVector(); auto points = series->points();
qreal xmin = 1.0e100; qreal xmin = 1.0e100;
qreal xmax = -1.0e100; qreal xmax = -1.0e100;
@ -309,7 +317,7 @@ void ChartViewer::add_data(int step, double data)
void ChartViewer::reset_zoom() void ChartViewer::reset_zoom()
{ {
auto points = series->pointsVector(); auto points = series->points();
qreal xmin = 1.0e100; qreal xmin = 1.0e100;
qreal xmax = -1.0e100; qreal xmax = -1.0e100;

View File

@ -14,16 +14,17 @@
#ifndef CHARTVIEWER_H #ifndef CHARTVIEWER_H
#define CHARTVIEWER_H #define CHARTVIEWER_H
#include <QComboBox>
#include <QList> #include <QList>
#include <QString> #include <QString>
#include <QWidget> #include <QWidget>
#include <QtCharts>
class QAction; class QAction;
class QMenuBar; class QMenuBar;
class QMenu; class QMenu;
class QComboBox; namespace QtCharts {
class ChartViewer; class ChartViewer;
}
class ChartWindow : public QWidget { class ChartWindow : public QWidget {
Q_OBJECT Q_OBJECT
@ -64,12 +65,18 @@ private:
QAction *closeAct, *stopAct, *quitAct; QAction *closeAct, *stopAct, *quitAct;
QString filename; QString filename;
QList<ChartViewer *> charts; QList<QtCharts::ChartViewer *> charts;
}; };
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
class ChartViewer : public QtCharts::QChartView { #include <QChart>
#include <QChartView>
#include <QLineSeries>
#include <QValueAxis>
namespace QtCharts {
class ChartViewer : public QChartView {
Q_OBJECT Q_OBJECT
public: public:
@ -86,11 +93,12 @@ public:
private: private:
int last_step, index; int last_step, index;
QtCharts::QChart *chart; QChart *chart;
QtCharts::QLineSeries *series; QLineSeries *series;
QtCharts::QValueAxis *xaxis; QValueAxis *xaxis;
QtCharts::QValueAxis *yaxis; QValueAxis *yaxis;
}; };
} // namespace QtCharts
#endif #endif
// Local Variables: // Local Variables:

View File

@ -35,6 +35,7 @@
#include <QLabel> #include <QLabel>
#include <QLocale> #include <QLocale>
#include <QMessageBox> #include <QMessageBox>
#include <QMetaType>
#include <QPlainTextEdit> #include <QPlainTextEdit>
#include <QProcess> #include <QProcess>
#include <QProgressBar> #include <QProgressBar>
@ -69,8 +70,10 @@ LammpsGui::LammpsGui(QWidget *parent, const char *filename) :
// enforce using the plain ASCII C locale within the GUI. // enforce using the plain ASCII C locale within the GUI.
QLocale::setDefault(QLocale("C")); QLocale::setDefault(QLocale("C"));
// register QList<QString> #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
// register QList<QString> only needed for Qt5
qRegisterMetaTypeStreamOperators<QList<QString>>("QList<QString>"); qRegisterMetaTypeStreamOperators<QList<QString>>("QList<QString>");
#endif
ui->setupUi(this); ui->setupUi(this);
this->setCentralWidget(ui->textEdit); this->setCentralWidget(ui->textEdit);
@ -588,7 +591,8 @@ void LammpsGui::open_file(const QString &fileName)
if (!file.open(QIODevice::ReadOnly | QFile::Text)) { if (!file.open(QIODevice::ReadOnly | QFile::Text)) {
QMessageBox::warning(this, "Warning", QMessageBox::warning(this, "Warning",
"Cannot open file " + path.absoluteFilePath() + ": " + "Cannot open file " + path.absoluteFilePath() + ": " +
file.errorString() + ".\nWill create new file on saving editor buffer."); file.errorString() +
".\nWill create new file on saving editor buffer.");
ui->textEdit->document()->setPlainText(QString()); ui->textEdit->document()->setPlainText(QString());
} else { } else {
QTextStream in(&file); QTextStream in(&file);
@ -1039,9 +1043,9 @@ void LammpsGui::do_run(bool use_buffer)
logwindow->document()->setDefaultFont(text_font); logwindow->document()->setDefaultFont(text_font);
logwindow->setLineWrapMode(LogWindow::NoWrap); logwindow->setLineWrapMode(LogWindow::NoWrap);
logwindow->setMinimumSize(400, 300); logwindow->setMinimumSize(400, 300);
QShortcut *shortcut = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_W), logwindow); QShortcut *shortcut = new QShortcut(QKeySequence(Qt::CTRL | Qt::Key_W), logwindow);
QObject::connect(shortcut, &QShortcut::activated, logwindow, &LogWindow::close); QObject::connect(shortcut, &QShortcut::activated, logwindow, &LogWindow::close);
shortcut = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_Slash), logwindow); shortcut = new QShortcut(QKeySequence(Qt::CTRL | Qt::Key_Slash), logwindow);
QObject::connect(shortcut, &QShortcut::activated, this, &LammpsGui::stop_run); QObject::connect(shortcut, &QShortcut::activated, this, &LammpsGui::stop_run);
if (settings.value("viewlog", true).toBool()) if (settings.value("viewlog", true).toBool())
logwindow->show(); logwindow->show();
@ -1058,9 +1062,9 @@ void LammpsGui::do_run(bool use_buffer)
.arg(run_counter)); .arg(run_counter));
chartwindow->setWindowIcon(QIcon(":/icons/lammps-icon-128x128.png")); chartwindow->setWindowIcon(QIcon(":/icons/lammps-icon-128x128.png"));
chartwindow->setMinimumSize(400, 300); chartwindow->setMinimumSize(400, 300);
shortcut = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_W), chartwindow); shortcut = new QShortcut(QKeySequence(Qt::CTRL | Qt::Key_W), chartwindow);
QObject::connect(shortcut, &QShortcut::activated, chartwindow, &ChartWindow::close); QObject::connect(shortcut, &QShortcut::activated, chartwindow, &ChartWindow::close);
shortcut = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_Slash), chartwindow); shortcut = new QShortcut(QKeySequence(Qt::CTRL | Qt::Key_Slash), chartwindow);
QObject::connect(shortcut, &QShortcut::activated, this, &LammpsGui::stop_run); QObject::connect(shortcut, &QShortcut::activated, this, &LammpsGui::stop_run);
if (settings.value("viewchart", true).toBool()) if (settings.value("viewchart", true).toBool())
chartwindow->show(); chartwindow->show();

View File

@ -16,8 +16,10 @@
#include <QMainWindow> #include <QMainWindow>
#include <QGridLayout>
#include <QList> #include <QList>
#include <QPair> #include <QPair>
#include <QSpacerItem>
#include <QString> #include <QString>
#include <vector> #include <vector>