From cc132995fa91134fd317d24449a4a2b07effd56f Mon Sep 17 00:00:00 2001 From: Angus Leeming Date: Fri, 2 Aug 2002 16:30:58 +0000 Subject: [PATCH] Cache the BufferView as a weak_ptr; get rid of those horrible current_views. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@4846 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/graphics/ChangeLog | 10 ++++++++++ src/graphics/GraphicsLoader.C | 16 +++++++++++----- src/graphics/PreviewLoader.C | 16 ++++++++++++++-- src/graphics/PreviewLoader.h | 3 +++ src/graphics/PreviewedInset.C | 30 ++++++++++++++++++++++++------ src/graphics/PreviewedInset.h | 11 +++++++++-- src/insets/ChangeLog | 10 ++++++++++ src/insets/insetcite.C | 2 +- src/insets/insetgraphics.C | 15 +++++++++------ src/insets/insetinclude.C | 9 +++------ src/mathed/ChangeLog | 5 +++++ src/mathed/formula.C | 7 +------ 12 files changed, 100 insertions(+), 34 deletions(-) diff --git a/src/graphics/ChangeLog b/src/graphics/ChangeLog index df1936d610..e09183afbd 100644 --- a/src/graphics/ChangeLog +++ b/src/graphics/ChangeLog @@ -1,3 +1,13 @@ +2002-08-02 Angus Leeming + + * GraphicsLoader.C: cache the BufferView as a weak_ptr. + + * PreviewLoader.[Ch] (buffer): new method, returning the owner. + (add): don't add empty snippets. + + * PreviewedInset.[Ch]: cache the BufferView as a weak_ptr. + (view, setView): access to to the cache_. + 2002-08-02 Angus Leeming * PreviewedInset.[Ch]: cache the LaTeX snippet. diff --git a/src/graphics/GraphicsLoader.C b/src/graphics/GraphicsLoader.C index c819f4379f..6873fdf7d1 100644 --- a/src/graphics/GraphicsLoader.C +++ b/src/graphics/GraphicsLoader.C @@ -13,14 +13,19 @@ #endif #include "GraphicsLoader.h" + +#include "BufferView.h" + #include "GraphicsCache.h" #include "GraphicsCacheItem.h" #include "GraphicsImage.h" #include "GraphicsParams.h" #include "GraphicsSupport.h" +#include "frontends/LyXView.h" #include "frontends/Timeout.h" +#include #include #include @@ -70,7 +75,7 @@ private: /// InsetList insets; /// - BufferView const * view; + boost::weak_ptr view; }; @@ -194,7 +199,7 @@ Image const * Loader::image() const Loader::Impl::Impl(Params const & params) : status_(WaitingToLoad), params_(params), - timer(2000, Timeout::ONETIME), view(0) + timer(2000, Timeout::ONETIME) { timer.timeout.connect(boost::bind(&Impl::checkedLoading, this)); } @@ -298,7 +303,7 @@ void Loader::Impl::startLoading(Inset const & inset, BufferView const & bv) it = std::find(it, end, &inset); if (it == end) insets.push_back(&inset); - view = &bv; + view = bv.owner()->view(); timer.start(); } @@ -326,10 +331,11 @@ private: void Loader::Impl::checkedLoading() { - if (insets.empty() || !view) + if (insets.empty() || !view.get()) return; - std::list const vps = getVisibleParagraphs(*view); + std::list const vps = + getVisibleParagraphs(*view.get()); InsetList::const_iterator it = insets.begin(); InsetList::const_iterator end = insets.end(); diff --git a/src/graphics/PreviewLoader.C b/src/graphics/PreviewLoader.C index a72cf57085..8e15ac8184 100644 --- a/src/graphics/PreviewLoader.C +++ b/src/graphics/PreviewLoader.C @@ -137,6 +137,8 @@ struct PreviewLoader::Impl : public boost::signals::trackable { /// Emit this signal when an image is ready for display. boost::signal1 imageReady; + Buffer const & buffer() const { return buffer_; } + private: /// Called by the Forkedcall process that generated the bitmap files. void finishedGenerating(string const &, pid_t, int); @@ -232,6 +234,12 @@ void PreviewLoader::emitSignal(PreviewImage const & pimage) const pimpl_->imageReady(pimage); } + +Buffer const & PreviewLoader::buffer() const +{ + return pimpl_->buffer(); +} + } // namespace grfx @@ -383,9 +391,13 @@ void PreviewLoader::Impl::add(string const & latex_snippet) if (!pconverter_ || status(latex_snippet) != NotFound) return; - lyxerr[Debug::GRAPHICS] << "adding snippet:\n" << latex_snippet << endl; + string const snippet = trim(latex_snippet); + if (snippet.empty()) + return; + + lyxerr[Debug::GRAPHICS] << "adding snippet:\n" << snippet << endl; - pending_.push_back(latex_snippet); + pending_.push_back(snippet); } diff --git a/src/graphics/PreviewLoader.h b/src/graphics/PreviewLoader.h index 82947e2ac4..b0238474e7 100644 --- a/src/graphics/PreviewLoader.h +++ b/src/graphics/PreviewLoader.h @@ -84,6 +84,9 @@ public: */ void emitSignal(PreviewImage const &) const; + /// Which buffer owns this loader. + Buffer const & buffer() const; + private: /// Use the Pimpl idiom to hide the internals. class Impl; diff --git a/src/graphics/PreviewedInset.C b/src/graphics/PreviewedInset.C index 1350396f6f..07a889b4e3 100644 --- a/src/graphics/PreviewedInset.C +++ b/src/graphics/PreviewedInset.C @@ -14,14 +14,18 @@ #include #include "PreviewedInset.h" - -#include "BufferView.h" - #include "GraphicsImage.h" #include "PreviewLoader.h" #include "PreviewImage.h" #include "Previews.h" +#include "buffer.h" +#include "BufferView.h" + +#include "frontends/LyXView.h" + +#include "support/lstrings.h" + #include @@ -42,7 +46,8 @@ void PreviewedInset::generatePreview() grfx::Previews & previews = grfx::Previews::get(); grfx::PreviewLoader & loader = previews.loader(view()->buffer()); addPreview(loader); - loader.startLoading(); + if (!snippet_.empty()) + loader.startLoading(); } @@ -51,7 +56,11 @@ void PreviewedInset::addPreview(grfx::PreviewLoader & ploader) if (!Previews::activated() || !previewWanted()) return; - snippet_ = latexString(); + setView(ploader.buffer().getUser()); + + snippet_ = trim(latexString()); + if (snippet_.empty()) + return; pimage_ = ploader.preview(snippet_); if (pimage_) @@ -71,7 +80,7 @@ void PreviewedInset::addPreview(grfx::PreviewLoader & ploader) bool PreviewedInset::previewReady() const { - if (!grfx::Previews::activated() || !previewWanted() || + if (!Previews::activated() || !previewWanted() || !view() || !view()->buffer()) return false; @@ -88,6 +97,14 @@ bool PreviewedInset::previewReady() const } +void PreviewedInset::setView(BufferView * bv) +{ + if (!bv) + return; + + view_ = bv->owner()->view(); +} + void PreviewedInset::imageReady(grfx::PreviewImage const & pimage) const { // Check snippet against the Inset's current contents @@ -95,6 +112,7 @@ void PreviewedInset::imageReady(grfx::PreviewImage const & pimage) const return; pimage_ = &pimage; + if (view()) view()->updateInset(&inset_, false); } diff --git a/src/graphics/PreviewedInset.h b/src/graphics/PreviewedInset.h index b7147478fe..eba7fbfd9b 100644 --- a/src/graphics/PreviewedInset.h +++ b/src/graphics/PreviewedInset.h @@ -19,6 +19,7 @@ #endif #include "LString.h" +#include #include #include @@ -56,9 +57,14 @@ public: /// If !previewReady() returns 0. PreviewImage const * pimage() const { return pimage_; } + /// + void setView(BufferView *); + protected: /// Allow the daughter classes to cast up to the parent inset. Inset * inset() const { return &inset_; } + /// + BufferView * view() const { return view_.get(); } private: /** This method is connected to the grfx::PreviewLoader::imageReady @@ -68,8 +74,6 @@ private: /// Does the owning inset want a preview? virtual bool previewWanted() const = 0; - /// - virtual BufferView * view() const = 0; /// a wrapper to Inset::latex virtual string const latexString() const = 0; @@ -77,6 +81,9 @@ private: Inset & inset_; /// string snippet_; + /// + boost::weak_ptr view_; + /// We don't own this. Cached for efficiency reasons. mutable PreviewImage const * pimage_; /// diff --git a/src/insets/ChangeLog b/src/insets/ChangeLog index 2bb07559b7..6050ae6bfe 100644 --- a/src/insets/ChangeLog +++ b/src/insets/ChangeLog @@ -1,3 +1,13 @@ +2002-08-02 Angus Leeming + + * insetcite.C: fix typo. + + * insetgraphics.C: kill current_view and instead cache the BufferView + as a weak_ptr. + + * insetinclude.C: kill current_view and pass the BufferView to the + PreviewedInset so that it can cache it. + 2002-08-01 Angus Leeming * inset.h (generatePreview): renamed as addPreview. diff --git a/src/insets/insetcite.C b/src/insets/insetcite.C index 0cdc98d2b5..dffb0abefe 100644 --- a/src/insets/insetcite.C +++ b/src/insets/insetcite.C @@ -40,7 +40,7 @@ using std::map; namespace { // An optimisation. We assume that until the first InsetCitation::edit is -// called, we're loding the buffer and that, therefore, we don't need to +// called, we're loading the buffer and that, therefore, we don't need to // reload the bibkey list std::map loading_buffer; diff --git a/src/insets/insetgraphics.C b/src/insets/insetgraphics.C index 67a5bd0e53..681f8c1810 100644 --- a/src/insets/insetgraphics.C +++ b/src/insets/insetgraphics.C @@ -64,7 +64,6 @@ TODO #include "graphics/GraphicsImage.h" #include "graphics/GraphicsParams.h" -#include "frontends/LyXView.h" #include "lyxtext.h" #include "buffer.h" #include "BufferView.h" @@ -77,8 +76,10 @@ TODO #include "LaTeXFeatures.h" #include "lyxlex.h" -#include "frontends/Dialogs.h" #include "frontends/Alert.h" +#include "frontends/Dialogs.h" +#include "frontends/LyXView.h" + #include "frontends/controllers/helper_funcs.h" // getVectorFromString #include "support/LAssert.h" @@ -88,14 +89,12 @@ TODO #include "support/systemcall.h" #include "support/os.h" +#include #include #include #include // For the std::max -// Very, Very UGLY! -extern BufferView * current_view; - extern string system_tempdir; using std::ostream; @@ -147,6 +146,8 @@ struct InsetGraphics::Cache : boost::signals::trackable grfx::Loader loader; /// unsigned long checksum; + /// + boost::weak_ptr view; private: /// @@ -293,6 +294,7 @@ int InsetGraphics::width(BufferView *, LyXFont const & font) const void InsetGraphics::draw(BufferView * bv, LyXFont const & font, int baseline, float & x, bool) const { + cache_->view = bv->owner()->view(); int oasc = cache_->old_ascent; int ldescent = descent(bv, font); @@ -888,7 +890,8 @@ void InsetGraphics::validate(LaTeXFeatures & features) const void InsetGraphics::statusChanged() { - current_view->updateInset(this, false); + if (cache_->view.get()) + cache_->view.get()->updateInset(this, false); } diff --git a/src/insets/insetinclude.C b/src/insets/insetinclude.C index f8c62f404a..6ecc614219 100644 --- a/src/insets/insetinclude.C +++ b/src/insets/insetinclude.C @@ -10,11 +10,11 @@ #include "BufferView.h" #include "debug.h" #include "lyxrc.h" -#include "frontends/LyXView.h" #include "LaTeXFeatures.h" #include "gettext.h" #include "frontends/Dialogs.h" +#include "frontends/LyXView.h" #include "frontends/Painter.h" #include "support/filetools.h" @@ -33,7 +33,7 @@ using std::vector; using std::pair; extern BufferList bufferlist; -extern BufferView * current_view; + class InsetInclude::PreviewImpl : public grfx::PreviewedInset { public: @@ -44,9 +44,6 @@ public: bool previewWanted() const; /// string const latexString() const; - - /// - BufferView * view() const { return current_view; } /// InsetInclude & parent() const { return *static_cast(inset()); @@ -500,6 +497,7 @@ int InsetInclude::width(BufferView * bv, LyXFont const & font) const void InsetInclude::draw(BufferView * bv, LyXFont const & font, int y, float & xx, bool b) const { + preview_->setView(bv); if (!preview_->previewReady()) { InsetButton::draw(bv, font, y, xx, b); return; @@ -524,7 +522,6 @@ void InsetInclude::draw(BufferView * bv, LyXFont const & font, int y, void InsetInclude::addPreview(grfx::PreviewLoader & ploader) const { - lyxerr << "InsetInclude::addPreview" << endl; preview_->addPreview(ploader); } diff --git a/src/mathed/ChangeLog b/src/mathed/ChangeLog index c17805e5d4..bc8c6f6c08 100644 --- a/src/mathed/ChangeLog +++ b/src/mathed/ChangeLog @@ -1,3 +1,8 @@ +2002-08-02 Angus Leeming + + * formula.C: pass the BufferView to the PreviewedInset so that it can + cache it. + 2002-08-02 Angus Leeming * formulabase.[Ch]: store the BufferView as a weak_ptr. diff --git a/src/mathed/formula.C b/src/mathed/formula.C index 8cc653e901..3f05043ecb 100644 --- a/src/mathed/formula.C +++ b/src/mathed/formula.C @@ -74,11 +74,6 @@ private: /// string const latexString() const; /// - BufferView * view() const - { - return parent().view(); - } - /// InsetFormula & parent() const { return *static_cast(inset()); @@ -207,7 +202,7 @@ void InsetFormula::draw(BufferView * bv, LyXFont const & font, { // This initiates the loading of the preview, so should come // before the metrics are computed. - view_ = bv->owner()->view(); + preview_->setView(bv); bool const use_preview = preview_->previewReady(); int const x = int(xx); -- 2.39.2