]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QLImage.C
qtabular skeleton
[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_),
103           xformed_pixmap_(other.xformed_pixmap_)
104 {
105 }
106
107
108 QLImage::~QLImage()
109 {
110 }
111
112
113 Image * QLImage::clone() const
114 {
115         return new QLImage(*this);
116 }
117
118
119 unsigned int QLImage::getWidth() const
120 {
121         return xformed_pixmap_.width();
122 }
123
124
125 unsigned int QLImage::getHeight() const
126 {
127         return xformed_pixmap_.height();
128 }
129
130
131 void QLImage::load(string const & filename)
132 {
133         if (!pixmap_.isNull()) {
134                 lyxerr[Debug::GRAPHICS]
135                         << "Image is loaded already!" << endl;
136                 finishedLoading(false);
137                 return;
138         }
139
140         if (!pixmap_.load(filename.c_str())) {
141                 lyxerr[Debug::GRAPHICS]
142                         << "Unable to open image" << endl;
143                 finishedLoading(false);
144                 return;
145         }
146         lyxerr[Debug::GRAPHICS] << "just Loaded." << endl;
147         xformed_pixmap_ = pixmap_;
148         lyxerr[Debug::GRAPHICS] << "pixmap isNull " << pixmap_.isNull()
149                 << " xformed_pixmap_ isNull " << xformed_pixmap_.isNull() << endl;
150         finishedLoading(true);
151 }
152
153
154 bool QLImage::setPixmap(Params const & params)
155 {
156         lyxerr[Debug::GRAPHICS] << "pixmap isNull " << pixmap_.isNull()
157                 << " xformed_pixmap_ isNull " << xformed_pixmap_.isNull() << endl;
158         if (pixmap_.isNull() || params.display == NoDisplay)
159                 return false;
160
161         lyxerr[Debug::GRAPHICS] << "setPixmap()" << endl;
162
163 // FIXME
164 #if 0
165         int color_key;
166         switch (params.display) {
167         case MonochromeDisplay:
168                 color_key = FL_IMAGE_MONO;
169                 break;
170         case GrayscaleDisplay:
171                 color_key = FL_IMAGE_GRAY;
172                 break;
173         case ColorDisplay:
174         default: // NoDisplay cannot happen!
175                 color_key = FL_IMAGE_RGB;
176                 break;
177         }
178
179         if (color_key != FL_IMAGE_RGB) {
180                 flimage_convert(image_, color_key, 0);
181         }
182
183         unsigned int fill = packedcolor(LColor::graphicsbg);
184         if (fill != image_->fill_color) {
185                 // the background color has changed.
186                 // Note that in grayscale/monochrome images the background is
187                 // grayed also, so this call will have no visible effect. Sorry!
188                 flimage_replace_pixel(image_, image_->fill_color, fill);
189                 image_->fill_color = fill;
190         }
191 #endif
192
193         return true;
194 }
195
196
197 void QLImage::clip(Params const & params)
198 {
199         if (xformed_pixmap_.isNull())
200                 return;
201
202         if (params.bb.empty())
203                 // No clipping is necessary.
204                 return;
205
206         int const new_width  = params.bb.xr - params.bb.xl;
207         int const new_height = params.bb.yt - params.bb.yb;
208
209         // No need to check if the width, height are > 0 because the
210         // Bounding Box would be empty() in this case.
211         if (new_width > pixmap_.width() || new_height > pixmap_.height()) {
212                 // Bounds are invalid.
213                 return;
214         }
215
216         if (new_width == pixmap_.width() && new_height == pixmap_.height())
217                 return;
218
219         int const xoffset_l = params.bb.xl;
220         int const yoffset_t = ( pixmap_.height() > params.bb.yt ?
221                                 pixmap_.height() - params.bb.yt : 0 );
222
223         xformed_pixmap_.resize(new_width, new_height);
224         QPainter p;
225         p.begin(&xformed_pixmap_);
226         p.drawPixmap(0, 0, pixmap_, xoffset_l, yoffset_t, new_width, new_height);
227         p.end();
228 }
229
230
231 void QLImage::rotate(Params const & params)
232 {
233         if (xformed_pixmap_.isNull())
234                 return;
235
236         if (!params.angle)
237                 return;
238
239         // The angle passed to flimage_rotate is the angle in one-tenth of a
240         // degree units.
241
242         lyxerr[Debug::GRAPHICS] << "rotating image by " << params.angle << " degrees" << endl;
243
244         QWMatrix m;
245         m.rotate(-params.angle);
246         xformed_pixmap_ = xformed_pixmap_.xForm(m);
247 }
248
249
250 void QLImage::scale(Params const & params)
251 {
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