TGUI  0.10-beta
FileDialog.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2022 Bruno Van de Velde (vdv_b@tgui.eu)
5//
6// This software is provided 'as-is', without any express or implied warranty.
7// In no event will the authors be held liable for any damages arising from the use of this software.
8//
9// Permission is granted to anyone to use this software for any purpose,
10// including commercial applications, and to alter it and redistribute it freely,
11// subject to the following restrictions:
12//
13// 1. The origin of this software must not be misrepresented;
14// you must not claim that you wrote the original software.
15// If you use this software in a product, an acknowledgment
16// in the product documentation would be appreciated but is not required.
17//
18// 2. Altered source versions must be plainly marked as such,
19// and must not be misrepresented as being the original software.
20//
21// 3. This notice may not be removed or altered from any source distribution.
22//
24
25
26#ifndef TGUI_FILE_DIALOG_HPP
27#define TGUI_FILE_DIALOG_HPP
28
29
30#include <TGUI/CopiedSharedPtr.hpp>
31#include <TGUI/Widgets/Label.hpp>
32#include <TGUI/Widgets/Button.hpp>
33#include <TGUI/Widgets/EditBox.hpp>
34#include <TGUI/Widgets/ComboBox.hpp>
35#include <TGUI/Widgets/ListView.hpp>
36#include <TGUI/Widgets/ChildWindow.hpp>
37#include <TGUI/Renderers/FileDialogRenderer.hpp>
38#include <TGUI/Filesystem.hpp>
39#include <tuple>
40
42
43namespace tgui
44{
45 class FileDialogIconLoader;
46
61 class TGUI_API FileDialog : public ChildWindow
62 {
63 public:
64
65 typedef std::shared_ptr<FileDialog> Ptr;
66 typedef std::shared_ptr<const FileDialog> ConstPtr;
67
68
76 FileDialog(const char* typeName = "FileDialog", bool initRenderer = true);
77
78
87 static FileDialog::Ptr create(String title = "Open file", String confirmButtonText = "Open");
88
89
93 FileDialog(const FileDialog& copy);
94
95
99 FileDialog(FileDialog&& copy) noexcept;
100
101
105 FileDialog& operator= (const FileDialog& right);
106
107
111 FileDialog& operator= (FileDialog&& right) noexcept;
112
113
122
123
129 const FileDialogRenderer* getSharedRenderer() const;
130
137 const FileDialogRenderer* getRenderer() const;
138
139
157 const std::vector<Filesystem::Path>& getSelectedPaths() const;
158
159
167 void setPath(const String& path);
168
169
177 void setPath(const Filesystem::Path& path);
178
179
185 const Filesystem::Path& getPath() const;
186
187
193 void setFilename(const String& filename);
194
195
205 const String& getFilename() const;
206
207
231 void setFileTypeFilters(const std::vector<std::pair<String, std::vector<String>>>& filters, std::size_t defaultFilterIndex = 0);
232
233
241 const std::vector<std::pair<String, std::vector<String>>>& getFileTypeFilters() const;
242
243
251 std::size_t getFileTypeFiltersIndex() const;
252
253
259 void setConfirmButtonText(const String& text = "Open");
260
261
268
269
275 void setCancelButtonText(const String& text = "Cancel");
276
277
284
285
291 void setFilenameLabelText(const String& labelText = "Filename:");
292
293
300
301
309 void setListViewColumnCaptions(const String& nameColumnText = "Name", const String& sizeColumnText = "Size", const String& modifiedColumnText = "Modified");
310
311
319 std::tuple<String, String, String> getListViewColumnCaptions() const;
320
321
329 void setFileMustExist(bool enforceExistence);
330
331
337 bool getFileMustExist() const;
338
339
348 void setSelectingDirectory(bool selectDirectories);
349
350
357
358
364 void setMultiSelect(bool multiSelect);
365
366
372 bool getMultiSelect() const;
373
374
384 void setIconLoader(std::shared_ptr<FileDialogIconLoader> iconLoader);
385
386
392 std::shared_ptr<FileDialogIconLoader> getIconLoader() const;
393
394
398 void keyPressed(const Event::KeyEvent& event) override;
399
403 void textEntered(char32_t key) override;
404
405
407 protected:
408
418 Signal& getSignal(String signalName) override;
419
420
427 void rendererChanged(const String& property) override;
428
429
433 std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
434
435
439 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
440
441
443 // This function is called every frame with the time passed since the last frame.
445 bool updateTime(Duration elapsedTime) override;
446
447
449 // Makes a copy of the widget
451 Widget::Ptr clone() const override;
452
453
455 private:
456
458 // Changes the directory that is shown in the dialog
460 void changePath(const Filesystem::Path& path, bool updateHistory);
461
462
464 // Updates the back and forward buttons when the path history changes
466 void historyChanged();
467
468
470 // Adds the files to the list view in the order shosen by the user
472 void sortFilesInListView();
473
474
476 // Stores the selected files and closes the dialog
478 void filesSelected(std::vector<Filesystem::Path> filenames);
479
480
482 // Updates whether the open/save button is enabled or disabled
484 void updateConfirmButtonEnabled();
485
486
488 // Handles a press of the open/save button
490 void confirmButtonPressed();
491
492
494 // Initializes the widget pointers after copying or loading the dialog
496 void identifyChildWidgets();
497
498
500 // Connects the signals of the child widgets
502 void connectSignals();
503
504
506 public:
507
511 SignalPathList onFileSelect = {"FileSelected"};
512
513
515 protected:
516
517 Button::Ptr m_buttonBack;
518 Button::Ptr m_buttonForward;
519 Button::Ptr m_buttonUp;
520 EditBox::Ptr m_editBoxPath;
521 ListView::Ptr m_listView;
522 Label::Ptr m_labelFilename;
523 EditBox::Ptr m_editBoxFilename;
524 ComboBox::Ptr m_comboBoxFileTypes;
525 Button::Ptr m_buttonCancel;
526 Button::Ptr m_buttonConfirm;
527
528 Filesystem::Path m_currentDirectory;
529 std::vector<Filesystem::FileInfo> m_filesInDirectory;
530 std::vector<Texture> m_fileIcons; // Same order as m_filesInDirectory
531 std::size_t m_sortColumnIndex = 0;
532 bool m_sortInversed = false;
533
534 std::vector<Filesystem::Path> m_pathHistory;
535 std::size_t m_pathHistoryIndex = 0;
536
537 bool m_fileMustExist = true;
538 bool m_selectingDirectory = false;
539 bool m_multiSelect = false;
540
541 std::vector<std::pair<String, std::vector<String>>> m_fileTypeFilters;
542 std::size_t m_selectedFileTypeFilter = 0;
543
544 std::shared_ptr<FileDialogIconLoader> m_iconLoader;
545
546 std::vector<Filesystem::Path> m_selectedFiles;
547 };
548
550}
551
553
554#endif // TGUI_FILE_DIALOG_HPP
std::shared_ptr< Button > Ptr
Shared widget pointer.
Definition: Button.hpp:42
Child window widget.
Definition: ChildWindow.hpp:44
std::shared_ptr< ComboBox > Ptr
Shared widget pointer.
Definition: ComboBox.hpp:54
Wrapper for durations.
Definition: Duration.hpp:52
std::shared_ptr< EditBox > Ptr
Shared widget pointer.
Definition: EditBox.hpp:49
Definition: FileDialogRenderer.hpp:37
File dialog widget.
Definition: FileDialog.hpp:62
void setCancelButtonText(const String &text="Cancel")
Changes the text of the cancel button (e.g. to display it in a different language)
void setMultiSelect(bool multiSelect)
Changes whether multiple files can be selected.
static FileDialog::Ptr create(String title="Open file", String confirmButtonText="Open")
Creates a new file dialog widget.
void setFileTypeFilters(const std::vector< std::pair< String, std::vector< String > > > &filters, std::size_t defaultFilterIndex=0)
Changes the file filters which the user can select to only show files of a certain type.
void setFileMustExist(bool enforceExistence)
Changes whether the file should exist or whether the filename can be a non-existent file.
std::size_t getFileTypeFiltersIndex() const
Returns the index of the currently selected file filter.
void setConfirmButtonText(const String &text="Open")
Changes the text of the open/save button.
std::unique_ptr< DataIO::Node > save(SavingRenderersMap &renderers) const override
Saves the widget as a tree node in order to save it to a file.
void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
Loads the widget from a tree of nodes.
const String & getConfirmButtonText() const
Return the text of the open/save button.
std::shared_ptr< const FileDialog > ConstPtr
Shared constant widget pointer.
Definition: FileDialog.hpp:66
FileDialogRenderer * getRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
bool getFileMustExist() const
Returns whether the file should exist or whether the filename can be a non-existent file.
void setPath(const String &path)
Changes the directory for which the files are to be displayed.
const String & getCancelButtonText() const
Return the text of the cancel button.
std::shared_ptr< FileDialogIconLoader > getIconLoader() const
Gets the icon loader that is currently being used.
const String & getFilenameLabelText() const
Return the text of the filename label.
FileDialogRenderer * getSharedRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
std::shared_ptr< FileDialog > Ptr
Shared widget pointer.
Definition: FileDialog.hpp:65
const std::vector< std::pair< String, std::vector< String > > > & getFileTypeFilters() const
Returns the file filters which the user can select to only show files of a certain type.
void setPath(const Filesystem::Path &path)
Changes the directory for which the files are to be displayed.
std::tuple< String, String, String > getListViewColumnCaptions() const
Returns the names of the list view columns.
const String & getFilename() const
Returns the filename that is entered in the filename edit box.
bool getMultiSelect() const
Returns whether multiple files can be selected.
void setIconLoader(std::shared_ptr< FileDialogIconLoader > iconLoader)
Sets a custom icon loader.
void setSelectingDirectory(bool selectDirectories)
Changes whether the file dialog is used for selecting a file or for selecting a directory.
void rendererChanged(const String &property) override
Function called when one of the properties of the renderer is changed.
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
bool getSelectingDirectory() const
Returns whether the file dialog is used for selecting a file or for selecting a directory.
void setFilenameLabelText(const String &labelText="Filename:")
Changes the text of the filename label (e.g. to display it in a different language)
void setFilename(const String &filename)
Sets the filename that is shown at the bottom of the file dialog.
FileDialog(FileDialog &&copy) noexcept
Move constructor.
const std::vector< Filesystem::Path > & getSelectedPaths() const
Returns the selected files/directories.
void setListViewColumnCaptions(const String &nameColumnText="Name", const String &sizeColumnText="Size", const String &modifiedColumnText="Modified")
Changes the names of the list view columns (e.g. to display them in a different language)
Signal & getSignal(String signalName) override
Retrieves a signal based on its name.
FileDialog(const FileDialog &copy)
Copy constructor.
const Filesystem::Path & getPath() const
Returns the directory that is currently being shown in the file dialog.
static FileDialog::Ptr copy(FileDialog::ConstPtr dialog)
Makes a copy of another file dialog.
Object to represent paths on a filesystem.
Definition: Filesystem.hpp:56
std::shared_ptr< Label > Ptr
Shared widget pointer.
Definition: Label.hpp:47
std::shared_ptr< ListView > Ptr
Shared widget pointer.
Definition: ListView.hpp:49
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:58
Wrapper class to store strings.
Definition: String.hpp:79
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:73
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
KeyPressed event parameters.
Definition: Event.hpp:167