Replace FileMonitor with FileMonitor2 in RenderPreview

This commit is contained in:
Guillaume Munch 2017-03-09 22:44:07 +01:00
parent e3a1396813
commit a4fae021f2
4 changed files with 58 additions and 25 deletions

View File

@ -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<RenderMonitoredPreview>(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)

View File

@ -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<RenderMonitoredPreview>(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<RenderMonitoredPreview>(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_);

View File

@ -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

View File

@ -20,6 +20,7 @@
#include "support/docstring.h"
#include "support/FileMonitor.h"
#include "support/FileName.h"
#include <boost/signals2.hpp>
#include <boost/signals2/trackable.hpp>
@ -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<void()> 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