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