[67] | 1 | #include <qdatetime.h>
|
---|
| 2 | #include <qmainwindow.h>
|
---|
| 3 | #include <qstatusbar.h>
|
---|
| 4 | #include <qmessagebox.h>
|
---|
| 5 | #include <qmenubar.h>
|
---|
| 6 | #include <qapplication.h>
|
---|
| 7 | #include <qpainter.h>
|
---|
| 8 | #include <qprinter.h>
|
---|
| 9 | #include <qlabel.h>
|
---|
| 10 | #include <qimage.h>
|
---|
| 11 | #include <qprogressdialog.h>
|
---|
| 12 | #include "canvas.h"
|
---|
| 13 |
|
---|
| 14 | #include <stdlib.h>
|
---|
| 15 |
|
---|
| 16 | // We use a global variable to save memory - all the brushes and pens in
|
---|
| 17 | // the mesh are shared.
|
---|
| 18 | static QBrush *tb = 0;
|
---|
| 19 | static QPen *tp = 0;
|
---|
| 20 |
|
---|
| 21 | class EdgeItem;
|
---|
| 22 | class NodeItem;
|
---|
| 23 |
|
---|
| 24 | class EdgeItem: public QCanvasLine
|
---|
| 25 | {
|
---|
| 26 | public:
|
---|
| 27 | EdgeItem( NodeItem*, NodeItem*, QCanvas *canvas );
|
---|
| 28 | void setFromPoint( int x, int y ) ;
|
---|
| 29 | void setToPoint( int x, int y );
|
---|
| 30 | static int count() { return c; }
|
---|
| 31 | void moveBy(double dx, double dy);
|
---|
| 32 | private:
|
---|
| 33 | static int c;
|
---|
| 34 | };
|
---|
| 35 |
|
---|
| 36 | static const int imageRTTI = 984376;
|
---|
| 37 |
|
---|
| 38 |
|
---|
| 39 | class ImageItem: public QCanvasRectangle
|
---|
| 40 | {
|
---|
| 41 | public:
|
---|
| 42 | ImageItem( QImage img, QCanvas *canvas );
|
---|
| 43 | int rtti () const { return imageRTTI; }
|
---|
| 44 | bool hit( const QPoint&) const;
|
---|
| 45 | protected:
|
---|
| 46 | void drawShape( QPainter & );
|
---|
| 47 | private:
|
---|
| 48 | QImage image;
|
---|
| 49 | QPixmap pixmap;
|
---|
| 50 | };
|
---|
| 51 |
|
---|
| 52 |
|
---|
| 53 | ImageItem::ImageItem( QImage img, QCanvas *canvas )
|
---|
| 54 | : QCanvasRectangle( canvas ), image(img)
|
---|
| 55 | {
|
---|
| 56 | setSize( image.width(), image.height() );
|
---|
| 57 |
|
---|
| 58 | #if !defined(Q_WS_QWS)
|
---|
| 59 | pixmap.convertFromImage(image, OrderedAlphaDither);
|
---|
| 60 | #endif
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 |
|
---|
| 64 | void ImageItem::drawShape( QPainter &p )
|
---|
| 65 | {
|
---|
| 66 | // On Qt/Embedded, we can paint a QImage as fast as a QPixmap,
|
---|
| 67 | // but on other platforms, we need to use a QPixmap.
|
---|
| 68 | #if defined(Q_WS_QWS)
|
---|
| 69 | p.drawImage( int(x()), int(y()), image, 0, 0, -1, -1, OrderedAlphaDither );
|
---|
| 70 | #else
|
---|
| 71 | p.drawPixmap( int(x()), int(y()), pixmap );
|
---|
| 72 | #endif
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | bool ImageItem::hit( const QPoint &p ) const
|
---|
| 76 | {
|
---|
| 77 | int ix = p.x()-int(x());
|
---|
| 78 | int iy = p.y()-int(y());
|
---|
| 79 | if ( !image.valid( ix , iy ) )
|
---|
| 80 | return FALSE;
|
---|
| 81 | QRgb pixel = image.pixel( ix, iy );
|
---|
| 82 | return qAlpha( pixel ) != 0;
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | class NodeItem: public QCanvasEllipse
|
---|
| 86 | {
|
---|
| 87 | public:
|
---|
| 88 | NodeItem( QCanvas *canvas );
|
---|
| 89 | ~NodeItem() {}
|
---|
| 90 |
|
---|
| 91 | void addInEdge( EdgeItem *edge ) { inList.append( edge ); }
|
---|
| 92 | void addOutEdge( EdgeItem *edge ) { outList.append( edge ); }
|
---|
| 93 |
|
---|
| 94 | void moveBy(double dx, double dy);
|
---|
| 95 |
|
---|
| 96 | // QPoint center() { return boundingRect().center(); }
|
---|
| 97 | private:
|
---|
| 98 | QPtrList<EdgeItem> inList;
|
---|
| 99 | QPtrList<EdgeItem> outList;
|
---|
| 100 | };
|
---|
| 101 |
|
---|
| 102 |
|
---|
| 103 | int EdgeItem::c = 0;
|
---|
| 104 |
|
---|
| 105 |
|
---|
| 106 | void EdgeItem::moveBy(double, double)
|
---|
| 107 | {
|
---|
| 108 | //nothing
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | EdgeItem::EdgeItem( NodeItem *from, NodeItem *to, QCanvas *canvas )
|
---|
| 112 | : QCanvasLine( canvas )
|
---|
| 113 | {
|
---|
| 114 | c++;
|
---|
| 115 | setPen( *tp );
|
---|
| 116 | setBrush( *tb );
|
---|
| 117 | from->addOutEdge( this );
|
---|
| 118 | to->addInEdge( this );
|
---|
| 119 | setPoints( int(from->x()), int(from->y()), int(to->x()), int(to->y()) );
|
---|
| 120 | setZ( 127 );
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | void EdgeItem::setFromPoint( int x, int y )
|
---|
| 124 | {
|
---|
| 125 | setPoints( x,y, endPoint().x(), endPoint().y() );
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | void EdgeItem::setToPoint( int x, int y )
|
---|
| 129 | {
|
---|
| 130 | setPoints( startPoint().x(), startPoint().y(), x, y );
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 |
|
---|
| 134 |
|
---|
| 135 | void NodeItem::moveBy(double dx, double dy)
|
---|
| 136 | {
|
---|
| 137 | QCanvasEllipse::moveBy( dx, dy );
|
---|
| 138 |
|
---|
| 139 | QPtrListIterator<EdgeItem> it1( inList );
|
---|
| 140 | EdgeItem *edge;
|
---|
| 141 | while (( edge = it1.current() )) {
|
---|
| 142 | ++it1;
|
---|
| 143 | edge->setToPoint( int(x()), int(y()) );
|
---|
| 144 | }
|
---|
| 145 | QPtrListIterator<EdgeItem> it2( outList );
|
---|
| 146 | while (( edge = it2.current() )) {
|
---|
| 147 | ++it2;
|
---|
| 148 | edge->setFromPoint( int(x()), int(y()) );
|
---|
| 149 | }
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | NodeItem::NodeItem( QCanvas *canvas )
|
---|
| 153 | : QCanvasEllipse( 6, 6, canvas )
|
---|
| 154 | {
|
---|
| 155 | setPen( *tp );
|
---|
| 156 | setBrush( *tb );
|
---|
| 157 | setZ( 128 );
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | FigureEditor::FigureEditor(
|
---|
| 161 | QCanvas& c, QWidget* parent,
|
---|
| 162 | const char* name, WFlags f) :
|
---|
| 163 | QCanvasView(&c,parent,name,f)
|
---|
| 164 | {
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | void FigureEditor::contentsMousePressEvent(QMouseEvent* e)
|
---|
| 168 | {
|
---|
| 169 | QPoint p = inverseWorldMatrix().map(e->pos());
|
---|
| 170 | QCanvasItemList l=canvas()->collisions(p);
|
---|
| 171 | for (QCanvasItemList::Iterator it=l.begin(); it!=l.end(); ++it) {
|
---|
| 172 | if ( (*it)->rtti() == imageRTTI ) {
|
---|
| 173 | ImageItem *item= (ImageItem*)(*it);
|
---|
| 174 | if ( !item->hit( p ) )
|
---|
| 175 | continue;
|
---|
| 176 | }
|
---|
| 177 | moving = *it;
|
---|
| 178 | moving_start = p;
|
---|
| 179 | return;
|
---|
| 180 | }
|
---|
| 181 | moving = 0;
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | void FigureEditor::clear()
|
---|
| 185 | {
|
---|
| 186 | QCanvasItemList list = canvas()->allItems();
|
---|
| 187 | QCanvasItemList::Iterator it = list.begin();
|
---|
| 188 | for (; it != list.end(); ++it) {
|
---|
| 189 | if ( *it )
|
---|
| 190 | delete *it;
|
---|
| 191 | }
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | void FigureEditor::contentsMouseMoveEvent(QMouseEvent* e)
|
---|
| 195 | {
|
---|
| 196 | if ( moving ) {
|
---|
| 197 | QPoint p = inverseWorldMatrix().map(e->pos());
|
---|
| 198 | moving->moveBy(p.x() - moving_start.x(),
|
---|
| 199 | p.y() - moving_start.y());
|
---|
| 200 | moving_start = p;
|
---|
| 201 | canvas()->update();
|
---|
| 202 | }
|
---|
| 203 | }
|
---|
| 204 |
|
---|
| 205 |
|
---|
| 206 |
|
---|
| 207 | BouncyLogo::BouncyLogo(QCanvas* canvas) :
|
---|
| 208 | QCanvasSprite(0,canvas)
|
---|
| 209 | {
|
---|
| 210 | static QCanvasPixmapArray logo("qt-trans.xpm");
|
---|
| 211 | setSequence(&logo);
|
---|
| 212 | setAnimated(TRUE);
|
---|
| 213 | initPos();
|
---|
| 214 | }
|
---|
| 215 |
|
---|
| 216 |
|
---|
| 217 | const int logo_rtti = 1234;
|
---|
| 218 |
|
---|
| 219 | int BouncyLogo::rtti() const
|
---|
| 220 | {
|
---|
| 221 | return logo_rtti;
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 | void BouncyLogo::initPos()
|
---|
| 225 | {
|
---|
| 226 | initSpeed();
|
---|
| 227 | int trial=1000;
|
---|
| 228 | do {
|
---|
| 229 | move(rand()%canvas()->width(),rand()%canvas()->height());
|
---|
| 230 | advance(0);
|
---|
| 231 | } while (trial-- && xVelocity()==0.0 && yVelocity()==0.0);
|
---|
| 232 | }
|
---|
| 233 |
|
---|
| 234 | void BouncyLogo::initSpeed()
|
---|
| 235 | {
|
---|
| 236 | const double speed = 4.0;
|
---|
| 237 | double d = (double)(rand()%1024) / 1024.0;
|
---|
| 238 | setVelocity( d*speed*2-speed, (1-d)*speed*2-speed );
|
---|
| 239 | }
|
---|
| 240 |
|
---|
| 241 | void BouncyLogo::advance(int stage)
|
---|
| 242 | {
|
---|
| 243 | switch ( stage ) {
|
---|
| 244 | case 0: {
|
---|
| 245 | double vx = xVelocity();
|
---|
| 246 | double vy = yVelocity();
|
---|
| 247 |
|
---|
| 248 | if ( vx == 0.0 && vy == 0.0 ) {
|
---|
| 249 | // stopped last turn
|
---|
| 250 | initSpeed();
|
---|
| 251 | vx = xVelocity();
|
---|
| 252 | vy = yVelocity();
|
---|
| 253 | }
|
---|
| 254 |
|
---|
| 255 | double nx = x() + vx;
|
---|
| 256 | double ny = y() + vy;
|
---|
| 257 |
|
---|
| 258 | if ( nx < 0 || nx >= canvas()->width() )
|
---|
| 259 | vx = -vx;
|
---|
| 260 | if ( ny < 0 || ny >= canvas()->height() )
|
---|
| 261 | vy = -vy;
|
---|
| 262 |
|
---|
| 263 | for (int bounce=0; bounce<4; bounce++) {
|
---|
| 264 | QCanvasItemList l=collisions(FALSE);
|
---|
| 265 | for (QCanvasItemList::Iterator it=l.begin(); it!=l.end(); ++it) {
|
---|
| 266 | QCanvasItem *hit = *it;
|
---|
| 267 | if ( hit->rtti()==logo_rtti && hit->collidesWith(this) ) {
|
---|
| 268 | switch ( bounce ) {
|
---|
| 269 | case 0:
|
---|
| 270 | vx = -vx;
|
---|
| 271 | break;
|
---|
| 272 | case 1:
|
---|
| 273 | vy = -vy;
|
---|
| 274 | vx = -vx;
|
---|
| 275 | break;
|
---|
| 276 | case 2:
|
---|
| 277 | vx = -vx;
|
---|
| 278 | break;
|
---|
| 279 | case 3:
|
---|
| 280 | // Stop for this turn
|
---|
| 281 | vx = 0;
|
---|
| 282 | vy = 0;
|
---|
| 283 | break;
|
---|
| 284 | }
|
---|
| 285 | setVelocity(vx,vy);
|
---|
| 286 | break;
|
---|
| 287 | }
|
---|
| 288 | }
|
---|
| 289 | }
|
---|
| 290 |
|
---|
| 291 | if ( x()+vx < 0 || x()+vx >= canvas()->width() )
|
---|
| 292 | vx = 0;
|
---|
| 293 | if ( y()+vy < 0 || y()+vy >= canvas()->height() )
|
---|
| 294 | vy = 0;
|
---|
| 295 |
|
---|
| 296 | setVelocity(vx,vy);
|
---|
| 297 | } break;
|
---|
| 298 | case 1:
|
---|
| 299 | QCanvasItem::advance(stage);
|
---|
| 300 | break;
|
---|
| 301 | }
|
---|
| 302 | }
|
---|
| 303 |
|
---|
| 304 | static uint mainCount = 0;
|
---|
| 305 | static QImage *butterflyimg;
|
---|
| 306 | static QImage *logoimg;
|
---|
| 307 |
|
---|
| 308 | Main::Main(QCanvas& c, QWidget* parent, const char* name, WFlags f) :
|
---|
| 309 | QMainWindow(parent,name,f),
|
---|
| 310 | canvas(c)
|
---|
| 311 | {
|
---|
| 312 | editor = new FigureEditor(canvas,this);
|
---|
| 313 | QMenuBar* menu = menuBar();
|
---|
| 314 |
|
---|
| 315 | QPopupMenu* file = new QPopupMenu( menu );
|
---|
| 316 | file->insertItem("&Fill canvas", this, SLOT(init()), CTRL+Key_F);
|
---|
| 317 | file->insertItem("&Erase canvas", this, SLOT(clear()), CTRL+Key_E);
|
---|
| 318 | file->insertItem("&New view", this, SLOT(newView()), CTRL+Key_N);
|
---|
| 319 | file->insertSeparator();
|
---|
[70] | 320 | #ifndef QT_NO_PRINTER
|
---|
[67] | 321 | file->insertItem("&Print...", this, SLOT(print()), CTRL+Key_P);
|
---|
| 322 | file->insertSeparator();
|
---|
[70] | 323 | #endif
|
---|
[67] | 324 | file->insertItem("E&xit", qApp, SLOT(quit()), CTRL+Key_Q);
|
---|
| 325 | menu->insertItem("&File", file);
|
---|
| 326 |
|
---|
| 327 | QPopupMenu* edit = new QPopupMenu( menu );
|
---|
| 328 | edit->insertItem("Add &Circle", this, SLOT(addCircle()), ALT+Key_C);
|
---|
| 329 | edit->insertItem("Add &Hexagon", this, SLOT(addHexagon()), ALT+Key_H);
|
---|
| 330 | edit->insertItem("Add &Polygon", this, SLOT(addPolygon()), ALT+Key_P);
|
---|
| 331 | edit->insertItem("Add Spl&ine", this, SLOT(addSpline()), ALT+Key_I);
|
---|
| 332 | edit->insertItem("Add &Text", this, SLOT(addText()), ALT+Key_T);
|
---|
| 333 | edit->insertItem("Add &Line", this, SLOT(addLine()), ALT+Key_L);
|
---|
| 334 | edit->insertItem("Add &Rectangle", this, SLOT(addRectangle()), ALT+Key_R);
|
---|
| 335 | edit->insertItem("Add &Sprite", this, SLOT(addSprite()), ALT+Key_S);
|
---|
| 336 | edit->insertItem("Create &Mesh", this, SLOT(addMesh()), ALT+Key_M );
|
---|
| 337 | edit->insertItem("Add &Alpha-blended image", this, SLOT(addButterfly()), ALT+Key_A);
|
---|
| 338 | menu->insertItem("&Edit", edit);
|
---|
| 339 |
|
---|
| 340 | QPopupMenu* view = new QPopupMenu( menu );
|
---|
| 341 | view->insertItem("&Enlarge", this, SLOT(enlarge()), SHIFT+CTRL+Key_Plus);
|
---|
| 342 | view->insertItem("Shr&ink", this, SLOT(shrink()), SHIFT+CTRL+Key_Minus);
|
---|
| 343 | view->insertSeparator();
|
---|
| 344 | view->insertItem("&Rotate clockwise", this, SLOT(rotateClockwise()), CTRL+Key_PageDown);
|
---|
| 345 | view->insertItem("Rotate &counterclockwise", this, SLOT(rotateCounterClockwise()), CTRL+Key_PageUp);
|
---|
| 346 | view->insertItem("&Zoom in", this, SLOT(zoomIn()), CTRL+Key_Plus);
|
---|
| 347 | view->insertItem("Zoom &out", this, SLOT(zoomOut()), CTRL+Key_Minus);
|
---|
| 348 | view->insertItem("Translate left", this, SLOT(moveL()), CTRL+Key_Left);
|
---|
| 349 | view->insertItem("Translate right", this, SLOT(moveR()), CTRL+Key_Right);
|
---|
| 350 | view->insertItem("Translate up", this, SLOT(moveU()), CTRL+Key_Up);
|
---|
| 351 | view->insertItem("Translate down", this, SLOT(moveD()), CTRL+Key_Down);
|
---|
| 352 | view->insertItem("&Mirror", this, SLOT(mirror()), CTRL+Key_Home);
|
---|
| 353 | menu->insertItem("&View", view);
|
---|
| 354 |
|
---|
| 355 | options = new QPopupMenu( menu );
|
---|
| 356 | dbf_id = options->insertItem("Double buffer", this, SLOT(toggleDoubleBuffer()));
|
---|
| 357 | options->setItemChecked(dbf_id, TRUE);
|
---|
| 358 | menu->insertItem("&Options",options);
|
---|
| 359 |
|
---|
| 360 | menu->insertSeparator();
|
---|
| 361 |
|
---|
| 362 | QPopupMenu* help = new QPopupMenu( menu );
|
---|
| 363 | help->insertItem("&About", this, SLOT(help()), 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 | srand(++r);
|
---|
| 382 |
|
---|
| 383 | mainCount++;
|
---|
| 384 | butterflyimg = 0;
|
---|
| 385 | logoimg = 0;
|
---|
| 386 |
|
---|
| 387 | int i;
|
---|
| 388 | for ( i=0; i<canvas.width() / 56; i++) {
|
---|
| 389 | addButterfly();
|
---|
| 390 | }
|
---|
| 391 | for ( i=0; i<canvas.width() / 85; i++) {
|
---|
| 392 | addHexagon();
|
---|
| 393 | }
|
---|
| 394 | for ( i=0; i<canvas.width() / 128; i++) {
|
---|
| 395 | addLogo();
|
---|
| 396 | }
|
---|
| 397 | }
|
---|
| 398 |
|
---|
| 399 | Main::~Main()
|
---|
| 400 | {
|
---|
[70] | 401 | #ifndef QT_NO_PRINTER
|
---|
[67] | 402 | delete printer;
|
---|
[70] | 403 | #endif
|
---|
[67] | 404 | if ( !--mainCount ) {
|
---|
| 405 | delete[] butterflyimg;
|
---|
| 406 | butterflyimg = 0;
|
---|
| 407 | delete[] logoimg;
|
---|
| 408 | logoimg = 0;
|
---|
| 409 | }
|
---|
| 410 | }
|
---|
| 411 |
|
---|
| 412 | void Main::newView()
|
---|
| 413 | {
|
---|
| 414 | // Open a new view... have it delete when closed.
|
---|
| 415 | Main *m = new Main(canvas, 0, 0, WDestructiveClose);
|
---|
| 416 | qApp->setMainWidget(m);
|
---|
| 417 | m->show();
|
---|
| 418 | qApp->setMainWidget(0);
|
---|
| 419 | }
|
---|
| 420 |
|
---|
| 421 | void Main::clear()
|
---|
| 422 | {
|
---|
| 423 | editor->clear();
|
---|
| 424 | }
|
---|
| 425 |
|
---|
| 426 | void Main::help()
|
---|
| 427 | {
|
---|
| 428 | static QMessageBox* about = new QMessageBox( "Qt Canvas Example",
|
---|
| 429 | "<h3>The QCanvas classes example</h3>"
|
---|
| 430 | "<ul>"
|
---|
| 431 | "<li> Press ALT-S for some sprites."
|
---|
| 432 | "<li> Press ALT-C for some circles."
|
---|
| 433 | "<li> Press ALT-L for some lines."
|
---|
| 434 | "<li> Drag the objects around."
|
---|
| 435 | "<li> Read the code!"
|
---|
| 436 | "</ul>", QMessageBox::Information, 1, 0, 0, this, 0, FALSE );
|
---|
| 437 | about->setButtonText( 1, "Dismiss" );
|
---|
| 438 | about->show();
|
---|
| 439 | }
|
---|
| 440 |
|
---|
| 441 | void Main::aboutQt()
|
---|
| 442 | {
|
---|
| 443 | QMessageBox::aboutQt( this, "Qt Canvas Example" );
|
---|
| 444 | }
|
---|
| 445 |
|
---|
| 446 | void Main::toggleDoubleBuffer()
|
---|
| 447 | {
|
---|
| 448 | bool s = !options->isItemChecked(dbf_id);
|
---|
| 449 | options->setItemChecked(dbf_id,s);
|
---|
| 450 | canvas.setDoubleBuffering(s);
|
---|
| 451 | }
|
---|
| 452 |
|
---|
| 453 | void Main::enlarge()
|
---|
| 454 | {
|
---|
| 455 | canvas.resize(canvas.width()*4/3, canvas.height()*4/3);
|
---|
| 456 | }
|
---|
| 457 |
|
---|
| 458 | void Main::shrink()
|
---|
| 459 | {
|
---|
| 460 | canvas.resize(canvas.width()*3/4, canvas.height()*3/4);
|
---|
| 461 | }
|
---|
| 462 |
|
---|
| 463 | void Main::rotateClockwise()
|
---|
| 464 | {
|
---|
| 465 | QWMatrix m = editor->worldMatrix();
|
---|
| 466 | m.rotate( 22.5 );
|
---|
| 467 | editor->setWorldMatrix( m );
|
---|
| 468 | }
|
---|
| 469 |
|
---|
| 470 | void Main::rotateCounterClockwise()
|
---|
| 471 | {
|
---|
| 472 | QWMatrix m = editor->worldMatrix();
|
---|
| 473 | m.rotate( -22.5 );
|
---|
| 474 | editor->setWorldMatrix( m );
|
---|
| 475 | }
|
---|
| 476 |
|
---|
| 477 | void Main::zoomIn()
|
---|
| 478 | {
|
---|
| 479 | QWMatrix m = editor->worldMatrix();
|
---|
| 480 | m.scale( 2.0, 2.0 );
|
---|
| 481 | editor->setWorldMatrix( m );
|
---|
| 482 | }
|
---|
| 483 |
|
---|
| 484 | void Main::zoomOut()
|
---|
| 485 | {
|
---|
| 486 | QWMatrix m = editor->worldMatrix();
|
---|
| 487 | m.scale( 0.5, 0.5 );
|
---|
| 488 | editor->setWorldMatrix( m );
|
---|
| 489 | }
|
---|
| 490 |
|
---|
| 491 | void Main::mirror()
|
---|
| 492 | {
|
---|
| 493 | QWMatrix m = editor->worldMatrix();
|
---|
| 494 | m.scale( -1, 1 );
|
---|
| 495 | editor->setWorldMatrix( m );
|
---|
| 496 | }
|
---|
| 497 |
|
---|
| 498 | void Main::moveL()
|
---|
| 499 | {
|
---|
| 500 | QWMatrix m = editor->worldMatrix();
|
---|
| 501 | m.translate( -16, 0 );
|
---|
| 502 | editor->setWorldMatrix( m );
|
---|
| 503 | }
|
---|
| 504 |
|
---|
| 505 | void Main::moveR()
|
---|
| 506 | {
|
---|
| 507 | QWMatrix m = editor->worldMatrix();
|
---|
| 508 | m.translate( +16, 0 );
|
---|
| 509 | editor->setWorldMatrix( m );
|
---|
| 510 | }
|
---|
| 511 |
|
---|
| 512 | void Main::moveU()
|
---|
| 513 | {
|
---|
| 514 | QWMatrix m = editor->worldMatrix();
|
---|
| 515 | m.translate( 0, -16 );
|
---|
| 516 | editor->setWorldMatrix( m );
|
---|
| 517 | }
|
---|
| 518 |
|
---|
| 519 | void Main::moveD()
|
---|
| 520 | {
|
---|
| 521 | QWMatrix m = editor->worldMatrix();
|
---|
| 522 | m.translate( 0, +16 );
|
---|
| 523 | editor->setWorldMatrix( m );
|
---|
| 524 | }
|
---|
| 525 |
|
---|
| 526 | void Main::print()
|
---|
| 527 | {
|
---|
[70] | 528 | #ifndef QT_NO_PRINTER
|
---|
[67] | 529 | if ( !printer ) printer = new QPrinter;
|
---|
| 530 | if ( printer->setup(this) ) {
|
---|
| 531 | QPainter pp(printer);
|
---|
| 532 | canvas.drawArea(QRect(0,0,canvas.width(),canvas.height()),&pp,FALSE);
|
---|
| 533 | }
|
---|
[70] | 534 | #endif
|
---|
[67] | 535 | }
|
---|
| 536 |
|
---|
| 537 |
|
---|
| 538 | void Main::addSprite()
|
---|
| 539 | {
|
---|
| 540 | QCanvasItem* i = new BouncyLogo(&canvas);
|
---|
| 541 | i->setZ(rand()%256);
|
---|
| 542 | i->show();
|
---|
| 543 | }
|
---|
| 544 |
|
---|
| 545 | QString butterfly_fn;
|
---|
| 546 | QString logo_fn;
|
---|
| 547 |
|
---|
| 548 |
|
---|
| 549 | void Main::addButterfly()
|
---|
| 550 | {
|
---|
| 551 | if ( butterfly_fn.isEmpty() )
|
---|
| 552 | return;
|
---|
| 553 | if ( !butterflyimg ) {
|
---|
| 554 | butterflyimg = new QImage[4];
|
---|
| 555 | butterflyimg[0].load( butterfly_fn );
|
---|
| 556 | butterflyimg[1] = butterflyimg[0].smoothScale( int(butterflyimg[0].width()*0.75),
|
---|
| 557 | int(butterflyimg[0].height()*0.75) );
|
---|
| 558 | butterflyimg[2] = butterflyimg[0].smoothScale( int(butterflyimg[0].width()*0.5),
|
---|
| 559 | int(butterflyimg[0].height()*0.5) );
|
---|
| 560 | butterflyimg[3] = butterflyimg[0].smoothScale( int(butterflyimg[0].width()*0.25),
|
---|
| 561 | int(butterflyimg[0].height()*0.25) );
|
---|
| 562 | }
|
---|
| 563 | QCanvasPolygonalItem* i = new ImageItem(butterflyimg[rand()%4],&canvas);
|
---|
| 564 | i->move(rand()%(canvas.width()-butterflyimg->width()),
|
---|
| 565 | rand()%(canvas.height()-butterflyimg->height()));
|
---|
| 566 | i->setZ(rand()%256+250);
|
---|
| 567 | i->show();
|
---|
| 568 | }
|
---|
| 569 |
|
---|
| 570 | void Main::addLogo()
|
---|
| 571 | {
|
---|
| 572 | if ( logo_fn.isEmpty() )
|
---|
| 573 | return;
|
---|
| 574 | if ( !logoimg ) {
|
---|
| 575 | logoimg = new QImage[4];
|
---|
| 576 | logoimg[0].load( logo_fn );
|
---|
| 577 | logoimg[1] = logoimg[0].smoothScale( int(logoimg[0].width()*0.75),
|
---|
| 578 | int(logoimg[0].height()*0.75) );
|
---|
| 579 | logoimg[2] = logoimg[0].smoothScale( int(logoimg[0].width()*0.5),
|
---|
| 580 | int(logoimg[0].height()*0.5) );
|
---|
| 581 | logoimg[3] = logoimg[0].smoothScale( int(logoimg[0].width()*0.25),
|
---|
| 582 | int(logoimg[0].height()*0.25) );
|
---|
| 583 | }
|
---|
| 584 | QCanvasPolygonalItem* i = new ImageItem(logoimg[rand()%4],&canvas);
|
---|
| 585 | i->move(rand()%(canvas.width()-logoimg->width()),
|
---|
| 586 | rand()%(canvas.height()-logoimg->width()));
|
---|
| 587 | i->setZ(rand()%256+256);
|
---|
| 588 | i->show();
|
---|
| 589 | }
|
---|
| 590 |
|
---|
| 591 |
|
---|
| 592 |
|
---|
| 593 | void Main::addCircle()
|
---|
| 594 | {
|
---|
| 595 | QCanvasPolygonalItem* i = new QCanvasEllipse(50,50,&canvas);
|
---|
| 596 | i->setBrush( QColor(rand()%32*8,rand()%32*8,rand()%32*8) );
|
---|
| 597 | i->move(rand()%canvas.width(),rand()%canvas.height());
|
---|
| 598 | i->setZ(rand()%256);
|
---|
| 599 | i->show();
|
---|
| 600 | }
|
---|
| 601 |
|
---|
| 602 | void Main::addHexagon()
|
---|
| 603 | {
|
---|
| 604 | QCanvasPolygon* i = new QCanvasPolygon(&canvas);
|
---|
| 605 | const int size = canvas.width() / 25;
|
---|
| 606 | QPointArray pa(6);
|
---|
| 607 | pa[0] = QPoint(2*size,0);
|
---|
| 608 | pa[1] = QPoint(size,-size*173/100);
|
---|
| 609 | pa[2] = QPoint(-size,-size*173/100);
|
---|
| 610 | pa[3] = QPoint(-2*size,0);
|
---|
| 611 | pa[4] = QPoint(-size,size*173/100);
|
---|
| 612 | pa[5] = QPoint(size,size*173/100);
|
---|
| 613 | i->setPoints(pa);
|
---|
| 614 | i->setBrush( QColor(rand()%32*8,rand()%32*8,rand()%32*8) );
|
---|
| 615 | i->move(rand()%canvas.width(),rand()%canvas.height());
|
---|
| 616 | i->setZ(rand()%256);
|
---|
| 617 | i->show();
|
---|
| 618 | }
|
---|
| 619 |
|
---|
| 620 | void Main::addPolygon()
|
---|
| 621 | {
|
---|
| 622 | QCanvasPolygon* i = new QCanvasPolygon(&canvas);
|
---|
| 623 | const int size = canvas.width()/2;
|
---|
| 624 | QPointArray pa(6);
|
---|
| 625 | pa[0] = QPoint(0,0);
|
---|
| 626 | pa[1] = QPoint(size,size/5);
|
---|
| 627 | pa[2] = QPoint(size*4/5,size);
|
---|
| 628 | pa[3] = QPoint(size/6,size*5/4);
|
---|
| 629 | pa[4] = QPoint(size*3/4,size*3/4);
|
---|
| 630 | pa[5] = QPoint(size*3/4,size/4);
|
---|
| 631 | i->setPoints(pa);
|
---|
| 632 | i->setBrush( QColor(rand()%32*8,rand()%32*8,rand()%32*8) );
|
---|
| 633 | i->move(rand()%canvas.width(),rand()%canvas.height());
|
---|
| 634 | i->setZ(rand()%256);
|
---|
| 635 | i->show();
|
---|
| 636 | }
|
---|
| 637 |
|
---|
| 638 | void Main::addSpline()
|
---|
| 639 | {
|
---|
| 640 | QCanvasSpline* i = new QCanvasSpline(&canvas);
|
---|
| 641 | const int size = canvas.width()/6;
|
---|
| 642 | QPointArray pa(12);
|
---|
| 643 | pa[0] = QPoint(0,0);
|
---|
| 644 | pa[1] = QPoint(size/2,0);
|
---|
| 645 | pa[2] = QPoint(size,size/2);
|
---|
| 646 | pa[3] = QPoint(size,size);
|
---|
| 647 | pa[4] = QPoint(size,size*3/2);
|
---|
| 648 | pa[5] = QPoint(size/2,size*2);
|
---|
| 649 | pa[6] = QPoint(0,size*2);
|
---|
| 650 | pa[7] = QPoint(-size/2,size*2);
|
---|
| 651 | pa[8] = QPoint(size/4,size*3/2);
|
---|
| 652 | pa[9] = QPoint(0,size);
|
---|
| 653 | pa[10]= QPoint(-size/4,size/2);
|
---|
| 654 | pa[11]= QPoint(-size/2,0);
|
---|
| 655 | i->setControlPoints(pa);
|
---|
| 656 | i->setBrush( QColor(rand()%32*8,rand()%32*8,rand()%32*8) );
|
---|
| 657 | i->move(rand()%canvas.width(),rand()%canvas.height());
|
---|
| 658 | i->setZ(rand()%256);
|
---|
| 659 | i->show();
|
---|
| 660 | }
|
---|
| 661 |
|
---|
| 662 | void Main::addText()
|
---|
| 663 | {
|
---|
| 664 | QCanvasText* i = new QCanvasText(&canvas);
|
---|
| 665 | i->setText("QCanvasText");
|
---|
| 666 | i->move(rand()%canvas.width(),rand()%canvas.height());
|
---|
| 667 | i->setZ(rand()%256);
|
---|
| 668 | i->show();
|
---|
| 669 | }
|
---|
| 670 |
|
---|
| 671 | void Main::addLine()
|
---|
| 672 | {
|
---|
| 673 | QCanvasLine* i = new QCanvasLine(&canvas);
|
---|
| 674 | i->setPoints( rand()%canvas.width(), rand()%canvas.height(),
|
---|
| 675 | rand()%canvas.width(), rand()%canvas.height() );
|
---|
| 676 | i->setPen( QPen(QColor(rand()%32*8,rand()%32*8,rand()%32*8), 6) );
|
---|
| 677 | i->setZ(rand()%256);
|
---|
| 678 | i->show();
|
---|
| 679 | }
|
---|
| 680 |
|
---|
| 681 | void Main::addMesh()
|
---|
| 682 | {
|
---|
| 683 | int x0 = 0;
|
---|
| 684 | int y0 = 0;
|
---|
| 685 |
|
---|
| 686 | if ( !tb ) tb = new QBrush( Qt::red );
|
---|
| 687 | if ( !tp ) tp = new QPen( Qt::black );
|
---|
| 688 |
|
---|
| 689 | int nodecount = 0;
|
---|
| 690 |
|
---|
| 691 | int w = canvas.width();
|
---|
| 692 | int h = canvas.height();
|
---|
| 693 |
|
---|
| 694 | const int dist = 30;
|
---|
| 695 | int rows = h / dist;
|
---|
| 696 | int cols = w / dist;
|
---|
| 697 |
|
---|
| 698 | #ifndef QT_NO_PROGRESSDIALOG
|
---|
| 699 | QProgressDialog progress( "Creating mesh...", "Abort", rows,
|
---|
| 700 | this, "progress", TRUE );
|
---|
| 701 | #endif
|
---|
| 702 |
|
---|
| 703 | QMemArray<NodeItem*> lastRow(cols);
|
---|
| 704 | for ( int j = 0; j < rows; j++ ) {
|
---|
| 705 | int n = j%2 ? cols-1 : cols;
|
---|
| 706 | NodeItem *prev = 0;
|
---|
| 707 | for ( int i = 0; i < n; i++ ) {
|
---|
| 708 | NodeItem *el = new NodeItem( &canvas );
|
---|
| 709 | nodecount++;
|
---|
| 710 | int r = rand();
|
---|
| 711 | int xrand = r %20;
|
---|
| 712 | int yrand = (r/20) %20;
|
---|
| 713 | el->move( xrand + x0 + i*dist + (j%2 ? dist/2 : 0 ),
|
---|
| 714 | yrand + y0 + j*dist );
|
---|
| 715 |
|
---|
| 716 | if ( j > 0 ) {
|
---|
| 717 | if ( i < cols-1 )
|
---|
| 718 | (new EdgeItem( lastRow[i], el, &canvas ))->show();
|
---|
| 719 | if ( j%2 )
|
---|
| 720 | (new EdgeItem( lastRow[i+1], el, &canvas ))->show();
|
---|
| 721 | else if ( i > 0 )
|
---|
| 722 | (new EdgeItem( lastRow[i-1], el, &canvas ))->show();
|
---|
| 723 | }
|
---|
| 724 | if ( prev ) {
|
---|
| 725 | (new EdgeItem( prev, el, &canvas ))->show();
|
---|
| 726 | }
|
---|
| 727 | if ( i > 0 ) lastRow[i-1] = prev;
|
---|
| 728 | prev = el;
|
---|
| 729 | el->show();
|
---|
| 730 | }
|
---|
| 731 | lastRow[n-1]=prev;
|
---|
| 732 | #ifndef QT_NO_PROGRESSDIALOG
|
---|
| 733 | progress.setProgress( j );
|
---|
| 734 | if ( progress.wasCancelled() )
|
---|
| 735 | break;
|
---|
| 736 | #endif
|
---|
| 737 | }
|
---|
| 738 | #ifndef QT_NO_PROGRESSDIALOG
|
---|
| 739 | progress.setProgress( rows );
|
---|
| 740 | #endif
|
---|
| 741 | // qDebug( "%d nodes, %d edges", nodecount, EdgeItem::count() );
|
---|
| 742 | }
|
---|
| 743 |
|
---|
| 744 | void Main::addRectangle()
|
---|
| 745 | {
|
---|
| 746 | QCanvasPolygonalItem *i = new QCanvasRectangle( rand()%canvas.width(),rand()%canvas.height(),
|
---|
| 747 | canvas.width()/5,canvas.width()/5,&canvas);
|
---|
| 748 | int z = rand()%256;
|
---|
| 749 | i->setBrush( QColor(z,z,z) );
|
---|
| 750 | i->setPen( QPen(QColor(rand()%32*8,rand()%32*8,rand()%32*8), 6) );
|
---|
| 751 | i->setZ(z);
|
---|
| 752 | i->show();
|
---|
| 753 | }
|
---|