Skip to content

Commit

Permalink
fix loading recent files and colour palettes
Browse files Browse the repository at this point in the history
  • Loading branch information
vsicurella committed Jan 6, 2024
1 parent 04494e2 commit 323ff44
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 113 deletions.
81 changes: 65 additions & 16 deletions Source/LumatoneEditorState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ juce::Array<juce::Identifier> GetLumatoneEditorProperty()
properties.add(LumatoneEditorProperty::InCalibrationMode);
properties.add(LumatoneEditorProperty::FirmwareUpdatePerformed);
properties.add(LumatoneEditorProperty::ColourPalettes);
properties.add(LumatoneEditorProperty::CurrentFile);
properties.add(LumatoneEditorProperty::RecentFiles);
properties.add(LumatoneEditorProperty::UserDocumentsDirectory);
properties.add(LumatoneEditorProperty::UserMappingsDirectory);
Expand Down Expand Up @@ -50,37 +51,53 @@ LumatoneEditorState::LumatoneEditorState(juce::String name, LumatoneFirmwareDriv
#endif

propertiesFile = std::make_shared<juce::PropertiesFile>(options);
DBG(propertiesFile->createXml("LumatoneEditorSettings")->toString());
jassert(propertiesFile != nullptr);

recentFiles = std::make_shared<juce::RecentlyOpenedFilesList>();
recentFiles->restoreFromString(propertiesFile->getValue(LumatoneEditorProperty::RecentFiles));
recentFiles->removeNonExistentFiles();

colourPalettes = std::make_shared<juce::Array<LumatoneEditorColourPalette>>();
}

LumatoneEditorState::LumatoneEditorState(juce::String name, const LumatoneEditorState &stateIn)
: LumatoneApplicationState(name, stateIn)
, appFonts(stateIn.appFonts)
, lookAndFeel(stateIn.lookAndFeel)
, propertiesFile(stateIn.propertiesFile)
, recentFiles(stateIn.recentFiles)
, colourPalettes(stateIn.colourPalettes)
{
}

LumatoneEditorState::~LumatoneEditorState()
{
recentFiles = nullptr;
propertiesFile = nullptr;
lookAndFeel = nullptr;
appFonts = nullptr;
}

juce::var LumatoneEditorState::getProperty(juce::Identifier propertyId) const
{
return propertiesFile->getValue(propertyId.toString());
}

juce::RecentlyOpenedFilesList LumatoneEditorState::getRecentFiles() const
juce::RecentlyOpenedFilesList& LumatoneEditorState::getRecentFiles()
{
return *recentFiles;
}

const juce::Array<LumatoneEditorColourPalette>& LumatoneEditorState::getColourPalettes()
{
juce::RecentlyOpenedFilesList recentFiles;
recentFiles.restoreFromString(propertiesFile->getValue("RecentFiles"));
recentFiles.removeNonExistentFiles();
return recentFiles;
loadColourPalettesFromFile();
return *colourPalettes;
}

