FilePicker.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/LexicalPath.h>
  28. #include <AK/Optional.h>
  29. #include <LibCore/StandardPaths.h>
  30. #include <LibGUI/Dialog.h>
  31. #include <LibGUI/ImageWidget.h>
  32. #include <LibGUI/Model.h>
  33. namespace GUI {
  34. class FilePicker final
  35. : public Dialog
  36. , private ModelClient {
  37. C_OBJECT(FilePicker);
  38. public:
  39. enum class Mode {
  40. Open,
  41. OpenMultiple,
  42. Save
  43. };
  44. enum class Options : unsigned {
  45. None = 0,
  46. DisablePreview = (1 << 0)
  47. };
  48. static Optional<String> get_open_filepath(Window* parent_window, Options options)
  49. {
  50. return get_open_filepath(parent_window, {}, options);
  51. }
  52. static Optional<String> get_open_filepath(Window* parent_window, const String& window_title = {}, Options options = Options::None);
  53. static Optional<String> get_save_filepath(Window* parent_window, const String& title, const String& extension, Options options = Options::None);
  54. static bool file_exists(const StringView& path);
  55. virtual ~FilePicker() override;
  56. LexicalPath selected_file() const { return m_selected_file; }
  57. private:
  58. bool have_preview() const { return m_preview_container; }
  59. void set_preview(const LexicalPath&);
  60. void clear_preview();
  61. void on_file_return();
  62. void set_path(const String&);
  63. // ^GUI::ModelClient
  64. virtual void model_did_update(unsigned) override;
  65. FilePicker(Window* parent_window, Mode type = Mode::Open, Options = Options::None, const StringView& file_name = "Untitled", const StringView& path = Core::StandardPaths::home_directory());
  66. static String ok_button_name(Mode mode)
  67. {
  68. switch (mode) {
  69. case Mode::Open:
  70. case Mode::OpenMultiple:
  71. return "Open";
  72. case Mode::Save:
  73. return "Save";
  74. default:
  75. return "OK";
  76. }
  77. }
  78. RefPtr<MultiView> m_view;
  79. NonnullRefPtr<FileSystemModel> m_model;
  80. LexicalPath m_selected_file;
  81. RefPtr<TextBox> m_filename_textbox;
  82. RefPtr<TextBox> m_location_textbox;
  83. RefPtr<Frame> m_preview_container;
  84. RefPtr<ImageWidget> m_preview_image;
  85. RefPtr<Label> m_preview_name_label;
  86. RefPtr<Label> m_preview_geometry_label;
  87. Mode m_mode { Mode::Open };
  88. };
  89. }