Images: more tweaks to save all modified images
authorBrecht Van Lommel <brechtvanlommel@gmail.com>
Thu, 16 May 2019 17:43:41 +0000 (19:43 +0200)
committerBrecht Van Lommel <brechtvanlommel@gmail.com>
Thu, 16 May 2019 17:58:52 +0000 (19:58 +0200)
Support showing warning messages before saving for cases that we can detect
in advance, to be used by quit dialog.

source/blender/editors/include/ED_image.h
source/blender/editors/space_image/image_ops.c

index a9be959..affe98e 100644 (file)
@@ -111,7 +111,7 @@ void ED_image_draw_info(struct Scene *scene,
 
 bool ED_space_image_show_cache(struct SpaceImage *sima);
 
-int ED_image_save_all_modified_count(const struct bContext *C);
+int ED_image_save_all_modified_info(const struct bContext *C, struct ReportList *reports);
 bool ED_image_save_all_modified(const struct bContext *C, struct ReportList *reports);
 
 #endif /* __ED_IMAGE_H__ */
index 3dd7dd2..a25ba6f 100644 (file)
@@ -2161,32 +2161,21 @@ void IMAGE_OT_save_sequence(wmOperatorType *ot)
 
 /********************** save all operator **********************/
 
-int ED_image_save_all_modified_count(const bContext *C)
-{
-  Main *bmain = CTX_data_main(C);
-  int num_files = 0;
-
-  for (Image *ima = bmain->images.first; ima; ima = ima->id.next) {
-    if (ELEM(ima->type, IMA_TYPE_R_RESULT, IMA_TYPE_COMPOSITE)) {
-      continue;
-    }
-    else if (BKE_image_is_dirty(ima)) {
-      if (ima->source == IMA_SRC_FILE && !BKE_image_has_packedfile(ima)) {
-        num_files++;
-      }
-    }
-  }
-
-  return num_files;
-}
-
-bool ED_image_save_all_modified(const bContext *C, ReportList *reports)
+static int image_save_all_modified(const bContext *C,
+                                   ReportList *reports,
+                                   int *num_files,
+                                   const bool dry_run,
+                                   const bool ignore_dry_run_warnings)
 {
   Main *bmain = CTX_data_main(C);
   Scene *scene = CTX_data_scene(C);
   GSet *unique_paths = BLI_gset_str_new(__func__);
   bool ok = true;
 
+  if (num_files) {
+    *num_files = 0;
+  }
+
   for (Image *ima = bmain->images.first; ima; ima = ima->id.next) {
     if (ELEM(ima->type, IMA_TYPE_R_RESULT, IMA_TYPE_COMPOSITE)) {
       /* Don't save render results automatically. */
@@ -2195,15 +2184,22 @@ bool ED_image_save_all_modified(const bContext *C, ReportList *reports)
       if (BKE_image_has_packedfile(ima)) {
         if (ima->id.lib == NULL) {
           /* Re-pack. */
-          BKE_image_memorypack(ima);
+          if (!dry_run) {
+            BKE_image_memorypack(ima);
+          }
+
+          if (num_files) {
+            (*num_files)++;
+          }
         }
-        else {
+        else if (!ignore_dry_run_warnings) {
           /* Can't pack to library data. */
           BKE_reportf(reports,
-                      RPT_ERROR,
+                      RPT_WARNING,
                       "Packed library image: %s from library %s can't be saved",
                       ima->id.name,
                       ima->id.lib->name);
+          ok = false;
         }
       }
       else {
@@ -2217,28 +2213,36 @@ bool ED_image_save_all_modified(const bContext *C, ReportList *reports)
 
           if (image_save_options_init(bmain, &opts, ima, NULL, false, false)) {
             if (!BLI_gset_haskey(unique_paths, opts.filepath)) {
-              const bool save_ok = BKE_image_save(reports, bmain, ima, NULL, &opts);
+              if (!dry_run) {
+                const bool save_ok = BKE_image_save(reports, bmain, ima, NULL, &opts);
+
+                if (save_ok) {
+                  BLI_gset_insert(unique_paths, BLI_strdup(opts.filepath));
+                }
 
-              if (save_ok) {
-                BLI_gset_insert(unique_paths, BLI_strdup(opts.filepath));
+                ok = ok && save_ok;
               }
 
-              ok = ok && save_ok;
+              if (num_files) {
+                (*num_files)++;
+              }
             }
-            else {
+            else if (!ignore_dry_run_warnings) {
               BKE_reportf(reports,
                           RPT_WARNING,
                           "File path used by more than one saved image: %s",
                           opts.filepath);
+              ok = false;
             }
           }
         }
-        else {
+        else if (!ignore_dry_run_warnings) {
           BKE_reportf(reports,
-                      RPT_ERROR,
+                      RPT_WARNING,
                       "Image %s can't be saved, no valid file path: %s",
                       ima->id.name,
                       ima->name);
+          ok = false;
         }
       }
     }
@@ -2249,15 +2253,35 @@ bool ED_image_save_all_modified(const bContext *C, ReportList *reports)
   return ok;
 }
 
-static int image_save_all_modified_exec(bContext *C, wmOperator *op)
+int ED_image_save_all_modified_info(const bContext *C, ReportList *reports)
 {
-  ED_image_save_all_modified(C, op->reports);
-  return OPERATOR_FINISHED;
+  /* Dry run to get number of files, and any warnings we can detect in advance. */
+  int num_files;
+  image_save_all_modified(C, reports, &num_files, true, false);
+  return num_files;
+}
+
+bool ED_image_save_all_modified(const bContext *C, ReportList *reports)
+{
+  /* Save, and ignore any warnings that we already detected in
+   * ED_image_save_all_modified_info. */
+  return image_save_all_modified(C, reports, NULL, false, true);
 }
 
 static bool image_save_all_modified_poll(bContext *C)
 {
-  return (ED_image_save_all_modified_count(C) > 0);
+  /* Let operator run if there are any files to saved, or any warnings to
+   * report about files that we can't save. */
+  int num_files;
+  bool ok = image_save_all_modified(C, NULL, &num_files, true, false);
+  return (num_files > 0) || !ok;
+}
+
+static int image_save_all_modified_exec(bContext *C, wmOperator *op)
+{
+  /* Save, and show all warnings. */
+  image_save_all_modified(C, op->reports, NULL, false, false);
+  return OPERATOR_FINISHED;
 }
 
 void IMAGE_OT_save_all_modified(wmOperatorType *ot)