cgv
Loading...
Searching...
No Matches
file_helper.h
1#pragma once
2
3#include <cgv/gui/property_string.h>
4#include <cgv/gui/provider.h>
5#include <cgv/utils/file.h>
6#include <cgv/utils/scan.h>
7
8namespace cgv {
9namespace gui {
10
36public:
38 enum class Mode {
39 kOpen,
40 kSave,
41 kOpenAndSave
42 };
43
44private:
46 provider* provider_ptr_ = nullptr;
47
49 Mode mode_ = Mode::kOpen;
51 std::string open_title_ = "";
53 std::string save_title_ = "";
55 std::string open_filter_ = "";
57 std::string save_filter_ = "";
59 std::string open_path_ = "";
61 std::string save_path_ = "";
62
64 bool mode_allows_open(Mode mode) const {
65 return mode == Mode::kOpenAndSave || mode == Mode::kOpen;
66 }
67
69 bool mode_allows_save(Mode mode) const {
70 return mode == Mode::kOpenAndSave || mode == Mode::kSave;
71 }
72
80 void extend_filter_string(const std::string& entry, std::string& to_filter) {
81 if(!to_filter.empty())
82 to_filter += "|";
83
84 to_filter += entry;
85 }
86
88 void add_filter_entry(const std::string& entry, Mode mode) {
89 if(mode_allows_open(mode))
90 extend_filter_string(entry, open_filter_);
91
92 if(mode_allows_save(mode))
93 extend_filter_string(entry, save_filter_);
94 }
95
96public:
100 std::string file_name = "";
101
107
112 file_helper(provider* p, const std::string& title, Mode mode) : provider_ptr_(p), mode_(mode) {
113 if(mode_allows_open(mode_))
114 open_title_ = title;
115
116 if(mode_allows_save(mode_))
117 save_title_ = title;
118 }
119
121 bool can_open() const {
122 return mode_allows_open(mode_);
123 }
124
126 bool can_save() const {
127 return mode_allows_save(mode_);
128 }
129
134 const std::string& get_title(Mode mode = Mode::kOpenAndSave) const {
135 if(mode_allows_open(mode))
136 return open_title_;
137
138 return save_title_;
139 }
140
145 void set_title(const std::string& title, Mode mode = Mode::kOpenAndSave) {
146 if(mode_allows_open(mode))
147 open_title_ = title;
148
149 if(mode_allows_save(mode))
150 save_title_ = title;
151
152 if(provider_ptr_) {
153 provider_ptr_->set_control_property(file_name, "open_title", open_title_);
154 provider_ptr_->set_control_property(file_name, "save_title", save_title_);
155 }
156 }
157
162 const std::string& get_default_path(Mode mode = Mode::kOpenAndSave) const {
163 if(mode_allows_open(mode))
164 return open_path_;
165
166 return save_path_;
167 }
168
173 void set_default_path(const std::string& path, Mode mode = Mode::kOpenAndSave) {
174 if(mode_allows_open(mode))
175 open_path_ = path;
176
177 if(mode_allows_save(mode))
178 save_path_ = path;
179
180 if(provider_ptr_) {
181 provider_ptr_->set_control_property(file_name, "open_path", open_path_);
182 provider_ptr_->set_control_property(file_name, "save_path", save_path_);
183 }
184 }
185
189 void set_file_name(const std::string& file_name) {
190 this->file_name = file_name;
191
192 if(provider_ptr_)
193 provider_ptr_->update_member(&this->file_name);
194 }
195
201 open_filter_ = "";
202 save_filter_ = "";
203 }
204
215 void add_filter(const std::string& name, const std::string& extension, Mode mode = Mode::kOpenAndSave) {
216 std::string entry = name + " (*." + extension + "):*." + extension;
217 add_filter_entry(entry, mode);
218 }
219
230 void add_multi_filter(const std::string& name, const std::vector<std::string>& extensions, Mode mode = Mode::kOpenAndSave) {
231 std::string entry = name + " (*." + cgv::utils::join(extensions, ", *.") + "):*." + cgv::utils::join(extensions, ";*.");
232 add_filter_entry(entry, mode);
233 }
234
236 void add_filter_for_all_files(Mode mode = Mode::kOpenAndSave) {
237 add_filter_entry("All Files (*.*):*.*", mode);
238 }
239
243 bool compare_extension(const std::string& extension) const {
244 return cgv::utils::to_upper(cgv::utils::file::get_extension(file_name)) == cgv::utils::to_upper(extension);
245 }
246
256 const std::string ensure_extension(const std::string& extension, bool force = false) {
257 std::string current_extension = cgv::utils::file::get_extension(file_name);
258
259 if(cgv::utils::to_upper(current_extension) != cgv::utils::to_upper(extension)) {
260 if(current_extension.empty() || force) {
261 set_file_name(file_name + "." + extension);
262 return extension;
263 }
264 }
265
266 return current_extension;
267 }
268
272 void create_gui(const std::string& label, const std::string& extra_options = "") {
273 property_string options;
274
275 options.add("open", can_open());
276 options.add("save", can_save());
277
278 options.add_bracketed("open_title", open_title_);
279 options.add_bracketed("save_title", save_title_);
280
281 options.add_bracketed("open_filter", open_filter_);
282 options.add_bracketed("save_filter", save_filter_);
283
284 options.add_bracketed("open_path", open_path_);
285 options.add_bracketed("save_path", save_path_);
286
287 options.add("", extra_options);
288
289 if(provider_ptr_)
290 provider_ptr_->add_gui(label, file_name, "file_name", options);
291 }
292
295 bool is_save_action() const {
296 return provider_ptr_ ? provider_ptr_->ref_tree_node_visible_flag(file_name) : false;
297 }
298};
299
300}
301}
A convenience class that provides a file input gui control.
Definition file_helper.h:35
bool is_save_action() const
Check whether a save action was performed.
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(const std::string &name, const std::string &extension, Mode mode=Mode::kOpenAndSave)
Add a custom filter matching a single extension.
void clear_filters()
Clear all custom filters.
file_helper()
Construct with default parameters.
void set_default_path(const std::string &path, Mode mode=Mode::kOpenAndSave)
Set the default path of the specified operation Mode.
Mode
Operation mode enum.
Definition file_helper.h:38
void add_filter_for_all_files(Mode mode=Mode::kOpenAndSave)
Add a filter that matches all files.
bool can_open() const
Return true if this file_helper supports opening files, false otherwise.
const std::string & get_default_path(Mode mode=Mode::kOpenAndSave) const
Get the default path of the specified operation Mode.
file_helper(provider *p, const std::string &title, Mode mode)
Construct with arguments.
void create_gui(const std::string &label, const std::string &extra_options="")
Create the gui control for the file input.
const std::string & get_title(Mode mode=Mode::kOpenAndSave) const
Get the title of the specified operation Mode.
bool can_save() const
Return true if this file_helper supports saving files, false otherwise.
void set_title(const std::string &title, Mode mode=Mode::kOpenAndSave)
Set the title of the specified operation Mode.
std::string file_name
The current file name.
bool compare_extension(const std::string &extension) const
Compare the extension of file_name to the given string (case insensitive)
void set_file_name(const std::string &file_name)
Set the current file name.
const std::string ensure_extension(const std::string &extension, bool force=false)
Ensure the stored file_name has the given extension.
A convenience class for compiling strings of delimited key-value pairs useful for defining GUI contro...
void add(const std::string &key, T value)
Add key-value pair to end of content, converting value to string.
void add_bracketed(const std::string &key, const T value, char bracket='\'')
See add.
derive from this class to provide a gui to the current viewer
Definition provider.h:64
bool add_gui(const std::string &label, T &value, const std::string &gui_type="", const std::string &options="")
Add a composed gui of the given gui_type for the given value.
Definition provider.h:247
virtual void update_member(void *member_ptr)
call this to update all views and controls of a member
Definition provider.cxx:72
void set_control_property(T &value, const std::string &property_name, const std::string &property_value)
Set the property value of all controls of a given class member.
Definition provider.h:343
static bool & ref_tree_node_visible_flag(const T &value)
return a reference to the boolean flag, that tells whether the tree node for the passed value is visi...
Definition provider.h:217
char to_upper(char c)
convert char to upper case
Definition scan.cxx:106
std::string join(const std::vector< std::string >::const_iterator first, const std::vector< std::string >::const_iterator last, const std::string &sep, bool trailing_sep)
joins a given range of strings, separating them by the given separator; if trailing_sep is true,...
Definition scan.cxx:733
the cgv namespace
Definition print.h:11
Helper functions to process strings.