Skip to content

Commit

Permalink
Refinements to FileDialog class
Browse files Browse the repository at this point in the history
- Move FileDialog flags into class scope.
- Remove unsupported FileDialog flags for clarity.
- Qualify filenames for header includes.
- Minor code style updates.
  • Loading branch information
jstone-lucasfilm committed Apr 5, 2023
1 parent e5e7d6a commit daea424
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 34 deletions.
25 changes: 7 additions & 18 deletions source/MaterialXGraphEditor/FileDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// SPDX-License-Identifier: Apache-2.0
//

#include "FileDialog.h"
#include <MaterialXGraphEditor/FileDialog.h>

#include <MaterialXCore/Exception.h>

Expand All @@ -27,8 +27,8 @@ void FileDialog::setTypeFilters(const mx::StringVec& typeFilters)

for (auto typefilter : typeFilters)
{
std::string minus_ext = typefilter.substr(1, typefilter.size() - 1);
std::pair<std::string, std::string> filterPair = { minus_ext, minus_ext };
std::string minusExt = typefilter.substr(1, typefilter.size() - 1);
std::pair<std::string, std::string> filterPair = { minusExt, minusExt };
_filetypes.push_back(filterPair);
}
}
Expand All @@ -51,12 +51,7 @@ bool FileDialog::hasSelected()

mx::FilePath FileDialog::getSelected()
{
if (_selectedFilenames.empty())
{
return {};
}

return *_selectedFilenames.begin();
return _selectedFilenames.empty() ? mx::FilePath() : _selectedFilenames[0];
}

void FileDialog::clearSelected()
Expand All @@ -74,10 +69,10 @@ void FileDialog::display()
_openFlag = false;

// Check if we want to save or open
bool save = !(_flags & FileDialogFlags_SelectDirectory) &&
(_flags & FileDialogFlags_EnterNewFilename);
bool save = !(_flags & SelectDirectory) && (_flags & EnterNewFilename);

std::string path = launchFileDialog(_filetypes, save);
mx::StringVec result = launchFileDialog(_filetypes, save, false);
std::string path = result.empty() ? "" : result.front();
if (!path.empty())
{
_selectedFilenames.push_back(path);
Expand All @@ -86,12 +81,6 @@ void FileDialog::display()
_isOpened = false;
}

std::string launchFileDialog(const std::vector<std::pair<std::string, std::string>>& filetypes, bool save)
{
mx::StringVec result = launchFileDialog(filetypes, save, false);
return result.empty() ? "" : result.front();
}

#if !defined(__APPLE__)
mx::StringVec launchFileDialog(const std::vector<std::pair<std::string, std::string>>& filetypes, bool save, bool multiple)
{
Expand Down
23 changes: 9 additions & 14 deletions source/MaterialXGraphEditor/FileDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,17 @@

namespace mx = MaterialX;

enum FileDialogFlags
{
FileDialogFlags_SelectDirectory = 1 << 0, // select directory instead of regular file
FileDialogFlags_EnterNewFilename = 1 << 1, // allow user to enter new filename when selecting regular file
FileDialogFlags_NoModal = 1 << 2, // file browsing window is modal by default. specify this to use a popup window
FileDialogFlags_NoTitleBar = 1 << 3, // hide window title bar
FileDialogFlags_NoStatusBar = 1 << 4, // hide status bar at the bottom of browsing window
FileDialogFlags_CloseOnEsc = 1 << 5, // close file browser when pressing 'ESC'
FileDialogFlags_CreateNewDir = 1 << 6, // allow user to create new directory
FileDialogFlags_MultipleSelection = 1 << 7, // allow user to select multiple files. this will hide FileDialogFlags_EnterNewFilename
};

// A native file browser class, based on the implementation in NanoGUI.
class FileDialog
{
public:
enum Flags
{
SelectDirectory = 1 << 0, // select directory instead of regular file
EnterNewFilename = 1 << 1, // allow user to enter new filename when selecting regular file
NoTitleBar = 1 << 2, // hide window title bar
};

public:
FileDialog(int flags = 0);
void setTitle(const std::string& title);
Expand All @@ -41,11 +37,10 @@ class FileDialog
std::string _title;
bool _openFlag = false;
bool _isOpened = false;
std::vector<mx::FilePath> _selectedFilenames;
mx::FilePathVec _selectedFilenames;
std::vector<std::pair<std::string, std::string>> _filetypes;
};

std::string launchFileDialog(const std::vector<std::pair<std::string, std::string>>& filetypes, bool save);
mx::StringVec launchFileDialog(const std::vector<std::pair<std::string, std::string>>& filetypes, bool save, bool multiple);

#endif
2 changes: 1 addition & 1 deletion source/MaterialXGraphEditor/FileDialog.mm
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// SPDX-License-Identifier: Apache-2.0
//

#include "FileDialog.h"
#include <MaterialXGraphEditor/FileDialog.h>

#include <MaterialXCore/Exception.h>

Expand Down
2 changes: 1 addition & 1 deletion source/MaterialXGraphEditor/Graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Graph::Graph(const std::string& materialFilename,
_libraryFolders(libraryFolders),
_initial(false),
_delete(false),
_fileDialogSave(FileDialogFlags_EnterNewFilename | FileDialogFlags_CreateNewDir),
_fileDialogSave(FileDialog::EnterNewFilename),
_isNodeGraph(false),
_graphTotalSize(0),
_popup(false),
Expand Down

0 comments on commit daea424

Please sign in to comment.