]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QLImage.C
Remove some more debug
[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
15 #include "QLImage.h"
16 #include "graphics/GraphicsParams.h"
17 #include "format.h"
18 #include "debug.h"
19 #include "support/LAssert.h"
20 #include "support/lyxfunctional.h"  // compare_memfun
21 #include "qt_helpers.h"
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         if (lyxerr.debugging()) {
78                 lyxerr[Debug::GRAPHICS]
79                         << "\nOf these, LyX recognises the following formats:\n";
80
81                 FormatList::const_iterator fbegin = fmts.begin();
82                 FormatList::const_iterator fend   = fmts.end();
83                 for (FormatList::const_iterator fit = fbegin; fit != fend; ++fit) {
84                         if (fit != fbegin)
85                                 lyxerr[Debug::GRAPHICS] << ", ";
86                         lyxerr[Debug::GRAPHICS] << *fit;
87                 }
88                 lyxerr[Debug::GRAPHICS] << '\n' << endl;
89         }
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(toqstr(filename))) {
141                 lyxerr[Debug::GRAPHICS]
142                         << "Unable to open image" << endl;
143                 finishedLoading(false);
144                 return;
145         }
146         xformed_pixmap_ = pixmap_;
147         finishedLoading(true);
148 }
149
150
151 bool QLImage::setPixmap(Params const & params)
152 {
153         if (pixmap_.isNull() || params.display == NoDisplay)
154                 return false;
155
156         // FIXME: it's a fake kind of grayscale !
157
158         switch (params.display) {
159                 case GrayscaleDisplay:
160                 case MonochromeDisplay: {
161                         QImage i(xformed_pixmap_.convertToImage());
162                         xformed_pixmap_.convertFromImage(i, QPixmap::Mono);
163                         break;
164                 }
165
166                 default:
167                         break;
168         }
169 // FIXME
170 #if 0
171         unsigned int fill = packedcolor(LColor::graphicsbg);
172         if (fill != image_->fill_color) {
173                 // the background color has changed.
174                 // Note that in grayscale/monochrome images the background is
175                 // grayed also, so this call will have no visible effect. Sorry!
176                 flimage_replace_pixel(image_, image_->fill_color, fill);
177                 image_->fill_color = fill;
178         }
179 #endif
180
181         return true;
182 }
183
184
185 void QLImage::clip(Params const & params)
186 {
187         if (xformed_pixmap_.isNull())
188                 return;
189
190         if (params.bb.empty())
191                 // No clipping is necessary.
192                 return;
193
194         int const new_width  = params.bb.xr - params.bb.xl;
195         int const new_height = params.bb.yt - params.bb.yb;
196
197         // No need to check if the width, height are > 0 because the
198         // Bounding Box would be empty() in this case.
199         if (new_width > pixmap_.width() || new_height > pixmap_.height()) {
200                 // Bounds are invalid.
201                 return;
202         }
203
204         if (new_width == pixmap_.width() && new_height == pixmap_.height())
205                 return;
206
207         int const xoffset_l = params.bb.xl;
208         int const yoffset_t = (pixmap_.height() > params.bb.yt ?
209                                 pixmap_.height() - params.bb.yt : 0);
210
211         xformed_pixmap_.resize(new_width, new_height);
212         QPainter p;
213         p.begin(&xformed_pixmap_);
214         p.drawPixmap(0, 0, pixmap_, xoffset_l, yoffset_t, new_width, new_height);
215         p.end();
216 }
217
218
219 void QLImage::rotate(Params const & params)
220 {
221         if (xformed_pixmap_.isNull())
222                 return;
223
224         if (!params.angle)
225                 return;
226
227         // The angle passed to flimage_rotate is the angle in one-tenth of a
228         // degree units.
229
230         QWMatrix m;
231         m.rotate(-params.angle);
232         xformed_pixmap_ = xformed_pixmap_.xForm(m);
233 }
234
235
236 void QLImage::scale(Params const & params)
237 {
238         if (xformed_pixmap_.isNull())
239                 return;
240
241         unsigned int width;
242         unsigned int height;
243         boost::tie(width, height) = getScaledDimensions(params);
244
245         if (width == getWidth() && height == getHeight())
246                 return;
247
248         QWMatrix m;
249         m.scale(double(width) / getWidth(), double(height) / getHeight());
250         xformed_pixmap_ = xformed_pixmap_.xForm(m);
251 }
252
253 } // namespace grfx