Set savefile if loading external file
MODIFY
src/edittab.cpp
src/edittab.cpp
@@ -131,29 +131,30 @@ void EditTab::showSymbolNavigator() {
}
}
-void EditTab::loadExternalFile(const LoadFileParams& params) {
+bool EditTab::loadExternalFile(const LoadFileParams& params) {
if (params.type == SourceType::C) {
// Try to enable C input and verify that source type was changed successfully. This allows us to trigger the
// message box associated with C input, if no compiler is set. If so, load the file.
enableEditor();
m_ui->setCInput->setChecked(true);
if (m_currentSourceType == SourceType::C) {
- loadFile(params);
+ return loadFile(params);
}
} else if (params.type == SourceType::Assembly) {
m_ui->setAssemblyInput->setChecked(true);
- loadFile(params);
+ return loadFile(params);
} else {
m_currentSourceType = params.type;
- loadFile(params);
+ return loadFile(params);
}
+ return false;
}
-void EditTab::loadFile(const LoadFileParams& fileParams) {
+bool EditTab::loadFile(const LoadFileParams& fileParams) {
QFile file(fileParams.filepath);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QMessageBox::warning(this, "Error", "Error: Could not open file " + fileParams.filepath);
- return;
+ return false;
}
bool success = true;
@@ -183,6 +184,7 @@ void EditTab::loadFile(const LoadFileParams& fileParams) {
QMessageBox::warning(this, "Error", "Error: Could not load file " + fileParams.filepath);
}
file.close();
+ return success;
}
QString EditTab::getAssemblyText() {

MODIFY
src/edittab.h
src/edittab.h
@@ -34,15 +34,14 @@ public:
const QByteArray* getBinaryData();
- void loadFile(const LoadFileParams&);
+ /// Loads a file into the editor. Returns true if the file was successfully loaded.
+ bool loadFile(const LoadFileParams&);
void loadSourceText(const QString& text);
Assembler::Errors* errors();
- /**
- * @brief loadExternalFile
- * sets the current source type to whatever is specified by @p params and calls loadFile(@p params);
- */
- void loadExternalFile(const LoadFileParams& params);
+ /// sets the current source type to whatever is specified by @p params and calls loadFile(@p params). Returns true
+ /// if the file loaded successfully.
+ bool loadExternalFile(const LoadFileParams& params);
signals:
void programChanged(const std::shared_ptr<Program>& program);

MODIFY
src/mainwindow.cpp
src/mainwindow.cpp
namespace Ripes {
+static void clearSaveFile() {
+ RipesSettings::setValue(RIPES_SETTING_HAS_SAVEFILE, false);
+ RipesSettings::setValue(RIPES_SETTING_SAVEPATH, "");
+}
+
MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), m_ui(new Ui::MainWindow) {
m_ui->setupUi(this);
setWindowTitle("Ripes");
@@ -95,7 +100,7 @@ MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), m_ui(new Ui::Main
setupMenus();
// setup and connect widgets
- connect(editTab, &EditTab::editorStateChanged, [=] { RipesSettings::setValue(RIPES_SETTING_HAS_SAVEFILE, false); });
+ connect(editTab, &EditTab::editorStateChanged, [=] { clearSaveFile(); });
// Setup status bar
setupStatusBar();
@@ -231,7 +236,7 @@ void MainWindow::setupExamplesMenu(QMenu* parent) {
parms.filepath = QString(":/examples/assembly/") + fileName;
parms.type = SourceType::Assembly;
static_cast<EditTab*>(m_tabWidgets.at(EditTabID).tab)->loadExternalFile(parms);
- RipesSettings::setValue(RIPES_SETTING_HAS_SAVEFILE, false);
+ clearSaveFile();
});
}
}
@@ -245,7 +250,7 @@ void MainWindow::setupExamplesMenu(QMenu* parent) {
parms.filepath = QString(":/examples/C/") + fileName;
parms.type = SourceType::C;
static_cast<EditTab*>(m_tabWidgets.at(EditTabID).tab)->loadExternalFile(parms);
- RipesSettings::setValue(RIPES_SETTING_HAS_SAVEFILE, false);
+ clearSaveFile();
});
}
}
@@ -267,7 +272,7 @@ void MainWindow::setupExamplesMenu(QMenu* parent) {
parms.filepath = tmpELFFile->fileName();
parms.type = SourceType::ExternalELF;
static_cast<EditTab*>(m_tabWidgets.at(EditTabID).tab)->loadExternalFile(parms);
- RipesSettings::setValue(RIPES_SETTING_HAS_SAVEFILE, false);
+ clearSaveFile();
tmpELFFile->remove();
});
}
@@ -303,8 +308,13 @@ void MainWindow::loadFileTriggered() {
if (!diag.exec())
return;
- static_cast<EditTab*>(m_tabWidgets.at(EditTabID).tab)->loadExternalFile(diag.getParams());
- RipesSettings::setValue(RIPES_SETTING_HAS_SAVEFILE, false);
+ if (static_cast<EditTab*>(m_tabWidgets.at(EditTabID).tab)->loadExternalFile(diag.getParams())) {
+ // Set the current save path to the loaded file. Only store the file basename without extension.
+ QFileInfo info = QFileInfo(diag.getParams().filepath);
+ auto savePath = info.dir().path() + QDir::separator() + info.baseName();
+ RipesSettings::setValue(RIPES_SETTING_SAVEPATH, savePath);
+ RipesSettings::setValue(RIPES_SETTING_HAS_SAVEFILE, true);
+ }
}
void MainWindow::wiki() {
@@ -405,7 +415,7 @@ void MainWindow::newProgramTriggered() {
}
}
}
- RipesSettings::setValue(RIPES_SETTING_HAS_SAVEFILE, false);
+ clearSaveFile();
static_cast<EditTab*>(m_tabWidgets.at(EditTabID).tab)->newProgram();
}

Set savefile if loading external file