diff --git a/src/insets/InsetExternal.cpp b/src/insets/InsetExternal.cpp index 8ddd7565d7..3b79249cc0 100644 --- a/src/insets/InsetExternal.cpp +++ b/src/insets/InsetExternal.cpp @@ -629,14 +629,12 @@ void InsetExternal::setParams(InsetExternalParams const & p) LASSERT(false, return); break; case PREVIEW_INSTANT: { - renderer_.reset(new RenderMonitoredPreview(this)); + renderer_ = make_unique(this); RenderMonitoredPreview * preview_ptr = renderer_->asMonitoredPreview(); - preview_ptr->fileChanged(bind(&InsetExternal::fileChanged, this)); - if (preview_ptr->monitoring()) - preview_ptr->stopMonitoring(); + preview_ptr->connect([=]() { fileChanged(); }); add_preview_and_start_loading(*preview_ptr, *this, buffer()); break; - } + } case PREVIEW_GRAPHICS: { RenderGraphic * graphic_ptr = renderer_->asGraphic(); if (!graphic_ptr) { @@ -837,7 +835,7 @@ void InsetExternal::validate(LaTeXFeatures & features) const void InsetExternal::addPreview(DocIterator const & /*inset_pos*/, - graphics::PreviewLoader & ploader) const + graphics::PreviewLoader & ploader) const { RenderMonitoredPreview * const ptr = renderer_->asMonitoredPreview(); if (!ptr) diff --git a/src/insets/InsetInclude.cpp b/src/insets/InsetInclude.cpp index 3a8c0b8c7e..a5ed017d75 100644 --- a/src/insets/InsetInclude.cpp +++ b/src/insets/InsetInclude.cpp @@ -170,10 +170,10 @@ InsetLabel * createLabel(Buffer * buf, docstring const & label_str) InsetInclude::InsetInclude(Buffer * buf, InsetCommandParams const & p) : InsetCommand(buf, p), include_label(uniqueID()), - preview_(new RenderMonitoredPreview(this)), failedtoload_(false), + preview_(make_unique(this)), failedtoload_(false), set_label_(false), label_(0), child_buffer_(0) { - preview_->fileChanged(bind(&InsetInclude::fileChanged, this)); + preview_->connect([=](){ fileChanged(); }); if (isListings(params())) { InsetListingsParams listing_params(to_utf8(p["lstparams"])); @@ -185,10 +185,10 @@ InsetInclude::InsetInclude(Buffer * buf, InsetCommandParams const & p) InsetInclude::InsetInclude(InsetInclude const & other) : InsetCommand(other), include_label(other.include_label), - preview_(new RenderMonitoredPreview(this)), failedtoload_(false), + preview_(make_unique(this)), failedtoload_(false), set_label_(false), label_(0), child_buffer_(0) { - preview_->fileChanged(bind(&InsetInclude::fileChanged, this)); + preview_->connect([=](){ fileChanged(); }); if (other.label_) label_ = new InsetLabel(*other.label_); diff --git a/src/insets/RenderPreview.cpp b/src/insets/RenderPreview.cpp index e985d290a2..99abca9f3c 100644 --- a/src/insets/RenderPreview.cpp +++ b/src/insets/RenderPreview.cpp @@ -271,28 +271,58 @@ void RenderPreview::imageReady(graphics::PreviewImage const & pimage) RenderMonitoredPreview::RenderMonitoredPreview(Inset const * inset) - : RenderPreview(inset), monitor_(FileName(), 2000) -{} + : RenderPreview(inset) +{ + setAbsFile(FileName()); +} void RenderMonitoredPreview::setAbsFile(FileName const & file) { - monitor_.reset(file); + bool mon = monitoring(); + if (mon) + stopMonitoring(); + filename_ = file; + if (mon) + startMonitoring(); } void RenderMonitoredPreview::draw(PainterInfo & pi, int x, int y) const { RenderPreview::draw(pi, x, y); - if (!monitoring()) - startMonitoring(); + startMonitoring(); } boost::signals2::connection -RenderMonitoredPreview::fileChanged(slot_type const & slot) +RenderMonitoredPreview::connect(ChangedSig::slot_type const & slot) { - return monitor_.connect(slot); + return changed_.connect(slot); } + +bool RenderMonitoredPreview::monitoring() const +{ + return (bool) monitor_; +} + + +void RenderMonitoredPreview::startMonitoring() const +{ + if (!monitoring()) { + monitor_ = FileSystemWatcher::monitor(filename_); + monitor_->connect(changed_); + } +} + + +void RenderMonitoredPreview::stopMonitoring() const +{ + monitor_ = nullptr; +} + + + + } // namespace lyx diff --git a/src/insets/RenderPreview.h b/src/insets/RenderPreview.h index 841ffdacde..c6dd74d28e 100644 --- a/src/insets/RenderPreview.h +++ b/src/insets/RenderPreview.h @@ -20,6 +20,7 @@ #include "support/docstring.h" #include "support/FileMonitor.h" +#include "support/FileName.h" #include #include @@ -31,8 +32,6 @@ class Buffer; class MetricsInfo; class PainterInfo; -namespace support { class FileName; } - namespace graphics { class PreviewImage; @@ -120,22 +119,28 @@ public: /// void setAbsFile(support::FileName const & file); /// - bool monitoring() const { return monitor_.monitoring(); } - void startMonitoring() const { monitor_.start(); } - void stopMonitoring() const { monitor_.stop(); } + bool monitoring() const; + void startMonitoring() const; + void stopMonitoring() const; /// Connect and you'll be informed when the file changes. - typedef support::FileMonitor::slot_type slot_type; - boost::signals2::connection fileChanged(slot_type const &); + typedef boost::signals2::signal ChangedSig; + boost::signals2::connection connect(ChangedSig::slot_type const &); /// equivalent to dynamic_cast virtual RenderMonitoredPreview * asMonitoredPreview() { return this; } private: + /// This signal is emitted if the file is modified + ChangedSig changed_; /// - mutable support::FileMonitor monitor_; + mutable support::FileMonitorPtr monitor_; + /// + support::FileName filename_; }; + + } // namespace lyx #endif // RENDERPREVIEW_H