]> git.mar77i.info Git - bigintmandel/commitdiff
consolidate file saving and loading
authormar77i <mar77i@protonmail.ch>
Wed, 24 Apr 2024 19:18:12 +0000 (21:18 +0200)
committermar77i <mar77i@protonmail.ch>
Wed, 24 Apr 2024 19:18:12 +0000 (21:18 +0200)
bigintwidget.cpp
mandel.cpp
mandel.h

index 916c141f90e5866a4be4748f442559942d3de738..17e213812baef49a6018b4505cd3e27d513d1d01 100644 (file)
@@ -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();
 }
index cdeae911f3bc69b6694b4a7af91c782e7f80fbb2..b6b11d8f777887e297c505a514fe0751cef35efa 100644 (file)
@@ -138,40 +138,26 @@ void MandelSettings::reset(size_t max_iter, const QSize size) {
     setup_cells(cells, &params);
 }
 
-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(&params));
     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<MandelCell>::iterator i;
-    QFile file(file_name);
-    QJsonObject json;
+QJsonObject MandelSettings::to_json() {
     QJsonArray c;
+    QJsonObject json;
+    QVector<MandelCell>::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) {}
index 8910f978901445489cba6bab6d668867aa80e690..27880e773384aadc74ddf0a46cac63a5c5809f10 100644 (file)
--- 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<QColor> colors;