]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QLImage.C
Rob's followup (nr. 1 ;)
[lyx.git] / src / frontends / qt2 / QLImage.C
1 /*
2  * \file QLImage.C
3  * Copyright 2002 the LyX Team
4  * Read the file COPYING
5  *
6  * \author Angus Leeming, a.leeming@ic.ac.uk
7  * \author John Levon <moz@compsoc.man.ac.uk>
8  */
9
10 #include <config.h>
11
12 #ifdef __GNUG__
13 #pragma implementation
14 #endif
15
16 #include "QLImage.h"
17 #include "graphics/GraphicsParams.h"
18 #include "converter.h"
19 #include "debug.h"
20 #include "support/LAssert.h"
21 #include "support/lyxfunctional.h"  // compare_memfun
22
23 #include <qimage.h>
24 #include <qwmatrix.h>
25 #include <qpainter.h>
26
27 #include <boost/tuple/tuple.hpp>
28
29 using std::find_if;
30 using std::endl;
31
32 namespace grfx {
33
34 /// Access to this class is through this static method.
35 Image::ImagePtr QLImage::newImage()
36 {
37         ImagePtr ptr;
38         ptr.reset(new QLImage);
39         return ptr;
40 }
41
42
43 /// Return the list of loadable formats.
44 Image::FormatList QLImage::loadableFormats()
45 {
46         static FormatList fmts;
47  
48         if (!fmts.empty())
49                 return fmts;
50
51         // The formats recognised by LyX
52         Formats::const_iterator begin = formats.begin();
53         Formats::const_iterator end   = formats.end();
54
55         lyxerr[Debug::GRAPHICS]
56                 << "\nThe image loader can load the following directly:\n";
57
58         QStrList qt_formats = QImageIO::inputFormats();
59  
60         QStrListIterator it(qt_formats);
61
62         for (; it.current(); ++it) {
63            lyxerr[Debug::GRAPHICS] << it.current() << endl;
64
65                 string ext = lowercase(it.current());
66          
67                 // special case
68                 if (ext == "jpeg")
69                         ext = "jpg";
70
71                 Formats::const_iterator fit =
72                         find_if(begin, end, lyx::compare_memfun(&Format::extension, ext));
73                 if (fit != end)
74                         fmts.push_back(fit->name());
75         }
76
77         lyxerr[Debug::GRAPHICS]
78                 << "\nOf these, LyX recognises the following formats:\n";
79
80         FormatList::const_iterator fbegin = fmts.begin();
81         FormatList::const_iterator fend   = fmts.end();
82         for (FormatList::const_iterator fit = fbegin; fit != fend; ++fit) {
83                 if (fit != fbegin)
84                         lyxerr[Debug::GRAPHICS] << ", ";
85                 lyxerr[Debug::GRAPHICS] << *fit;
86         }
87         lyxerr[Debug::GRAPHICS] << '\n' << endl;
88
89         return fmts;
90 }
91
92
93 QLImage::QLImage()
94         : Image()
95 {
96 }
97
98
99 QLImage::QLImage(QLImage const & other)
100         : Image(other), pixmap_(other.pixmap_), xformed_pixmap_(other.xformed_pixmap_)
101 {
102 }
103
104
105 QLImage::~QLImage()
106 {
107 }
108
109
110 Image * QLImage::clone() const
111 {
112         return new QLImage(*this);
113 }
114
115
116 unsigned int QLImage::getWidth() const
117 {
118         return xformed_pixmap_.width();
119 }
120
121
122 unsigned int QLImage::getHeight() const
123 {
124         return xformed_pixmap_.height();
125 }
126
127
128 void QLImage::load(string const & filename)
129 {
130         if (!pixmap_.isNull()) {
131                 lyxerr[Debug::GRAPHICS]
132                         << "Image is loaded already!" << endl;
133                 finishedLoading(false);
134                 return;
135         }
136
137         if (!pixmap_.load(filename.c_str())) {
138                 lyxerr[Debug::GRAPHICS]
139                         << "Unable to open image" << endl;
140                 finishedLoading(false);
141                 return;
142         }
143         lyxerr[Debug::GRAPHICS] << "just Loaded." << endl; 
144         xformed_pixmap_ = pixmap_;
145         lyxerr[Debug::GRAPHICS] << "pixmap isNull " << pixmap_.isNull()
146                 << " xformed_pixmap_ isNull " << xformed_pixmap_.isNull() << endl;
147         finishedLoading(true); 
148 }
149
150
151 bool QLImage::setPixmap(Params const & params)
152 {
153         lyxerr[Debug::GRAPHICS] << "pixmap isNull " << pixmap_.isNull()
154                 << " xformed_pixmap_ isNull " << xformed_pixmap_.isNull() << endl;
155         if (pixmap_.isNull() || params.display == NoDisplay)
156                 return false;
157
158         lyxerr[Debug::GRAPHICS] << "setPixmap()" << endl;
159  
160 // FIXME
161 #if 0  
162         int color_key;
163         switch (params.display) {
164         case MonochromeDisplay:
165                 color_key = FL_IMAGE_MONO;
166                 break;
167         case GrayscaleDisplay:
168                 color_key = FL_IMAGE_GRAY;
169                 break;
170         case ColorDisplay:
171         default: // NoDisplay cannot happen!
172                 color_key = FL_IMAGE_RGB;
173                 break;
174         }
175
176         if (color_key != FL_IMAGE_RGB) {
177                 flimage_convert(image_, color_key, 0);
178         }
179
180         unsigned int fill = packedcolor(LColor::graphicsbg);
181         if (fill != image_->fill_color) {
182                 // the background color has changed.
183                 // Note that in grayscale/monochrome images the background is
184                 // grayed also, so this call will have no visible effect. Sorry!
185                 flimage_replace_pixel(image_, image_->fill_color, fill);
186                 image_->fill_color = fill;
187         }
188 #endif 
189
190         return true;
191 }
192
193
194 void QLImage::clip(Params const & params)
195 {
196         lyxerr << "clip isNull " << pixmap_.isNull() << ", " <<xformed_pixmap_.isNull() << endl; 
197         if (xformed_pixmap_.isNull())
198                 return;
199
200         if (params.bb.empty())
201                 // No clipping is necessary.
202                 return;
203
204         int const new_width  = params.bb.xr - params.bb.xl;
205         int const new_height = params.bb.yt - params.bb.yb;
206
207         // No need to check if the width, height are > 0 because the
208         // Bounding Box would be empty() in this case.
209         if (new_width > pixmap_.width() || new_height > pixmap_.height()) {
210                 // Bounds are invalid.
211                 return;
212         }
213
214         if (new_width == pixmap_.width() && new_height == pixmap_.height())
215                 return;
216
217         int const xoffset_l = params.bb.xl;
218         int const yoffset_t = ( pixmap_.height() > params.bb.yt ?
219                                 pixmap_.height() - params.bb.yt : 0 );
220
221         xformed_pixmap_.resize(new_width, new_height);
222         QPainter p;
223         p.begin(&xformed_pixmap_);
224         p.drawPixmap(0, 0, pixmap_, xoffset_l, yoffset_t, new_width, new_height);
225         p.end();
226 }
227
228
229 void QLImage::rotate(Params const & params)
230 {
231         lyxerr << "rotate isNull " << pixmap_.isNull() << ", " <<xformed_pixmap_.isNull() << endl; 
232         if (xformed_pixmap_.isNull())
233                 return;
234
235         if (!params.angle)
236                 return;
237
238         // The angle passed to flimage_rotate is the angle in one-tenth of a
239         // degree units.
240
241         lyxerr[Debug::GRAPHICS] << "rotating image by " << params.angle << " degrees" << endl;
242  
243         QWMatrix m;
244         m.rotate(-params.angle);
245         xformed_pixmap_ = xformed_pixmap_.xForm(m);
246 }
247
248
249 void QLImage::scale(Params const & params)
250 {
251         lyxerr << "scale isNull " << pixmap_.isNull() << ", " <<xformed_pixmap_.isNull() << endl; 
252         if (xformed_pixmap_.isNull())
253                 return;
254
255         unsigned int width;
256         unsigned int height;
257         boost::tie(width, height) = getScaledDimensions(params);
258
259         if (width == getWidth() && height == getHeight())
260                 return;
261
262         lyxerr[Debug::GRAPHICS] << "resizing image to " << width << "(" <<
263                 (double(width)/getWidth()) << ")," << height << "(" <<
264                 (double(height)/getHeight()) << ")" << endl;
265         QWMatrix m;
266         m.scale(double(width) / getWidth(), double(height) / getHeight());
267         xformed_pixmap_ = xformed_pixmap_.xForm(m);
268 }
269
270 } // namespace grfx