export to YAML functionality is only available if there is YAML format data
This commit is contained in:
@ -1,6 +1,6 @@
|
|||||||
cmake_minimum_required(VERSION 3.16)
|
cmake_minimum_required(VERSION 3.16)
|
||||||
|
|
||||||
project(lammps-gui VERSION 1.6.1 LANGUAGES CXX)
|
project(lammps-gui VERSION 1.6.2 LANGUAGES CXX)
|
||||||
|
|
||||||
set(CMAKE_AUTOUIC ON)
|
set(CMAKE_AUTOUIC ON)
|
||||||
set(CMAKE_AUTOMOC ON)
|
set(CMAKE_AUTOMOC ON)
|
||||||
|
|||||||
@ -30,6 +30,8 @@
|
|||||||
#include <QString>
|
#include <QString>
|
||||||
#include <QTextStream>
|
#include <QTextStream>
|
||||||
|
|
||||||
|
const QString LogWindow::yaml_regex = QStringLiteral("^(keywords:.*$|data:$|---$|\\.\\.\\.$| - \\[.*\\]$)");
|
||||||
|
|
||||||
LogWindow::LogWindow(const QString &_filename, QWidget *parent) :
|
LogWindow::LogWindow(const QString &_filename, QWidget *parent) :
|
||||||
QPlainTextEdit(parent), filename(_filename)
|
QPlainTextEdit(parent), filename(_filename)
|
||||||
{
|
{
|
||||||
@ -97,23 +99,35 @@ void LogWindow::save_as()
|
|||||||
file.close();
|
file.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool LogWindow::check_yaml()
|
||||||
|
{
|
||||||
|
QRegularExpression is_yaml(yaml_regex);
|
||||||
|
QStringList lines = toPlainText().split('\n');
|
||||||
|
for (const auto &line : lines)
|
||||||
|
if (is_yaml.match(line).hasMatch()) return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
void LogWindow::extract_yaml()
|
void LogWindow::extract_yaml()
|
||||||
{
|
{
|
||||||
|
// ignore if no YAML format lines in buffer
|
||||||
|
if (!check_yaml()) return;
|
||||||
|
|
||||||
QString defaultname = filename + ".yaml";
|
QString defaultname = filename + ".yaml";
|
||||||
if (filename.isEmpty()) defaultname = "lammps.yaml";
|
if (filename.isEmpty()) defaultname = "lammps.yaml";
|
||||||
QString yamlFileName = QFileDialog::getSaveFileName(this, "Save YAML data to File", defaultname,
|
QString yamlFileName = QFileDialog::getSaveFileName(this, "Save YAML data to File", defaultname,
|
||||||
"YAML files (*.yaml *.yml)");
|
"YAML files (*.yaml *.yml)");
|
||||||
|
// cannot save without filename
|
||||||
if (yamlFileName.isEmpty()) return;
|
if (yamlFileName.isEmpty()) return;
|
||||||
|
|
||||||
QFileInfo path(yamlFileName);
|
QFileInfo path(yamlFileName);
|
||||||
QFile file(path.absoluteFilePath());
|
QFile file(path.absoluteFilePath());
|
||||||
|
|
||||||
if (!file.open(QIODevice::WriteOnly | QFile::Text)) {
|
if (!file.open(QIODevice::WriteOnly | QFile::Text)) {
|
||||||
QMessageBox::warning(this, "Warning", "Cannot save file: " + file.errorString());
|
QMessageBox::warning(this, "Warning", "Cannot save file: " + file.errorString());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
QRegularExpression is_yaml("^(keywords:.*$|data:$|---$|\\.\\.\\.$| - \\[.*\\]$)");
|
QRegularExpression is_yaml(yaml_regex);
|
||||||
QTextStream out(&file);
|
QTextStream out(&file);
|
||||||
QStringList lines = toPlainText().split('\n');
|
QStringList lines = toPlainText().split('\n');
|
||||||
for (const auto &line : lines) {
|
for (const auto &line : lines) {
|
||||||
@ -131,10 +145,13 @@ void LogWindow::contextMenuEvent(QContextMenuEvent *event)
|
|||||||
action->setIcon(QIcon(":/icons/document-save-as.png"));
|
action->setIcon(QIcon(":/icons/document-save-as.png"));
|
||||||
action->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_S));
|
action->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_S));
|
||||||
connect(action, &QAction::triggered, this, &LogWindow::save_as);
|
connect(action, &QAction::triggered, this, &LogWindow::save_as);
|
||||||
action = menu->addAction(QString("&Export YAML Data to File ..."));
|
// only show export-to-yaml entry if there is YAML format content.
|
||||||
action->setIcon(QIcon(":/icons/yaml-file-icon.png"));
|
if (check_yaml()) {
|
||||||
action->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_Y));
|
action = menu->addAction(QString("&Export YAML Data to File ..."));
|
||||||
connect(action, &QAction::triggered, this, &LogWindow::extract_yaml);
|
action->setIcon(QIcon(":/icons/yaml-file-icon.png"));
|
||||||
|
action->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_Y));
|
||||||
|
connect(action, &QAction::triggered, this, &LogWindow::extract_yaml);
|
||||||
|
}
|
||||||
action = menu->addAction("&Close Window", this, &QWidget::close);
|
action = menu->addAction("&Close Window", this, &QWidget::close);
|
||||||
action->setIcon(QIcon(":/icons/window-close.png"));
|
action->setIcon(QIcon(":/icons/window-close.png"));
|
||||||
action->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_W));
|
action->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_W));
|
||||||
|
|||||||
@ -32,9 +32,11 @@ protected:
|
|||||||
void closeEvent(QCloseEvent *event) override;
|
void closeEvent(QCloseEvent *event) override;
|
||||||
void contextMenuEvent(QContextMenuEvent *event) override;
|
void contextMenuEvent(QContextMenuEvent *event) override;
|
||||||
bool eventFilter(QObject *watched, QEvent *event) override;
|
bool eventFilter(QObject *watched, QEvent *event) override;
|
||||||
|
bool check_yaml();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QString filename;
|
QString filename;
|
||||||
|
static const QString yaml_regex;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user