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