diff options
author | Brecht Van Lommel <brechtvanlommel@gmail.com> | 2019-05-17 16:45:15 +0300 |
---|---|---|
committer | Brecht Van Lommel <brechtvanlommel@gmail.com> | 2019-05-17 18:59:26 +0300 |
commit | e8238e1123d9bab7210b922b4fb2d8f395bf70ef (patch) | |
tree | 0b56c95a3b3a12c86d22dc412d49c74450086605 | |
parent | b38853e89f63fe65b57af003dc803611cda313e7 (diff) |
Images: make image save operator available outside image editor
This includes some refactoring of image operator poll functions.
-rw-r--r-- | source/blender/editors/space_image/image_ops.c | 244 |
1 files changed, 137 insertions, 107 deletions
diff --git a/source/blender/editors/space_image/image_ops.c b/source/blender/editors/space_image/image_ops.c index eec9870ae45..873eae07901 100644 --- a/source/blender/editors/space_image/image_ops.c +++ b/source/blender/editors/space_image/image_ops.c @@ -181,24 +181,59 @@ static void sima_zoom_set_from_bounds(SpaceImage *sima, ARegion *ar, const rctf sima_zoom_set(sima, ar, size, NULL); } -static bool space_image_buffer_exists_poll(bContext *C) +static Image *image_from_context(const bContext *C) { - SpaceImage *sima = CTX_wm_space_image(C); - if (sima && ED_space_image_has_buffer(sima)) { - return true; + /* Edit image is set by templates used throughout the interface, so image + * operations work outside the image editor. */ + Image *ima = CTX_data_pointer_get_type(C, "edit_image", &RNA_Image).data; + + if (ima) { + return ima; + } + else { + /* Image editor. */ + SpaceImage *sima = CTX_wm_space_image(C); + return (sima) ? sima->image : NULL; } - return false; } -static bool image_not_packed_poll(bContext *C) +static ImageUser *image_user_from_context(const bContext *C) { - SpaceImage *sima = CTX_wm_space_image(C); + /* Edit image user is set by templates used throughout the interface, so + * image operations work outside the image editor. */ + ImageUser *iuser = CTX_data_pointer_get_type(C, "edit_image_user", &RNA_ImageUser).data; - /* Do not run 'replace' on packed images, it does not give user expected results at all. */ - if (sima && sima->image && BLI_listbase_is_empty(&sima->image->packedfiles)) { - return true; + if (iuser) { + return iuser; + } + else { + /* Image editor. */ + SpaceImage *sima = CTX_wm_space_image(C); + return (sima) ? &sima->iuser : NULL; } - return false; +} + +static bool image_buffer_exists_from_context(bContext *C) +{ + Image *ima = image_from_context(C); + ImageUser *iuser = image_user_from_context(C); + + if (ima == NULL) { + return false; + } + + void *lock; + ImBuf *ibuf = BKE_image_acquire_ibuf(ima, iuser, &lock); + const bool has_buffer = (ibuf && (ibuf->rect || ibuf->rect_float)); + BKE_image_release_ibuf(ima, ibuf, lock); + return has_buffer; +} + +static bool image_not_packed_poll(bContext *C) +{ + /* Do not run 'replace' on packed images, it does not give user expected results at all. */ + Image *ima = image_from_context(C); + return (ima && BLI_listbase_is_empty(&ima->packedfiles)); } static bool imbuf_format_writeable(const ImBuf *ibuf) @@ -211,37 +246,38 @@ static bool imbuf_format_writeable(const ImBuf *ibuf) static bool space_image_file_exists_poll(bContext *C) { - if (space_image_buffer_exists_poll(C)) { - Main *bmain = CTX_data_main(C); - SpaceImage *sima = CTX_wm_space_image(C); - ImBuf *ibuf; - void *lock; - bool ret = false; - char name[FILE_MAX]; + if (image_buffer_exists_from_context(C) == false) { + return false; + } - ibuf = ED_space_image_acquire_buffer(sima, &lock); - if (ibuf) { - BLI_strncpy(name, ibuf->name, FILE_MAX); - BLI_path_abs(name, BKE_main_blendfile_path(bmain)); + Main *bmain = CTX_data_main(C); + Image *ima = image_from_context(C); + ImageUser *iuser = image_user_from_context(C); + void *lock; + ImBuf *ibuf = BKE_image_acquire_ibuf(ima, iuser, &lock); + bool ret = false; - if (BLI_exists(name) == false) { - CTX_wm_operator_poll_msg_set(C, "image file not found"); - } - else if (!BLI_file_is_writable(name)) { - CTX_wm_operator_poll_msg_set(C, "image path can't be written to"); - } - else if (!imbuf_format_writeable(ibuf)) { - CTX_wm_operator_poll_msg_set(C, "image format is read-only"); - } - else { - ret = true; - } - } - ED_space_image_release_buffer(sima, ibuf, lock); + if (ibuf) { + char name[FILE_MAX]; + BLI_strncpy(name, ibuf->name, FILE_MAX); + BLI_path_abs(name, BKE_main_blendfile_path(bmain)); - return ret; + if (BLI_exists(name) == false) { + CTX_wm_operator_poll_msg_set(C, "image file not found"); + } + else if (!BLI_file_is_writable(name)) { + CTX_wm_operator_poll_msg_set(C, "image path can't be written to"); + } + else if (!imbuf_format_writeable(ibuf)) { + CTX_wm_operator_poll_msg_set(C, "image format is read-only"); + } + else { + ret = true; + } } - return false; + + BKE_image_release_ibuf(ima, ibuf, lock); + return ret; } bool space_image_main_region_poll(bContext *C) @@ -272,24 +308,23 @@ static bool space_image_main_area_not_uv_brush_poll(bContext *C) static bool image_sample_poll(bContext *C) { SpaceImage *sima = CTX_wm_space_image(C); - if (sima) { - Object *obedit = CTX_data_edit_object(C); - if (obedit) { - /* Disable when UV editing so it doesn't swallow all click events - * (use for setting cursor). */ - if (ED_space_image_show_uvedit(sima, obedit)) { - return false; - } - } - else if (sima->mode != SI_MODE_VIEW) { + if (sima == NULL) { + return false; + } + + Object *obedit = CTX_data_edit_object(C); + if (obedit) { + /* Disable when UV editing so it doesn't swallow all click events + * (use for setting cursor). */ + if (ED_space_image_show_uvedit(sima, obedit)) { return false; } - - return space_image_main_region_poll(C); } - else { + else if (sima->mode != SI_MODE_VIEW) { return false; } + + return true; } /********************** view pan operator *********************/ @@ -1506,21 +1541,16 @@ void IMAGE_OT_open(wmOperatorType *ot) static int image_match_len_exec(bContext *C, wmOperator *UNUSED(op)) { Scene *scene = CTX_data_scene(C); - Image *ima = CTX_data_pointer_get_type(C, "edit_image", &RNA_Image).data; - ImageUser *iuser = CTX_data_pointer_get_type(C, "edit_image_user", &RNA_ImageUser).data; + Image *ima = image_from_context(C); + ImageUser *iuser = image_user_from_context(C); if (!ima || !iuser) { /* Try to get a Texture, or a SpaceImage from context... */ - SpaceImage *sima = CTX_wm_space_image(C); Tex *tex = CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data; if (tex && tex->type == TEX_IMAGE) { ima = tex->ima; iuser = &tex->iuser; } - else if (sima) { - ima = sima->image; - iuser = &sima->iuser; - } } if (!ima || !iuser || !BKE_image_has_anim(ima)) { @@ -1795,14 +1825,11 @@ static void image_save_options_to_op(ImageSaveOptions *opts, wmOperator *op) RNA_string_set(op->ptr, "filepath", opts->filepath); } -static bool save_image_op(const bContext *C, - SpaceImage *sima, - wmOperator *op, - ImageSaveOptions *opts) +static bool save_image_op(const bContext *C, wmOperator *op, ImageSaveOptions *opts) { Main *bmain = CTX_data_main(C); - Image *ima = ED_space_image(sima); - ImageUser *iuser = &sima->iuser; + Image *ima = image_from_context(C); + ImageUser *iuser = image_user_from_context(C); opts->relative = (RNA_struct_find_property(op->ptr, "relative_path") && RNA_boolean_get(op->ptr, "relative_path")); @@ -1840,19 +1867,20 @@ static int image_save_as_exec(bContext *C, wmOperator *op) { Main *bmain = CTX_data_main(C); Scene *scene = CTX_data_scene(C); - SpaceImage *sima = CTX_wm_space_image(C); + Image *image = image_from_context(C); + ImageUser *iuser = image_user_from_context(C); ImageSaveOptions opts; BKE_image_save_options_init(&opts, bmain, scene); /* just in case to initialize values, * these should be set on invoke or by the caller. */ - image_save_options_init(bmain, &opts, sima->image, &sima->iuser, false, false); + image_save_options_init(bmain, &opts, image, iuser, false, false); image_save_options_from_op(bmain, &opts, op); opts.do_newpath = true; - save_image_op(C, sima, op, &opts); + save_image_op(C, op, &opts); image_save_as_free(op); return OPERATOR_FINISHED; @@ -1867,8 +1895,8 @@ static bool image_save_as_check(bContext *UNUSED(C), wmOperator *op) static int image_save_as_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event)) { Main *bmain = CTX_data_main(C); - SpaceImage *sima = CTX_wm_space_image(C); - Image *ima = ED_space_image(sima); + Image *ima = image_from_context(C); + ImageUser *iuser = image_user_from_context(C); Scene *scene = CTX_data_scene(C); ImageSaveOptions opts; PropertyRNA *prop; @@ -1881,7 +1909,7 @@ static int image_save_as_invoke(bContext *C, wmOperator *op, const wmEvent *UNUS BKE_image_save_options_init(&opts, bmain, scene); - if (image_save_options_init(bmain, &opts, ima, &sima->iuser, true, save_as_render) == 0) { + if (image_save_options_init(bmain, &opts, ima, iuser, true, save_as_render) == 0) { return OPERATOR_CANCELLED; } image_save_options_to_op(&opts, op); @@ -1949,20 +1977,21 @@ static void image_save_as_draw(bContext *UNUSED(C), wmOperator *op) static bool image_save_as_poll(bContext *C) { - if (space_image_buffer_exists_poll(C)) { - if (G.is_rendering) { - /* no need to NULL check here */ - SpaceImage *sima = CTX_wm_space_image(C); - Image *ima = ED_space_image(sima); + if (!image_buffer_exists_from_context(C)) { + return false; + } - if (ima->source == IMA_SRC_VIEWER) { - CTX_wm_operator_poll_msg_set(C, "can't save image while rendering"); - return false; - } + if (G.is_rendering) { + /* no need to NULL check here */ + Image *ima = image_from_context(C); + + if (ima->source == IMA_SRC_VIEWER) { + CTX_wm_operator_poll_msg_set(C, "can't save image while rendering"); + return false; } - return true; } - return false; + + return true; } void IMAGE_OT_save_as(wmOperatorType *ot) @@ -2009,18 +2038,19 @@ void IMAGE_OT_save_as(wmOperatorType *ot) static int image_save_exec(bContext *C, wmOperator *op) { Main *bmain = CTX_data_main(C); - SpaceImage *sima = CTX_wm_space_image(C); + Image *image = image_from_context(C); + ImageUser *iuser = image_user_from_context(C); Scene *scene = CTX_data_scene(C); ImageSaveOptions opts; BKE_image_save_options_init(&opts, bmain, scene); - if (image_save_options_init(bmain, &opts, sima->image, &sima->iuser, false, false) == 0) { + if (image_save_options_init(bmain, &opts, image, iuser, false, false) == 0) { return OPERATOR_CANCELLED; } image_save_options_from_op(bmain, &opts, op); if (BLI_exists(opts.filepath) && BLI_file_is_writable(opts.filepath)) { - if (save_image_op(C, sima, op, &opts)) { + if (save_image_op(C, op, &opts)) { /* report since this can be called from key-shortcuts */ BKE_reportf(op->reports, RPT_INFO, "Saved Image '%s'", opts.filepath); } @@ -2054,30 +2084,30 @@ void IMAGE_OT_save(wmOperatorType *ot) static int image_save_sequence_exec(bContext *C, wmOperator *op) { Main *bmain = CTX_data_main(C); - SpaceImage *sima = CTX_wm_space_image(C); + Image *image = image_from_context(C); ImBuf *ibuf, *first_ibuf = NULL; int tot = 0; char di[FILE_MAX]; struct MovieCacheIter *iter; - if (sima->image == NULL) { + if (image == NULL) { return OPERATOR_CANCELLED; } - if (sima->image->source != IMA_SRC_SEQUENCE) { + if (image->source != IMA_SRC_SEQUENCE) { BKE_report(op->reports, RPT_ERROR, "Can only save sequence on image sequences"); return OPERATOR_CANCELLED; } - if (sima->image->type == IMA_TYPE_MULTILAYER) { + if (image->type == IMA_TYPE_MULTILAYER) { BKE_report(op->reports, RPT_ERROR, "Cannot save multilayer sequences"); return OPERATOR_CANCELLED; } /* get total dirty buffers and first dirty buffer which is used for menu */ ibuf = NULL; - if (sima->image->cache != NULL) { - iter = IMB_moviecacheIter_new(sima->image->cache); + if (image->cache != NULL) { + iter = IMB_moviecacheIter_new(image->cache); while (!IMB_moviecacheIter_done(iter)) { ibuf = IMB_moviecacheIter_getImBuf(iter); if (ibuf->userflags & IB_BITMAPDIRTY) { @@ -2100,7 +2130,7 @@ static int image_save_sequence_exec(bContext *C, wmOperator *op) BLI_split_dir_part(first_ibuf->name, di, sizeof(di)); BKE_reportf(op->reports, RPT_INFO, "%d image(s) will be saved in %s", tot, di); - iter = IMB_moviecacheIter_new(sima->image->cache); + iter = IMB_moviecacheIter_new(image->cache); while (!IMB_moviecacheIter_done(iter)) { ibuf = IMB_moviecacheIter_getImBuf(iter); @@ -2135,7 +2165,7 @@ void IMAGE_OT_save_sequence(wmOperatorType *ot) /* api callbacks */ ot->exec = image_save_sequence_exec; - ot->poll = space_image_buffer_exists_poll; + ot->poll = image_buffer_exists_from_context; /* flags */ ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; @@ -2277,8 +2307,8 @@ void IMAGE_OT_save_all_modified(wmOperatorType *ot) static int image_reload_exec(bContext *C, wmOperator *UNUSED(op)) { Main *bmain = CTX_data_main(C); - Image *ima = CTX_data_edit_image(C); - SpaceImage *sima = CTX_wm_space_image(C); + Image *ima = image_from_context(C); + ImageUser *iuser = image_user_from_context(C); if (!ima) { return OPERATOR_CANCELLED; @@ -2287,7 +2317,7 @@ static int image_reload_exec(bContext *C, wmOperator *UNUSED(op)) /* XXX unpackImage frees image buffers */ ED_preview_kill_jobs(CTX_wm_manager(C), CTX_data_main(C)); - BKE_image_signal(bmain, ima, (sima) ? &sima->iuser : NULL, IMA_SIGNAL_RELOAD); + BKE_image_signal(bmain, ima, iuser, IMA_SIGNAL_RELOAD); DEG_id_tag_update(&ima->id, 0); WM_event_add_notifier(C, NC_IMAGE | NA_EDITED, ima); @@ -2529,14 +2559,14 @@ void IMAGE_OT_new(wmOperatorType *ot) static bool image_invert_poll(bContext *C) { - Image *ima = CTX_data_edit_image(C); + Image *ima = image_from_context(C); return BKE_image_has_ibuf(ima, NULL); } static int image_invert_exec(bContext *C, wmOperator *op) { - Image *ima = CTX_data_edit_image(C); + Image *ima = image_from_context(C); ImBuf *ibuf = BKE_image_acquire_ibuf(ima, NULL, NULL); SpaceImage *sima = CTX_wm_space_image(C); /* undo is supported only on image paint mode currently */ @@ -2661,7 +2691,7 @@ void IMAGE_OT_invert(wmOperatorType *ot) static bool image_pack_test(bContext *C, wmOperator *op) { - Image *ima = CTX_data_edit_image(C); + Image *ima = image_from_context(C); if (!ima) { return 0; @@ -2678,7 +2708,7 @@ static bool image_pack_test(bContext *C, wmOperator *op) static int image_pack_exec(bContext *C, wmOperator *op) { struct Main *bmain = CTX_data_main(C); - Image *ima = CTX_data_edit_image(C); + Image *ima = image_from_context(C); if (!image_pack_test(C, op)) { return OPERATOR_CANCELLED; @@ -2714,7 +2744,7 @@ void IMAGE_OT_pack(wmOperatorType *ot) static int image_unpack_exec(bContext *C, wmOperator *op) { - Image *ima = CTX_data_edit_image(C); + Image *ima = image_from_context(C); int method = RNA_enum_get(op->ptr, "method"); /* find the suppplied image by name */ @@ -2723,7 +2753,7 @@ static int image_unpack_exec(bContext *C, wmOperator *op) RNA_string_get(op->ptr, "id", imaname); ima = BLI_findstring(&CTX_data_main(C)->images, imaname, offsetof(ID, name) + 2); if (!ima) { - ima = CTX_data_edit_image(C); + ima = image_from_context(C); } } @@ -2754,7 +2784,7 @@ static int image_unpack_exec(bContext *C, wmOperator *op) static int image_unpack_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event)) { - Image *ima = CTX_data_edit_image(C); + Image *ima = image_from_context(C); if (RNA_struct_property_is_set(op->ptr, "id")) { return image_unpack_exec(C, op); @@ -3345,14 +3375,14 @@ void IMAGE_OT_curves_point_set(wmOperatorType *ot) static bool image_cycle_render_slot_poll(bContext *C) { - Image *ima = CTX_data_edit_image(C); + Image *ima = image_from_context(C); return (ima && ima->type == IMA_TYPE_R_RESULT); } static int image_cycle_render_slot_exec(bContext *C, wmOperator *op) { - Image *ima = CTX_data_edit_image(C); + Image *ima = image_from_context(C); const int direction = RNA_boolean_get(op->ptr, "reverse") ? -1 : 1; if (!ED_image_slot_cycle(ima, direction)) { @@ -3392,7 +3422,7 @@ void IMAGE_OT_cycle_render_slot(wmOperatorType *ot) static int image_clear_render_slot_exec(bContext *C, wmOperator *UNUSED(op)) { SpaceImage *sima = CTX_wm_space_image(C); - Image *ima = CTX_data_edit_image(C); + Image *ima = image_from_context(C); if (!BKE_image_clear_renderslot(ima, &sima->iuser, ima->render_slot)) { return OPERATOR_CANCELLED; @@ -3422,7 +3452,7 @@ void IMAGE_OT_clear_render_slot(wmOperatorType *ot) static int image_add_render_slot_exec(bContext *C, wmOperator *UNUSED(op)) { - Image *ima = CTX_data_edit_image(C); + Image *ima = image_from_context(C); RenderSlot *slot = BKE_image_add_renderslot(ima, NULL); ima->render_slot = BLI_findindex(&ima->renderslots, slot); @@ -3452,7 +3482,7 @@ void IMAGE_OT_add_render_slot(wmOperatorType *ot) static int image_remove_render_slot_exec(bContext *C, wmOperator *UNUSED(op)) { SpaceImage *sima = CTX_wm_space_image(C); - Image *ima = CTX_data_edit_image(C); + Image *ima = image_from_context(C); if (!BKE_image_remove_renderslot(ima, &sima->iuser, ima->render_slot)) { return OPERATOR_CANCELLED; |