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.
qt3/doc/html/canvas-example.html

942 lines
38 KiB

<!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/examples/canvas/canvas.doc:1 -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Canvas Example</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&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>Canvas Example</h1>
<p>
This example shows a <a href="qcanvas.html">QCanvas</a> and some <a href="qcanvasitem.html">QCanvasItem</a>s in action.
You can do a lot more with QCanvas than we show here, but the
example provides a taste of what can be done.
<p> <hr>
<p> Header file:
<p> <pre>#ifndef EXAMPLE_H
#define EXAMPLE_H
#include &lt;<a href="qpopupmenu-h.html">qpopupmenu.h</a>&gt;
#include &lt;<a href="qmainwindow-h.html">qmainwindow.h</a>&gt;
#include &lt;<a href="qintdict-h.html">qintdict.h</a>&gt;
#include &lt;<a href="qcanvas-h.html">qcanvas.h</a>&gt;
class BouncyLogo : public <a href="qcanvassprite.html">QCanvasSprite</a> {
void initPos();
void initSpeed();
public:
BouncyLogo(QCanvas*);
void advance(int);
int rtti() const;
};
class FigureEditor : public <a href="qcanvasview.html">QCanvasView</a> {
<a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>
public:
FigureEditor(QCanvas&amp;, QWidget* parent=0, const char* name=0, WFlags f=0);
void clear();
protected:
void contentsMousePressEvent(QMouseEvent*);
void contentsMouseMoveEvent(QMouseEvent*);
signals:
void status(const <a href="qstring.html">QString</a>&amp;);
private:
<a href="qcanvasitem.html">QCanvasItem</a>* moving;
<a href="qpoint.html">QPoint</a> moving_start;
};
class Main : public <a href="qmainwindow.html">QMainWindow</a> {
Q_OBJECT
public:
Main(QCanvas&amp;, QWidget* parent=0, const char* name=0, WFlags f=0);
~Main();
public slots:
void help();
private slots:
void aboutQt();
void newView();
void clear();
void init();
void addSprite();
void addCircle();
void addHexagon();
void addPolygon();
void addSpline();
void addText();
void addLine();
void addRectangle();
void addMesh();
void addLogo();
void addButterfly();
void enlarge();
void shrink();
void rotateClockwise();
void rotateCounterClockwise();
void zoomIn();
void zoomOut();
void mirror();
void moveL();
void moveR();
void moveU();
void moveD();
void print();
void toggleDoubleBuffer();
private:
<a href="qcanvas.html">QCanvas</a>&amp; canvas;
FigureEditor *editor;
<a href="qpopupmenu.html">QPopupMenu</a>* options;
<a href="qprinter.html">QPrinter</a>* printer;
int dbf_id;
};
#endif
</pre>
<p> <hr>
<p> Implementation:
<p> <pre>#include &lt;<a href="qdatetime-h.html">qdatetime.h</a>&gt;
#include &lt;<a href="qmainwindow-h.html">qmainwindow.h</a>&gt;
#include &lt;<a href="qstatusbar-h.html">qstatusbar.h</a>&gt;
#include &lt;<a href="qmessagebox-h.html">qmessagebox.h</a>&gt;
#include &lt;<a href="qmenubar-h.html">qmenubar.h</a>&gt;
#include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;
#include &lt;<a href="qpainter-h.html">qpainter.h</a>&gt;
#include &lt;<a href="qprinter-h.html">qprinter.h</a>&gt;
#include &lt;<a href="qlabel-h.html">qlabel.h</a>&gt;
#include &lt;<a href="qimage-h.html">qimage.h</a>&gt;
#include &lt;<a href="qprogressdialog-h.html">qprogressdialog.h</a>&gt;
#include "canvas.h"
#include &lt;stdlib.h&gt;
// We use a global variable to save memory - all the brushes and pens in
// the mesh are shared.
static QBrush *tb = 0;
static QPen *tp = 0;
class EdgeItem;
class NodeItem;
class EdgeItem: public <a href="qcanvasline.html">QCanvasLine</a>
{
public:
EdgeItem( NodeItem*, NodeItem*, QCanvas *canvas );
void setFromPoint( int x, int y ) ;
void setToPoint( int x, int y );
static int count() { return c; }
void moveBy(double dx, double dy);
private:
static int c;
};
static const int imageRTTI = 984376;
class ImageItem: public <a href="qcanvasrectangle.html">QCanvasRectangle</a>
{
public:
ImageItem( <a href="qimage.html">QImage</a> img, QCanvas *canvas );
int rtti () const { return imageRTTI; }
bool hit( const <a href="qpoint.html">QPoint</a>&amp;) const;
protected:
void drawShape( <a href="qpainter.html">QPainter</a> &amp; );
private:
<a href="qimage.html">QImage</a> image;
<a href="qpixmap.html">QPixmap</a> pixmap;
};
<a name="f623"></a>ImageItem::ImageItem( <a href="qimage.html">QImage</a> img, QCanvas *canvas )
: <a href="qcanvasrectangle.html">QCanvasRectangle</a>( canvas ), image(img)
{
<a name="x2935"></a><a name="x2932"></a> <a href="qcanvasrectangle.html#setSize">setSize</a>( image.<a href="qimage.html#width">width</a>(), image.<a href="qimage.html#height">height</a>() );
#if !defined(Q_WS_QWS)
<a name="x2943"></a> pixmap.<a href="qpixmap.html#convertFromImage">convertFromImage</a>(image, OrderedAlphaDither);
#endif
}
<a name="x2925"></a>void ImageItem::<a href="qcanvasrectangle.html#drawShape">drawShape</a>( <a href="qpainter.html">QPainter</a> &amp;p )
{
// On Qt/Embedded, we can paint a QImage as fast as a QPixmap,
// but on other platforms, we need to use a QPixmap.
#if defined(Q_WS_QWS)
<a name="x2941"></a> p.<a href="qpainter.html#drawImage">drawImage</a>( int(<a href="qcanvasitem.html#x">x</a>()), int(<a href="qcanvasitem.html#y">y</a>()), image, 0, 0, -1, -1, OrderedAlphaDither );
#else
p.<a href="qpainter.html#drawPixmap">drawPixmap</a>( int(<a href="qcanvasitem.html#x">x</a>()), int(<a href="qcanvasitem.html#y">y</a>()), pixmap );
#endif
}
bool <a name="f624"></a>ImageItem::hit( const <a href="qpoint.html">QPoint</a> &amp;p ) const
{
<a name="x2944"></a> int ix = p.<a href="qpoint.html#x">x</a>()-int(<a href="qcanvasitem.html#x">x</a>());
<a name="x2945"></a> int iy = p.<a href="qpoint.html#y">y</a>()-int(<a href="qcanvasitem.html#y">y</a>());
<a name="x2934"></a> if ( !image.<a href="qimage.html#valid">valid</a>( ix , iy ) )
return FALSE;
<a name="x2933"></a> QRgb pixel = image.<a href="qimage.html#pixel">pixel</a>( ix, iy );
return qAlpha( pixel ) != 0;
}
class NodeItem: public <a href="qcanvasellipse.html">QCanvasEllipse</a>
{
public:
NodeItem( <a href="qcanvas.html">QCanvas</a> *canvas );
~NodeItem() {}
void addInEdge( EdgeItem *edge ) { inList.<a href="qptrlist.html#append">append</a>( edge ); }
void addOutEdge( EdgeItem *edge ) { outList.<a href="qptrlist.html#append">append</a>( edge ); }
void moveBy(double dx, double dy);
// QPoint center() { return boundingRect().center(); }
private:
<a href="qptrlist.html">QPtrList</a>&lt;EdgeItem&gt; inList;
<a href="qptrlist.html">QPtrList</a>&lt;EdgeItem&gt; outList;
};
int EdgeItem::c = 0;
<a name="x2916"></a>void EdgeItem::<a href="qcanvasitem.html#moveBy">moveBy</a>(double, double)
{
//nothing
}
<a name="f618"></a>EdgeItem::EdgeItem( NodeItem *from, NodeItem *to, QCanvas *canvas )
: <a href="qcanvasline.html">QCanvasLine</a>( canvas )
{
c++;
setPen( *tp );
setBrush( *tb );
from-&gt;addOutEdge( this );
to-&gt;addInEdge( this );
<a name="x2921"></a><a name="x2920"></a> setPoints( int(from-&gt;<a href="qcanvasitem.html#x">x</a>()), int(from-&gt;<a href="qcanvasitem.html#y">y</a>()), int(to-&gt;x()), int(to-&gt;y()) );
setZ( 127 );
}
void <a name="f619"></a>EdgeItem::setFromPoint( int x, int y )
{
setPoints( x,y, endPoint().x(), endPoint().y() );
}
void <a name="f620"></a>EdgeItem::setToPoint( int x, int y )
{
setPoints( startPoint().x(), startPoint().y(), x, y );
}
void NodeItem::<a href="qcanvasitem.html#moveBy">moveBy</a>(double dx, double dy)
{
QCanvasEllipse::<a href="qcanvasitem.html#moveBy">moveBy</a>( dx, dy );
<a href="qptrlistiterator.html">QPtrListIterator</a>&lt;EdgeItem&gt; it1( inList );
EdgeItem *edge;
<a name="x2949"></a> while (( edge = it1.<a href="qptrlistiterator.html#current">current</a>() )) {
++it1;
edge-&gt;setToPoint( int(<a href="qwidget.html#x">x</a>()), int(<a href="qcanvasitem.html#y">y</a>()) );
}
<a href="qptrlistiterator.html">QPtrListIterator</a>&lt;EdgeItem&gt; it2( outList );
while (( edge = it2.<a href="qptrlistiterator.html#current">current</a>() )) {
++it2;
edge-&gt;setFromPoint( int(<a href="qcanvasitem.html#x">x</a>()), int(<a href="qcanvasitem.html#y">y</a>()) );
}
}
<a name="f655"></a>NodeItem::NodeItem( <a href="qcanvas.html">QCanvas</a> *canvas )
: <a href="qcanvasellipse.html">QCanvasEllipse</a>( 6, 6, canvas )
{
setPen( *tp );
setBrush( *tb );
<a href="qcanvasitem.html#setZ">setZ</a>( 128 );
}
<a name="f621"></a>FigureEditor::FigureEditor(
<a href="qcanvas.html">QCanvas</a>&amp; c, QWidget* parent,
const char* name, WFlags f) :
<a href="qcanvasview.html">QCanvasView</a>(&amp;c,parent,name,f)
{
}
<a name="x2951"></a>void FigureEditor::<a href="qscrollview.html#contentsMousePressEvent">contentsMousePressEvent</a>(QMouseEvent* e)
{
<a href="qpoint.html">QPoint</a> p = inverseWorldMatrix().map(e-&gt;pos());
<a href="qcanvasitemlist.html">QCanvasItemList</a> l=<a href="qcanvasitem.html#canvas">canvas</a>()-&gt;collisions(p);
<a name="x2953"></a><a name="x2952"></a> for (QCanvasItemList::Iterator it=l.<a href="qvaluelist.html#begin">begin</a>(); it!=l.<a href="qvaluelist.html#end">end</a>(); ++it) {
if ( (*it)-&gt;rtti() == imageRTTI ) {
ImageItem *item= (ImageItem*)(*it);
if ( !item-&gt;hit( p ) )
continue;
}
moving = *it;
moving_start = p;
return;
}
moving = 0;
}
void <a name="f622"></a>FigureEditor::clear()
{
<a href="qcanvasitemlist.html">QCanvasItemList</a> list = <a href="qcanvasitem.html#canvas">canvas</a>()-&gt;allItems();
QCanvasItemList::Iterator it = list.<a href="qvaluelist.html#begin">begin</a>();
for (; it != list.<a href="qvaluelist.html#end">end</a>(); ++it) {
if ( *it )
delete *it;
}
}
<a name="x2950"></a>void FigureEditor::<a href="qscrollview.html#contentsMouseMoveEvent">contentsMouseMoveEvent</a>(QMouseEvent* e)
{
if ( moving ) {
<a href="qpoint.html">QPoint</a> p = inverseWorldMatrix().map(e-&gt;pos());
moving-&gt;moveBy(p.<a href="qpoint.html#x">x</a>() - moving_start.x(),
p.<a href="qpoint.html#y">y</a>() - moving_start.y());
moving_start = p;
<a href="qcanvasitem.html#canvas">canvas</a>()-&gt;update();
}
}
<a name="f615"></a>BouncyLogo::BouncyLogo(QCanvas* canvas) :
<a href="qcanvassprite.html">QCanvasSprite</a>(0,canvas)
{
static QCanvasPixmapArray logo("qt-trans.xpm");
<a href="qcanvassprite.html#setSequence">setSequence</a>(&amp;logo);
<a href="qcanvasitem.html#setAnimated">setAnimated</a>(TRUE);
initPos();
}
const int logo_rtti = 1234;
<a name="x2928"></a>int BouncyLogo::<a href="qcanvassprite.html#rtti">rtti</a>() const
{
return logo_rtti;
}
void <a name="f616"></a>BouncyLogo::initPos()
{
initSpeed();
int trial=1000;
do {
<a href="qcanvassprite.html#move">move</a>(rand()%canvas()-&gt;width(),rand()%canvas()-&gt;height());
advance(0);
} while (trial-- &amp;&amp; xVelocity()==0.0 &amp;&amp; yVelocity()==0.0);
}
void <a name="f617"></a>BouncyLogo::initSpeed()
{
const double speed = 4.0;
double d = (double)(rand()%1024) / 1024.0;
setVelocity( d*speed*2-speed, (1-d)*speed*2-speed );
}
<a name="x2927"></a>void BouncyLogo::<a href="qcanvassprite.html#advance">advance</a>(int stage)
{
switch ( stage ) {
case 0: {
double vx = xVelocity();
double vy = yVelocity();
if ( vx == 0.0 &amp;&amp; vy == 0.0 ) {
// stopped last turn
initSpeed();
vx = xVelocity();
vy = yVelocity();
}
double nx = <a href="qwidget.html#x">x</a>() + vx;
double ny = <a href="qwidget.html#y">y</a>() + vy;
if ( nx &lt; 0 || nx &gt;= canvas()-&gt;width() )
vx = -vx;
if ( ny &lt; 0 || ny &gt;= canvas()-&gt;height() )
vy = -vy;
for (int bounce=0; bounce&lt;4; bounce++) {
<a href="qcanvasitemlist.html">QCanvasItemList</a> l=collisions(FALSE);
for (QCanvasItemList::Iterator it=l.<a href="qvaluelist.html#begin">begin</a>(); it!=l.<a href="qvaluelist.html#end">end</a>(); ++it) {
<a href="qcanvasitem.html">QCanvasItem</a> *hit = *it;
<a name="x2917"></a><a name="x2914"></a> if ( hit-&gt;<a href="qcanvasitem.html#rtti">rtti</a>()==logo_rtti &amp;&amp; hit-&gt;<a href="qcanvasitem.html#collidesWith">collidesWith</a>(this) ) {
switch ( bounce ) {
case 0:
vx = -vx;
break;
case 1:
vy = -vy;
vx = -vx;
break;
case 2:
vx = -vx;
break;
case 3:
// Stop for this turn
vx = 0;
vy = 0;
break;
}
setVelocity(vx,vy);
break;
}
}
}
if ( <a href="qwidget.html#x">x</a>()+vx &lt; 0 || x()+vx &gt;= canvas()-&gt;width() )
vx = 0;
if ( <a href="qwidget.html#y">y</a>()+vy &lt; 0 || y()+vy &gt;= canvas()-&gt;height() )
vy = 0;
setVelocity(vx,vy);
} break;
case 1:
<a name="x2913"></a> <a href="qcanvasitem.html">QCanvasItem</a>::<a href="qcanvasitem.html#advance">advance</a>(stage);
break;
}
}
static uint mainCount = 0;
static QImage *butterflyimg;
static QImage *logoimg;
<a name="f625"></a>Main::Main(QCanvas&amp; c, QWidget* parent, const char* name, WFlags f) :
<a href="qmainwindow.html">QMainWindow</a>(parent,name,f),
canvas(c)
{
editor = new FigureEditor(canvas,this);
<a href="qmenubar.html">QMenuBar</a>* menu = <a href="qmainwindow.html#menuBar">menuBar</a>();
<a href="qpopupmenu.html">QPopupMenu</a>* file = new <a href="qpopupmenu.html">QPopupMenu</a>( menu );
<a name="x2936"></a> file-&gt;<a href="qmenudata.html#insertItem">insertItem</a>("&amp;Fill canvas", this, SLOT(init()), CTRL+Key_F);
file-&gt;<a href="qmenudata.html#insertItem">insertItem</a>("&amp;Erase canvas", this, SLOT(clear()), CTRL+Key_E);
file-&gt;<a href="qmenudata.html#insertItem">insertItem</a>("&amp;New view", this, SLOT(newView()), CTRL+Key_N);
file-&gt;<a href="qmenudata.html#insertSeparator">insertSeparator</a>();
file-&gt;<a href="qmenudata.html#insertItem">insertItem</a>("&amp;Print...", this, SLOT(print()), CTRL+Key_P);
file-&gt;<a href="qmenudata.html#insertSeparator">insertSeparator</a>();
file-&gt;<a href="qmenudata.html#insertItem">insertItem</a>("E&amp;xit", qApp, SLOT(<a href="qapplication.html#quit">quit</a>()), CTRL+Key_Q);
menu-&gt;<a href="qmenudata.html#insertItem">insertItem</a>("&amp;File", file);
<a href="qpopupmenu.html">QPopupMenu</a>* edit = new <a href="qpopupmenu.html">QPopupMenu</a>( menu );
edit-&gt;<a href="qmenudata.html#insertItem">insertItem</a>("Add &amp;Circle", this, SLOT(addCircle()), ALT+Key_C);
edit-&gt;<a href="qmenudata.html#insertItem">insertItem</a>("Add &amp;Hexagon", this, SLOT(addHexagon()), ALT+Key_H);
edit-&gt;<a href="qmenudata.html#insertItem">insertItem</a>("Add &amp;Polygon", this, SLOT(addPolygon()), ALT+Key_P);
edit-&gt;<a href="qmenudata.html#insertItem">insertItem</a>("Add Spl&amp;ine", this, SLOT(addSpline()), ALT+Key_I);
edit-&gt;<a href="qmenudata.html#insertItem">insertItem</a>("Add &amp;Text", this, SLOT(addText()), ALT+Key_T);
edit-&gt;<a href="qmenudata.html#insertItem">insertItem</a>("Add &amp;Line", this, SLOT(addLine()), ALT+Key_L);
edit-&gt;<a href="qmenudata.html#insertItem">insertItem</a>("Add &amp;Rectangle", this, SLOT(addRectangle()), ALT+Key_R);
edit-&gt;<a href="qmenudata.html#insertItem">insertItem</a>("Add &amp;Sprite", this, SLOT(addSprite()), ALT+Key_S);
edit-&gt;<a href="qmenudata.html#insertItem">insertItem</a>("Create &amp;Mesh", this, SLOT(addMesh()), ALT+Key_M );
edit-&gt;<a href="qmenudata.html#insertItem">insertItem</a>("Add &amp;Alpha-blended image", this, SLOT(addButterfly()), ALT+Key_A);
menu-&gt;<a href="qmenudata.html#insertItem">insertItem</a>("&amp;Edit", edit);
<a href="qpopupmenu.html">QPopupMenu</a>* view = new <a href="qpopupmenu.html">QPopupMenu</a>( menu );
view-&gt;<a href="qmenudata.html#insertItem">insertItem</a>("&amp;Enlarge", this, SLOT(enlarge()), SHIFT+CTRL+Key_Plus);
view-&gt;<a href="qmenudata.html#insertItem">insertItem</a>("Shr&amp;ink", this, SLOT(shrink()), SHIFT+CTRL+Key_Minus);
view-&gt;<a href="qmenudata.html#insertSeparator">insertSeparator</a>();
view-&gt;<a href="qmenudata.html#insertItem">insertItem</a>("&amp;Rotate clockwise", this, SLOT(rotateClockwise()), CTRL+Key_PageDown);
view-&gt;<a href="qmenudata.html#insertItem">insertItem</a>("Rotate &amp;counterclockwise", this, SLOT(rotateCounterClockwise()), CTRL+Key_PageUp);
view-&gt;<a href="qmenudata.html#insertItem">insertItem</a>("&amp;Zoom in", this, SLOT(zoomIn()), CTRL+Key_Plus);
view-&gt;<a href="qmenudata.html#insertItem">insertItem</a>("Zoom &amp;out", this, SLOT(zoomOut()), CTRL+Key_Minus);
view-&gt;<a href="qmenudata.html#insertItem">insertItem</a>("Translate left", this, SLOT(moveL()), CTRL+Key_Left);
view-&gt;<a href="qmenudata.html#insertItem">insertItem</a>("Translate right", this, SLOT(moveR()), CTRL+Key_Right);
view-&gt;<a href="qmenudata.html#insertItem">insertItem</a>("Translate up", this, SLOT(moveU()), CTRL+Key_Up);
view-&gt;<a href="qmenudata.html#insertItem">insertItem</a>("Translate down", this, SLOT(moveD()), CTRL+Key_Down);
view-&gt;<a href="qmenudata.html#insertItem">insertItem</a>("&amp;Mirror", this, SLOT(mirror()), CTRL+Key_Home);
menu-&gt;<a href="qmenudata.html#insertItem">insertItem</a>("&amp;View", view);
options = new <a href="qpopupmenu.html">QPopupMenu</a>( menu );
dbf_id = options-&gt;<a href="qmenudata.html#insertItem">insertItem</a>("Double buffer", this, SLOT(toggleDoubleBuffer()));
<a name="x2939"></a> options-&gt;<a href="qmenudata.html#setItemChecked">setItemChecked</a>(dbf_id, TRUE);
menu-&gt;<a href="qmenudata.html#insertItem">insertItem</a>("&amp;Options",options);
menu-&gt;<a href="qmenudata.html#insertSeparator">insertSeparator</a>();
<a href="qpopupmenu.html">QPopupMenu</a>* help = new <a href="qpopupmenu.html">QPopupMenu</a>( menu );
help-&gt;<a href="qmenudata.html#insertItem">insertItem</a>("&amp;About", this, SLOT(help()), Key_F1);
help-&gt;<a href="qmenudata.html#setItemChecked">setItemChecked</a>(dbf_id, TRUE);
menu-&gt;<a href="qmenudata.html#insertItem">insertItem</a>("&amp;Help",help);
<a href="qmainwindow.html#statusBar">statusBar</a>();
<a href="qmainwindow.html#setCentralWidget">setCentralWidget</a>(editor);
printer = 0;
init();
}
void <a name="f626"></a>Main::init()
{
clear();
static int r=24;
srand(++r);
mainCount++;
butterflyimg = 0;
logoimg = 0;
int i;
<a name="x2912"></a> for ( i=0; i&lt;canvas.<a href="qcanvas.html#width">width</a>() / 56; i++) {
addButterfly();
}
for ( i=0; i&lt;canvas.<a href="qcanvas.html#width">width</a>() / 85; i++) {
addHexagon();
}
for ( i=0; i&lt;canvas.<a href="qcanvas.html#width">width</a>() / 128; i++) {
addLogo();
}
}
Main::~Main()
{
delete printer;
if ( !--mainCount ) {
delete[] butterflyimg;
butterflyimg = 0;
delete[] logoimg;
logoimg = 0;
}
}
void <a name="f627"></a>Main::newView()
{
// Open a new view... have it delete when closed.
Main *m = new Main(canvas, 0, 0, WDestructiveClose);
qApp-&gt;<a href="qapplication.html#setMainWidget">setMainWidget</a>(m);
m-&gt;<a href="qwidget.html#show">show</a>();
qApp-&gt;<a href="qapplication.html#setMainWidget">setMainWidget</a>(0);
}
void <a name="f628"></a>Main::clear()
{
editor-&gt;clear();
}
void <a name="f629"></a>Main::help()
{
static QMessageBox* about = new <a href="qmessagebox.html">QMessageBox</a>( "Qt Canvas Example",
"&lt;h3&gt;The QCanvas classes example&lt;/h3&gt;"
"&lt;ul&gt;"
"&lt;li&gt; Press ALT-S for some sprites."
"&lt;li&gt; Press ALT-C for some circles."
"&lt;li&gt; Press ALT-L for some lines."
"&lt;li&gt; Drag the objects around."
"&lt;li&gt; Read the code!"
"&lt;/ul&gt;", QMessageBox::Information, 1, 0, 0, this, 0, FALSE );
about-&gt;setButtonText( 1, "Dismiss" );
about-&gt;show();
}
void <a name="f630"></a>Main::aboutQt()
{
QMessageBox::<a href="qmessagebox.html#aboutQt">aboutQt</a>( this, "Qt Canvas Example" );
}
void <a name="f631"></a>Main::toggleDoubleBuffer()
{
<a name="x2938"></a> bool s = !options-&gt;<a href="qmenudata.html#isItemChecked">isItemChecked</a>(dbf_id);
options-&gt;<a href="qmenudata.html#setItemChecked">setItemChecked</a>(dbf_id,s);
<a name="x2911"></a> canvas.<a href="qcanvas.html#setDoubleBuffering">setDoubleBuffering</a>(s);
}
void <a name="f632"></a>Main::enlarge()
{
<a name="x2910"></a><a name="x2909"></a> canvas.<a href="qcanvas.html#resize">resize</a>(canvas.<a href="qcanvas.html#width">width</a>()*4/3, canvas.<a href="qcanvas.html#height">height</a>()*4/3);
}
void <a name="f633"></a>Main::shrink()
{
canvas.<a href="qcanvas.html#resize">resize</a>(canvas.<a href="qcanvas.html#width">width</a>()*3/4, canvas.<a href="qcanvas.html#height">height</a>()*3/4);
}
void <a name="f634"></a>Main::rotateClockwise()
{
<a name="x2931"></a> <a href="qwmatrix.html">QWMatrix</a> m = editor-&gt;<a href="qcanvasview.html#worldMatrix">worldMatrix</a>();
<a name="x2955"></a> m.<a href="qwmatrix.html#rotate">rotate</a>( 22.5 );
<a name="x2930"></a> editor-&gt;<a href="qcanvasview.html#setWorldMatrix">setWorldMatrix</a>( m );
}
void <a name="f635"></a>Main::rotateCounterClockwise()
{
<a href="qwmatrix.html">QWMatrix</a> m = editor-&gt;<a href="qcanvasview.html#worldMatrix">worldMatrix</a>();
m.<a href="qwmatrix.html#rotate">rotate</a>( -22.5 );
editor-&gt;<a href="qcanvasview.html#setWorldMatrix">setWorldMatrix</a>( m );
}
void <a name="f636"></a>Main::zoomIn()
{
<a href="qwmatrix.html">QWMatrix</a> m = editor-&gt;<a href="qcanvasview.html#worldMatrix">worldMatrix</a>();
<a name="x2956"></a> m.<a href="qwmatrix.html#scale">scale</a>( 2.0, 2.0 );
editor-&gt;<a href="qcanvasview.html#setWorldMatrix">setWorldMatrix</a>( m );
}
void <a name="f637"></a>Main::zoomOut()
{
<a href="qwmatrix.html">QWMatrix</a> m = editor-&gt;<a href="qcanvasview.html#worldMatrix">worldMatrix</a>();
m.<a href="qwmatrix.html#scale">scale</a>( 0.5, 0.5 );
editor-&gt;<a href="qcanvasview.html#setWorldMatrix">setWorldMatrix</a>( m );
}
void <a name="f638"></a>Main::mirror()
{
<a href="qwmatrix.html">QWMatrix</a> m = editor-&gt;<a href="qcanvasview.html#worldMatrix">worldMatrix</a>();
m.<a href="qwmatrix.html#scale">scale</a>( -1, 1 );
editor-&gt;<a href="qcanvasview.html#setWorldMatrix">setWorldMatrix</a>( m );
}
void <a name="f639"></a>Main::moveL()
{
<a href="qwmatrix.html">QWMatrix</a> m = editor-&gt;<a href="qcanvasview.html#worldMatrix">worldMatrix</a>();
<a name="x2957"></a> m.<a href="qwmatrix.html#translate">translate</a>( -16, 0 );
editor-&gt;<a href="qcanvasview.html#setWorldMatrix">setWorldMatrix</a>( m );
}
void <a name="f640"></a>Main::moveR()
{
<a href="qwmatrix.html">QWMatrix</a> m = editor-&gt;<a href="qcanvasview.html#worldMatrix">worldMatrix</a>();
m.<a href="qwmatrix.html#translate">translate</a>( +16, 0 );
editor-&gt;<a href="qcanvasview.html#setWorldMatrix">setWorldMatrix</a>( m );
}
void <a name="f641"></a>Main::moveU()
{
<a href="qwmatrix.html">QWMatrix</a> m = editor-&gt;<a href="qcanvasview.html#worldMatrix">worldMatrix</a>();
m.<a href="qwmatrix.html#translate">translate</a>( 0, -16 );
editor-&gt;<a href="qcanvasview.html#setWorldMatrix">setWorldMatrix</a>( m );
}
void <a name="f642"></a>Main::moveD()
{
<a href="qwmatrix.html">QWMatrix</a> m = editor-&gt;<a href="qcanvasview.html#worldMatrix">worldMatrix</a>();
m.<a href="qwmatrix.html#translate">translate</a>( 0, +16 );
editor-&gt;<a href="qcanvasview.html#setWorldMatrix">setWorldMatrix</a>( m );
}
void <a name="f643"></a>Main::print()
{
if ( !printer ) printer = new <a href="qprinter.html">QPrinter</a>;
if ( printer-&gt;setup(this) ) {
<a href="qpainter.html">QPainter</a> pp(printer);
<a name="x2908"></a> canvas.<a href="qcanvas.html#drawArea">drawArea</a>(QRect(0,0,canvas.<a href="qcanvas.html#width">width</a>(),canvas.<a href="qcanvas.html#height">height</a>()),&amp;pp,FALSE);
}
}
void <a name="f644"></a>Main::addSprite()
{
<a href="qcanvasitem.html">QCanvasItem</a>* i = new BouncyLogo(&amp;canvas);
<a name="x2918"></a> i-&gt;<a href="qcanvasitem.html#setZ">setZ</a>(rand()%256);
<a name="x2919"></a> i-&gt;<a href="qcanvasitem.html#show">show</a>();
}
QString butterfly_fn;
QString logo_fn;
void <a name="f645"></a>Main::addButterfly()
{
if ( butterfly_fn.isEmpty() )
return;
if ( !butterflyimg ) {
butterflyimg = new <a href="qimage.html">QImage</a>[4];
butterflyimg[0].load( butterfly_fn );
butterflyimg[1] = butterflyimg[0].smoothScale( int(butterflyimg[0].width()*0.75),
int(butterflyimg[0].height()*0.75) );
butterflyimg[2] = butterflyimg[0].smoothScale( int(butterflyimg[0].width()*0.5),
int(butterflyimg[0].height()*0.5) );
butterflyimg[3] = butterflyimg[0].smoothScale( int(butterflyimg[0].width()*0.25),
int(butterflyimg[0].height()*0.25) );
}
<a href="qcanvaspolygonalitem.html">QCanvasPolygonalItem</a>* i = new ImageItem(butterflyimg[rand()%4],&amp;canvas);
<a name="x2915"></a> i-&gt;<a href="qcanvasitem.html#move">move</a>(rand()%(canvas.<a href="qcanvas.html#width">width</a>()-butterflyimg-&gt;<a href="qimage.html#width">width</a>()),
rand()%(canvas.<a href="qcanvas.html#height">height</a>()-butterflyimg-&gt;<a href="qimage.html#height">height</a>()));
i-&gt;<a href="qcanvasitem.html#setZ">setZ</a>(rand()%256+250);
i-&gt;<a href="qcanvasitem.html#show">show</a>();
}
void <a name="f646"></a>Main::addLogo()
{
if ( logo_fn.isEmpty() )
return;
if ( !logoimg ) {
logoimg = new <a href="qimage.html">QImage</a>[4];
logoimg[0].load( logo_fn );
logoimg[1] = logoimg[0].smoothScale( int(logoimg[0].width()*0.75),
int(logoimg[0].height()*0.75) );
logoimg[2] = logoimg[0].smoothScale( int(logoimg[0].width()*0.5),
int(logoimg[0].height()*0.5) );
logoimg[3] = logoimg[0].smoothScale( int(logoimg[0].width()*0.25),
int(logoimg[0].height()*0.25) );
}
<a href="qcanvaspolygonalitem.html">QCanvasPolygonalItem</a>* i = new ImageItem(logoimg[rand()%4],&amp;canvas);
i-&gt;<a href="qcanvasitem.html#move">move</a>(rand()%(canvas.<a href="qcanvas.html#width">width</a>()-logoimg-&gt;<a href="qimage.html#width">width</a>()),
rand()%(canvas.<a href="qcanvas.html#height">height</a>()-logoimg-&gt;<a href="qimage.html#width">width</a>()));
i-&gt;<a href="qcanvasitem.html#setZ">setZ</a>(rand()%256+256);
i-&gt;<a href="qcanvasitem.html#show">show</a>();
}
void <a name="f647"></a>Main::addCircle()
{
<a href="qcanvaspolygonalitem.html">QCanvasPolygonalItem</a>* i = new <a href="qcanvasellipse.html">QCanvasEllipse</a>(50,50,&amp;canvas);
<a name="x2923"></a> i-&gt;<a href="qcanvaspolygonalitem.html#setBrush">setBrush</a>( QColor(rand()%32*8,rand()%32*8,rand()%32*8) );
i-&gt;<a href="qcanvasitem.html#move">move</a>(rand()%canvas.<a href="qcanvas.html#width">width</a>(),rand()%canvas.<a href="qcanvas.html#height">height</a>());
i-&gt;<a href="qcanvasitem.html#setZ">setZ</a>(rand()%256);
i-&gt;<a href="qcanvasitem.html#show">show</a>();
}
void <a name="f648"></a>Main::addHexagon()
{
<a href="qcanvaspolygon.html">QCanvasPolygon</a>* i = new <a href="qcanvaspolygon.html">QCanvasPolygon</a>(&amp;canvas);
const int size = canvas.<a href="qcanvas.html#width">width</a>() / 25;
<a href="qpointarray.html">QPointArray</a> pa(6);
pa[0] = QPoint(2*size,0);
pa[1] = QPoint(size,-size*173/100);
pa[2] = QPoint(-size,-size*173/100);
pa[3] = QPoint(-2*size,0);
pa[4] = QPoint(-size,size*173/100);
pa[5] = QPoint(size,size*173/100);
<a name="x2922"></a> i-&gt;<a href="qcanvasline.html#setPoints">setPoints</a>(pa);
i-&gt;<a href="qcanvaspolygonalitem.html#setBrush">setBrush</a>( QColor(rand()%32*8,rand()%32*8,rand()%32*8) );
i-&gt;<a href="qcanvasitem.html#move">move</a>(rand()%canvas.<a href="qcanvas.html#width">width</a>(),rand()%canvas.<a href="qcanvas.html#height">height</a>());
i-&gt;<a href="qcanvasitem.html#setZ">setZ</a>(rand()%256);
i-&gt;<a href="qcanvasitem.html#show">show</a>();
}
void <a name="f649"></a>Main::addPolygon()
{
<a href="qcanvaspolygon.html">QCanvasPolygon</a>* i = new <a href="qcanvaspolygon.html">QCanvasPolygon</a>(&amp;canvas);
const int size = canvas.<a href="qcanvas.html#width">width</a>()/2;
<a href="qpointarray.html">QPointArray</a> pa(6);
pa[0] = QPoint(0,0);
pa[1] = QPoint(size,size/5);
pa[2] = QPoint(size*4/5,size);
pa[3] = QPoint(size/6,size*5/4);
pa[4] = QPoint(size*3/4,size*3/4);
pa[5] = QPoint(size*3/4,size/4);
i-&gt;<a href="qcanvasline.html#setPoints">setPoints</a>(pa);
i-&gt;<a href="qcanvaspolygonalitem.html#setBrush">setBrush</a>( QColor(rand()%32*8,rand()%32*8,rand()%32*8) );
i-&gt;<a href="qcanvasitem.html#move">move</a>(rand()%canvas.<a href="qcanvas.html#width">width</a>(),rand()%canvas.<a href="qcanvas.html#height">height</a>());
i-&gt;<a href="qcanvasitem.html#setZ">setZ</a>(rand()%256);
i-&gt;<a href="qcanvasitem.html#show">show</a>();
}
void <a name="f650"></a>Main::addSpline()
{
<a href="qcanvasspline.html">QCanvasSpline</a>* i = new <a href="qcanvasspline.html">QCanvasSpline</a>(&amp;canvas);
const int size = canvas.<a href="qcanvas.html#width">width</a>()/6;
<a href="qpointarray.html">QPointArray</a> pa(12);
pa[0] = QPoint(0,0);
pa[1] = QPoint(size/2,0);
pa[2] = QPoint(size,size/2);
pa[3] = QPoint(size,size);
pa[4] = QPoint(size,size*3/2);
pa[5] = QPoint(size/2,size*2);
pa[6] = QPoint(0,size*2);
pa[7] = QPoint(-size/2,size*2);
pa[8] = QPoint(size/4,size*3/2);
pa[9] = QPoint(0,size);
pa[10]= QPoint(-size/4,size/2);
pa[11]= QPoint(-size/2,0);
<a name="x2926"></a> i-&gt;<a href="qcanvasspline.html#setControlPoints">setControlPoints</a>(pa);
i-&gt;<a href="qcanvaspolygonalitem.html#setBrush">setBrush</a>( QColor(rand()%32*8,rand()%32*8,rand()%32*8) );
i-&gt;<a href="qcanvasitem.html#move">move</a>(rand()%canvas.<a href="qcanvas.html#width">width</a>(),rand()%canvas.<a href="qcanvas.html#height">height</a>());
i-&gt;<a href="qcanvasitem.html#setZ">setZ</a>(rand()%256);
i-&gt;<a href="qcanvasitem.html#show">show</a>();
}
void <a name="f651"></a>Main::addText()
{
<a href="qcanvastext.html">QCanvasText</a>* i = new <a href="qcanvastext.html">QCanvasText</a>(&amp;canvas);
<a name="x2929"></a> i-&gt;<a href="qcanvastext.html#setText">setText</a>("QCanvasText");
i-&gt;<a href="qcanvasitem.html#move">move</a>(rand()%canvas.<a href="qcanvas.html#width">width</a>(),rand()%canvas.<a href="qcanvas.html#height">height</a>());
i-&gt;<a href="qcanvasitem.html#setZ">setZ</a>(rand()%256);
i-&gt;<a href="qcanvasitem.html#show">show</a>();
}
void <a name="f652"></a>Main::addLine()
{
<a href="qcanvasline.html">QCanvasLine</a>* i = new <a href="qcanvasline.html">QCanvasLine</a>(&amp;canvas);
i-&gt;<a href="qcanvasline.html#setPoints">setPoints</a>( rand()%canvas.<a href="qcanvas.html#width">width</a>(), rand()%canvas.<a href="qcanvas.html#height">height</a>(),
rand()%canvas.<a href="qcanvas.html#width">width</a>(), rand()%canvas.<a href="qcanvas.html#height">height</a>() );
<a name="x2924"></a> i-&gt;<a href="qcanvaspolygonalitem.html#setPen">setPen</a>( QPen(QColor(rand()%32*8,rand()%32*8,rand()%32*8), 6) );
i-&gt;<a href="qcanvasitem.html#setZ">setZ</a>(rand()%256);
i-&gt;<a href="qcanvasitem.html#show">show</a>();
}
void <a name="f653"></a>Main::addMesh()
{
int x0 = 0;
int y0 = 0;
if ( !tb ) tb = new <a href="qbrush.html">QBrush</a>( Qt::red );
if ( !tp ) tp = new <a href="qpen.html">QPen</a>( Qt::black );
int nodecount = 0;
int w = canvas.<a href="qcanvas.html#width">width</a>();
int h = canvas.<a href="qcanvas.html#height">height</a>();
const int dist = 30;
int rows = h / dist;
int cols = w / dist;
#ifndef QT_NO_PROGRESSDIALOG
<a href="qprogressdialog.html">QProgressDialog</a> progress( "Creating mesh...", "Abort", rows,
this, "progress", TRUE );
#endif
<a href="qmemarray.html">QMemArray</a>&lt;NodeItem*&gt; lastRow(cols);
for ( int j = 0; j &lt; rows; j++ ) {
int n = j%2 ? cols-1 : cols;
NodeItem *prev = 0;
for ( int i = 0; i &lt; n; i++ ) {
NodeItem *el = new NodeItem( &amp;canvas );
nodecount++;
int r = rand();
int xrand = r %20;
int yrand = (r/20) %20;
el-&gt;<a href="qcanvasitem.html#move">move</a>( xrand + x0 + i*dist + (j%2 ? dist/2 : 0 ),
yrand + y0 + j*dist );
if ( j &gt; 0 ) {
if ( i &lt; cols-1 )
(new EdgeItem( lastRow[i], el, &amp;canvas ))-&gt;show();
if ( j%2 )
(new EdgeItem( lastRow[i+1], el, &amp;canvas ))-&gt;show();
else if ( i &gt; 0 )
(new EdgeItem( lastRow[i-1], el, &amp;canvas ))-&gt;show();
}
if ( prev ) {
(new EdgeItem( prev, el, &amp;canvas ))-&gt;show();
}
if ( i &gt; 0 ) lastRow[i-1] = prev;
prev = el;
el-&gt;<a href="qcanvasitem.html#show">show</a>();
}
lastRow[n-1]=prev;
#ifndef QT_NO_PROGRESSDIALOG
<a name="x2946"></a> progress.<a href="qprogressdialog.html#setProgress">setProgress</a>( j );
<a name="x2947"></a> if ( progress.<a href="qprogressdialog.html#wasCancelled">wasCancelled</a>() )
break;
#endif
}
#ifndef QT_NO_PROGRESSDIALOG
progress.<a href="qprogressdialog.html#setProgress">setProgress</a>( rows );
#endif
// qDebug( "%d nodes, %d edges", nodecount, EdgeItem::count() );
}
void <a name="f654"></a>Main::addRectangle()
{
<a href="qcanvaspolygonalitem.html">QCanvasPolygonalItem</a> *i = new <a href="qcanvasrectangle.html">QCanvasRectangle</a>( rand()%canvas.<a href="qcanvas.html#width">width</a>(),rand()%canvas.<a href="qcanvas.html#height">height</a>(),
canvas.<a href="qcanvas.html#width">width</a>()/5,canvas.<a href="qcanvas.html#width">width</a>()/5,&amp;canvas);
int z = rand()%256;
i-&gt;<a href="qcanvaspolygonalitem.html#setBrush">setBrush</a>( QColor(z,z,z) );
i-&gt;<a href="qcanvaspolygonalitem.html#setPen">setPen</a>( QPen(QColor(rand()%32*8,rand()%32*8,rand()%32*8), 6) );
i-&gt;<a href="qcanvasitem.html#setZ">setZ</a>(z);
i-&gt;<a href="qcanvasitem.html#show">show</a>();
}
</pre>
<p> <hr>
<p> Main:
<p> <pre>#include &lt;<a href="qstatusbar-h.html">qstatusbar.h</a>&gt;
#include &lt;<a href="qmessagebox-h.html">qmessagebox.h</a>&gt;
#include &lt;<a href="qmenubar-h.html">qmenubar.h</a>&gt;
#include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;
#include &lt;<a href="qimage-h.html">qimage.h</a>&gt;
#include "canvas.h"
#include &lt;stdlib.h&gt;
extern QString butterfly_fn;
extern QString logo_fn;
int main(int argc, char** argv)
{
<a href="qapplication.html">QApplication</a> app(argc,argv);
if ( argc &gt; 1 )
butterfly_fn = argv[1];
else
butterfly_fn = "butterfly.png";
if ( argc &gt; 2 )
logo_fn = argv[2];
else
logo_fn = "qtlogo.png";
<a href="qcanvas.html">QCanvas</a> canvas(800,600);
<a name="x2962"></a> canvas.<a href="qcanvas.html#setAdvancePeriod">setAdvancePeriod</a>(30);
Main m(canvas);
<a name="x2969"></a> m.<a href="qwidget.html#resize">resize</a>(m.<a href="qwidget.html#sizeHint">sizeHint</a>());
m.<a href="qwidget.html#setCaption">setCaption</a>("Qt Example - Canvas");
<a name="x2970"></a><a name="x2958"></a> if ( QApplication::<a href="qapplication.html#desktop">desktop</a>()-&gt;width() &gt; m.<a href="qwidget.html#width">width</a>() + 10
<a name="x2964"></a> &amp;&amp; QApplication::<a href="qapplication.html#desktop">desktop</a>()-&gt;height() &gt; m.<a href="qwidget.html#height">height</a>() +30 )
m.<a href="qwidget.html#show">show</a>();
else
<a name="x2968"></a> m.<a href="qwidget.html#showMaximized">showMaximized</a>();
QObject::<a href="qobject.html#connect">connect</a>( qApp, SIGNAL(<a href="qapplication.html#lastWindowClosed">lastWindowClosed</a>()), qApp, SLOT(<a href="qapplication.html#quit">quit</a>()) );
return app.<a href="qapplication.html#exec">exec</a>();
}
</pre>
<p>See also <a href="examples.html">Examples</a>.
<!-- eof -->
<p><address><hr><div align=center>
<table width=100% cellspacing=0 border=0><tr>
<td>Copyright &copy; 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>