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