André, I have a question for you. 

I'm envisaging starting off the preview process at two points in the 
document's lifetime.

1. Once the entire document is loaded into Buffer, loop over all insets to 
add a snippet of LaTeX to the preview queue and then start off the image 
generation. I thought that we could do this at the end of Buffer::readFile().

2. When the cursor leaves an inset, update the previewed image. You do that 
for the math insets in MathNestInset::notifyCursorLeaves(). 

I envisage the buffer having a method
        /// Must belong to the buffer, because we need the preamble
        grfx::PreviewQueue & previewer();

to be used so:

void MathNestInset::notifyCursorLeaves()
{
        if (!mathcursor)
                return;

        grfx::PreviewQueue & previewer = buffer_.previewer();
        mathcursor->formula()->generatePreview(previewer);
        previwer.startProcessing();
}

My question: how to I get hold of buffer_.previewer() from within 
notifyCursorLeaves()?

Angus

PS, so you can see the rest of my vision, here is some code showing how I 
think the Insets should interact with the Previewer. I haven't tried to 
compile this yet but I think the logic pans out. Could you have a look and 
give me your thoughts?

Regards,
A


/** \file insets/inset.h */
namespace grfx {
        class PreviewQueue;
}

class Inset {
public:
        /** Adds a LaTeX snippet to the preview queue for transformation
         *  into a bitmap image. Also connects to PreviewQueue::readyToLoad
         *  so that the inset is informed when the image has been generated,
         *  in order to initiate its loading into LyX.
         *
         *  Most insets have no interest in this capability, so the method
         *  defaults to empty.
         */
        virtual void generatePreview(grfx::PreviewQueue &) const {}
};

/** \file mathed/formula.h */
class InsetFormula {
public:
        ///
        void generatePreview(grfx::PreviewQueue &) const;

private:
        /** This method is connected to the grfx::PreviewQueue::readyToLoad
         *  signal. The inset will start the loading of image_file corresponding
         *  to the LaTeX snippet, assuming that this snippet is not redundant 
         *  of course...
         */
        void previewReady(string const & snippet,
                          string const & image_file) const;

        /** This method is connected to the grfx::Loader signal that'll inform
         *  us when the preview image has been loaded.
         */
        void statusChanged() const;
        
        /** A helper method. 
         *  Returns the current contents of the inset as a LaTeX string.
         */
        string const latexString() const;

        ///
        mutable string previewed_snippet_;
        ///
        mutable boost::signals::connection preview_connection_;
        ///
        mutable boost::scoped_ptr<grfx::Loader> loader_;
};

/* \file mathed/formula.h */
#include "graphics/GraphicsPreview.h"

string const latexString() const
{
        ostringstream ls;
        WriteStream wi(ls, false, false);
        par_->write(wi);
        return ls.str().c_str();
}


void InsetFormula::generatePreview(grfx::PreviewQueue & previewer) const
{
        // Do nothing if no preview is desired.
        if (!lyxrc.preview)
                return;

        // Generate the LaTeX snippet.
        string const data = latexString();

        // If this is the first time of calling, connect to the
        // grfx::PreviewQueue signal that'll inform us when the preview image
        // is ready for loading.
        if (!preview_connection_.conected()) {
                preview_connection_ = previewer.readyToLoad.connect(
                        boost::bind(&InsetFormula::previewReady, this));
        }

        previewer.addSnippet(data);
}

        
void InsetFormula::previewReady(string const & snippet,
                                string const & image_file) const
{
        // Check snippet against the Inset's current contents
        if (latexString() != snippet)
                return;

        previewed_snippet_ = snippet;

        // If this is the first time of calling, connect to the
        // grfx::Loader signal that'll inform us when the preview image has
        // been loaded.
        if (!loader_.get()) {
                loader_.reset(new grfx::Loader(image_file));
                loader_->statusChanged.connect(
                        boost::bind(&InsetFormula::statusChanged, this));
        } else {
                loader_->reset(image_file);
        }

        // Call directly just in case we can use the image already
        statusChanged();
}


void InsetFormula::statusChanged()
{
        // Check snippet against the Inset's current contents
        if (latexString() != previewed_snippet_)
                return;

        if (loader_->status() == grfx::WaitingToLoad)
                loader_.startLoading();
        else if (loader_->status() == grfx::Ready) {
                previewed_snippet_.clear();
                view()->updateInset(this, false);
        }
}

Reply via email to