Blender  V2.59
info_ops.c
Go to the documentation of this file.
00001 /*
00002  * $Id: info_ops.c 35853 2011-03-28 17:08:33Z campbellbarton $
00003  *
00004  * ***** BEGIN GPL LICENSE BLOCK *****
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version. 
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software Foundation,
00018  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  *
00020  * The Original Code is Copyright (C) 2008 Blender Foundation.
00021  * All rights reserved.
00022  *
00023  * 
00024  * Contributor(s): Blender Foundation
00025  *
00026  * ***** END GPL LICENSE BLOCK *****
00027  */
00028 
00034 #include <string.h>
00035 #include <stdio.h>
00036 
00037 #include "DNA_packedFile_types.h"
00038 #include "DNA_space_types.h"
00039 #include "DNA_windowmanager_types.h"
00040 
00041 #include "MEM_guardedalloc.h"
00042 
00043 #include "BLI_blenlib.h"
00044 #include "BLI_math.h"
00045 #include "BLI_bpath.h"
00046 #include "BLI_utildefines.h"
00047 
00048 #include "BKE_context.h"
00049 #include "BKE_global.h"
00050 #include "BKE_image.h"
00051 #include "BKE_main.h"
00052 #include "BKE_packedFile.h"
00053 #include "BKE_report.h"
00054 
00055 
00056 #include "WM_api.h"
00057 #include "WM_types.h"
00058 
00059 
00060 #include "UI_interface.h"
00061 #include "UI_resources.h"
00062 
00063 #include "IMB_imbuf_types.h"
00064 
00065 #include "RNA_access.h"
00066 #include "RNA_define.h"
00067 
00068 
00069 #include "info_intern.h"
00070 
00071 /********************* pack all operator *********************/
00072 
00073 static int pack_all_exec(bContext *C, wmOperator *op)
00074 {
00075         Main *bmain= CTX_data_main(C);
00076 
00077         packAll(bmain, op->reports);
00078         G.fileflags |= G_AUTOPACK;
00079 
00080         return OPERATOR_FINISHED;
00081 }
00082 
00083 static int pack_all_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
00084 {
00085         Main *bmain= CTX_data_main(C);
00086         Image *ima;
00087         ImBuf *ibuf;
00088 
00089         // first check for dirty images
00090         for(ima=bmain->image.first; ima; ima=ima->id.next) {
00091                 if(ima->ibufs.first) { /* XXX FIX */
00092                         ibuf= BKE_image_get_ibuf(ima, NULL);
00093                         
00094                         if(ibuf && (ibuf->userflags & IB_BITMAPDIRTY))
00095                                 break;
00096                 }
00097         }
00098 
00099         if(ima) {
00100                 uiPupMenuOkee(C, "FILE_OT_pack_all", "Some images are painted on. These changes will be lost. Continue?");
00101                 return OPERATOR_CANCELLED;
00102         }
00103 
00104         return pack_all_exec(C, op);
00105 }
00106 
00107 void FILE_OT_pack_all(wmOperatorType *ot)
00108 {
00109         /* identifiers */
00110         ot->name= "Pack All";
00111         ot->idname= "FILE_OT_pack_all";
00112         
00113         /* api callbacks */
00114         ot->exec= pack_all_exec;
00115         ot->invoke= pack_all_invoke;
00116 
00117         /* flags */
00118         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
00119 }
00120 
00121 /********************* unpack all operator *********************/
00122 
00123 static const EnumPropertyItem unpack_all_method_items[] = {
00124         {PF_USE_LOCAL, "USE_LOCAL", 0, "Use files in current directory (create when necessary)", ""},
00125         {PF_WRITE_LOCAL, "WRITE_LOCAL", 0, "Write files to current directory (overwrite existing files)", ""},
00126         {PF_USE_ORIGINAL, "USE_ORIGINAL", 0, "Use files in original location (create when necessary)", ""},
00127         {PF_WRITE_ORIGINAL, "WRITE_ORIGINAL", 0, "Write files to original location (overwrite existing files)", ""},
00128         {PF_KEEP, "KEEP", 0, "Disable AutoPack, keep all packed files", ""},
00129         {PF_ASK, "ASK", 0, "Ask for each file", ""},
00130         {0, NULL, 0, NULL, NULL}};
00131 
00132 static int unpack_all_exec(bContext *C, wmOperator *op)
00133 {
00134         Main *bmain= CTX_data_main(C);
00135         int method= RNA_enum_get(op->ptr, "method");
00136 
00137         if(method != PF_KEEP) unpackAll(bmain, op->reports, method); /* XXX PF_ASK can't work here */
00138         G.fileflags &= ~G_AUTOPACK;
00139 
00140         return OPERATOR_FINISHED;
00141 }
00142 
00143 static int unpack_all_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
00144 {
00145         Main *bmain= CTX_data_main(C);
00146         uiPopupMenu *pup;
00147         uiLayout *layout;
00148         char title[128];
00149         int count = 0;
00150         
00151         count = countPackedFiles(bmain);
00152         
00153         if(!count) {
00154                 BKE_report(op->reports, RPT_WARNING, "No packed files. Autopack disabled.");
00155                 G.fileflags &= ~G_AUTOPACK;
00156                 return OPERATOR_CANCELLED;
00157         }
00158 
00159         if(count == 1)
00160                 sprintf(title, "Unpack 1 file");
00161         else
00162                 sprintf(title, "Unpack %d files", count);
00163         
00164         pup= uiPupMenuBegin(C, title, ICON_NONE);
00165         layout= uiPupMenuLayout(pup);
00166 
00167         uiLayoutSetOperatorContext(layout, WM_OP_EXEC_DEFAULT);
00168         uiItemsEnumO(layout, "FILE_OT_unpack_all", "method");
00169 
00170         uiPupMenuEnd(C, pup);
00171 
00172         return OPERATOR_CANCELLED;
00173 }
00174 
00175 void FILE_OT_unpack_all(wmOperatorType *ot)
00176 {
00177         /* identifiers */
00178         ot->name= "Unpack All";
00179         ot->idname= "FILE_OT_unpack_all";
00180         
00181         /* api callbacks */
00182         ot->exec= unpack_all_exec;
00183         ot->invoke= unpack_all_invoke;
00184 
00185         /* flags */
00186         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
00187 
00188         /* properties */
00189         RNA_def_enum(ot->srna, "method", unpack_all_method_items, PF_USE_LOCAL, "Method", "How to unpack.");
00190 }
00191 
00192 /********************* make paths relative operator *********************/
00193 
00194 static int make_paths_relative_exec(bContext *C, wmOperator *op)
00195 {
00196         Main *bmain= CTX_data_main(C);
00197 
00198         if(!G.relbase_valid) {
00199                 BKE_report(op->reports, RPT_WARNING, "Can't set relative paths with an unsaved blend file.");
00200                 return OPERATOR_CANCELLED;
00201         }
00202 
00203         makeFilesRelative(bmain, bmain->name, op->reports);
00204 
00205         /* redraw everything so any changed paths register */
00206         WM_main_add_notifier(NC_WINDOW, NULL);
00207 
00208         return OPERATOR_FINISHED;
00209 }
00210 
00211 void FILE_OT_make_paths_relative(wmOperatorType *ot)
00212 {
00213         /* identifiers */
00214         ot->name= "Make All Paths Relative";
00215         ot->idname= "FILE_OT_make_paths_relative";
00216         
00217         /* api callbacks */
00218         ot->exec= make_paths_relative_exec;
00219 
00220         /* flags */
00221         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
00222 }
00223 
00224 /********************* make paths absolute operator *********************/
00225 
00226 static int make_paths_absolute_exec(bContext *C, wmOperator *op)
00227 {
00228         Main *bmain= CTX_data_main(C);
00229 
00230         if(!G.relbase_valid) {
00231                 BKE_report(op->reports, RPT_WARNING, "Can't set absolute paths with an unsaved blend file.");
00232                 return OPERATOR_CANCELLED;
00233         }
00234 
00235         makeFilesAbsolute(bmain, bmain->name, op->reports);
00236 
00237         /* redraw everything so any changed paths register */
00238         WM_main_add_notifier(NC_WINDOW, NULL);
00239 
00240         return OPERATOR_FINISHED;
00241 }
00242 
00243 void FILE_OT_make_paths_absolute(wmOperatorType *ot)
00244 {
00245         /* identifiers */
00246         ot->name= "Make All Paths Absolute";
00247         ot->idname= "FILE_OT_make_paths_absolute";
00248         
00249         /* api callbacks */
00250         ot->exec= make_paths_absolute_exec;
00251 
00252         /* flags */
00253         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
00254 }
00255 
00256 /********************* report missing files operator *********************/
00257 
00258 static int report_missing_files_exec(bContext *UNUSED(C), wmOperator *op)
00259 {
00260         /* run the missing file check */
00261         checkMissingFiles(G.main, op->reports);
00262         
00263         return OPERATOR_FINISHED;
00264 }
00265 
00266 void FILE_OT_report_missing_files(wmOperatorType *ot)
00267 {
00268         /* identifiers */
00269         ot->name= "Report Missing Files";
00270         ot->idname= "FILE_OT_report_missing_files";
00271         
00272         /* api callbacks */
00273         ot->exec= report_missing_files_exec;
00274 
00275         /* flags */
00276         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
00277 }
00278 
00279 /********************* find missing files operator *********************/
00280 
00281 static int find_missing_files_exec(bContext *UNUSED(C), wmOperator *op)
00282 {
00283         char *path;
00284         
00285         path= RNA_string_get_alloc(op->ptr, "filepath", NULL, 0);
00286         findMissingFiles(G.main, path);
00287         MEM_freeN(path);
00288 
00289         return OPERATOR_FINISHED;
00290 }
00291 
00292 static int find_missing_files_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
00293 {
00294         /* XXX file open button text "Find Missing Files" */
00295         WM_event_add_fileselect(C, op); 
00296         return OPERATOR_RUNNING_MODAL;
00297 }
00298 
00299 void FILE_OT_find_missing_files(wmOperatorType *ot)
00300 {
00301         /* identifiers */
00302         ot->name= "Find Missing Files";
00303         ot->idname= "FILE_OT_find_missing_files";
00304         
00305         /* api callbacks */
00306         ot->exec= find_missing_files_exec;
00307         ot->invoke= find_missing_files_invoke;
00308 
00309         /* flags */
00310         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
00311 
00312         /* properties */
00313         WM_operator_properties_filesel(ot, 0, FILE_SPECIAL, FILE_OPENFILE, WM_FILESEL_FILEPATH);
00314 }
00315 
00316 /********************* report box operator *********************/
00317 
00318 /* Hard to decide whether to keep this as an operator, 
00319  * or turn it into a hardcoded ui control feature, 
00320  * handling TIMER events for all regions in interface_handlers.c
00321  * Not sure how good that is to be accessing UI data from 
00322  * inactive regions, so use this for now. --matt
00323  */
00324 
00325 #define INFO_TIMEOUT            5.0f
00326 #define INFO_COLOR_TIMEOUT      3.0f
00327 #define ERROR_TIMEOUT           10.0f
00328 #define ERROR_COLOR_TIMEOUT     6.0f
00329 #define COLLAPSE_TIMEOUT        0.25f
00330 static int update_reports_display_invoke(bContext *C, wmOperator *UNUSED(op), wmEvent *event)
00331 {
00332         wmWindowManager *wm= CTX_wm_manager(C);
00333         ReportList *reports= CTX_wm_reports(C);
00334         Report *report;
00335         ReportTimerInfo *rti;
00336         float progress=0.0, color_progress=0.0;
00337         float neutral_col[3] = {0.35, 0.35, 0.35};
00338         float neutral_grey= 0.6;
00339         float timeout=0.0, color_timeout=0.0;
00340         int send_note= 0;
00341         
00342         /* escape if not our timer */
00343         if(             (reports->reporttimer==NULL) ||
00344                         (reports->reporttimer != event->customdata) ||
00345                         ((report= BKE_reports_last_displayable(reports))==NULL) /* may have been deleted */
00346         ) {
00347                 return OPERATOR_PASS_THROUGH;
00348         }
00349 
00350         rti = (ReportTimerInfo *)reports->reporttimer->customdata;
00351         
00352         timeout = (report->type & RPT_ERROR_ALL)?ERROR_TIMEOUT:INFO_TIMEOUT;
00353         color_timeout = (report->type & RPT_ERROR_ALL)?ERROR_COLOR_TIMEOUT:INFO_COLOR_TIMEOUT;
00354         
00355         /* clear the report display after timeout */
00356         if ((float)reports->reporttimer->duration > timeout) {
00357                 WM_event_remove_timer(wm, NULL, reports->reporttimer);
00358                 reports->reporttimer = NULL;
00359                 
00360                 WM_event_add_notifier(C, NC_SPACE|ND_SPACE_INFO, NULL);
00361                 
00362                 return (OPERATOR_FINISHED|OPERATOR_PASS_THROUGH);
00363         }
00364 
00365         if (rti->widthfac == 0.0f) {
00366                 /* initialise colors based on report type */
00367                 if(report->type & RPT_ERROR_ALL) {
00368                         rti->col[0] = 1.0;
00369                         rti->col[1] = 0.2;
00370                         rti->col[2] = 0.0;
00371                 } else if(report->type & RPT_WARNING_ALL) {
00372                         rti->col[0] = 1.0;
00373                         rti->col[1] = 1.0;
00374                         rti->col[2] = 0.0;
00375                 } else if(report->type & RPT_INFO_ALL) {
00376                         rti->col[0] = 0.3;
00377                         rti->col[1] = 0.45;
00378                         rti->col[2] = 0.7;
00379                 }
00380                 rti->greyscale = 0.75;
00381                 rti->widthfac=1.0;
00382         }
00383         
00384         progress = (float)reports->reporttimer->duration / timeout;
00385         color_progress = (float)reports->reporttimer->duration / color_timeout;
00386         
00387         /* save us from too many draws */
00388         if(color_progress <= 1.0f) {
00389                 send_note= 1;
00390                 
00391                 /* fade colors out sharply according to progress through fade-out duration */
00392                 interp_v3_v3v3(rti->col, rti->col, neutral_col, color_progress);
00393                 rti->greyscale = interpf(neutral_grey, rti->greyscale, color_progress);
00394         }
00395 
00396         /* collapse report at end of timeout */
00397         if (progress*timeout > timeout - COLLAPSE_TIMEOUT) {
00398                 rti->widthfac = (progress*timeout - (timeout - COLLAPSE_TIMEOUT)) / COLLAPSE_TIMEOUT;
00399                 rti->widthfac = 1.0f - rti->widthfac;
00400                 send_note= 1;
00401         }
00402         
00403         if(send_note) {
00404                 WM_event_add_notifier(C, NC_SPACE|ND_SPACE_INFO, NULL);
00405         }
00406         
00407         return (OPERATOR_FINISHED|OPERATOR_PASS_THROUGH);
00408 }
00409 
00410 void INFO_OT_reports_display_update(wmOperatorType *ot)
00411 {
00412         /* identifiers */
00413         ot->name= "Update Reports Display";
00414         ot->idname= "INFO_OT_reports_display_update";
00415         
00416         /* api callbacks */
00417         ot->invoke= update_reports_display_invoke;
00418         
00419         /* flags */
00420         ot->flag= 0;
00421         
00422         /* properties */
00423 }
00424 
00425 /* report operators */