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);
}
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();
}
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<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) {}