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;
39 GCacheItem::GCacheItem(InsetGraphics const & inset, GParams const & params)
40 : filename_(params.filename), zipped_(false),
41 remove_loaded_file_(false), status_(WaitingToLoad)
43 ModifiedItemPtr item(new ModifiedItem(inset, params, image_));
44 modified_images.push_back(item);
50 typedef GCacheItem::ModifiedItemPtr ModifiedItemPtr;
52 class Compare_Params {
54 Compare_Params(GParams const & p) : p_(p) {}
56 bool operator()(ModifiedItemPtr const & ptr)
60 return ptr->params() == p_;
69 Find_Inset(InsetGraphics const & i) : i_(i) {}
71 bool operator()(ModifiedItemPtr const & ptr)
75 return ptr->referencedBy(i_);
79 InsetGraphics const & i_;
85 void GCacheItem::modify(InsetGraphics const & inset, GParams const & params)
87 // Does this inset currently reference an existing ModifiedItem with
89 // If so, remove the inset from the ModifiedItem's internal list
91 ListType::iterator begin = modified_images.begin();
92 ListType::iterator end = modified_images.end();
93 ListType::iterator it = begin;
95 it = std::find_if(it, end, Find_Inset(inset));
98 if ((*it)->params() != params) {
101 it = modified_images.erase(it);
106 // Is there an existing ModifiedItem with these params?
107 // If so, add inset to the list of insets referencing this ModifiedItem
108 begin = modified_images.begin();
109 end = modified_images.end();
110 it = std::find_if(begin, end, Compare_Params(params));
116 // If no ModifiedItem exists with these params, then create one.
117 ModifiedItemPtr item(new ModifiedItem(inset, params, image_));
118 modified_images.push_back(item);
124 void GCacheItem::remove(InsetGraphics const & inset)
126 // search the list of ModifiedItems for one referenced by this inset.
127 // If it is found, remove the reference.
128 // If the ModifiedItem is now referenced by no insets, remove it.
129 ListType::iterator begin = modified_images.begin();
130 ListType::iterator end = modified_images.end();
131 ListType::iterator it = std::find_if(begin, end, Find_Inset(inset));
136 (*it)->remove(inset);
137 if ((*it)->empty()) {
138 modified_images.clear();
143 void GCacheItem::startLoading(InsetGraphics const & inset)
145 if (status() != WaitingToLoad)
148 // Check that the image is referenced by this inset
149 ListType::const_iterator begin = modified_images.begin();
150 ListType::const_iterator end = modified_images.end();
151 ListType::const_iterator it =
152 std::find_if(begin, end, Find_Inset(inset));
157 if ((*it)->params().display == GParams::NONE)
160 convertToDisplayFormat();
164 bool GCacheItem::empty() const
166 return modified_images.empty();
170 bool GCacheItem::referencedBy(InsetGraphics const & inset) const
172 // Is one of the list of ModifiedItems referenced by this inset?
173 ListType::const_iterator begin = modified_images.begin();
174 ListType::const_iterator end = modified_images.end();
175 return std::find_if(begin, end, Find_Inset(inset)) != end;
179 string const & GCacheItem::filename() const
185 ImagePtr const GCacheItem::image(InsetGraphics const & inset) const
187 // find a ModifiedItem that is referenced by this inset.
188 ListType::const_iterator begin = modified_images.begin();
189 ListType::const_iterator end = modified_images.end();
190 ListType::const_iterator it =
191 std::find_if(begin, end, Find_Inset(inset));
193 // Someone's being daft.
197 // We are expressly requested to not render the image
198 if ((*it)->params().display == GParams::NONE)
201 // If the original image has been loaded, return what's going on
202 // in the ModifiedItem
203 if (status() == Loaded)
204 return (*it)->image();
210 ImageStatus GCacheItem::status(InsetGraphics const & inset) const
212 // find a ModifiedItem that is referenced by this inset.
213 ListType::const_iterator begin = modified_images.begin();
214 ListType::const_iterator end = modified_images.end();
215 ListType::const_iterator it =
216 std::find_if(begin, end, Find_Inset(inset));
218 // Someone's being daft.
222 if (status() == Loaded)
223 return (*it)->status();
229 // Called internally only. Use to ascertain the status of the loading of the
230 // original image. No scaling etc.
231 ImageStatus GCacheItem::status() const
237 void GCacheItem::setStatus(ImageStatus new_status)
239 status_ = new_status;
241 // Loop over all insets and tell the BufferView that it has changed.
242 typedef ModifiedItem::ListType::const_iterator inset_iterator;
244 ListType::const_iterator it = modified_images.begin();
245 ListType::const_iterator end = modified_images.end();
246 for (; it != end; ++it) {
247 inset_iterator it2 = (*it)->insets.begin();
248 inset_iterator end2 = (*it)->insets.end();
250 for (; it2 != end2; ++it2) {
251 InsetGraphics * inset =
252 const_cast<InsetGraphics *>(*it2);
254 // Use of current_view is very, very Evil!!
255 current_view->updateInset(inset, false);
261 void GCacheItem::changeDisplay(bool changed_background)
263 ListType::iterator begin = modified_images.begin();
264 ListType::iterator end = modified_images.end();
266 // The background has changed. Change all modified images.
267 if (changed_background) {
268 for (ListType::iterator it = begin; it != end; ++it) {
276 for (ListType::iterator it = begin; it != end; ++it) {
277 // ModifiedItem::changeDisplay returns a full
278 // ModifiedItemPtr if any of the insets have display=DEFAULT
279 // and if that DEFAULT value has changed
280 ModifiedItemPtr new_item = (*it)->changeDisplay();
284 temp_list.push_back(new_item);
286 // The original store may now be empty
287 if ((*it)->insets.empty()) {
288 it = modified_images.erase(it);
292 if (temp_list.empty())
295 // Recombine new_list and modified_images.
296 begin = modified_images.begin();
297 end = modified_images.end();
299 ListType::const_iterator tbegin = temp_list.begin();
300 ListType::const_iterator tend = temp_list.end();
302 ListType append_list;
304 for (ListType::const_iterator tit = tbegin; tit != tend; ++tit) {
305 GParams const & params = (*tit)->params();
306 ListType::iterator it =
307 std::find_if(begin, end, Compare_Params(params));
309 append_list.push_back(*tit);
311 (*it)->insets.merge((*tit)->insets);
314 if (append_list.empty())
317 modified_images.splice(modified_images.end(), append_list);
321 void GCacheItem::imageConverted(string const & file_to_load)
324 (!file_to_load.empty() && IsFileReadable(file_to_load));
326 string const text = success ? "succeeded" : "failed";
327 lyxerr[Debug::GRAPHICS] << "Image conversion " << text << "." << endl;
330 setStatus(ErrorConverting);
333 lyx::unlink(unzipped_filename_);
340 // Do the actual image loading from file to memory.
341 file_to_load_ = file_to_load;
347 // This function gets called from the callback after the image has been
348 // converted successfully.
349 void GCacheItem::loadImage()
352 lyxerr[Debug::GRAPHICS] << "Loading image." << endl;
354 // Connect a signal to this->imageLoaded and pass this signal to
355 // GImage::loadImage.
356 SignalLoadTypePtr on_finish;
357 on_finish.reset(new SignalLoadType);
358 cl_ = on_finish->connect(SigC::slot(this, &GCacheItem::imageLoaded));
360 image_ = GImage::newImage();
361 image_->load(file_to_load_, on_finish);
365 void GCacheItem::imageLoaded(bool success)
367 string const text = success ? "succeeded" : "failed";
368 lyxerr[Debug::GRAPHICS] << "Image loading " << text << "." << endl;
370 // Clean up after loading.
372 lyx::unlink(unzipped_filename_);
374 if (remove_loaded_file_ && unzipped_filename_ != file_to_load_)
375 lyx::unlink(file_to_load_);
380 setStatus(ErrorLoading);
386 // Loop over the list of modified images and create them.
387 ListType::iterator it = modified_images.begin();
388 ListType::iterator end = modified_images.end();
389 for (; it != end; ++it) {
390 (*it)->modify(image_);
395 unsigned int GCacheItem::raw_width() const
400 return image_->getWidth();
404 unsigned int GCacheItem::raw_height() const
409 return image_->getHeight();
415 string const findTargetFormat(string const & from)
417 typedef GImage::FormatList FormatList;
418 FormatList const formats = GImage::loadableFormats();
420 // There must be a format to load from.
421 lyx::Assert(!formats.empty());
423 // First ascertain if we can load directly with no conversion
424 FormatList::const_iterator it1 = formats.begin();
425 FormatList::const_iterator end = formats.end();
426 for (; it1 != end; ++it1) {
431 // So, we have to convert to a loadable format. Can we?
432 grfx::GConverter const & graphics_converter = grfx::GConverter::get();
434 FormatList::const_iterator it2 = formats.begin();
435 for (; it2 != end; ++it2) {
436 if (graphics_converter.isReachable(from, *it2))
447 void GCacheItem::convertToDisplayFormat()
449 setStatus(Converting);
450 string filename = filename_; // Make a local copy in case we unzip it
451 string const displayed_filename = MakeDisplayPath(filename_);
453 // First, check that the file exists!
454 if (!IsFileReadable(filename)) {
455 setStatus(ErrorNoFile);
459 // maybe that other zip extensions also be useful, especially the
460 // ones that may be declared in texmf/tex/latex/config/graphics.cfg.
462 /* -----------snip-------------
463 {\DeclareGraphicsRule{.pz}{eps}{.bb}{}%
464 \DeclareGraphicsRule{.eps.Z}{eps}{.eps.bb}{}%
465 \DeclareGraphicsRule{.ps.Z}{eps}{.ps.bb}{}%
466 \DeclareGraphicsRule{.ps.gz}{eps}{.ps.bb}{}%
467 \DeclareGraphicsRule{.eps.gz}{eps}{.eps.bb}{}}}%
468 -----------snip-------------*/
470 lyxerr[Debug::GRAPHICS]
471 << "Attempting to convert image file: " << displayed_filename
472 << "\nwith recognised extension: " << GetExtension(filename)
475 zipped_ = zippedFile(filename);
477 filename = unzipFile(filename);
478 unzipped_filename_ = filename;
481 string const from = getExtFromContents(filename);
482 string const to = grfx::findTargetFormat(from);
484 lyxerr[Debug::GRAPHICS]
485 << "The file contains " << from << " format data." << endl;
488 Alert::alert(_("Unable to convert file ") +
490 _(" to a loadable format."));
491 setStatus(ErrorConverting);
496 // No conversion needed!
497 lyxerr[Debug::GRAPHICS] << "No conversion needed!" << endl;
498 file_to_load_ = filename;
503 lyxerr[Debug::GRAPHICS] << "Converting it to " << to << " format." << endl;
505 // Take only the filename part of the file, without path or extension.
506 string const temp = ChangeExtension(OnlyFilename(filename), string());
508 // Add some stuff to create a uniquely named temporary file.
509 // This file is deleted in loadImage after it is loaded into memory.
510 string const to_file_base = lyx::tempName(string(), temp);
511 remove_loaded_file_ = true;
513 // Remove the temp file, we only want the name...
514 lyx::unlink(to_file_base);
516 // Connect a signal to this->imageConverted and pass this signal to
517 // the graphics converter so that we can load the modified file
518 // on completion of the conversion process.
519 SignalConvertTypePtr on_finish;
520 on_finish.reset(new SignalConvertType);
521 cc_ = on_finish->connect(SigC::slot(this, &GCacheItem::imageConverted));
523 GConverter & graphics_converter = GConverter::get();
524 graphics_converter.convert(filename, to_file_base, from, to, on_finish);
528 ModifiedItem::ModifiedItem(InsetGraphics const & new_inset,
529 GParams const & new_params,
530 ImagePtr const & new_image)
531 : status_(ScalingEtc)
533 p_.reset(new GParams(new_params));
534 insets.push_back(&new_inset);
539 void ModifiedItem::add(InsetGraphics const & inset)
541 insets.push_back(&inset);
546 void ModifiedItem::remove(InsetGraphics const & inset)
548 ListType::iterator begin = insets.begin();
549 ListType::iterator end = insets.end();
550 ListType::iterator it = std::remove(begin, end, &inset);
551 insets.erase(it, end);
555 bool ModifiedItem::referencedBy(InsetGraphics const & inset) const
557 ListType::const_iterator begin = insets.begin();
558 ListType::const_iterator end = insets.end();
559 return std::find(begin, end, &inset) != end;
563 ImagePtr const ModifiedItem::image() const
565 if (modified_image_.get())
566 return modified_image_;
568 return original_image_;
572 void ModifiedItem::modify(ImagePtr const & new_image)
574 if (!new_image.get())
577 original_image_ = new_image;
578 modified_image_.reset(original_image_->clone());
580 if (params().display == GParams::NONE) {
585 setStatus(ScalingEtc);
586 modified_image_->clip(params());
587 modified_image_->rotate(params());
588 modified_image_->scale(params());
593 void ModifiedItem::setPixmap()
595 if (!modified_image_.get())
598 if (params().display == GParams::NONE) {
603 bool const success = modified_image_->setPixmap(params());
608 modified_image_.reset();
609 setStatus(ErrorScalingEtc);
614 void ModifiedItem::setStatus(ImageStatus new_status)
616 status_ = new_status;
618 // Tell the BufferView that the inset has changed.
620 ListType::const_iterator it = insets.begin();
621 ListType::const_iterator end = insets.end();
622 for (; it != end; ++it) {
623 InsetGraphics * inset = const_cast<InsetGraphics *>(*it);
624 current_view->updateInset(inset, false);
631 struct Params_Changed {
633 Params_Changed(GParams const & p) : p_(p) {}
635 bool operator()(InsetGraphics const * inset)
637 string const path = OnlyPath(p_.filename);
638 return GParams(inset->params(), path) != p_;
647 // changeDisplay returns an initialised ModifiedItem if any of the insets
648 // have display == DEFAULT and if that DEFAULT value has changed.
649 // If this occurs, then (this) has these insets removed.
650 ModifiedItemPtr ModifiedItem::changeDisplay()
652 // Loop over the list of insets. Compare the updated params for each
653 // with params(). If different, move into a new list.
654 ListType::iterator begin = insets.begin();
655 ListType::iterator end = insets.end();
656 ListType::iterator it =
657 std::remove_if(begin, end, Params_Changed(params()));
660 // No insets have changed params
661 return ModifiedItemPtr();
664 // it -> end have params that are changed. Move to the new list.
666 new_insets.insert(new_insets.begin(), it, end);
667 insets.erase(it, end);
669 // Create a new ModifiedItem with these new params. Note that
670 // the only params that have changed are the display ones,
671 // so we don't need to crop, rotate, scale.
672 string const path = OnlyPath(p_->filename);
674 ModifiedItemPtr new_item(new ModifiedItem(*this));
675 new_item->insets = new_insets;
676 *(new_item->p_) = GParams((*new_insets.begin())->params(), path);
678 new_item->setPixmap();