*/
#include <config.h>
+#include <math.h> /* ceil */
#include "GuiImage.h"
#include "qt_helpers.h"
namespace lyx {
namespace graphics {
-/// Access to this class is through this static method.
-Image * GuiImage::newImage()
+/// Implement factory method defined in GraphicsImage.h
+Image * newImage()
{
return new GuiImage;
}
unsigned int GuiImage::width() const
{
+#if QT_VERSION >= 0x050000
+ return static_cast<unsigned int>(ceil(is_transformed_ ?
+ (transformed_.width() / transformed_.devicePixelRatio()) :
+ (original_.width() / original_.devicePixelRatio())));
+#else
return is_transformed_ ? transformed_.width() : original_.width();
+#endif
}
unsigned int GuiImage::height() const
{
+#if QT_VERSION >= 0x050000
+ return static_cast<unsigned int>(ceil(is_transformed_ ?
+ (transformed_.height() / transformed_.devicePixelRatio()) :
+ (original_.height() / original_.devicePixelRatio())));
+#else
return is_transformed_ ? transformed_.height() : original_.height();
+#endif
}
LYXERR(Debug::GRAPHICS, "Image is loaded already!");
return false;
}
- fname_ = toqstr(filename.absFilename());
+ fname_ = toqstr(filename.absFileName());
return load();
}
if (!load())
return false;
}
-
+
+#if QT_VERSION >= 0x050000
+ original_.setDevicePixelRatio(params.pixel_ratio);
+#endif
+
is_transformed_ = clip(params);
is_transformed_ |= rotate(params);
is_transformed_ |= scale(params);
// No clipping is necessary.
return false;
- int const new_width = params.bb.xr - params.bb.xl;
- int const new_height = params.bb.yt - params.bb.yb;
+#if QT_VERSION >= 0x050000
+ double const pixelRatio = is_transformed_ ? transformed_.devicePixelRatio() : original_.devicePixelRatio();
+ int const new_width = static_cast<int>((params.bb.xr.inBP() - params.bb.xl.inBP()) * pixelRatio);
+ int const new_height = static_cast<int>((params.bb.yt.inBP() - params.bb.yb.inBP()) * pixelRatio);
+#else
+ int const new_width = static_cast<int>((params.bb.xr.inBP() - params.bb.xl.inBP()));
+ int const new_height = static_cast<int>((params.bb.yt.inBP() - params.bb.yb.inBP()));
+#endif
QImage const & image = is_transformed_ ? transformed_ : original_;
if (new_width == image.width() && new_height == image.height())
return false;
- int const xoffset_l = params.bb.xl;
- int const yoffset_t = (image.height() > int(params.bb.yt))
- ? image.height() - params.bb.yt : 0;
+ int const xoffset_l = params.bb.xl.inBP();
+ int const yoffset_t = (image.height() > params.bb.yt.inBP())
+ ? image.height() - params.bb.yt.inBP() : 0;
transformed_ = image.copy(xoffset_l, yoffset_t, new_width, new_height);
return true;
{
QImage const & image = is_transformed_ ? transformed_ : original_;
- unsigned int w = image.width();
- unsigned int h = image.height();
+ if (params.scale == 100)
+ return false;
- // scale only when value > 0
- if (params.scale > 0) {
- w = (w * params.scale) / 100;
- h = (h * params.scale) / 100;
- }
+#if QT_VERSION >= 0x050000
+ double const pixelRatio = is_transformed_ ? transformed_.devicePixelRatio() : original_.devicePixelRatio();
+ qreal scale = qreal(params.scale) / 100.0 * pixelRatio;
+#else
+ qreal scale = qreal(params.scale) / 100.0;
+#endif
- LYXERR(Debug::GRAPHICS, "\n\tparams.scale : " << params.scale
- << "\n\twidth : " << w
- << "\n\theight : " << h);
+#if (QT_VERSION >= 0x040500) && (QT_VERSION <= 0x040502)
+ // Due to a bug in Qt, LyX will crash for certain
+ // scaling factors and sizes of the image.
+ // see bug #5957: http://www.lyx.org/trac/ticket/5957
+ scale += 0.0001;
+#endif
- if (w == image.width() && h == image.height())
- return false;
-
QMatrix m;
- m.scale(double(w) / image.width(), double(h) / image.height());
- transformed_ = image.transformed(m);
+ m.scale(scale, scale);
+ // Bilinear filtering is used to scale graphics preview
+ transformed_ = image.transformed(m, Qt::SmoothTransformation);
return true;
}