]> git.lyx.org Git - features.git/commitdiff
Add a monitor to the previewed image of a \input-ed file, so that the
authorAngus Leeming <leeming@lyx.org>
Tue, 6 Aug 2002 14:02:59 +0000 (14:02 +0000)
committerAngus Leeming <leeming@lyx.org>
Tue, 6 Aug 2002 14:02:59 +0000 (14:02 +0000)
image is refreshed if the file changes.

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@4879 a592a061-630c-0410-9148-cb99ea01b6c8

src/graphics/ChangeLog
src/graphics/PreviewedInset.C
src/graphics/PreviewedInset.h
src/insets/ChangeLog
src/insets/insetinclude.C

index 839345263d0dc95e25f2cfcedf0f5e0e695f64b8..58ee7bf15844847794e4aa1947bbd40b5045ba5f 100644 (file)
@@ -3,6 +3,9 @@
        * PreviewLoader.C: add support for preview.sty 0.73 (currently
        #ifdef-ed out, awaiting the formal release).
 
+       * PreviewedInset.[Ch] (removePreview): new method. Useful if previewing
+       the contents of a file that has changed.
+
 2002-08-05  Angus Leeming  <leeming@lyx.org>
 
        * PreviewLoader.C (dumpPreamble): add lyx to the list of options output
index 07a889b4e3563065f6300c64e026835f50182f77..940b15aaf3c0547c8517840958f2c8c24541e7e4 100644 (file)
@@ -28,6 +28,7 @@
 
 #include <boost/bind.hpp>
 
+#include "debug.h"    // temporary
 
 namespace grfx {
 
@@ -78,6 +79,19 @@ void PreviewedInset::addPreview(grfx::PreviewLoader & ploader)
 }
 
 
+void PreviewedInset::removePreview()
+{
+       if (!view() || !view()->buffer() || snippet_.empty())
+               return;
+
+       grfx::Previews & previews = grfx::Previews::get();
+       grfx::PreviewLoader & loader = previews.loader(view()->buffer());
+       loader.remove(snippet_);
+       snippet_.erase();
+       pimage_ = 0;
+}
+
+
 bool PreviewedInset::previewReady() const
 {
        if (!Previews::activated() || !previewWanted() ||
index eba7fbfd9b38864d26b03f3461dae60ff51d911e..192e3d28ea01ec282adefccd8010f6f50879c3c7 100644 (file)
@@ -51,6 +51,11 @@ public:
         */
        void addPreview(PreviewLoader & ploader);
 
+       /** Remove a snippet from the cache of previews.
+        *  Useful if previewing the contents of a file that has changed.
+        */
+       void removePreview();
+
        /// The preview has been generated and is ready to use.
        bool previewReady() const;
 
index 2fb59073e86e5a219259cdd5c914adfd598e390f..016b120f4e1ae3998ee2c955575805ce332e82be 100644 (file)
@@ -1,3 +1,8 @@
+2002-08-06  Angus Leeming  <leeming@lyx.org>
+
+       * insetinclude.C: add a monitor to the previewed image, so that the
+       image is refreshed if the \input-ed file changes.
+
 2002-08-04  John Levon  <levon@movementarian.org>
 
        * inset.h:
index dfc699a713a1f1cd8e25323c127cd1dacdb94b3a..74dbb0f691c321547ff0e2949ac454226517150f 100644 (file)
 
 #include "support/filetools.h"
 #include "support/FileInfo.h"
+#include "support/FileMonitor.h"
 #include "support/lstrings.h"
 
 #include "graphics/PreviewedInset.h"
 #include "graphics/PreviewImage.h"
 
+#include <boost/bind.hpp>
+
 #include <cstdlib>
 
 
@@ -48,6 +51,19 @@ public:
        InsetInclude & parent() const {
                return *static_cast<InsetInclude*>(inset());
        }
+
+       ///
+       bool monitoring() const { return monitor_.get(); }
+       ///
+       void startMonitoring();
+       ///
+       void stopMonitoring() { monitor_.reset(); }
+
+private:
+       /// Invoked by monitor_ should the parent file change.
+       void restartLoading();
+       ///
+       boost::scoped_ptr<FileMonitor> monitor_;
 };
 
 
@@ -134,6 +150,9 @@ void InsetInclude::set(Params const & p)
 
        params_.cparams.setCmdName(command);
 
+       if (preview_->monitoring())
+               preview_->stopMonitoring();
+
        if (grfx::PreviewedInset::activated() && params_.flag == INPUT)
                preview_->generatePreview();
 }
@@ -504,6 +523,9 @@ void InsetInclude::draw(BufferView * bv, LyXFont const & font, int y,
                return;
        }
 
+       if (!preview_->monitoring())
+               preview_->startMonitoring();
+
        int const x = int(xx);
        int const w = width(bv, font);
        int const d = descent(bv, font);
@@ -530,7 +552,8 @@ void InsetInclude::addPreview(grfx::PreviewLoader & ploader) const
 bool InsetInclude::PreviewImpl::previewWanted() const
 {
        return parent().params_.flag == InsetInclude::INPUT &&
-               parent().params_.cparams.preview();
+               parent().params_.cparams.preview() &&
+               IsFileReadable(parent().getFileName());
 }
 
 
@@ -545,7 +568,8 @@ string const InsetInclude::PreviewImpl::latexString() const
        // This fails if the file has a relative path.
        // return os.str().c_str();
 
-       // I would /really/ like not to do this...
+       // I would /really/ like not to do this, but don't know how to tell
+       // LaTeX where to find a \input-ed file...
        // HELP!
        string command;
        string file = rtrim(split(os.str().c_str(), command, '{'), "}");
@@ -558,3 +582,21 @@ string const InsetInclude::PreviewImpl::latexString() const
        
        return out.str().c_str();
 }
+
+
+void InsetInclude::PreviewImpl::startMonitoring()
+{
+       monitor_.reset(new FileMonitor(parent().getFileName(), 2000));
+       monitor_->connect(boost::bind(&PreviewImpl::restartLoading, this));
+       monitor_->start();
+}
+
+
+void InsetInclude::PreviewImpl::restartLoading()
+{
+       lyxerr << "restartLoading()" << std::endl;
+       removePreview();
+       if (view())
+               view()->updateInset(&parent(), false);
+       generatePreview();
+}