]> 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 efa198bc69efb2137d6385108456ed7b4f425657..eb5124fcadae65017e39fa3a51100bdc10b644fd 100644 (file)
  */
 
 #include <config.h>
+#include <math.h> /* ceil */
 
 #include "GuiImage.h"
 #include "qt_helpers.h"
 
-#include "debug.h"
 #include "Format.h"
 
 #include "graphics/GraphicsParams.h"
 
+#include "support/debug.h"
 #include "support/FileName.h"
 #include "support/lstrings.h"       // ascii_lowercase
 
 #include <QPainter>
-#include <QImage>
 #include <QImageReader>
 
-#include <boost/bind.hpp>
-#include <boost/tuple/tuple.hpp>
-
-using lyx::support::ascii_lowercase;
-
-using boost::bind;
-
-using std::endl;
-using std::equal_to;
-using std::find_if;
-using std::string;
+using namespace std;
+using namespace lyx::support;
 
 namespace lyx {
 namespace graphics {
 
-/// Access to this class is through this static method.
-Image::ImagePtr GuiImage::newImage()
+/// Implement factory method defined in GraphicsImage.h
+Image * newImage()
 {
-       ImagePtr ptr;
-       ptr.reset(new GuiImage);
-       return ptr;
+       return new GuiImage;
 }
 
 
-/// Return the list of loadable formats.
-Image::FormatList GuiImage::loadableFormats()
-{
-       static FormatList fmts;
-
-       if (!fmts.empty())
-               return fmts;
-
-       // The formats recognised by LyX
-       Formats::const_iterator begin = formats.begin();
-       Formats::const_iterator end   = formats.end();
-
-
-//     LYXERR(Debug::GRAPHICS)
-//             << "D:/msys/home/yns/src/lyx-devel/lib/images/banner.png mis of format: "
-//             << fromqstr(Pic.pictureFormat("D:/msys/home/yns/src/lyx-devel/lib/images/banner.png"))
-//             << endl;
-//     if (Pic.pictureFormat("D:/msys/home/yns/src/lyx-devel/lib/images/banner.png"))
-//             LYXERR(Debug::GRAPHICS)
-//                     << "pictureFormat not returned NULL\n" << endl;
-//                     << "Supported formats are: " << Pic.inputFormats() << endl;
-
-       QList<QByteArray> qt_formats = QImageReader::supportedImageFormats();
-
-       LYXERR(Debug::GRAPHICS)
-               << "\nThe image loader can load the following directly:\n";
-
-       if (qt_formats.empty()) {
-               LYXERR(Debug::GRAPHICS)
-                       << "\nQt4 Problem: No Format available!" << endl;
-       }
-
-       for (QList<QByteArray>::const_iterator it = qt_formats.begin(); it != qt_formats.end(); ++it) {
-
-               LYXERR(Debug::GRAPHICS) << (const char *) *it << ", ";
-
-               string ext = ascii_lowercase((const char *) *it);
-
-               // special case
-               if (ext == "jpeg")
-                       ext = "jpg";
-
-               Formats::const_iterator fit =
-                       find_if(begin, end,
-                               bind(equal_to<string>(),
-                                    bind(&Format::extension, _1),
-                                    ext));
-               if (fit != end)
-                       fmts.push_back(fit->name());
-       }
-
-       if (lyxerr.debugging()) {
-               LYXERR(Debug::GRAPHICS)
-                       << "\nOf these, LyX recognises the following formats:\n";
-
-               FormatList::const_iterator fbegin = fmts.begin();
-               FormatList::const_iterator fend   = fmts.end();
-               for (FormatList::const_iterator fit = fbegin; fit != fend; ++fit) {
-                       if (fit != fbegin) {
-                               LYXERR(Debug::GRAPHICS) << ", ";
-                       }
-                       LYXERR(Debug::GRAPHICS) << *fit;
-               }
-               LYXERR(Debug::GRAPHICS) << '\n' << endl;
-       }
-
-       return fmts;
-}
+GuiImage::GuiImage() : is_transformed_(false)
+{}
 
 
 GuiImage::GuiImage(GuiImage const & other)
        : Image(other), original_(other.original_),
-         transformed_(other.transformed_),
-         transformed_pixmap_(other.transformed_pixmap_)
+       transformed_(other.transformed_), is_transformed_(other.is_transformed_),
+       fname_(other.fname_)
 {}
 
 
-Image * GuiImage::clone_impl() const
+Image * GuiImage::clone() const
 {
        return new GuiImage(*this);
 }
 
 
-unsigned int GuiImage::getWidth_impl() const
+QImage const & GuiImage::image() const
 {
-       return transformed_.width();
+       return is_transformed_ ? transformed_ : original_;
 }
 
 
-unsigned int GuiImage::getHeight_impl() const
+unsigned int GuiImage::width() const
 {
-       return transformed_.height();
+#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
 }
 
 
-void GuiImage::load_impl(support::FileName const & filename)
+unsigned int GuiImage::height() const
 {
-       if (!original_.isNull()) {
-               LYXERR(Debug::GRAPHICS)
-                       << "Image is loaded already!" << endl;
-               finishedLoading(false);
-               return;
-       }
-
-       if (!original_.load(toqstr(filename.absFilename()))) {
-               LYXERR(Debug::GRAPHICS)
-                       << "Unable to open image" << endl;
-               finishedLoading(false);
-               return;
-       }
-       transformed_ = original_;
-       finishedLoading(true);
+#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
 }
 
 
-// This code is taken from KImageEffect::toGray
-static QImage & toGray(QImage & img)
+bool GuiImage::load(FileName const & filename)
 {
-       if (img.width() == 0 || img.height() == 0)
-               return img;
-
-       int const pixels = img.depth() > 8 ?
-               img.width() * img.height() : img.numColors();
+       if (!original_.isNull()) {
+               LYXERR(Debug::GRAPHICS, "Image is loaded already!");
+               return false;
+       }
+       fname_ = toqstr(filename.absFileName());
+       return load();
+}
 
-       unsigned int *data = img.depth() > 8 ?
-               reinterpret_cast<unsigned int *>(img.bits()) :
-               reinterpret_cast<unsigned int *>(&img.colorTable()[0]);
 
-       for(int i = 0; i < pixels; ++i){
-               int const val = qGray(data[i]);
-               data[i] = qRgba(val, val, val, qAlpha(data[i]));
+bool GuiImage::load()
+{
+       if (!original_.load(fname_)) {
+               LYXERR(Debug::GRAPHICS, "Unable to open image");
+               return false;
        }
-       return img;
+       return true;
 }
 
 
-bool GuiImage::setPixmap_impl(Params const & params)
+bool GuiImage::setPixmap(Params const & params)
 {
-       if (original_.isNull() || params.display == NoDisplay)
+       if (!params.display)
                return false;
 
-       switch (params.display) {
-       case GrayscaleDisplay: {
-               toGray(transformed_);
-               break;
+       if (original_.isNull()) {
+               if (!load())
+                       return false;
        }
 
-       case MonochromeDisplay: {
-               transformed_.convertToFormat(transformed_.format(), Qt::MonoOnly);
-               break;
-       }
+#if QT_VERSION >= 0x050000
+       original_.setDevicePixelRatio(params.pixel_ratio);
+#endif
 
-       default:
-               break;
-       }
+       is_transformed_ = clip(params);
+       is_transformed_ |= rotate(params);
+       is_transformed_ |= scale(params);
+
+       // Clear the pixmap to save some memory.
+       if (is_transformed_)
+               original_ = QImage();
+       else
+               transformed_ = QImage();
 
-       transformed_pixmap_ = QPixmap::fromImage(transformed_);
        return true;
 }
 
 
-void GuiImage::clip_impl(Params const & params)
+bool GuiImage::clip(Params const & params)
 {
-       if (transformed_.isNull())
-               return;
-
        if (params.bb.empty())
                // No clipping is necessary.
-               return;
+               return false;
+
+#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
 
-       int const new_width  = params.bb.xr - params.bb.xl;
-       int const new_height = params.bb.yt - params.bb.yb;
+       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;
+               return false;
        }
 
-       if (new_width == original_.width() && new_height == original_.height())
-               return;
+       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;
 }
 
 
-void GuiImage::rotate_impl(Params const & params)
+bool GuiImage::rotate(Params const & params)
 {
-       if (transformed_.isNull())
-               return;
-
        if (!params.angle)
-               return;
+               return false;
 
+       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;
 }
 
 
-void GuiImage::scale_impl(Params const & params)
+bool GuiImage::scale(Params const & params)
 {
-       if (transformed_.isNull())
-               return;
+       QImage const & image = is_transformed_ ? transformed_ : original_;
 
-       unsigned int width;
-       unsigned int height;
-       boost::tie(width, height) = getScaledDimensions(params);
+       if (params.scale == 100)
+               return false;
+
+#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
 
-       if (width == getWidth() && height == getHeight())
-               return;
+#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(double(width) / getWidth(), double(height) / getHeight());
-       transformed_ = transformed_.transformed(m);
+       m.scale(scale, scale);
+       transformed_ = image.transformed(m);
+       return true;
 }
 
 } // namespace graphics