juce::File LumatoneEditorState::getUserDocumentsDirectory() const
{
juce::String possibleDirectory = propertiesFile->getValue("UserDocumentsDirectory");
juce::String possibleDirectory = propertiesFile->getValue(LumatoneEditorProperty::UserDocumentsDirectory);
juce::File directory;

if (juce::File::isAbsolutePath(possibleDirectory))
Expand All @@ -101,7 +118,7 @@ juce::File LumatoneEditorState::getUserMappingsDirectory() const
{
juce::File parentFolder = getUserDocumentsDirectory();

juce::String possibleDirectory = propertiesFile->getValue("UserMappingsDirectory");
juce::String possibleDirectory = propertiesFile->getValue(LumatoneEditorProperty::UserMappingsDirectory);
juce::File directory;

if (juce::File::isAbsolutePath(possibleDirectory))
Expand All @@ -122,7 +139,7 @@ juce::File LumatoneEditorState::getUserPalettesDirectory() const
{
juce::File parentFolder = getUserDocumentsDirectory();

juce::String possibleDirectory = propertiesFile->getValue("UserPalettesDirectory");
juce::String possibleDirectory = propertiesFile->getValue(LumatoneEditorProperty::UserPalettesDirectory);
juce::File directory;

if (juce::File::isAbsolutePath(possibleDirectory))
Expand Down Expand Up @@ -160,9 +177,9 @@ void LumatoneEditorState::setDeveloperMode(bool developerModeOn)
state.setPropertyExcludingListener(this, LumatoneEditorProperty::DeveloperModeOn, inDeveloperMode, undoManager);
}

bool LumatoneEditorState::sendSysExToDevice() const
bool LumatoneEditorState::doSendChangesToDevice() const
{
return LumatoneApplicationState::sendSysExToDevice() && editorMode == EditorMode::ONLINE;
return LumatoneApplicationState::doSendChangesToDevice() && editorMode == EditorMode::ONLINE;
}

juce::ValueTree LumatoneEditorState::loadStateProperties(juce::ValueTree stateIn)
Expand Down Expand Up @@ -206,10 +223,13 @@ void LumatoneEditorState::handleStatePropertyChange(juce::ValueTree stateIn, con
else if (property == LumatoneEditorProperty::ColourPalettes)
{

}
else if (property == LumatoneEditorProperty::CurrentFile)
{
currentFile = juce::File(stateIn[property]);
}
else if (property == LumatoneEditorProperty::RecentFiles)
{

}
else if (property == LumatoneEditorProperty::UserDocumentsDirectory)
{
Expand Down Expand Up @@ -239,12 +259,31 @@ void LumatoneEditorState::handleStatePropertyChange(juce::ValueTree stateIn, con

void LumatoneEditorState::setColourPalettes(const juce::Array<LumatoneEditorColourPalette> &palettesIn)
{
colourPalettes = palettesIn;
*colourPalettes = palettesIn;
}

void LumatoneEditorState::loadColourPalettesFromFile()
{
auto directory = getUserPalettesDirectory();
auto foundPaletteFiles = directory.findChildFiles(juce::File::TypesOfFileToFind::findFiles, true, '*' + juce::String(PALETTEFILEEXTENSION));

juce::Array<LumatoneEditorColourPalette> newPalettes;

auto paletteSorter = LumatoneEditorPaletteSorter();
for (auto file : foundPaletteFiles)
{
LumatoneEditorColourPalette palette = LumatoneEditorColourPalette::loadFromFile(file);
newPalettes.addSorted(paletteSorter, palette);
}

setColourPalettes(newPalettes);
}

void LumatoneEditorState::addPalette(const LumatoneEditorColourPalette &newPalette)
{
colourPalettes.add(newPalette);
colourPalettes->add(newPalette);
// TODO
state.setPropertyExcludingListener(this, LumatoneEditorProperty::ColourPalettes, "", nullptr);
}

bool LumatoneEditorState::deletePaletteFile(juce::File pathToPalette)
Expand All @@ -260,13 +299,14 @@ bool LumatoneEditorState::deletePaletteFile(juce::File pathToPalette)
}

// Open a SysEx mapping from the file specified in currentFile
bool LumatoneEditorState::openFromCurrentFile()
bool LumatoneEditorState::resetToCurrentFile()
{
if (getCurrentFile().getFullPathName().isEmpty())
{
// Replace with blank file
LumatoneLayout defaultLayout;
setCompleteConfig(defaultLayout);
setHasChangesToSave(false);
return true;
}

Expand Down Expand Up @@ -294,7 +334,7 @@ bool LumatoneEditorState::openFromCurrentFile()
// undoManager.clearUndoHistory();

// Add file to recent files list
recentFiles.addFile(currentFile);
recentFiles->addFile(currentFile);

return true;
}
Expand All @@ -309,9 +349,18 @@ bool LumatoneEditorState::openFromCurrentFile()
bool LumatoneEditorState::setCurrentFile(File fileToOpen)
{
currentFile = fileToOpen;
return openFromCurrentFile();
state.setPropertyExcludingListener(this, LumatoneEditorProperty::CurrentFile, currentFile.getFullPathName(), nullptr);
return resetToCurrentFile();
}

// open a file from the "recent files" menu
bool LumatoneEditorState::openRecentFile(int recentFileIndex)
{
jassert(recentFileIndex >= 0 && recentFileIndex < recentFiles->getNumFiles());
return setCurrentFile(recentFiles->getFile(recentFileIndex));
}


void LumatoneEditorState::setEditMode(EditorMode editMode)
{
editorMode = editMode;
Expand Down
30 changes: 20 additions & 10 deletions Source/LumatoneEditorState.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ namespace LumatoneEditorProperty
static const juce::Identifier FirmwareUpdatePerformed = juce::Identifier("FirmwareUpdatePerformed");

static const juce::Identifier ColourPalettes = juce::Identifier("ColourPalettes");


static const juce::Identifier CurrentFile = juce::Identifier("CurrentFile");
static const juce::Identifier RecentFiles = juce::Identifier("RecentFiles");

static const juce::Identifier AutoConnectDevice = juce::Identifier("AutoConnectDevice");
Expand Down Expand Up @@ -79,7 +80,8 @@ class LumatoneEditorState : public LumatoneApplicationState

LumatoneEditorLookAndFeel& getEditorLookAndFeel() { return *lookAndFeel; }

const juce::Array<LumatoneEditorColourPalette>& getColourPalettes() { return colourPalettes; }
//const juce::Array<LumatoneEditorColourPalette>& getColourPalettes() { return colourPalettes; }
const juce::Array<LumatoneEditorColourPalette>& getColourPalettes();

const LumatoneEditorFontLibrary& getAppFonts() const { return *appFonts; }

Expand All @@ -88,7 +90,8 @@ class LumatoneEditorState : public LumatoneApplicationState
// juce::PropertiesFile& getPropertiesFile() { return *propertiesFile; }
juce::File getCurrentFile() const { return currentFile; }

juce::RecentlyOpenedFilesList getRecentFiles() const;
juce::RecentlyOpenedFilesList& getRecentFiles();

juce::File getUserDocumentsDirectory() const;
juce::File getUserMappingsDirectory() const;
juce::File getUserPalettesDirectory() const;
Expand All @@ -97,20 +100,27 @@ class LumatoneEditorState : public LumatoneApplicationState
// Font getAppFont(LumatoneEditorFont fontIdIn, float height = 12.0f) { return appFonts.getFont(fontIdIn, height); }

public:

bool sendSysExToDevice() const override;
bool doSendChangesToDevice() const override;

protected:
juce::ValueTree loadStateProperties(juce::ValueTree stateIn) override;
void handleStatePropertyChange(juce::ValueTree stateIn, const juce::Identifier& property) override;

bool resetToCurrentFile();
bool openRecentFile(int recentFileIndex);

void addPalette(const LumatoneEditorColourPalette& newPalette);
bool deletePaletteFile(juce::File pathToPalette);

public:



// Only main app should access these
private:
void setColourPalettes(const juce::Array<LumatoneEditorColourPalette>& palettesIn);
void addPalette(const LumatoneEditorColourPalette& newPalette);
bool deletePaletteFile(juce::File pathToPalette);
void loadColourPalettesFromFile();

bool openFromCurrentFile();
bool setCurrentFile(juce::File fileToOpen);

void setEditMode(EditorMode editMode);
Expand All @@ -130,10 +140,10 @@ class LumatoneEditorState : public LumatoneApplicationState

std::shared_ptr<LumatoneEditorFontLibrary> appFonts;
std::shared_ptr<LumatoneEditorLookAndFeel> lookAndFeel;
juce::Array<LumatoneEditorColourPalette> colourPalettes;
std::shared_ptr<juce::Array<LumatoneEditorColourPalette>> colourPalettes;

juce::File currentFile;
juce::RecentlyOpenedFilesList recentFiles;
std::shared_ptr<juce::RecentlyOpenedFilesList> recentFiles;

std::shared_ptr<juce::PropertiesFile> propertiesFile;

Expand Down
1 change: 1 addition & 0 deletions Source/LumatoneMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ namespace Lumatone {
{
// open a file from the "recent files" menu
// TerpstraSysExApplication::getApp().openRecentFile(menuItemID - recentFilesBaseID);
openRecentFile(menuItemID - recentFilesBaseID);
}
}

Expand Down
Loading

0 comments on commit 323ff44

Please sign in to comment.