Skip to content

Commit

Permalink
Remember folder used for savings or opening files most recently
Browse files Browse the repository at this point in the history
When you open or save files like simulation INI files or image files
with HaloRay, the folder is remembered. The next time you open the file
dialog, it opens the same folder, unless it has stopped existing.
  • Loading branch information
naavis committed Jul 23, 2022
1 parent e360228 commit 127478f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ and this project adheres to
Color to a polynomial fitted to data provided by Warren and Brandt in their
paper Optical Constants of Ice from The Ultraviolet to The Microwave, which
is what HaloPoint 2.0 used to use
- HaloRay now remembers which folder you opened last when loading or saving
files like simulation parameter files or images, and opens the file dialog
picker in that same folder the next time

## 4.0.1 - 2021-08-26

Expand Down
38 changes: 31 additions & 7 deletions src/haloray-core/gui/mainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,39 +98,43 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), m_previousTimedIt
auto defaultFilename = QString("haloray_%1.png")
.arg(currentTime)
.replace(":", "-");
auto defaultFilePath = getLatestAccessedFolder().absoluteFilePath(defaultFilename);
QString filename = QFileDialog::getSaveFileName(this,
tr("Save File"),
defaultFilename,
defaultFilePath,
tr("Images (*.png)"));

if (!filename.isNull())
{
image.save(filename, "PNG", 50);
}
if (filename.isNull()) return;

image.save(filename, "PNG", 50);
updateLatestAccessedFolder(filename);
});
connect(m_saveSimulationAction, &QAction::triggered, [this]() {
auto currentTime = QDateTime::currentDateTimeUtc().toString(Qt::DateFormat::ISODate);
auto defaultFilename = QString("haloray_sim_%1.ini")
.arg(currentTime)
.replace(":", "-");
auto defaultFilePath = getLatestAccessedFolder().absoluteFilePath(defaultFilename);
QString filename = QFileDialog::getSaveFileName(this,
tr("Save File"),
defaultFilename,
defaultFilePath,
tr("Simulation files (*.ini)"));

if (filename.isNull()) return;

StateSaver::SaveState(filename, m_engine, m_crystalRepository.get());
updateLatestAccessedFolder(filename);
});
connect(m_loadSimulationAction, &QAction::triggered, [this]() {
QString filename = QFileDialog::getOpenFileName(this,
tr("Open file"),
QString(),
getLatestAccessedFolder().absolutePath(),
tr("Simulation files (*.ini)"));

if (filename.isNull()) return;

StateSaver::LoadState(filename, m_simulationStateModel, m_crystalModel);
updateLatestAccessedFolder(filename);
});
connect(m_openCrystalPreviewWindow, &QAction::triggered, [this]() {
auto previewWindow = new CrystalPreviewWindow(m_crystalModel, m_crystalSettingsWidget->getCurrentPopulationIndex(), this);
Expand Down Expand Up @@ -273,4 +277,24 @@ void HaloRay::MainWindow::restartSimulation()
m_openGLWidget->update();
}

QDir MainWindow::getLatestAccessedFolder() const
{
QSettings settings;
QString pathString = settings.value("filedialog/latestFolder", QString()).toString();
QDir dir(pathString);
if (dir.exists()) {
return dir;
}

return QDir();
}

void MainWindow::updateLatestAccessedFolder(QString fileOrDirPath)
{
QFileInfo fileInfo(fileOrDirPath);
QString path = fileInfo.absoluteDir().path();
QSettings settings;
settings.setValue("filedialog/latestFolder", path);
}

}
5 changes: 4 additions & 1 deletion src/haloray-core/gui/mainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
#include <QTimer>
#include "gui/models/simulationStateModel.h"


class QDoubleSpinBox;
class QProgressBar;
class QScrollArea;
class QAction;
class QDir;

namespace HaloRay
{
Expand Down Expand Up @@ -39,6 +39,9 @@ class MainWindow : public QMainWindow
void setupRenderTimer();
void restartSimulation();

QDir getLatestAccessedFolder() const;
void updateLatestAccessedFolder(QString fileOrDirPath);

GeneralSettingsWidget *m_generalSettingsWidget;
CrystalSettingsWidget *m_crystalSettingsWidget;
ViewSettingsWidget *m_viewSettingsWidget;
Expand Down

0 comments on commit 127478f

Please sign in to comment.