Blender V4.5
io_drop_import_file.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "BLI_path_utils.hh"
6#include "BLI_string.h"
7
8#include "BLT_translation.hh"
9
10#include "BKE_file_handler.hh"
11
12#include "CLG_log.h"
13
14#include "DNA_space_types.h"
15
16#include "RNA_access.hh"
17#include "RNA_define.hh"
18#include "RNA_prototypes.hh"
19
20#include "WM_api.hh"
21#include "WM_types.hh"
22
23#include "UI_interface.hh"
24
26#include "io_utils.hh"
27
28static CLG_LogRef LOG = {"io.drop_import_file"};
29
36 const bContext *C, const blender::Span<std::string> paths, const bool quiet = true)
37{
38 using namespace blender;
39 auto file_handlers = bke::file_handlers_poll_file_drop(C, paths);
40 file_handlers.remove_if([quiet](const bke::FileHandlerType *file_handler) {
41 return WM_operatortype_find(file_handler->import_operator, quiet) == nullptr;
42 });
43 return file_handlers;
44}
45
50 const blender::bke::FileHandlerType *file_handler,
51 PointerRNA &props,
53{
54
55 const auto supported_paths = file_handler->filter_supported_paths(paths);
56
57 PropertyRNA *filepath_prop = RNA_struct_find_property_check(props, "filepath", PROP_STRING);
58 if (filepath_prop) {
59 RNA_property_string_set(&props, filepath_prop, paths[supported_paths[0]].c_str());
60 }
61
62 PropertyRNA *directory_prop = RNA_struct_find_property_check(props, "directory", PROP_STRING);
63 char dir[FILE_MAX];
64 BLI_path_split_dir_part(paths[0].c_str(), dir, sizeof(dir));
65 if (directory_prop) {
66 RNA_property_string_set(&props, directory_prop, dir);
67 }
68
70 props, "files", &RNA_OperatorFileListElement);
71 if (files_prop) {
72 RNA_property_collection_clear(&props, files_prop);
73 for (const auto &index : supported_paths) {
74 char file[FILE_MAX];
75 STRNCPY(file, paths[index].c_str());
76 BLI_path_rel(file, dir);
77
78 PointerRNA item_ptr{};
79 RNA_property_collection_add(&props, files_prop, &item_ptr);
80 BLI_assert_msg(BLI_path_is_rel(file), "Expected path to be relative (start with '//')");
81 RNA_string_set(&item_ptr, "name", file + 2);
82 }
83 }
84 const bool has_any_filepath_prop = filepath_prop || directory_prop || files_prop;
89 const bool has_missing_filepath_prop = bool(directory_prop) != bool(files_prop);
90
91 if (!has_any_filepath_prop || has_missing_filepath_prop) {
92 const char *message =
93 "Expected operator properties filepath or files and directory not found. Refer to "
94 "FileHandler documentation for details.";
95 CLOG_WARN(&LOG, "%s", message);
96 }
97}
98
100{
102 if (paths.is_empty()) {
103 return OPERATOR_CANCELLED;
104 }
105
106 auto file_handlers = drop_import_file_poll_file_handlers(C, paths, false);
107 if (file_handlers.is_empty()) {
108 return OPERATOR_CANCELLED;
109 }
110
111 wmOperatorType *ot = WM_operatortype_find(file_handlers[0]->import_operator, false);
112 PointerRNA file_props;
114 file_handler_import_operator_write_ptr(file_handlers[0], file_props, paths);
115
116 WM_operator_name_call_ptr(C, ot, WM_OP_INVOKE_DEFAULT, &file_props, nullptr);
117 WM_operator_properties_free(&file_props);
118 return OPERATOR_FINISHED;
119}
120
122 wmOperator *op,
123 const wmEvent * /*event*/)
124{
126 if (paths.is_empty()) {
127 return OPERATOR_CANCELLED;
128 }
129
130 auto file_handlers = drop_import_file_poll_file_handlers(C, paths, false);
131 if (file_handlers.size() == 1) {
132 return wm_drop_import_file_exec(C, op);
133 }
134
139 uiPopupMenu *pup = UI_popup_menu_begin(C, "", ICON_NONE);
140 uiLayout *layout = UI_popup_menu_layout(pup);
142
143 for (auto *file_handler : file_handlers) {
144 wmOperatorType *ot = WM_operatortype_find(file_handler->import_operator, false);
145 PointerRNA file_props = layout->op(ot,
146 CTX_TIP_(ot->translation_context, ot->name),
147 ICON_NONE,
150 file_handler_import_operator_write_ptr(file_handler, file_props, paths);
151 }
152
153 UI_popup_menu_end(C, pup);
154 return OPERATOR_INTERFACE;
155}
156
158{
159 ot->name = "Drop to Import File";
160 ot->description = "Operator that allows file handlers to receive file drops";
161 ot->idname = "WM_OT_drop_import_file";
162 ot->flag = OPTYPE_INTERNAL;
165
166 PropertyRNA *prop;
167
169 ot->srna, "directory", nullptr, FILE_MAX, "Directory", "Directory of the file");
171
172 prop = RNA_def_collection_runtime(ot->srna, "files", &RNA_OperatorFileListElement, "Files", "");
174}
175
180
181static bool drop_import_file_poll(bContext *C, wmDrag *drag, const wmEvent * /*event*/)
182{
183 if (drag->type != WM_DRAG_PATH) {
184 return false;
185 }
186 const auto paths = WM_drag_get_paths(drag);
187 return !drop_import_file_poll_file_handlers(C, paths, true).is_empty();
188}
189
191 wmDrag *drag,
192 const int /*xy*/[2],
193 wmDropBox * /*drop*/)
194{
195 const auto paths = WM_drag_get_paths(drag);
196 const auto file_handlers = drop_import_file_poll_file_handlers(C, paths, true);
197 if (file_handlers.size() == 1) {
198 wmOperatorType *ot = WM_operatortype_find(file_handlers[0]->import_operator, false);
199 return TIP_(ot->name);
200 }
201
202 return TIP_("Multiple file handlers can be used, drop to pick which to use");
203}
204
206{
209 "WM_OT_drop_import_file",
212 nullptr,
214}
#define BLI_assert_msg(a, msg)
Definition BLI_assert.h:53
#define FILE_MAX
bool BLI_path_is_rel(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
void void BLI_path_split_dir_part(const char *filepath, char *dir, size_t dir_maxncpy) ATTR_NONNULL(1
bool void BLI_path_rel(char path[FILE_MAX], const char *basepath) ATTR_NONNULL(1)
char * STRNCPY(char(&dst)[N], const char *src)
Definition BLI_string.h:688
#define TIP_(msgid)
#define CTX_TIP_(context, msgid)
#define CLOG_WARN(clg_ref,...)
Definition CLG_log.h:181
@ RGN_TYPE_WINDOW
@ SPACE_EMPTY
@ OPERATOR_CANCELLED
@ OPERATOR_INTERFACE
@ OPERATOR_FINISHED
@ PROP_STRING
Definition RNA_types.hh:153
@ PROP_SKIP_SAVE
Definition RNA_types.hh:330
@ PROP_HIDDEN
Definition RNA_types.hh:324
#define C
Definition RandGen.cpp:29
void UI_popup_menu_end(bContext *C, uiPopupMenu *pup)
uiPopupMenu * UI_popup_menu_begin(bContext *C, const char *title, int icon) ATTR_NONNULL()
uiLayout * UI_popup_menu_layout(uiPopupMenu *pup)
#define UI_ITEM_NONE
void uiLayoutSetOperatorContext(uiLayout *layout, wmOperatorCallContext opcontext)
@ WM_OP_INVOKE_DEFAULT
Definition WM_types.hh:238
@ WM_DRAG_PATH
Definition WM_types.hh:1205
@ OPTYPE_INTERNAL
Definition WM_types.hh:202
static wmOperatorStatus wm_drop_import_file_invoke(bContext *C, wmOperator *op, const wmEvent *)
static blender::Vector< blender::bke::FileHandlerType * > drop_import_file_poll_file_handlers(const bContext *C, const blender::Span< std::string > paths, const bool quiet=true)
static std::string drop_import_file_tooltip(bContext *C, wmDrag *drag, const int[2], wmDropBox *)
static void file_handler_import_operator_write_ptr(const blender::bke::FileHandlerType *file_handler, PointerRNA &props, const blender::Span< std::string > paths)
static wmOperatorStatus wm_drop_import_file_exec(bContext *C, wmOperator *op)
void WM_OT_drop_import_file(wmOperatorType *ot)
static void drop_import_file_copy(bContext *, wmDrag *drag, wmDropBox *drop)
static bool drop_import_file_poll(bContext *C, wmDrag *drag, const wmEvent *)
void ED_dropbox_drop_import_file()
#define LOG(severity)
Definition log.h:32
blender::Vector< FileHandlerType * > file_handlers_poll_file_drop(const bContext *C, const blender::Span< std::string > paths)
Vector< std::string > paths_from_operator_properties(PointerRNA *ptr)
Definition io_utils.cc:75
void paths_to_operator_properties(PointerRNA *ptr, const Span< std::string > paths)
Definition io_utils.cc:117
PropertyRNA * RNA_struct_find_collection_property_check(PointerRNA &props, const char *name, const StructRNA *struct_type_check)
void RNA_string_set(PointerRNA *ptr, const char *name, const char *value)
PropertyRNA * RNA_struct_find_property_check(PointerRNA &props, const char *name, const PropertyType property_type_check)
void RNA_property_collection_add(PointerRNA *ptr, PropertyRNA *prop, PointerRNA *r_ptr)
void RNA_property_collection_clear(PointerRNA *ptr, PropertyRNA *prop)
void RNA_property_string_set(PointerRNA *ptr, PropertyRNA *prop, const char *value)
PropertyRNA * RNA_def_collection_runtime(StructOrFunctionRNA *cont_, const char *identifier, StructRNA *type, const char *ui_name, const char *ui_description)
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
PropertyRNA * RNA_def_string_dir_path(StructOrFunctionRNA *cont_, const char *identifier, const char *default_value, const int maxlen, const char *ui_name, const char *ui_description)
char import_operator[OP_MAX_TYPENAME]
blender::Vector< int64_t > filter_supported_paths(const blender::Span< std::string > paths) const
PointerRNA op(wmOperatorType *ot, std::optional< blender::StringRef > name, int icon, wmOperatorCallContext context, eUI_Item_Flag flag)
eWM_DragDataType type
Definition WM_types.hh:1327
PointerRNA * ptr
Definition WM_types.hh:1415
struct PointerRNA * ptr
wmDropBox * WM_dropbox_add(ListBase *lb, const char *idname, bool(*poll)(bContext *C, wmDrag *drag, const wmEvent *event), void(*copy)(bContext *C, wmDrag *drag, wmDropBox *drop), void(*cancel)(Main *bmain, wmDrag *drag, wmDropBox *drop), WMDropboxTooltipFunc tooltip)
blender::Span< std::string > WM_drag_get_paths(const wmDrag *drag)
ListBase * WM_dropboxmap_find(const char *idname, int spaceid, int regionid)
wmOperatorStatus WM_operator_name_call_ptr(bContext *C, wmOperatorType *ot, wmOperatorCallContext context, PointerRNA *properties, const wmEvent *event)
wmOperatorType * ot
Definition wm_files.cc:4226
wmOperatorType * WM_operatortype_find(const char *idname, bool quiet)
void WM_operator_properties_create_ptr(PointerRNA *ptr, wmOperatorType *ot)
void WM_operator_properties_free(PointerRNA *ptr)