]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiImage.cpp
Improve wording (#10670)
[lyx.git] / src / frontends / qt4 / GuiImage.cpp
index cf580b12530150370c2bbdb3b121602c35dec3b4..08259ecd92b43a6e69e339ad2869b5d203adc3e8 100644 (file)
@@ -10,6 +10,7 @@
  */
 
 #include <config.h>
+#include <math.h> /* ceil */
 
 #include "GuiImage.h"
 #include "qt_helpers.h"
@@ -31,8 +32,8 @@ using namespace lyx::support;
 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;
 }
@@ -63,13 +64,25 @@ QImage const & GuiImage::image() const
 
 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
 }
 
 
@@ -79,7 +92,7 @@ bool GuiImage::load(FileName const & filename)
                LYXERR(Debug::GRAPHICS, "Image is loaded already!");
                return false;
        }
-       fname_ = toqstr(filename.absFilename());
+       fname_ = toqstr(filename.absFileName());
        return load();
 }
 
@@ -90,7 +103,6 @@ bool GuiImage::load()
                LYXERR(Debug::GRAPHICS, "Unable to open image");
                return false;
        }
-       original_.detach();
        return true;
 }
 
@@ -104,19 +116,20 @@ bool GuiImage::setPixmap(Params const & params)
                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);
 
        // Clear the pixmap to save some memory.
-       if (is_transformed_) {
-               original_.detach();
+       if (is_transformed_)
                original_ = QImage();
-       } else {
-               transformed_.detach();
+       else
                transformed_ = QImage();
-       }
 
        return true;
 }
@@ -128,8 +141,14 @@ bool GuiImage::clip(Params const & 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_;
 
@@ -143,9 +162,9 @@ bool GuiImage::clip(Params const & params)
        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;
@@ -169,25 +188,27 @@ bool GuiImage::scale(Params const & params)
 {
        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;
 }