diff options
author | Timothy Pearson <kb9vqf@pearsoncomputing.net> | 2011-11-08 12:31:36 -0600 |
---|---|---|
committer | Timothy Pearson <kb9vqf@pearsoncomputing.net> | 2011-11-08 12:31:36 -0600 |
commit | d796c9dd933ab96ec83b9a634feedd5d32e1ba3f (patch) | |
tree | 6e3dcca4f77e20ec8966c666aac7c35bd4704053 /examples/canvas | |
download | tqt3-d796c9dd933ab96ec83b9a634feedd5d32e1ba3f.tar.gz tqt3-d796c9dd933ab96ec83b9a634feedd5d32e1ba3f.zip |
Test conversion to TQt3 from Qt3 8c6fc1f8e35fd264dd01c582ca5e7549b32ab731
Diffstat (limited to 'examples/canvas')
-rw-r--r-- | examples/canvas/blendshadow.cpp | 53 | ||||
-rw-r--r-- | examples/canvas/butterfly.png | bin | 0 -> 37832 bytes | |||
-rw-r--r-- | examples/canvas/canvas.cpp | 747 | ||||
-rw-r--r-- | examples/canvas/canvas.doc | 29 | ||||
-rw-r--r-- | examples/canvas/canvas.h | 91 | ||||
-rw-r--r-- | examples/canvas/canvas.pro | 10 | ||||
-rw-r--r-- | examples/canvas/main.cpp | 43 | ||||
-rw-r--r-- | examples/canvas/makeimg.cpp | 92 | ||||
-rw-r--r-- | examples/canvas/qt-trans.xpm | 54 | ||||
-rw-r--r-- | examples/canvas/qtlogo.png | bin | 0 -> 22383 bytes |
10 files changed, 1119 insertions, 0 deletions
diff --git a/examples/canvas/blendshadow.cpp b/examples/canvas/blendshadow.cpp new file mode 100644 index 000000000..deb18a4a1 --- /dev/null +++ b/examples/canvas/blendshadow.cpp @@ -0,0 +1,53 @@ +#include <qimage.h> +#include <qcolor.h> + +static inline int blendComponent( int v, int av, int s, int as ) +{ + return as*s + av*v -(av*as*s)/255; +} + +static inline TQRgb blendShade( TQRgb v, TQRgb s ) +{ + //shadow image is already reduced and blurred + int as = qAlpha(s); + int av = qAlpha(v); + if ( as == 0 || av == 255 ) + return v; + + int a = as + av -(as*av)/255; + + int r = blendComponent( qRed(v),av, qRed(s), as)/a; + int g = blendComponent( qGreen(v),av, qGreen(s), as)/a; + int b = blendComponent( qBlue(v),av, qBlue(s), as)/a; + + return qRgba(r,g,b,a); +} + + + +int main( int*, char**) +{ + TQImage image( "out.png" ); + image.convertDepth( 32 ); + TQImage shade( "outshade.png" ); + shade.convertDepth( 32 ); + int dx = 10; + int dy = 5; + + int w = image.width(); + int h = image.height(); + + TQImage img( w+dx, h+dy, 32 ); + img.setAlphaBuffer( TRUE ); + + for ( int y = 0; y < h+dy; y++ ) { + for ( int x = 0; x < w+dx; x++ ) { + TQRgb sh = (x<dx||y<dy) ? 0 : shade.pixel( x-dx, y-dy ); + TQRgb pixel = (x<w&y<h) ? image.pixel( x, y ) : 0; + img.setPixel( x, y, blendShade( pixel, sh ) ); + } + } + img.save("blend.png", "PNG" ); +} + + diff --git a/examples/canvas/butterfly.png b/examples/canvas/butterfly.png Binary files differnew file mode 100644 index 000000000..619f25306 --- /dev/null +++ b/examples/canvas/butterfly.png diff --git a/examples/canvas/canvas.cpp b/examples/canvas/canvas.cpp new file mode 100644 index 000000000..0be872859 --- /dev/null +++ b/examples/canvas/canvas.cpp @@ -0,0 +1,747 @@ +#include <qdatetime.h> +#include <qmainwindow.h> +#include <qstatusbar.h> +#include <qmessagebox.h> +#include <qmenubar.h> +#include <qapplication.h> +#include <qpainter.h> +#include <qprinter.h> +#include <qlabel.h> +#include <qimage.h> +#include <qprogressdialog.h> +#include "canvas.h" + +#include <stdlib.h> + +// We use a global variable to save memory - all the brushes and pens in +// the mesh are shared. +static TQBrush *tb = 0; +static TQPen *tp = 0; + +class EdgeItem; +class NodeItem; + +class EdgeItem: public TQCanvasLine +{ +public: + EdgeItem( NodeItem*, NodeItem*, TQCanvas *canvas ); + void setFromPoint( int x, int y ) ; + void setToPoint( int x, int y ); + static int count() { return c; } + void moveBy(double dx, double dy); +private: + static int c; +}; + +static const int imageRTTI = 984376; + + +class ImageItem: public TQCanvasRectangle +{ +public: + ImageItem( TQImage img, TQCanvas *canvas ); + int rtti () const { return imageRTTI; } + bool hit( const TQPoint&) const; +protected: + void drawShape( TQPainter & ); +private: + TQImage image; + TQPixmap pixmap; +}; + + +ImageItem::ImageItem( TQImage img, TQCanvas *canvas ) + : TQCanvasRectangle( canvas ), image(img) +{ + setSize( image.width(), image.height() ); + +#if !defined(Q_WS_QWS) + pixmap.convertFromImage(image, OrderedAlphaDither); +#endif +} + + +void ImageItem::drawShape( TQPainter &p ) +{ +// On TQt/Embedded, we can paint a TQImage as fast as a TQPixmap, +// but on other platforms, we need to use a TQPixmap. +#if defined(Q_WS_QWS) + p.drawImage( int(x()), int(y()), image, 0, 0, -1, -1, OrderedAlphaDither ); +#else + p.drawPixmap( int(x()), int(y()), pixmap ); +#endif +} + +bool ImageItem::hit( const TQPoint &p ) const +{ + int ix = p.x()-int(x()); + int iy = p.y()-int(y()); + if ( !image.valid( ix , iy ) ) + return FALSE; + TQRgb pixel = image.pixel( ix, iy ); + return qAlpha( pixel ) != 0; +} + +class NodeItem: public TQCanvasEllipse +{ +public: + NodeItem( TQCanvas *canvas ); + ~NodeItem() {} + + void addInEdge( EdgeItem *edge ) { inList.append( edge ); } + void addOutEdge( EdgeItem *edge ) { outList.append( edge ); } + + void moveBy(double dx, double dy); + + // TQPoint center() { return boundingRect().center(); } +private: + TQPtrList<EdgeItem> inList; + TQPtrList<EdgeItem> outList; +}; + + +int EdgeItem::c = 0; + + +void EdgeItem::moveBy(double, double) +{ + //nothing +} + +EdgeItem::EdgeItem( NodeItem *from, NodeItem *to, TQCanvas *canvas ) + : TQCanvasLine( canvas ) +{ + c++; + setPen( *tp ); + setBrush( *tb ); + from->addOutEdge( this ); + to->addInEdge( this ); + setPoints( int(from->x()), int(from->y()), int(to->x()), int(to->y()) ); + setZ( 127 ); +} + +void EdgeItem::setFromPoint( int x, int y ) +{ + setPoints( x,y, endPoint().x(), endPoint().y() ); +} + +void EdgeItem::setToPoint( int x, int y ) +{ + setPoints( startPoint().x(), startPoint().y(), x, y ); +} + + + +void NodeItem::moveBy(double dx, double dy) +{ + TQCanvasEllipse::moveBy( dx, dy ); + + TQPtrListIterator<EdgeItem> it1( inList ); + EdgeItem *edge; + while (( edge = it1.current() )) { + ++it1; + edge->setToPoint( int(x()), int(y()) ); + } + TQPtrListIterator<EdgeItem> it2( outList ); + while (( edge = it2.current() )) { + ++it2; + edge->setFromPoint( int(x()), int(y()) ); + } +} + +NodeItem::NodeItem( TQCanvas *canvas ) + : TQCanvasEllipse( 6, 6, canvas ) +{ + setPen( *tp ); + setBrush( *tb ); + setZ( 128 ); +} + +FigureEditor::FigureEditor( + TQCanvas& c, TQWidget* parent, + const char* name, WFlags f) : + TQCanvasView(&c,parent,name,f) +{ +} + +void FigureEditor::contentsMousePressEvent(TQMouseEvent* e) +{ + TQPoint p = inverseWorldMatrix().map(e->pos()); + TQCanvasItemList l=canvas()->collisions(p); + for (TQCanvasItemList::Iterator it=l.begin(); it!=l.end(); ++it) { + if ( (*it)->rtti() == imageRTTI ) { + ImageItem *item= (ImageItem*)(*it); + if ( !item->hit( p ) ) + continue; + } + moving = *it; + moving_start = p; + return; + } + moving = 0; +} + +void FigureEditor::clear() +{ + TQCanvasItemList list = canvas()->allItems(); + TQCanvasItemList::Iterator it = list.begin(); + for (; it != list.end(); ++it) { + if ( *it ) + delete *it; + } +} + +void FigureEditor::contentsMouseMoveEvent(TQMouseEvent* e) +{ + if ( moving ) { + TQPoint p = inverseWorldMatrix().map(e->pos()); + moving->moveBy(p.x() - moving_start.x(), + p.y() - moving_start.y()); + moving_start = p; + canvas()->update(); + } +} + + + +BouncyLogo::BouncyLogo(TQCanvas* canvas) : + TQCanvasSprite(0,canvas) +{ + static TQCanvasPixmapArray logo("qt-trans.xpm"); + setSequence(&logo); + setAnimated(TRUE); + initPos(); +} + + +const int logo_rtti = 1234; + +int BouncyLogo::rtti() const +{ + return logo_rtti; +} + +void BouncyLogo::initPos() +{ + initSpeed(); + int trial=1000; + do { + move(rand()%canvas()->width(),rand()%canvas()->height()); + advance(0); + } while (trial-- && xVelocity()==0.0 && yVelocity()==0.0); +} + +void BouncyLogo::initSpeed() +{ + const double speed = 4.0; + double d = (double)(rand()%1024) / 1024.0; + setVelocity( d*speed*2-speed, (1-d)*speed*2-speed ); +} + +void BouncyLogo::advance(int stage) +{ + switch ( stage ) { + case 0: { + double vx = xVelocity(); + double vy = yVelocity(); + + if ( vx == 0.0 && vy == 0.0 ) { + // stopped last turn + initSpeed(); + vx = xVelocity(); + vy = yVelocity(); + } + + double nx = x() + vx; + double ny = y() + vy; + + if ( nx < 0 || nx >= canvas()->width() ) + vx = -vx; + if ( ny < 0 || ny >= canvas()->height() ) + vy = -vy; + + for (int bounce=0; bounce<4; bounce++) { + TQCanvasItemList l=collisions(FALSE); + for (TQCanvasItemList::Iterator it=l.begin(); it!=l.end(); ++it) { + TQCanvasItem *hit = *it; + if ( hit->rtti()==logo_rtti && hit->collidesWith(this) ) { + switch ( bounce ) { + case 0: + vx = -vx; + break; + case 1: + vy = -vy; + vx = -vx; + break; + case 2: + vx = -vx; + break; + case 3: + // Stop for this turn + vx = 0; + vy = 0; + break; + } + setVelocity(vx,vy); + break; + } + } + } + + if ( x()+vx < 0 || x()+vx >= canvas()->width() ) + vx = 0; + if ( y()+vy < 0 || y()+vy >= canvas()->height() ) + vy = 0; + + setVelocity(vx,vy); + } break; + case 1: + TQCanvasItem::advance(stage); + break; + } +} + +static uint mainCount = 0; +static TQImage *butterflyimg; +static TQImage *logoimg; + +Main::Main(TQCanvas& c, TQWidget* parent, const char* name, WFlags f) : + TQMainWindow(parent,name,f), + canvas(c) +{ + editor = new FigureEditor(canvas,this); + TQMenuBar* menu = menuBar(); + + TQPopupMenu* file = new TQPopupMenu( menu ); + file->insertItem("&Fill canvas", this, SLOT(init()), CTRL+Key_F); + file->insertItem("&Erase canvas", this, SLOT(clear()), CTRL+Key_E); + file->insertItem("&New view", this, SLOT(newView()), CTRL+Key_N); + file->insertSeparator(); + file->insertItem("&Print...", this, SLOT(print()), CTRL+Key_P); + file->insertSeparator(); + file->insertItem("E&xit", qApp, SLOT(tquit()), CTRL+Key_Q); + menu->insertItem("&File", file); + + TQPopupMenu* edit = new TQPopupMenu( menu ); + edit->insertItem("Add &Circle", this, SLOT(addCircle()), ALT+Key_C); + edit->insertItem("Add &Hexagon", this, SLOT(addHexagon()), ALT+Key_H); + edit->insertItem("Add &Polygon", this, SLOT(addPolygon()), ALT+Key_P); + edit->insertItem("Add Spl&ine", this, SLOT(addSpline()), ALT+Key_I); + edit->insertItem("Add &Text", this, SLOT(addText()), ALT+Key_T); + edit->insertItem("Add &Line", this, SLOT(addLine()), ALT+Key_L); + edit->insertItem("Add &Rectangle", this, SLOT(addRectangle()), ALT+Key_R); + edit->insertItem("Add &Sprite", this, SLOT(addSprite()), ALT+Key_S); + edit->insertItem("Create &Mesh", this, SLOT(addMesh()), ALT+Key_M ); + edit->insertItem("Add &Alpha-blended image", this, SLOT(addButterfly()), ALT+Key_A); + menu->insertItem("&Edit", edit); + + TQPopupMenu* view = new TQPopupMenu( menu ); + view->insertItem("&Enlarge", this, SLOT(enlarge()), SHIFT+CTRL+Key_Plus); + view->insertItem("Shr&ink", this, SLOT(shrink()), SHIFT+CTRL+Key_Minus); + view->insertSeparator(); + view->insertItem("&Rotate clockwise", this, SLOT(rotateClockwise()), CTRL+Key_PageDown); + view->insertItem("Rotate &counterclockwise", this, SLOT(rotateCounterClockwise()), CTRL+Key_PageUp); + view->insertItem("&Zoom in", this, SLOT(zoomIn()), CTRL+Key_Plus); + view->insertItem("Zoom &out", this, SLOT(zoomOut()), CTRL+Key_Minus); + view->insertItem("Translate left", this, SLOT(moveL()), CTRL+Key_Left); + view->insertItem("Translate right", this, SLOT(moveR()), CTRL+Key_Right); + view->insertItem("Translate up", this, SLOT(moveU()), CTRL+Key_Up); + view->insertItem("Translate down", this, SLOT(moveD()), CTRL+Key_Down); + view->insertItem("&Mirror", this, SLOT(mirror()), CTRL+Key_Home); + menu->insertItem("&View", view); + + options = new TQPopupMenu( menu ); + dbf_id = options->insertItem("Double buffer", this, SLOT(toggleDoubleBuffer())); + options->setItemChecked(dbf_id, TRUE); + menu->insertItem("&Options",options); + + menu->insertSeparator(); + + TQPopupMenu* help = new TQPopupMenu( menu ); + help->insertItem("&About", this, SLOT(help()), Key_F1); + help->setItemChecked(dbf_id, TRUE); + menu->insertItem("&Help",help); + + statusBar(); + + setCentralWidget(editor); + + printer = 0; + + init(); +} + +void Main::init() +{ + clear(); + + static int r=24; + srand(++r); + + mainCount++; + butterflyimg = 0; + logoimg = 0; + + int i; + for ( i=0; i<canvas.width() / 56; i++) { + addButterfly(); + } + for ( i=0; i<canvas.width() / 85; i++) { + addHexagon(); + } + for ( i=0; i<canvas.width() / 128; i++) { + addLogo(); + } +} + +Main::~Main() +{ + delete printer; + if ( !--mainCount ) { + delete[] butterflyimg; + butterflyimg = 0; + delete[] logoimg; + logoimg = 0; + } +} + +void Main::newView() +{ + // Open a new view... have it delete when closed. + Main *m = new Main(canvas, 0, 0, WDestructiveClose); + qApp->setMainWidget(m); + m->show(); + qApp->setMainWidget(0); +} + +void Main::clear() +{ + editor->clear(); +} + +void Main::help() +{ + static TQMessageBox* about = new TQMessageBox( "TQt Canvas Example", + "<h3>The TQCanvas classes example</h3>" + "<ul>" + "<li> Press ALT-S for some sprites." + "<li> Press ALT-C for some circles." + "<li> Press ALT-L for some lines." + "<li> Drag the objects around." + "<li> Read the code!" + "</ul>", TQMessageBox::Information, 1, 0, 0, this, 0, FALSE ); + about->setButtonText( 1, "Dismiss" ); + about->show(); +} + +void Main::aboutTQt() +{ + TQMessageBox::aboutTQt( this, "TQt Canvas Example" ); +} + +void Main::toggleDoubleBuffer() +{ + bool s = !options->isItemChecked(dbf_id); + options->setItemChecked(dbf_id,s); + canvas.setDoubleBuffering(s); +} + +void Main::enlarge() +{ + canvas.resize(canvas.width()*4/3, canvas.height()*4/3); +} + +void Main::shrink() +{ + canvas.resize(canvas.width()*3/4, canvas.height()*3/4); +} + +void Main::rotateClockwise() +{ + TQWMatrix m = editor->worldMatrix(); + m.rotate( 22.5 ); + editor->setWorldMatrix( m ); +} + +void Main::rotateCounterClockwise() +{ + TQWMatrix m = editor->worldMatrix(); + m.rotate( -22.5 ); + editor->setWorldMatrix( m ); +} + +void Main::zoomIn() +{ + TQWMatrix m = editor->worldMatrix(); + m.scale( 2.0, 2.0 ); + editor->setWorldMatrix( m ); +} + +void Main::zoomOut() +{ + TQWMatrix m = editor->worldMatrix(); + m.scale( 0.5, 0.5 ); + editor->setWorldMatrix( m ); +} + +void Main::mirror() +{ + TQWMatrix m = editor->worldMatrix(); + m.scale( -1, 1 ); + editor->setWorldMatrix( m ); +} + +void Main::moveL() +{ + TQWMatrix m = editor->worldMatrix(); + m.translate( -16, 0 ); + editor->setWorldMatrix( m ); +} + +void Main::moveR() +{ + TQWMatrix m = editor->worldMatrix(); + m.translate( +16, 0 ); + editor->setWorldMatrix( m ); +} + +void Main::moveU() +{ + TQWMatrix m = editor->worldMatrix(); + m.translate( 0, -16 ); + editor->setWorldMatrix( m ); +} + +void Main::moveD() +{ + TQWMatrix m = editor->worldMatrix(); + m.translate( 0, +16 ); + editor->setWorldMatrix( m ); +} + +void Main::print() +{ + if ( !printer ) printer = new TQPrinter; + if ( printer->setup(this) ) { + TQPainter pp(printer); + canvas.drawArea(TQRect(0,0,canvas.width(),canvas.height()),&pp,FALSE); + } +} + + +void Main::addSprite() +{ + TQCanvasItem* i = new BouncyLogo(&canvas); + i->setZ(rand()%256); + i->show(); +} + +TQString butterfly_fn; +TQString logo_fn; + + +void Main::addButterfly() +{ + if ( butterfly_fn.isEmpty() ) + return; + if ( !butterflyimg ) { + butterflyimg = new TQImage[4]; + butterflyimg[0].load( butterfly_fn ); + butterflyimg[1] = butterflyimg[0].smoothScale( int(butterflyimg[0].width()*0.75), + int(butterflyimg[0].height()*0.75) ); + butterflyimg[2] = butterflyimg[0].smoothScale( int(butterflyimg[0].width()*0.5), + int(butterflyimg[0].height()*0.5) ); + butterflyimg[3] = butterflyimg[0].smoothScale( int(butterflyimg[0].width()*0.25), + int(butterflyimg[0].height()*0.25) ); + } + TQCanvasPolygonalItem* i = new ImageItem(butterflyimg[rand()%4],&canvas); + i->move(rand()%(canvas.width()-butterflyimg->width()), + rand()%(canvas.height()-butterflyimg->height())); + i->setZ(rand()%256+250); + i->show(); +} + +void Main::addLogo() +{ + if ( logo_fn.isEmpty() ) + return; + if ( !logoimg ) { + logoimg = new TQImage[4]; + logoimg[0].load( logo_fn ); + logoimg[1] = logoimg[0].smoothScale( int(logoimg[0].width()*0.75), + int(logoimg[0].height()*0.75) ); + logoimg[2] = logoimg[0].smoothScale( int(logoimg[0].width()*0.5), + int(logoimg[0].height()*0.5) ); + logoimg[3] = logoimg[0].smoothScale( int(logoimg[0].width()*0.25), + int(logoimg[0].height()*0.25) ); + } + TQCanvasPolygonalItem* i = new ImageItem(logoimg[rand()%4],&canvas); + i->move(rand()%(canvas.width()-logoimg->width()), + rand()%(canvas.height()-logoimg->width())); + i->setZ(rand()%256+256); + i->show(); +} + + + +void Main::addCircle() +{ + TQCanvasPolygonalItem* i = new TQCanvasEllipse(50,50,&canvas); + i->setBrush( TQColor(rand()%32*8,rand()%32*8,rand()%32*8) ); + i->move(rand()%canvas.width(),rand()%canvas.height()); + i->setZ(rand()%256); + i->show(); +} + +void Main::addHexagon() +{ + TQCanvasPolygon* i = new TQCanvasPolygon(&canvas); + const int size = canvas.width() / 25; + TQPointArray pa(6); + pa[0] = TQPoint(2*size,0); + pa[1] = TQPoint(size,-size*173/100); + pa[2] = TQPoint(-size,-size*173/100); + pa[3] = TQPoint(-2*size,0); + pa[4] = TQPoint(-size,size*173/100); + pa[5] = TQPoint(size,size*173/100); + i->setPoints(pa); + i->setBrush( TQColor(rand()%32*8,rand()%32*8,rand()%32*8) ); + i->move(rand()%canvas.width(),rand()%canvas.height()); + i->setZ(rand()%256); + i->show(); +} + +void Main::addPolygon() +{ + TQCanvasPolygon* i = new TQCanvasPolygon(&canvas); + const int size = canvas.width()/2; + TQPointArray pa(6); + pa[0] = TQPoint(0,0); + pa[1] = TQPoint(size,size/5); + pa[2] = TQPoint(size*4/5,size); + pa[3] = TQPoint(size/6,size*5/4); + pa[4] = TQPoint(size*3/4,size*3/4); + pa[5] = TQPoint(size*3/4,size/4); + i->setPoints(pa); + i->setBrush( TQColor(rand()%32*8,rand()%32*8,rand()%32*8) ); + i->move(rand()%canvas.width(),rand()%canvas.height()); + i->setZ(rand()%256); + i->show(); +} + +void Main::addSpline() +{ + TQCanvasSpline* i = new TQCanvasSpline(&canvas); + const int size = canvas.width()/6; + TQPointArray pa(12); + pa[0] = TQPoint(0,0); + pa[1] = TQPoint(size/2,0); + pa[2] = TQPoint(size,size/2); + pa[3] = TQPoint(size,size); + pa[4] = TQPoint(size,size*3/2); + pa[5] = TQPoint(size/2,size*2); + pa[6] = TQPoint(0,size*2); + pa[7] = TQPoint(-size/2,size*2); + pa[8] = TQPoint(size/4,size*3/2); + pa[9] = TQPoint(0,size); + pa[10]= TQPoint(-size/4,size/2); + pa[11]= TQPoint(-size/2,0); + i->setControlPoints(pa); + i->setBrush( TQColor(rand()%32*8,rand()%32*8,rand()%32*8) ); + i->move(rand()%canvas.width(),rand()%canvas.height()); + i->setZ(rand()%256); + i->show(); +} + +void Main::addText() +{ + TQCanvasText* i = new TQCanvasText(&canvas); + i->setText("TQCanvasText"); + i->move(rand()%canvas.width(),rand()%canvas.height()); + i->setZ(rand()%256); + i->show(); +} + +void Main::addLine() +{ + TQCanvasLine* i = new TQCanvasLine(&canvas); + i->setPoints( rand()%canvas.width(), rand()%canvas.height(), + rand()%canvas.width(), rand()%canvas.height() ); + i->setPen( TQPen(TQColor(rand()%32*8,rand()%32*8,rand()%32*8), 6) ); + i->setZ(rand()%256); + i->show(); +} + +void Main::addMesh() +{ + int x0 = 0; + int y0 = 0; + + if ( !tb ) tb = new TQBrush( TQt::red ); + if ( !tp ) tp = new TQPen( TQt::black ); + + int nodecount = 0; + + int w = canvas.width(); + int h = canvas.height(); + + const int dist = 30; + int rows = h / dist; + int cols = w / dist; + +#ifndef QT_NO_PROGRESSDIALOG + TQProgressDialog progress( "Creating mesh...", "Abort", rows, + this, "progress", TRUE ); +#endif + + TQMemArray<NodeItem*> lastRow(cols); + for ( int j = 0; j < rows; j++ ) { + int n = j%2 ? cols-1 : cols; + NodeItem *prev = 0; + for ( int i = 0; i < n; i++ ) { + NodeItem *el = new NodeItem( &canvas ); + nodecount++; + int r = rand(); + int xrand = r %20; + int yrand = (r/20) %20; + el->move( xrand + x0 + i*dist + (j%2 ? dist/2 : 0 ), + yrand + y0 + j*dist ); + + if ( j > 0 ) { + if ( i < cols-1 ) + (new EdgeItem( lastRow[i], el, &canvas ))->show(); + if ( j%2 ) + (new EdgeItem( lastRow[i+1], el, &canvas ))->show(); + else if ( i > 0 ) + (new EdgeItem( lastRow[i-1], el, &canvas ))->show(); + } + if ( prev ) { + (new EdgeItem( prev, el, &canvas ))->show(); + } + if ( i > 0 ) lastRow[i-1] = prev; + prev = el; + el->show(); + } + lastRow[n-1]=prev; +#ifndef QT_NO_PROGRESSDIALOG + progress.setProgress( j ); + if ( progress.wasCancelled() ) + break; +#endif + } +#ifndef QT_NO_PROGRESSDIALOG + progress.setProgress( rows ); +#endif + // qDebug( "%d nodes, %d edges", nodecount, EdgeItem::count() ); +} + +void Main::addRectangle() +{ + TQCanvasPolygonalItem *i = new TQCanvasRectangle( rand()%canvas.width(),rand()%canvas.height(), + canvas.width()/5,canvas.width()/5,&canvas); + int z = rand()%256; + i->setBrush( TQColor(z,z,z) ); + i->setPen( TQPen(TQColor(rand()%32*8,rand()%32*8,rand()%32*8), 6) ); + i->setZ(z); + i->show(); +} diff --git a/examples/canvas/canvas.doc b/examples/canvas/canvas.doc new file mode 100644 index 000000000..f6d77ad68 --- /dev/null +++ b/examples/canvas/canvas.doc @@ -0,0 +1,29 @@ +/*! \page canvas-example.html + + \ingroup examples + \title Canvas Example + + This example shows a QCanvas and some \l{QCanvasItem}s in action. + You can do a lot more with QCanvas than we show here, but the + example provides a taste of what can be done. + + <hr> + + Header file: + + \include canvas/canvas.h + + <hr> + + Implementation: + + \include canvas/canvas.cpp + + <hr> + + Main: + + \include canvas/main.cpp +*/ + + diff --git a/examples/canvas/canvas.h b/examples/canvas/canvas.h new file mode 100644 index 000000000..178e5c0c3 --- /dev/null +++ b/examples/canvas/canvas.h @@ -0,0 +1,91 @@ +#ifndef EXAMPLE_H +#define EXAMPLE_H + +#include <qpopupmenu.h> +#include <qmainwindow.h> +#include <qintdict.h> +#include <qcanvas.h> + +class BouncyLogo : public TQCanvasSprite { + void initPos(); + void initSpeed(); +public: + BouncyLogo(TQCanvas*); + void advance(int); + int rtti() const; +}; + + +class FigureEditor : public TQCanvasView { + Q_OBJECT + +public: + FigureEditor(TQCanvas&, TQWidget* parent=0, const char* name=0, WFlags f=0); + void clear(); + +protected: + void contentsMousePressEvent(TQMouseEvent*); + void contentsMouseMoveEvent(TQMouseEvent*); + +signals: + void status(const TQString&); + +private: + TQCanvasItem* moving; + TQPoint moving_start; +}; + +class Main : public TQMainWindow { + Q_OBJECT + +public: + Main(TQCanvas&, TQWidget* parent=0, const char* name=0, WFlags f=0); + ~Main(); + +public slots: + void help(); + +private slots: + void aboutTQt(); + void newView(); + void clear(); + void init(); + + void addSprite(); + void addCircle(); + void addHexagon(); + void addPolygon(); + void addSpline(); + void addText(); + void addLine(); + void addRectangle(); + void addMesh(); + void addLogo(); + void addButterfly(); + + void enlarge(); + void shrink(); + void rotateClockwise(); + void rotateCounterClockwise(); + void zoomIn(); + void zoomOut(); + void mirror(); + void moveL(); + void moveR(); + void moveU(); + void moveD(); + + void print(); + + void toggleDoubleBuffer(); + +private: + TQCanvas& canvas; + FigureEditor *editor; + + TQPopupMenu* options; + TQPrinter* printer; + int dbf_id; +}; + +#endif diff --git a/examples/canvas/canvas.pro b/examples/canvas/canvas.pro new file mode 100644 index 000000000..68c368da1 --- /dev/null +++ b/examples/canvas/canvas.pro @@ -0,0 +1,10 @@ +TEMPLATE = app +TARGET = canvas + +CONFIG += qt warn_on release +DEPENDPATH = ../../include + +REQUIRES = canvas full-config + +HEADERS = canvas.h +SOURCES = canvas.cpp main.cpp diff --git a/examples/canvas/main.cpp b/examples/canvas/main.cpp new file mode 100644 index 000000000..3666ae95a --- /dev/null +++ b/examples/canvas/main.cpp @@ -0,0 +1,43 @@ +#include <qstatusbar.h> +#include <qmessagebox.h> +#include <qmenubar.h> +#include <qapplication.h> +#include <qimage.h> + +#include "canvas.h" + +#include <stdlib.h> + +extern TQString butterfly_fn; +extern TQString logo_fn; + +int main(int argc, char** argv) +{ + TQApplication app(argc,argv); + + if ( argc > 1 ) + butterfly_fn = argv[1]; + else + butterfly_fn = "butterfly.png"; + + if ( argc > 2 ) + logo_fn = argv[2]; + else + logo_fn = "qtlogo.png"; + + TQCanvas canvas(800,600); + canvas.setAdvancePeriod(30); + Main m(canvas); + m.resize(m.sizeHint()); + m.setCaption("TQt Example - Canvas"); + if ( TQApplication::desktop()->width() > m.width() + 10 + && TQApplication::desktop()->height() > m.height() +30 ) + m.show(); + else + m.showMaximized(); + + TQObject::connect( qApp, SIGNAL(lastWindowClosed()), qApp, SLOT(tquit()) ); + + return app.exec(); +} + diff --git a/examples/canvas/makeimg.cpp b/examples/canvas/makeimg.cpp new file mode 100644 index 000000000..23f3c6acc --- /dev/null +++ b/examples/canvas/makeimg.cpp @@ -0,0 +1,92 @@ +#include <qimage.h> +#include <qcolor.h> + +static inline int blendComponent( int v, int av, int s, int as ) +{ + //shadow gets a color inversely proportional to the + //alpha value + s = (s*(255-as))/255; + //then do standard blending + return as*s + av*v -(av*as*s)/255; +} + +static inline TQRgb blendShade( TQRgb v, TQRgb s ) +{ + //pick a number: shadow is 1/3 of object + int as = qAlpha(s)/3; + int av = qAlpha(v); + if ( as == 0 || av == 255 ) + return v; + + int a = as + av -(as*av)/255; + + + int r = blendComponent( qRed(v),av, qRed(s), as)/a; + int g = blendComponent( qGreen(v),av, qGreen(s), as)/a; + int b = blendComponent( qBlue(v),av, qBlue(s), as)/a; + + return qRgba(r,g,b,a); +} + +int main( int*, char**) +{ + TQImage *img; + + img = new TQImage( "in.png" ); + int w,h; + int y; + img->setAlphaBuffer( TRUE ); + *img = img->convertDepth( 32 ); + w = img->width(); + h = img->height(); +#if 0 + for ( y = 0; y < h; y ++ ) { + uint *line = (uint*)img->scanLine( y ); + for ( int x = 0; x < w; x++ ) { + uint pixel = line[x]; + int r = qRed(pixel); + int g = qGreen(pixel); + int b = qBlue(pixel); + int min = TQMIN( r, TQMIN( g, b ) ); + int max = TQMAX( r, TQMAX( g, b ) ); + r -= min; + g -= min; + b -= min; + if ( max !=min ) { + r = (r*255)/(max-min); + g = (g*255)/(max-min); + b = (b*255)/(max-min); + } + int a = 255-min; + a -= (max-min)/3; //hack more transparency for colors. + line[x] = qRgba( r, g, b, a ); + } + } +#endif + *img = img->smoothScale( w/2, h/2 ); + + qDebug( "saving out.png"); + img->save( "out.png", "PNG" ); + + w = img->width(); + h = img->height(); + + TQImage *img2 = new TQImage( w, h, 32 ); + img2->setAlphaBuffer( TRUE ); + for ( y = 0; y < h; y++ ) { + for ( int x = 0; x < w; x++ ) { + TQRgb shader = img->pixel( x, y ); + + int as = qAlpha(shader)/3; + + int r = (qRed(shader)*(255-as))/255; + int g = (qGreen(shader)*(255-as))/255; + int b = (qBlue(shader)*(255-as))/255; + + img2->setPixel( x, y, qRgba(r,g,b,as) ); + } + } + + img2->save( "outshade.png", "PNG" ); + +} diff --git a/examples/canvas/qt-trans.xpm b/examples/canvas/qt-trans.xpm new file mode 100644 index 000000000..225f684fd --- /dev/null +++ b/examples/canvas/qt-trans.xpm @@ -0,0 +1,54 @@ +/* XPM */ +static char *qtlogo_xpm[] = { +/* width height ncolors chars_per_pixel */ +"54 34 13 1", +/* colors */ +" c #000000", +". c #999999", +"X c #333366", +"o c #6666CC", +"O c #333333", +"@ c #666699", +"# c #000066", +"$ c #666666", +"% c #3333CC", +"& c #000033", +"* c #9999CC", +"= c #333399", +"+ c None", +/* pixels */ +"+++++++++++++++++++.$OOO$.++++++++++++++++++++++++++++", +"+++++++++++++++++$ O.+++++++++++++++++++++++++", +"+++++++++++++++.O $++++++++++++++++++++++++", +"++++++++++++++. $.++.$ O+++++++++++++++++++++++", +"+++++++++++++. O.+++++++$ O++++++++++++++++++++++", +"+++++++++++++O ++++++++++$ $+++++++++++++++++++++", +"++++++++++++$ .+++++++++++O .++++++++++++++++++++", +"+++++++++++. O+++++++++++++ O++++++.+++++++++++++", +"+++++++++++$ .+++++++++++++$ .+++.O +++++++++++++", +"+++++++++++ +++++++++++++++ O+++. +++++++++++++", +"++++++++++. &Xoooo*++++++++++$ +++. +++++++++++++", +"++++++++++@=%%%%%%%%%%*+++++++. .++. +++++++++++++", +"+++++++**oooooo**++*o%%%%o+++++ $++O +++++++++++++", +"+++++*****$OOX@oooo*++*%%%%%*++O $+. OOO$++++++++*", +"++..+.++....$O$+*ooooo*+*o%%%%%O O+$ $$O.++++++**+", +"***+*+++++$$....+++*oooo**+*o%%# O++O ++++++***o*++", +"*+++++++++O $.....++**oooo**+*X &o*O ++++*ooo*++++", +"++++++++++$ O++.....++**oooo*X &%%& ..*o%%*++++++", +"++++++++++$ ++++.....+++**ooO $*o& @oo*+++++++++", +"++++++++++. .++++++.....+++*O Xo*O .++++++++++++", +"+++++++++++ O+++++++++...... .++O +++++++++++++", +"+++++++++++O +++.$$$.++++++. O+++O +++++++++++++", +"+++++++++++. $$OO O.++++O .+++O +++++++++++++", +"++++++++++++O .+++.O $++. O++++O +++++++++++++", +"++++++++++++. O+++++O $+O +++++O +++++++++++++", +"+++++++++++++. O+++++O O .+++++O .++++++++++++", +"++++++++++++++$ .++++O .++++.+$ O+.$.++++++++", +"+++++++++++++++. O$$O .+++++... +++++++++", +"+++++++++++++++++$ O+++++$$+.O O$.+++++++++", +"+++++++++++++++++++$OO O$.O O.++. .++++++++++++++++", +"+++++++++++++++++++++++++++. OO .++++++++++++++++", +"++++++++++++++++++++++++++++. O+++++++++++++++++", +"+++++++++++++++++++++++++++++. .+++++++++++++++++", +"++++++++++++++++++++++++++++++.O O.++++++++++++++++++" +}; diff --git a/examples/canvas/qtlogo.png b/examples/canvas/qtlogo.png Binary files differnew file mode 100644 index 000000000..68cb8bfd1 --- /dev/null +++ b/examples/canvas/qtlogo.png |