]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiImage.cpp
Correct early return position for if use_pixmap_cache_ check
[lyx.git] / src / frontends / qt4 / GuiImage.cpp
index 2e83bd63909631738e739a830c67d5aa4d400278..eb5124fcadae65017e39fa3a51100bdc10b644fd 100644 (file)
@@ -10,6 +10,7 @@
  */
 
 #include <config.h>
+#include <math.h> /* ceil */
 
 #include "GuiImage.h"
 #include "qt_helpers.h"
@@ -23,7 +24,6 @@
 #include "support/lstrings.h"       // ascii_lowercase
 
 #include <QPainter>
-#include <QImage>
 #include <QImageReader>
 
 using namespace std;
@@ -32,16 +32,21 @@ 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;
 }
 
 
+GuiImage::GuiImage() : is_transformed_(false)
+{}
+
+
 GuiImage::GuiImage(GuiImage const & other)
        : Image(other), original_(other.original_),
-         transformed_(other.transformed_), is_transformed_(other.is_transformed_)
+       transformed_(other.transformed_), is_transformed_(other.is_transformed_),
+       fname_(other.fname_)
 {}
 
 
@@ -51,15 +56,33 @@ Image * GuiImage::clone() const
 }
 
 
+QImage const & GuiImage::image() const
+{
+       return is_transformed_ ? transformed_ : original_;
+}
+
+
 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
 }
 
 
@@ -69,9 +92,13 @@ bool GuiImage::load(FileName const & filename)
                LYXERR(Debug::GRAPHICS, "Image is loaded already!");
                return false;
        }
+       fname_ = toqstr(filename.absFileName());
+       return load();
+}
 
-       fname_ = toqstr(filename.absFilename());
 
+bool GuiImage::load()
+{
        if (!original_.load(fname_)) {
                LYXERR(Debug::GRAPHICS, "Unable to open image");
                return false;
@@ -86,19 +113,23 @@ bool GuiImage::setPixmap(Params const & params)
                return false;
 
        if (original_.isNull()) {
-               if (original_.load(fname_))
+               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_ = QPixmap();
+               original_ = QImage();
        else
-               transformed_ = QPixmap();
+               transformed_ = QImage();
 
        return true;
 }
@@ -110,25 +141,32 @@ 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_;
 
        // No need to check if the width, height are > 0 because the
        // Bounding Box would be empty() in this case.
-       if (new_width > original_.width() || new_height > original_.height()) {
+       if (new_width > image.width() || new_height > image.height()) {
                // Bounds are invalid.
                return false;
        }
 
-       if (new_width == original_.width() && new_height == original_.height())
+       if (new_width == image.width() && new_height == image.height())
                return false;
 
-       int const xoffset_l = params.bb.xl;
-       int const yoffset_t = (original_.height() > int(params.bb.yt) ?
-                              original_.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_ = original_.copy(xoffset_l, yoffset_t,
-                                     new_width, new_height);
+       transformed_ = image.copy(xoffset_l, yoffset_t, new_width, new_height);
        return true;
 }
 
@@ -138,30 +176,38 @@ bool GuiImage::rotate(Params const & params)
        if (!params.angle)
                return false;
 
-       if (!is_transformed_)
-               transformed_ = original_;
-
+       QImage const & image = is_transformed_ ? transformed_ : original_;
        QMatrix m;
-       m.rotate(-params.angle);
-       transformed_ = transformed_.transformed(m);
+       m.rotate(- params.angle);
+       transformed_ = image.transformed(m);
        return true;
 }
 
 
 bool GuiImage::scale(Params const & params)
 {
-       Dimension dim = scaledDimension(params);
+       QImage const & image = is_transformed_ ? transformed_ : original_;
 
-       if (dim.width() == width() && dim.height() == height())
+       if (params.scale == 100)
                return false;
 
-       if (!is_transformed_)
-               transformed_ = original_;
+#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
 
-       QMatrix m;
-       m.scale(double(dim.width()) / width(), double(dim.height()) / height());
-       transformed_ = transformed_.transformed(m);
+#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
 
+       QMatrix m;
+       m.scale(scale, scale);
+       transformed_ = image.transformed(m);
        return true;
 }