Changeset View
Changeset View
Standalone View
Standalone View
source/blender/editors/space_image/image_ops.c
| Context not available. | |||||
| /** \name Save Image Operator | /** \name Save Image Operator | ||||
| * \{ */ | * \{ */ | ||||
| static bool image_file_path_saveable(bContext *C, Image *ima, ImageUser *iuser) | static bool image_file_format_writable(Image *ima, ImageUser *iuser) | ||||
| { | { | ||||
| /* Can always repack images. */ | |||||
| if (BKE_image_has_packedfile(ima)) { | |||||
| return true; | |||||
| } | |||||
| /* Test for valid filepath. */ | |||||
| void *lock; | void *lock; | ||||
| ImBuf *ibuf = BKE_image_acquire_ibuf(ima, iuser, &lock); | ImBuf *ibuf = BKE_image_acquire_ibuf(ima, iuser, &lock); | ||||
| bool ret = false; | bool ret = false; | ||||
| if (ibuf) { | if (ibuf && BKE_image_buffer_format_writable(ibuf)) { | ||||
| Main *bmain = CTX_data_main(C); | ret = true; | ||||
| char name[FILE_MAX]; | |||||
| BLI_strncpy(name, ibuf->name, FILE_MAX); | |||||
| BLI_path_abs(name, BKE_main_blendfile_path(bmain)); | |||||
| 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 (!BKE_image_buffer_format_writable(ibuf)) { | |||||
| CTX_wm_operator_poll_msg_set(C, "image format is read-only"); | |||||
| } | |||||
| else { | |||||
| ret = true; | |||||
| } | |||||
| } | } | ||||
| BKE_image_release_ibuf(ima, ibuf, lock); | BKE_image_release_ibuf(ima, ibuf, lock); | ||||
| Context not available. | |||||
| return false; | return false; | ||||
| } | } | ||||
| Image *ima = image_from_context(C); | /* Check if there is a valid file path and image format we can write | ||||
| ImageUser *iuser = image_user_from_context(C); | * outside of the 'poll' so we can show a report with a pop-up. */ | ||||
| /* Images without a filepath will go to save as. */ | |||||
| if (!BKE_image_has_filepath(ima)) { | |||||
| return true; | |||||
| } | |||||
| /* Check if there is a valid file path and image format we can write. */ | /* Can always repack images. | ||||
| return image_file_path_saveable(C, ima, iuser); | * Images without a filepath will go to "Save As". */ | ||||
| return true; | |||||
| } | } | ||||
| static int image_save_exec(bContext *C, wmOperator *op) | static int image_save_exec(bContext *C, wmOperator *op) | ||||
| Context not available. | |||||
| if (BKE_image_has_packedfile(image)) { | if (BKE_image_has_packedfile(image)) { | ||||
| /* Save packed files to memory. */ | /* Save packed files to memory. */ | ||||
| BKE_image_memorypack(image); | BKE_image_memorypack(image); | ||||
| /* Report since this can be called from key shortcuts. */ | |||||
| BKE_reportf(op->reports, RPT_INFO, "Packed to memory image \"%s\"", image->name); | |||||
| return OPERATOR_FINISHED; | return OPERATOR_FINISHED; | ||||
| } | } | ||||
| Context not available. | |||||
| } | } | ||||
| image_save_options_from_op(bmain, &opts, op); | image_save_options_from_op(bmain, &opts, op); | ||||
| if (BLI_exists(opts.filepath) && BLI_file_is_writable(opts.filepath)) { | /* Check if file write permission is ok. */ | ||||
| if (save_image_op(C, op, &opts)) { | if (BLI_exists(opts.filepath) && !BLI_file_is_writable(opts.filepath)) { | ||||
| /* report since this can be called from key-shortcuts */ | |||||
| BKE_reportf(op->reports, RPT_INFO, "Saved Image '%s'", opts.filepath); | |||||
| } | |||||
| } | |||||
| else { | |||||
| BKE_reportf( | BKE_reportf( | ||||
| op->reports, RPT_ERROR, "Cannot save image, path '%s' is not writable", opts.filepath); | op->reports, RPT_ERROR, "Cannot save image, path \"%s\" is not writable", opts.filepath); | ||||
| return OPERATOR_CANCELLED; | return OPERATOR_CANCELLED; | ||||
| } | } | ||||
| 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); | |||||
| } | |||||
| return OPERATOR_FINISHED; | return OPERATOR_FINISHED; | ||||
| } | } | ||||
| static int image_save_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event)) | static int image_save_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event)) | ||||
| { | { | ||||
| Image *ima = image_from_context(C); | Image *ima = image_from_context(C); | ||||
| ImageUser *iuser = image_user_from_context(C); | |||||
| if (!BKE_image_has_packedfile(ima) && !BKE_image_has_filepath(ima)) { | /* Not writable formats or images without a filepath will go to "Save As". */ | ||||
| if (!BKE_image_has_packedfile(ima) && | |||||
| (!BKE_image_has_filepath(ima) || !image_file_format_writable(ima, iuser))) { | |||||
| WM_operator_name_call(C, "IMAGE_OT_save_as", WM_OP_INVOKE_DEFAULT, NULL); | WM_operator_name_call(C, "IMAGE_OT_save_as", WM_OP_INVOKE_DEFAULT, NULL); | ||||
| return OPERATOR_CANCELLED; | return OPERATOR_CANCELLED; | ||||
| } | } | ||||
| Context not available. | |||||