]> git.lyx.org Git - lyx.git/blobdiff - src/graphics/PreviewImage.cpp
Do not compute metrics at each preview when loading file
[lyx.git] / src / graphics / PreviewImage.cpp
index 47b30a4d339e1e43f7f78f6f0078c6cc8e3ba3bb..e94384c168a091935389b42fecaf1bfc52ad837d 100644 (file)
@@ -11,6 +11,9 @@
 #include <config.h>
 
 #include "PreviewImage.h"
+
+#include "Buffer.h"
+#include "Dimension.h"
 #include "GraphicsImage.h"
 #include "GraphicsLoader.h"
 #include "PreviewLoader.h"
 #include "support/FileName.h"
 #include "support/lyxlib.h"
 
-#include <boost/bind.hpp>
-
-using std::string;
 
+using namespace std;
+using namespace lyx::support;
 
 namespace lyx {
-
-using support::FileName;
-
 namespace graphics {
 
-class PreviewImage::Impl : public boost::signals::trackable {
+class PreviewImage::Impl {
 public:
        ///
        Impl(PreviewImage & p, PreviewLoader & l,
-            string const & s, FileName const & f, double af);
+            docstring const & s, FileName const & f, double af);
        ///
        ~Impl();
        ///
-       Image const * image();
+       Image const * image() const;
        ///
        void statusChanged();
 
@@ -48,14 +47,14 @@ public:
        ///
        Loader iloader_;
        ///
-       string const snippet_;
+       docstring const snippet_;
        ///
        double const ascent_frac_;
 };
 
 
 PreviewImage::PreviewImage(PreviewLoader & l,
-                          string const & s,
+                          docstring const & s,
                           FileName const & f,
                           double af)
        : pimpl_(new Impl(*this, l, s, f, af))
@@ -68,64 +67,66 @@ PreviewImage::~PreviewImage()
 }
 
 
-string const & PreviewImage::snippet() const
+docstring const & PreviewImage::snippet() const
 {
        return pimpl_->snippet_;
 }
 
 
-int PreviewImage::ascent() const
+support::FileName const & PreviewImage::filename() const
 {
-       Image const * const image = pimpl_->iloader_.image();
-       if (!image)
-               return 0;
-
-       return int(pimpl_->ascent_frac_ * double(image->height()));
+       return pimpl_->iloader_.filename();
 }
 
 
-int PreviewImage::descent() const
+Dimension PreviewImage::dim() const
 {
+       Dimension dim;
        Image const * const image = pimpl_->iloader_.image();
        if (!image)
-               return 0;
+               return dim;
 
-       // Avoids rounding errors.
-       return image->height() - ascent();
+       dim.asc = support::iround(pimpl_->ascent_frac_ * double(image->height()));
+       dim.des = image->height() - dim.asc;
+       dim.wid = image->width();
+       return dim;
 }
 
 
-int PreviewImage::width() const
+Image const * PreviewImage::image() const
 {
-       Image const * const image = pimpl_->iloader_.image();
-       return image ? image->width() : 0;
+       return pimpl_->image();
 }
 
 
-Image const * PreviewImage::image() const
+PreviewLoader & PreviewImage::previewLoader() const
 {
-       return pimpl_->image();
+       return pimpl_->ploader_;
 }
 
 
-PreviewImage::Impl::Impl(PreviewImage & p, PreviewLoader & l,
-                        string const & s,
-                        FileName const & bf,
-                        double af)
-       : parent_(p), ploader_(l), iloader_(bf),
+PreviewImage::Impl::Impl(PreviewImage & p, PreviewLoader & l, docstring const & s,
+                         FileName const & bf, double af)
+       : parent_(p), ploader_(l), iloader_(l.buffer().fileName(), bf),
          snippet_(s), ascent_frac_(af)
 {
-       iloader_.connect(boost::bind(&Impl::statusChanged, this));
+       iloader_.setDisplayPixelRatio(l.displayPixelRatio());
+       // This connection is destroyed at the same time as this.
+       iloader_.connect([this](){ statusChanged(); });
 }
 
 
 PreviewImage::Impl::~Impl()
 {
-       support::unlink(iloader_.filename());
+       // If these images are generated for a clone, then that may be
+       // because we are previewing. We therefore do not want to delete
+       // them when this Buffer is destroyed.
+       if (!ploader_.buffer().isClone())
+               iloader_.filename().removeFile();
 }
 
 
-Image const * PreviewImage::Impl::image()
+Image const * PreviewImage::Impl::image() const
 {
        if (iloader_.status() == WaitingToLoad)
                iloader_.startLoading();
@@ -149,12 +150,15 @@ void PreviewImage::Impl::statusChanged()
        case ErrorLoading:
        case ErrorGeneratingPixmap:
        case ErrorUnknown:
-               //lyx::unlink(iloader_.filename());
+               //iloader_.filename().removeFile();
                ploader_.remove(snippet_);
-               break;
+               // FIXME: We need to return here, because PreviewLoader::remove
+               // removes the preview image from the cache, which deletes this
+               // object, so we should not try to do anything here.
+               return;
 
        case Ready:
-               support::unlink(iloader_.filename());
+               iloader_.filename().removeFile();
                break;
        }
        ploader_.emitSignal(parent_);