Blender V5.3
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
8
9#include "BLI_path_utils.hh"
10#include "BLI_string.hh"
11
12#include "BLT_translation.hh"
13
14#include "BKE_file_handler.hh"
15
16#include "CLG_log.h"
17
18#include "DNA_space_types.h"
19
20#include "RNA_access.hh"
21#include "RNA_define.hh"
22#include "RNA_prototypes.hh"
23
24#include "WM_api.hh"
25#include "WM_types.hh"
26
27#include "UI_interface.hh"
29
31#include "io_utils.hh"
32
33namespace blender {
34
35static CLG_LogRef LOG = {"io.drop_import_file"};
36
43 const bContext *C, const Span<std::string> paths, const bool quiet = true)
44{
46 file_handlers.remove_if([quiet](const bke::FileHandlerType *file_handler) {
47 return WM_operatortype_find(file_handler->import_operator, quiet) == nullptr;
48 });
49 return file_handlers;
50}
51
56 PointerRNA &props,
57 const Span<std::string> paths)
58{
59
60 const auto supported_paths = file_handler->filter_supported_paths(paths);
61
62 PropertyRNA *filepath_prop = RNA_struct_find_property_check(props, "filepath", PROP_STRING);
63 if (filepath_prop) {
64 RNA_property_string_set(&props, filepath_prop, paths[supported_paths[0]].c_str());
65 }
66
67 PropertyRNA *directory_prop = RNA_struct_find_property_check(props, "directory", PROP_STRING);
68 char dir[FILE_MAX];
69 BLI_path_split_dir_part(paths[0].c_str(), dir, sizeof(dir));
70 if (directory_prop) {
71 RNA_property_string_set(&props, directory_prop, dir);
72 }
73
75 props, "files", RNA_OperatorFileListElement);
76 if (files_prop) {
77 RNA_property_collection_clear(&props, files_prop);
78 for (const auto &index : supported_paths) {
79 char file[FILE_MAX];
80 STRNCPY(file, paths[index].c_str());
81 BLI_path_rel(file, dir);
82
83 PointerRNA item_ptr{};
84 RNA_property_collection_add(&props, files_prop, &item_ptr);
85 BLI_assert_msg(BLI_path_is_rel(file), "Expected path to be relative (start with '//')");
86 RNA_string_set(&item_ptr, "name", file + 2);
87 }
88 }
89 const bool has_any_filepath_prop = filepath_prop || directory_prop || files_prop;
90 if (!has_any_filepath_prop) {
92 "The '%s' file handler import operator ('%s') is missing the required operator "
93 "properties.",
94 file_handler->idname,
95 file_handler->import_operator);
96 }
97 if (directory_prop && !files_prop) {
99 &LOG,
100 "The '%s' file handler import operator ('%s') is missing the 'files' operator property.",
101 file_handler->idname,
102 file_handler->import_operator);
103 }
104 if (!directory_prop && files_prop) {
105 CLOG_WARN(&LOG,
106 "The '%s' file handler import operator ('%s') is missing the 'directory' operator "
107 "property.",
108 file_handler->idname,
109 file_handler->import_operator);
110 }
111}
112
114{
115 const auto paths = ed::io::paths_from_operator_properties(op->ptr);
116 if (paths.is_empty()) {
117 return OPERATOR_CANCELLED;
118 }
119
121 if (file_handlers.is_empty()) {
122 return OPERATOR_CANCELLED;
123 }
124
125 wmOperatorType *ot = WM_operatortype_find(file_handlers[0]->import_operator, false);
128
130 WM_operator_properties_free(&file_props);
131 return OPERATOR_FINISHED;
132}
133
135 wmOperator *op,
136 const wmEvent * /*event*/)
137{
138 const auto paths = ed::io::paths_from_operator_properties(op->ptr);
139 if (paths.is_empty()) {
140 return OPERATOR_CANCELLED;
141 }
142
144 if (file_handlers.size() == 1) {
145 return wm_drop_import_file_exec(C, op);
146 }
147
152 ui::PopupMenu *pup = ui::popup_menu_begin(C, "", ICON_NONE);
153 ui::Layout &layout = *popup_menu_layout(pup);
155
156 for (auto *file_handler : file_handlers) {
157 wmOperatorType *ot = WM_operatortype_find(file_handler->import_operator, false);
158 PointerRNA file_props = layout.op(ot,
160 ICON_NONE,
163 file_handler_import_operator_write_ptr(file_handler, file_props, paths);
164 }
165
166 popup_menu_end(C, pup);
167 return OPERATOR_INTERFACE;
168}
169
171{
172 ot->name = "Drop to Import File";
173 ot->description = "Operator that allows file handlers to receive file drops";
174 ot->idname = "WM_OT_drop_import_file";
175 ot->flag = OPTYPE_INTERNAL;
178
179 PropertyRNA *prop;
180
182 ot->srna, "directory", nullptr, FILE_MAX, "Directory", "Directory of the file");
184
185 prop = RNA_def_collection_runtime(ot->srna, "files", RNA_OperatorFileListElement, "Files", "");
187}
188
189static void drop_import_file_copy(bContext * /*C*/, wmDrag *drag, wmDropBox *drop)
190{
192}
193
194static bool drop_import_file_poll(bContext *C, wmDrag *drag, const wmEvent * /*event*/)
195{
196 if (drag->type != WM_DRAG_PATH) {
197 return false;
198 }
199 const auto paths = WM_drag_get_paths(drag);
200 return !drop_import_file_poll_file_handlers(C, paths, true).is_empty();
201}
202
204 wmDrag *drag,
205 const int /*xy*/[2],
206 wmDropBox * /*drop*/)
207{
208 const auto paths = WM_drag_get_paths(drag);
209 const auto file_handlers = drop_import_file_poll_file_handlers(C, paths, true);
210 if (file_handlers.size() == 1) {
211 wmOperatorType *ot = WM_operatortype_find(file_handlers[0]->import_operator, false);
212 return TIP_(ot->name);
213 }
214
215 return TIP_("Multiple file handlers can be used, drop to pick which to use");
216}
217
219{
220 ListBaseT<wmDropBox> *lb = WM_dropboxmap_find("Window", SPACE_EMPTY, RGN_TYPE_WINDOW);
222 "WM_OT_drop_import_file",
225 nullptr,
227}
228
229} // namespace blender
#define FILE_MAX
#define TIP_(msgid)
#define CTX_TIP_(context, msgid)
#define CLOG_WARN(clg_ref,...)
Definition CLG_log.h:189
#define C
Definition RandGen.cpp:29
#define UI_ITEM_NONE
#define BLI_assert_msg(a, msg)
Vector< FileHandlerType * > file_handlers_poll_file_drop(const bContext *C, const Span< std::string > paths)
Span< std::unique_ptr< FileHandlerType > > file_handlers()
Vector< std::string > paths_from_operator_properties(PointerRNA *ptr)
Definition io_utils.cc:79
void paths_to_operator_properties(PointerRNA *ptr, const Span< std::string > paths)
Definition io_utils.cc:121
PopupMenu * popup_menu_begin(bContext *C, const char *title, int icon) ATTR_NONNULL()
BMesh Iterators.
wmDropBox * WM_dropbox_add(ListBaseT< wmDropBox > *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)
char * STRNCPY(char(&dst)[N], const char *src)
void WM_operator_properties_free(PointerRNA *ptr)
void RNA_string_set(PointerRNA *ptr, const char *name, const char *value)
PointerRNA WM_operator_properties_create_ptr(wmOperatorType *ot)
@ WM_DRAG_PATH
Definition WM_types.hh:1302
void ED_dropbox_drop_import_file()
PropertyRNA * RNA_struct_find_property_check(PointerRNA &props, const char *name, const PropertyType property_type_check)
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)
PropertyRNA * RNA_def_collection_runtime(StructOrFunctionRNA *cont, const char *identifier, StructRNA *type, const char *ui_name, const char *ui_description)
void RNA_property_collection_add(PointerRNA *ptr, PropertyRNA *prop, PointerRNA *r_ptr)
static wmOperatorStatus wm_drop_import_file_exec(bContext *C, wmOperator *op)
void void BLI_path_split_dir_part(const char *filepath, char *dir, size_t dir_maxncpy) ATTR_NONNULL(1
void RNA_property_string_set(PointerRNA *ptr, PropertyRNA *prop, const char *value)
@ PROP_STRING
Definition RNA_types.hh:213
static void file_handler_import_operator_write_ptr(const bke::FileHandlerType *file_handler, PointerRNA &props, const Span< std::string > paths)
static std::string drop_import_file_tooltip(bContext *C, wmDrag *drag, const int[2], wmDropBox *)
static wmOperatorStatus wm_drop_import_file_invoke(bContext *C, wmOperator *op, const wmEvent *)
bool void BLI_path_rel(char path[FILE_MAX], const char *basepath) ATTR_NONNULL(1)
void WM_OT_drop_import_file(wmOperatorType *ot)
wmOperatorType * WM_operatortype_find(const char *idname, bool quiet)
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
bool BLI_path_is_rel(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
void RNA_property_collection_clear(PointerRNA *ptr, PropertyRNA *prop)
wmOperatorStatus WM_operator_name_call_ptr(bContext *C, wmOperatorType *ot, wm::OpCallContext context, PointerRNA *properties, const wmEvent *event)
@ PROP_SKIP_SAVE
Definition RNA_types.hh:414
@ PROP_HIDDEN
Definition RNA_types.hh:408
Span< std::string > WM_drag_get_paths(const wmDrag *drag)
static CLG_LogRef LOG
Definition logging.cpp:12
static void drop_import_file_copy(bContext *, wmDrag *drag, wmDropBox *drop)
@ OPTYPE_INTERNAL
Definition WM_types.hh:210
static Vector< bke::FileHandlerType * > drop_import_file_poll_file_handlers(const bContext *C, const Span< std::string > paths, const bool quiet=true)
ListBaseT< wmDropBox > * WM_dropboxmap_find(const char *idname, int spaceid, int regionid)
PropertyRNA * RNA_struct_find_collection_property_check(PointerRNA &props, const char *name, const StructRNA *struct_type_check)
static bool drop_import_file_poll(bContext *C, wmDrag *drag, const wmEvent *)
Vector< int64_t > filter_supported_paths(const Span< std::string > paths) const
char import_operator[OP_MAX_TYPENAME]
void operator_context_set(wm::OpCallContext opcontext)
PointerRNA op(wmOperatorType *ot, std::optional< StringRef > name, int icon, wm::OpCallContext context, eUI_Item_Flag flag)
eWM_DragDataType type
Definition WM_types.hh:1424
PointerRNA * ptr
Definition WM_types.hh:1524
wmOperatorStatus(*) exec(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1125
const char * description
Definition WM_types.hh:1115
const char * translation_context
Definition WM_types.hh:1113
wmOperatorStatus(*) invoke(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1141