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"
19 #include "support/LAssert.h"
20 #include "support/lyxfunctional.h" // compare_memfun
21 #include "qt_helpers.h"
27 #include <boost/tuple/tuple.hpp>
34 /// Access to this class is through this static method.
35 Image::ImagePtr QLImage::newImage()
38 ptr.reset(new QLImage);
43 /// Return the list of loadable formats.
44 Image::FormatList QLImage::loadableFormats()
46 static FormatList fmts;
51 // The formats recognised by LyX
52 Formats::const_iterator begin = formats.begin();
53 Formats::const_iterator end = formats.end();
55 lyxerr[Debug::GRAPHICS]
56 << "\nThe image loader can load the following directly:\n";
58 QStrList qt_formats = QImageIO::inputFormats();
60 QStrListIterator it(qt_formats);
62 for (; it.current(); ++it) {
63 lyxerr[Debug::GRAPHICS] << it.current() << endl;
65 string ext = lowercase(it.current());
71 Formats::const_iterator fit =
72 find_if(begin, end, lyx::compare_memfun(&Format::extension, ext));
74 fmts.push_back(fit->name());
77 if (lyxerr.debugging()) {
78 lyxerr[Debug::GRAPHICS]
79 << "\nOf these, LyX recognises the following formats:\n";
81 FormatList::const_iterator fbegin = fmts.begin();
82 FormatList::const_iterator fend = fmts.end();
83 for (FormatList::const_iterator fit = fbegin; fit != fend; ++fit) {
85 lyxerr[Debug::GRAPHICS] << ", ";
86 lyxerr[Debug::GRAPHICS] << *fit;
88 lyxerr[Debug::GRAPHICS] << '\n' << endl;
101 QLImage::QLImage(QLImage const & other)
102 : Image(other), pixmap_(other.pixmap_),
103 xformed_pixmap_(other.xformed_pixmap_)
113 Image * QLImage::clone() const
115 return new QLImage(*this);
119 unsigned int QLImage::getWidth() const
121 return xformed_pixmap_.width();
125 unsigned int QLImage::getHeight() const
127 return xformed_pixmap_.height();
131 void QLImage::load(string const & filename)
133 if (!pixmap_.isNull()) {
134 lyxerr[Debug::GRAPHICS]
135 << "Image is loaded already!" << endl;
136 finishedLoading(false);
140 if (!pixmap_.load(toqstr(filename))) {
141 lyxerr[Debug::GRAPHICS]
142 << "Unable to open image" << endl;
143 finishedLoading(false);
146 xformed_pixmap_ = pixmap_;
147 finishedLoading(true);
151 bool QLImage::setPixmap(Params const & params)
153 if (pixmap_.isNull() || params.display == NoDisplay)
156 // FIXME: it's a fake kind of grayscale !
158 switch (params.display) {
159 case GrayscaleDisplay:
160 case MonochromeDisplay: {
161 QImage i(xformed_pixmap_.convertToImage());
162 xformed_pixmap_.convertFromImage(i, QPixmap::Mono);
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;
185 void QLImage::clip(Params const & params)
187 if (xformed_pixmap_.isNull())
190 if (params.bb.empty())
191 // No clipping is necessary.
194 int const new_width = params.bb.xr - params.bb.xl;
195 int const new_height = params.bb.yt - params.bb.yb;
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.
204 if (new_width == pixmap_.width() && new_height == pixmap_.height())
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);
211 xformed_pixmap_.resize(new_width, new_height);
213 p.begin(&xformed_pixmap_);
214 p.drawPixmap(0, 0, pixmap_, xoffset_l, yoffset_t, new_width, new_height);
219 void QLImage::rotate(Params const & params)
221 if (xformed_pixmap_.isNull())
227 // The angle passed to flimage_rotate is the angle in one-tenth of a
231 m.rotate(-params.angle);
232 xformed_pixmap_ = xformed_pixmap_.xForm(m);
236 void QLImage::scale(Params const & params)
238 if (xformed_pixmap_.isNull())
243 boost::tie(width, height) = getScaledDimensions(params);
245 if (width == getWidth() && height == getHeight())
249 m.scale(double(width) / getWidth(), double(height) / getHeight());
250 xformed_pixmap_ = xformed_pixmap_.xForm(m);