NDEVR
API Documentation
FileChooserDialog.h
1/*--------------------------------------------------------------------------------------------
2Copyright (c) 2019, NDEVR LLC
3tyler.parke@ndevr.org
4 __ __ ____ _____ __ __ _______
5 | \ | | | __ \ | ___|\ \ / / | __ \
6 | \ | | | | \ \ | |___ \ \ / / | |__) |
7 | . \| | | |__/ / | |___ \ V / | _ /
8 | |\ |_|_____/__|_____|___\_/____| | \ \
9 |__| \__________________________________| \__\
10
11Subject to the terms of the Enterprise+ Agreement, NDEVR hereby grants
12Licensee a limited, non-exclusive, non-transferable, royalty-free license
13(without the right to sublicense) to use the API solely for the purpose of
14Licensee's internal development efforts to develop applications for which
15the API was provided.
16
17The above copyright notice and this permission notice shall be included in all
18copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
21INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
22PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
23FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25DEALINGS IN THE SOFTWARE.
26
27Library: Widgets
28File: FileChooserDialog
29Included in API: True
30Author(s): Tyler Parke
31 *-----------------------------------------------------------------------------------------**/
32#pragma once
33#include "DLLInfo.h"
34#include <NDEVR/QCustomStackedWidget.h>
35#include <NDEVR/File.h>
36#include <NDEVR/FileFormat.h>
37#include <NDEVR/Buffer.h>
38#include <NDEVR/UUID.h>
39#include <NDEVR/INIInterface.h>
40#include <QWidget>
41#include <functional>
42#include <QPointer>
43#include <QIcon>
44class QHBoxLayout;
45#if NDEVR_SUPPORTS_THREADING
46class QThread;
47#endif
48namespace NDEVR
49{
50
51 class INIFactory;
52 class Button;
54 class Thread;
55 class Toggle;
56 class ButtonGallery;
57 class QCustomLineEdit;
58 class QCustomComboBox;
60 class FolderCreatorWidget;
66 {
67 QPointer<Button> button;
68 QPointer<QCustomTableWidget> table;
69 uint04 table_index = Constant<uint04>::Invalid;
70 };
71
80 class NDEVR_WIDGETS_API FileChooserDialog : public QWidget, public INIInterface
81 {
82 Q_OBJECT
83 public:
84#if NDEVR_SUPPORTS_THREADING
85 friend class ImageCacheThread;
86#endif
96
101 FileChooserDialog(bool save_dialog = false, QWidget *parent = nullptr);
108 FileChooserDialog(const File& ini_location, bool save_dialog = false, QWidget *parent = nullptr);
115 void setOptions(const Buffer<FileFormat>& extensions, bool save_dialog);
120 QSize sizeHint() const override;
145 void setAllowAny(bool allow_any) { m_allow_any = allow_any; }
150 bool isSaveDialog() const { return m_save_dialog; }
155 void setCurrentDirectory(const File& current_directory);
160 void setFallbackDirectory(const File& file);
165 void setSaveContent(const Buffer<UUID>& save_content) { m_save_content = save_content; }
170 const Buffer<UUID>& saveContent() const { return m_save_content; }
175 void showModifiedTime(bool show_modified_time);
180 void showAccessedTime(bool show_accessed_time);
185 void showFileSize(bool show_modified_time);
190 void setShowRecents(bool show_recent);
200 void setCurrentFile(const File& file);
206 void setCurrentExtension(const StringView& extension, bool update_format_to_first_match);
212 bool event(QEvent* e) override;
217 bool isFolderBrowser() const;
222 bool showRecents() const { return m_show_recents; };
227 bool showFavorites() const;
232 void prepareForINI(INIFactory& factory) override;
237 void finishReadingINI(INIFactory& factory) override;
251 void browseFile(PopupInfo popup_origin = PopupInfo());
256 void setIconProvider(const std::function<String(const FileRequest&)>& icon_provider);
261 void setCanReadCallback(const std::function<bool(const FileFormat&, const File&)>& callback);
271 void setActionName(const TranslatedString& action_name);
276 void addToRecents(const Buffer<File>& files);
282 void addToRecents(const File& file, bool update_display);
296 const File& currentDirectory() const { return m_current_directory; };
301 void setNativeParent(QWidget* native_parent);
306 static void SaveFileContent(File file);
314 void setRecentPriorityModifiers(sint04 constant_modifier, fltp08 recent_priority_time_modifier);
315 protected:
339 QWidget* createRequestWidget(File item);
347 QWidget* settingsDialog();
349 String extensionsString(const StringView& delimiter = ";;") const;
351 void addItem(ButtonGallery* gallery, QCustomTableWidget* table, uint04 index, const File& item);
359 void setup();
361 FileFormat getFileFormat(const StringView& file, const StringView& string) const;
367 Buffer<File> filterFiles(const Buffer<File>& all_files) const;
369 bool checkFileAgainstFilter(const File& file, const FileFormat& selected_format) const;
375 void setIsFileChooser(bool is_file_chooser);
377 void onFileChosen(File file);
378 private slots:
380 void contextMenu(const QPoint &pos);
382 void browsingFinished();
384 void updateFilesSlot();
385 signals:
389 private:
391 void _setDisplayMode(DisplayMode mode);
393 QIcon _getIconCallback(const File& file);
395 String _getIconID(const File& file);
397 void _setCurrentDirectory(const File& dir);
399 void onUserReadyToFinish(bool was_browsing);
401 void onUserFinish();
403 void setFileIcon(File file, QIcon icon);
405 void showFiles(ButtonGallery* gallery, QCustomTableWidget* table, const Buffer<File>& files);
407 QCustomTableWidget* createTable(bool is_favorites = false);
409 virtual void getINI(INIFactory& factory) override;
411 bool getSelectedFilters(String & selected_filter, String & default_suffix);
413 void resolveIcons();
415 void resolveIcon(const File& file);
416 private:
417 Buffer<File> m_recent_files;
418 Dictionary<File, FileCachedData> m_program_managed_data;
419 Dictionary<File, UUID> m_last_format_used;
420 mutable Buffer<File> m_current_directory_files;
421 Buffer<File> m_additional_favorites;
422 Dictionary<File, QIcon> m_cached_icons;
423 Dictionary<File, IconCacheWidget> m_cached_icon_locations;
424 File m_current_directory;
425 File m_fallback_directory;
426 String m_current_file_name;
427 String m_current_file_extension;
428 Buffer<FileFormat> m_formats;
429
430 QHBoxLayout* m_top_layout;
431 sint04 m_recent_priority_constant_modifier = 1;
432 fltp08 m_recent_priority_time_modifier = 100.0;
433 File m_last_dir;
434 bool m_save_dialog;
435 bool m_show_recents = true;
436 bool m_touch_prefer_gallery = true;
437 bool m_desktop_prefer_gallery = false;
438 bool m_show_hidden = false;
439 bool m_show_extensions = true;
440 bool m_show_favorites;
441 DisplayMode m_display_mode;
442 FolderCreatorWidget* m_folder_creation_widget;
443 QCustomTableWidget* m_file_table;
444 QCustomTableWidget* m_favorites_table;
445 ButtonGallery* m_file_gallery;
446 QCustomStackedWidget* m_view_stack;
447 Button* m_browse_button;
448 Button* m_database_button;
449 Button* m_view_toggle;
450 Button* m_accept_button;
451 Button* m_new_folder_button;
452 Button* m_up_level_button;
453 Button* m_favorites_button;
454 QWidget* m_navigation_buttons;
455 QWidget* m_bottom_buttons;
456 QWidget* m_favorites_panel;
457 QCustomComboBox* m_file_types_combo;
458 QCustomLineEdit* m_location_label;
459
460 QCustomLineEdit* m_file_name_line_edit;
461 QCustomMessageBox* m_override_file_message_box;
462 QCustomMessageBox* m_invalid_file_message_box;
463 Buffer<UUID> m_save_content;
464 QIcon m_loading_icon;
465 QWidget* m_popup_dialog;
466 QWidget* m_settings_dialog;
467
468 TranslatedString m_action_name;
469
470 Buffer<File> m_pending_icons;
471#if NDEVR_SUPPORTS_THREADING
472 QThread* m_icon_thread = nullptr;
473#endif
474 QWidget* m_native_parent = nullptr;
475 std::function<String(const FileRequest&)> m_icon_provider;
476 std::function<bool(const FileFormat&, const File&)> m_can_read_callback;
477 protected:
482 bool m_needs_to_update = false;
483 };
484}
The equivelent of std::vector but with a bit more control.
Definition Buffer.hpp:58
A scrollable gallery widget that displays a collection of Buttons in a flow layout.
A core widget that allows the user to click one of many button types.
Definition Button.h:68
A hash-based key-value store, useful for quick associative lookups.
Definition Dictionary.h:64
QWidget * createRequestWidget(File item)
Creates a widget representing a single file request entry.
void postResolveIcons()
Finalizes icon resolution after background icon loading completes.
void clearRecents()
Clears all entries from the recents list.
void autoSelectFavoritesItem()
Automatically selects a favorites item based on the current directory.
bool isFolderBrowser() const
Returns whether this dialog is configured to browse for folders only.
void setCurrentFileName(const StringView &name)
Sets the current file name in the name field.
Buffer< FileRequest > m_selected_files
The currently selected file requests.
const Buffer< UUID > & saveContent() const
Returns the UUIDs of content to be saved.
void hidePopupDialog()
Hides the popup dialog if it is currently shown.
void showRecents()
Switches the display mode to show recent files.
DisplayMode
Enumerates the display modes available in the file chooser dialog.
@ e_favorites
Show favorited files and directories.
@ e_browser
Browse the filesystem directly.
@ e_recents
Show recently accessed files.
void setCurrentFile(const File &file)
Sets the current file, updating both directory and file name.
void setCurrentDirectory(const File &current_directory)
Sets the current browsing directory.
bool isSaveDialog() const
Returns whether this dialog is configured for saving files.
const Buffer< FileRequest > & getSelectedRequests() const
Returns the selected file requests, including format information.
void addToRecents(const Buffer< File > &files)
Adds multiple files to the recents list.
QSize sizeHint() const override
Returns the recommended size for this dialog.
void setSaveContent(const Buffer< UUID > &save_content)
Sets the UUIDs of content to be saved.
bool showRecents() const
Returns whether the recents section is shown.
FileFormat currentSelectedFormat() const
Returns the currently selected file format from the format combo box.
TranslatedString actionName() const
Returns the action name displayed on the accept button (e.g., "Open" or "Save").
void retranslate()
Retranslates all user-visible text in the dialog.
bool showFavorites() const
Returns whether the favorites section is shown.
void setActionName(const TranslatedString &action_name)
Sets the action name displayed on the accept button.
FileChooserDialog(const File &ini_location, bool save_dialog=false, QWidget *parent=nullptr)
Constructs a FileChooserDialog with a custom INI settings location.
void autoHideDialog(PopupInfo info=PopupInfo())
Hides the dialog using the most appropriate method for the current platform.
void setIconProvider(const std::function< String(const FileRequest &)> &icon_provider)
Sets a callback to provide custom icons for file requests.
QWidget * settingsDialog()
Creates and returns the settings dialog widget.
void finishReadingINI(INIFactory &factory) override
Called after reading dialog state from INI to finalize initialization.
void setOptions(const Buffer< FileFormat > &extensions, bool save_dialog)
Sets the available file formats and whether this is a save dialog.
void finishBrowse()
Finalizes the browse operation after the user makes a selection.
bool m_needs_to_update
Flag indicating that the file list needs refreshing.
Buffer< File > filterFiles(const Buffer< File > &all_files) const
Filters a list of files according to the current format selection.
void onFileChosenSignal()
Emitted when a file has been chosen by the user.
void setAllowAny(bool allow_any)
Sets whether any file type is allowed regardless of filter.
void setIsFileChooser(bool is_file_chooser)
Sets whether this dialog is choosing files (true) or folders (false).
void setCurrentExtension(const StringView &extension, bool update_format_to_first_match)
Sets the current file extension filter.
void autoShowDialog(PopupInfo info=PopupInfo())
Shows the dialog using the most appropriate method for the current platform.
bool event(QEvent *e) override
Handles events for the dialog.
QWidget * createNavigationButtons()
Creates the navigation button bar (back, up, favorites, etc.).
void updateTableIconSize(QCustomTableWidget *table)
Updates the icon size in the given table to match the current settings.
FileFormat getFileFormat(const StringView &file, const StringView &string) const
Determines the file format from a file path and a filter string.
FileChooserDialog(bool save_dialog=false, QWidget *parent=nullptr)
Constructs a FileChooserDialog.
const Buffer< File > favoriteFiles() const
Returns the list of favorite files and directories.
void setNativeParent(QWidget *native_parent)
Sets the parent widget for native file dialogs.
String extensionsString(const StringView &delimiter=";;") const
Builds a string of all supported extensions joined by the given delimiter.
bool checkFileAgainstFilter(const File &file, const FileFormat &selected_format) const
Checks whether a file matches the given format filter.
void setRecentPriorityModifiers(sint04 constant_modifier, fltp08 recent_priority_time_modifier)
Allows adding weight to a file (changing extension listing order) based on how recently a file has be...
void addItem(ButtonGallery *gallery, QCustomTableWidget *table, uint04 index, const File &item)
Adds a file item to both the gallery and table views.
void setAndSortFormats(const Buffer< FileFormat > &formats)
Assigns and sorts the available file formats by priority.
void setup()
Performs initial setup of all dialog widgets and layout.
void setFallbackDirectory(const File &file)
Sets a fallback directory to use when the current directory is invalid.
static void SaveFileContent(File file)
Saves file content associated with the given file.
void showModifiedTime(bool show_modified_time)
Sets whether the modified time column is visible.
void SetupForFavorites()
Sets up the dialog UI for favorites mode.
void addToRecents(const File &file, bool update_display)
Adds a single file to the recents list.
const Buffer< File > & currentDirectoryFiles() const
Returns the cached list of files in the current directory.
void updateBrowserWidgets()
Refreshes the file browser widgets with current directory content.
QWidget * createBottomPanel()
Creates the bottom panel with accept/cancel buttons and file name input.
void setShowRecents(bool show_recent)
Sets whether the recents section is available.
void onCanceledSignal()
Emitted when the user cancels the dialog.
void onFileChosen(File file)
Called when the user has chosen a file to process the selection.
bool m_allow_any
Whether any file type is accepted regardless of filter.
void selectFileNameField()
Selects and focuses the file name input field.
void showAccessedTime(bool show_accessed_time)
Sets whether the accessed time column is visible.
TranslatedString windowTitle() const
Returns the translated window title for this dialog.
void finishedSignal()
Emitted when the dialog finishes with a selection.
void setAdditionalFavorites(Buffer< File > favorites)
Sets additional directories to appear in the favorites list.
Buffer< File > getSelected() const
Returns the list of files currently selected by the user.
void updateGallery(Button *b)
Updates the gallery display when a button is selected.
void setCanReadCallback(const std::function< bool(const FileFormat &, const File &)> &callback)
Sets a callback to determine if a file can be read with a given format.
FileFormat getFileFormat(const File &file)
Determines the file format for a given file.
void browseFileNative()
Opens a native OS file browse dialog.
virtual void getINI(INIFactory &factory) override
Reads or writes dialog settings from/to an INI factory.
QWidget * createfavoritesPanel()
Creates the favorites side panel.
void showFileSize(bool show_modified_time)
Sets whether the file size column is visible.
void removeFromRecents(File item)
Removes a file from the recents list.
void SetupForBrowser()
Sets up the dialog UI for filesystem browsing mode.
FileFormat getFileFormatFromExtension(const StringView &ext) const
Determines the file format from a file extension string.
const File & currentDirectory() const
Returns the current browsing directory.
void prepareForINI(INIFactory &factory) override
Prepares dialog state for writing to INI.
void browseFile(PopupInfo popup_origin=PopupInfo())
Opens a native file browse dialog.
Data that describes a particular file format and how to use the format with the program.
Definition FileFormat.h:45
Logic for reading or writing to a file as well as navigating filesystems or other common file operati...
Definition File.h:53
Contains methods for easily reading and writing to an INI file including efficient casting,...
Definition INIReader.h:107
INIInterface()
Constructs an INIInterface with no default INI file.
A compact way to present a list of options to the user.
A line edit allows users to enter and edit a single line of plain text with useful editing functions,...
A message box displays a primary text to alert the user to a situation, an informative text to furthe...
The main class for a layout where all widgets occupy the same same and only one widget is shown at a ...
Provides a standard way to show table data within the application.
The core String View class for the NDEVR API.
Definition StringView.h:58
The core String class for the NDEVR API.
Definition String.h:95
The default thread class for executing concurrent sequences in the NDEVR API.
Definition Thread.h:113
A Button that toggles between on and off.
Definition Toggle.h:50
Any text displayed to the user should be defined as a TranslatedString which allows the program to lo...
The primary namespace for the NDEVR SDK.
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
double fltp08
Defines an alias representing an 8 byte floating-point number.
int32_t sint04
-Defines an alias representing a 4 byte, signed integer.
A FileRequest bundles format data as well as a particular file.
Definition FileFormat.h:101
A placeholder widget for Files displayed with FileChooserDialog until the actual icon can be loaded f...
QPointer< QCustomTableWidget > table
The table widget containing this icon entry.
QPointer< Button > button
The button widget displaying the file icon.
uint04 table_index
The row index in the table, or Invalid if not yet assigned.
Class which is used to pass arguments and requests for creating a popup dialog or widget.
Definition PopupInfo.h:16