TGUI  0.9.1
FileDialog.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2021 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 
43 namespace 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 
267  const String& getConfirmButtonText() const;
268 
269 
275  void setCancelButtonText(const String& text = "Cancel");
276 
277 
283  const String& getCancelButtonText() const;
284 
285 
291  void setFilenameLabelText(const String& labelText = "Filename:");
292 
293 
299  const String& getFilenameLabelText() const;
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 
356  bool getSelectingDirectory() const;
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 
400 
402  protected:
403 
413  Signal& getSignal(String signalName) override;
414 
415 
422  void rendererChanged(const String& property) override;
423 
424 
428  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
429 
430 
434  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
435 
436 
438  // This function is called every frame with the time passed since the last frame.
440  bool updateTime(Duration elapsedTime) override;
441 
442 
444  // Makes a copy of the widget
446  Widget::Ptr clone() const override
447  {
448  return std::make_shared<FileDialog>(*this);
449  }
450 
451 
453  private:
454 
456  // Changes the directory that is shown in the dialog
458  void changePath(const Filesystem::Path& path, bool updateHistory);
459 
460 
462  // Updates the back and forward buttons when the path history changes
464  void historyChanged();
465 
466 
468  // Adds the files to the list view in the order shosen by the user
470  void sortFilesInListView();
471 
472 
474  // Stores the selected files and closes the dialog
476  void filesSelected(std::vector<Filesystem::Path> filenames);
477 
478 
480  // Updates whether the open/save button is enabled or disabled
482  void updateConfirmButtonEnabled();
483 
484 
486  // Handles a press of the open/save button
488  void confirmButtonPressed();
489 
490 
492  // Initializes the widget pointers after copying or loading the dialog
494  void identifyChildWidgets();
495 
496 
498  // Connects the signals of the child widgets
500  void connectSignals();
501 
502 
504  public:
505 
509  SignalPathList onFileSelect = {"FileSelected"};
510 
511 
513  protected:
514 
515  Button::Ptr m_buttonBack;
516  Button::Ptr m_buttonForward;
517  Button::Ptr m_buttonUp;
518  EditBox::Ptr m_editBoxPath;
519  ListView::Ptr m_listView;
520  Label::Ptr m_labelFilename;
521  EditBox::Ptr m_editBoxFilename;
522  ComboBox::Ptr m_comboBoxFileTypes;
523  Button::Ptr m_buttonCancel;
524  Button::Ptr m_buttonConfirm;
525 
526  Filesystem::Path m_currentDirectory;
527  std::vector<Filesystem::FileInfo> m_filesInDirectory;
528  std::vector<Texture> m_fileIcons; // Same order as m_filesInDirectory
529  std::size_t m_sortColumnIndex = 0;
530  bool m_sortInversed = false;
531 
532  std::vector<Filesystem::Path> m_pathHistory;
533  std::size_t m_pathHistoryIndex = 0;
534 
535  bool m_fileMustExist = true;
536  bool m_selectingDirectory = false;
537  bool m_multiSelect = false;
538 
539  std::vector<std::pair<String, std::vector<String>>> m_fileTypeFilters;
540  std::size_t m_selectedFileTypeFilter = 0;
541 
542  std::shared_ptr<FileDialogIconLoader> m_iconLoader;
543 
544  std::vector<Filesystem::Path> m_selectedFiles;
545  };
546 
548 }
549 
551 
552 #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)
const String & getConfirmButtonText() const
Return the text of the open/save button.
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 setFileMustExist(bool enforceExistence)
Changes whether the file should exist or whether the filename can be a non-existent file.
const String & getFilename() const
Returns the filename that is entered in the filename edit box.
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.
Signal & getSignal(String signalName) override
Retrieves a signal based on its name.
void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
Loads the widget from a tree of nodes.
FileDialogRenderer * getRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
std::shared_ptr< const FileDialog > ConstPtr
Shared constant widget pointer.
Definition: FileDialog.hpp:66
FileDialogRenderer * getSharedRenderer()
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< FileDialog > Ptr
Shared widget pointer.
Definition: FileDialog.hpp:65
void setPath(const Filesystem::Path &path)
Changes the directory for which the files are to be displayed.
bool getMultiSelect() const
Returns whether multiple files can be selected.
std::shared_ptr< FileDialogIconLoader > getIconLoader() const
Gets the icon loader that is currently being used.
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 setIconLoader(std::shared_ptr< FileDialogIconLoader > iconLoader)
Sets a custom icon loader.
std::tuple< String, String, String > getListViewColumnCaptions() const
Returns the names of the list view columns.
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.
Definition: FileDialog.hpp:446
const String & getFilenameLabelText() const
Return the text of the filename label.
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.
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.
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)
const std::vector< Filesystem::Path > & getSelectedPaths() const
Returns the selected files/directories.
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.
FileDialog(const FileDialog &copy)
Copy constructor.
static FileDialog::Ptr copy(FileDialog::ConstPtr dialog)
Makes a copy of another file dialog.
const Filesystem::Path & getPath() const
Returns the directory that is currently being shown in the 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:74
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