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"
23 #include "insets/insetgraphics.h"
25 #include "BufferView.h"
28 #include "lyx_main.h" // for global dispatch method
30 #include "support/LAssert.h"
31 #include "support/filetools.h"
33 #include <boost/bind.hpp>
36 extern BufferView * current_view;
43 GCacheItem::GCacheItem(InsetGraphics const & inset, GParams const & params)
44 : filename_(params.filename), zipped_(false),
45 remove_loaded_file_(false), status_(WaitingToLoad)
47 ModifiedItemPtr item(new ModifiedItem(inset, params, image_));
48 modified_images.push_back(item);
54 typedef GCacheItem::ModifiedItemPtr ModifiedItemPtr;
56 class Compare_Params {
58 Compare_Params(GParams const & p) : p_(p) {}
60 bool operator()(ModifiedItemPtr const & ptr)
64 return ptr->params() == p_;
73 Find_Inset(InsetGraphics const & i) : i_(i) {}
75 bool operator()(ModifiedItemPtr const & ptr)
79 return ptr->referencedBy(i_);
83 InsetGraphics const & i_;
89 void GCacheItem::modify(InsetGraphics const & inset, GParams const & params)
91 // Does this inset currently reference an existing ModifiedItem with
93 // If so, remove the inset from the ModifiedItem's internal list
95 ListType::iterator begin = modified_images.begin();
96 ListType::iterator end = modified_images.end();
97 ListType::iterator it = begin;
99 it = std::find_if(it, end, Find_Inset(inset));
102 if ((*it)->params() != params) {
103 (*it)->remove(inset);
105 it = modified_images.erase(it);
110 // Is there an existing ModifiedItem with these params?
111 // If so, add inset to the list of insets referencing this ModifiedItem
112 begin = modified_images.begin();
113 end = modified_images.end();
114 it = std::find_if(begin, end, Compare_Params(params));
120 // If no ModifiedItem exists with these params, then create one.
121 ModifiedItemPtr item(new ModifiedItem(inset, params, image_));
122 modified_images.push_back(item);
128 void GCacheItem::remove(InsetGraphics const & inset)
130 // search the list of ModifiedItems for one referenced by this inset.
131 // If it is found, remove the reference.
132 // If the ModifiedItem is now referenced by no insets, remove it.
133 ListType::iterator begin = modified_images.begin();
134 ListType::iterator end = modified_images.end();
135 ListType::iterator it = std::find_if(begin, end, Find_Inset(inset));
140 (*it)->remove(inset);
141 if ((*it)->empty()) {
142 modified_images.clear();
147 void GCacheItem::startLoading(InsetGraphics const & inset)
149 if (status() != WaitingToLoad)
152 // Check that the image is referenced by this inset
153 ListType::const_iterator begin = modified_images.begin();
154 ListType::const_iterator end = modified_images.end();
155 ListType::const_iterator it =
156 std::find_if(begin, end, Find_Inset(inset));
161 if ((*it)->params().display == GParams::NONE)
164 convertToDisplayFormat();
168 bool GCacheItem::empty() const
170 return modified_images.empty();
174 bool GCacheItem::referencedBy(InsetGraphics const & inset) const
176 // Is one of the list of ModifiedItems referenced by this inset?
177 ListType::const_iterator begin = modified_images.begin();
178 ListType::const_iterator end = modified_images.end();
179 return std::find_if(begin, end, Find_Inset(inset)) != end;
183 string const & GCacheItem::filename() const
189 ImagePtr const GCacheItem::image(InsetGraphics const & inset) const
191 // find a ModifiedItem that is referenced by this inset.
192 ListType::const_iterator begin = modified_images.begin();
193 ListType::const_iterator end = modified_images.end();
194 ListType::const_iterator it =
195 std::find_if(begin, end, Find_Inset(inset));
197 // Someone's being daft.
201 // We are expressly requested to not render the image
202 if ((*it)->params().display == GParams::NONE)
205 // If the original image has been loaded, return what's going on
206 // in the ModifiedItem
207 if (status() == Loaded)
208 return (*it)->image();
214 ImageStatus GCacheItem::status(InsetGraphics const & inset) const
216 // find a ModifiedItem that is referenced by this inset.
217 ListType::const_iterator begin = modified_images.begin();
218 ListType::const_iterator end = modified_images.end();
219 ListType::const_iterator it =
220 std::find_if(begin, end, Find_Inset(inset));
222 // Someone's being daft.
226 if (status() == Loaded)
227 return (*it)->status();
233 // Called internally only. Use to ascertain the status of the loading of the
234 // original image. No scaling etc.
235 ImageStatus GCacheItem::status() const
241 void GCacheItem::setStatus(ImageStatus new_status)
243 status_ = new_status;
245 // Loop over all insets and tell the BufferView that it has changed.
246 typedef ModifiedItem::ListType::const_iterator inset_iterator;
248 ListType::const_iterator it = modified_images.begin();
249 ListType::const_iterator end = modified_images.end();
250 for (; it != end; ++it) {
251 inset_iterator it2 = (*it)->insets.begin();
252 inset_iterator end2 = (*it)->insets.end();
254 for (; it2 != end2; ++it2) {
255 InsetGraphics * inset =
256 const_cast<InsetGraphics *>(*it2);
258 // Use of current_view is very, very Evil!!
259 current_view->updateInset(inset, false);
265 void GCacheItem::changeDisplay(bool changed_background)
267 ListType::iterator begin = modified_images.begin();
268 ListType::iterator end = modified_images.end();
270 // The background has changed. Change all modified images.
271 if (changed_background) {
272 for (ListType::iterator it = begin; it != end; ++it) {
280 for (ListType::iterator it = begin; it != end; ++it) {
281 // ModifiedItem::changeDisplay returns a full
282 // ModifiedItemPtr if any of the insets have display=DEFAULT
283 // and if that DEFAULT value has changed
284 ModifiedItemPtr new_item = (*it)->changeDisplay();
288 temp_list.push_back(new_item);
290 // The original store may now be empty
291 if ((*it)->insets.empty()) {
292 it = modified_images.erase(it);
296 if (temp_list.empty())
299 // Recombine new_list and modified_images.
300 begin = modified_images.begin();
301 end = modified_images.end();
303 ListType::const_iterator tbegin = temp_list.begin();
304 ListType::const_iterator tend = temp_list.end();
306 ListType append_list;
308 for (ListType::const_iterator tit = tbegin; tit != tend; ++tit) {
309 GParams const & params = (*tit)->params();
310 ListType::iterator it =
311 std::find_if(begin, end, Compare_Params(params));
313 append_list.push_back(*tit);
315 (*it)->insets.merge((*tit)->insets);
318 if (append_list.empty())
321 modified_images.splice(modified_images.end(), append_list);
325 void GCacheItem::imageConverted(string const & file_to_load)
328 (!file_to_load.empty() && IsFileReadable(file_to_load));
330 string const text = success ? "succeeded" : "failed";
331 lyxerr[Debug::GRAPHICS] << "Image conversion " << text << "." << endl;
334 setStatus(ErrorConverting);
337 lyx::unlink(unzipped_filename_);
344 // Do the actual image loading from file to memory.
345 file_to_load_ = file_to_load;
351 // This function gets called from the callback after the image has been
352 // converted successfully.
353 void GCacheItem::loadImage()
356 lyxerr[Debug::GRAPHICS] << "Loading image." << endl;
358 // Connect a signal to this->imageLoaded and pass this signal to
359 // GImage::loadImage.
360 SignalLoadTypePtr on_finish;
361 on_finish.reset(new SignalLoadType);
362 cl_ = on_finish->connect(boost::bind(&GCacheItem::imageLoaded, this, _1));
364 image_ = GImage::newImage();
365 image_->load(file_to_load_, on_finish);
369 void GCacheItem::imageLoaded(bool success)
371 string const text = success ? "succeeded" : "failed";
372 lyxerr[Debug::GRAPHICS] << "Image loading " << text << "." << endl;
374 // Clean up after loading.
376 lyx::unlink(unzipped_filename_);
378 if (remove_loaded_file_ && unzipped_filename_ != file_to_load_)
379 lyx::unlink(file_to_load_);
384 setStatus(ErrorLoading);
390 // Loop over the list of modified images and create them.
391 ListType::iterator it = modified_images.begin();
392 ListType::iterator end = modified_images.end();
393 for (; it != end; ++it) {
394 (*it)->modify(image_);
399 unsigned int GCacheItem::raw_width() const
404 return image_->getWidth();
408 unsigned int GCacheItem::raw_height() const
413 return image_->getHeight();
419 string const findTargetFormat(string const & from)
421 typedef GImage::FormatList FormatList;
422 FormatList const formats = GImage::loadableFormats();
424 // There must be a format to load from.
425 lyx::Assert(!formats.empty());
427 // First ascertain if we can load directly with no conversion
428 FormatList::const_iterator it1 = formats.begin();
429 FormatList::const_iterator end = formats.end();
430 for (; it1 != end; ++it1) {
435 // So, we have to convert to a loadable format. Can we?
436 grfx::GConverter const & graphics_converter = grfx::GConverter::get();
438 FormatList::const_iterator it2 = formats.begin();
439 for (; it2 != end; ++it2) {
440 if (graphics_converter.isReachable(from, *it2))
444 // Failed! so we have to try to convert it to XPM format
445 // with the standard converter
446 return string("xpm");
452 void GCacheItem::convertToDisplayFormat()
454 setStatus(Converting);
455 // Make a local copy in case we unzip it
456 string const filename = zippedFile(filename_) ?
457 unzipFile(filename_) : filename_;
458 string const displayed_filename = MakeDisplayPath(filename_);
459 lyxerr[Debug::GRAPHICS] << "[GrahicsCacheItem::convertToDisplayFormat]\n"
460 << "\tAttempting to convert image file: " << filename
461 << "\n\twith displayed filename: " << displayed_filename
464 // First, check that the file exists!
465 if (!IsFileReadable(filename)) {
466 setStatus(ErrorNoFile);
467 lyxerr[Debug::GRAPHICS] << "\tThe file is not readable" << endl;
471 string from = getExtFromContents(filename);
472 // Some old ps-files make problems, so we do not need direct
473 // loading of an ps-file
475 lyxerr[Debug::GRAPHICS]
476 << "\n\tThe file contains PostScript format data.\n"
477 << "\tchanging it to eps-format to get it converted to xpm\n";
480 lyxerr[Debug::GRAPHICS]
481 << "\n\tThe file contains " << from << " format data." << endl;
482 string const to = grfx::findTargetFormat(from);
485 // No conversion needed!
486 lyxerr[Debug::GRAPHICS] << "\tNo conversion needed (from == to)!" << endl;
487 file_to_load_ = filename;
492 lyxerr[Debug::GRAPHICS] << "\tConverting it to " << to << " format." << endl;
493 // Take only the filename part of the file, without path or extension.
494 string const temp = ChangeExtension(OnlyFilename(filename), string());
496 // Add some stuff to create a uniquely named temporary file.
497 // This file is deleted in loadImage after it is loaded into memory.
498 string const to_file_base = lyx::tempName(string(), temp);
499 remove_loaded_file_ = true;
501 // Remove the temp file, we only want the name...
502 lyx::unlink(to_file_base);
504 // Connect a signal to this->imageConverted and pass this signal to
505 // the graphics converter so that we can load the modified file
506 // on completion of the conversion process.
507 SignalConvertTypePtr on_finish;
508 on_finish.reset(new SignalConvertType);
509 cc_ = on_finish->connect(boost::bind(&GCacheItem::imageConverted, this, _1));
511 GConverter & graphics_converter = GConverter::get();
512 graphics_converter.convert(filename, to_file_base, from, to, on_finish);
516 ModifiedItem::ModifiedItem(InsetGraphics const & new_inset,
517 GParams const & new_params,
518 ImagePtr const & new_image)
519 : status_(ScalingEtc)
521 p_.reset(new GParams(new_params));
522 insets.push_back(&new_inset);
527 void ModifiedItem::add(InsetGraphics const & inset)
529 insets.push_back(&inset);
534 void ModifiedItem::remove(InsetGraphics const & inset)
536 ListType::iterator begin = insets.begin();
537 ListType::iterator end = insets.end();
538 ListType::iterator it = std::remove(begin, end, &inset);
539 insets.erase(it, end);
543 bool ModifiedItem::referencedBy(InsetGraphics const & inset) const
545 ListType::const_iterator begin = insets.begin();
546 ListType::const_iterator end = insets.end();
547 return std::find(begin, end, &inset) != end;
551 ImagePtr const ModifiedItem::image() const
553 if (modified_image_.get())
554 return modified_image_;
556 return original_image_;
560 void ModifiedItem::modify(ImagePtr const & new_image)
562 if (!new_image.get())
565 original_image_ = new_image;
566 modified_image_.reset(original_image_->clone());
568 if (params().display == GParams::NONE) {
573 setStatus(ScalingEtc);
574 modified_image_->clip(params());
575 modified_image_->rotate(params());
576 modified_image_->scale(params());
581 void ModifiedItem::setPixmap()
583 if (!modified_image_.get())
586 if (params().display == GParams::NONE) {
591 bool const success = modified_image_->setPixmap(params());
596 modified_image_.reset();
597 setStatus(ErrorScalingEtc);
602 void ModifiedItem::setStatus(ImageStatus new_status)
604 status_ = new_status;
606 // Tell the BufferView that the inset has changed.
608 ListType::const_iterator it = insets.begin();
609 ListType::const_iterator end = insets.end();
610 for (; it != end; ++it) {
611 InsetGraphics * inset = const_cast<InsetGraphics *>(*it);
612 current_view->updateInset(inset, false);
619 struct Params_Changed {
621 Params_Changed(GParams const & p) : p_(p) {}
623 bool operator()(InsetGraphics const * inset)
625 string const path = OnlyPath(p_.filename);
626 return inset->params().asGParams(path) != p_;
635 // changeDisplay returns an initialised ModifiedItem if any of the insets
636 // have display == DEFAULT and if that DEFAULT value has changed.
637 // If this occurs, then (this) has these insets removed.
638 ModifiedItemPtr ModifiedItem::changeDisplay()
640 // Loop over the list of insets. Compare the updated params for each
641 // with params(). If different, move into a new list.
642 ListType::iterator begin = insets.begin();
643 ListType::iterator end = insets.end();
644 ListType::iterator it =
645 std::remove_if(begin, end, Params_Changed(params()));
648 // No insets have changed params
649 return ModifiedItemPtr();
652 // it -> end have params that are changed. Move to the new list.
654 new_insets.insert(new_insets.begin(), it, end);
655 insets.erase(it, end);
657 // Create a new ModifiedItem with these new params. Note that
658 // the only params that have changed are the display ones,
659 // so we don't need to crop, rotate, scale.
660 string const path = OnlyPath(p_->filename);
662 ModifiedItemPtr new_item(new ModifiedItem(*this));
663 new_item->insets = new_insets;
664 *(new_item->p_) = (*new_insets.begin())->params().asGParams(path);
666 new_item->setPixmap();