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