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.
15 #include "qt_helpers.h"
19 #include "graphics/GraphicsParams.h"
21 #include "support/debug.h"
22 #include "support/FileName.h"
23 #include "support/lstrings.h" // ascii_lowercase
26 #include <QImageReader>
29 using namespace lyx::support;
34 /// Implement factory method defined in GraphicsImage.h
41 GuiImage::GuiImage() : is_transformed_(false)
45 GuiImage::GuiImage(GuiImage const & other)
46 : Image(other), original_(other.original_),
47 transformed_(other.transformed_), is_transformed_(other.is_transformed_),
52 Image * GuiImage::clone() const
54 return new GuiImage(*this);
58 QImage const & GuiImage::image() const
60 return is_transformed_ ? transformed_ : original_;
64 unsigned int GuiImage::width() const
66 return is_transformed_ ? transformed_.width() : original_.width();
70 unsigned int GuiImage::height() const
72 return is_transformed_ ? transformed_.height() : original_.height();
76 bool GuiImage::load(FileName const & filename)
78 if (!original_.isNull()) {
79 LYXERR(Debug::GRAPHICS, "Image is loaded already!");
82 fname_ = toqstr(filename.absFilename());
89 if (!original_.load(fname_)) {
90 LYXERR(Debug::GRAPHICS, "Unable to open image");
97 bool GuiImage::setPixmap(Params const & params)
102 if (original_.isNull()) {
107 is_transformed_ = clip(params);
108 is_transformed_ |= rotate(params);
109 is_transformed_ |= scale(params);
111 // Clear the pixmap to save some memory.
113 original_ = QImage();
115 transformed_ = QImage();
121 bool GuiImage::clip(Params const & params)
123 if (params.bb.empty())
124 // No clipping is necessary.
127 int const new_width = params.bb.xr - params.bb.xl;
128 int const new_height = params.bb.yt - params.bb.yb;
130 QImage const & image = is_transformed_ ? transformed_ : original_;
132 // No need to check if the width, height are > 0 because the
133 // Bounding Box would be empty() in this case.
134 if (new_width > image.width() || new_height > image.height()) {
135 // Bounds are invalid.
139 if (new_width == image.width() && new_height == image.height())
142 int const xoffset_l = params.bb.xl;
143 int const yoffset_t = (image.height() > int(params.bb.yt))
144 ? image.height() - params.bb.yt : 0;
146 transformed_ = image.copy(xoffset_l, yoffset_t, new_width, new_height);
151 bool GuiImage::rotate(Params const & params)
156 QImage const & image = is_transformed_ ? transformed_ : original_;
158 m.rotate(- params.angle);
159 transformed_ = image.transformed(m);
164 bool GuiImage::scale(Params const & params)
166 QImage const & image = is_transformed_ ? transformed_ : original_;
168 if (params.scale == 100)
171 qreal scale = qreal(params.scale) / 100.0;
173 #if (QT_VERSION >= 0x040500) && (QT_VERSION <= 0x040502)
174 // Due to a bug in Qt, LyX will crash for certain
175 // scaling factors and sizes of the image.
176 // see bug #5957: http://www.lyx.org/trac/ticket/5957
181 m.scale(scale, scale);
182 transformed_ = image.transformed(m);
186 } // namespace graphics