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