subclass QPlainTextEdit for logwindows, so we can store its size on close

also some more settings tweaks (e.g. do not store window size when maximized)
and add setting to replace log windows instead or creating new ones.
This commit is contained in:
Axel Kohlmeyer
2023-08-05 10:33:51 -04:00
parent ea15cec4bf
commit d414dd52d5
5 changed files with 113 additions and 21 deletions

View File

@ -79,6 +79,8 @@ set(PROJECT_SOURCES
lammpswrapper.cpp
lammpswrapper.h
linenumberarea.h
logwindow.cpp
logwindow.h
preferences.cpp
preferences.h
stdcapture.cpp

View File

@ -16,6 +16,7 @@
#include "highlighter.h"
#include "imageviewer.h"
#include "lammpsrunner.h"
#include "logwindow.h"
#include "preferences.h"
#include "stdcapture.h"
#include "ui_lammpsgui.h"
@ -81,8 +82,8 @@ LammpsGui::LammpsGui(QWidget *parent, const char *filename) :
// restorge and initialize settings
QSettings settings;
// check and initialize nthreads setting. Default is to use max,
// but not override OMP_NUM_THREADS and preferences setting.
// check and initialize nthreads setting. Default is to use max if there
// is no preference but do not override OMP_NUM_THREADS
#if defined(_OPENMP)
// use maximum number of available threads unless OMP_NUM_THREADS was set
int nthreads = settings.value("nthreads", omp_get_max_threads()).toInt();
@ -155,11 +156,11 @@ LammpsGui::LammpsGui(QWidget *parent, const char *filename) :
status->setFixedWidth(300);
ui->statusbar->addWidget(status);
dirstatus = new QLabel(QString(" Directory: ") + current_dir);
dirstatus->setMinimumWidth(500);
dirstatus->setMinimumWidth(400);
ui->statusbar->addWidget(dirstatus);
progress = new QProgressBar();
progress->setRange(0, 1000);
progress->setMinimumWidth(500);
progress->setMinimumWidth(400);
progress->hide();
dirstatus->show();
ui->statusbar->addWidget(progress);
@ -191,8 +192,7 @@ LammpsGui::LammpsGui(QWidget *parent, const char *filename) :
} else {
setWindowTitle(QString("LAMMPS-GUI - *unknown*"));
}
resize(settings.value("mainx", 500).toInt(), settings.value("mainy", 320).toInt());
resize(settings.value("mainx", "500").toInt(), settings.value("mainy", "320").toInt());
}
LammpsGui::~LammpsGui()
@ -335,8 +335,10 @@ void LammpsGui::quit()
// store some global settings
QSettings settings;
settings.setValue("mainx", width());
settings.setValue("mainy", height());
if (!isMaximized()) {
settings.setValue("mainx", width());
settings.setValue("mainy", height());
}
settings.sync();
QCoreApplication::quit();
}
@ -412,7 +414,7 @@ void LammpsGui::modified()
void LammpsGui::run_done()
{
logupdater->stop();
if (logupdater) logupdater->stop();
delete logupdater;
logupdater = nullptr;
progress->setValue(1000);
@ -444,12 +446,23 @@ void LammpsGui::run_done()
void LammpsGui::run_buffer()
{
if (lammps.is_running()) {
QMessageBox::warning(this, "LAMMPS GUI Error",
"Must stop current run before starting a new run");
return;
}
QSettings settings;
progress->setValue(0);
dirstatus->hide();
progress->show();
int nthreads = settings.value("nthreads", 1).toInt();
status->setText(QString("Running LAMMPS with %1 thread(s)...").arg(nthreads));
int accel = settings.value("accelerator", AcceleratorTab::None).toInt();
if ((accel != AcceleratorTab::OpenMP) && (accel != AcceleratorTab::Intel) &&
(accel != AcceleratorTab::Kokkos)) nthreads = 1;
if (nthreads > 1)
status->setText(QString("Running LAMMPS with %1 thread(s)...").arg(nthreads));
else
status->setText(QString("Running LAMMPS ..."));
status->repaint();
start_lammps();
if (!lammps.is_open()) return;
@ -466,7 +479,9 @@ void LammpsGui::run_buffer()
connect(runner, &LammpsRunner::finished, runner, &QObject::deleteLater);
runner->start();
logwindow = new QPlainTextEdit();
// if configured, delete old log window before opening new one
if (settings.value("logreplace", false).toBool()) delete logwindow;
logwindow = new LogWindow();
logwindow->setReadOnly(true);
logwindow->setCenterOnScroll(true);
logwindow->moveCursor(QTextCursor::End);
@ -480,17 +495,17 @@ void LammpsGui::run_buffer()
#endif
text_font.setStyleHint(QFont::TypeWriter);
logwindow->document()->setDefaultFont(text_font);
logwindow->setLineWrapMode(QPlainTextEdit::NoWrap);
logwindow->setMinimumSize(600, 400);
logwindow->setLineWrapMode(LogWindow::NoWrap);
logwindow->setMinimumSize(400, 300);
QShortcut *shortcut = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_W), logwindow);
QObject::connect(shortcut, &QShortcut::activated, logwindow, &QPlainTextEdit::close);
QObject::connect(shortcut, &QShortcut::activated, logwindow, &LogWindow::close);
shortcut = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_Slash), logwindow);
QObject::connect(shortcut, &QShortcut::activated, this, &LammpsGui::stop_run);
logwindow->show();
logupdater = new QTimer(this);
connect(logupdater, &QTimer::timeout, this, &LammpsGui::logupdate);
logupdater->start(1000);
logupdater->start(500);
}
void LammpsGui::view_image()

