From 850e4d80c0ec8df578c4ab5d73952e435141b7d6 Mon Sep 17 00:00:00 2001 From: Abdelrazak Younes Date: Tue, 12 Jul 2011 12:43:11 +0000 Subject: [PATCH] Graphics cleanup: get rid of Previews.{h,cpp} The PreviewLoader is created directly by Buffer on demand. The PreviewLoader cache was complex and unneeded because there is one and only one PreviewLoader per Buffer. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@39276 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/Buffer.cpp | 45 +++++++++++++++----- src/Buffer.h | 6 +++ src/BufferView.cpp | 2 - src/LyX.cpp | 11 ----- src/LyX.h | 5 --- src/Makefile.am | 4 +- src/graphics/Previews.cpp | 82 ------------------------------------ src/graphics/Previews.h | 63 --------------------------- src/insets/RenderPreview.cpp | 34 +++++++-------- src/insets/RenderPreview.h | 2 +- 10 files changed, 59 insertions(+), 195 deletions(-) delete mode 100644 src/graphics/Previews.cpp delete mode 100644 src/graphics/Previews.h diff --git a/src/Buffer.cpp b/src/Buffer.cpp index 7d85e3a2ce..a96e46c0d9 100644 --- a/src/Buffer.cpp +++ b/src/Buffer.cpp @@ -77,12 +77,12 @@ #include "mathed/MathMacroTemplate.h" #include "mathed/MathSupport.h" +#include "graphics/PreviewLoader.h" + #include "frontends/alert.h" #include "frontends/Delegates.h" #include "frontends/WorkAreaManager.h" -#include "graphics/Previews.h" - #include "support/lassert.h" #include "support/convert.h" #include "support/debug.h" @@ -117,6 +117,7 @@ using namespace std; using namespace lyx::support; +using namespace lyx::graphics; namespace lyx { @@ -150,6 +151,7 @@ public: ~Impl() { + delete preview_loader_; if (wa_) { wa_->closeAll(); delete wa_; @@ -267,6 +269,9 @@ public: /// our Text that should be wrapped in an InsetText InsetText * inset; + /// + PreviewLoader * preview_loader_; + /// This is here to force the test to be done whenever parent_buffer /// is accessed. Buffer const * parent() const { @@ -331,10 +336,11 @@ Buffer::Impl::Impl(Buffer * owner, FileName const & file, bool readonly_, Buffer const * cloned_buffer) : owner_(owner), lyx_clean(true), bak_clean(true), unnamed(false), read_only(readonly_), filename(file), file_fully_loaded(false), - toc_backend(owner), macro_lock(false), timestamp_(0), - checksum_(0), wa_(0), gui_(0), undo_(*owner), bibinfo_cache_valid_(false), - bibfile_cache_valid_(false), cite_labels_valid_(false), - cloned_buffer_(cloned_buffer), doing_export(false), parent_buffer(0) + toc_backend(owner), macro_lock(false), timestamp_(0), checksum_(0), + wa_(0), gui_(0), undo_(*owner), preview_loader_(0), + bibinfo_cache_valid_(false), bibfile_cache_valid_(false), + cite_labels_valid_(false), cloned_buffer_(cloned_buffer), + doing_export(false), parent_buffer(0) { if (!cloned_buffer_) { temppath = createBufferTmpDir(); @@ -946,16 +952,35 @@ void Buffer::setFullyLoaded(bool value) } -void Buffer::updatePreviews() const +PreviewLoader * Buffer::loader() const { - if (graphics::Previews::status() != LyXRC::PREVIEW_OFF) - thePreviews().generateBufferPreviews(*this); + if (lyxrc.preview == LyXRC::PREVIEW_OFF) + return 0; + if (!d->preview_loader_) + d->preview_loader_ = new PreviewLoader(*this); + return d->preview_loader_; } void Buffer::removePreviews() const { - thePreviews().removeLoader(*this); + delete d->preview_loader_; + d->preview_loader_ = 0; +} + + +void Buffer::updatePreviews() const +{ + PreviewLoader * ploader = loader(); + if (!ploader) + return; + + InsetIterator it = inset_iterator_begin(*d->inset); + InsetIterator const end = inset_iterator_end(*d->inset); + for (; it != end; ++it) + it->addPreview(it, *ploader); + + ploader->startLoading(); } diff --git a/src/Buffer.h b/src/Buffer.h index 79ccabf5f5..118428ad12 100644 --- a/src/Buffer.h +++ b/src/Buffer.h @@ -72,6 +72,10 @@ class FileName; class FileNameList; } +namespace graphics { +class PreviewLoader; +} + class Buffer; typedef std::list ListOfBuffers; @@ -511,6 +515,8 @@ public: /// Set by buffer_funcs' newFile. void setFullyLoaded(bool); + /// FIXME: Needed by RenderPreview. + graphics::PreviewLoader * Buffer::loader() const; /// Update the LaTeX preview snippets associated with this buffer void updatePreviews() const; /// Remove any previewed LaTeX snippets associated with this buffer diff --git a/src/BufferView.cpp b/src/BufferView.cpp index 9ec1e7d4b3..21f04d6684 100644 --- a/src/BufferView.cpp +++ b/src/BufferView.cpp @@ -69,8 +69,6 @@ #include "frontends/Painter.h" #include "frontends/Selection.h" -#include "graphics/Previews.h" - #include "support/convert.h" #include "support/debug.h" #include "support/ExceptionMessage.h" diff --git a/src/LyX.cpp b/src/LyX.cpp index 8652b4b032..05cca99c78 100644 --- a/src/LyX.cpp +++ b/src/LyX.cpp @@ -48,8 +48,6 @@ #include "frontends/alert.h" #include "frontends/Application.h" -#include "graphics/Previews.h" - #include "support/lassert.h" #include "support/debug.h" #include "support/environment.h" @@ -193,8 +191,6 @@ struct LyX::Impl /// the parsed command line batch command if any vector batch_commands; - /// - graphics::Previews preview_; /// SpellChecker * spell_checker_; /// @@ -1380,13 +1376,6 @@ Messages const & getGuiMessages() } -graphics::Previews & thePreviews() -{ - LASSERT(singleton_, /**/); - return singleton_->pimpl_->preview_; -} - - Session & theSession() { LASSERT(singleton_, /**/); diff --git a/src/LyX.h b/src/LyX.h index 4e66f2c196..0b1e02ea06 100644 --- a/src/LyX.h +++ b/src/LyX.h @@ -57,10 +57,6 @@ namespace frontend { class Application; } -namespace graphics { -class Previews; -} - /// initial startup class LyX { public: @@ -151,7 +147,6 @@ private: friend void setMover(std::string const & fmt, std::string const & command); friend Movers & theSystemMovers(); friend frontend::Application * theApp(); - friend graphics::Previews & thePreviews(); friend Session & theSession(); friend CmdDef & theTopLevelCmdDef(); friend SpellChecker * theSpellChecker(); diff --git a/src/Makefile.am b/src/Makefile.am index 6a38664d96..6d196d98b5 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -348,9 +348,7 @@ liblyxgraphics_a_SOURCES = \ graphics/PreviewImage.h \ graphics/PreviewImage.cpp \ graphics/PreviewLoader.h \ - graphics/PreviewLoader.cpp \ - graphics/Previews.h \ - graphics/Previews.cpp + graphics/PreviewLoader.cpp ############################### Mathed ############################## diff --git a/src/graphics/Previews.cpp b/src/graphics/Previews.cpp deleted file mode 100644 index 0b1e481078..0000000000 --- a/src/graphics/Previews.cpp +++ /dev/null @@ -1,82 +0,0 @@ -/** - * \file Previews.cpp - * This file is part of LyX, the document processor. - * Licence details can be found in the file COPYING. - * - * \author Angus Leeming - * - * Full author contact details are available in file CREDITS. - */ - -#include - -#include "Previews.h" -#include "PreviewLoader.h" - -#include "Buffer.h" -#include "InsetIterator.h" -#include "LyXRC.h" - -#include "insets/Inset.h" -#include "support/shared_ptr.h" - -using namespace std; - -namespace lyx { - -namespace graphics { - -LyXRC_PreviewStatus Previews::status() -{ - return lyxrc.preview; -} - - -namespace { -typedef shared_ptr PreviewLoaderPtr; -/// -typedef map LyxCacheType; -/// -static LyxCacheType preview_cache_; -} - - -PreviewLoader & Previews::loader(Buffer const & buffer) const -{ - LyxCacheType::iterator it = preview_cache_.find(&buffer); - - if (it == preview_cache_.end()) { - PreviewLoaderPtr ptr(new PreviewLoader(buffer)); - preview_cache_[&buffer] = ptr; - return *ptr.get(); - } - - return *it->second.get(); -} - - -void Previews::removeLoader(Buffer const & buffer) const -{ - LyxCacheType::iterator it = preview_cache_.find(&buffer); - - if (it != preview_cache_.end()) - preview_cache_.erase(it); -} - - -void Previews::generateBufferPreviews(Buffer const & buffer) const -{ - PreviewLoader & ploader = loader(buffer); - - Inset & inset = buffer.inset(); - InsetIterator it = inset_iterator_begin(inset); - InsetIterator const end = inset_iterator_end(inset); - - for (; it != end; ++it) - it->addPreview(it, ploader); - - ploader.startLoading(); -} - -} // namespace graphics -} // namespace lyx diff --git a/src/graphics/Previews.h b/src/graphics/Previews.h deleted file mode 100644 index 31ba7ed95a..0000000000 --- a/src/graphics/Previews.h +++ /dev/null @@ -1,63 +0,0 @@ -// -*- C++ -*- -/** - * \file Previews.h - * This file is part of LyX, the document processor. - * Licence details can be found in the file COPYING. - * - * \author Angus Leeming - * - * Full author contact details are available in file CREDITS. - * - * graphics::Previews is a singleton class that stores the - * graphics::PreviewLoader for each buffer requiring one. - */ - -#ifndef PREVIEWS_H -#define PREVIEWS_H - -namespace lyx { - -class Buffer; -class LyXRC_PreviewStatus; - -namespace graphics { - -class PreviewLoader; - -class Previews { -public: - /// This should be a singleton class only instanciated in LyX.cpp. - Previews() {} - - /// a wrapper for lyxrc.preview - static LyXRC_PreviewStatus status(); - - /** Returns the PreviewLoader for this buffer. - * Used by individual insets to update their own preview. - */ - PreviewLoader & loader(Buffer const & buffer) const; - - /// Called from the Buffer d-tor. - void removeLoader(Buffer const & buffer) const; - - /** For a particular buffer, initiate the generation of previews - * for each and every snippet of LaTeX that's of interest with - * a single forked process. - */ - void generateBufferPreviews(Buffer const & buffer) const; - -private: - /// noncopyable - Previews(Previews const &); - void operator=(Previews const &); -}; - -} // namespace graphics - -/// This is a singleton class. Get the instance. -/// Implemented in LyX.cpp. -graphics::Previews & thePreviews(); - -} // namespace lyx - -#endif // PREVIEWS_H diff --git a/src/insets/RenderPreview.cpp b/src/insets/RenderPreview.cpp index 5a51d793b2..0c567631b0 100644 --- a/src/insets/RenderPreview.cpp +++ b/src/insets/RenderPreview.cpp @@ -13,6 +13,7 @@ #include "insets/RenderPreview.h" #include "insets/Inset.h" +#include "Buffer.h" #include "BufferView.h" #include "Dimension.h" #include "LyX.h" @@ -24,7 +25,6 @@ #include "graphics/PreviewImage.h" #include "graphics/PreviewLoader.h" -#include "graphics/Previews.h" #include "support/FileName.h" #include "support/gettext.h" @@ -41,7 +41,7 @@ namespace lyx { LyXRC_PreviewStatus RenderPreview::status() { - return graphics::Previews::status(); + return lyxrc.preview; } @@ -74,19 +74,13 @@ RenderBase * RenderPreview::clone(Inset const * inset) const namespace { -graphics::PreviewLoader & getPreviewLoader(Buffer const & buffer) -{ - return thePreviews().loader(buffer); -} - - docstring const statusMessage(BufferView const * bv, string const & snippet) { LASSERT(bv, /**/); Buffer const & buffer = bv->buffer(); - graphics::PreviewLoader const & loader = getPreviewLoader(buffer); - graphics::PreviewLoader::Status const status = loader.status(snippet); + graphics::PreviewLoader const * loader = buffer.loader(); + graphics::PreviewLoader::Status const status = loader->status(snippet); docstring message; switch (status) { @@ -111,8 +105,9 @@ docstring const statusMessage(BufferView const * bv, string const & snippet) graphics::PreviewImage const * RenderPreview::getPreviewImage(Buffer const & buffer) const { - graphics::PreviewLoader const & loader = getPreviewLoader(buffer); - return loader.preview(snippet_); + graphics::PreviewLoader const * loader = buffer.loader(); + LASSERT(loader, return 0); + return loader->preview(snippet_); } @@ -180,8 +175,9 @@ void RenderPreview::startLoading(Buffer const & buffer, bool forexport) const if (!forexport && (status() == LyXRC::PREVIEW_OFF || snippet_.empty())) return; - graphics::PreviewLoader const & loader = getPreviewLoader(buffer); - loader.startLoading(forexport); + graphics::PreviewLoader * loader = buffer.loader(); + LASSERT(loader, return); + loader->startLoading(forexport); } @@ -192,8 +188,9 @@ void RenderPreview::addPreview(docstring const & latex_snippet, if (status() == LyXRC::PREVIEW_OFF && !ignore_lyxrc) return; - graphics::PreviewLoader & loader = getPreviewLoader(buffer); - addPreview(latex_snippet, loader, ignore_lyxrc); + graphics::PreviewLoader * loader = buffer.loader(); + LASSERT(loader, return); + addPreview(latex_snippet, *loader, ignore_lyxrc); } @@ -230,8 +227,9 @@ void RenderPreview::removePreview(Buffer const & buffer) if (snippet_.empty()) return; - graphics::PreviewLoader & loader = getPreviewLoader(buffer); - loader.remove(snippet_); + graphics::PreviewLoader * loader = buffer.loader(); + LASSERT(loader, return); + loader->remove(snippet_); snippet_.erase(); } diff --git a/src/insets/RenderPreview.h b/src/insets/RenderPreview.h index 0476e687e5..a0f447e336 100644 --- a/src/insets/RenderPreview.h +++ b/src/insets/RenderPreview.h @@ -44,7 +44,7 @@ class PreviewLoader; class RenderPreview : public RenderBase, public boost::signals::trackable { public: - /// a wrapper for Previews::status() + /// a wrapper for lyxrc.preview static LyXRC_PreviewStatus status(); RenderPreview(Inset const *);