3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Angus Leeming
9 * Full author contact details are available in file CREDITS.
16 #include "graphics/GraphicsParams.h"
18 #include "support/lstrings.h" // lowercase
19 #include "support/lyxfunctional.h" // compare_memfun
20 #include "qt_helpers.h"
25 #include <boost/tuple/tuple.hpp>
27 using lyx::support::lowercase;
37 /// Access to this class is through this static method.
38 Image::ImagePtr QLImage::newImage()
41 ptr.reset(new QLImage);
46 /// Return the list of loadable formats.
47 Image::FormatList QLImage::loadableFormats()
49 static FormatList fmts;
54 // The formats recognised by LyX
55 Formats::const_iterator begin = formats.begin();
56 Formats::const_iterator end = formats.end();
58 lyxerr[Debug::GRAPHICS]
59 << "\nThe image loader can load the following directly:\n";
61 QStrList qt_formats = QImageIO::inputFormats();
63 QStrListIterator it(qt_formats);
65 for (; it.current(); ++it) {
66 lyxerr[Debug::GRAPHICS] << it.current() << endl;
68 string ext = lowercase(it.current());
74 Formats::const_iterator fit =
75 find_if(begin, end, lyx::compare_memfun(&Format::extension, ext));
77 fmts.push_back(fit->name());
80 if (lyxerr.debugging()) {
81 lyxerr[Debug::GRAPHICS]
82 << "\nOf these, LyX recognises the following formats:\n";
84 FormatList::const_iterator fbegin = fmts.begin();
85 FormatList::const_iterator fend = fmts.end();
86 for (FormatList::const_iterator fit = fbegin; fit != fend; ++fit) {
88 lyxerr[Debug::GRAPHICS] << ", ";
89 lyxerr[Debug::GRAPHICS] << *fit;
91 lyxerr[Debug::GRAPHICS] << '\n' << endl;
104 QLImage::QLImage(QLImage const & other)
105 : Image(other), pixmap_(other.pixmap_),
106 xformed_pixmap_(other.xformed_pixmap_)
116 Image * QLImage::clone_impl() const
118 return new QLImage(*this);
122 unsigned int QLImage::getWidth_impl() const
124 return xformed_pixmap_.width();
128 unsigned int QLImage::getHeight_impl() const
130 return xformed_pixmap_.height();
134 void QLImage::load_impl(string const & filename)
136 if (!pixmap_.isNull()) {
137 lyxerr[Debug::GRAPHICS]
138 << "Image is loaded already!" << endl;
139 finishedLoading(false);
143 if (!pixmap_.load(toqstr(filename))) {
144 lyxerr[Debug::GRAPHICS]
145 << "Unable to open image" << endl;
146 finishedLoading(false);
149 xformed_pixmap_ = pixmap_;
150 finishedLoading(true);
154 bool QLImage::setPixmap_impl(Params const & params)
156 if (pixmap_.isNull() || params.display == NoDisplay)
159 // FIXME: it's a fake kind of grayscale !
161 switch (params.display) {
162 case GrayscaleDisplay:
163 case MonochromeDisplay: {
164 QImage i(xformed_pixmap_.convertToImage());
165 xformed_pixmap_.convertFromImage(i, QPixmap::Mono);
174 unsigned int fill = packedcolor(LColor::graphicsbg);
175 if (fill != image_->fill_color) {
176 // the background color has changed.
177 // Note that in grayscale/monochrome images the background is
178 // grayed also, so this call will have no visible effect. Sorry!
179 flimage_replace_pixel(image_, image_->fill_color, fill);
180 image_->fill_color = fill;
188 void QLImage::clip_impl(Params const & params)
190 if (xformed_pixmap_.isNull())
193 if (params.bb.empty())
194 // No clipping is necessary.
197 int const new_width = params.bb.xr - params.bb.xl;
198 int const new_height = params.bb.yt - params.bb.yb;
200 // No need to check if the width, height are > 0 because the
201 // Bounding Box would be empty() in this case.
202 if (new_width > pixmap_.width() || new_height > pixmap_.height()) {
203 // Bounds are invalid.
207 if (new_width == pixmap_.width() && new_height == pixmap_.height())
210 int const xoffset_l = params.bb.xl;
211 int const yoffset_t = (pixmap_.height() > params.bb.yt ?
212 pixmap_.height() - params.bb.yt : 0);
214 xformed_pixmap_.resize(new_width, new_height);
216 p.begin(&xformed_pixmap_);
217 p.drawPixmap(0, 0, pixmap_, xoffset_l, yoffset_t, new_width, new_height);
222 void QLImage::rotate_impl(Params const & params)
224 if (xformed_pixmap_.isNull())
230 // The angle passed to flimage_rotate is the angle in one-tenth of a
234 m.rotate(-params.angle);
235 xformed_pixmap_ = xformed_pixmap_.xForm(m);
239 void QLImage::scale_impl(Params const & params)
241 if (xformed_pixmap_.isNull())
246 boost::tie(width, height) = getScaledDimensions(params);
248 if (width == getWidth() && height == getHeight())
252 m.scale(double(width) / getWidth(), double(height) / getHeight());
253 xformed_pixmap_ = xformed_pixmap_.xForm(m);
256 } // namespace graphics