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
27 #include <QImageReader>
30 using namespace lyx::support;
35 /// Access to this class is through this static method.
36 Image * GuiImage::newImage()
42 GuiImage::GuiImage(GuiImage const & other)
43 : Image(other), original_(other.original_),
44 transformed_(other.transformed_), is_transformed_(other.is_transformed_)
48 Image * GuiImage::clone() const
50 return new GuiImage(*this);
54 unsigned int GuiImage::width() const
56 return is_transformed_ ? transformed_.width() : original_.width();
60 unsigned int GuiImage::height() const
62 return is_transformed_ ? transformed_.height() : original_.height();
66 bool GuiImage::load(FileName const & filename)
68 if (!original_.isNull()) {
69 LYXERR(Debug::GRAPHICS, "Image is loaded already!");
73 if (!original_.load(toqstr(filename.absFilename()))) {
74 LYXERR(Debug::GRAPHICS, "Unable to open image");
81 bool GuiImage::setPixmap(Params const & params)
83 if (original_.isNull() || !params.display)
86 is_transformed_ = clip(params);
87 is_transformed_ |= rotate(params);
88 is_transformed_ |= scale(params);
91 // Clear it out to save some memory.
92 transformed_ = QPixmap();
98 bool GuiImage::clip(Params const & params)
100 if (params.bb.empty())
101 // No clipping is necessary.
104 int const new_width = params.bb.xr - params.bb.xl;
105 int const new_height = params.bb.yt - params.bb.yb;
107 // No need to check if the width, height are > 0 because the
108 // Bounding Box would be empty() in this case.
109 if (new_width > original_.width() || new_height > original_.height()) {
110 // Bounds are invalid.
114 if (new_width == original_.width() && new_height == original_.height())
117 int const xoffset_l = params.bb.xl;
118 int const yoffset_t = (original_.height() > int(params.bb.yt) ?
119 original_.height() - params.bb.yt : 0);
121 transformed_ = original_.copy(xoffset_l, yoffset_t,
122 new_width, new_height);
127 bool GuiImage::rotate(Params const & params)
132 if (!is_transformed_)
133 transformed_ = original_;
136 m.rotate(-params.angle);
137 transformed_ = transformed_.transformed(m);
142 bool GuiImage::scale(Params const & params)
144 Dimension dim = scaledDimension(params);
146 if (dim.width() == width() && dim.height() == height())
149 if (!is_transformed_)
150 transformed_ = original_;
153 m.scale(double(dim.width()) / width(), double(dim.height()) / height());
154 transformed_ = transformed_.transformed(m);
159 } // namespace graphics