From 3b97fecd9419a39b2cdb1085ed4a818e06b729c1 Mon Sep 17 00:00:00 2001 From: mar77i Date: Wed, 24 Apr 2024 21:18:12 +0200 Subject: [PATCH] consolidate file saving and loading --- bigintwidget.cpp | 81 ++++++++++++++++++++++++++++++++++++++++++------ mandel.cpp | 24 +++----------- mandel.h | 4 +-- 3 files changed, 78 insertions(+), 31 deletions(-) diff --git a/bigintwidget.cpp b/bigintwidget.cpp index 916c141..17e2138 100644 --- a/bigintwidget.cpp +++ b/bigintwidget.cpp @@ -175,9 +175,41 @@ void BigintWidget::update_img() { update(); } +static inline QString get_save_file_name( + QWidget *parent = nullptr, + const QString &caption = QString(), + const QString &dir = QString(), + const QString &filter_title = QString(), + const QStringList &ext_list = QStringList{}, + const QString default_ext = QString() +) { + QTextStream ss(new QString()); + QStringList::const_iterator ext_list_iter; + QString file_name; + qsizetype pos; + ss << filter_title << " (" << ext_list.join(" ") << ")"; + file_name = QFileDialog::getSaveFileName( + parent, caption, dir, *ss.string() + ); + if (file_name.isEmpty()) + return file_name; + ss.seek(0); + ss.string()->clear(); + ss << file_name; + pos = file_name.lastIndexOf("."); + if (pos < 0 || !ext_list.contains(file_name.mid(pos))) + ss << default_ext; + return *ss.string(); +} + void BigintWidget::export_img() { - QString file_name = QFileDialog::getSaveFileName( - this, "Save image", "", "Images (*.png *.xpm *.jpg)" + QString file_name = get_save_file_name( + this, + "Save image", + "", + "Images", + QStringList{".png", ".xpm", ".jpg"}, + ".png" ); if (!file_name.isEmpty()) settings.save_img(file_name); @@ -203,19 +235,48 @@ void BigintWidget::settings_widget_accepted() { } void BigintWidget::load_data() { + QFile file; + QJsonObject json; + QByteArray qba; QString file_name = QFileDialog::getOpenFileName( - this, "Load Data", "", "BigintMandel Data (*.bim)" + this, "Load Data", "", "BigintMandel Data (*.json *.json.qcompress)" ); - if (!file_name.isEmpty()) { - settings.load_data(file_name); - update_img(); + if (file_name.isEmpty()) + return; + file.setFileName(file_name); + if (!file.open(QIODevice::ReadOnly)) { + qWarning("Couldn't open save file."); + return; } + qba = file.readAll(); + file.close(); + if (file_name.endsWith(".qcompress")) + qba = qUncompress(qba); + settings.from_json(QJsonDocument::fromJson(qba).object()); + update_img(); } void BigintWidget::save_data() { - QString file_name = QFileDialog::getSaveFileName( - this, "Save Data", "", "BigintMandel Data (*.bim)" + QFile file; + QByteArray qba; + QString file_name = get_save_file_name( + this, + "Save data", + "", + "Bigintmandel Data", + QStringList{".json", ".json.qcompress"}, + ".json.qcompress" ); - if (!file_name.isEmpty()) - settings.save_data(file_name); + if (file_name.isEmpty()) + return; + qba = QJsonDocument(settings.to_json()).toJson(); + if (file_name.endsWith(".qcompress")) + qba = qCompress(qba, 9); + file.setFileName(file_name); + if (!file.open(QIODevice::WriteOnly)) { + qWarning("Couldn't open save file."); + return; + } + file.write(qba); + file.close(); } diff --git a/mandel.cpp b/mandel.cpp index cdeae91..b6b11d8 100644 --- a/mandel.cpp +++ b/mandel.cpp @@ -138,40 +138,26 @@ void MandelSettings::reset(size_t max_iter, const QSize size) { setup_cells(cells, ¶ms); } -void MandelSettings::load_data(QString file_name) { - QFile file(file_name); - QJsonObject json; +void MandelSettings::from_json(const QJsonObject &json) { QJsonArray json_array; int i; - if (!file.open(QIODevice::ReadOnly)) { - qWarning("Couldn't open save file."); - return; - } - json = QJsonDocument::fromJson(qUncompress(file.readAll())).object(); params = MandelParams::from_json(json["params"].toObject()); json_array = json["cells"].toArray(); cells.resize(json_array.size(), MandelCell(¶ms)); img = setup_image(img, params.get_size()); for (i = 0; i < json_array.size(); i++) finished_cell(i, cells[i].from_json(json_array[i].toObject())); - file.close(); } -void MandelSettings::save_data(QString file_name) { - QVector::iterator i; - QFile file(file_name); - QJsonObject json; +QJsonObject MandelSettings::to_json() { QJsonArray c; + QJsonObject json; + QVector::iterator i; json["params"] = params.to_json(); for (i = cells.begin(); i != cells.end(); i++) c.append(i->to_json()); json["cells"] = c; - if (!file.open(QIODevice::WriteOnly)) { - qWarning("Couldn't open save file."); - return; - } - file.write(qCompress(QJsonDocument(json).toJson(), 9)); - file.close(); + return json; } MandelCell::MandelCell(const MandelParams *params) : params(params) {} diff --git a/mandel.h b/mandel.h index 8910f97..27880e7 100644 --- a/mandel.h +++ b/mandel.h @@ -74,8 +74,8 @@ public: void finished_cell(int num, const MandelResultCell &cell); void save_img(QString file_name); void reset(size_t max_iter, QSize size); - void load_data(QString file_name); - void save_data(QString file_name); + void from_json(const QJsonObject &json); + QJsonObject to_json(); }; extern QVector colors; -- 2.47.0