]> git.lyx.org Git - lyx.git/blob - src/graphics/GraphicsCacheItem.C
Add LColor::graphicsbg and use it in the graphics and figure insets.
[lyx.git] / src / graphics / GraphicsCacheItem.C
1 /* This file is part of
2  * =================================================
3  * 
4  *          LyX, The Document Processor
5  *          Copyright 1995 Matthias Ettrich.
6  *          Copyright 1995-2001 The LyX Team.
7  *
8  *          This file Copyright 2000 Baruch Even
9  * ================================================= */
10
11 #include <config.h>
12
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include "graphics/GraphicsCache.h"
18 #include "graphics/GraphicsCacheItem.h"
19 #include "frontends/support/LyXImage.h"
20 #include "graphics/ImageLoaderXPM.h"
21 #include "converter.h"
22 #include "support/filetools.h"
23 #include "support/lyxlib.h"
24 #include "lyx_gui_misc.h"
25 #include "debug.h"
26 #include "support/LAssert.h"
27 #include "gettext.h"
28
29 using std::endl;
30
31 /*
32  * The order of conversion:
33  *
34  * The c-tor calls convertImage()
35  * 
36  * convertImage() verifies that we need to do conversion, if not it will just
37  * call the loadImage()
38  * if conversion is needed, it will initiate the conversion.
39  *
40  * When the conversion is completed imageConverted() is called, which in turn
41  * calls loadImage().
42  *
43  * Since we currently do everything synchronously, convertImage() calls
44  * imageConverted() right after it does the call to the conversion process.
45 */
46
47 GraphicsCacheItem::GraphicsCacheItem(string const & filename)
48         : imageStatus_(GraphicsCacheItem::Loading)
49 {
50         filename_ = filename;
51         
52         bool success = convertImage(filename);
53         if (! success) // Conversion failed miserably (couldn't even start).
54                 setStatus(ErrorConverting);
55 }
56
57
58 GraphicsCacheItem::~GraphicsCacheItem()
59 {}
60
61
62 GraphicsCacheItem::ImageStatus 
63 GraphicsCacheItem::getImageStatus() const { return imageStatus_; }
64
65
66 void GraphicsCacheItem::setStatus(ImageStatus new_status)
67 {
68         imageStatus_ = new_status;
69 }
70
71
72 LyXImage * 
73 GraphicsCacheItem::getImage() const { return image_.get(); }
74
75
76 void
77 GraphicsCacheItem::imageConverted(bool success)
78 {
79         // Debug output
80         string text = "succeeded";
81         if (!success)
82                 text = "failed";
83         lyxerr << "imageConverted, conversion " << text << "." << endl;
84
85         if (! success) {
86                 lyxerr << "(GraphicsCacheItem::imageConverter) "
87                         "Error converting image." << endl;
88                 setStatus(GraphicsCacheItem::ErrorConverting);
89                 return;
90         }
91
92         // Do the actual image loading from file to memory.
93         loadImage();    
94 }
95
96
97 namespace {
98
99 string const findTargetFormat(string const & from)
100 {
101         typedef ImageLoader::FormatList FormatList;
102         FormatList formats = ImageLoaderXPM().loadableFormats();
103         lyx::Assert(formats.size() > 0); // There must be a format to load from.
104
105         FormatList::const_iterator iter = formats.begin();
106         FormatList::const_iterator end  = formats.end();
107
108         for (; iter != end; ++iter) {
109                 if (converters.isReachable(from, *iter))
110                         break;
111         }
112         
113         if (iter == end) {
114                 // We do not know how to convert the image to something loadable.
115                 lyxerr << "ERROR: Do not know how to convert image." << std::endl;
116                 return string();
117         }
118
119         return (*iter);
120 }
121
122 } // anon namespace
123
124         
125 bool
126 GraphicsCacheItem::convertImage(string const & filename)
127 {
128         string const from = GetExtension(filename);
129         string const to = findTargetFormat(from);
130         if (to.empty()) 
131                 return false;
132
133         if (from == to) {
134                 // No conversion needed!
135                 // Saves more than just time: prevents the deletion of
136                 // the "to" file after loading when it's the same as the "from"!
137                 tempfile = filename;
138                 loadImage();    
139                 return true;
140         }
141
142         // Take only the filename part of the file, without path or extension.
143         string temp = OnlyFilename(filename);
144         temp = ChangeExtension(filename, string());
145         
146         // Add some stuff to have it a unique temp file.
147         // This tempfile is deleted in loadImage after it is loaded to memory.
148         tempfile = lyx::tempName(string(), temp);
149         // Remove the temp file, we only want the name...
150         lyx::unlink(tempfile);
151
152         bool result = converters.convert(0, filename, tempfile, from, to);
153         tempfile.append(".xpm");
154
155         // For now we are synchronous
156         imageConverted(result);
157
158         // Cleanup after the conversion.
159         lyx::unlink(tempfile);
160         tempfile = string();
161
162         return true;
163 }
164
165
166 // This function gets called from the callback after the image has been
167 // converted successfully.
168 void
169 GraphicsCacheItem::loadImage()
170 {
171         lyxerr << "Loading XPM Image... ";
172
173         ImageLoaderXPM imageLoader;
174         if (imageLoader.loadImage(tempfile) == ImageLoader::OK) {
175                 lyxerr << "Success." << endl;
176                 image_.reset(imageLoader.getImage());
177                 setStatus(GraphicsCacheItem::Loaded);
178         } else {
179                 lyxerr << "Loading " << tempfile << "Failed" << endl;
180                 setStatus(GraphicsCacheItem::ErrorReading);
181         }
182 }