diff --git a/src/graphics/ChangeLog b/src/graphics/ChangeLog index 7234e7f416..9841e1049e 100644 --- a/src/graphics/ChangeLog +++ b/src/graphics/ChangeLog @@ -1,3 +1,11 @@ +2003-10-09 Angus Leeming + + * PreviewedInset.[Ch]: move PreviewedInset out of namespace lyx::graphics. + Remove the inset and view member functions. + Add a new connect member function and preview_ready_signal_ member + variable, to enable the class to tell an arbirary connectee that the + preview is ready. + 2003-10-09 Angus Leeming * PreviewedInset.[Ch] (removePreview, previewReady): these functions diff --git a/src/graphics/PreviewedInset.C b/src/graphics/PreviewedInset.C index e2ca9a9829..39a078cf8e 100644 --- a/src/graphics/PreviewedInset.C +++ b/src/graphics/PreviewedInset.C @@ -15,53 +15,47 @@ #include "PreviewLoader.h" #include "Previews.h" -#include "BufferView.h" - -#include "insets/inset.h" - #include "support/lstrings.h" #include -namespace support = lyx::support; +namespace graphics = lyx::graphics; +namespace support = lyx::support; -namespace lyx { -namespace graphics { - bool PreviewedInset::activated() { - return Previews::activated(); + return graphics::Previews::activated(); } -PreviewedInset::PreviewedInset(InsetOld & inset) - : inset_(inset), pimage_(0) +PreviewedInset::PreviewedInset() + : pimage_(0) {} -BufferView * PreviewedInset::view() const +boost::signals::connection PreviewedInset::connect(slot_type const & slot) { - return inset_.view(); + return preview_ready_signal_.connect(slot); } void PreviewedInset::generatePreview(Buffer const & buffer) { - if (!Previews::activated() || !previewWanted(buffer)) + if (!activated() || !previewWanted(buffer)) return; - Previews & previews = Previews::get(); - PreviewLoader & loader = previews.loader(buffer); + graphics::Previews & previews = graphics::Previews::get(); + graphics::PreviewLoader & loader = previews.loader(buffer); addPreview(loader); if (!snippet_.empty()) loader.startLoading(); } -void PreviewedInset::addPreview(PreviewLoader & ploader) +void PreviewedInset::addPreview(graphics::PreviewLoader & ploader) { - if (!Previews::activated() || !previewWanted(ploader.buffer())) + if (!activated() || !previewWanted(ploader.buffer())) return; snippet_ = support::trim(latexString(ploader.buffer())); @@ -89,8 +83,8 @@ void PreviewedInset::removePreview(Buffer const & buffer) if (snippet_.empty()) return; - Previews & previews = Previews::get(); - PreviewLoader & loader = previews.loader(buffer); + graphics::Previews & previews = graphics::Previews::get(); + graphics::PreviewLoader & loader = previews.loader(buffer); loader.remove(snippet_); snippet_.erase(); pimage_ = 0; @@ -99,11 +93,12 @@ void PreviewedInset::removePreview(Buffer const & buffer) bool PreviewedInset::previewReady(Buffer const & buffer) const { - if (!Previews::activated() || !previewWanted(buffer)) + if (!activated() || !previewWanted(buffer)) return false; if (!pimage_ || snippet_ != pimage_->snippet()) { - PreviewLoader & ploader = Previews::get().loader(buffer); + graphics::PreviewLoader & ploader = + graphics::Previews::get().loader(buffer); pimage_ = ploader.preview(snippet_); } @@ -111,17 +106,12 @@ bool PreviewedInset::previewReady(Buffer const & buffer) const } -void PreviewedInset::imageReady(PreviewImage const & pimage) const +void PreviewedInset::imageReady(graphics::PreviewImage const & pimage) const { // Check the current snippet is the same as that previewed. if (snippet_ != pimage.snippet()) return; pimage_ = &pimage; - - if (view()) - view()->updateInset(&inset()); + preview_ready_signal_(); } - -} // namespace graphics -} // namespace lyx diff --git a/src/graphics/PreviewedInset.h b/src/graphics/PreviewedInset.h index 03e7df730d..ee05138b63 100644 --- a/src/graphics/PreviewedInset.h +++ b/src/graphics/PreviewedInset.h @@ -16,12 +16,12 @@ #ifndef PREVIEWEDINSET_H #define PREVIEWEDINSET_H +#include #include #include class Buffer; class BufferView; -class InsetOld; namespace lyx { @@ -30,13 +30,16 @@ namespace graphics { class PreviewImage; class PreviewLoader; +} // namespace graphics +} // namespace lyx + class PreviewedInset : public boost::signals::trackable { public: /// a wrapper for Previews::activated() static bool activated(); /// - PreviewedInset(InsetOld & inset); + PreviewedInset(); /** Find the PreviewLoader, add a LaTeX snippet to it and * start the loading process. @@ -46,7 +49,7 @@ public: /** Add a LaTeX snippet to the PreviewLoader but do not start the * loading process. */ - void addPreview(PreviewLoader & ploader); + void addPreview(lyx::graphics::PreviewLoader & ploader); /** Remove a snippet from the cache of previews. * Useful if previewing the contents of a file that has changed. @@ -56,54 +59,39 @@ public: /// The preview has been generated and is ready to use. bool previewReady(Buffer const &) const; - /// If !previewReady() returns 0. - PreviewImage const * pimage() const; + /// If the preview is not ready, returns 0. + lyx::graphics::PreviewImage const * const pimage() const { return pimage_; } + + /// Connect and you'll be informed when the preview is ready. + typedef boost::signal0::slot_type slot_type; + boost::signals::connection connect(slot_type const &); protected: /// virtual ~PreviewedInset() {} - /// Allow the daughter classes to cast up to the parent inset. - InsetOld const & inset() const; - /// - BufferView * view() const; private: /// This method is connected to the PreviewLoader::imageReady signal. - void imageReady(PreviewImage const &) const; + void imageReady(lyx::graphics::PreviewImage const &) const; /// Does the owning inset want a preview? virtual bool previewWanted(Buffer const &) const = 0; /// a wrapper to Inset::latex virtual std::string const latexString(Buffer const &) const = 0; - /// - InsetOld & inset_; - /// + /// The thing that we're trying to generate a preview of. std::string snippet_; /// We don't own this. Cached for efficiency reasons. - mutable PreviewImage const * pimage_; + mutable lyx::graphics::PreviewImage const * pimage_; + /** Store the connection to the preview loader so that we connect * only once. */ boost::signals::connection ploader_connection_; + + /// This signal is emitted when the preview is ready for display. + boost::signal0 preview_ready_signal_; }; - -inline -PreviewImage const * PreviewedInset::pimage() const -{ - return pimage_; -} - - -inline -InsetOld const & PreviewedInset::inset() const -{ - return inset_; -} - -} // namespace graphics -} // namespace lyx - #endif // PREVIEWEDINSET_H diff --git a/src/insets/ChangeLog b/src/insets/ChangeLog index f1f984b539..32ebf45e50 100644 --- a/src/insets/ChangeLog +++ b/src/insets/ChangeLog @@ -1,3 +1,11 @@ +2003-10-09 Angus Leeming + + * insetexternal.[Ch] (statusChanged): + * insetgraphics.[Ch] (statusChanged): make a const member function. + + * insetinclude.[Ch]: mods to PreviewImpl due to the changes to + PreviewedInset. + 2003-10-09 Angus Leeming * insetinclude.C (metrics, draw, restartLoading): pass a buffer arg diff --git a/src/insets/insetexternal.C b/src/insets/insetexternal.C index 08f4a4f240..89e2ca9c61 100644 --- a/src/insets/insetexternal.C +++ b/src/insets/insetexternal.C @@ -387,7 +387,7 @@ InsetExternal::~InsetExternal() } -void InsetExternal::statusChanged() +void InsetExternal::statusChanged() const { BufferView * const bv = renderer_->view(); if (bv) diff --git a/src/insets/insetexternal.h b/src/insets/insetexternal.h index 0c700b30f4..408848132f 100644 --- a/src/insets/insetexternal.h +++ b/src/insets/insetexternal.h @@ -131,7 +131,7 @@ private: /** This method is connected to the graphics loader, so we are * informed when the image has been loaded. */ - void statusChanged(); + void statusChanged() const; /// The current params InsetExternalParams params_; diff --git a/src/insets/insetgraphics.C b/src/insets/insetgraphics.C index 5c1164c254..db63aaba09 100644 --- a/src/insets/insetgraphics.C +++ b/src/insets/insetgraphics.C @@ -181,7 +181,7 @@ InsetGraphics::~InsetGraphics() } -void InsetGraphics::statusChanged() +void InsetGraphics::statusChanged() const { BufferView * bv = graphic_->view(); if (bv) diff --git a/src/insets/insetgraphics.h b/src/insets/insetgraphics.h index 6ac5f21424..e9de865a19 100644 --- a/src/insets/insetgraphics.h +++ b/src/insets/insetgraphics.h @@ -85,7 +85,7 @@ private: /** This method is connected to the graphics loader, so we are * informed when the image has been loaded. */ - void statusChanged(); + void statusChanged() const; /// Read the inset native format void readInsetGraphics(LyXLex & lex, std::string const & bufpath); diff --git a/src/insets/insetinclude.C b/src/insets/insetinclude.C index c27daa8a51..24e75ddfc7 100644 --- a/src/insets/insetinclude.C +++ b/src/insets/insetinclude.C @@ -65,20 +65,16 @@ using std::ostringstream; extern BufferList bufferlist; -class InsetInclude::PreviewImpl : public lyx::graphics::PreviewedInset { +class InsetInclude::PreviewImpl : public PreviewedInset { public: /// - PreviewImpl(InsetInclude & p) : PreviewedInset(p) {} + PreviewImpl(InsetInclude const & p) : parent_(p) {} /// bool previewWanted(Buffer const &) const; /// string const latexString(Buffer const &) const; /// - InsetInclude const & parent() const { - return dynamic_cast(inset()); - } - /// bool monitoring() const { return monitor_.get(); } /// @@ -91,6 +87,8 @@ private: void restartLoading(); /// boost::scoped_ptr monitor_; + /// + InsetInclude const & parent_; }; @@ -109,7 +107,9 @@ InsetInclude::InsetInclude(InsetCommandParams const & p) : params_(p), include_label(uniqueID()), preview_(new PreviewImpl(*this)), set_label_(false) -{} +{ + preview_->connect(boost::bind(&InsetInclude::statusChanged, this)); +} InsetInclude::InsetInclude(InsetInclude const & other) @@ -118,7 +118,9 @@ InsetInclude::InsetInclude(InsetInclude const & other) include_label(other.include_label), preview_(new PreviewImpl(*this)), set_label_(other.set_label_) -{} +{ + preview_->connect(boost::bind(&InsetInclude::statusChanged, this)); +} InsetInclude::~InsetInclude() @@ -195,7 +197,7 @@ Types type(InsetCommandParams const & params) bool isVerbatim(InsetCommandParams const & params) { string const command_name = params.getCmdName(); - return command_name == "verbatiminput" || + return command_name == "verbatiminput" || command_name == "verbatiminput*"; } @@ -210,8 +212,7 @@ void InsetInclude::set(InsetCommandParams const & p, Buffer const & buffer) if (preview_->monitoring()) preview_->stopMonitoring(); - if (lyx::graphics::PreviewedInset::activated() && - type(params_) == INPUT) + if (PreviewedInset::activated() && type(params_) == INPUT) preview_->generatePreview(buffer); } @@ -593,6 +594,13 @@ BufferView * InsetInclude::view() const // preview stuff // +void InsetInclude::statusChanged() const +{ + if (view()) + view()->updateInset(this); +} + + void InsetInclude::addPreview(lyx::graphics::PreviewLoader & ploader) const { preview_->addPreview(ploader); @@ -601,10 +609,10 @@ void InsetInclude::addPreview(lyx::graphics::PreviewLoader & ploader) const bool InsetInclude::PreviewImpl::previewWanted(Buffer const & buffer) const { - string const included_file = includedFilename(buffer, parent().params()); + string const included_file = includedFilename(buffer, parent_.params()); - return type(parent().params_) == INPUT && - parent().params_.preview() && + return type(parent_.params_) == INPUT && + parent_.params_.preview() && IsFileReadable(included_file); } @@ -614,7 +622,7 @@ string const InsetInclude::PreviewImpl::latexString(Buffer const & buffer) const ostringstream os; LatexRunParams runparams; runparams.flavor = LatexRunParams::LATEX; - parent().latex(buffer, os, runparams); + parent_.latex(buffer, os, runparams); return os.str(); } @@ -630,11 +638,12 @@ void InsetInclude::PreviewImpl::startMonitoring(string const & file) void InsetInclude::PreviewImpl::restartLoading() { - if (!view()) + BufferView * const view = parent_.view(); + if (!view) return; - view()->updateInset(&parent()); - if (view()->buffer()) { - Buffer const & buffer = *view()->buffer(); + view->updateInset(&parent_); + if (view->buffer()) { + Buffer const & buffer = *view->buffer(); removePreview(buffer); generatePreview(buffer); } diff --git a/src/insets/insetinclude.h b/src/insets/insetinclude.h index 27b4a551ac..127babff59 100644 --- a/src/insets/insetinclude.h +++ b/src/insets/insetinclude.h @@ -83,6 +83,9 @@ public: void addPreview(lyx::graphics::PreviewLoader &) const; private: + /// Slot receiving a signal that the preview is ready to display. + void statusChanged() const; + friend class InsetIncludeMailer; /// set the parameters diff --git a/src/mathed/ChangeLog b/src/mathed/ChangeLog index 7c0637f8c1..eecd49f19c 100644 --- a/src/mathed/ChangeLog +++ b/src/mathed/ChangeLog @@ -1,3 +1,8 @@ +2003-10-09 Angus Leeming + + * formula.[Ch]: mods to PreviewImpl due to the changes to + PreviewedInset. + 2003-10-09 Angus Leeming * formula.C (metrics, draw): pass a buffer arg to PreviewedInset's diff --git a/src/mathed/formula.C b/src/mathed/formula.C index 47e0a82337..e739e16d7b 100644 --- a/src/mathed/formula.C +++ b/src/mathed/formula.C @@ -30,6 +30,7 @@ #include "support/std_sstream.h" +#include using std::string; using std::ostream; @@ -39,10 +40,10 @@ using std::auto_ptr; using std::endl; -class InsetFormula::PreviewImpl : public lyx::graphics::PreviewedInset { +class InsetFormula::PreviewImpl : public PreviewedInset { public: /// - PreviewImpl(InsetFormula & p) : PreviewedInset(p) {} + PreviewImpl(InsetFormula const & p) : parent_(p) {} private: /// @@ -50,18 +51,15 @@ private: /// string const latexString(Buffer const &) const; /// - InsetFormula const & parent() const - { - return dynamic_cast(inset()); - } + InsetFormula const & parent_; }; - InsetFormula::InsetFormula(bool chemistry) : par_(MathAtom(new MathHullInset)), preview_(new PreviewImpl(*this)) { + preview_->connect(boost::bind(&InsetFormula::statusChanged, this)); if (chemistry) mutate("chemistry"); } @@ -71,14 +69,16 @@ InsetFormula::InsetFormula(InsetFormula const & other) : InsetFormulaBase(other), par_(other.par_), preview_(new PreviewImpl(*this)) -{} +{ + preview_->connect(boost::bind(&InsetFormula::statusChanged, this)); +} InsetFormula::InsetFormula(BufferView *) : par_(MathAtom(new MathHullInset)), preview_(new PreviewImpl(*this)) { - //view_ = bv->owner()->view(); + preview_->connect(boost::bind(&InsetFormula::statusChanged, this)); } @@ -86,6 +86,7 @@ InsetFormula::InsetFormula(string const & data) : par_(MathAtom(new MathHullInset)), preview_(new PreviewImpl(*this)) { + preview_->connect(boost::bind(&InsetFormula::statusChanged, this)); if (!data.size()) return; if (!mathed_parse_normal(par_, data)) @@ -93,7 +94,6 @@ InsetFormula::InsetFormula(string const & data) } - InsetFormula::~InsetFormula() {} @@ -300,6 +300,13 @@ void InsetFormula::mutate(string const & type) // preview stuff // +void InsetFormula::statusChanged() const +{ + if (view()) + view()->updateInset(this); +} + + void InsetFormula::addPreview(lyx::graphics::PreviewLoader & ploader) const { preview_->addPreview(ploader); @@ -314,7 +321,7 @@ void InsetFormula::generatePreview(Buffer const & buffer) const bool InsetFormula::PreviewImpl::previewWanted(Buffer const &) const { - return !parent().par_->asNestInset()->editing(); + return !parent_.par_->asNestInset()->editing(); } @@ -322,6 +329,6 @@ string const InsetFormula::PreviewImpl::latexString(Buffer const &) const { ostringstream ls; WriteStream wi(ls, false, false); - parent().par_->write(wi); + parent_.par_->write(wi); return ls.str(); } diff --git a/src/mathed/formula.h b/src/mathed/formula.h index 14c650e301..927d3512c3 100644 --- a/src/mathed/formula.h +++ b/src/mathed/formula.h @@ -74,6 +74,8 @@ public: void mutate(std::string const & type); private: + /// Slot receiving a signal that the preview is ready to display. + void statusChanged() const; /// available in AMS only? bool ams() const;