diff options
author | Pavel Pisa <pisa@cmp.felk.cvut.cz> | 2019-07-03 10:16:25 +0200 |
---|---|---|
committer | Pavel Pisa <pisa@cmp.felk.cvut.cz> | 2019-07-03 10:16:25 +0200 |
commit | 9cef3efa2251833883e0f1fcf47138776e2a45aa (patch) | |
tree | 6ab8bb8eeb3163cc5bdf138d953ee5841187aea2 /qtmips_gui | |
parent | 9f4c221eb9767adec56f57c2479be5fa38049397 (diff) | |
download | qtmips-9cef3efa2251833883e0f1fcf47138776e2a45aa.tar.gz qtmips-9cef3efa2251833883e0f1fcf47138776e2a45aa.tar.bz2 qtmips-9cef3efa2251833883e0f1fcf47138776e2a45aa.zip |
Implement file save for emscripten build.
Signed-off-by: Pavel Pisa <pisa@cmp.felk.cvut.cz>
Diffstat (limited to 'qtmips_gui')
-rw-r--r-- | qtmips_gui/mainwindow.cpp | 17 | ||||
-rw-r--r-- | qtmips_gui/srceditor.cpp | 21 | ||||
-rw-r--r-- | qtmips_gui/srceditor.h | 1 |
3 files changed, 28 insertions, 11 deletions
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(); |