]> git.lyx.org Git - lyx.git/blob - src/graphics/GraphicsCache.C
move inset related stuff from src/graphics to src/inset/
[lyx.git] / src / graphics / GraphicsCache.C
1 /*
2  * \file GraphicsCache.C
3  * Copyright 2002 the LyX Team
4  * Read the file COPYING
5  *
6  * \author Baruch Even <baruch.even@writeme.com>
7  * \author Angus Leeming <a.leeming@ic.ac.uk>
8  */
9
10 #include <config.h>
11
12 #ifdef __GNUG__
13 #pragma implementation
14 #endif
15
16 #include "GraphicsCache.h"
17 #include "GraphicsCacheItem.h"
18 #include "GraphicsImage.h"
19 #include "GraphicsParams.h"
20 #include "insets/insetgraphics.h"
21 #include "frontends/GUIRunTime.h"
22
23
24 namespace grfx {
25
26 GCache & GCache::get()
27 {
28         static bool start = true;
29         if (start) {
30                 start = false;
31                 GUIRunTime::initialiseGraphics();
32         }
33
34         // Now return the cache
35         static GCache singleton;
36         return singleton;
37 }
38
39
40 GCache::GCache()
41 {
42         cache = new CacheType;
43 }
44
45
46 // all elements are destroyed by the shared_ptr's in the map.
47 GCache::~GCache()
48 {
49         delete cache;
50 }
51
52
53 void GCache::update(InsetGraphics const & inset, string const & filepath)
54 {
55         // A subset only of InsetGraphicsParams is needed for display purposes.
56         // The GraphicsParams c-tor also interrogates lyxrc to ascertain whether
57         // to display or not.
58         GParams params = inset.params().asGParams(filepath);
59
60         // Each inset can reference only one file, so check the cache for any
61         // graphics files referenced by inset. If the name of this file is
62         // different from that in params, then remove the reference.
63         CacheType::iterator it = find(inset);
64
65         if (it != cache->end()) {
66                 CacheItemType item = it->second;
67                 if (item->filename() != params.filename) {
68                         item->remove(inset);
69                         if (item->empty())
70                                 cache->erase(it);
71                 }
72         }
73
74         // Are we adding a new file or modifying the display of an existing one?
75         it = cache->find(params.filename);
76
77         if (it != cache->end()) {
78                 it->second->modify(inset, params);
79                 return;
80         }
81
82         CacheItemType item(new GCacheItem(inset, params));
83         if (item.get() != 0)
84                 (*cache)[params.filename] = item;
85 }
86
87
88 void GCache::remove(InsetGraphics const & inset)
89 {
90         CacheType::iterator it = find(inset);
91         if (it == cache->end())
92                 return;
93
94         CacheItemType item = it->second;
95         item->remove(inset);
96         if (item->empty()) {
97                 cache->erase(it);
98         }
99 }
100
101
102 void GCache::startLoading(InsetGraphics const & inset)
103 {
104         CacheType::iterator it = find(inset);
105         if (it == cache->end())
106                 return;
107
108         it->second->startLoading(inset);
109 }
110
111
112 ImagePtr const GCache::image(InsetGraphics const & inset) const
113 {
114         CacheType::const_iterator it = find(inset);
115         if (it == cache->end())
116                 return ImagePtr();
117
118         return it->second->image(inset);
119 }
120
121
122 ImageStatus GCache::status(InsetGraphics const & inset) const
123 {
124         CacheType::const_iterator it = find(inset);
125         if (it == cache->end())
126                 return ErrorUnknown;
127
128         return it->second->status(inset);
129 }
130
131
132 void GCache::changeDisplay(bool changed_background)
133 {
134         CacheType::iterator it = cache->begin();
135         CacheType::iterator end = cache->end();
136         for(; it != end; ++it)
137                 it->second->changeDisplay(changed_background);
138 }
139
140
141 GCache::CacheType::iterator
142 GCache::find(InsetGraphics const & inset)
143 {
144         CacheType::iterator it  = cache->begin();
145         CacheType::iterator end = cache->end();
146         for (; it != end; ++it) {
147                 if (it->second->referencedBy(inset))
148                         return it;
149         }
150
151         return cache->end();
152 }
153
154
155 GCache::CacheType::const_iterator
156 GCache::find(InsetGraphics const & inset) const
157 {
158         CacheType::const_iterator it  = cache->begin();
159         CacheType::const_iterator end = cache->end();
160         for (; it != end; ++it) {
161                 if (it->second->referencedBy(inset))
162                         return it;
163         }
164
165         return cache->end();
166 }
167
168
169 unsigned int GCache::raw_width(string const & filename) const
170 {
171         CacheType::const_iterator it = cache->find(filename);
172         if (it == cache->end())
173                 return 0;
174
175         return it->second->raw_width();
176 }
177
178
179 unsigned int GCache::raw_height(string const & filename) const
180 {
181         CacheType::const_iterator it = cache->find(filename);
182         if (it == cache->end())
183                 return 0;
184
185         return it->second->raw_height();
186 }
187
188
189 std::vector<string> GCache::loadableFormats() const {
190         return GImage::loadableFormats();
191 }
192
193 } // namespace grfx
194