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"
31 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_);
394 unsigned int GCacheItem::raw_width() const
399 return image_->getWidth();
403 unsigned int GCacheItem::raw_height() const
408 return image_->getHeight();
414 string const findTargetFormat(string const & from)
416 typedef GImage::FormatList FormatList;
417 FormatList const formats = GImage::loadableFormats();
419 // There must be a format to load from.
420 lyx::Assert(!formats.empty());
422 // First ascertain if we can load directly with no conversion
423 FormatList::const_iterator it1 = formats.begin();
424 FormatList::const_iterator end = formats.end();
425 for (; it1 != end; ++it1) {
430 // So, we have to convert to a loadable format. Can we?
431 grfx::GConverter const & graphics_converter = grfx::GConverter::get();
433 FormatList::const_iterator it2 = formats.begin();
434 for (; it2 != end; ++it2) {
435 if (graphics_converter.isReachable(from, *it2))
446 void GCacheItem::convertToDisplayFormat()
448 setStatus(Converting);
449 string filename = filename_; // Make a local copy in case we unzip it
450 string const displayed_filename = MakeDisplayPath(filename_);
452 // First, check that the file exists!
453 if (!IsFileReadable(filename)) {
454 setStatus(ErrorNoFile);
458 // maybe that other zip extensions also be useful, especially the
459 // ones that may be declared in texmf/tex/latex/config/graphics.cfg.
461 /* -----------snip-------------
462 {\DeclareGraphicsRule{.pz}{eps}{.bb}{}%
463 \DeclareGraphicsRule{.eps.Z}{eps}{.eps.bb}{}%
464 \DeclareGraphicsRule{.ps.Z}{eps}{.ps.bb}{}%
465 \DeclareGraphicsRule{.ps.gz}{eps}{.ps.bb}{}%
466 \DeclareGraphicsRule{.eps.gz}{eps}{.eps.bb}{}}}%
467 -----------snip-------------*/
469 lyxerr[Debug::GRAPHICS]
470 << "Attempting to convert image file: " << displayed_filename
471 << "\nwith recognised extension: " << GetExtension(filename)
474 zipped_ = zippedFile(filename);
476 filename = unzipFile(filename);
477 unzipped_filename_ = filename;
480 string const from = getExtFromContents(filename);
481 string const to = grfx::findTargetFormat(from);
483 lyxerr[Debug::GRAPHICS]
484 << "The file contains " << from << " format data." << endl;
487 setStatus(ErrorConverting);
492 // No conversion needed!
493 lyxerr[Debug::GRAPHICS] << "No conversion needed!" << endl;
494 file_to_load_ = filename;
499 lyxerr[Debug::GRAPHICS] << "Converting it to " << to << " format." << endl;
501 // Take only the filename part of the file, without path or extension.
502 string const temp = ChangeExtension(OnlyFilename(filename), string());
504 // Add some stuff to create a uniquely named temporary file.
505 // This file is deleted in loadImage after it is loaded into memory.
506 string const to_file_base = lyx::tempName(string(), temp);
507 remove_loaded_file_ = true;
509 // Remove the temp file, we only want the name...
510 lyx::unlink(to_file_base);
512 // Connect a signal to this->imageConverted and pass this signal to
513 // the graphics converter so that we can load the modified file
514 // on completion of the conversion process.
515 SignalConvertTypePtr on_finish;
516 on_finish.reset(new SignalConvertType);
517 cc_ = on_finish->connect(SigC::slot(this, &GCacheItem::imageConverted));
519 GConverter & graphics_converter = GConverter::get();
520 graphics_converter.convert(filename, to_file_base, from, to, on_finish);
524 ModifiedItem::ModifiedItem(InsetGraphics const & new_inset,
525 GParams const & new_params,
526 ImagePtr const & new_image)
527 : status_(ScalingEtc)
529 p_.reset(new GParams(new_params));
530 insets.push_back(&new_inset);
535 void ModifiedItem::add(InsetGraphics const & inset)
537 insets.push_back(&inset);
542 void ModifiedItem::remove(InsetGraphics const & inset)
544 ListType::iterator begin = insets.begin();
545 ListType::iterator end = insets.end();
546 ListType::iterator it = std::remove(begin, end, &inset);
547 insets.erase(it, end);
551 bool ModifiedItem::referencedBy(InsetGraphics const & inset) const
553 ListType::const_iterator begin = insets.begin();
554 ListType::const_iterator end = insets.end();
555 return std::find(begin, end, &inset) != end;
559 ImagePtr const ModifiedItem::image() const
561 if (modified_image_.get())
562 return modified_image_;
564 return original_image_;
568 void ModifiedItem::modify(ImagePtr const & new_image)
570 if (!new_image.get())
573 original_image_ = new_image;
574 modified_image_.reset(original_image_->clone());
576 if (params().display == GParams::NONE) {
581 setStatus(ScalingEtc);
582 modified_image_->clip(params());
583 modified_image_->rotate(params());
584 modified_image_->scale(params());
589 void ModifiedItem::setPixmap()
591 if (!modified_image_.get())
594 if (params().display == GParams::NONE) {
599 bool const success = modified_image_->setPixmap(params());
604 modified_image_.reset();
605 setStatus(ErrorScalingEtc);
610 void ModifiedItem::setStatus(ImageStatus new_status)
612 status_ = new_status;
614 // Tell the BufferView that the inset has changed.
616 ListType::const_iterator it = insets.begin();
617 ListType::const_iterator end = insets.end();
618 for (; it != end; ++it) {
619 InsetGraphics * inset = const_cast<InsetGraphics *>(*it);
620 current_view->updateInset(inset, false);
627 struct Params_Changed {
629 Params_Changed(GParams const & p) : p_(p) {}
631 bool operator()(InsetGraphics const * inset)
633 string const path = OnlyPath(p_.filename);
634 return GParams(inset->params(), path) != p_;
643 // changeDisplay returns an initialised ModifiedItem if any of the insets
644 // have display == DEFAULT and if that DEFAULT value has changed.
645 // If this occurs, then (this) has these insets removed.
646 ModifiedItemPtr ModifiedItem::changeDisplay()
648 // Loop over the list of insets. Compare the updated params for each
649 // with params(). If different, move into a new list.
650 ListType::iterator begin = insets.begin();
651 ListType::iterator end = insets.end();
652 ListType::iterator it =
653 std::remove_if(begin, end, Params_Changed(params()));
656 // No insets have changed params
657 return ModifiedItemPtr();
660 // it -> end have params that are changed. Move to the new list.
662 new_insets.insert(new_insets.begin(), it, end);
663 insets.erase(it, end);
665 // Create a new ModifiedItem with these new params. Note that
666 // the only params that have changed are the display ones,
667 // so we don't need to crop, rotate, scale.
668 string const path = OnlyPath(p_->filename);
670 ModifiedItemPtr new_item(new ModifiedItem(*this));
671 new_item->insets = new_insets;
672 *(new_item->p_) = GParams((*new_insets.begin())->params(), path);
674 new_item->setPixmap();