2 * \file GraphicsCacheItem.C
3 * Copyright 2002 the LyX Team
4 * Read the file COPYING
6 * \author Baruch Even <baruch.even@writeme.com>
7 * \author Herbert Voss <voss@lyx.org>
8 * \author Angus Leeming <a.leeming@ic.ac.uk>
14 #pragma implementation
17 #include "graphics/GraphicsCache.h"
18 #include "graphics/GraphicsCacheItem.h"
19 #include "graphics/GraphicsImage.h"
20 #include "graphics/GraphicsParams.h"
21 #include "graphics/GraphicsConverter.h"
22 #include "insets/insetgraphics.h"
23 #include "BufferView.h"
26 #include "lyx_main.h" // for global dispatch method
27 #include "support/LAssert.h"
28 #include "support/filetools.h"
29 #include "frontends/Alert.h"
32 extern BufferView * current_view;
38 GCacheItem::GCacheItem(InsetGraphics const & inset, GParams const & params)
39 : filename_(params.filename), zipped_(false),
40 remove_loaded_file_(false), status_(WaitingToLoad)
42 ModifiedItemPtr item(new ModifiedItem(inset, params, image_));
43 modified_images.push_back(item);
49 typedef GCacheItem::ModifiedItemPtr ModifiedItemPtr;
51 class Compare_Params {
53 Compare_Params(GParams const & p) : p_(p) {}
55 bool operator()(ModifiedItemPtr const & ptr)
59 return ptr->params() == p_;
68 Find_Inset(InsetGraphics const & i) : i_(i) {}
70 bool operator()(ModifiedItemPtr const & ptr)
74 return ptr->referencedBy(i_);
78 InsetGraphics const & i_;
84 void GCacheItem::modify(InsetGraphics const & inset, GParams const & params)
86 // Does this inset currently reference an existing ModifiedItem with
88 // If so, remove the inset from the ModifiedItem's internal list
90 ListType::iterator begin = modified_images.begin();
91 ListType::iterator end = modified_images.end();
92 ListType::iterator it = begin;
94 it = std::find_if(it, end, Find_Inset(inset));
97 if ((*it)->params() != params) {
100 it = modified_images.erase(it);
105 // Is there an existing ModifiedItem with these params?
106 // If so, add inset to the list of insets referencing this ModifiedItem
107 begin = modified_images.begin();
108 end = modified_images.end();
109 it = std::find_if(begin, end, Compare_Params(params));
115 // If no ModifiedItem exists with these params, then create one.
116 ModifiedItemPtr item(new ModifiedItem(inset, params, image_));
117 modified_images.push_back(item);
123 void GCacheItem::remove(InsetGraphics const & inset)
125 // search the list of ModifiedItems for one referenced by this inset.
126 // If it is found, remove the reference.
127 // If the ModifiedItem is now referenced by no insets, remove it.
128 ListType::iterator begin = modified_images.begin();
129 ListType::iterator end = modified_images.end();
130 ListType::iterator it = std::find_if(begin, end, Find_Inset(inset));
135 (*it)->remove(inset);
136 if ((*it)->empty()) {
137 modified_images.clear();
142 void GCacheItem::startLoading(InsetGraphics const & inset)
144 if (status() != WaitingToLoad)
147 // Check that the image is referenced by this inset
148 ListType::const_iterator begin = modified_images.begin();
149 ListType::const_iterator end = modified_images.end();
150 ListType::const_iterator it =
151 std::find_if(begin, end, Find_Inset(inset));
156 if ((*it)->params().display == GParams::NONE)
159 convertToDisplayFormat();
163 bool GCacheItem::empty() const
165 return modified_images.empty();
169 bool GCacheItem::referencedBy(InsetGraphics const & inset) const
171 // Is one of the list of ModifiedItems referenced by this inset?
172 ListType::const_iterator begin = modified_images.begin();
173 ListType::const_iterator end = modified_images.end();
174 return std::find_if(begin, end, Find_Inset(inset)) != end;
178 string const & GCacheItem::filename() const
184 ImagePtr const GCacheItem::image(InsetGraphics const & inset) const
186 // find a ModifiedItem that is referenced by this inset.
187 ListType::const_iterator begin = modified_images.begin();
188 ListType::const_iterator end = modified_images.end();
189 ListType::const_iterator it =
190 std::find_if(begin, end, Find_Inset(inset));
192 // Someone's being daft.
196 // We are expressly requested to not render the image
197 if ((*it)->params().display == GParams::NONE)
200 // If the original image has been loaded, return what's going on
201 // in the ModifiedItem
202 if (status() == Loaded)
203 return (*it)->image();
209 ImageStatus GCacheItem::status(InsetGraphics const & inset) const
211 // find a ModifiedItem that is referenced by this inset.
212 ListType::const_iterator begin = modified_images.begin();
213 ListType::const_iterator end = modified_images.end();
214 ListType::const_iterator it =
215 std::find_if(begin, end, Find_Inset(inset));
217 // Someone's being daft.
221 if (status() == Loaded)
222 return (*it)->status();
228 // Called internally only. Use to ascertain the status of the loading of the
229 // original image. No scaling etc.
230 ImageStatus GCacheItem::status() const
236 void GCacheItem::setStatus(ImageStatus new_status)
238 status_ = new_status;
240 // Loop over all insets and tell the BufferView that it has changed.
241 typedef ModifiedItem::ListType::const_iterator inset_iterator;
243 ListType::const_iterator it = modified_images.begin();
244 ListType::const_iterator end = modified_images.end();
245 for (; it != end; ++it) {
246 inset_iterator it2 = (*it)->insets.begin();
247 inset_iterator end2 = (*it)->insets.end();
249 for (; it2 != end2; ++it2) {
250 InsetGraphics * inset =
251 const_cast<InsetGraphics *>(*it2);
253 // Use of current_view is very, very Evil!!
254 current_view->updateInset(inset, false);
260 void GCacheItem::changeDisplay(bool changed_background)
262 ListType::iterator begin = modified_images.begin();
263 ListType::iterator end = modified_images.end();
265 // The background has changed. Change all modified images.
266 if (changed_background) {
267 for (ListType::iterator it = begin; it != end; ++it) {
275 for (ListType::iterator it = begin; it != end; ++it) {
276 // ModifiedItem::changeDisplay returns a full
277 // ModifiedItemPtr if any of the insets have display=DEFAULT
278 // and if that DEFAULT value has changed
279 ModifiedItemPtr new_item = (*it)->changeDisplay();
283 temp_list.push_back(new_item);
285 // The original store may now be empty
286 if ((*it)->insets.empty()) {
287 it = modified_images.erase(it);
291 if (temp_list.empty())
294 // Recombine new_list and modified_images.
295 begin = modified_images.begin();
296 end = modified_images.end();
298 ListType::const_iterator tbegin = temp_list.begin();
299 ListType::const_iterator tend = temp_list.end();
301 ListType append_list;
303 for (ListType::const_iterator tit = tbegin; tit != tend; ++tit) {
304 GParams const & params = (*tit)->params();
305 ListType::iterator it =
306 std::find_if(begin, end, Compare_Params(params));
308 append_list.push_back(*tit);
310 (*it)->insets.merge((*tit)->insets);
313 if (append_list.empty())
316 modified_images.splice(modified_images.end(), append_list);
320 void GCacheItem::imageConverted(string const & file_to_load)
323 (!file_to_load.empty() && IsFileReadable(file_to_load));
325 string const text = success ? "succeeded" : "failed";
326 lyxerr[Debug::GRAPHICS] << "Image conversion " << text << "." << endl;
329 setStatus(ErrorConverting);
332 lyx::unlink(unzipped_filename_);
339 // Do the actual image loading from file to memory.
340 file_to_load_ = file_to_load;
346 // This function gets called from the callback after the image has been
347 // converted successfully.
348 void GCacheItem::loadImage()
351 lyxerr[Debug::GRAPHICS] << "Loading image." << endl;
353 // Connect a signal to this->imageLoaded and pass this signal to
354 // GImage::loadImage.
355 SignalLoadTypePtr on_finish;
356 on_finish.reset(new SignalLoadType);
357 cl_ = on_finish->connect(SigC::slot(this, &GCacheItem::imageLoaded));
359 image_ = GImage::newImage();
360 image_->load(file_to_load_, on_finish);
364 void GCacheItem::imageLoaded(bool success)
366 string const text = success ? "succeeded" : "failed";
367 lyxerr[Debug::GRAPHICS] << "Image loading " << text << "." << endl;
369 // Clean up after loading.
371 lyx::unlink(unzipped_filename_);
373 if (remove_loaded_file_ && unzipped_filename_ != file_to_load_)
374 lyx::unlink(file_to_load_);
379 setStatus(ErrorLoading);
385 // Loop over the list of modified images and create them.
386 ListType::iterator it = modified_images.begin();
387 ListType::iterator end = modified_images.end();
388 for (; it != end; ++it) {
389 (*it)->modify(image_);
396 string const findTargetFormat(string const & from)
398 typedef GImage::FormatList FormatList;
399 FormatList const & formats = GImage::loadableFormats();
401 // There must be a format to load from.
402 lyx::Assert(!formats.empty());
404 // First ascertain if we can load directly with no conversion
405 FormatList::const_iterator it1 = formats.begin();
406 FormatList::const_iterator end = formats.end();
407 for (; it1 != end; ++it1) {
412 // So, we have to convert to a loadable format. Can we?
413 grfx::GConverter const & graphics_converter = grfx::GConverter::get();
415 FormatList::const_iterator it2 = formats.begin();
416 for (; it2 != end; ++it2) {
417 if (graphics_converter.isReachable(from, *it2))
428 void GCacheItem::convertToDisplayFormat()
430 setStatus(Converting);
431 string filename = filename_; // Make a local copy in case we unzip it
432 string const displayed_filename = MakeDisplayPath(filename_);
434 // First, check that the file exists!
435 if (!IsFileReadable(filename)) {
436 Alert::alert(_("File ") + displayed_filename,
437 _("\nisn't readable or doesn't exist!"));
438 setStatus(ErrorNoFile);
442 // maybe that other zip extensions also be useful, especially the
443 // ones that may be declared in texmf/tex/latex/config/graphics.cfg.
445 /* -----------snip-------------
446 {\DeclareGraphicsRule{.pz}{eps}{.bb}{}%
447 \DeclareGraphicsRule{.eps.Z}{eps}{.eps.bb}{}%
448 \DeclareGraphicsRule{.ps.Z}{eps}{.ps.bb}{}%
449 \DeclareGraphicsRule{.ps.gz}{eps}{.ps.bb}{}%
450 \DeclareGraphicsRule{.eps.gz}{eps}{.eps.bb}{}}}%
451 -----------snip-------------*/
453 lyxerr[Debug::GRAPHICS]
454 << "Attempting to convert image file: " << displayed_filename
455 << "\nwith recognised extension: " << GetExtension(filename)
458 zipped_ = zippedFile(filename);
460 filename = unzipFile(filename);
461 unzipped_filename_ = filename;
464 string const from = getExtFromContents(filename);
465 string const to = grfx::findTargetFormat(from);
467 lyxerr[Debug::GRAPHICS]
468 << "The file contains " << from << " format data." << endl;
471 Alert::alert(_("Unable to convert file ") +
473 _(" to a loadable format."));
474 setStatus(ErrorConverting);
479 // No conversion needed!
480 lyxerr[Debug::GRAPHICS] << "No conversion needed!" << endl;
481 file_to_load_ = filename;
486 lyxerr[Debug::GRAPHICS] << "Converting it to " << to << " format." << endl;
488 // Take only the filename part of the file, without path or extension.
489 string const temp = ChangeExtension(OnlyFilename(filename), string());
491 // Add some stuff to create a uniquely named temporary file.
492 // This file is deleted in loadImage after it is loaded into memory.
493 string const to_file_base = lyx::tempName(string(), temp);
494 remove_loaded_file_ = true;
496 // Remove the temp file, we only want the name...
497 lyx::unlink(to_file_base);
499 // Connect a signal to this->imageConverted and pass this signal to
500 // the graphics converter so that we can load the modified file
501 // on completion of the conversion process.
502 SignalConvertTypePtr on_finish;
503 on_finish.reset(new SignalConvertType);
504 cc_ = on_finish->connect(SigC::slot(this, &GCacheItem::imageConverted));
506 GConverter & graphics_converter = GConverter::get();
507 graphics_converter.convert(filename, to_file_base, from, to, on_finish);
511 ModifiedItem::ModifiedItem(InsetGraphics const & new_inset,
512 GParams const & new_params,
513 ImagePtr const & new_image)
514 : status_(ScalingEtc)
516 p_.reset(new GParams(new_params));
517 insets.push_back(&new_inset);
522 void ModifiedItem::add(InsetGraphics const & inset)
524 insets.push_back(&inset);
529 void ModifiedItem::remove(InsetGraphics const & inset)
531 ListType::iterator begin = insets.begin();
532 ListType::iterator end = insets.end();
533 ListType::iterator it = std::remove(begin, end, &inset);
534 insets.erase(it, end);
538 bool ModifiedItem::referencedBy(InsetGraphics const & inset) const
540 ListType::const_iterator begin = insets.begin();
541 ListType::const_iterator end = insets.end();
542 return std::find(begin, end, &inset) != end;
546 ImagePtr const ModifiedItem::image() const
548 if (modified_image_.get())
549 return modified_image_;
551 return original_image_;
555 void ModifiedItem::modify(ImagePtr const & new_image)
557 if (!new_image.get())
560 original_image_ = new_image;
561 modified_image_.reset(original_image_->clone());
563 if (params().display == GParams::NONE) {
568 setStatus(ScalingEtc);
569 modified_image_->clip(params());
570 modified_image_->rotate(params());
571 modified_image_->scale(params());
576 void ModifiedItem::setPixmap()
578 if (!modified_image_.get())
581 if (params().display == GParams::NONE) {
586 bool const success = modified_image_->setPixmap(params());
591 modified_image_.reset();
592 setStatus(ErrorScalingEtc);
597 void ModifiedItem::setStatus(ImageStatus new_status)
599 status_ = new_status;
601 // Tell the BufferView that the inset has changed.
603 ListType::const_iterator it = insets.begin();
604 ListType::const_iterator end = insets.end();
605 for (; it != end; ++it) {
606 InsetGraphics * inset = const_cast<InsetGraphics *>(*it);
607 current_view->updateInset(inset, false);
614 struct Params_Changed {
616 Params_Changed(GParams const & p) : p_(p) {}
618 bool operator()(InsetGraphics const * inset)
620 return GParams(inset->params()) != p_;
629 // changeDisplay returns an initialised ModifiedItem if any of the insets
630 // have display == DEFAULT and if that DEFAULT value has changed.
631 // If this occurs, then (this) has these insets removed.
632 ModifiedItemPtr ModifiedItem::changeDisplay()
634 // Loop over the list of insets. Compare the updated params for each
635 // with params(). If different, move into a new list.
636 ListType::iterator begin = insets.begin();
637 ListType::iterator end = insets.end();
638 ListType::iterator it =
639 std::remove_if(begin, end, Params_Changed(params()));
642 // No insets have changed params
643 return ModifiedItemPtr();
646 // it -> end have params that are changed. Move to the new list.
648 new_insets.insert(new_insets.begin(), it, end);
649 insets.erase(it, end);
651 // Create a new ModifiedItem with these new params. Note that
652 // the only params that have changed are the display ones,
653 // so we don't need to crop, rotate, scale.
654 ModifiedItemPtr new_item(new ModifiedItem(*this));
655 new_item->insets = new_insets;
656 *(new_item->p_) = GParams((*new_insets.begin())->params());
658 new_item->setPixmap();