3 * \file GraphicsCacheItem.h
4 * Copyright 2002 the LyX Team
5 * Read the file COPYING
7 * \author Baruch Even <baruch.even@writeme.com>
8 * \author Angus Leeming <a.leeming@ic.ac.uk>
10 * The graphics cache is a container of GCacheItems. Each GCacheItem, defined
11 * here represents a separate image file. However, each file can be viewed in
12 * different ways (different sizes, rotations etc), so each GCacheItem itself
13 * contains a list of ModifiedItems, also defined here. Each ModifiedItem
14 * has a GParams variable that defines the way it will be viewed. It also
15 * contains a list of the graphics insets that refer to it, so calls through
16 * the GCache to GCacheItem ultimately return the loading status and image
17 * for that particular graphics inset.
19 * The graphics cache supports fully asynchronous:
20 * file conversion to a loadable format;
23 * Whether you get that, of course, depends on grfx::GConverter and on the
24 * grfx::GImage-derived image class.
26 * Image modification (scaling, rotation etc) is blocking.
29 #ifndef GRAPHICSCACHEITEM_H
30 #define GRAPHICSCACHEITEM_H
36 #include "GraphicsTypes.h"
39 #include <boost/utility.hpp>
40 #include <boost/smart_ptr.hpp>
41 #include <sigc++/signal_system.h>
50 /// A grfx::GCache item holder.
51 class GCacheItem : boost::noncopyable, public SigC::Object {
53 /// the GCacheItem contains data of this type.
54 typedef boost::shared_ptr<ModifiedItem> ModifiedItemPtr;
57 GCacheItem(InsetGraphics const &, GParams const &);
59 /// The params have changed (but still refer to this file).
60 void modify(InsetGraphics const &, GParams const &);
62 /// Remove the reference to this inset.
63 void remove(InsetGraphics const &);
65 /// It's in the cache. Now start the loading process.
66 void startLoading(InsetGraphics const &);
68 /// Is the cache item referenced by any insets at all?
71 /// The name of the original image file.
72 string const & filename() const;
74 /// Is this image file referenced by this inset?
75 bool referencedBy(InsetGraphics const &) const;
77 /** Returns the image referenced by this inset (or an empty container
78 * if it's not yet loaded.
80 ImagePtr const image(InsetGraphics const &) const;
82 /// The loading status of the image referenced by this inset.
83 ImageStatus status(InsetGraphics const &) const;
85 /** If (changed_background == true), then the background color of the
86 * graphics inset has changed. Update all images.
87 * Else, the preferred display type has changed.
88 * Update the view of all insets whose display type is DEFAULT.
90 void changeDisplay(bool changed_background);
92 /// Used to ascertain the Bounding Box of non (e)ps files.
93 unsigned int raw_width() const;
95 unsigned int raw_height() const;
98 /** Start the image conversion process, checking first that it is
99 * necessary. If it is necessary, then a conversion task is started.
100 * GCacheItem asumes that the conversion is asynchronous and so
101 * passes a Signal to the converting routine. When the conversion
102 * is finished, this Signal is emitted, returning the converted
103 * file to this->imageConverted.
105 * If no file conversion is needed, then convertToDisplayFormat() calls
106 * loadImage() directly.
108 * convertToDisplayFormat() will set the loading status flag as
109 * approriate through calls to setStatus().
111 void convertToDisplayFormat();
113 /** Load the image into memory. This is called either from
114 * convertToDisplayFormat() direct or from imageConverted().
118 /** Get a notification when the image conversion is done.
119 * Connected to a signal on_finish_ which is passed to
120 * GConverter::convert.
122 void imageConverted(string const & file_to_load);
124 /** Get a notification when the image loading is done.
125 * Connected to a signal on_finish_ which is passed to
128 void imageLoaded(bool);
130 /// How far have we got in loading the original, unmodified image?
131 ImageStatus status() const;
133 /** Sets the status of the loading process. Also notifies
134 * listeners that the status has chacnged.
136 void setStatus(ImageStatus new_status);
138 /// The filename we refer too.
140 /// Is the file compressed?
142 /// If so, store the uncompressed file in this temporary file.
143 string unzipped_filename_;
144 /// What file are we trying to load?
145 string file_to_load_;
146 /** Should we delete the file after loading? True if the file is
147 * the result of a conversion process.
149 bool remove_loaded_file_;
151 /// The original, unmodified image and its loading status.
156 /** A SignalLoadTypePtr is connected to this->imageLoaded and
157 * then passed to ImagePtr::load.
158 * When the image has been loaded, the signal is emitted.
160 * We pass a shared_ptr because it is eminently possible for the
161 * ModifiedItem to be destructed before the loading is complete and
162 * the signal must remain in scope. It doesn't matter if the slot
163 * disappears, SigC takes care of that.
165 typedef SigC::Signal1<void, bool> SignalLoadType;
167 typedef boost::shared_ptr<SignalLoadType> SignalLoadTypePtr;
169 /// The connection of the signal passed to ImagePtr::loadImage.
170 SigC::Connection cl_;
172 /** A SignalConvertTypePtr is connected to this->imageConverted and
173 * then passed to GConverter::convert.
174 * When the image has been converted to a loadable format, the signal
175 * is emitted, returning the name of the loadable file to
178 typedef SigC::Signal1<void, string const &> SignalConvertType;
180 typedef boost::shared_ptr<SignalConvertType> SignalConvertTypePtr;
182 /// The connection of the signal passed to GConverter::convert.
183 SigC::Connection cc_;
185 /// The list of all modified images.
186 typedef std::list<ModifiedItemPtr> ListType;
188 ListType modified_images;
196 ModifiedItem(InsetGraphics const &, GParams const &, ImagePtr const &);
199 GParams const & params() { return *p_.get(); }
201 /// Add inset to the list of insets.
202 void add(InsetGraphics const & inset);
204 /// Remove inset from the list of insets.
205 void remove(InsetGraphics const & inset);
208 bool empty() const { return insets.empty(); }
210 /// Is this ModifiedItem referenced by inset?
211 bool referencedBy(InsetGraphics const & inset) const;
214 ImagePtr const image() const;
216 /// How far have we got in loading the modified image?
217 ImageStatus status() const { return status_; }
219 /** Called from GCacheItem once the raw image is loaded.
220 * Modifies the image in accord with p_.
222 void modify(ImagePtr const &);
224 /// Updates the pixmap.
227 /** changeDisplay returns a full ModifiedItemPtr if any of the
228 * insets have display=DEFAULT and if that DEFAULT value has
230 * If this occurs, then this has these insets removed.
232 boost::shared_ptr<ModifiedItem> changeDisplay();
235 typedef std::list<InsetGraphics const *> ListType;
237 /// Make these accessible for changeDisplay.
241 /** Sets the status of the loading process. Also notifies
242 * listeners that the status has changed.
244 void setStatus(ImageStatus new_status);
246 /// The original and modified images and its loading status.
247 ImagePtr original_image_;
249 ImagePtr modified_image_;
253 boost::shared_ptr<GParams> p_;
258 #endif // GRAPHICSCACHEITEM_H