[190] | 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
|
---|
| 2 | <!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/examples/canvas/canvas.doc:1 -->
|
---|
| 3 | <html>
|
---|
| 4 | <head>
|
---|
| 5 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
---|
| 6 | <title>Canvas Example</title>
|
---|
| 7 | <style type="text/css"><!--
|
---|
| 8 | fn { margin-left: 1cm; text-indent: -1cm; }
|
---|
| 9 | a:link { color: #004faf; text-decoration: none }
|
---|
| 10 | a:visited { color: #672967; text-decoration: none }
|
---|
| 11 | body { background: #ffffff; color: black; }
|
---|
| 12 | --></style>
|
---|
| 13 | </head>
|
---|
| 14 | <body>
|
---|
| 15 |
|
---|
| 16 | <table border="0" cellpadding="0" cellspacing="0" width="100%">
|
---|
| 17 | <tr bgcolor="#E5E5E5">
|
---|
| 18 | <td valign=center>
|
---|
| 19 | <a href="index.html">
|
---|
| 20 | <font color="#004faf">Home</font></a>
|
---|
| 21 | | <a href="classes.html">
|
---|
| 22 | <font color="#004faf">All Classes</font></a>
|
---|
| 23 | | <a href="mainclasses.html">
|
---|
| 24 | <font color="#004faf">Main Classes</font></a>
|
---|
| 25 | | <a href="annotated.html">
|
---|
| 26 | <font color="#004faf">Annotated</font></a>
|
---|
| 27 | | <a href="groups.html">
|
---|
| 28 | <font color="#004faf">Grouped Classes</font></a>
|
---|
| 29 | | <a href="functions.html">
|
---|
| 30 | <font color="#004faf">Functions</font></a>
|
---|
| 31 | </td>
|
---|
| 32 | <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>
|
---|
| 33 |
|
---|
| 34 |
|
---|
| 35 | <p>
|
---|
| 36 | This example shows a <a href="qcanvas.html">QCanvas</a> and some <a href="qcanvasitem.html">QCanvasItem</a>s in action.
|
---|
| 37 | You can do a lot more with QCanvas than we show here, but the
|
---|
| 38 | example provides a taste of what can be done.
|
---|
| 39 | <p> <hr>
|
---|
| 40 | <p> Header file:
|
---|
| 41 | <p> <pre>#ifndef EXAMPLE_H
|
---|
| 42 | #define EXAMPLE_H
|
---|
| 43 |
|
---|
| 44 | #include <<a href="qpopupmenu-h.html">qpopupmenu.h</a>>
|
---|
| 45 | #include <<a href="qmainwindow-h.html">qmainwindow.h</a>>
|
---|
| 46 | #include <<a href="qintdict-h.html">qintdict.h</a>>
|
---|
| 47 | #include <<a href="qcanvas-h.html">qcanvas.h</a>>
|
---|
| 48 |
|
---|
| 49 | class BouncyLogo : public <a href="qcanvassprite.html">QCanvasSprite</a> {
|
---|
| 50 | void initPos();
|
---|
| 51 | void initSpeed();
|
---|
| 52 | public:
|
---|
| 53 | BouncyLogo(QCanvas*);
|
---|
| 54 | void advance(int);
|
---|
| 55 | int rtti() const;
|
---|
| 56 | };
|
---|
| 57 |
|
---|
| 58 |
|
---|
| 59 | class FigureEditor : public <a href="qcanvasview.html">QCanvasView</a> {
|
---|
| 60 | <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>
|
---|
| 61 |
|
---|
| 62 | public:
|
---|
| 63 | FigureEditor(QCanvas&, QWidget* parent=0, const char* name=0, WFlags f=0);
|
---|
| 64 | void clear();
|
---|
| 65 |
|
---|
| 66 | protected:
|
---|
| 67 | void contentsMousePressEvent(QMouseEvent*);
|
---|
| 68 | void contentsMouseMoveEvent(QMouseEvent*);
|
---|
| 69 |
|
---|
| 70 | signals:
|
---|
| 71 | void status(const <a href="qstring.html">QString</a>&);
|
---|
| 72 |
|
---|
| 73 | private:
|
---|
| 74 | <a href="qcanvasitem.html">QCanvasItem</a>* moving;
|
---|
| 75 | <a href="qpoint.html">QPoint</a> moving_start;
|
---|
| 76 | };
|
---|
| 77 |
|
---|
| 78 | class Main : public <a href="qmainwindow.html">QMainWindow</a> {
|
---|
| 79 | Q_OBJECT
|
---|
| 80 |
|
---|
| 81 | public:
|
---|
| 82 | Main(QCanvas&, QWidget* parent=0, const char* name=0, WFlags f=0);
|
---|
| 83 | ~Main();
|
---|
| 84 |
|
---|
| 85 | public slots:
|
---|
| 86 | void help();
|
---|
| 87 |
|
---|
| 88 | private slots:
|
---|
| 89 | void aboutQt();
|
---|
| 90 | void newView();
|
---|
| 91 | void clear();
|
---|
| 92 | void init();
|
---|
| 93 |
|
---|
| 94 | void addSprite();
|
---|
| 95 | void addCircle();
|
---|
| 96 | void addHexagon();
|
---|
| 97 | void addPolygon();
|
---|
| 98 | void addSpline();
|
---|
| 99 | void addText();
|
---|
| 100 | void addLine();
|
---|
| 101 | void addRectangle();
|
---|
| 102 | void addMesh();
|
---|
| 103 | void addLogo();
|
---|
| 104 | void addButterfly();
|
---|
| 105 |
|
---|
| 106 | void enlarge();
|
---|
| 107 | void shrink();
|
---|
| 108 | void rotateClockwise();
|
---|
| 109 | void rotateCounterClockwise();
|
---|
| 110 | void zoomIn();
|
---|
| 111 | void zoomOut();
|
---|
| 112 | void mirror();
|
---|
| 113 | void moveL();
|
---|
| 114 | void moveR();
|
---|
| 115 | void moveU();
|
---|
| 116 | void moveD();
|
---|
| 117 |
|
---|
| 118 | void print();
|
---|
| 119 |
|
---|
| 120 | void toggleDoubleBuffer();
|
---|
| 121 |
|
---|
| 122 | private:
|
---|
| 123 | <a href="qcanvas.html">QCanvas</a>& canvas;
|
---|
| 124 | FigureEditor *editor;
|
---|
| 125 |
|
---|
| 126 | <a href="qpopupmenu.html">QPopupMenu</a>* options;
|
---|
| 127 | <a href="qprinter.html">QPrinter</a>* printer;
|
---|
| 128 | int dbf_id;
|
---|
| 129 | };
|
---|
| 130 |
|
---|
| 131 | #endif
|
---|
| 132 | </pre>
|
---|
| 133 |
|
---|
| 134 | <p> <hr>
|
---|
| 135 | <p> Implementation:
|
---|
| 136 | <p> <pre>#include <<a href="qdatetime-h.html">qdatetime.h</a>>
|
---|
| 137 | #include <<a href="qmainwindow-h.html">qmainwindow.h</a>>
|
---|
| 138 | #include <<a href="qstatusbar-h.html">qstatusbar.h</a>>
|
---|
| 139 | #include <<a href="qmessagebox-h.html">qmessagebox.h</a>>
|
---|
| 140 | #include <<a href="qmenubar-h.html">qmenubar.h</a>>
|
---|
| 141 | #include <<a href="qapplication-h.html">qapplication.h</a>>
|
---|
| 142 | #include <<a href="qpainter-h.html">qpainter.h</a>>
|
---|
| 143 | #include <<a href="qprinter-h.html">qprinter.h</a>>
|
---|
| 144 | #include <<a href="qlabel-h.html">qlabel.h</a>>
|
---|
| 145 | #include <<a href="qimage-h.html">qimage.h</a>>
|
---|
| 146 | #include <<a href="qprogressdialog-h.html">qprogressdialog.h</a>>
|
---|
| 147 | #include "canvas.h"
|
---|
| 148 |
|
---|
| 149 | #include <stdlib.h>
|
---|
| 150 |
|
---|
| 151 | // We use a global variable to save memory - all the brushes and pens in
|
---|
| 152 | // the mesh are shared.
|
---|
| 153 | static QBrush *tb = 0;
|
---|
| 154 | static QPen *tp = 0;
|
---|
| 155 |
|
---|
| 156 | class EdgeItem;
|
---|
| 157 | class NodeItem;
|
---|
| 158 |
|
---|
| 159 | class EdgeItem: public <a href="qcanvasline.html">QCanvasLine</a>
|
---|
| 160 | {
|
---|
| 161 | public:
|
---|
| 162 | EdgeItem( NodeItem*, NodeItem*, QCanvas *canvas );
|
---|
| 163 | void setFromPoint( int x, int y ) ;
|
---|
| 164 | void setToPoint( int x, int y );
|
---|
| 165 | static int count() { return c; }
|
---|
| 166 | void moveBy(double dx, double dy);
|
---|
| 167 | private:
|
---|
| 168 | static int c;
|
---|
| 169 | };
|
---|
| 170 |
|
---|
| 171 | static const int imageRTTI = 984376;
|
---|
| 172 |
|
---|
| 173 |
|
---|
| 174 | class ImageItem: public <a href="qcanvasrectangle.html">QCanvasRectangle</a>
|
---|
| 175 | {
|
---|
| 176 | public:
|
---|
| 177 | ImageItem( <a href="qimage.html">QImage</a> img, QCanvas *canvas );
|
---|
| 178 | int rtti () const { return imageRTTI; }
|
---|
| 179 | bool hit( const <a href="qpoint.html">QPoint</a>&) const;
|
---|
| 180 | protected:
|
---|
| 181 | void drawShape( <a href="qpainter.html">QPainter</a> & );
|
---|
| 182 | private:
|
---|
| 183 | <a href="qimage.html">QImage</a> image;
|
---|
| 184 | <a href="qpixmap.html">QPixmap</a> pixmap;
|
---|
| 185 | };
|
---|
| 186 |
|
---|
| 187 |
|
---|
| 188 | <a name="f623"></a>ImageItem::ImageItem( <a href="qimage.html">QImage</a> img, QCanvas *canvas )
|
---|
| 189 | : <a href="qcanvasrectangle.html">QCanvasRectangle</a>( canvas ), image(img)
|
---|
| 190 | {
|
---|
| 191 | <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>() );
|
---|
| 192 |
|
---|
| 193 | #if !defined(Q_WS_QWS)
|
---|
| 194 | <a name="x2943"></a> pixmap.<a href="qpixmap.html#convertFromImage">convertFromImage</a>(image, OrderedAlphaDither);
|
---|
| 195 | #endif
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 |
|
---|
| 199 | <a name="x2925"></a>void ImageItem::<a href="qcanvasrectangle.html#drawShape">drawShape</a>( <a href="qpainter.html">QPainter</a> &p )
|
---|
| 200 | {
|
---|
| 201 | // On Qt/Embedded, we can paint a QImage as fast as a QPixmap,
|
---|
| 202 | // but on other platforms, we need to use a QPixmap.
|
---|
| 203 | #if defined(Q_WS_QWS)
|
---|
| 204 | <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 );
|
---|
| 205 | #else
|
---|
| 206 | 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 );
|
---|
| 207 | #endif
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 | bool <a name="f624"></a>ImageItem::hit( const <a href="qpoint.html">QPoint</a> &p ) const
|
---|
| 211 | {
|
---|
| 212 | <a name="x2944"></a> int ix = p.<a href="qpoint.html#x">x</a>()-int(<a href="qcanvasitem.html#x">x</a>());
|
---|
| 213 | <a name="x2945"></a> int iy = p.<a href="qpoint.html#y">y</a>()-int(<a href="qcanvasitem.html#y">y</a>());
|
---|
| 214 | <a name="x2934"></a> if ( !image.<a href="qimage.html#valid">valid</a>( ix , iy ) )
|
---|
| 215 | return FALSE;
|
---|
| 216 | <a name="x2933"></a> QRgb pixel = image.<a href="qimage.html#pixel">pixel</a>( ix, iy );
|
---|
| 217 | return qAlpha( pixel ) != 0;
|
---|
| 218 | }
|
---|
| 219 |
|
---|
| 220 | class NodeItem: public <a href="qcanvasellipse.html">QCanvasEllipse</a>
|
---|
| 221 | {
|
---|
| 222 | public:
|
---|
| 223 | NodeItem( <a href="qcanvas.html">QCanvas</a> *canvas );
|
---|
| 224 | ~NodeItem() {}
|
---|
| 225 |
|
---|
| 226 | void addInEdge( EdgeItem *edge ) { inList.<a href="qptrlist.html#append">append</a>( edge ); }
|
---|
| 227 | void addOutEdge( EdgeItem *edge ) { outList.<a href="qptrlist.html#append">append</a>( edge ); }
|
---|
| 228 |
|
---|
| 229 | void moveBy(double dx, double dy);
|
---|
| 230 |
|
---|
| 231 | // QPoint center() { return boundingRect().center(); }
|
---|
| 232 | private:
|
---|
| 233 | <a href="qptrlist.html">QPtrList</a><EdgeItem> inList;
|
---|
| 234 | <a href="qptrlist.html">QPtrList</a><EdgeItem> outList;
|
---|
| 235 | };
|
---|
| 236 |
|
---|
| 237 |
|
---|
| 238 | int EdgeItem::c = 0;
|
---|
| 239 |
|
---|
| 240 |
|
---|
| 241 | <a name="x2916"></a>void EdgeItem::<a href="qcanvasitem.html#moveBy">moveBy</a>(double, double)
|
---|
| 242 | {
|
---|
| 243 | //nothing
|
---|
| 244 | }
|
---|
| 245 |
|
---|
| 246 | <a name="f618"></a>EdgeItem::EdgeItem( NodeItem *from, NodeItem *to, QCanvas *canvas )
|
---|
| 247 | : <a href="qcanvasline.html">QCanvasLine</a>( canvas )
|
---|
| 248 | {
|
---|
| 249 | c++;
|
---|
| 250 | setPen( *tp );
|
---|
| 251 | setBrush( *tb );
|
---|
| 252 | from->addOutEdge( this );
|
---|
| 253 | to->addInEdge( this );
|
---|
| 254 | <a name="x2921"></a><a name="x2920"></a> setPoints( int(from-><a href="qcanvasitem.html#x">x</a>()), int(from-><a href="qcanvasitem.html#y">y</a>()), int(to->x()), int(to->y()) );
|
---|
| 255 | setZ( 127 );
|
---|
| 256 | }
|
---|
| 257 |
|
---|
| 258 | void <a name="f619"></a>EdgeItem::setFromPoint( int x, int y )
|
---|
| 259 | {
|
---|
| 260 | setPoints( x,y, endPoint().x(), endPoint().y() );
|
---|
| 261 | }
|
---|
| 262 |
|
---|
| 263 | void <a name="f620"></a>EdgeItem::setToPoint( int x, int y )
|
---|
| 264 | {
|
---|
| 265 | setPoints( startPoint().x(), startPoint().y(), x, y );
|
---|
| 266 | }
|
---|
| 267 |
|
---|
| 268 |
|
---|
| 269 |
|
---|
| 270 | void NodeItem::<a href="qcanvasitem.html#moveBy">moveBy</a>(double dx, double dy)
|
---|
| 271 | {
|
---|
| 272 | QCanvasEllipse::<a href="qcanvasitem.html#moveBy">moveBy</a>( dx, dy );
|
---|
| 273 |
|
---|
| 274 | <a href="qptrlistiterator.html">QPtrListIterator</a><EdgeItem> it1( inList );
|
---|
| 275 | EdgeItem *edge;
|
---|
| 276 | <a name="x2949"></a> while (( edge = it1.<a href="qptrlistiterator.html#current">current</a>() )) {
|
---|
| 277 | ++it1;
|
---|
| 278 | edge->setToPoint( int(<a href="qwidget.html#x">x</a>()), int(<a href="qcanvasitem.html#y">y</a>()) );
|
---|
| 279 | }
|
---|
| 280 | <a href="qptrlistiterator.html">QPtrListIterator</a><EdgeItem> it2( outList );
|
---|
| 281 | while (( edge = it2.<a href="qptrlistiterator.html#current">current</a>() )) {
|
---|
| 282 | ++it2;
|
---|
| 283 | edge->setFromPoint( int(<a href="qcanvasitem.html#x">x</a>()), int(<a href="qcanvasitem.html#y">y</a>()) );
|
---|
| 284 | }
|
---|
| 285 | }
|
---|
| 286 |
|
---|
| 287 | <a name="f655"></a>NodeItem::NodeItem( <a href="qcanvas.html">QCanvas</a> *canvas )
|
---|
| 288 | : <a href="qcanvasellipse.html">QCanvasEllipse</a>( 6, 6, canvas )
|
---|
| 289 | {
|
---|
| 290 | setPen( *tp );
|
---|
| 291 | setBrush( *tb );
|
---|
| 292 | <a href="qcanvasitem.html#setZ">setZ</a>( 128 );
|
---|
| 293 | }
|
---|
| 294 |
|
---|
| 295 | <a name="f621"></a>FigureEditor::FigureEditor(
|
---|
| 296 | <a href="qcanvas.html">QCanvas</a>& c, QWidget* parent,
|
---|
| 297 | const char* name, WFlags f) :
|
---|
| 298 | <a href="qcanvasview.html">QCanvasView</a>(&c,parent,name,f)
|
---|
| 299 | {
|
---|
| 300 | }
|
---|
| 301 |
|
---|
| 302 | <a name="x2951"></a>void FigureEditor::<a href="qscrollview.html#contentsMousePressEvent">contentsMousePressEvent</a>(QMouseEvent* e)
|
---|
| 303 | {
|
---|
| 304 | <a href="qpoint.html">QPoint</a> p = inverseWorldMatrix().map(e->pos());
|
---|
| 305 | <a href="qcanvasitemlist.html">QCanvasItemList</a> l=<a href="qcanvasitem.html#canvas">canvas</a>()->collisions(p);
|
---|
| 306 | <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) {
|
---|
| 307 | if ( (*it)->rtti() == imageRTTI ) {
|
---|
| 308 | ImageItem *item= (ImageItem*)(*it);
|
---|
| 309 | if ( !item->hit( p ) )
|
---|
| 310 | continue;
|
---|
| 311 | }
|
---|
| 312 | moving = *it;
|
---|
| 313 | moving_start = p;
|
---|
| 314 | return;
|
---|
| 315 | }
|
---|
| 316 | moving = 0;
|
---|
| 317 | }
|
---|
| 318 |
|
---|
| 319 | void <a name="f622"></a>FigureEditor::clear()
|
---|
| 320 | {
|
---|
| 321 | <a href="qcanvasitemlist.html">QCanvasItemList</a> list = <a href="qcanvasitem.html#canvas">canvas</a>()->allItems();
|
---|
| 322 | QCanvasItemList::Iterator it = list.<a href="qvaluelist.html#begin">begin</a>();
|
---|
| 323 | for (; it != list.<a href="qvaluelist.html#end">end</a>(); ++it) {
|
---|
| 324 | if ( *it )
|
---|
| 325 | delete *it;
|
---|
| 326 | }
|
---|
| 327 | }
|
---|
| 328 |
|
---|
| 329 | <a name="x2950"></a>void FigureEditor::<a href="qscrollview.html#contentsMouseMoveEvent">contentsMouseMoveEvent</a>(QMouseEvent* e)
|
---|
| 330 | {
|
---|
| 331 | if ( moving ) {
|
---|
| 332 | <a href="qpoint.html">QPoint</a> p = inverseWorldMatrix().map(e->pos());
|
---|
| 333 | moving->moveBy(p.<a href="qpoint.html#x">x</a>() - moving_start.x(),
|
---|
| 334 | p.<a href="qpoint.html#y">y</a>() - moving_start.y());
|
---|
| 335 | moving_start = p;
|
---|
| 336 | <a href="qcanvasitem.html#canvas">canvas</a>()->update();
|
---|
| 337 | }
|
---|
| 338 | }
|
---|
| 339 |
|
---|
| 340 |
|
---|
| 341 |
|
---|
| 342 | <a name="f615"></a>BouncyLogo::BouncyLogo(QCanvas* canvas) :
|
---|
| 343 | <a href="qcanvassprite.html">QCanvasSprite</a>(0,canvas)
|
---|
| 344 | {
|
---|
| 345 | static QCanvasPixmapArray logo("qt-trans.xpm");
|
---|
| 346 | <a href="qcanvassprite.html#setSequence">setSequence</a>(&logo);
|
---|
| 347 | <a href="qcanvasitem.html#setAnimated">setAnimated</a>(TRUE);
|
---|
| 348 | initPos();
|
---|
| 349 | }
|
---|
| 350 |
|
---|
| 351 |
|
---|
| 352 | const int logo_rtti = 1234;
|
---|
| 353 |
|
---|
| 354 | <a name="x2928"></a>int BouncyLogo::<a href="qcanvassprite.html#rtti">rtti</a>() const
|
---|
| 355 | {
|
---|
| 356 | return logo_rtti;
|
---|
| 357 | }
|
---|
| 358 |
|
---|
| 359 | void <a name="f616"></a>BouncyLogo::initPos()
|
---|
| 360 | {
|
---|
| 361 | initSpeed();
|
---|
| 362 | int trial=1000;
|
---|
| 363 | do {
|
---|
| 364 | <a href="qcanvassprite.html#move">move</a>(rand()%canvas()->width(),rand()%canvas()->height());
|
---|
| 365 | advance(0);
|
---|
| 366 | } while (trial-- && xVelocity()==0.0 && yVelocity()==0.0);
|
---|
| 367 | }
|
---|
| 368 |
|
---|
| 369 | void <a name="f617"></a>BouncyLogo::initSpeed()
|
---|
| 370 | {
|
---|
| 371 | const double speed = 4.0;
|
---|
| 372 | double d = (double)(rand()%1024) / 1024.0;
|
---|
| 373 | setVelocity( d*speed*2-speed, (1-d)*speed*2-speed );
|
---|
| 374 | }
|
---|
| 375 |
|
---|
| 376 | <a name="x2927"></a>void BouncyLogo::<a href="qcanvassprite.html#advance">advance</a>(int stage)
|
---|
| 377 | {
|
---|
| 378 | switch ( stage ) {
|
---|
| 379 | case 0: {
|
---|
| 380 | double vx = xVelocity();
|
---|
| 381 | double vy = yVelocity();
|
---|
| 382 |
|
---|
| 383 | if ( vx == 0.0 && vy == 0.0 ) {
|
---|
| 384 | // stopped last turn
|
---|
| 385 | initSpeed();
|
---|
| 386 | vx = xVelocity();
|
---|
| 387 | vy = yVelocity();
|
---|
| 388 | }
|
---|
| 389 |
|
---|
| 390 | double nx = <a href="qwidget.html#x">x</a>() + vx;
|
---|
| 391 | double ny = <a href="qwidget.html#y">y</a>() + vy;
|
---|
| 392 |
|
---|
| 393 | if ( nx < 0 || nx >= canvas()->width() )
|
---|
| 394 | vx = -vx;
|
---|
| 395 | if ( ny < 0 || ny >= canvas()->height() )
|
---|
| 396 | vy = -vy;
|
---|
| 397 |
|
---|
| 398 | for (int bounce=0; bounce<4; bounce++) {
|
---|
| 399 | <a href="qcanvasitemlist.html">QCanvasItemList</a> l=collisions(FALSE);
|
---|
| 400 | for (QCanvasItemList::Iterator it=l.<a href="qvaluelist.html#begin">begin</a>(); it!=l.<a href="qvaluelist.html#end">end</a>(); ++it) {
|
---|
| 401 | <a href="qcanvasitem.html">QCanvasItem</a> *hit = *it;
|
---|
| 402 | <a name="x2917"></a><a name="x2914"></a> if ( hit-><a href="qcanvasitem.html#rtti">rtti</a>()==logo_rtti && hit-><a href="qcanvasitem.html#collidesWith">collidesWith</a>(this) ) {
|
---|
| 403 | switch ( bounce ) {
|
---|
| 404 | case 0:
|
---|
| 405 | vx = -vx;
|
---|
| 406 | break;
|
---|
| 407 | case 1:
|
---|
| 408 | vy = -vy;
|
---|
| 409 | vx = -vx;
|
---|
| 410 | break;
|
---|
| 411 | case 2:
|
---|
| 412 | vx = -vx;
|
---|
| 413 | break;
|
---|
| 414 | case 3:
|
---|
| 415 | // Stop for this turn
|
---|
| 416 | vx = 0;
|
---|
| 417 | vy = 0;
|
---|
| 418 | break;
|
---|
| 419 | }
|
---|
| 420 | setVelocity(vx,vy);
|
---|
| 421 | break;
|
---|
| 422 | }
|
---|
| 423 | }
|
---|
| 424 | }
|
---|
| 425 |
|
---|
| 426 | if ( <a href="qwidget.html#x">x</a>()+vx < 0 || x()+vx >= canvas()->width() )
|
---|
| 427 | vx = 0;
|
---|
| 428 | if ( <a href="qwidget.html#y">y</a>()+vy < 0 || y()+vy >= canvas()->height() )
|
---|
| 429 | vy = 0;
|
---|
| 430 |
|
---|
| 431 | setVelocity(vx,vy);
|
---|
| 432 | } break;
|
---|
| 433 | case 1:
|
---|
| 434 | <a name="x2913"></a> <a href="qcanvasitem.html">QCanvasItem</a>::<a href="qcanvasitem.html#advance">advance</a>(stage);
|
---|
| 435 | break;
|
---|
| 436 | }
|
---|
| 437 | }
|
---|
| 438 |
|
---|
| 439 | static uint mainCount = 0;
|
---|
| 440 | static QImage *butterflyimg;
|
---|
| 441 | static QImage *logoimg;
|
---|
| 442 |
|
---|
| 443 | <a name="f625"></a>Main::Main(QCanvas& c, QWidget* parent, const char* name, WFlags f) :
|
---|
| 444 | <a href="qmainwindow.html">QMainWindow</a>(parent,name,f),
|
---|
| 445 | canvas(c)
|
---|
| 446 | {
|
---|
| 447 | editor = new FigureEditor(canvas,this);
|
---|
| 448 | <a href="qmenubar.html">QMenuBar</a>* menu = <a href="qmainwindow.html#menuBar">menuBar</a>();
|
---|
| 449 |
|
---|
| 450 | <a href="qpopupmenu.html">QPopupMenu</a>* file = new <a href="qpopupmenu.html">QPopupMenu</a>( menu );
|
---|
| 451 | <a name="x2936"></a> file-><a href="qmenudata.html#insertItem">insertItem</a>("&Fill canvas", this, SLOT(init()), CTRL+Key_F);
|
---|
| 452 | file-><a href="qmenudata.html#insertItem">insertItem</a>("&Erase canvas", this, SLOT(clear()), CTRL+Key_E);
|
---|
| 453 | file-><a href="qmenudata.html#insertItem">insertItem</a>("&New view", this, SLOT(newView()), CTRL+Key_N);
|
---|
| 454 | file-><a href="qmenudata.html#insertSeparator">insertSeparator</a>();
|
---|
| 455 | file-><a href="qmenudata.html#insertItem">insertItem</a>("&Print...", this, SLOT(print()), CTRL+Key_P);
|
---|
| 456 | file-><a href="qmenudata.html#insertSeparator">insertSeparator</a>();
|
---|
| 457 | file-><a href="qmenudata.html#insertItem">insertItem</a>("E&xit", qApp, SLOT(<a href="qapplication.html#quit">quit</a>()), CTRL+Key_Q);
|
---|
| 458 | menu-><a href="qmenudata.html#insertItem">insertItem</a>("&File", file);
|
---|
| 459 |
|
---|
| 460 | <a href="qpopupmenu.html">QPopupMenu</a>* edit = new <a href="qpopupmenu.html">QPopupMenu</a>( menu );
|
---|
| 461 | edit-><a href="qmenudata.html#insertItem">insertItem</a>("Add &Circle", this, SLOT(addCircle()), ALT+Key_C);
|
---|
| 462 | edit-><a href="qmenudata.html#insertItem">insertItem</a>("Add &Hexagon", this, SLOT(addHexagon()), ALT+Key_H);
|
---|
| 463 | edit-><a href="qmenudata.html#insertItem">insertItem</a>("Add &Polygon", this, SLOT(addPolygon()), ALT+Key_P);
|
---|
| 464 | edit-><a href="qmenudata.html#insertItem">insertItem</a>("Add Spl&ine", this, SLOT(addSpline()), ALT+Key_I);
|
---|
| 465 | edit-><a href="qmenudata.html#insertItem">insertItem</a>("Add &Text", this, SLOT(addText()), ALT+Key_T);
|
---|
| 466 | edit-><a href="qmenudata.html#insertItem">insertItem</a>("Add &Line", this, SLOT(addLine()), ALT+Key_L);
|
---|
| 467 | edit-><a href="qmenudata.html#insertItem">insertItem</a>("Add &Rectangle", this, SLOT(addRectangle()), ALT+Key_R);
|
---|
| 468 | edit-><a href="qmenudata.html#insertItem">insertItem</a>("Add &Sprite", this, SLOT(addSprite()), ALT+Key_S);
|
---|
| 469 | edit-><a href="qmenudata.html#insertItem">insertItem</a>("Create &Mesh", this, SLOT(addMesh()), ALT+Key_M );
|
---|
| 470 | edit-><a href="qmenudata.html#insertItem">insertItem</a>("Add &Alpha-blended image", this, SLOT(addButterfly()), ALT+Key_A);
|
---|
| 471 | menu-><a href="qmenudata.html#insertItem">insertItem</a>("&Edit", edit);
|
---|
| 472 |
|
---|
| 473 | <a href="qpopupmenu.html">QPopupMenu</a>* view = new <a href="qpopupmenu.html">QPopupMenu</a>( menu );
|
---|
| 474 | view-><a href="qmenudata.html#insertItem">insertItem</a>("&Enlarge", this, SLOT(enlarge()), SHIFT+CTRL+Key_Plus);
|
---|
| 475 | view-><a href="qmenudata.html#insertItem">insertItem</a>("Shr&ink", this, SLOT(shrink()), SHIFT+CTRL+Key_Minus);
|
---|
| 476 | view-><a href="qmenudata.html#insertSeparator">insertSeparator</a>();
|
---|
| 477 | view-><a href="qmenudata.html#insertItem">insertItem</a>("&Rotate clockwise", this, SLOT(rotateClockwise()), CTRL+Key_PageDown);
|
---|
| 478 | view-><a href="qmenudata.html#insertItem">insertItem</a>("Rotate &counterclockwise", this, SLOT(rotateCounterClockwise()), CTRL+Key_PageUp);
|
---|
| 479 | view-><a href="qmenudata.html#insertItem">insertItem</a>("&Zoom in", this, SLOT(zoomIn()), CTRL+Key_Plus);
|
---|
| 480 | view-><a href="qmenudata.html#insertItem">insertItem</a>("Zoom &out", this, SLOT(zoomOut()), CTRL+Key_Minus);
|
---|
| 481 | view-><a href="qmenudata.html#insertItem">insertItem</a>("Translate left", this, SLOT(moveL()), CTRL+Key_Left);
|
---|
| 482 | view-><a href="qmenudata.html#insertItem">insertItem</a>("Translate right", this, SLOT(moveR()), CTRL+Key_Right);
|
---|
| 483 | view-><a href="qmenudata.html#insertItem">insertItem</a>("Translate up", this, SLOT(moveU()), CTRL+Key_Up);
|
---|
| 484 | view-><a href="qmenudata.html#insertItem">insertItem</a>("Translate down", this, SLOT(moveD()), CTRL+Key_Down);
|
---|
| 485 | view-><a href="qmenudata.html#insertItem">insertItem</a>("&Mirror", this, SLOT(mirror()), CTRL+Key_Home);
|
---|
| 486 | menu-><a href="qmenudata.html#insertItem">insertItem</a>("&View", view);
|
---|
| 487 |
|
---|
| 488 | options = new <a href="qpopupmenu.html">QPopupMenu</a>( menu );
|
---|
| 489 | dbf_id = options-><a href="qmenudata.html#insertItem">insertItem</a>("Double buffer", this, SLOT(toggleDoubleBuffer()));
|
---|
| 490 | <a name="x2939"></a> options-><a href="qmenudata.html#setItemChecked">setItemChecked</a>(dbf_id, TRUE);
|
---|
| 491 | menu-><a href="qmenudata.html#insertItem">insertItem</a>("&Options",options);
|
---|
| 492 |
|
---|
| 493 | menu-><a href="qmenudata.html#insertSeparator">insertSeparator</a>();
|
---|
| 494 |
|
---|
| 495 | <a href="qpopupmenu.html">QPopupMenu</a>* help = new <a href="qpopupmenu.html">QPopupMenu</a>( menu );
|
---|
| 496 | help-><a href="qmenudata.html#insertItem">insertItem</a>("&About", this, SLOT(help()), Key_F1);
|
---|
| 497 | help-><a href="qmenudata.html#setItemChecked">setItemChecked</a>(dbf_id, TRUE);
|
---|
| 498 | menu-><a href="qmenudata.html#insertItem">insertItem</a>("&Help",help);
|
---|
| 499 |
|
---|
| 500 | <a href="qmainwindow.html#statusBar">statusBar</a>();
|
---|
| 501 |
|
---|
| 502 | <a href="qmainwindow.html#setCentralWidget">setCentralWidget</a>(editor);
|
---|
| 503 |
|
---|
| 504 | printer = 0;
|
---|
| 505 |
|
---|
| 506 | init();
|
---|
| 507 | }
|
---|
| 508 |
|
---|
| 509 | void <a name="f626"></a>Main::init()
|
---|
| 510 | {
|
---|
| 511 | clear();
|
---|
| 512 |
|
---|
| 513 | static int r=24;
|
---|
| 514 | srand(++r);
|
---|
| 515 |
|
---|
| 516 | mainCount++;
|
---|
| 517 | butterflyimg = 0;
|
---|
| 518 | logoimg = 0;
|
---|
| 519 |
|
---|
| 520 | int i;
|
---|
| 521 | <a name="x2912"></a> for ( i=0; i<canvas.<a href="qcanvas.html#width">width</a>() / 56; i++) {
|
---|
| 522 | addButterfly();
|
---|
| 523 | }
|
---|
| 524 | for ( i=0; i<canvas.<a href="qcanvas.html#width">width</a>() / 85; i++) {
|
---|
| 525 | addHexagon();
|
---|
| 526 | }
|
---|
| 527 | for ( i=0; i<canvas.<a href="qcanvas.html#width">width</a>() / 128; i++) {
|
---|
| 528 | addLogo();
|
---|
| 529 | }
|
---|
| 530 | }
|
---|
| 531 |
|
---|
| 532 | Main::~Main()
|
---|
| 533 | {
|
---|
| 534 | delete printer;
|
---|
| 535 | if ( !--mainCount ) {
|
---|
| 536 | delete[] butterflyimg;
|
---|
| 537 | butterflyimg = 0;
|
---|
| 538 | delete[] logoimg;
|
---|
| 539 | logoimg = 0;
|
---|
| 540 | }
|
---|
| 541 | }
|
---|
| 542 |
|
---|
| 543 | void <a name="f627"></a>Main::newView()
|
---|
| 544 | {
|
---|
| 545 | // Open a new view... have it delete when closed.
|
---|
| 546 | Main *m = new Main(canvas, 0, 0, WDestructiveClose);
|
---|
| 547 | qApp-><a href="qapplication.html#setMainWidget">setMainWidget</a>(m);
|
---|
| 548 | m-><a href="qwidget.html#show">show</a>();
|
---|
| 549 | qApp-><a href="qapplication.html#setMainWidget">setMainWidget</a>(0);
|
---|
| 550 | }
|
---|
| 551 |
|
---|
| 552 | void <a name="f628"></a>Main::clear()
|
---|
| 553 | {
|
---|
| 554 | editor->clear();
|
---|
| 555 | }
|
---|
| 556 |
|
---|
| 557 | void <a name="f629"></a>Main::help()
|
---|
| 558 | {
|
---|
| 559 | static QMessageBox* about = new <a href="qmessagebox.html">QMessageBox</a>( "Qt Canvas Example",
|
---|
| 560 | "<h3>The QCanvas classes example</h3>"
|
---|
| 561 | "<ul>"
|
---|
| 562 | "<li> Press ALT-S for some sprites."
|
---|
| 563 | "<li> Press ALT-C for some circles."
|
---|
| 564 | "<li> Press ALT-L for some lines."
|
---|
| 565 | "<li> Drag the objects around."
|
---|
| 566 | "<li> Read the code!"
|
---|
| 567 | "</ul>", QMessageBox::Information, 1, 0, 0, this, 0, FALSE );
|
---|
| 568 | about->setButtonText( 1, "Dismiss" );
|
---|
| 569 | about->show();
|
---|
| 570 | }
|
---|
| 571 |
|
---|
| 572 | void <a name="f630"></a>Main::aboutQt()
|
---|
| 573 | {
|
---|
| 574 | QMessageBox::<a href="qmessagebox.html#aboutQt">aboutQt</a>( this, "Qt Canvas Example" );
|
---|
| 575 | }
|
---|
| 576 |
|
---|
| 577 | void <a name="f631"></a>Main::toggleDoubleBuffer()
|
---|
| 578 | {
|
---|
| 579 | <a name="x2938"></a> bool s = !options-><a href="qmenudata.html#isItemChecked">isItemChecked</a>(dbf_id);
|
---|
| 580 | options-><a href="qmenudata.html#setItemChecked">setItemChecked</a>(dbf_id,s);
|
---|
| 581 | <a name="x2911"></a> canvas.<a href="qcanvas.html#setDoubleBuffering">setDoubleBuffering</a>(s);
|
---|
| 582 | }
|
---|
| 583 |
|
---|
| 584 | void <a name="f632"></a>Main::enlarge()
|
---|
| 585 | {
|
---|
| 586 | <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);
|
---|
| 587 | }
|
---|
| 588 |
|
---|
| 589 | void <a name="f633"></a>Main::shrink()
|
---|
| 590 | {
|
---|
| 591 | 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);
|
---|
| 592 | }
|
---|
| 593 |
|
---|
| 594 | void <a name="f634"></a>Main::rotateClockwise()
|
---|
| 595 | {
|
---|
| 596 | <a name="x2931"></a> <a href="qwmatrix.html">QWMatrix</a> m = editor-><a href="qcanvasview.html#worldMatrix">worldMatrix</a>();
|
---|
| 597 | <a name="x2955"></a> m.<a href="qwmatrix.html#rotate">rotate</a>( 22.5 );
|
---|
| 598 | <a name="x2930"></a> editor-><a href="qcanvasview.html#setWorldMatrix">setWorldMatrix</a>( m );
|
---|
| 599 | }
|
---|
| 600 |
|
---|
| 601 | void <a name="f635"></a>Main::rotateCounterClockwise()
|
---|
| 602 | {
|
---|
| 603 | <a href="qwmatrix.html">QWMatrix</a> m = editor-><a href="qcanvasview.html#worldMatrix">worldMatrix</a>();
|
---|
| 604 | m.<a href="qwmatrix.html#rotate">rotate</a>( -22.5 );
|
---|
| 605 | editor-><a href="qcanvasview.html#setWorldMatrix">setWorldMatrix</a>( m );
|
---|
| 606 | }
|
---|
| 607 |
|
---|
| 608 | void <a name="f636"></a>Main::zoomIn()
|
---|
| 609 | {
|
---|
| 610 | <a href="qwmatrix.html">QWMatrix</a> m = editor-><a href="qcanvasview.html#worldMatrix">worldMatrix</a>();
|
---|
| 611 | <a name="x2956"></a> m.<a href="qwmatrix.html#scale">scale</a>( 2.0, 2.0 );
|
---|
| 612 | editor-><a href="qcanvasview.html#setWorldMatrix">setWorldMatrix</a>( m );
|
---|
| 613 | }
|
---|
| 614 |
|
---|
| 615 | void <a name="f637"></a>Main::zoomOut()
|
---|
| 616 | {
|
---|
| 617 | <a href="qwmatrix.html">QWMatrix</a> m = editor-><a href="qcanvasview.html#worldMatrix">worldMatrix</a>();
|
---|
| 618 | m.<a href="qwmatrix.html#scale">scale</a>( 0.5, 0.5 );
|
---|
| 619 | editor-><a href="qcanvasview.html#setWorldMatrix">setWorldMatrix</a>( m );
|
---|
| 620 | }
|
---|
| 621 |
|
---|
| 622 | void <a name="f638"></a>Main::mirror()
|
---|
| 623 | {
|
---|
| 624 | <a href="qwmatrix.html">QWMatrix</a> m = editor-><a href="qcanvasview.html#worldMatrix">worldMatrix</a>();
|
---|
| 625 | m.<a href="qwmatrix.html#scale">scale</a>( -1, 1 );
|
---|
| 626 | editor-><a href="qcanvasview.html#setWorldMatrix">setWorldMatrix</a>( m );
|
---|
| 627 | }
|
---|
| 628 |
|
---|
| 629 | void <a name="f639"></a>Main::moveL()
|
---|
| 630 | {
|
---|
| 631 | <a href="qwmatrix.html">QWMatrix</a> m = editor-><a href="qcanvasview.html#worldMatrix">worldMatrix</a>();
|
---|
| 632 | <a name="x2957"></a> m.<a href="qwmatrix.html#translate">translate</a>( -16, 0 );
|
---|
| 633 | editor-><a href="qcanvasview.html#setWorldMatrix">setWorldMatrix</a>( m );
|
---|
| 634 | }
|
---|
| 635 |
|
---|
| 636 | void <a name="f640"></a>Main::moveR()
|
---|
| 637 | {
|
---|
| 638 | <a href="qwmatrix.html">QWMatrix</a> m = editor-><a href="qcanvasview.html#worldMatrix">worldMatrix</a>();
|
---|
| 639 | m.<a href="qwmatrix.html#translate">translate</a>( +16, 0 );
|
---|
| 640 | editor-><a href="qcanvasview.html#setWorldMatrix">setWorldMatrix</a>( m );
|
---|
| 641 | }
|
---|
| 642 |
|
---|
| 643 | void <a name="f641"></a>Main::moveU()
|
---|
| 644 | {
|
---|
| 645 | <a href="qwmatrix.html">QWMatrix</a> m = editor-><a href="qcanvasview.html#worldMatrix">worldMatrix</a>();
|
---|
| 646 | m.<a href="qwmatrix.html#translate">translate</a>( 0, -16 );
|
---|
| 647 | editor-><a href="qcanvasview.html#setWorldMatrix">setWorldMatrix</a>( m );
|
---|
| 648 | }
|
---|
| 649 |
|
---|
| 650 | void <a name="f642"></a>Main::moveD()
|
---|
| 651 | {
|
---|
| 652 | <a href="qwmatrix.html">QWMatrix</a> m = editor-><a href="qcanvasview.html#worldMatrix">worldMatrix</a>();
|
---|
| 653 | m.<a href="qwmatrix.html#translate">translate</a>( 0, +16 );
|
---|
| 654 | editor-><a href="qcanvasview.html#setWorldMatrix">setWorldMatrix</a>( m );
|
---|
| 655 | }
|
---|
| 656 |
|
---|
| 657 | void <a name="f643"></a>Main::print()
|
---|
| 658 | {
|
---|
| 659 | if ( !printer ) printer = new <a href="qprinter.html">QPrinter</a>;
|
---|
| 660 | if ( printer->setup(this) ) {
|
---|
| 661 | <a href="qpainter.html">QPainter</a> pp(printer);
|
---|
| 662 | <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>()),&pp,FALSE);
|
---|
| 663 | }
|
---|
| 664 | }
|
---|
| 665 |
|
---|
| 666 |
|
---|
| 667 | void <a name="f644"></a>Main::addSprite()
|
---|
| 668 | {
|
---|
| 669 | <a href="qcanvasitem.html">QCanvasItem</a>* i = new BouncyLogo(&canvas);
|
---|
| 670 | <a name="x2918"></a> i-><a href="qcanvasitem.html#setZ">setZ</a>(rand()%256);
|
---|
| 671 | <a name="x2919"></a> i-><a href="qcanvasitem.html#show">show</a>();
|
---|
| 672 | }
|
---|
| 673 |
|
---|
| 674 | QString butterfly_fn;
|
---|
| 675 | QString logo_fn;
|
---|
| 676 |
|
---|
| 677 |
|
---|
| 678 | void <a name="f645"></a>Main::addButterfly()
|
---|
| 679 | {
|
---|
| 680 | if ( butterfly_fn.isEmpty() )
|
---|
| 681 | return;
|
---|
| 682 | if ( !butterflyimg ) {
|
---|
| 683 | butterflyimg = new <a href="qimage.html">QImage</a>[4];
|
---|
| 684 | butterflyimg[0].load( butterfly_fn );
|
---|
| 685 | butterflyimg[1] = butterflyimg[0].smoothScale( int(butterflyimg[0].width()*0.75),
|
---|
| 686 | int(butterflyimg[0].height()*0.75) );
|
---|
| 687 | butterflyimg[2] = butterflyimg[0].smoothScale( int(butterflyimg[0].width()*0.5),
|
---|
| 688 | int(butterflyimg[0].height()*0.5) );
|
---|
| 689 | butterflyimg[3] = butterflyimg[0].smoothScale( int(butterflyimg[0].width()*0.25),
|
---|
| 690 | int(butterflyimg[0].height()*0.25) );
|
---|
| 691 | }
|
---|
| 692 | <a href="qcanvaspolygonalitem.html">QCanvasPolygonalItem</a>* i = new ImageItem(butterflyimg[rand()%4],&canvas);
|
---|
| 693 | <a name="x2915"></a> i-><a href="qcanvasitem.html#move">move</a>(rand()%(canvas.<a href="qcanvas.html#width">width</a>()-butterflyimg-><a href="qimage.html#width">width</a>()),
|
---|
| 694 | rand()%(canvas.<a href="qcanvas.html#height">height</a>()-butterflyimg-><a href="qimage.html#height">height</a>()));
|
---|
| 695 | i-><a href="qcanvasitem.html#setZ">setZ</a>(rand()%256+250);
|
---|
| 696 | i-><a href="qcanvasitem.html#show">show</a>();
|
---|
| 697 | }
|
---|
| 698 |
|
---|
| 699 | void <a name="f646"></a>Main::addLogo()
|
---|
| 700 | {
|
---|
| 701 | if ( logo_fn.isEmpty() )
|
---|
| 702 | return;
|
---|
| 703 | if ( !logoimg ) {
|
---|
| 704 | logoimg = new <a href="qimage.html">QImage</a>[4];
|
---|
| 705 | logoimg[0].load( logo_fn );
|
---|
| 706 | logoimg[1] = logoimg[0].smoothScale( int(logoimg[0].width()*0.75),
|
---|
| 707 | int(logoimg[0].height()*0.75) );
|
---|
| 708 | logoimg[2] = logoimg[0].smoothScale( int(logoimg[0].width()*0.5),
|
---|
| 709 | int(logoimg[0].height()*0.5) );
|
---|
| 710 | logoimg[3] = logoimg[0].smoothScale( int(logoimg[0].width()*0.25),
|
---|
| 711 | int(logoimg[0].height()*0.25) );
|
---|
| 712 | }
|
---|
| 713 | <a href="qcanvaspolygonalitem.html">QCanvasPolygonalItem</a>* i = new ImageItem(logoimg[rand()%4],&canvas);
|
---|
| 714 | i-><a href="qcanvasitem.html#move">move</a>(rand()%(canvas.<a href="qcanvas.html#width">width</a>()-logoimg-><a href="qimage.html#width">width</a>()),
|
---|
| 715 | rand()%(canvas.<a href="qcanvas.html#height">height</a>()-logoimg-><a href="qimage.html#width">width</a>()));
|
---|
| 716 | i-><a href="qcanvasitem.html#setZ">setZ</a>(rand()%256+256);
|
---|
| 717 | i-><a href="qcanvasitem.html#show">show</a>();
|
---|
| 718 | }
|
---|
| 719 |
|
---|
| 720 |
|
---|
| 721 |
|
---|
| 722 | void <a name="f647"></a>Main::addCircle()
|
---|
| 723 | {
|
---|
| 724 | <a href="qcanvaspolygonalitem.html">QCanvasPolygonalItem</a>* i = new <a href="qcanvasellipse.html">QCanvasEllipse</a>(50,50,&canvas);
|
---|
| 725 | <a name="x2923"></a> i-><a href="qcanvaspolygonalitem.html#setBrush">setBrush</a>( QColor(rand()%32*8,rand()%32*8,rand()%32*8) );
|
---|
| 726 | i-><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>());
|
---|
| 727 | i-><a href="qcanvasitem.html#setZ">setZ</a>(rand()%256);
|
---|
| 728 | i-><a href="qcanvasitem.html#show">show</a>();
|
---|
| 729 | }
|
---|
| 730 |
|
---|
| 731 | void <a name="f648"></a>Main::addHexagon()
|
---|
| 732 | {
|
---|
| 733 | <a href="qcanvaspolygon.html">QCanvasPolygon</a>* i = new <a href="qcanvaspolygon.html">QCanvasPolygon</a>(&canvas);
|
---|
| 734 | const int size = canvas.<a href="qcanvas.html#width">width</a>() / 25;
|
---|
| 735 | <a href="qpointarray.html">QPointArray</a> pa(6);
|
---|
| 736 | pa[0] = QPoint(2*size,0);
|
---|
| 737 | pa[1] = QPoint(size,-size*173/100);
|
---|
| 738 | pa[2] = QPoint(-size,-size*173/100);
|
---|
| 739 | pa[3] = QPoint(-2*size,0);
|
---|
| 740 | pa[4] = QPoint(-size,size*173/100);
|
---|
| 741 | pa[5] = QPoint(size,size*173/100);
|
---|
| 742 | <a name="x2922"></a> i-><a href="qcanvasline.html#setPoints">setPoints</a>(pa);
|
---|
| 743 | i-><a href="qcanvaspolygonalitem.html#setBrush">setBrush</a>( QColor(rand()%32*8,rand()%32*8,rand()%32*8) );
|
---|
| 744 | i-><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>());
|
---|
| 745 | i-><a href="qcanvasitem.html#setZ">setZ</a>(rand()%256);
|
---|
| 746 | i-><a href="qcanvasitem.html#show">show</a>();
|
---|
| 747 | }
|
---|
| 748 |
|
---|
| 749 | void <a name="f649"></a>Main::addPolygon()
|
---|
| 750 | {
|
---|
| 751 | <a href="qcanvaspolygon.html">QCanvasPolygon</a>* i = new <a href="qcanvaspolygon.html">QCanvasPolygon</a>(&canvas);
|
---|
| 752 | const int size = canvas.<a href="qcanvas.html#width">width</a>()/2;
|
---|
| 753 | <a href="qpointarray.html">QPointArray</a> pa(6);
|
---|
| 754 | pa[0] = QPoint(0,0);
|
---|
| 755 | pa[1] = QPoint(size,size/5);
|
---|
| 756 | pa[2] = QPoint(size*4/5,size);
|
---|
| 757 | pa[3] = QPoint(size/6,size*5/4);
|
---|
| 758 | pa[4] = QPoint(size*3/4,size*3/4);
|
---|
| 759 | pa[5] = QPoint(size*3/4,size/4);
|
---|
| 760 | i-><a href="qcanvasline.html#setPoints">setPoints</a>(pa);
|
---|
| 761 | i-><a href="qcanvaspolygonalitem.html#setBrush">setBrush</a>( QColor(rand()%32*8,rand()%32*8,rand()%32*8) );
|
---|
| 762 | i-><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>());
|
---|
| 763 | i-><a href="qcanvasitem.html#setZ">setZ</a>(rand()%256);
|
---|
| 764 | i-><a href="qcanvasitem.html#show">show</a>();
|
---|
| 765 | }
|
---|
| 766 |
|
---|
| 767 | void <a name="f650"></a>Main::addSpline()
|
---|
| 768 | {
|
---|
| 769 | <a href="qcanvasspline.html">QCanvasSpline</a>* i = new <a href="qcanvasspline.html">QCanvasSpline</a>(&canvas);
|
---|
| 770 | const int size = canvas.<a href="qcanvas.html#width">width</a>()/6;
|
---|
| 771 | <a href="qpointarray.html">QPointArray</a> pa(12);
|
---|
| 772 | pa[0] = QPoint(0,0);
|
---|
| 773 | pa[1] = QPoint(size/2,0);
|
---|
| 774 | pa[2] = QPoint(size,size/2);
|
---|
| 775 | pa[3] = QPoint(size,size);
|
---|
| 776 | pa[4] = QPoint(size,size*3/2);
|
---|
| 777 | pa[5] = QPoint(size/2,size*2);
|
---|
| 778 | pa[6] = QPoint(0,size*2);
|
---|
| 779 | pa[7] = QPoint(-size/2,size*2);
|
---|
| 780 | pa[8] = QPoint(size/4,size*3/2);
|
---|
| 781 | pa[9] = QPoint(0,size);
|
---|
| 782 | pa[10]= QPoint(-size/4,size/2);
|
---|
| 783 | pa[11]= QPoint(-size/2,0);
|
---|
| 784 | <a name="x2926"></a> i-><a href="qcanvasspline.html#setControlPoints">setControlPoints</a>(pa);
|
---|
| 785 | i-><a href="qcanvaspolygonalitem.html#setBrush">setBrush</a>( QColor(rand()%32*8,rand()%32*8,rand()%32*8) );
|
---|
| 786 | i-><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>());
|
---|
| 787 | i-><a href="qcanvasitem.html#setZ">setZ</a>(rand()%256);
|
---|
| 788 | i-><a href="qcanvasitem.html#show">show</a>();
|
---|
| 789 | }
|
---|
| 790 |
|
---|
| 791 | void <a name="f651"></a>Main::addText()
|
---|
| 792 | {
|
---|
| 793 | <a href="qcanvastext.html">QCanvasText</a>* i = new <a href="qcanvastext.html">QCanvasText</a>(&canvas);
|
---|
| 794 | <a name="x2929"></a> i-><a href="qcanvastext.html#setText">setText</a>("QCanvasText");
|
---|
| 795 | i-><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>());
|
---|
| 796 | i-><a href="qcanvasitem.html#setZ">setZ</a>(rand()%256);
|
---|
| 797 | i-><a href="qcanvasitem.html#show">show</a>();
|
---|
| 798 | }
|
---|
| 799 |
|
---|
| 800 | void <a name="f652"></a>Main::addLine()
|
---|
| 801 | {
|
---|
| 802 | <a href="qcanvasline.html">QCanvasLine</a>* i = new <a href="qcanvasline.html">QCanvasLine</a>(&canvas);
|
---|
| 803 | i-><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>(),
|
---|
| 804 | rand()%canvas.<a href="qcanvas.html#width">width</a>(), rand()%canvas.<a href="qcanvas.html#height">height</a>() );
|
---|
| 805 | <a name="x2924"></a> i-><a href="qcanvaspolygonalitem.html#setPen">setPen</a>( QPen(QColor(rand()%32*8,rand()%32*8,rand()%32*8), 6) );
|
---|
| 806 | i-><a href="qcanvasitem.html#setZ">setZ</a>(rand()%256);
|
---|
| 807 | i-><a href="qcanvasitem.html#show">show</a>();
|
---|
| 808 | }
|
---|
| 809 |
|
---|
| 810 | void <a name="f653"></a>Main::addMesh()
|
---|
| 811 | {
|
---|
| 812 | int x0 = 0;
|
---|
| 813 | int y0 = 0;
|
---|
| 814 |
|
---|
| 815 | if ( !tb ) tb = new <a href="qbrush.html">QBrush</a>( Qt::red );
|
---|
| 816 | if ( !tp ) tp = new <a href="qpen.html">QPen</a>( Qt::black );
|
---|
| 817 |
|
---|
| 818 | int nodecount = 0;
|
---|
| 819 |
|
---|
| 820 | int w = canvas.<a href="qcanvas.html#width">width</a>();
|
---|
| 821 | int h = canvas.<a href="qcanvas.html#height">height</a>();
|
---|
| 822 |
|
---|
| 823 | const int dist = 30;
|
---|
| 824 | int rows = h / dist;
|
---|
| 825 | int cols = w / dist;
|
---|
| 826 |
|
---|
| 827 | #ifndef QT_NO_PROGRESSDIALOG
|
---|
| 828 | <a href="qprogressdialog.html">QProgressDialog</a> progress( "Creating mesh...", "Abort", rows,
|
---|
| 829 | this, "progress", TRUE );
|
---|
| 830 | #endif
|
---|
| 831 |
|
---|
| 832 | <a href="qmemarray.html">QMemArray</a><NodeItem*> lastRow(cols);
|
---|
| 833 | for ( int j = 0; j < rows; j++ ) {
|
---|
| 834 | int n = j%2 ? cols-1 : cols;
|
---|
| 835 | NodeItem *prev = 0;
|
---|
| 836 | for ( int i = 0; i < n; i++ ) {
|
---|
| 837 | NodeItem *el = new NodeItem( &canvas );
|
---|
| 838 | nodecount++;
|
---|
| 839 | int r = rand();
|
---|
| 840 | int xrand = r %20;
|
---|
| 841 | int yrand = (r/20) %20;
|
---|
| 842 | el-><a href="qcanvasitem.html#move">move</a>( xrand + x0 + i*dist + (j%2 ? dist/2 : 0 ),
|
---|
| 843 | yrand + y0 + j*dist );
|
---|
| 844 |
|
---|
| 845 | if ( j > 0 ) {
|
---|
| 846 | if ( i < cols-1 )
|
---|
| 847 | (new EdgeItem( lastRow[i], el, &canvas ))->show();
|
---|
| 848 | if ( j%2 )
|
---|
| 849 | (new EdgeItem( lastRow[i+1], el, &canvas ))->show();
|
---|
| 850 | else if ( i > 0 )
|
---|
| 851 | (new EdgeItem( lastRow[i-1], el, &canvas ))->show();
|
---|
| 852 | }
|
---|
| 853 | if ( prev ) {
|
---|
| 854 | (new EdgeItem( prev, el, &canvas ))->show();
|
---|
| 855 | }
|
---|
| 856 | if ( i > 0 ) lastRow[i-1] = prev;
|
---|
| 857 | prev = el;
|
---|
| 858 | el-><a href="qcanvasitem.html#show">show</a>();
|
---|
| 859 | }
|
---|
| 860 | lastRow[n-1]=prev;
|
---|
| 861 | #ifndef QT_NO_PROGRESSDIALOG
|
---|
| 862 | <a name="x2946"></a> progress.<a href="qprogressdialog.html#setProgress">setProgress</a>( j );
|
---|
| 863 | <a name="x2947"></a> if ( progress.<a href="qprogressdialog.html#wasCancelled">wasCancelled</a>() )
|
---|
| 864 | break;
|
---|
| 865 | #endif
|
---|
| 866 | }
|
---|
| 867 | #ifndef QT_NO_PROGRESSDIALOG
|
---|
| 868 | progress.<a href="qprogressdialog.html#setProgress">setProgress</a>( rows );
|
---|
| 869 | #endif
|
---|
| 870 | // qDebug( "%d nodes, %d edges", nodecount, EdgeItem::count() );
|
---|
| 871 | }
|
---|
| 872 |
|
---|
| 873 | void <a name="f654"></a>Main::addRectangle()
|
---|
| 874 | {
|
---|
| 875 | <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>(),
|
---|
| 876 | canvas.<a href="qcanvas.html#width">width</a>()/5,canvas.<a href="qcanvas.html#width">width</a>()/5,&canvas);
|
---|
| 877 | int z = rand()%256;
|
---|
| 878 | i-><a href="qcanvaspolygonalitem.html#setBrush">setBrush</a>( QColor(z,z,z) );
|
---|
| 879 | i-><a href="qcanvaspolygonalitem.html#setPen">setPen</a>( QPen(QColor(rand()%32*8,rand()%32*8,rand()%32*8), 6) );
|
---|
| 880 | i-><a href="qcanvasitem.html#setZ">setZ</a>(z);
|
---|
| 881 | i-><a href="qcanvasitem.html#show">show</a>();
|
---|
| 882 | }
|
---|
| 883 | </pre>
|
---|
| 884 |
|
---|
| 885 | <p> <hr>
|
---|
| 886 | <p> Main:
|
---|
| 887 | <p> <pre>#include <<a href="qstatusbar-h.html">qstatusbar.h</a>>
|
---|
| 888 | #include <<a href="qmessagebox-h.html">qmessagebox.h</a>>
|
---|
| 889 | #include <<a href="qmenubar-h.html">qmenubar.h</a>>
|
---|
| 890 | #include <<a href="qapplication-h.html">qapplication.h</a>>
|
---|
| 891 | #include <<a href="qimage-h.html">qimage.h</a>>
|
---|
| 892 |
|
---|
| 893 | #include "canvas.h"
|
---|
| 894 |
|
---|
| 895 | #include <stdlib.h>
|
---|
| 896 |
|
---|
| 897 | extern QString butterfly_fn;
|
---|
| 898 | extern QString logo_fn;
|
---|
| 899 |
|
---|
| 900 | int main(int argc, char** argv)
|
---|
| 901 | {
|
---|
| 902 | <a href="qapplication.html">QApplication</a> app(argc,argv);
|
---|
| 903 |
|
---|
| 904 | if ( argc > 1 )
|
---|
| 905 | butterfly_fn = argv[1];
|
---|
| 906 | else
|
---|
| 907 | butterfly_fn = "butterfly.png";
|
---|
| 908 |
|
---|
| 909 | if ( argc > 2 )
|
---|
| 910 | logo_fn = argv[2];
|
---|
| 911 | else
|
---|
| 912 | logo_fn = "qtlogo.png";
|
---|
| 913 |
|
---|
| 914 | <a href="qcanvas.html">QCanvas</a> canvas(800,600);
|
---|
| 915 | <a name="x2962"></a> canvas.<a href="qcanvas.html#setAdvancePeriod">setAdvancePeriod</a>(30);
|
---|
| 916 | Main m(canvas);
|
---|
| 917 | <a name="x2969"></a> m.<a href="qwidget.html#resize">resize</a>(m.<a href="qwidget.html#sizeHint">sizeHint</a>());
|
---|
| 918 | m.<a href="qwidget.html#setCaption">setCaption</a>("Qt Example - Canvas");
|
---|
| 919 | <a name="x2970"></a><a name="x2958"></a> if ( QApplication::<a href="qapplication.html#desktop">desktop</a>()->width() > m.<a href="qwidget.html#width">width</a>() + 10
|
---|
| 920 | <a name="x2964"></a> && QApplication::<a href="qapplication.html#desktop">desktop</a>()->height() > m.<a href="qwidget.html#height">height</a>() +30 )
|
---|
| 921 | m.<a href="qwidget.html#show">show</a>();
|
---|
| 922 | else
|
---|
| 923 | <a name="x2968"></a> m.<a href="qwidget.html#showMaximized">showMaximized</a>();
|
---|
| 924 |
|
---|
| 925 | 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>()) );
|
---|
| 926 |
|
---|
| 927 | return app.<a href="qapplication.html#exec">exec</a>();
|
---|
| 928 | }
|
---|
| 929 |
|
---|
| 930 | </pre>
|
---|
| 931 |
|
---|
| 932 | <p>See also <a href="examples.html">Examples</a>.
|
---|
| 933 |
|
---|
| 934 | <!-- eof -->
|
---|
| 935 | <p><address><hr><div align=center>
|
---|
| 936 | <table width=100% cellspacing=0 border=0><tr>
|
---|
| 937 | <td>Copyright © 2007
|
---|
| 938 | <a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
|
---|
| 939 | <td align=right><div align=right>Qt 3.3.8</div>
|
---|
| 940 | </table></div></address></body>
|
---|
| 941 | </html>
|
---|