]> git.lyx.org Git - lyx.git/blob - src/graphics/GraphicsLoader.C
redraw fix 1.
[lyx.git] / src / graphics / GraphicsLoader.C
1 /*
2  * \file GraphicsLoader.C
3  * Copyright 2002 the LyX Team
4  * Read the file COPYING
5  *
6  * \author Angus Leeming <leeming@lyx.org>
7  */
8
9 #include <config.h>
10
11 #ifdef __GNUG__
12 #pragma implementation
13 #endif
14
15 #include "GraphicsLoader.h"
16 #include "GraphicsCache.h"
17 #include "GraphicsCacheItem.h"
18 #include "GraphicsImage.h"
19 #include "GraphicsParams.h"
20
21 #include <boost/bind.hpp>
22 #include <boost/signals/trackable.hpp>
23
24 namespace grfx {
25
26 struct Loader::Impl : boost::signals::trackable {
27         ///
28         Impl(Loader &, Params const &);
29         ///
30         ~Impl();
31         ///
32         void resetFile(string const &);
33         ///
34         void resetParams(Params const &);
35         ///
36         void createPixmap();
37
38         /// The loading status of the image.
39         ImageStatus status_;
40         /** Must store a copy of the cached item to ensure that it is not
41          *  erased unexpectedly by the cache itself.
42          */
43         Cache::ItemPtr cached_item_;
44         /// We modify a local copy of the image once it is loaded.
45         Image::ImagePtr image_;
46
47 private:
48         ///
49         void statusChanged();
50         
51         ///
52         Params params_;
53         ///
54         Loader & parent_;
55 };
56
57
58 Loader::Impl::Impl(Loader & parent, Params const & params)
59         : status_(WaitingToLoad), params_(params), parent_(parent)
60 {}
61
62
63 Loader::Impl::~Impl()
64 {
65         resetFile(string());
66 }
67
68
69 void Loader::Impl::resetFile(string const & file)
70 {
71         string const old_file = cached_item_.get() ?
72                 cached_item_->filename() : string();
73
74         if (file == old_file)
75                 return;
76
77         if (!old_file.empty()) {
78                 cached_item_.reset();
79                 Cache::get().remove(old_file);
80         }
81
82         status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
83         image_.reset();
84         
85         if (cached_item_.get() || file.empty())
86                 return;
87
88         Cache & gc = Cache::get();
89         if (!gc.inCache(file))
90                 gc.add(file);
91
92         // We /must/ make a local copy of this.
93         cached_item_ = gc.item(file);
94         status_ = cached_item_->status();
95
96         cached_item_->statusChanged.connect(
97                 boost::bind(&Impl::statusChanged, this));
98 }
99
100
101 void Loader::Impl::resetParams(Params const & params)
102 {
103         if (params == params_)
104                 return;
105
106         params_ = params;
107         status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
108         image_.reset();
109 }
110
111
112 void Loader::Impl::statusChanged()
113 {
114         status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
115         createPixmap();
116         parent_.statusChanged();
117 }
118
119
120 void Loader::Impl::createPixmap()
121 {
122         if (!cached_item_.get() || image_.get() ||
123             params_.display == NoDisplay || status_ != Loaded)
124                 return;
125
126         image_.reset(cached_item_->image()->clone());
127
128         // These do nothing if there's nothing to do
129         image_->clip(params_);
130         image_->rotate(params_);
131         image_->scale(params_);
132
133         bool const success = image_->setPixmap(params_);
134
135         if (success) {
136                 status_ = Ready;
137         } else {
138                 image_.reset();
139                 status_ = ErrorGeneratingPixmap;
140         }
141 }
142
143
144 Loader::Loader()
145         : pimpl_(new Impl(*this, Params()))
146 {}
147
148
149 Loader::Loader(string const & file, DisplayType type)
150         : pimpl_(new Impl(*this, Params()))
151 {
152         reset(file, type);
153 }
154
155
156 Loader::Loader(string const & file, Params const & params)
157         : pimpl_(new Impl(*this, params))
158 {
159         reset(file, params);
160 }
161
162
163 Loader::~Loader()
164 {}
165
166
167 void Loader::reset(string const & file, DisplayType type)
168 {
169         Params params;
170         params.display = type;
171         pimpl_->resetParams(params);
172
173         pimpl_->resetFile(file);
174         pimpl_->createPixmap();
175 }
176
177
178 void Loader::reset(string const & file, Params const & params)
179 {
180         pimpl_->resetParams(params);
181         pimpl_->resetFile(file);
182         pimpl_->createPixmap();
183 }
184
185
186 void Loader::reset(Params const & params)
187 {
188         pimpl_->resetParams(params);
189         pimpl_->createPixmap();
190 }
191
192
193 void Loader::startLoading()
194 {
195         if (pimpl_->status_ != WaitingToLoad || !pimpl_->cached_item_.get())
196                 return;
197         pimpl_->cached_item_->startLoading();
198 }
199
200
201 string const & Loader::filename() const
202 {
203         static string const empty;
204         return pimpl_->cached_item_.get() ?
205                 pimpl_->cached_item_->filename() : empty;
206 }
207
208
209 ImageStatus Loader::status() const
210 {
211         return pimpl_->status_;
212 }
213
214
215 Image const * Loader::image() const
216 {
217         return pimpl_->image_.get();
218 }
219
220 } // namespace grfx