cgv
Loading...
Searching...
No Matches
cgv::gui::file_helper Class Reference

A convenience class that provides a file input gui control. More...

#include <file_helper.h>

Public Types

enum class  Mode { kOpen , kSave , kOpenAndSave }
 Operation mode enum. More...
 

Public Member Functions

 file_helper ()
 Construct with default parameters.
 
 file_helper (provider *p, const std::string &title, Mode mode)
 Construct with arguments.
 
bool can_open () const
 Return true if this file_helper supports opening files, false otherwise.
 
bool can_save () const
 Return true if this file_helper supports saving files, false otherwise.
 
const std::string & get_title (Mode mode=Mode::kOpenAndSave) const
 Get the title of the specified operation Mode.
 
void set_title (const std::string &title, Mode mode=Mode::kOpenAndSave)
 Set the title of the specified operation Mode.
 
const std::string & get_default_path (Mode mode=Mode::kOpenAndSave) const
 Get the default path of the specified operation Mode.
 
void set_default_path (const std::string &path, Mode mode=Mode::kOpenAndSave)
 Set the default path of the specified operation Mode.
 
void set_file_name (const std::string &file_name)
 Set the current file name.
 
void clear_filters ()
 Clear all custom filters.
 
void add_filter (const std::string &name, const std::string &extension, Mode mode=Mode::kOpenAndSave)
 Add a custom filter matching a single extension.
 
void add_multi_filter (const std::string &name, const std::vector< std::string > &extensions, Mode mode=Mode::kOpenAndSave)
 Add a custom filter matching multiple extensions.
 
void add_filter_for_all_files (Mode mode=Mode::kOpenAndSave)
 Add a filter that matches all files.
 
bool compare_extension (const std::string &extension) const
 Compare the extension of file_name to the given string (case insensitive)
 
const std::string ensure_extension (const std::string &extension, bool force=false)
 Ensure the stored file_name has the given extension.
 
void create_gui (const std::string &label, const std::string &extra_options="")
 Create the gui control for the file input.
 
bool is_save_action () const
 Check whether a save action was performed.
 

Public Attributes

std::string file_name = ""
 The current file name.
 

Detailed Description

A convenience class that provides a file input gui control.

Usage: Add as a member to your provider class: cgv::gui::file_helper input;

Set the provider pointer and mode in the constructor of your class: input = cgv::gui::file_helper(this, "Title", cgv::gui::file_helper::Mode::kOpenAndSave);

Set filters to match file types as needed. The first filter that is added will be selected by default when openign the dialog. input.add_filter("Text Files", "txt");

Create the gui in your create_gui method: input.create_gui("Label");

Testing for value changes in on_set(void* member_ptr): if(member_ptr == &input.file_name) { if(input.is_save_action()) { ...handle save } else { ...handle open } }

Definition at line 35 of file file_helper.h.

Member Enumeration Documentation

◆ Mode

enum class cgv::gui::file_helper::Mode
strong

Operation mode enum.

Definition at line 38 of file file_helper.h.

Constructor & Destructor Documentation

◆ file_helper() [1/2]

cgv::gui::file_helper::file_helper ( )
inline

Construct with default parameters.

Default constructor is given to enable using this class as a stack variable member in other classes. Before usage it must be re-assigned with a valid provider pointer.

Definition at line 106 of file file_helper.h.

◆ file_helper() [2/2]

cgv::gui::file_helper::file_helper ( provider p,
const std::string &  title,
Mode  mode 
)
inline

Construct with arguments.

Parameters
pThe gui provider pointer.
titleThe title shown in the file dialog.
modeThe operating mode.

Definition at line 112 of file file_helper.h.

Member Function Documentation

◆ add_filter()

void cgv::gui::file_helper::add_filter ( const std::string &  name,
const std::string &  extension,
Mode  mode = Mode::kOpenAndSave 
)
inline

Add a custom filter matching a single extension.

Add a filter for matching files with the given extension in the file dialog. Example: add_filter("Text Files", "txt"); will add a filter displaying "Text Files (*.txt)"

Parameters
nameThe display name.
extensionThe extensions matched.
modeThe operation mode where this filter is used (defaults to kOpenAndSave).

Definition at line 215 of file file_helper.h.

◆ add_filter_for_all_files()

void cgv::gui::file_helper::add_filter_for_all_files ( Mode  mode = Mode::kOpenAndSave)
inline

Add a filter that matches all files.

Definition at line 236 of file file_helper.h.

◆ add_multi_filter()

void cgv::gui::file_helper::add_multi_filter ( const std::string &  name,
const std::vector< std::string > &  extensions,
Mode  mode = Mode::kOpenAndSave 
)
inline

Add a custom filter matching multiple extensions.

