Add continuous save for assembly editor
This commit adds Save/Save as... actions for saving the contents of
the assembly editor. This allows for more intuitive control over when
(and what) is saved, without encountering repeated save boxes on
subsequent saves to the same file.
MODIFY
src/mainwindow.cpp
src/mainwindow.cpp
@@ -66,8 +66,10 @@ void MainWindow::setupExamples() {
auto* binaryExampleMenu = new QMenu();
binaryExampleMenu->setTitle("Binary");
for (const auto& fileName : binaryExamples) {
- binaryExampleMenu->addAction(fileName,
- [=] { this->loadBinaryFile(QString(":/examples/binary/") + fileName); });
+ binaryExampleMenu->addAction(fileName, [=] {
+ this->loadBinaryFile(QString(":/examples/binary/") + fileName);
+ m_currentFile = QString();
+ });
}
// Add binary example menu to example menu
m_ui->menuExamples->addMenu(binaryExampleMenu);
@@ -77,8 +79,10 @@ void MainWindow::setupExamples() {
auto* assemblyExampleMenu = new QMenu();
assemblyExampleMenu->setTitle("Assembly");
for (const auto& fileName : assemblyExamples) {
- assemblyExampleMenu->addAction(fileName,
- [=] { this->loadAssemblyFile(QString(":/examples/assembly/") + fileName); });
+ assemblyExampleMenu->addAction(fileName, [=] {
+ this->loadAssemblyFile(QString(":/examples/assembly/") + fileName);
+ m_currentFile = QString();
+ });
}
// Add binary example menu to example menu
m_ui->menuExamples->addMenu(assemblyExampleMenu);
@@ -122,6 +126,7 @@ void MainWindow::loadAssemblyFile(QString fileName) {
m_ui->programfiletab->setAssemblyText(file.readAll());
file.close();
}
+ m_currentFile = fileName;
m_ui->programfiletab->setDisassemblerText();
}
@@ -135,18 +140,24 @@ void MainWindow::on_actionOpen_wiki_triggered() {
}
void MainWindow::on_actionSave_Assembly_File_triggered() {
- QFileDialog dialog;
- dialog.setNameFilter("*.as *.s");
- dialog.setAcceptMode(QFileDialog::AcceptSave);
- if (dialog.exec()) {
- auto files = dialog.selectedFiles();
- if (files.length() == 1) {
- QFile file(files[0]);
- if (file.open(QIODevice::ReadWrite)) {
+ if (m_currentFile.isEmpty()) {
+ on_actionSave_Assembly_File_As_triggered();
+ }
+ QFile file(m_currentFile);
+ if (file.open(QIODevice::WriteOnly)) {
QTextStream stream(&file);
stream << m_ui->programfiletab->getAssemblyText();
file.close();
}
}
+
+void MainWindow::on_actionSave_Assembly_File_As_triggered() {
+ QFileDialog dialog;
+ dialog.setNameFilter("*.as *.s");
+ dialog.setAcceptMode(QFileDialog::AcceptSave);
+ dialog.setDefaultSuffix(".s");
+ if (dialog.exec()) {
+ m_currentFile = dialog.selectedFiles()[0];
+ on_actionSave_Assembly_File_triggered();
}
}

MODIFY
src/mainwindow.h
src/mainwindow.h
@@ -30,6 +30,8 @@ private slots:
void on_actionSave_Assembly_File_triggered();
+ void on_actionSave_Assembly_File_As_triggered();
+
signals:
void update();
void updateMemoryTab();
@@ -37,6 +39,7 @@ signals:
private:
Ui::MainWindow* m_ui;
void setupExamples();
+ QString m_currentFile = QString();
};
#endif // MAINWINDOW_H

MODIFY
src/mainwindow.ui
src/mainwindow.ui
<addaction name="menuExamples"/>
<addaction name="separator"/>
<addaction name="actionSave_Assembly_File"/>
+ <addaction name="actionSave_Assembly_File_As"/>
<addaction name="separator"/>
<addaction name="actionexit"/>
</widget>
<string>Ctrl+S</string>
</property>
</action>
+ <action name="actionSave_Assembly_File_As">
+ <property name="icon">
+ <iconset resource="../resources/images.qrc">
+ <normaloff>:/icons/save.svg</normaloff>:/icons/save.svg</iconset>
+ </property>
+ <property name="text">
+ <string>Save Assembly File As...</string>
+ </property>
+ <property name="shortcut">
+ <string>Ctrl+Shift+S</string>
+ </property>
+ </action>
</widget>
<customwidgets>
<customwidget>

Add continuous save for assembly editor #25

This commit adds Save/Save as... actions for saving the contents of
the assembly editor. This allows for more intuitive control over when
(and what) is saved, without encountering repeated save boxes on
subsequent saves to the same file.