From 9cef3efa2251833883e0f1fcf47138776e2a45aa Mon Sep 17 00:00:00 2001 From: Pavel Pisa Date: Wed, 3 Jul 2019 10:16:25 +0200 Subject: Implement file save for emscripten build. Signed-off-by: Pavel Pisa --- qtmips_gui/mainwindow.cpp | 17 +++++++++++++++++ qtmips_gui/srceditor.cpp | 21 ++++++++++----------- qtmips_gui/srceditor.h | 1 + 3 files changed, 28 insertions(+), 11 deletions(-) (limited to 'qtmips_gui') diff --git a/qtmips_gui/mainwindow.cpp b/qtmips_gui/mainwindow.cpp index d0338cc..f300b5e 100644 --- a/qtmips_gui/mainwindow.cpp +++ b/qtmips_gui/mainwindow.cpp @@ -555,6 +555,7 @@ void MainWindow::open_source() { void MainWindow::save_source_as() { if (current_srceditor == nullptr) return; +#ifndef __EMSCRIPTEN__ QFileDialog fileDialog(this, tr("Save as...")); fileDialog.setAcceptMode(QFileDialog::AcceptSave); fileDialog.setDefaultSuffix("s"); @@ -568,6 +569,17 @@ void MainWindow::save_source_as() { } else { QMessageBox::critical(this, "QtMips Error", tr("Cannot save file '%1'.").arg(fn)); } +#else + bool ok; + QString text = QInputDialog::getText(this, tr("Select file name"), + tr("File name:"), QLineEdit::Normal, + "unknow.s", &ok); + if (ok && !text.isEmpty()) { + current_srceditor->setFileName(text); + if (!current_srceditor->filename().isEmpty()) + save_source(); + } +#endif update_open_file_list(); } @@ -576,9 +588,14 @@ void MainWindow::save_source() { return; if (current_srceditor->filename().isEmpty()) return save_source_as(); +#ifndef __EMSCRIPTEN__ if (!current_srceditor->saveFile()) { QMessageBox::critical(this, "QtMips Error", tr("Cannot save file '%1'.").arg(current_srceditor->filename())); } +#else + QHtml5File::save(current_srceditor->document()->toPlainText().toUtf8(), + current_srceditor->filename()); +#endif } void MainWindow::close_source() { diff --git a/qtmips_gui/srceditor.cpp b/qtmips_gui/srceditor.cpp index bb6da16..55f372a 100644 --- a/qtmips_gui/srceditor.cpp +++ b/qtmips_gui/srceditor.cpp @@ -72,13 +72,18 @@ QString SrcEditor::title() { return tname; } +void SrcEditor::setFileName(QString filename) { + QFileInfo fi(filename); + fname = filename; + tname = fi.fileName(); +} + + bool SrcEditor::loadFile(QString filename) { QFile file(filename); - QFileInfo fi(filename); if (file.open(QFile::ReadOnly | QFile::Text)) { setPlainText(file.readAll()); - fname = filename; - tname = fi.fileName(); + setFileName(filename); return true; } else { return false; @@ -88,9 +93,7 @@ bool SrcEditor::loadFile(QString filename) { bool SrcEditor::loadByteArray(const QByteArray &content, QString filename) { setPlainText(QString::fromUtf8(content.data(), content.size())); if (!filename.isEmpty()) { - QFileInfo fi(filename); - fname = filename; - tname = fi.fileName(); + setFileName(filename); } return true; } @@ -100,14 +103,10 @@ bool SrcEditor::saveFile(QString filename) { filename = this->filename(); if (filename.isEmpty()) return false; - QFileInfo fi(filename); QTextDocumentWriter writer(filename); writer.setFormat("plaintext"); bool success = writer.write(document()); - if (success) { - fname = filename; - tname = fi.fileName(); - } + setFileName(filename); return success; } diff --git a/qtmips_gui/srceditor.h b/qtmips_gui/srceditor.h index 1c2e677..1fdbb98 100644 --- a/qtmips_gui/srceditor.h +++ b/qtmips_gui/srceditor.h @@ -54,6 +54,7 @@ public: bool saveFile(QString filename = ""); bool loadByteArray(const QByteArray &content, QString filename = ""); void setCursorToLine(int ln); + void setFileName(QString filename); private: Highlighter *highlighter; void setup_common(); -- cgit v1.2.3