source: trunk/examples/graphicsview/portedcanvas/canvas.cpp@ 875

Last change on this file since 875 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

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