Cleanup: factor out "set default filepath" into a ED_fileselect_ensure_default_filepath

Follow up to D15904, a bunch of places had exact same logic for
"is filepath set? if not, set some default one", so factor all that out
into a separate ED_fileselect_ensure_default_filepath function.
This commit is contained in:
2022-09-07 13:27:27 +03:00
parent 97bd04d665
commit 13a7516f43
8 changed files with 44 additions and 94 deletions

View File

@@ -175,6 +175,14 @@ struct ScrArea *ED_fileselect_handler_area_find(const struct wmWindow *win,
*/
struct ScrArea *ED_fileselect_handler_area_find_any_with_op(const struct wmWindow *win);
/**
* If filepath property is not set on the operator, sets it to
* the blend file path (or untitled if file is not saved yet) with the given extension.
*/
void ED_fileselect_ensure_default_filepath(struct bContext *C,
struct wmOperator *op,
const char *extension);
/* TODO: Maybe we should move this to BLI?
* On the other hand, it's using defines from space-file area, so not sure... */
int ED_path_extension_type(const char *path);

View File

@@ -39,6 +39,7 @@
# include "RNA_define.h"
# include "RNA_enum_types.h"
# include "ED_fileselect.h"
# include "ED_object.h"
# include "UI_interface.h"
@@ -75,20 +76,7 @@ static int wm_alembic_export_invoke(bContext *C, wmOperator *op, const wmEvent *
RNA_boolean_set(op->ptr, "init_scene_frame_range", true);
if (!RNA_struct_property_is_set_ex(op->ptr, "filepath", false)) {
Main *bmain = CTX_data_main(C);
char filepath[FILE_MAX];
if (BKE_main_blendfile_path(bmain)[0] == '\0') {
BLI_strncpy(filepath, DATA_("untitled"), sizeof(filepath));
}
else {
BLI_strncpy(filepath, BKE_main_blendfile_path(bmain), sizeof(filepath));
}
BLI_path_extension_replace(filepath, sizeof(filepath), ".abc");
RNA_string_set(op->ptr, "filepath", filepath);
}
ED_fileselect_ensure_default_filepath(C, op, ".abc");
WM_event_add_fileselect(C, op);

View File

@@ -19,6 +19,7 @@
# include "DEG_depsgraph.h"
# include "ED_fileselect.h"
# include "ED_object.h"
# include "RNA_access.h"
@@ -36,22 +37,7 @@
static int wm_collada_export_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
{
Main *bmain = CTX_data_main(C);
if (!RNA_struct_property_is_set_ex(op->ptr, "filepath", false)) {
char filepath[FILE_MAX];
const char *blendfile_path = BKE_main_blendfile_path(bmain);
if (blendfile_path[0] == '\0') {
BLI_strncpy(filepath, DATA_("untitled"), sizeof(filepath));
}
else {
BLI_strncpy(filepath, blendfile_path, sizeof(filepath));
}
BLI_path_extension_replace(filepath, sizeof(filepath), ".dae");
RNA_string_set(op->ptr, "filepath", filepath);
}
ED_fileselect_ensure_default_filepath(C, op, ".dae");
WM_event_add_fileselect(C, op);

View File

@@ -20,6 +20,8 @@
# include "BLT_translation.h"
# include "ED_fileselect.h"
# include "RNA_access.h"
# include "RNA_define.h"
@@ -71,24 +73,6 @@ static void gpencil_export_common_props_definition(wmOperatorType *ot)
"Normalize",
"Export strokes with constant thickness");
}
static void set_export_filepath(bContext *C, wmOperator *op, const char *extension)
{
if (!RNA_struct_property_is_set_ex(op->ptr, "filepath", false)) {
Main *bmain = CTX_data_main(C);
char filepath[FILE_MAX];
if (BKE_main_blendfile_path(bmain)[0] == '\0') {
BLI_strncpy(filepath, DATA_("untitled"), sizeof(filepath));
}
else {
BLI_strncpy(filepath, BKE_main_blendfile_path(bmain), sizeof(filepath));
}
BLI_path_extension_replace(filepath, sizeof(filepath), extension);
RNA_string_set(op->ptr, "filepath", filepath);
}
}
# endif
/* <-------- SVG single frame export. --------> */
@@ -109,7 +93,7 @@ static bool wm_gpencil_export_svg_common_check(bContext *UNUSED(C), wmOperator *
static int wm_gpencil_export_svg_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
{
set_export_filepath(C, op, ".svg");
ED_fileselect_ensure_default_filepath(C, op, ".svg");
WM_event_add_fileselect(C, op);
@@ -264,7 +248,7 @@ static bool wm_gpencil_export_pdf_common_check(bContext *UNUSED(C), wmOperator *
static int wm_gpencil_export_pdf_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
{
set_export_filepath(C, op, ".pdf");
ED_fileselect_ensure_default_filepath(C, op, ".pdf");
WM_event_add_fileselect(C, op);

View File

@@ -18,6 +18,7 @@
# include "BLT_translation.h"
# include "ED_fileselect.h"
# include "ED_outliner.h"
# include "MEM_guardedalloc.h"
@@ -58,20 +59,7 @@ static const EnumPropertyItem io_obj_path_mode[] = {
static int wm_obj_export_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
{
if (!RNA_struct_property_is_set_ex(op->ptr, "filepath", false)) {
Main *bmain = CTX_data_main(C);
char filepath[FILE_MAX];
if (BKE_main_blendfile_path(bmain)[0] == '\0') {
BLI_strncpy(filepath, DATA_("untitled"), sizeof(filepath));
}
else {
BLI_strncpy(filepath, BKE_main_blendfile_path(bmain), sizeof(filepath));
}
BLI_path_extension_replace(filepath, sizeof(filepath), ".obj");
RNA_string_set(op->ptr, "filepath", filepath);
}
ED_fileselect_ensure_default_filepath(C, op, ".obj");
WM_event_add_fileselect(C, op);
return OPERATOR_RUNNING_MODAL;

View File

@@ -21,6 +21,7 @@
# include "BLT_translation.h"
# include "ED_fileselect.h"
# include "ED_object.h"
# include "MEM_guardedalloc.h"
@@ -84,21 +85,7 @@ static int wm_usd_export_invoke(bContext *C, wmOperator *op, const wmEvent *UNUS
options->as_background_job = true;
op->customdata = options;
if (!RNA_struct_property_is_set_ex(op->ptr, "filepath", false)) {
Main *bmain = CTX_data_main(C);
char filepath[FILE_MAX];
const char *main_blendfile_path = BKE_main_blendfile_path(bmain);
if (main_blendfile_path[0] == '\0') {
BLI_strncpy(filepath, DATA_("untitled"), sizeof(filepath));
}
else {
BLI_strncpy(filepath, main_blendfile_path, sizeof(filepath));
}
BLI_path_extension_replace(filepath, sizeof(filepath), ".usdc");
RNA_string_set(op->ptr, "filepath", filepath);
}
ED_fileselect_ensure_default_filepath(C, op, ".usdc");
WM_event_add_fileselect(C, op);

View File

@@ -1385,3 +1385,24 @@ ScrArea *ED_fileselect_handler_area_find_any_with_op(const wmWindow *win)
return NULL;
}
void ED_fileselect_ensure_default_filepath(struct bContext *C,
struct wmOperator *op,
const char *extension)
{
if (!RNA_struct_property_is_set_ex(op->ptr, "filepath", false)) {
struct Main *bmain = CTX_data_main(C);
char filepath[FILE_MAX];
const char *blendfile_path = BKE_main_blendfile_path(bmain);
if (blendfile_path[0] == '\0') {
BLI_strncpy(filepath, DATA_("untitled"), sizeof(filepath));
}
else {
BLI_strncpy(filepath, blendfile_path, sizeof(filepath));
}
BLI_path_extension_replace(filepath, sizeof(filepath), extension);
RNA_string_set(op->ptr, "filepath", filepath);
}
}

View File

@@ -54,6 +54,7 @@
#include "RNA_prototypes.h"
/* For menu, popup, icons, etc. */
#include "ED_fileselect.h"
#include "ED_keyframing.h"
#include "ED_numinput.h"
#include "ED_outliner.h"
@@ -3088,20 +3089,7 @@ static int sequencer_export_subtitles_invoke(bContext *C,
wmOperator *op,
const wmEvent *UNUSED(event))
{
Main *bmain = CTX_data_main(C);
if (!RNA_struct_property_is_set(op->ptr, "filepath")) {
char filepath[FILE_MAX];
if (BKE_main_blendfile_path(bmain)[0] == '\0') {
BLI_strncpy(filepath, DATA_("untitled"), sizeof(filepath));
}
else {
BLI_strncpy(filepath, BKE_main_blendfile_path(bmain), sizeof(filepath));
}
BLI_path_extension_replace(filepath, sizeof(filepath), ".srt");
RNA_string_set(op->ptr, "filepath", filepath);
}
ED_fileselect_ensure_default_filepath(C, op, ".srt");
WM_event_add_fileselect(C, op);
@@ -3136,7 +3124,7 @@ static int sequencer_export_subtitles_exec(bContext *C, wmOperator *op)
FILE *file;
char filepath[FILE_MAX];
if (!RNA_struct_property_is_set(op->ptr, "filepath")) {
if (!RNA_struct_property_is_set_ex(op->ptr, "filepath", false)) {
BKE_report(op->reports, RPT_ERROR, "No filename given");
return OPERATOR_CANCELLED;
}