examples/graphicsview/portedcanvas/canvas.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the examples of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include <qdatetime.h>
       
    43 #include <qmainwindow.h>
       
    44 #include <qstatusbar.h>
       
    45 #include <qmessagebox.h>
       
    46 #include <qmenubar.h>
       
    47 #include <qapplication.h>
       
    48 #include <qpainter.h>
       
    49 #include <qprinter.h>
       
    50 #include <qlabel.h>
       
    51 #include <qimage.h>
       
    52 #include <q3progressdialog.h>
       
    53 #include <Q3PointArray>
       
    54 #include <Q3PtrList>
       
    55 #include <QPixmap>
       
    56 #include <Q3PopupMenu>
       
    57 #include <QMouseEvent>
       
    58 #include <Q3MemArray>
       
    59 #include "canvas.h"
       
    60 #include <QStyleOptionGraphicsItem>
       
    61 #include <qdebug.h>
       
    62 #include <stdlib.h>
       
    63 
       
    64 // We use a global variable to save memory - all the brushes and pens in
       
    65 // the mesh are shared.
       
    66 static QBrush *tb = 0;
       
    67 static QPen *tp = 0;
       
    68 
       
    69 class EdgeItem;
       
    70 class NodeItem;
       
    71 
       
    72 class EdgeItem: public QGraphicsLineItem
       
    73 {
       
    74 public:
       
    75     EdgeItem( NodeItem*, NodeItem* );
       
    76     void setFromPoint( int x, int y ) ;
       
    77     void setToPoint( int x, int y );
       
    78     static int count() { return c; }
       
    79 private:
       
    80     static int c;
       
    81 };
       
    82 
       
    83 static const int imageRTTI = 984376;
       
    84 
       
    85 
       
    86 class ImageItem: public QGraphicsRectItem
       
    87 {
       
    88 public:
       
    89     ImageItem( QImage img );
       
    90     int rtti () const { return imageRTTI; }
       
    91 protected:
       
    92     void paint( QPainter *, const QStyleOptionGraphicsItem *option, QWidget *widget );
       
    93 private:
       
    94     QImage image;
       
    95     QPixmap pixmap;
       
    96 };
       
    97 
       
    98 
       
    99 ImageItem::ImageItem( QImage img )
       
   100     : image(img)
       
   101 {
       
   102     setRect(0, 0, image.width(), image.height());
       
   103     setFlag(ItemIsMovable);
       
   104 #if !defined(Q_WS_QWS)
       
   105     pixmap.convertFromImage(image, Qt::OrderedAlphaDither);
       
   106 #endif
       
   107 }
       
   108 
       
   109 void ImageItem::paint( QPainter *p, const QStyleOptionGraphicsItem *option, QWidget * )
       
   110 {
       
   111 // On Qt/Embedded, we can paint a QImage as fast as a QPixmap,
       
   112 // but on other platforms, we need to use a QPixmap.
       
   113 #if defined(Q_WS_QWS)
       
   114     p->drawImage( option->exposedRect, image, option->exposedRect, Qt::OrderedAlphaDither );
       
   115 #else
       
   116     p->drawPixmap( option->exposedRect, pixmap, option->exposedRect );
       
   117 #endif
       
   118 }
       
   119 
       
   120 class NodeItem: public QGraphicsEllipseItem
       
   121 {
       
   122 public:
       
   123     NodeItem();
       
   124     ~NodeItem() {}
       
   125 
       
   126     void addInEdge( EdgeItem *edge ) { inList.append( edge ); }
       
   127     void addOutEdge( EdgeItem *edge ) { outList.append( edge ); }
       
   128 
       
   129 protected:
       
   130     QVariant itemChange(GraphicsItemChange change, const QVariant &value);
       
   131 
       
   132 
       
   133     //    QPoint center() { return boundingRect().center(); }
       
   134 private:
       
   135     Q3PtrList<EdgeItem> inList;
       
   136     Q3PtrList<EdgeItem> outList;
       
   137 };
       
   138 
       
   139 
       
   140 int EdgeItem::c = 0;
       
   141 
       
   142 EdgeItem::EdgeItem( NodeItem *from, NodeItem *to )
       
   143     : QGraphicsLineItem( )
       
   144 {
       
   145     c++;
       
   146     setPen( *tp );
       
   147     from->addOutEdge( this );
       
   148     to->addInEdge( this );
       
   149     setLine( QLineF(int(from->x()), int(from->y()), int(to->x()), int(to->y()) ));
       
   150     setZValue( 127 );
       
   151     setBoundingRegionGranularity(0.05);
       
   152 }
       
   153 
       
   154 void EdgeItem::setFromPoint( int x, int y )
       
   155 {
       
   156     setLine(QLineF( x,y, line().p2().x(), line().p2().y() ));
       
   157 }
       
   158 
       
   159 void EdgeItem::setToPoint( int x, int y )
       
   160 {
       
   161     setLine(QLineF( line().p1().x(), line().p1().y(), x, y ));
       
   162 }
       
   163 
       
   164 QVariant NodeItem::itemChange(GraphicsItemChange change, const QVariant &value)
       
   165 {
       
   166     if (change == ItemPositionHasChanged) {
       
   167         Q3PtrListIterator<EdgeItem> it1( inList );
       
   168         EdgeItem *edge;
       
   169         while (( edge = it1.current() )) {
       
   170             ++it1;
       
   171             edge->setToPoint( int(x()), int(y()) );
       
   172         }
       
   173         Q3PtrListIterator<EdgeItem> it2( outList );
       
   174         while (( edge = it2.current() )) {
       
   175             ++it2;
       
   176             edge->setFromPoint( int(x()), int(y()) );
       
   177         }
       
   178     }
       
   179 
       
   180     return QGraphicsEllipseItem::itemChange(change, value);
       
   181 }
       
   182 
       
   183 NodeItem::NodeItem( )
       
   184     : QGraphicsEllipseItem( QRectF(-3, -3, 6, 6) )
       
   185 {
       
   186     setPen( *tp );
       
   187     setBrush( *tb );
       
   188     setZValue( 128 );
       
   189     setFlag(ItemIsMovable);
       
   190 }
       
   191 
       
   192 FigureEditor::FigureEditor(
       
   193 	QGraphicsScene& c, QWidget* parent,
       
   194 	const char* name, Qt::WindowFlags f) :
       
   195     QGraphicsView(&c,parent)
       
   196 {
       
   197     setObjectName(name);
       
   198     setWindowFlags(f);
       
   199     setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
       
   200 }
       
   201 
       
   202 void FigureEditor::clear()
       
   203 {
       
   204     scene()->clear();
       
   205 }
       
   206 
       
   207 BouncyLogo::BouncyLogo() :
       
   208     xvel(0), yvel(0)
       
   209 {
       
   210     setPixmap(QPixmap(":/trolltech/examples/graphicsview/portedcanvas/qt-trans.xpm"));
       
   211 }
       
   212 
       
   213 const int logo_rtti = 1234;
       
   214 
       
   215 int BouncyLogo::type() const
       
   216 {
       
   217     return logo_rtti;
       
   218 }
       
   219 
       
   220 QPainterPath BouncyLogo::shape() const
       
   221 {
       
   222     QPainterPath path;
       
   223     path.addRect(boundingRect());
       
   224     return path;
       
   225 }
       
   226 
       
   227 void BouncyLogo::initPos()
       
   228 {
       
   229     initSpeed();
       
   230     int trial=1000;
       
   231     do {
       
   232 	setPos(qrand()%int(scene()->width()),qrand()%int(scene()->height()));
       
   233 	advance(0);
       
   234     } while (trial-- && xvel==0.0 && yvel==0.0);
       
   235 }
       
   236 
       
   237 void BouncyLogo::initSpeed()
       
   238 {
       
   239     const double speed = 4.0;
       
   240     double d = (double)(qrand()%1024) / 1024.0;
       
   241     xvel = d*speed*2-speed;
       
   242     yvel = (1-d)*speed*2-speed;
       
   243 }
       
   244 
       
   245 void BouncyLogo::advance(int stage)
       
   246 {
       
   247     switch ( stage ) {
       
   248       case 0: {
       
   249 	double vx = xvel;
       
   250 	double vy = yvel;
       
   251 
       
   252 	if ( vx == 0.0 && vy == 0.0 ) {
       
   253 	    // stopped last turn
       
   254 	    initSpeed();
       
   255 	    vx = xvel;
       
   256 	    vy = yvel;
       
   257 	}
       
   258 
       
   259 	double nx = x() + vx;
       
   260 	double ny = y() + vy;
       
   261 
       
   262 	if ( nx < 0 || nx >= scene()->width() )
       
   263 	    vx = -vx;
       
   264 	if ( ny < 0 || ny >= scene()->height() )
       
   265 	    vy = -vy;
       
   266 
       
   267 	for (int bounce=0; bounce<4; bounce++) {
       
   268 	    QList<QGraphicsItem *> l=scene()->collidingItems(this);
       
   269             for (QList<QGraphicsItem *>::Iterator it=l.begin(); it!=l.end(); ++it) {
       
   270                 QGraphicsItem *hit = *it;
       
   271                 QPainterPath advancedShape = QMatrix().translate(xvel, yvel).map(shape());
       
   272                 if ( hit->type()==logo_rtti && hit->collidesWithPath(mapToItem(hit, advancedShape)) ) {
       
   273 		    switch ( bounce ) {
       
   274 		      case 0:
       
   275 			vx = -vx;
       
   276 			break;
       
   277 		      case 1:
       
   278 			vy = -vy;
       
   279 			vx = -vx;
       
   280 			break;
       
   281 		      case 2:
       
   282 			vx = -vx;
       
   283 			break;
       
   284 		      case 3:
       
   285 			// Stop for this turn
       
   286 			vx = 0;
       
   287 			vy = 0;
       
   288 			break;
       
   289 		    }
       
   290 		    xvel = vx;
       
   291                     yvel = vy;
       
   292 		    break;
       
   293 		}
       
   294 	    }
       
   295         }
       
   296 
       
   297 	if ( x()+vx < 0 || x()+vx >= scene()->width() )
       
   298 	    vx = 0;
       
   299 	if ( y()+vy < 0 || y()+vy >= scene()->height() )
       
   300 	    vy = 0;
       
   301 
       
   302 	xvel = vx;
       
   303         yvel = vy;
       
   304       } break;
       
   305       case 1:
       
   306         moveBy(xvel, yvel);
       
   307 	break;
       
   308     }
       
   309 }
       
   310 
       
   311 static uint mainCount = 0;
       
   312 static QImage *butterflyimg;
       
   313 static QImage *logoimg;
       
   314 
       
   315 Main::Main(QGraphicsScene& c, QWidget* parent, const char* name, Qt::WindowFlags f) :
       
   316     Q3MainWindow(parent,name,f),
       
   317     canvas(c)
       
   318 {
       
   319     editor = new FigureEditor(canvas,this);
       
   320     QMenuBar* menu = menuBar();
       
   321 
       
   322     Q3PopupMenu* file = new Q3PopupMenu( menu );
       
   323     file->insertItem("&Fill canvas", this, SLOT(init()), Qt::CTRL+Qt::Key_F);
       
   324     file->insertItem("&Erase canvas", this, SLOT(clear()), Qt::CTRL+Qt::Key_E);
       
   325     file->insertItem("&New view", this, SLOT(newView()), Qt::CTRL+Qt::Key_N);
       
   326     file->insertSeparator();
       
   327     file->insertItem("&Print...", this, SLOT(print()), Qt::CTRL+Qt::Key_P);
       
   328     file->insertSeparator();
       
   329     file->insertItem("E&xit", qApp, SLOT(quit()), Qt::CTRL+Qt::Key_Q);
       
   330     menu->insertItem("&File", file);
       
   331 
       
   332     Q3PopupMenu* edit = new Q3PopupMenu( menu );
       
   333     edit->insertItem("Add &Circle", this, SLOT(addCircle()), Qt::ALT+Qt::Key_C);
       
   334     edit->insertItem("Add &Hexagon", this, SLOT(addHexagon()), Qt::ALT+Qt::Key_H);
       
   335     edit->insertItem("Add &Polygon", this, SLOT(addPolygon()), Qt::ALT+Qt::Key_P);
       
   336     edit->insertItem("Add Spl&ine", this, SLOT(addSpline()), Qt::ALT+Qt::Key_I);
       
   337     edit->insertItem("Add &Text", this, SLOT(addText()), Qt::ALT+Qt::Key_T);
       
   338     edit->insertItem("Add &Line", this, SLOT(addLine()), Qt::ALT+Qt::Key_L);
       
   339     edit->insertItem("Add &Rectangle", this, SLOT(addRectangle()), Qt::ALT+Qt::Key_R);
       
   340     edit->insertItem("Add &Sprite", this, SLOT(addSprite()), Qt::ALT+Qt::Key_S);
       
   341     edit->insertItem("Create &Mesh", this, SLOT(addMesh()), Qt::ALT+Qt::Key_M );
       
   342     edit->insertItem("Add &Alpha-blended image", this, SLOT(addButterfly()), Qt::ALT+Qt::Key_A);
       
   343     menu->insertItem("&Edit", edit);
       
   344 
       
   345     Q3PopupMenu* view = new Q3PopupMenu( menu );
       
   346     view->insertItem("&Enlarge", this, SLOT(enlarge()), Qt::SHIFT+Qt::CTRL+Qt::Key_Plus);
       
   347     view->insertItem("Shr&ink", this, SLOT(shrink()), Qt::SHIFT+Qt::CTRL+Qt::Key_Minus);
       
   348     view->insertSeparator();
       
   349     view->insertItem("&Rotate clockwise", this, SLOT(rotateClockwise()), Qt::CTRL+Qt::Key_PageDown);
       
   350     view->insertItem("Rotate &counterclockwise", this, SLOT(rotateCounterClockwise()), Qt::CTRL+Qt::Key_PageUp);
       
   351     view->insertItem("&Zoom in", this, SLOT(zoomIn()), Qt::CTRL+Qt::Key_Plus);
       
   352     view->insertItem("Zoom &out", this, SLOT(zoomOut()), Qt::CTRL+Qt::Key_Minus);
       
   353     view->insertItem("Translate left", this, SLOT(moveL()), Qt::CTRL+Qt::Key_Left);
       
   354     view->insertItem("Translate right", this, SLOT(moveR()), Qt::CTRL+Qt::Key_Right);
       
   355     view->insertItem("Translate up", this, SLOT(moveU()), Qt::CTRL+Qt::Key_Up);
       
   356     view->insertItem("Translate down", this, SLOT(moveD()), Qt::CTRL+Qt::Key_Down);
       
   357     view->insertItem("&Mirror", this, SLOT(mirror()), Qt::CTRL+Qt::Key_Home);
       
   358     menu->insertItem("&View", view);
       
   359 
       
   360     menu->insertSeparator();
       
   361 
       
   362     Q3PopupMenu* help = new Q3PopupMenu( menu );
       
   363     help->insertItem("&About", this, SLOT(help()), Qt::Key_F1);
       
   364     help->setItemChecked(dbf_id, TRUE);
       
   365     menu->insertItem("&Help",help);
       
   366 
       
   367     statusBar();
       
   368 
       
   369     setCentralWidget(editor);
       
   370 
       
   371     printer = 0;
       
   372 
       
   373     init();
       
   374 }
       
   375 
       
   376 void Main::init()
       
   377 {
       
   378     clear();
       
   379 
       
   380     static int r=24;
       
   381     qsrand(++r);
       
   382 
       
   383     mainCount++;
       
   384     butterflyimg = 0;
       
   385     logoimg = 0;
       
   386 
       
   387     int i;
       
   388     for ( i=0; i < int(canvas.width()) / 56; i++) {
       
   389 	addButterfly();
       
   390     }
       
   391     for ( i=0; i < int(canvas.width()) / 85; i++) {
       
   392 	addHexagon();
       
   393     }
       
   394     for ( i=0; i < int(canvas.width()) / 128; i++) {
       
   395 	addLogo();
       
   396     }
       
   397 }
       
   398 
       
   399 Main::~Main()
       
   400 {
       
   401     delete printer;
       
   402     if ( !--mainCount ) {
       
   403 	delete[] butterflyimg;
       
   404 	butterflyimg = 0;
       
   405 	delete[] logoimg;
       
   406 	logoimg = 0;
       
   407     }
       
   408 }
       
   409 
       
   410 void Main::newView()
       
   411 {
       
   412     // Open a new view... have it delete when closed.
       
   413     Main *m = new Main(canvas, 0, 0, Qt::WDestructiveClose);
       
   414     m->show();
       
   415 }
       
   416 
       
   417 void Main::clear()
       
   418 {
       
   419     editor->clear();
       
   420 }
       
   421 
       
   422 void Main::help()
       
   423 {
       
   424     static QMessageBox* about = new QMessageBox( "Qt Canvas Example",
       
   425 	    "<h3>The QCanvas classes example</h3>"
       
   426 	    "<ul>"
       
   427 		"<li> Press ALT-S for some sprites."
       
   428 		"<li> Press ALT-C for some circles."
       
   429 		"<li> Press ALT-L for some lines."
       
   430 		"<li> Drag the objects around."
       
   431 		"<li> Read the code!"
       
   432 	    "</ul>", QMessageBox::Information, 1, 0, 0, this, 0, FALSE );
       
   433     about->setButtonText( 1, "Dismiss" );
       
   434     about->show();
       
   435 }
       
   436 
       
   437 void Main::aboutQt()
       
   438 {
       
   439     QMessageBox::aboutQt( this, "Qt Canvas Example" );
       
   440 }
       
   441 
       
   442 void Main::enlarge()
       
   443 {
       
   444     canvas.setSceneRect(0, 0, canvas.width()*4/3, canvas.height()*4/3);
       
   445 }
       
   446 
       
   447 void Main::shrink()
       
   448 {
       
   449     canvas.setSceneRect(0, 0, qMax(canvas.width()*3/4, qreal(1.0)), qMax(canvas.height()*3/4, qreal(1.0)));
       
   450 }
       
   451 
       
   452 void Main::rotateClockwise()
       
   453 {
       
   454     editor->rotate( 22.5 );
       
   455 }
       
   456 
       
   457 void Main::rotateCounterClockwise()
       
   458 {
       
   459     editor->rotate( -22.5 );
       
   460 }
       
   461 
       
   462 void Main::zoomIn()
       
   463 {
       
   464     editor->scale( 2.0, 2.0 );
       
   465 }
       
   466 
       
   467 void Main::zoomOut()
       
   468 {
       
   469     editor->scale( 0.5, 0.5 );
       
   470 }
       
   471 
       
   472 void Main::mirror()
       
   473 {
       
   474     editor->scale( -1, 1 );
       
   475 }
       
   476 
       
   477 void Main::moveL()
       
   478 {
       
   479     editor->translate( -16, 0 );
       
   480 }
       
   481 
       
   482 void Main::moveR()
       
   483 {
       
   484     editor->translate( +16, 0 );
       
   485 }
       
   486 
       
   487 void Main::moveU()
       
   488 {
       
   489     editor->translate( 0, -16 );
       
   490 }
       
   491 
       
   492 void Main::moveD()
       
   493 {
       
   494     editor->translate( 0, +16 );
       
   495 }
       
   496 
       
   497 void Main::print()
       
   498 {
       
   499     if ( !printer ) printer = new QPrinter;
       
   500     if ( printer->setup(this) ) {
       
   501 	QPainter pp(printer);
       
   502         canvas.render(&pp);
       
   503     }
       
   504 }
       
   505 
       
   506 
       
   507 void Main::addSprite()
       
   508 {
       
   509     BouncyLogo* i = new BouncyLogo;
       
   510     canvas.addItem(i);
       
   511     i->initPos();
       
   512     i->setZValue(qrand()%256);
       
   513 }
       
   514 
       
   515 QString butterfly_fn;
       
   516 QString logo_fn;
       
   517 
       
   518 
       
   519 void Main::addButterfly()
       
   520 {
       
   521     if ( butterfly_fn.isEmpty() )
       
   522 	return;
       
   523     if ( !butterflyimg ) {
       
   524 	butterflyimg = new QImage[4];
       
   525 	butterflyimg[0].load( butterfly_fn );
       
   526 	butterflyimg[1] = butterflyimg[0].smoothScale( int(butterflyimg[0].width()*0.75),
       
   527 		int(butterflyimg[0].height()*0.75) );
       
   528 	butterflyimg[2] = butterflyimg[0].smoothScale( int(butterflyimg[0].width()*0.5),
       
   529 		int(butterflyimg[0].height()*0.5) );
       
   530 	butterflyimg[3] = butterflyimg[0].smoothScale( int(butterflyimg[0].width()*0.25),
       
   531 		int(butterflyimg[0].height()*0.25) );
       
   532     }
       
   533     QAbstractGraphicsShapeItem* i = new ImageItem(butterflyimg[qrand()%4]);
       
   534     canvas.addItem(i);
       
   535     i->setPos(qrand()%int(canvas.width()-butterflyimg->width()),
       
   536 	    qrand()%int(canvas.height()-butterflyimg->height()));
       
   537     i->setZValue(qrand()%256+250);
       
   538 }
       
   539 
       
   540 void Main::addLogo()
       
   541 {
       
   542     if ( logo_fn.isEmpty() )
       
   543 	return;
       
   544     if ( !logoimg ) {
       
   545 	logoimg = new QImage[4];
       
   546 	logoimg[0].load( logo_fn );
       
   547 	logoimg[1] = logoimg[0].smoothScale( int(logoimg[0].width()*0.75),
       
   548 		int(logoimg[0].height()*0.75) );
       
   549 	logoimg[2] = logoimg[0].smoothScale( int(logoimg[0].width()*0.5),
       
   550 		int(logoimg[0].height()*0.5) );
       
   551 	logoimg[3] = logoimg[0].smoothScale( int(logoimg[0].width()*0.25),
       
   552 		int(logoimg[0].height()*0.25) );
       
   553     }
       
   554     QAbstractGraphicsShapeItem* i = new ImageItem(logoimg[qrand()%4]);
       
   555     canvas.addItem(i);
       
   556     i->setPos(qrand()%int(canvas.width()-logoimg->width()),
       
   557 	    qrand()%int(canvas.height()-logoimg->width()));
       
   558     i->setZValue(qrand()%256+256);
       
   559 }
       
   560 
       
   561 
       
   562 
       
   563 void Main::addCircle()
       
   564 {
       
   565     QAbstractGraphicsShapeItem* i = canvas.addEllipse(QRectF(0,0,50,50));
       
   566     i->setFlag(QGraphicsItem::ItemIsMovable);
       
   567     i->setPen(Qt::NoPen);
       
   568     i->setBrush( QColor(qrand()%32*8,qrand()%32*8,qrand()%32*8) );
       
   569     i->setPos(qrand()%int(canvas.width()),qrand()%int(canvas.height()));
       
   570     i->setZValue(qrand()%256);
       
   571 }
       
   572 
       
   573 void Main::addHexagon()
       
   574 {
       
   575     const int size = int(canvas.width() / 25);
       
   576     Q3PointArray pa(6);
       
   577     pa[0] = QPoint(2*size,0);
       
   578     pa[1] = QPoint(size,-size*173/100);
       
   579     pa[2] = QPoint(-size,-size*173/100);
       
   580     pa[3] = QPoint(-2*size,0);
       
   581     pa[4] = QPoint(-size,size*173/100);
       
   582     pa[5] = QPoint(size,size*173/100);
       
   583     QGraphicsPolygonItem* i = canvas.addPolygon(pa);
       
   584     i->setFlag(QGraphicsItem::ItemIsMovable);
       
   585     i->setPen(Qt::NoPen);
       
   586     i->setBrush( QColor(qrand()%32*8,qrand()%32*8,qrand()%32*8) );
       
   587     i->setPos(qrand()%int(canvas.width()),qrand()%int(canvas.height()));
       
   588     i->setZValue(qrand()%256);
       
   589 }
       
   590 
       
   591 void Main::addPolygon()
       
   592 {
       
   593     const int size = int(canvas.width()/2);
       
   594     Q3PointArray pa(6);
       
   595     pa[0] = QPoint(0,0);
       
   596     pa[1] = QPoint(size,size/5);
       
   597     pa[2] = QPoint(size*4/5,size);
       
   598     pa[3] = QPoint(size/6,size*5/4);
       
   599     pa[4] = QPoint(size*3/4,size*3/4);
       
   600     pa[5] = QPoint(size*3/4,size/4);
       
   601     QGraphicsPolygonItem* i = canvas.addPolygon(pa);
       
   602     i->setFlag(QGraphicsItem::ItemIsMovable);
       
   603     i->setPen(Qt::NoPen);
       
   604     i->setBrush( QColor(qrand()%32*8,qrand()%32*8,qrand()%32*8) );
       
   605     i->setPos(qrand()%int(canvas.width()),qrand()%int(canvas.height()));
       
   606     i->setZValue(qrand()%256);
       
   607 }
       
   608 
       
   609 void Main::addSpline()
       
   610 {
       
   611     const int size = int(canvas.width()/6);
       
   612 
       
   613     Q3PointArray pa(12);
       
   614     pa[0] = QPoint(0,0);
       
   615     pa[1] = QPoint(size/2,0);
       
   616     pa[2] = QPoint(size,size/2);
       
   617     pa[3] = QPoint(size,size);
       
   618     pa[4] = QPoint(size,size*3/2);
       
   619     pa[5] = QPoint(size/2,size*2);
       
   620     pa[6] = QPoint(0,size*2);
       
   621     pa[7] = QPoint(-size/2,size*2);
       
   622     pa[8] = QPoint(size/4,size*3/2);
       
   623     pa[9] = QPoint(0,size);
       
   624     pa[10]= QPoint(-size/4,size/2);
       
   625     pa[11]= QPoint(-size/2,0);
       
   626 
       
   627     QPainterPath path;
       
   628     path.moveTo(pa[0]);
       
   629     for (int i = 1; i < pa.size(); i += 3)
       
   630         path.cubicTo(pa[i], pa[(i + 1) % pa.size()], pa[(i + 2) % pa.size()]);
       
   631 
       
   632     QGraphicsPathItem* item = canvas.addPath(path);
       
   633     item->setFlag(QGraphicsItem::ItemIsMovable);
       
   634     item->setPen(Qt::NoPen);
       
   635     item->setBrush( QColor(qrand()%32*8,qrand()%32*8,qrand()%32*8) );
       
   636     item->setPos(qrand()%int(canvas.width()),qrand()%int(canvas.height()));
       
   637     item->setZValue(qrand()%256);
       
   638 }
       
   639 
       
   640 void Main::addText()
       
   641 {
       
   642     QGraphicsTextItem* i = canvas.addText("QCanvasText");
       
   643     i->setFlag(QGraphicsItem::ItemIsMovable);
       
   644     i->setPos(qrand()%int(canvas.width()),qrand()%int(canvas.height()));
       
   645     i->setZValue(qrand()%256);
       
   646 }
       
   647 
       
   648 void Main::addLine()
       
   649 {
       
   650     QGraphicsLineItem* i = canvas.addLine(QLineF( qrand()%int(canvas.width()), qrand()%int(canvas.height()),
       
   651                                                   qrand()%int(canvas.width()), qrand()%int(canvas.height()) ));
       
   652     i->setFlag(QGraphicsItem::ItemIsMovable);
       
   653     i->setPen( QPen(QColor(qrand()%32*8,qrand()%32*8,qrand()%32*8), 6) );
       
   654     i->setZValue(qrand()%256);
       
   655 }
       
   656 
       
   657 void Main::addMesh()
       
   658 {
       
   659     int x0 = 0;
       
   660     int y0 = 0;
       
   661 
       
   662     if ( !tb ) tb = new QBrush( Qt::red );
       
   663     if ( !tp ) tp = new QPen( Qt::black );
       
   664 
       
   665     int nodecount = 0;
       
   666 
       
   667     int w = int(canvas.width());
       
   668     int h = int(canvas.height());
       
   669 
       
   670     const int dist = 30;
       
   671     int rows = h / dist;
       
   672     int cols = w / dist;
       
   673 
       
   674 #ifndef QT_NO_PROGRESSDIALOG
       
   675     Q3ProgressDialog progress( "Creating mesh...", "Abort", rows,
       
   676 			      this, "progress", TRUE );
       
   677 #endif
       
   678 
       
   679     canvas.update();
       
   680     
       
   681     Q3MemArray<NodeItem*> lastRow(cols);
       
   682     for ( int j = 0; j < rows; j++ ) {
       
   683 	int n = j%2 ? cols-1 : cols;
       
   684 	NodeItem *prev = 0;
       
   685 	for ( int i = 0; i < n; i++ ) {
       
   686 	    NodeItem *el = new NodeItem;
       
   687             canvas.addItem(el);
       
   688 	    nodecount++;
       
   689 	    int r = qrand();
       
   690 	    int xrand = r %20;
       
   691 	    int yrand = (r/20) %20;
       
   692 	    el->setPos( xrand + x0 + i*dist + (j%2 ? dist/2 : 0 ),
       
   693                         yrand + y0 + j*dist );
       
   694 
       
   695 	    if ( j > 0 ) {
       
   696 		if ( i < cols-1 )
       
   697 		    canvas.addItem(new EdgeItem( lastRow[i], el));
       
   698 		if ( j%2 )
       
   699 		    canvas.addItem(new EdgeItem( lastRow[i+1], el));
       
   700 		else if ( i > 0 )
       
   701 		    canvas.addItem(new EdgeItem( lastRow[i-1], el));
       
   702 	    }
       
   703 	    if ( prev ) {
       
   704 		canvas.addItem(new EdgeItem( prev, el));
       
   705 	    }
       
   706 	    if ( i > 0 ) lastRow[i-1] = prev;
       
   707 	    prev = el;
       
   708 	}
       
   709 	lastRow[n-1]=prev;
       
   710 #ifndef QT_NO_PROGRESSDIALOG
       
   711 	progress.setProgress( j );
       
   712 	if ( progress.wasCancelled() )
       
   713 	    break;
       
   714 #endif
       
   715     }
       
   716 #ifndef QT_NO_PROGRESSDIALOG
       
   717     progress.setProgress( rows );
       
   718 #endif
       
   719     // qDebug( "%d nodes, %d edges", nodecount, EdgeItem::count() );
       
   720 }
       
   721 
       
   722 void Main::addRectangle()
       
   723 {
       
   724     QAbstractGraphicsShapeItem *i = canvas.addRect( QRectF(qrand()%int(canvas.width()),
       
   725                                                           qrand()%int(canvas.height()),
       
   726                                                           canvas.width()/5,
       
   727                                                           canvas.width()/5) );
       
   728     i->setFlag(QGraphicsItem::ItemIsMovable);
       
   729     int z = qrand()%256;
       
   730     i->setBrush( QColor(z,z,z) );
       
   731     i->setPen( QPen(QColor(qrand()%32*8,qrand()%32*8,qrand()%32*8), 6) );
       
   732     i->setZValue(z);
       
   733 }