Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions

Display Images in Qt-supported Formats

This example displays images which are in any format supported by Qt. Combining this with the Qt ImageIO Extension adds PNG image support to your browser. It demonstrates the use of the QNPInstance::streamAsFile() function.

To build the example, you must first build the Qt Netscape Plugin Extension library. Then type make in extensions/nsplugin/examples/qtimage/ and copy the resulting qtimage.so or npqtimage.dll to the Plugins directory of your web browser.

This plugin displays a PNG format image.

New image formats can be supported by adding new image I/O handlers to the Qt library (see QImage::defineIOHandler), thus providing the functionality in both applications and WWW plugins from the same code.

This plugin supports transparency and scaling of the image, just like GIFs in most web browsers.

Implementation:

// Qt stuff
#include "qnp.h"
#include <qpainter.h>
#include <qimage.h>
#include <qpixmap.h>
#include <qmessagebox.h>
#include <qpopupmenu.h>
#include "qpngio.h"

#include <math.h>
#include <stdlib.h>
#include <stdio.h>

class ImageView : public QNPWidget {
public:
    ImageView()
    {
        popup = new QPopupMenu;
        popup->insertItem("Left as");
        popup->insertItem("An exercise");
        popup->insertItem("For the");
        popup->insertItem("Reader!");
    }

    void paintEvent(QPaintEvent* event)
    {
        QPainter p(this);
        p.setClipRect(event->rect());

        if ( pm.size() == size() ) {
            p.drawPixmap(0,0,pm);
        } else {
            if ( pmScaled.size() != size() ) {
                QWMatrix m;
                m.scale((double)width()/pm.width(),
                        (double)height()/pm.height());
                pmScaled = pm.xForm(m);
            }
            p.drawPixmap(0,0,pmScaled);
        }
    }

    void mousePressEvent(QMouseEvent* e)
    {
        popup->popup(mapToGlobal(e->pos()));
    }

    void showImage(const QImage& image)
    {
        pm.convertFromImage(image, QPixmap::Color);
        repaint( FALSE );
    }

private:
    QPixmap pm;
    QPixmap pmScaled;
    QPopupMenu* popup;
};

class ImageLoader : public QNPInstance {
    ImageView* iv;
    QImage image;

public:
    ImageLoader() : iv(0)
    {
    }

    QNPWidget* newWindow()
    {
        iv = new ImageView;
        imageToIV();
        return iv;
    }

    void imageToIV()
    {
        if (!iv || image.isNull()) return;

        iv->showImage(image);
        image.reset();
    }

    bool newStreamCreated(QNPStream*, StreamMode& smode)
    {
        smode = AsFileOnly;
        return TRUE;
    }

    void streamAsFile(QNPStream*, const char* fname)
    {
        //qInitPngIO();

        image = QImage(fname);
        if ( image.isNull() )
            fprintf(stderr, "Could not convert file: %s\n", fname);
        imageToIV();
    }
};

class ImagePlugin : public QNPlugin {

public:
    ImagePlugin()
    {
    }

    QNPInstance* newInstance()
    {
        return new ImageLoader;
    }

    const char* getMIMEDescription() const
    {
        return "image/x-png:png:PNG Image;"
               "image/png:png:PNG Image;"
               "image/x-portable-bitmap:pbm:PBM Image;"
               "image/x-portable-graymap:pgm:PGM Image;"
               "image/x-portable-pixmap:ppm:PPM Image;"
               "image/bmp:bmp:BMP Image;"
               "image/x-ms-bmp:bmp:BMP Image;"
               "image/x-xpixmap:xpm:XPM Image;"
               "image/xpm:xpm:XPM Image";
    }

    const char * getPluginNameString() const
    {
        return "Qt-based Image Plugin";
    }

    const char * getPluginDescriptionString() const
    {
        return "Supports all image formats supported by Qt";
    }
};

QNPlugin* QNPlugin::create()
{
    return new ImagePlugin;
}

See also LiveConnect Examples.


Copyright © 2002 TrolltechTrademarks
Qt version 3.0.5