You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
705 lines
24 KiB
705 lines
24 KiB
14 years ago
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
|
||
|
<!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/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"><!--
|
||
|
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 Classes</font></a>
|
||
|
| <a href="mainclasses.html">
|
||
|
<font color="#004faf">Main Classes</font></a>
|
||
|
| <a href="annotated.html">
|
||
|
<font color="#004faf">Annotated</font></a>
|
||
|
| <a href="groups.html">
|
||
|
<font color="#004faf">Grouped 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. It
|
||
|
demonstrates 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.g1n
|
||
|
graphstyle=pie fontfamily=times fontsize=18>
|
||
|
<p> The text file it accepts as input has a title line, then
|
||
|
a sequence of lines with a number, then a string. The
|
||
|
plugin displays a pie chart of the numbers, each segment
|
||
|
labelled by the associated string. The user can select
|
||
|
a bar chart view of the same data by selecting from the
|
||
|
menu that appears when they point at the plugin.
|
||
|
<p> The HTML tag used to embed the graph is:
|
||
|
<small>
|
||
|
<pre>
|
||
|
<EMBED
|
||
|
SRC=graph.g1n
|
||
|
ALIGN=LEFT
|
||
|
WIDTH=49% HEIGHT=300
|
||
|
graphstyle=pie fontfamily=times
|
||
|
fontsize=18>
|
||
|
</pre><p> </small>
|
||
|
Note that some HTML arguments (which we have capitalized here)
|
||
|
are interpreted by the browser, while others are used by the
|
||
|
plugin.
|
||
|
<p> <br clear>
|
||
|
With the simplicity and cross-platform nature of Qt-based plugins,
|
||
|
pages like <a href="http://www.netcraft.com/survey/">Netcraft's
|
||
|
Server Graphs</a> can be provided much more efficiently for both
|
||
|
the service provider and consumer. Data need not be converted
|
||
|
to 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 <<a href="qpainter-h.html">qpainter.h</a>>
|
||
|
#include <<a href="qtextstream-h.html">qtextstream.h</a>>
|
||
|
#include <<a href="qbuffer-h.html">qbuffer.h</a>>
|
||
|
#include <<a href="qpixmap-h.html">qpixmap.h</a>>
|
||
|
#include <<a href="qmenubar-h.html">qmenubar.h</a>>
|
||
|
#include <<a href="qpushbutton-h.html">qpushbutton.h</a>>
|
||
|
#include <<a href="qptrlist-h.html">qptrlist.h</a>>
|
||
|
#include <<a href="qmessagebox-h.html">qmessagebox.h</a>>
|
||
|
|
||
|
// Include some C library functions.
|
||
|
#include <math.h>
|
||
|
#include <stdlib.h>
|
||
|
|
||
|
#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<Datum>& 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&);
|
||
|
~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& 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="f564"></a>Graph::Graph( GraphModel& 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="x2768"></a> stylemenu-><a href="qpopupmenu.html#setCheckable">setCheckable</a>(TRUE);
|
||
|
for ( Style s = Pie; styleName[s]; s = Style(s+1)) {
|
||
|
stylemenu-><a href="qmenudata.html#insertItem">insertItem</a>(styleName[s], s+100);
|
||
|
}
|
||
|
<a name="x2767"></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-><a href="qmenudata.html#insertItem">insertItem</a>("Style", stylemenu);
|
||
|
menubar-><a href="qmenudata.html#insertSeparator">insertSeparator</a>();
|
||
|
|
||
|
<a href="qpopupmenu.html">QPopupMenu</a>* help = new <a href="qpopupmenu.html">QPopupMenu</a>;
|
||
|
help-><a href="qmenudata.html#insertItem">insertItem</a>( "About plugin...", this, SIGNAL(aboutPlugin()) );
|
||
|
help-><a href="qmenudata.html#insertItem">insertItem</a>( "About data...", this, SIGNAL(aboutData()) );
|
||
|
menubar-><a href="qmenudata.html#insertItem">insertItem</a>("Help", help);
|
||
|
<a name="x2745"></a> menubar-><a href="qmenubar.html#hide">hide</a>();
|
||
|
}
|
||
|
|
||
|
Graph::~Graph()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
<a name="x2778"></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="x2749"></a> stylemenu-><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-><a href="qmenudata.html#setItemChecked">setItemChecked</a>(100+style, TRUE);
|
||
|
<a href="qwidget.html#update">update</a>();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
<a name="x2755"></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="x2753"></a>void Graph::<a href="qnpwidget.html#enterInstance">enterInstance</a>()
|
||
|
{
|
||
|
<a name="x2746"></a> menubar-><a href="qmenubar.html#show">show</a>();
|
||
|
}
|
||
|
|
||
|
<a name="x2754"></a>void Graph::<a href="qnpwidget.html#leaveInstance">leaveInstance</a>()
|
||
|
{
|
||
|
menubar-><a href="qmenubar.html#hide">hide</a>();
|
||
|
}
|
||
|
|
||
|
void <a name="f565"></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="x2760"></a> p.<a href="qpainter.html#drawText">drawText</a>(w/8, 0, w-w/4, height(), AlignCenter|WordBreak, e);
|
||
|
}
|
||
|
|
||
|
void <a name="f566"></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><GraphModel::Datum>& data = model.graphData();
|
||
|
|
||
|
double max = 0.0;
|
||
|
|
||
|
<a name="x2772"></a> for (GraphModel::Datum* rowdata = data.<a href="qptrlist.html#first">first</a>();
|
||
|
<a name="x2773"></a> rowdata; rowdata = data.<a href="qptrlist.html#next">next</a>())
|
||
|
{
|
||
|
if (rowdata[0].dbl > 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="x2762"></a> p.<a href="qpainter.html#setClipRect">setClipRect</a>(event->rect());
|
||
|
|
||
|
<a name="x2771"></a> if ( w > 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="x2741"></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="x2740"></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 > 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="f567"></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><GraphModel::Datum>& 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;
|
||
|
|
||
|
int apos = (pieRotation-90)*16;
|
||
|
|
||
|
const int w = <a href="qwidget.html#width">width</a>();
|
||
|
const int h = <a href="qwidget.html#height">height</a>();
|
||
|
|
||
|
const int xd = w - w/5;
|
||
|
const int yd = h - h/5;
|
||
|
|
||
|
<a name="x2766"></a> pm.<a href="qpixmap.html#resize">resize</a>(<a href="qwidget.html#width">width</a>(),height());
|
||
|
<a name="x2765"></a> pm.<a href="qpixmap.html#fill">fill</a>(<a href="qwidget.html#backgroundColor">backgroundColor</a>());
|
||
|
<a href="qpainter.html">QPainter</a> p(&pm);
|
||
|
<a name="x2763"></a> p.<a href="qpainter.html#setFont">setFont</a>(<a href="qwidget.html#font">font</a>());
|
||
|
|
||
|
p.<a href="qpainter.html#setClipRect">setClipRect</a>(event->rect());
|
||
|
|
||
|
int i = 0;
|
||
|
|
||
|
for (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;
|
||
|
|
||
|
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 ); // solid fill with color c
|
||
|
|
||
|
int a = int(( rowdata[0].dbl * 360.0 ) / total * 16.0 + 0.5);
|
||
|
<a name="x2757"></a> p.<a href="qpainter.html#drawPie">drawPie</a>( w/10, h/10, xd, yd, -apos, -a );
|
||
|
apos += a;
|
||
|
i++;
|
||
|
}
|
||
|
|
||
|
if (model.colType(1) == GraphModel::Label) {
|
||
|
double apos = (pieRotation-90)*M_PI/180;
|
||
|
|
||
|
for (rowdata = data.<a href="qptrlist.html#first">first</a>();
|
||
|
rowdata; rowdata = data.<a href="qptrlist.html#next">next</a>())
|
||
|
{
|
||
|
double a = rowdata[0].dbl * 360 / total * M_PI / 180;
|
||
|
int x = int(cos(apos+a/2)*w*5/16 + w/2 + 0.5);
|
||
|
int y = int(sin(apos+a/2)*h*5/16 + h/2 + 0.5);
|
||
|
|
||
|
// ### This causes a crash, so comment out for now
|
||
|
/*p.<a href="qpainter.html#drawText">drawText</a>(x-w/8, y-h/8, w/4, h/4,
|
||
|
WordBreak|AlignCenter,
|
||
|
*rowdata[1].str);*/
|
||
|
apos += a;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
<a href="qpainter.html">QPainter</a> p2(this);
|
||
|
p2.<a href="qpainter.html#setClipRect">setClipRect</a>(event->rect());
|
||
|
<a name="x2758"></a> p2.<a href="qpainter.html#drawPixmap">drawPixmap</a>(0,0,pm);
|
||
|
}
|
||
|
|
||
|
void <a name="f568"></a>Graph::paintWait(QPaintEvent*)
|
||
|
{
|
||
|
<a href="qpainter.html">QPainter</a> p(this);
|
||
|
p.<a href="qpainter.html#drawText">drawText</a>(rect(), AlignCenter, "Loading...");
|
||
|
}
|
||
|
|
||
|
void Graph::<a href="qwidget.html#paintEvent">paintEvent</a>(QPaintEvent* event)
|
||
|
{
|
||
|
if (!model.nCols()) {
|
||
|
paintWait(event);
|
||
|
} else {
|
||
|
switch (style) {
|
||
|
case Pie:
|
||
|
paintPie(event);
|
||
|
break;
|
||
|
case Bar:
|
||
|
paintBar(event);
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void <a name="f569"></a>Graph::setStyleFromMenu(int id)
|
||
|
{
|
||
|
setStyle(Style(id-100));
|
||
|
}
|
||
|
|
||
|
const char* Graph::styleName[] = { "Pie", "Bar", 0 };
|
||
|
|
||
|
|
||
|
//
|
||
|
// Grapher is a subclass of QNPInstance, and so it can be returned
|
||
|
// by GrapherPlugin::newInstance(). A QNPInstance represents the
|
||
|
// plugin, distinctly from the plugin window.
|
||
|
//
|
||
|
// Grapher is also a GraphModel, because it loads graph data from
|
||
|
// the net. When Grapher creates a window in newWindow(), it creates
|
||
|
// a Graph widget to display the GraphModel that is the Grapher itself.
|
||
|
//
|
||
|
|
||
|
class Grapher : public <a href="qnpinstance.html">QNPInstance</a>, GraphModel {
|
||
|
Q_OBJECT
|
||
|
public:
|
||
|
// Create a Grapher - all Grapher plugins are created
|
||
|
// by one GrapherPlugin object.
|
||
|
//
|
||
|
Grapher();
|
||
|
~Grapher();
|
||
|
|
||
|
// We override this QNPInstance function to create our
|
||
|
// own subclass of QNPWidget, a Graph widget.
|
||
|
//
|
||
|
<a href="qnpwidget.html">QNPWidget</a>* newWindow();
|
||
|
|
||
|
// We override this QNPInstance function to process the
|
||
|
// incoming graph data.
|
||
|
//
|
||
|
int write(QNPStream* /*str*/, int /*offset*/, int len, void* buffer);
|
||
|
|
||
|
private:
|
||
|
// Grapher is a GraphModel, so it implements the pure virtual
|
||
|
// functions of that class.
|
||
|
//
|
||
|
<a href="qptrlist.html">QPtrList</a><Datum>& graphData();
|
||
|
ColType colType(int col) const;
|
||
|
int nCols() const;
|
||
|
|
||
|
void consumeLine();
|
||
|
<a href="qptrlist.html">QPtrList</a><Datum> data;
|
||
|
<a href="qbuffer.html">QBuffer</a> line;
|
||
|
int ncols;
|
||
|
ColType *coltype;
|
||
|
|
||
|
private slots:
|
||
|
// Slots that are connected to the Graph menu items.
|
||
|
//
|
||
|
void aboutPlugin();
|
||
|
void aboutData();
|
||
|
};
|
||
|
|
||
|
<a name="f570"></a>Grapher::Grapher()
|
||
|
{
|
||
|
<a name="x2769"></a> data.<a href="qptrcollection.html#setAutoDelete">setAutoDelete</a>(TRUE);
|
||
|
ncols = 0;
|
||
|
<a name="x2743"></a> line.<a href="qiodevice.html#open">open</a>(IO_WriteOnly|IO_Truncate);
|
||
|
}
|
||
|
|
||
|
Grapher::~Grapher()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
QPtrList<GraphModel::Datum>& <a name="f571"></a>Grapher::graphData()
|
||
|
{
|
||
|
return data;
|
||
|
}
|
||
|
|
||
|
GraphModel::ColType <a name="f572"></a>Grapher::colType(int col) const
|
||
|
{
|
||
|
return coltype[col];
|
||
|
}
|
||
|
|
||
|
int <a name="f573"></a>Grapher::nCols() const
|
||
|
{
|
||
|
return ncols;
|
||
|
}
|
||
|
|
||
|
|
||
|
<a name="x2751"></a>QNPWidget* Grapher::<a href="qnpinstance.html#newWindow">newWindow</a>()
|
||
|
{
|
||
|
// Create a Graph - our subclass of QNPWidget.
|
||
|
Graph *graph = new Graph(*this);
|
||
|
|
||
|
// Look at the arguments from the EMBED tag.
|
||
|
// GRAPHSTYLE chooses pie or bar
|
||
|
// FONTFAMILY and FONTSIZE choose the font
|
||
|
//
|
||
|
const char* style = <a href="qnpinstance.html#arg">arg</a>("GRAPHSTYLE");
|
||
|
if ( style ) graph-><a href="qwidget.html#setStyle">setStyle</a>(style);
|
||
|
|
||
|
const char* fontfamily = <a href="qnpinstance.html#arg">arg</a>("FONTFAMILY");
|
||
|
const char* fontsize = <a href="qnpinstance.html#arg">arg</a>("FONTSIZE");
|
||
|
<a name="x2775"></a> int ptsize = fontsize ? atoi(fontsize) : graph-><a href="qwidget.html#font">font</a>().pointSize();
|
||
|
if (fontfamily) graph-><a href="qwidget.html#setFont">setFont</a>(QFont(fontfamily, ptsize));
|
||
|
|
||
|
<a href="qobject.html#connect">connect</a>(graph, SIGNAL(aboutPlugin()), this, SLOT(aboutPlugin()));
|
||
|
<a href="qobject.html#connect">connect</a>(graph, SIGNAL(aboutData()), this, SLOT(aboutData()));
|
||
|
|
||
|
return graph;
|
||
|
}
|
||
|
|
||
|
void <a name="f574"></a>Grapher::consumeLine()
|
||
|
{
|
||
|
<a name="x2742"></a> line.<a href="qiodevice.html#close">close</a>();
|
||
|
line.<a href="qiodevice.html#open">open</a>(IO_ReadOnly);
|
||
|
|
||
|
<a href="qtextstream.html">QTextStream</a> ts( &line );
|
||
|
|
||
|
if (ncols == 0 ) {
|
||
|
ncols=0;
|
||
|
<a href="qptrlist.html">QPtrList</a><ColType> typelist;
|
||
|
typelist.<a href="qptrcollection.html#setAutoDelete">setAutoDelete</a>(TRUE);
|
||
|
do {
|
||
|
<a href="qstring.html">QString</a> typestr;
|
||
|
ts >> typestr >> ws;
|
||
|
ColType* t = 0;
|
||
|
if ( typestr == "num" ) {
|
||
|
t = new ColType(Numeric);
|
||
|
} else if ( typestr == "label" ) {
|
||
|
t = new ColType(Label);
|
||
|
}
|
||
|
<a name="x2770"></a> if (t) typelist.<a href="qptrlist.html#append">append</a>(t);
|
||
|
<a name="x2774"></a> } while (!ts.<a href="qtextstream.html#atEnd">atEnd</a>());
|
||
|
coltype = new ColType[ncols];
|
||
|
for (ColType* t = typelist.<a href="qptrlist.html#first">first</a>(); t; t = typelist.<a href="qptrlist.html#next">next</a>()) {
|
||
|
coltype[ncols++] = *t;
|
||
|
}
|
||
|
} else {
|
||
|
int col=0;
|
||
|
Datum *rowdata = new Datum[ncols];
|
||
|
while ( col < ncols && !ts.<a href="qtextstream.html#atEnd">atEnd</a>() ) {
|
||
|
switch (coltype[col]) {
|
||
|
case Numeric: {
|
||
|
double value;
|
||
|
ts >> value >> ws;
|
||
|
rowdata[col].dbl = value;
|
||
|
break;
|
||
|
}
|
||
|
case Label: {
|
||
|
<a href="qstring.html">QString</a>* value = new <a href="qstring.html">QString</a>;
|
||
|
ts >> *value >> ws;
|
||
|
rowdata[col].str = value;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
col++;
|
||
|
}
|
||
|
|
||
|
data.<a href="qptrlist.html#append">append</a>(rowdata);
|
||
|
}
|
||
|
|
||
|
line.<a href="qiodevice.html#close">close</a>();
|
||
|
line.<a href="qiodevice.html#open">open</a>(IO_WriteOnly|IO_Truncate);
|
||
|
}
|
||
|
|
||
|
<a name="x2752"></a>int Grapher::<a href="qnpinstance.html#write">write</a>(QNPStream* /*str*/, int /*offset*/, int len, void* buffer)
|
||
|
{
|
||
|
// The browser calls this function when data is available on one
|
||
|
// of the streams the plugin has requested. Since we are only
|
||
|
// processing one stream - the URL in the SRC argument of the EMBED
|
||
|
// tag, we assume the QNPStream is that one. Also, since we do not
|
||
|
// override QNPInstance::writeReady(), we must accepts ALL the data
|
||
|
// that is sent to this function.
|
||
|
//
|
||
|
char* txt = (char*)buffer;
|
||
|
for (int i=0; i<len; i++) {
|
||
|
char ch = txt[i];
|
||
|
switch ( ch ) {
|
||
|
case '\n':
|
||
|
consumeLine();
|
||
|
break;
|
||
|
case '\r': // ignore;
|
||
|
break;
|
||
|
default:
|
||
|
<a name="x2744"></a> line.<a href="qiodevice.html#putch">putch</a>(ch);
|
||
|
}
|
||
|
}
|
||
|
if ( <a href="qnpinstance.html#widget">widget</a>() )
|
||
|
<a href="qnpinstance.html#widget">widget</a>()->update();
|
||
|
|
||
|
return len;
|
||
|
}
|
||
|
|
||
|
void <a name="f575"></a>Grapher::aboutPlugin()
|
||
|
{
|
||
|
<a href="qnpinstance.html#getURL">getURL</a>( "http://doc.trolltech.com/netscape-plugin.html", "_blank" );
|
||
|
}
|
||
|
|
||
|
void <a name="f576"></a>Grapher::aboutData()
|
||
|
{
|
||
|
const char* page = <a href="qnpinstance.html#arg">arg</a>("DATAPAGE");
|
||
|
if (page)
|
||
|
<a href="qnpinstance.html#getURL">getURL</a>( page, "_blank" );
|
||
|
else
|
||
|
<a name="x2750"></a> QMessageBox::<a href="qmessagebox.html#message">message</a>("Help", "No help for this data");
|
||
|
}
|
||
|
|
||
|
|
||
|
//
|
||
|
// GrapherPlugin is the start of everything. It is a QNPlugin subclass,
|
||
|
// and it is responsible for describing the plugin to the browser, and
|
||
|
// creating instances of the plugin when it appears in web page.
|
||
|
//
|
||
|
|
||
|
class GrapherPlugin : public <a href="qnplugin.html">QNPlugin</a> {
|
||
|
public:
|
||
|
GrapherPlugin()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
<a href="qnpinstance.html">QNPInstance</a>* newInstance()
|
||
|
{
|
||
|
// Make a new Grapher, our subclass of QNPInstance.
|
||
|
return new Grapher;
|
||
|
}
|
||
|
|
||
|
const char* getMIMEDescription() const
|
||
|
{
|
||
|
// Describe the MIME types which this plugin can
|
||
|
// process. Just the concocted "application/x-graphable"
|
||
|
// type, with the "g1n" filename extension.
|
||
|
//
|
||
|
return "application/x-graphable:g1n:Graphable ASCII numeric data";
|
||
|
}
|
||
|
|
||
|
const char * getPluginNameString() const
|
||
|
{
|
||
|
// The name of the plugin. This is the title string used in
|
||
|
// the "About Plugins" page of the browser.
|
||
|
//
|
||
|
return "Qt-based Graph Plugin";
|
||
|
}
|
||
|
|
||
|
const char * getPluginDescriptionString() const
|
||
|
{
|
||
|
// A longer description of the plugin.
|
||
|
//
|
||
|
return "A Qt-based LiveConnected plug-in that graphs numeric data";
|
||
|
}
|
||
|
|
||
|
};
|
||
|
|
||
|
//
|
||
|
// Finally, we provide the implementation of QNPlugin::create(), to
|
||
|
// provide our subclass of QNPlugin.
|
||
|
//
|
||
|
|
||
|
QNPlugin* QNPlugin::create()
|
||
|
{
|
||
|
return new GrapherPlugin;
|
||
|
}
|
||
|
|
||
|
#include "grapher.moc"
|
||
|
</pre>
|
||
|
|
||
|
<p>See also <a href="nsplugin-examples.html">Netscape Plugin Examples</a>.
|
||
|
|
||
|
<!-- eof -->
|
||
|
<p><address><hr><div align=center>
|
||
|
<table width=100% cellspacing=0 border=0><tr>
|
||
|
<td>Copyright © 2007
|
||
|
<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
|
||
|
<td align=right><div align=right>Qt 3.3.8</div>
|
||
|
</table></div></address></body>
|
||
|
</html>
|