]> git.lyx.org Git - lyx.git/blob - src/graphics/GraphicsCache.C
Move connection of the image loader signals to the frontends.
[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/Dialogs.h"
22
23
24 namespace grfx {
25
26 GCache & GCache::get()
27 {
28         static bool start = true;
29         if (start) {
30                 start = false;
31                 Dialogs::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)
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());
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         for (; it != cache->end(); ++it) {
146                 if (it->second->referencedBy(inset))
147                         return it;
148         }
149         
150         return cache->end();
151 }
152
153
154 GCache::CacheType::const_iterator
155 GCache::find(InsetGraphics const & inset) const
156 {
157         CacheType::const_iterator it = cache->begin();
158         for (; it != cache->end(); ++it) {
159                 if (it->second->referencedBy(inset))
160                         return it;
161         }
162         
163         return cache->end();
164 }
165
166 } // namespace grfx