]> git.lyx.org Git - lyx.git/blobdiff - src/graphics/PreviewImage.cpp
Merge branch 'master' into biblatex2
[lyx.git] / src / graphics / PreviewImage.cpp
index 47b30a4d339e1e43f7f78f6f0078c6cc8e3ba3bb..da0f1e21ceeb942207dd91152c937ef70a6d10a0 100644 (file)
 #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;
+#include "support/bind.h"
 
+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 boost::signals2::trackable {
 public:
        ///
        Impl(PreviewImage & p, PreviewLoader & l,
@@ -74,37 +73,35 @@ string const & PreviewImage::snippet() const
 }
 
 
-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 = int(pimpl_->ascent_frac_ * double(image->height()) + 0.5);
+       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_;
 }
 
 
@@ -112,16 +109,21 @@ PreviewImage::Impl::Impl(PreviewImage & p, PreviewLoader & l,
                         string const & s,
                         FileName const & bf,
                         double af)
-       : parent_(p), ploader_(l), iloader_(bf),
+       : 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());
+       iloader_.connect(bind(&Impl::statusChanged, this));
 }
 
 
 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();
 }
 
 
@@ -149,12 +151,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_);