作図ソフト dia の改良版
Revision | 8f4005ff0481d7205c0b69fce8c84dfdd044a092 (tree) |
---|---|
Zeit | 2014-09-26 04:20:11 |
Autor | Hans Breuer <hans@breu...> |
Commiter | Hans Breuer |
image: fix 'Standard - Image' set_props() for pixbuf changes
Also provide a test file and an updated uninline_data.py script.
@@ -116,9 +116,9 @@ static PropDescription image_props[] = { | ||
116 | 116 | ELEMENT_COMMON_PROPERTIES, |
117 | 117 | { "image_file", PROP_TYPE_FILE, PROP_FLAG_VISIBLE, |
118 | 118 | N_("Image file"), NULL, NULL}, |
119 | - { "inline_data", PROP_TYPE_BOOL, PROP_FLAG_DONT_MERGE|PROP_FLAG_VISIBLE|PROP_FLAG_OPTIONAL, | |
119 | + { "inline_data", PROP_TYPE_BOOL, PROP_FLAG_VISIBLE|PROP_FLAG_OPTIONAL, | |
120 | 120 | N_("Inline data"), N_("Store image data in diagram"), NULL }, |
121 | - { "pixbuf", PROP_TYPE_PIXBUF, PROP_FLAG_OPTIONAL, | |
121 | + { "pixbuf", PROP_TYPE_PIXBUF, PROP_FLAG_DONT_MERGE|PROP_FLAG_OPTIONAL, | |
122 | 122 | N_("Pixbuf"), N_("The Pixbuf reference"), NULL }, |
123 | 123 | { "show_border", PROP_TYPE_BOOL, PROP_FLAG_VISIBLE, |
124 | 124 | N_("Draw border"), NULL, NULL}, |
@@ -191,6 +191,27 @@ image_get_props(Image *image, GPtrArray *props) | ||
191 | 191 | object_get_props_from_offsets(&image->element.object, image_offsets, props); |
192 | 192 | } |
193 | 193 | |
194 | +/*! | |
195 | + * \brief Set properties of the _Image | |
196 | + * \memberof _Image | |
197 | + * | |
198 | + * The properties of _Image are not completely independent. Some logic | |
199 | + * in this method should lead to a consistent object state. | |
200 | + * | |
201 | + * - with inline_data=TRUE we shall have a valid pixbuf | |
202 | + * - with inline_data=FALSE we should have a valid filename (when created | |
203 | + * from scratch we don't) | |
204 | + * | |
205 | + * Changing the properties tries to ensure consistency and might trigger | |
206 | + * additional actions like saving a file or loading it from disk. Also | |
207 | + * some dependent parameters might change in response. | |
208 | + * | |
209 | + * - switch on inline data => make inline: set pixbuf, keep filename | |
210 | + * - switch off inline data => save pixbuf to filename, if new filename | |
211 | + * - set a new, non empty filename => load the file | |
212 | + * - set an empty filename => make inline | |
213 | + * - set a new, non NULL pixbuf => use as image, inline | |
214 | + */ | |
194 | 215 | static void |
195 | 216 | image_set_props(Image *image, GPtrArray *props) |
196 | 217 | { |
@@ -202,43 +223,47 @@ image_set_props(Image *image, GPtrArray *props) | ||
202 | 223 | |
203 | 224 | object_set_props_from_offsets(&image->element.object, image_offsets, props); |
204 | 225 | |
205 | - if (old_pixbuf != image->pixbuf) { | |
206 | - if (!image->file || *image->file == '\0') { | |
207 | - GdkPixbuf *pixbuf = NULL; | |
208 | - image->inline_data = TRUE; /* otherwise we'll loose it */ | |
209 | - /* somebody deleting the filename? */ | |
210 | - if (!image->pixbuf && image->image) | |
211 | - pixbuf = g_object_ref ((GdkPixbuf *)dia_image_pixbuf (image->image)); | |
212 | - if (image->image) | |
213 | - g_object_unref (image->image); | |
214 | - image->image = dia_image_new_from_pixbuf (image->pixbuf ? image->pixbuf : pixbuf); | |
215 | - if (image->pixbuf) | |
216 | - g_object_unref (image->pixbuf); | |
217 | - image->pixbuf = g_object_ref ((GdkPixbuf *)dia_image_pixbuf (image->image)); | |
218 | - if (pixbuf) | |
219 | - g_object_unref (pixbuf); | |
220 | - } else { | |
221 | - if (image->pixbuf) { | |
222 | - if (image->image) | |
223 | - g_object_unref (image->image); | |
224 | - image->image = dia_image_new_from_pixbuf (image->pixbuf); | |
225 | - } | |
226 | - } | |
227 | - } | |
228 | - | |
229 | 226 | /* use old value on error */ |
230 | 227 | if (!image->file || g_stat (image->file, &st) != 0) |
231 | 228 | mtime = image->mtime; |
232 | 229 | else |
233 | 230 | mtime = st.st_mtime; |
234 | 231 | |
235 | - /* handle changing the image. */ | |
236 | - if (image->file && image->pixbuf && was_inline && !image->inline_data) { | |
237 | - /* export inline data */ | |
238 | - if (was_inline && !image->inline_data) | |
239 | - /* if saving fails we keep it inline */ | |
240 | - image->inline_data = !dia_image_save (image->image, image->file); | |
241 | - } else if (image->file && ((old_file && strcmp(image->file, old_file) != 0) || image->mtime != mtime)) { | |
232 | + if ( (!was_inline && image->inline_data && old_pixbuf) | |
233 | + || ( (!image->file || strlen(image->file) == 0) | |
234 | + && old_pixbuf) | |
235 | + || (image->pixbuf && (image->pixbuf != old_pixbuf))) { /* switch on inline */ | |
236 | + if (!image->pixbuf || old_pixbuf == image->pixbuf) { /* just reference the old image */ | |
237 | + image->pixbuf = g_object_ref ((GdkPixbuf *)old_pixbuf); | |
238 | + image->inline_data = TRUE; | |
239 | + } else if (old_pixbuf != image->pixbuf && image->pixbuf) { /* substitute the image and pixbuf */ | |
240 | + DiaImage *old_image = image->image; | |
241 | + image->image = dia_image_new_from_pixbuf (image->pixbuf); | |
242 | + image->pixbuf = g_object_ref ((GdkPixbuf *)dia_image_pixbuf (image->image)); | |
243 | + if (old_image) | |
244 | + g_object_unref (old_image); | |
245 | + image->inline_data = TRUE; | |
246 | + } else { | |
247 | + image->inline_data = FALSE; | |
248 | + g_warning ("Not setting inline_data"); | |
249 | + } | |
250 | + } else if (was_inline && !image->inline_data) { /* switch off inline */ | |
251 | + if (old_file && image->file && strcmp (old_file, image->file) != 0) { | |
252 | + /* export inline data, if saving fails we keep it inline */ | |
253 | + image->inline_data = !dia_image_save (image->image, image->file); | |
254 | + } else if (!image->file) { | |
255 | + message_warning (_("Can't save image without filename")); | |
256 | + image->inline_data = TRUE; /* keep inline */ | |
257 | + } | |
258 | + if (!image->inline_data) { | |
259 | + /* drop the pixbuf reference */ | |
260 | + if (image->pixbuf) | |
261 | + g_object_unref (image->pixbuf); | |
262 | + image->pixbuf = NULL; | |
263 | + } | |
264 | + } else if ( image->file && strlen(image->file) > 0 | |
265 | + && ( (!old_file || (strcmp (old_file, image->file) != 0)) | |
266 | + || (image->mtime != mtime))) { /* load new file */ | |
242 | 267 | Element *elem = &image->element; |
243 | 268 | DiaImage *img = NULL; |
244 | 269 |
@@ -249,9 +274,20 @@ image_set_props(Image *image, GPtrArray *props) | ||
249 | 274 | elem->height = (elem->width*(float)dia_image_height(image->image))/ |
250 | 275 | (float)dia_image_width(image->image); |
251 | 276 | /* release image->pixbuf? */ |
277 | + } else if ((!image->file || strlen(image->file)==0) && !image->pixbuf) { | |
278 | + /* make it a "broken image" without complaints */ | |
279 | + dia_log_message ("_Image::set_props() seting empty."); | |
280 | + } else { | |
281 | + if ( (image->inline_data && image->pixbuf != NULL) /* don't need a filename */ | |
282 | + || (!image->inline_data && image->file)) /* don't need a pixbuf */ | |
283 | + dia_log_message ("_Image::set_props() ok."); | |
284 | + else | |
285 | + g_warning ("_Image::set_props() not handled file='%s', pixbuf=%p, inline=%s", | |
286 | + image->file ? image->file : "", | |
287 | + image->pixbuf, image->inline_data ? "TRUE" : "FALSE"); | |
252 | 288 | } |
253 | 289 | g_free(old_file); |
254 | - /* remember it */ | |
290 | + /* remember modification time */ | |
255 | 291 | image->mtime = mtime; |
256 | 292 | |
257 | 293 | image_update_data(image); |
@@ -839,6 +875,11 @@ image_load(ObjectNode obj_node, int version, DiaContext *ctx) | ||
839 | 875 | g_object_unref (pixbuf); |
840 | 876 | } |
841 | 877 | } |
878 | + } else { | |
879 | + attr = object_find_attribute(obj_node, "inline_data"); | |
880 | + if (attr != NULL) | |
881 | + image->inline_data = data_boolean(attribute_first_data(attr), ctx); | |
882 | + /* Should be set pixbuf, too? Or leave it till the first get. */ | |
842 | 883 | } |
843 | 884 | |
844 | 885 | /* update mtime */ |
@@ -24,6 +24,7 @@ class UninlineRenderer : | ||
24 | 24 | def begin_render (self, data, filename) : |
25 | 25 | imgmap = {} |
26 | 26 | dirname = os.path.dirname (filename) |
27 | + basename = os.path.basename(filename) | |
27 | 28 | ext = filename[string.rfind(filename, ".")+1:] |
28 | 29 | for layer in data.layers : |
29 | 30 | for o in layer.objects : |
@@ -33,7 +34,7 @@ class UninlineRenderer : | ||
33 | 34 | pos = o.properties["obj_pos"].value |
34 | 35 | xk = "%03g" % pos.x |
35 | 36 | yk = "%03g" % pos.y |
36 | - key = "L" + layer.name + "x" + xk + "y" + yk | |
37 | + key = basename + "-" + layer.name + "x" + xk + "y" + yk | |
37 | 38 | imgmap[key] = o |
38 | 39 | for k, o in imgmap.iteritems() : |
39 | 40 | fname = dirname + "/" + k + "." + ext |