On Tuesday 25 June 2002 7:28 pm, Andre Poenitz wrote:
> On Tue, Jun 25, 2002 at 08:21:17PM +0000, [EMAIL PROTECTED] wrote:
> >     first shot at preview. It crashes. Don't know why. To see it work a
> >     bit, change formula.C:191, to see it crash remove the comment further
> >     down.
>
> Actually I still don't really understand the way the graphics/*.[Ch] stuff
> wiorks, but it looks already _much_ simpler now.
>
> Could it be that the comments are curretnliy a bit out-of-sync with the
> code?
>
> Andre'

I don't think so. Which comments aren't you happy with.

Anyway, last night I was mulling over a grfx::Loader class that would make 
life really easy. I've just about finished it, so here's the header file for 
you to comment on. This should be the only graphics file you need to worry 
about.

Here's an example of how you'd use it if you're interested in exactly when 
the pixmap is ready for use so that you can display it instantly.

struct Preview {
        Preview();
        void statusChanged();
private:
        grfx::Loader loader;
};

Preview::Preview()
{
        string const file_with_path = ...;
        loader.reset(file_with_path);
        /// The signal is emitted when a file is connected...
        loader.statusChanged.connect(
                boost::bind(&Preview::statusChanged, this));
        if (loader.status() == grfx::WaitingToLoad)
                loader.startLoading();
}

void Preview::statusChanged()
{
        if (loader.status() != grfx::Ready)
                return;
        Pixmap pixmap = loader.image()->getPixmap();
        ...
}

If you aren't interested in such niceties then lose the "connect" step and 
just use 
        if (loader.image()->getPixmap()) {
                ...
        }
in your own draw routine. How simple is that?

Angus


// -*- C++ -*-
/**
 *  \file GraphicsLoader.h
 *  Copyright 2002 the LyX Team
 *  Read the file COPYING
 *
 *  \author Angus Leeming <[EMAIL PROTECTED]>
 *
 *  The public view of the graphics cache.
 *  * The user supplies an image file and the display parameters.
 *  * He can change the file or the display parameters through a reset() 
method.
 *  * He must start the loading process explicitly with startLoading().
 *  * He receives a statusChanged signal when the loading status changes.
 *  * When (status() == Ready), he uses image() to access the loaded image
 *    and passes it to the Painter.
 *
 *  What could be simpler?
 */

#ifndef GRAPHICSLOADER_H
#define GRAPHICSLOADER_H

#ifdef __GNUG__
#pragma interface
#endif

#include "GraphicsTypes.h"
#include "LString.h"

#include <boost/signals/signal0.hpp>
#include <memory>

namespace grfx {

class GParams;

/** One image, one instance of GraphicsLoader, although the image can be
 *  changed.
 */
class Loader {
public:
        /// Must use the reset methods to make this instance usable.
        Loader();
        /// The image is not transformed, just displayed as-is.
        Loader(string const & file_with_path, DisplayType = ColorDisplay);
        /// The image is transformed before display.
        Loader(string const & file_with_path, GParams const &);

        /// The file can be changed, or the display params or both.
        void resetFile(string const & file_with_path,
                       DisplayType = ColorDisplay);
        ///
        void resetFile(string const & file_with_path, GParams const &);
        ///
        void resetParams(GParams const &);

        /// Returns the absolute path of the loaded (loading?) file.
        string const & filename() const;
        ///
        bool empty() const { return filename().empty(); }

        /// We are explicit about when we begin the loading process.
        void startLoading();

        ///  How far have we got in loading the image?
        ImageStatus status() const;

        /// This signal is emitted when the image loading status changes.
        boost::signal0<void> statusChanged;

        /** The loaded image with Pixmap set.
         *  If the Pixmap is not yet set (see status() for why...), returns 0.
         */
        GImage const * image() const;

private:
        /// Use the Pimpl idiom to hide the internals.
        class Impl;
        /// The pointer never changes although *pimpl_'s contents may.
        std::auto_ptr<Impl> const pimpl_;
};

} // namespace grfx

#endif // GRAPHICSLOADER_H

Reply via email to