View File

@ -0,0 +1,37 @@
/* ----------------------------------------------------------------------
LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
https://www.lammps.org/, Sandia National Laboratories
LAMMPS development team: developers@lammps.org
Copyright (2003) Sandia Corporation. Under the terms of Contract
DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains
certain rights in this software. This software is distributed under
the GNU General Public License.
See the README file in the top-level LAMMPS directory.
------------------------------------------------------------------------- */
#include "logwindow.h"
#include <QSettings>
#include <cstdio>
LogWindow::LogWindow(QWidget *parent) : QPlainTextEdit(parent)
{
QSettings settings;
resize(settings.value("logx", 500).toInt(), settings.value("logy", 320).toInt());
}
void LogWindow::closeEvent(QCloseEvent *event)
{
fprintf(stderr, "log closing\n");
QSettings settings;
if (!isMaximized()) {
settings.setValue("logx", width());
settings.setValue("logy", height());
}
QPlainTextEdit::closeEvent(event);
}
// Local Variables:
// c-basic-offset: 4
// End:

View File

@ -0,0 +1,32 @@
/* -*- c++ -*- ----------------------------------------------------------
LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
https://www.lammps.org/, Sandia National Laboratories
LAMMPS development team: developers@lammps.org
Copyright (2003) Sandia Corporation. Under the terms of Contract
DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains
certain rights in this software. This software is distributed under
the GNU General Public License.
See the README file in the top-level LAMMPS directory.
------------------------------------------------------------------------- */
#ifndef LOGWINDOW_H
#define LOGWINDOW_H
#include <QPlainTextEdit>
class LogWindow : public QPlainTextEdit {
Q_OBJECT
public:
LogWindow(QWidget *parent = nullptr);
protected:
void closeEvent(QCloseEvent *event) override;
};
#endif
// Local Variables:
// c-basic-offset: 4
// End:

View File

@ -110,10 +110,11 @@ void Preferences::accept()
// general settings
QCheckBox *box = tabWidget->findChild<QCheckBox *>("echo");
if (box) settings->setValue("echo", box->isChecked() ? "1" : "0");
if (box) settings->setValue("echo", box->isChecked());
box = tabWidget->findChild<QCheckBox *>("cite");
if (box) settings->setValue("cite", box->isChecked() ? "1" : "0");
if (box) settings->setValue("cite", box->isChecked());
box = tabWidget->findChild<QCheckBox *>("logreplace");
if (box) settings->setValue("logreplace", box->isChecked());
QDialog::accept();
}
@ -123,11 +124,15 @@ GeneralTab::GeneralTab(QSettings *_settings, LammpsWrapper *_lammps, QWidget *pa
auto *layout = new QVBoxLayout;
auto *echo = new QCheckBox("Echo input to log");
echo->setCheckState(settings->value("echo", "0").toInt() ? Qt::Checked : Qt::Unchecked);
echo->setObjectName("echo");
auto *cite = new QCheckBox("Include Citations");
cite->setCheckState(settings->value("cite", "0").toInt() ? Qt::Checked : Qt::Unchecked);
echo->setCheckState(settings->value("echo", false).toBool() ? Qt::Checked : Qt::Unchecked);
auto *cite = new QCheckBox("Include citation details");
cite->setObjectName("cite");
cite->setCheckState(settings->value("cite", false).toBool() ? Qt::Checked : Qt::Unchecked);
auto *logr = new QCheckBox("Replace log window on new run");
logr->setObjectName("logreplace");
logr->setCheckState(settings->value("logreplace", false).toBool() ? Qt::Checked
: Qt::Unchecked);
#if !defined(__APPLE__)
auto *tmplabel = new QLabel("Scratch Folder:");
auto *tmpedit = new QLineEdit(settings->value("tempdir", ".").toString());
@ -153,6 +158,7 @@ GeneralTab::GeneralTab(QSettings *_settings, LammpsWrapper *_lammps, QWidget *pa
#endif
layout->addWidget(echo);
layout->addWidget(cite);
layout->addWidget(logr);
#if !defined(__APPLE__)
layout->addLayout(tmplayout);
#endif