grapher-nsplugin-example.html

来自「QT 下载资料仅供参考」· HTML 代码 · 共 709 行 · 第 1/2 页

HTML
709
字号
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><!-- /home/reggie/tmp/qt-3.0-reggie-5401/qt-x11-commercial-3.0.5/extensions/nsplugin/examples/grapher/grapher.doc:1 --><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Grapher Plugin</title><style type="text/css"><!--h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }a:link { color: #004faf; text-decoration: none }a:visited { color: #672967; text-decoration: none }body { background: #ffffff; color: black; }--></style></head><body><table border="0" cellpadding="0" cellspacing="0" width="100%"><tr bgcolor="#E5E5E5"><td valign=center> <a href="index.html"><font color="#004faf">Home</font></a> | <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a> | <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a> | <a href="annotated.html"><font color="#004faf">Annotated</font></a> | <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a> | <a href="functions.html"><font color="#004faf">Functions</font></a></td><td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>Grapher Plugin</h1> <p> This example graphs data from a simple text file.  Itdemonstrates the use of the <a href="qnpinstance.html#writeReady">QNPInstance::writeReady</a>()and <a href="qnpinstance.html#write">QNPInstance::write</a>() functions.<p> To build the example, you must first build the<a href=nsplugin.html>Qt Netscape Plugin Extension</a> library.Then type <tt>make</tt> in <tt>extensions/nsplugin/examples/grapher/</tt>and copy the resulting <tt>grapher.so</tt> or <tt>npgrapher.dll</tt>to the Plugins directory of your WWW browser.<p> <EMBED ALIGN=LEFT WIDTH=49% HEIGHT=300 SRC=graph.g1ngraphstyle=pie fontfamily=times fontsize=18><p> The text file it accepts as input has a title line, thena sequence of lines with a number, then a string.  Theplugin displays a pie chart of the numbers, each segmentlabelled by the associated string.  The user can selecta bar chart view of the same data by selecting from themenu that appears when they point at the plugin.<p> The HTML tag used to embed the graph is:<small><pre>  &lt;EMBED    SRC=graph.g1n    ALIGN=LEFT    WIDTH=49% HEIGHT=300    graphstyle=pie fontfamily=times    fontsize=18&gt;</pre><p> </small>Note that some HTML arguments (which we have capitalized here)are interpreted by the browser, while others are used by theplugin.<p> <br clear>With the simplicity and cross-platform nature of Qt-based plugins,pages like <a href="http://www.netcraft.com/survey/">Netcraft'sServer Graphs</a> can be provided much more efficiently for boththe service provider and consumer.  Data need not be convertedto an image at the server.<p> <br clear><hr>Implementation:<p> <pre>// Include Qt Netscape Plugin classes.#include "qnp.h"// Include other Qt classes.#include &lt;<a href="qpainter-h.html">qpainter.h</a>&gt;#include &lt;<a href="qtextstream-h.html">qtextstream.h</a>&gt;#include &lt;<a href="qbuffer-h.html">qbuffer.h</a>&gt;#include &lt;<a href="qpixmap-h.html">qpixmap.h</a>&gt;#include &lt;<a href="qmenubar-h.html">qmenubar.h</a>&gt;#include &lt;<a href="qpushbutton-h.html">qpushbutton.h</a>&gt;#include &lt;<a href="qptrlist-h.html">qptrlist.h</a>&gt;#include &lt;<a href="qmessagebox-h.html">qmessagebox.h</a>&gt;// Include some C library functions.#include &lt;math.h&gt;#include &lt;stdlib.h&gt;#ifndef M_PI // Some math.h don't include this.#define M_PI 3.14159265358979323846264338327950288#endif//// GraphModel is a simple abstract class that describes// a table of numeric and text data.//class GraphModel {public:    enum ColType { Numeric, Label };    union Datum {        double dbl;        <a href="qstring.html">QString</a>* str;    };    virtual QPtrList&lt;Datum&gt;&amp; graphData()=0;    virtual ColType colType(int col) const=0;    virtual int nCols() const=0;};//// Graph is a widget subclass that displays a GraphModel.// Since the widget is a QNPWidget, it can be used as a plugin window,// returned by Grapher::newWindow() below.//class Graph : public <a href="qnpwidget.html">QNPWidget</a> {    <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>public:    // Constructs a Graph to display a GraphModel    //    Graph(GraphModel&amp;);    ~Graph();    // Two styles are available - Pie and Bar graph    //    enum Style { Pie, Bar };    static const char* styleName[];    void setStyle(Style);    void setStyle(const char*);    // Timer event processing rotates the pie graph    //    void timerEvent(QTimerEvent*);    // These functions are provided by QNPWidget - we override    // them to hide and show the plugin menubar.    //    void enterInstance();    void leaveInstance();    // Paint the graph...    //    void paintEvent(QPaintEvent*);    //    // ... as either a "Loading" message, a Bar graph, a Pie graph,    // or an error message.    //    void paintWait(QPaintEvent*);    void paintBar(QPaintEvent*);    void paintPie(QPaintEvent*);    void paintError(const char*);signals:    // Signals emitted when the Help menus are selected.    void aboutPlugin();    void aboutData();private:    GraphModel&amp; model;    <a href="qmenubar.html">QMenuBar</a> *menubar;    Style style;    <a href="qpopupmenu.html">QPopupMenu</a>* stylemenu;    int pieRotationTimer;    int pieRotation;    <a href="qpixmap.html">QPixmap</a> pm;private slots:    void setStyleFromMenu(int id);};<a name="f628"></a>Graph::Graph( GraphModel&amp; mdl ) :    model(mdl),    <a href="qwidget.html#style">style</a>(Bar),    pieRotationTimer(0),    pieRotation(0){    // Create a menubar for the widget    //    menubar = new <a href="qmenubar.html">QMenuBar</a>( this );    stylemenu = new <a href="qpopupmenu.html">QPopupMenu</a>;<a name="x2776"></a>    stylemenu-&gt;<a href="qpopupmenu.html#setCheckable">setCheckable</a>(TRUE);    for ( Style s = Pie; styleName[s]; s = Style(s+1)) {        stylemenu-&gt;<a href="qmenudata.html#insertItem">insertItem</a>(styleName[s], s+100);    }<a name="x2775"></a>    <a href="qobject.html#connect">connect</a>(stylemenu, SIGNAL(<a href="qpopupmenu.html#activated">activated</a>(int)),        this, SLOT(setStyleFromMenu(int)));    <a href="qwidget.html#setStyle">setStyle</a>(Pie);    menubar-&gt;<a href="qmenudata.html#insertItem">insertItem</a>("Style", stylemenu);    menubar-&gt;<a href="qmenudata.html#insertSeparator">insertSeparator</a>();    <a href="qpopupmenu.html">QPopupMenu</a>* help = new <a href="qpopupmenu.html">QPopupMenu</a>;    help-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "About plugin...", this, SIGNAL(aboutPlugin()) );    help-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "About data...", this, SIGNAL(aboutData()) );    menubar-&gt;<a href="qmenudata.html#insertItem">insertItem</a>("Help", help);}Graph::~Graph(){}<a name="x2786"></a>void Graph::<a href="qwidget.html#setStyle">setStyle</a>(Style s){    if (style != s) {        if (pieRotationTimer)            <a href="qobject.html#killTimer">killTimer</a>(pieRotationTimer);<a name="x2757"></a>        stylemenu-&gt;<a href="qmenudata.html#setItemChecked">setItemChecked</a>(100+style, FALSE);        style = s;        if ( style == Pie )            pieRotationTimer = <a href="qobject.html#startTimer">startTimer</a>( 80 );        else            pieRotationTimer = 0;        stylemenu-&gt;<a href="qmenudata.html#setItemChecked">setItemChecked</a>(100+style, TRUE);        <a href="qwidget.html#update">update</a>();    }}<a name="x2763"></a>void Graph::<a href="qobject.html#timerEvent">timerEvent</a>(QTimerEvent*){    pieRotation = ( pieRotation + 6 ) % 360; repaint(FALSE);}void Graph::<a href="qwidget.html#setStyle">setStyle</a>(const char* stext){    for ( Style s = Pie; styleName[s]; s = Style(s+1) ) {        if ( <a href="qcstring.html#qstricmp">qstricmp</a>(stext,styleName[s])==0 ) {            <a href="qwidget.html#setStyle">setStyle</a>(s);            return;        }    }}<a name="x2761"></a>void Graph::<a href="qnpwidget.html#enterInstance">enterInstance</a>(){<a name="x2754"></a>    menubar-&gt;<a href="qmenubar.html#show">show</a>();}<a name="x2762"></a>void Graph::<a href="qnpwidget.html#leaveInstance">leaveInstance</a>(){<a name="x2753"></a>    menubar-&gt;<a href="qmenubar.html#hide">hide</a>();}void <a name="f629"></a>Graph::paintError(const char* e){    <a href="qpainter.html">QPainter</a> p(this);    int w = <a href="qwidget.html#width">width</a>();<a name="x2768"></a>    p.<a href="qpainter.html#drawText">drawText</a>(w/8, 0, w-w/4, height(), AlignCenter|WordBreak, e);}void <a name="f630"></a>Graph::paintBar(QPaintEvent* event){    if ( model.colType(0) != GraphModel::Numeric ) {        paintError("First column not numeric, cannot draw bar graph\n");        return;    }    <a href="qptrlist.html">QPtrList</a>&lt;GraphModel::Datum&gt;&amp; data = model.graphData();    double max = 0.0;<a name="x2780"></a>    for (GraphModel::Datum* rowdata = data.<a href="qptrlist.html#first">first</a>();<a name="x2781"></a>        rowdata; rowdata = data.<a href="qptrlist.html#next">next</a>())    {        if (rowdata[0].dbl &gt; max) max = rowdata[0].dbl;    }    const uint w = <a href="qwidget.html#width">width</a>();    const uint h = <a href="qwidget.html#height">height</a>();    <a href="qpainter.html">QPainter</a> p(this);<a name="x2770"></a>    p.<a href="qpainter.html#setClipRect">setClipRect</a>(event-&gt;rect());<a name="x2779"></a>    if ( w &gt; data.<a href="qptrlist.html#count">count</a>() ) {        // More pixels than data        int x = 0;        int i = 0;        <a href="qfontmetrics.html">QFontMetrics</a> fm=<a href="qwidget.html#fontMetrics">fontMetrics</a>();<a name="x2749"></a>        int fh = fm.<a href="qfontmetrics.html#height">height</a>();        for (GraphModel::Datum* rowdata = data.<a href="qptrlist.html#first">first</a>();            rowdata; rowdata = data.<a href="qptrlist.html#next">next</a>())        {            <a href="qcolor.html">QColor</a> c;<a name="x2748"></a>            c.<a href="qcolor.html#setHsv">setHsv</a>( (i * 255)/data.<a href="qptrlist.html#count">count</a>(), 255, 255 );// rainbow effect            p.<a href="qpainter.html#setBrush">setBrush</a>(c);            int bw = (w-w/4-x)/(data.<a href="qptrlist.html#count">count</a>()-i);            int bh = int((h-h/4-1)*rowdata[0].dbl/max);            p.<a href="qpainter.html#drawRect">drawRect</a>( w/8+x, h-h/8-1-bh, bw, bh );            i++;            x+=bw;        }    } else {        // More data than pixels        int x = 0;        int i = 0;        double av = 0.0;        int n = 0;        for (GraphModel::Datum* rowdata = data.<a href="qptrlist.html#first">first</a>(); rowdata;            rowdata = data.<a href="qptrlist.html#next">next</a>())        {            int bx = i*w/data.<a href="qptrlist.html#count">count</a>();            if (bx &gt; x) {                <a href="qcolor.html">QColor</a> c;                c.<a href="qcolor.html#setHsv">setHsv</a>( (x * 255)/w, 255, 255 );// rainbow effect                p.<a href="qpainter.html#setPen">setPen</a>(c);                int bh = int(h*av/n/max);                p.<a href="qpainter.html#drawLine">drawLine</a>(x,h-1,x,h-bh);                av = 0.0;                n = 0;                x = bx;            }            av += rowdata[0].dbl;            n++;            i++;        }    }}void <a name="f631"></a>Graph::paintPie(QPaintEvent* event){    if ( model.colType(0) != GraphModel::Numeric ) {        paintError("First column not numeric, cannot draw pie graph\n");        return;    }    <a href="qptrlist.html">QPtrList</a>&lt;GraphModel::Datum&gt;&amp; data = model.graphData();    double total = 0.0;    GraphModel::Datum* rowdata;    for (rowdata = data.<a href="qptrlist.html#first">first</a>();        rowdata; rowdata = data.<a href="qptrlist.html#next">next</a>())    {        total += rowdata[0].dbl;    }    // Only use first column for pie chart    if ( !total ) return;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?