From 005545f28100fd30afa22313d6e3b1b67aa9a857 Mon Sep 17 00:00:00 2001 From: Angus Leeming Date: Mon, 19 Apr 2004 13:05:36 +0000 Subject: [PATCH] Remove cached var from RenderPreview. Changes elsewhere to suit. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@8669 a592a061-630c-0410-9148-cb99ea01b6c8 --- lib/ChangeLog | 5 +++ lib/scripts/lyxpreview2bitmap.py | 2 +- src/insets/ChangeLog | 15 +++++++++ src/insets/insetexternal.C | 21 +++++++----- src/insets/insetinclude.C | 27 ++++++++++++--- src/insets/render_preview.C | 57 +++++++++++++++++--------------- src/insets/render_preview.h | 10 +++--- src/mathed/ChangeLog | 5 +++ src/mathed/math_hullinset.C | 24 ++++++++++---- 9 files changed, 112 insertions(+), 54 deletions(-) diff --git a/lib/ChangeLog b/lib/ChangeLog index ebfb07b320..cbff83dcc7 100644 --- a/lib/ChangeLog +++ b/lib/ChangeLog @@ -1,3 +1,8 @@ +2004-04-19 Angus Leeming + + * scripts/lyxpreview2bitmap.py (extract_metrics_info): make the + regex a little more robust. Now recognizes negative integers. + 2004-04-15 Angus Leeming * scripts/lyxpreview2bitmap.sh: removed. diff --git a/lib/scripts/lyxpreview2bitmap.py b/lib/scripts/lyxpreview2bitmap.py index 0a1f77c85f..ee91964728 100755 --- a/lib/scripts/lyxpreview2bitmap.py +++ b/lib/scripts/lyxpreview2bitmap.py @@ -102,7 +102,7 @@ def make_texcolor(hexcolor): def extract_metrics_info(dvipng_stdout, metrics_file): metrics = open(metrics_file, 'w') - metrics_re = re.compile("\[([0-9]+) depth=([0-9]+) height=([0-9]+)\]") + metrics_re = re.compile("\[([0-9]+) depth=(-?[0-9]+) height=(-?[0-9]+)") success = 0 pos = 0 diff --git a/src/insets/ChangeLog b/src/insets/ChangeLog index d7734ae4a0..633663e8e1 100644 --- a/src/insets/ChangeLog +++ b/src/insets/ChangeLog @@ -1,3 +1,18 @@ +2004-04-19 Angus Leeming + + * insetexternal.C (priv_dispatch): remove unnecessary invocation of + BufferView::update. + (add_preview): renamed as add_preview_and_start_loading. Invoke + RenderPreview::startLoading. + + * insetinclude.C (metrics, draw): changes due to the changed + RenderPreview interface. + + * render_preview.C: remove cached pimage_. + (previewReady): removed. Replaced by getPreviewImage(Buffer const &). + (draw): don't call startLoading. That's up to the user of + RenderPreview. + 2004-04-16 Angus Leeming * render_graphic.h (checksum_, hasFileChanged): unused, so removed. diff --git a/src/insets/insetexternal.C b/src/insets/insetexternal.C index 007100b8ea..51c79fea42 100644 --- a/src/insets/insetexternal.C +++ b/src/insets/insetexternal.C @@ -449,8 +449,6 @@ void InsetExternal::priv_dispatch(LCursor & cur, FuncRequest & cmd) InsetExternalParams p; InsetExternalMailer::string2params(cmd.argument, buffer, p); setParams(p, buffer); -#warning is this needed? - cur.bv().update(); break; } @@ -567,7 +565,9 @@ string const getScreenLabel(InsetExternalParams const & params, return external::doSubstitution(params, buffer, ptr->guiName); } -void add_preview(RenderMonitoredPreview &, InsetExternal const &, Buffer const &); +void add_preview_and_start_loading(RenderMonitoredPreview &, + InsetExternal const &, + Buffer const &); } // namespace anon @@ -621,7 +621,7 @@ void InsetExternal::setParams(InsetExternalParams const & p, if (preview_ptr->monitoring()) preview_ptr->stopMonitoring(); - add_preview(*preview_ptr, *this, buffer); + add_preview_and_start_loading(*preview_ptr, *this, buffer); break; } @@ -640,8 +640,7 @@ void InsetExternal::fileChanged() const Buffer const & buffer = *buffer_ptr; ptr->removePreview(buffer); - add_preview(*ptr, *this, buffer); - ptr->startLoading(buffer); + add_preview_and_start_loading(*ptr, *this, buffer); } @@ -774,14 +773,18 @@ string const latex_string(InsetExternal const & inset, Buffer const & buffer) } -void add_preview(RenderMonitoredPreview & renderer, InsetExternal const & inset, - Buffer const & buffer) +void add_preview_and_start_loading(RenderMonitoredPreview & renderer, + InsetExternal const & inset, + Buffer const & buffer) { InsetExternalParams const & params = inset.params(); - if (RenderPreview::activated() && preview_wanted(params)) { + + if (RenderPreview::activated() && + preview_wanted(params)) { renderer.setAbsFile(params.filename.absFilename()); string const snippet = latex_string(inset, buffer); renderer.addPreview(snippet, buffer); + renderer.startLoading(buffer); } } diff --git a/src/insets/insetinclude.C b/src/insets/insetinclude.C index e816694043..0930842971 100644 --- a/src/insets/insetinclude.C +++ b/src/insets/insetinclude.C @@ -32,6 +32,7 @@ #include "frontends/LyXView.h" #include "frontends/Painter.h" +#include "graphics/PreviewImage.h" #include "graphics/PreviewLoader.h" #include "insets/render_preview.h" @@ -568,7 +569,16 @@ void InsetInclude::fillWithBibKeys(Buffer const & buffer, void InsetInclude::metrics(MetricsInfo & mi, Dimension & dim) const { - if (RenderPreview::activated() && preview_->previewReady()) { + BOOST_ASSERT(mi.base.bv && mi.base.bv->buffer()); + + bool use_preview = false; + if (RenderPreview::activated()) { + lyx::graphics::PreviewImage const * pimage = + preview_->getPreviewImage(*mi.base.bv->buffer()); + use_preview = pimage && pimage->image(); + } + + if (use_preview) { preview_->metrics(mi, dim); } else { if (!set_label_) { @@ -592,12 +602,19 @@ void InsetInclude::draw(PainterInfo & pi, int x, int y) const { setPosCache(pi, x, y); - if (!RenderPreview::activated() || !preview_->previewReady()) { - button_.draw(pi, x + button_.box().x1, y); - return; + BOOST_ASSERT(pi.base.bv && pi.base.bv->buffer()); + + bool use_preview = false; + if (RenderPreview::activated()) { + lyx::graphics::PreviewImage const * pimage = + preview_->getPreviewImage(*pi.base.bv->buffer()); + use_preview = pimage && pimage->image(); } - preview_->draw(pi, x + button_.box().x1, y); + if (use_preview) + preview_->draw(pi, x + button_.box().x1, y); + else + button_.draw(pi, x + button_.box().x1, y); } diff --git a/src/insets/render_preview.C b/src/insets/render_preview.C index 40a54c9081..4bc8f4f477 100644 --- a/src/insets/render_preview.C +++ b/src/insets/render_preview.C @@ -14,6 +14,7 @@ #include "insets/inset.h" #include "BufferView.h" +#include "debug.h" #include "dimension.h" #include "gettext.h" #include "LColor.h" @@ -45,8 +46,7 @@ bool RenderPreview::activated() RenderPreview::RenderPreview(InsetBase const * inset) - : pimage_(0), - parent_(inset) + : parent_(inset) {} @@ -55,7 +55,6 @@ RenderPreview::RenderPreview(RenderPreview const & other, : RenderBase(other), boost::signals::trackable(), snippet_(other.snippet_), - pimage_(0), parent_(inset) {} @@ -97,12 +96,26 @@ string const statusMessage(BufferView const * bv, string const & snippet) } // namespace anon +graphics::PreviewImage const * +RenderPreview::getPreviewImage(Buffer const & buffer) const +{ + graphics::Previews & previews = graphics::Previews::get(); + graphics::PreviewLoader & loader = previews.loader(buffer); + return loader.preview(snippet_); +} + + void RenderPreview::metrics(MetricsInfo & mi, Dimension & dim) const { - if (previewReady()) { - dim.asc = pimage_->ascent(); - dim.des = pimage_->descent(); - dim.wid = pimage_->width(); + BOOST_ASSERT(mi.base.bv && mi.base.bv->buffer()); + + graphics::PreviewImage const * const pimage = + getPreviewImage(*mi.base.bv->buffer()); + + if (pimage) { + dim.asc = pimage->ascent(); + dim.des = pimage->descent(); + dim.wid = pimage->width(); } else { dim.asc = 50; dim.des = 0; @@ -121,13 +134,14 @@ void RenderPreview::metrics(MetricsInfo & mi, Dimension & dim) const void RenderPreview::draw(PainterInfo & pi, int x, int y) const { - BOOST_ASSERT(pi.base.bv && pi.base.bv->buffer()); - Buffer const & buffer = *pi.base.bv->buffer(); - startLoading(buffer); + BOOST_ASSERT(pi.base.bv && pi.base.bv->buffer()); - if (previewReady()) { + graphics::PreviewImage const * const pimage = + getPreviewImage(*pi.base.bv->buffer()); + + if (pimage && pimage->image()) { pi.pain.image(x, y - dim_.asc, dim_.wid, dim_.height(), - *(pimage_->image())); + *(pimage->image())); return; } @@ -154,6 +168,7 @@ void RenderPreview::startLoading(Buffer const & buffer) const graphics::Previews & previews = graphics::Previews::get(); graphics::PreviewLoader & loader = previews.loader(buffer); loader.startLoading(); + lyxerr << "RenderPreview::startLoading: " << snippet_ << std::endl; } @@ -176,12 +191,10 @@ void RenderPreview::addPreview(string const & latex_snippet, return; snippet_ = support::trim(latex_snippet); - pimage_ = 0; if (snippet_.empty()) return; - pimage_ = ploader.preview(snippet_); - if (pimage_) + if (ploader.preview(snippet_)) return; // If this is the first time of calling, connect to the @@ -205,24 +218,14 @@ void RenderPreview::removePreview(Buffer const & buffer) graphics::PreviewLoader & loader = previews.loader(buffer); loader.remove(snippet_); snippet_.erase(); - pimage_ = 0; -} - - -bool RenderPreview::previewReady() const -{ - return pimage_ ? pimage_->image() : false; } void RenderPreview::imageReady(graphics::PreviewImage const & pimage) { // Check the current snippet is the same as that previewed. - if (snippet_ != pimage.snippet()) - return; - - pimage_ = &pimage; - LyX::cref().updateInset(parent_); + if (snippet_ == pimage.snippet()) + LyX::cref().updateInset(parent_); } diff --git a/src/insets/render_preview.h b/src/insets/render_preview.h index 5b0bd23917..036a5ae2f3 100644 --- a/src/insets/render_preview.h +++ b/src/insets/render_preview.h @@ -72,8 +72,11 @@ public: */ void removePreview(Buffer const &); - /// The preview has been generated and is ready to use. - bool previewReady() const; + /** \returns a pointer to the PreviewImage associated with this snippet + * of latex. + */ + lyx::graphics::PreviewImage const * + getPreviewImage(Buffer const & buffer) const; /// equivalent to dynamic_cast virtual RenderPreview * asPreview() { return this; } @@ -88,9 +91,6 @@ private: /// The thing that we're trying to generate a preview of. std::string snippet_; - /// We don't own this. Cached for efficiency reasons. - lyx::graphics::PreviewImage const * pimage_; - /** Store the connection to the preview loader so that we connect * only once. */ diff --git a/src/mathed/ChangeLog b/src/mathed/ChangeLog index 8b8a17e243..53739de8d0 100644 --- a/src/mathed/ChangeLog +++ b/src/mathed/ChangeLog @@ -1,3 +1,8 @@ +2004-04-19 Angus Leeming + + * math_hullinset.C (metrics, draw): changes due to the changed + RenderPreview interface. + 2004-04-13 Angus Leeming * math_hullinset.[Ch]: add a RenderPreview variable. diff --git a/src/mathed/math_hullinset.C b/src/mathed/math_hullinset.C index da3d42eef8..8490ff9c0e 100644 --- a/src/mathed/math_hullinset.C +++ b/src/mathed/math_hullinset.C @@ -37,6 +37,7 @@ #include "frontends/Alert.h" +#include "graphics/PreviewImage.h" #include "graphics/PreviewLoader.h" #include "support/std_sstream.h" @@ -228,9 +229,14 @@ char const * MathHullInset::standardFont() const void MathHullInset::metrics(MetricsInfo & mi, Dimension & dim) const { - bool const use_preview = (!editing(mi.base.bv) && - RenderPreview::activated() && - preview_->previewReady()); + BOOST_ASSERT(mi.base.bv && mi.base.bv->buffer()); + + bool use_preview = false; + if (!editing(mi.base.bv) && RenderPreview::activated()) { + lyx::graphics::PreviewImage const * pimage = + preview_->getPreviewImage(*mi.base.bv->buffer()); + use_preview = pimage && pimage->image(); + } if (use_preview) { preview_->metrics(mi, dim); @@ -276,10 +282,14 @@ void MathHullInset::metrics(MetricsInfo & mi, Dimension & dim) const void MathHullInset::draw(PainterInfo & pi, int x, int y) const { - // The previews are drawn only when we're not editing the inset. - bool const use_preview = (!editing(pi.base.bv) && - RenderPreview::activated() && - preview_->previewReady()); + BOOST_ASSERT(pi.base.bv && pi.base.bv->buffer()); + + bool use_preview = false; + if (!editing(pi.base.bv) && RenderPreview::activated()) { + lyx::graphics::PreviewImage const * pimage = + preview_->getPreviewImage(*pi.base.bv->buffer()); + use_preview = pimage && pimage->image(); + } if (use_preview) { // one pixel gap in front