Add a filter for matching files with one of the given extensions in the file dialog. Example: add_multi_filter("Table Files", { "csv", "txt" }); will add a filter displaying "Table Files (*.csv, *.txt)"

Parameters
nameThe display name.
extensionThe extensions matched.
modeThe operation mode where this filter is used (defaults to kOpenAndSave).

Definition at line 230 of file file_helper.h.

References cgv::utils::join().

◆ can_open()

bool cgv::gui::file_helper::can_open ( ) const
inline

Return true if this file_helper supports opening files, false otherwise.

Definition at line 121 of file file_helper.h.

Referenced by create_gui().

◆ can_save()

bool cgv::gui::file_helper::can_save ( ) const
inline

Return true if this file_helper supports saving files, false otherwise.

Definition at line 126 of file file_helper.h.

Referenced by create_gui().

◆ clear_filters()

void cgv::gui::file_helper::clear_filters ( )
inline

Clear all custom filters.

After this method has been called new filters may be set. The gui must be redrawn manually in order for this change to take effect.

Definition at line 200 of file file_helper.h.

◆ compare_extension()

bool cgv::gui::file_helper::compare_extension ( const std::string &  extension) const
inline

Compare the extension of file_name to the given string (case insensitive)

Parameters
extensionThe extension to compare against.
Returns
True if the stored file_name has extension, false otherwise.

Definition at line 243 of file file_helper.h.

References file_name, and cgv::utils::to_upper().

◆ create_gui()

void cgv::gui::file_helper::create_gui ( const std::string &  label,
const std::string &  extra_options = "" 
)
inline

Create the gui control for the file input.

Parameters
labelThe control label
extra_optionsAdditional options applied to the string input control.

Definition at line 272 of file file_helper.h.

References cgv::gui::property_string::add(), cgv::gui::property_string::add_bracketed(), cgv::gui::provider::add_gui(), can_open(), can_save(), and file_name.

◆ ensure_extension()

const std::string cgv::gui::file_helper::ensure_extension ( const std::string &  extension,
bool  force = false 
)
inline

Ensure the stored file_name has the given extension.

When a file_name was selected through a file dialog, a user might not always input an extension. This method ensures the file_name hast the given extension and adds it if not present or in all cases when force is true.

Parameters
extensionThe extension string to add (without dot).
forceForce adding the extension.
Returns
The extension of the file_name after this operation.

Definition at line 256 of file file_helper.h.

References file_name, set_file_name(), and cgv::utils::to_upper().

◆ get_default_path()

const std::string & cgv::gui::file_helper::get_default_path ( Mode  mode = Mode::kOpenAndSave) const
inline

Get the default path of the specified operation Mode.

Parameters
modeThe operation Mode.
Returns
The default path string.

Definition at line 162 of file file_helper.h.

◆ get_title()

const std::string & cgv::gui::file_helper::get_title ( Mode  mode = Mode::kOpenAndSave) const
inline

Get the title of the specified operation Mode.

Parameters
modeThe optional operation Mode.
Returns
The title string.

Definition at line 134 of file file_helper.h.

◆ is_save_action()

bool cgv::gui::file_helper::is_save_action ( ) const
inline

Check whether a save action was performed.

Returns
True if a save action occured, false otherwise.

Definition at line 295 of file file_helper.h.

References file_name, and cgv::gui::provider::ref_tree_node_visible_flag().

◆ set_default_path()

void cgv::gui::file_helper::set_default_path ( const std::string &  path,
Mode  mode = Mode::kOpenAndSave 
)
inline

Set the default path of the specified operation Mode.

Parameters
pathThe new defautl path.
modeThe operation Mode.

Definition at line 173 of file file_helper.h.

References file_name, and cgv::gui::provider::set_control_property().

◆ set_file_name()

void cgv::gui::file_helper::set_file_name ( const std::string &  file_name)
inline

Set the current file name.

Parameters
file_nameThe file name.

Definition at line 189 of file file_helper.h.

References file_name, and cgv::gui::provider::update_member().

Referenced by ensure_extension().

◆ set_title()

void cgv::gui::file_helper::set_title ( const std::string &  title,
Mode  mode = Mode::kOpenAndSave 
)
inline

Set the title of the specified operation Mode.

Parameters
titleThe new title.
modeThe operation Mode.

Definition at line 145 of file file_helper.h.

References file_name, and cgv::gui::provider::set_control_property().

Member Data Documentation

◆ file_name

std::string cgv::gui::file_helper::file_name = ""

The current file name.

This member is public to make it accessible to the GUI control flow. It should never be set directly and set_file_name should be used instead.

Definition at line 100 of file file_helper.h.

Referenced by compare_extension(), create_gui(), ensure_extension(), is_save_action(), set_default_path(), set_file_name(), and set_title().


The documentation for this class was generated from the following file: