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 fname_ = toqstr(filename.absFilename());
75 if (!original_.load(fname_)) {
76 LYXERR(Debug::GRAPHICS, "Unable to open image");
83 bool GuiImage::setPixmap(Params const & params)
88 if (original_.isNull()) {
89 if (!original_.load(fname_))
93 is_transformed_ = clip(params);
94 is_transformed_ |= rotate(params);
95 is_transformed_ |= scale(params);
97 // Clear the pixmap to save some memory.
99 original_ = QPixmap();
101 transformed_ = QPixmap();
107 bool GuiImage::clip(Params const & params)
109 if (params.bb.empty())
110 // No clipping is necessary.
113 int const new_width = params.bb.xr - params.bb.xl;
114 int const new_height = params.bb.yt - params.bb.yb;
116 // No need to check if the width, height are > 0 because the
117 // Bounding Box would be empty() in this case.
118 if (new_width > original_.width() || new_height > original_.height()) {
119 // Bounds are invalid.
123 if (new_width == original_.width() && new_height == original_.height())
126 int const xoffset_l = params.bb.xl;
127 int const yoffset_t = (original_.height() > int(params.bb.yt) ?
128 original_.height() - params.bb.yt : 0);
130 transformed_ = original_.copy(xoffset_l, yoffset_t,
131 new_width, new_height);
136 bool GuiImage::rotate(Params const & params)
141 if (!is_transformed_)
142 transformed_ = original_;
145 m.rotate(-params.angle);
146 transformed_ = transformed_.transformed(m);
151 bool GuiImage::scale(Params const & params)
153 Dimension dim = scaledDimension(params);
155 if (dim.width() == width() && dim.height() == height())
158 if (!is_transformed_)
159 transformed_ = original_;
162 m.scale(double(dim.width()) / width(), double(dim.height()) / height());
163 transformed_ = transformed_.transformed(m);
168 } // namespace graphics