diff options
Diffstat (limited to 'examples/graphicsview/portedcanvas/canvas.cpp')
-rw-r--r-- | examples/graphicsview/portedcanvas/canvas.cpp | 259 |
1 files changed, 129 insertions, 130 deletions
diff --git a/examples/graphicsview/portedcanvas/canvas.cpp b/examples/graphicsview/portedcanvas/canvas.cpp index 7937762..efcfcc5 100644 --- a/examples/graphicsview/portedcanvas/canvas.cpp +++ b/examples/graphicsview/portedcanvas/canvas.cpp @@ -38,27 +38,24 @@ ** ****************************************************************************/ -#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 <q3progressdialog.h> -#include <Q3PointArray> -#include <Q3PtrList> +#include <QDateTime> +#include <QMainWindow> +#include <QStatusBar> +#include <QMessageBox> +#include <QMenuBar> +#include <QApplication> +#include <QPainter> +#include <QPrinter> +#include <QPrintDialog> +#include <QLabel> +#include <QImage> +#include <QProgressDialog> #include <QPixmap> -#include <Q3PopupMenu> #include <QMouseEvent> -#include <Q3MemArray> -#include "canvas.h" #include <QStyleOptionGraphicsItem> -#include <qdebug.h> -#include <stdlib.h> +#include <QDebug> + +#include "canvas.h" // We use a global variable to save memory - all the brushes and pens in // the mesh are shared. @@ -79,14 +76,11 @@ private: static int c; }; -static const int imageRTTI = 984376; - class ImageItem: public QGraphicsRectItem { public: ImageItem( QImage img ); - int rtti () const { return imageRTTI; } protected: void paint( QPainter *, const QStyleOptionGraphicsItem *option, QWidget *widget ); private: @@ -101,7 +95,7 @@ ImageItem::ImageItem( QImage img ) setRect(0, 0, image.width(), image.height()); setFlag(ItemIsMovable); #if !defined(Q_WS_QWS) - pixmap.convertFromImage(image, Qt::OrderedAlphaDither); + pixmap.fromImage(image, Qt::OrderedAlphaDither); #endif } @@ -131,8 +125,8 @@ protected: // QPoint center() { return boundingRect().center(); } private: - Q3PtrList<EdgeItem> inList; - Q3PtrList<EdgeItem> outList; + QList<EdgeItem*> inList; + QList<EdgeItem*> outList; }; @@ -163,15 +157,12 @@ void EdgeItem::setToPoint( int x, int y ) QVariant NodeItem::itemChange(GraphicsItemChange change, const QVariant &value) { if (change == ItemPositionHasChanged) { - Q3PtrListIterator<EdgeItem> it1( inList ); EdgeItem *edge; - while (( edge = it1.current() )) { - ++it1; + foreach(edge, inList) { edge->setToPoint( int(x()), int(y()) ); } - Q3PtrListIterator<EdgeItem> it2( outList ); - while (( edge = it2.current() )) { - ++it2; + + foreach(edge, outList) { edge->setFromPoint( int(x()), int(y()) ); } } @@ -311,63 +302,65 @@ static uint mainCount = 0; static QImage *butterflyimg; static QImage *logoimg; -Main::Main(QGraphicsScene& c, QWidget* parent, const char* name, Qt::WindowFlags f) : - Q3MainWindow(parent,name,f), +Main::Main(QGraphicsScene& c, QWidget* parent, Qt::WindowFlags f) : + QMainWindow(parent, f), canvas(c) { editor = new FigureEditor(canvas,this); + QMenuBar* menu = menuBar(); - Q3PopupMenu* file = new Q3PopupMenu( menu ); - file->insertItem("&Fill canvas", this, SLOT(init()), Qt::CTRL+Qt::Key_F); - file->insertItem("&Erase canvas", this, SLOT(clear()), Qt::CTRL+Qt::Key_E); - file->insertItem("&New view", this, SLOT(newView()), Qt::CTRL+Qt::Key_N); - file->insertSeparator(); - file->insertItem("&Print...", this, SLOT(print()), Qt::CTRL+Qt::Key_P); - file->insertSeparator(); - file->insertItem("E&xit", qApp, SLOT(quit()), Qt::CTRL+Qt::Key_Q); - menu->insertItem("&File", file); - - Q3PopupMenu* edit = new Q3PopupMenu( menu ); - edit->insertItem("Add &Circle", this, SLOT(addCircle()), Qt::ALT+Qt::Key_C); - edit->insertItem("Add &Hexagon", this, SLOT(addHexagon()), Qt::ALT+Qt::Key_H); - edit->insertItem("Add &Polygon", this, SLOT(addPolygon()), Qt::ALT+Qt::Key_P); - edit->insertItem("Add Spl&ine", this, SLOT(addSpline()), Qt::ALT+Qt::Key_I); - edit->insertItem("Add &Text", this, SLOT(addText()), Qt::ALT+Qt::Key_T); - edit->insertItem("Add &Line", this, SLOT(addLine()), Qt::ALT+Qt::Key_L); - edit->insertItem("Add &Rectangle", this, SLOT(addRectangle()), Qt::ALT+Qt::Key_R); - edit->insertItem("Add &Sprite", this, SLOT(addSprite()), Qt::ALT+Qt::Key_S); - edit->insertItem("Create &Mesh", this, SLOT(addMesh()), Qt::ALT+Qt::Key_M ); - edit->insertItem("Add &Alpha-blended image", this, SLOT(addButterfly()), Qt::ALT+Qt::Key_A); - menu->insertItem("&Edit", edit); - - Q3PopupMenu* view = new Q3PopupMenu( menu ); - view->insertItem("&Enlarge", this, SLOT(enlarge()), Qt::SHIFT+Qt::CTRL+Qt::Key_Plus); - view->insertItem("Shr&ink", this, SLOT(shrink()), Qt::SHIFT+Qt::CTRL+Qt::Key_Minus); - view->insertSeparator(); - view->insertItem("&Rotate clockwise", this, SLOT(rotateClockwise()), Qt::CTRL+Qt::Key_PageDown); - view->insertItem("Rotate &counterclockwise", this, SLOT(rotateCounterClockwise()), Qt::CTRL+Qt::Key_PageUp); - view->insertItem("&Zoom in", this, SLOT(zoomIn()), Qt::CTRL+Qt::Key_Plus); - view->insertItem("Zoom &out", this, SLOT(zoomOut()), Qt::CTRL+Qt::Key_Minus); - view->insertItem("Translate left", this, SLOT(moveL()), Qt::CTRL+Qt::Key_Left); - view->insertItem("Translate right", this, SLOT(moveR()), Qt::CTRL+Qt::Key_Right); - view->insertItem("Translate up", this, SLOT(moveU()), Qt::CTRL+Qt::Key_Up); - view->insertItem("Translate down", this, SLOT(moveD()), Qt::CTRL+Qt::Key_Down); - view->insertItem("&Mirror", this, SLOT(mirror()), Qt::CTRL+Qt::Key_Home); - menu->insertItem("&View", view); - - menu->insertSeparator(); - - Q3PopupMenu* help = new Q3PopupMenu( menu ); - help->insertItem("&About", this, SLOT(help()), Qt::Key_F1); - help->setItemChecked(dbf_id, TRUE); - menu->insertItem("&Help",help); + QMenu* file = new QMenu("&File", menu ); + file->addAction("&Fill canvas", this, SLOT(init()), Qt::CTRL+Qt::Key_F); + file->addAction("&Erase canvas", this, SLOT(clear()), Qt::CTRL+Qt::Key_E); + file->addAction("&New view", this, SLOT(newView()), Qt::CTRL+Qt::Key_N); + file->addSeparator(); + file->addAction("&Print...", this, SLOT(print()), Qt::CTRL+Qt::Key_P); + file->addSeparator(); + file->addAction("E&xit", qApp, SLOT(quit()), Qt::CTRL+Qt::Key_Q); + menu->addMenu(file); + + QMenu* edit = new QMenu("&Edit", menu ); + edit->addAction("Add &Circle", this, SLOT(addCircle()), Qt::ALT+Qt::Key_C); + edit->addAction("Add &Hexagon", this, SLOT(addHexagon()), Qt::ALT+Qt::Key_H); + edit->addAction("Add &Polygon", this, SLOT(addPolygon()), Qt::ALT+Qt::Key_P); + edit->addAction("Add Spl&ine", this, SLOT(addSpline()), Qt::ALT+Qt::Key_I); + edit->addAction("Add &Text", this, SLOT(addText()), Qt::ALT+Qt::Key_T); + edit->addAction("Add &Line", this, SLOT(addLine()), Qt::ALT+Qt::Key_L); + edit->addAction("Add &Rectangle", this, SLOT(addRectangle()), Qt::ALT+Qt::Key_R); + edit->addAction("Add &Sprite", this, SLOT(addSprite()), Qt::ALT+Qt::Key_S); + edit->addAction("Create &Mesh", this, SLOT(addMesh()), Qt::ALT+Qt::Key_M ); + edit->addAction("Add &Alpha-blended image", this, SLOT(addButterfly()), Qt::ALT+Qt::Key_A); + menu->addMenu(edit); + + QMenu* view = new QMenu("&View", menu ); + view->addAction("&Enlarge", this, SLOT(enlarge()), Qt::SHIFT+Qt::CTRL+Qt::Key_Plus); + view->addAction("Shr&ink", this, SLOT(shrink()), Qt::SHIFT+Qt::CTRL+Qt::Key_Minus); + view->addSeparator(); + view->addAction("&Rotate clockwise", this, SLOT(rotateClockwise()), Qt::CTRL+Qt::Key_PageDown); + view->addAction("Rotate &counterclockwise", this, SLOT(rotateCounterClockwise()), Qt::CTRL+Qt::Key_PageUp); + view->addAction("&Zoom in", this, SLOT(zoomIn()), Qt::CTRL+Qt::Key_Plus); + view->addAction("Zoom &out", this, SLOT(zoomOut()), Qt::CTRL+Qt::Key_Minus); + view->addAction("Translate left", this, SLOT(moveL()), Qt::CTRL+Qt::Key_Left); + view->addAction("Translate right", this, SLOT(moveR()), Qt::CTRL+Qt::Key_Right); + view->addAction("Translate up", this, SLOT(moveU()), Qt::CTRL+Qt::Key_Up); + view->addAction("Translate down", this, SLOT(moveD()), Qt::CTRL+Qt::Key_Down); + view->addAction("&Mirror", this, SLOT(mirror()), Qt::CTRL+Qt::Key_Home); + menu->addMenu(view); + + menu->addSeparator(); + + QMenu* help = new QMenu("&Help", menu ); + help->addAction("&About", this, SLOT(help()), Qt::Key_F1); + menu->addMenu(help); statusBar(); setCentralWidget(editor); +#if !defined(Q_OS_SYMBIAN) printer = 0; +#endif init(); } @@ -397,7 +390,9 @@ void Main::init() Main::~Main() { +#if !defined(Q_OS_SYMBIAN) delete printer; +#endif if ( !--mainCount ) { delete[] butterflyimg; butterflyimg = 0; @@ -409,7 +404,7 @@ Main::~Main() void Main::newView() { // Open a new view... have it delete when closed. - Main *m = new Main(canvas, 0, 0, Qt::WDestructiveClose); + Main *m = new Main(canvas, 0); // AKr, Qt::WA_DeleteOnClose); m->show(); } @@ -428,7 +423,7 @@ void Main::help() "<li> Press ALT-L for some lines." "<li> Drag the objects around." "<li> Read the code!" - "</ul>", QMessageBox::Information, 1, 0, 0, this, 0, FALSE ); + "</ul>", QMessageBox::Information, 1, 0, 0, this, 0); about->setButtonText( 1, "Dismiss" ); about->show(); } @@ -495,11 +490,14 @@ void Main::moveD() void Main::print() { +#if !defined(Q_OS_SYMBIAN) if ( !printer ) printer = new QPrinter; - if ( printer->setup(this) ) { - QPainter pp(printer); + QPrintDialog dialog(printer, this); + if(dialog.exec()) { + QPainter pp(printer); canvas.render(&pp); } +#endif } @@ -522,12 +520,12 @@ void Main::addButterfly() if ( !butterflyimg ) { butterflyimg = new QImage[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) ); + butterflyimg[1] = butterflyimg[0].scaled( int(butterflyimg[0].width()*0.75), + int(butterflyimg[0].height()*0.75), Qt::IgnoreAspectRatio, Qt::SmoothTransformation); + butterflyimg[2] = butterflyimg[0].scaled( int(butterflyimg[0].width()*0.5), + int(butterflyimg[0].height()*0.5), Qt::IgnoreAspectRatio, Qt::SmoothTransformation); + butterflyimg[3] = butterflyimg[0].scaled( int(butterflyimg[0].width()*0.25), + int(butterflyimg[0].height()*0.25), Qt::IgnoreAspectRatio, Qt::SmoothTransformation); } QAbstractGraphicsShapeItem* i = new ImageItem(butterflyimg[qrand()%4]); canvas.addItem(i); @@ -543,12 +541,12 @@ void Main::addLogo() if ( !logoimg ) { logoimg = new QImage[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) ); + logoimg[1] = logoimg[0].scaled( int(logoimg[0].width()*0.75), + int(logoimg[0].height()*0.75), Qt::IgnoreAspectRatio, Qt::SmoothTransformation); + logoimg[2] = logoimg[0].scaled( int(logoimg[0].width()*0.5), + int(logoimg[0].height()*0.5), Qt::IgnoreAspectRatio, Qt::SmoothTransformation); + logoimg[3] = logoimg[0].scaled( int(logoimg[0].width()*0.25), + int(logoimg[0].height()*0.25), Qt::IgnoreAspectRatio, Qt::SmoothTransformation); } QAbstractGraphicsShapeItem* i = new ImageItem(logoimg[qrand()%4]); canvas.addItem(i); @@ -572,14 +570,15 @@ void Main::addCircle() void Main::addHexagon() { const int size = int(canvas.width() / 25); - Q3PointArray pa(6); - pa[0] = QPoint(2*size,0); - pa[1] = QPoint(size,-size*173/100); - pa[2] = QPoint(-size,-size*173/100); - pa[3] = QPoint(-2*size,0); - pa[4] = QPoint(-size,size*173/100); - pa[5] = QPoint(size,size*173/100); - QGraphicsPolygonItem* i = canvas.addPolygon(pa); + QPolygon polygon; + polygon << QPoint(2*size,0) + << QPoint(size,-size*173/100) + << QPoint(-size,-size*173/100) + << QPoint(-2*size,0) + << QPoint(-size,size*173/100) + << QPoint(size,size*173/100); + + QGraphicsPolygonItem* i = canvas.addPolygon(polygon); i->setFlag(QGraphicsItem::ItemIsMovable); i->setPen(Qt::NoPen); i->setBrush( QColor(qrand()%32*8,qrand()%32*8,qrand()%32*8) ); @@ -590,14 +589,15 @@ void Main::addHexagon() void Main::addPolygon() { const int size = int(canvas.width()/2); - Q3PointArray pa(6); - pa[0] = QPoint(0,0); - pa[1] = QPoint(size,size/5); - pa[2] = QPoint(size*4/5,size); - pa[3] = QPoint(size/6,size*5/4); - pa[4] = QPoint(size*3/4,size*3/4); - pa[5] = QPoint(size*3/4,size/4); - QGraphicsPolygonItem* i = canvas.addPolygon(pa); + QPolygon polygon; + polygon << QPoint(0,0) + << QPoint(size,size/5) + << QPoint(size*4/5,size) + << QPoint(size/6,size*5/4) + << QPoint(size*3/4,size*3/4) + << QPoint(size*3/4,size/4); + + QGraphicsPolygonItem* i = canvas.addPolygon(polygon); i->setFlag(QGraphicsItem::ItemIsMovable); i->setPen(Qt::NoPen); i->setBrush( QColor(qrand()%32*8,qrand()%32*8,qrand()%32*8) ); @@ -609,24 +609,24 @@ void Main::addSpline() { const int size = int(canvas.width()/6); - Q3PointArray pa(12); - pa[0] = QPoint(0,0); - pa[1] = QPoint(size/2,0); - pa[2] = QPoint(size,size/2); - pa[3] = QPoint(size,size); - pa[4] = QPoint(size,size*3/2); - pa[5] = QPoint(size/2,size*2); - pa[6] = QPoint(0,size*2); - pa[7] = QPoint(-size/2,size*2); - pa[8] = QPoint(size/4,size*3/2); - pa[9] = QPoint(0,size); - pa[10]= QPoint(-size/4,size/2); - pa[11]= QPoint(-size/2,0); + QPolygon polygon; + polygon << QPoint(0,0) + << QPoint(size/2,0) + << QPoint(size,size/2) + << QPoint(size,size) + << QPoint(size,size*3/2) + << QPoint(size/2,size*2) + << QPoint(0,size*2) + << QPoint(-size/2,size*2) + << QPoint(size/4,size*3/2) + << QPoint(0,size) + << QPoint(-size/4,size/2) + << QPoint(-size/2,0); QPainterPath path; - path.moveTo(pa[0]); - for (int i = 1; i < pa.size(); i += 3) - path.cubicTo(pa[i], pa[(i + 1) % pa.size()], pa[(i + 2) % pa.size()]); + path.moveTo(polygon[0]); + for (int i = 1; i < polygon.size(); i += 3) + path.cubicTo(polygon[i], polygon[(i + 1) % polygon.size()], polygon[(i + 2) % polygon.size()]); QGraphicsPathItem* item = canvas.addPath(path); item->setFlag(QGraphicsItem::ItemIsMovable); @@ -671,13 +671,12 @@ void Main::addMesh() int cols = w / dist; #ifndef QT_NO_PROGRESSDIALOG - Q3ProgressDialog progress( "Creating mesh...", "Abort", rows, - this, "progress", TRUE ); + QProgressDialog progress("Creating mesh...", "Abort", 0, rows, this); #endif canvas.update(); - Q3MemArray<NodeItem*> lastRow(cols); + QVector<NodeItem*> lastRow(cols); for ( int j = 0; j < rows; j++ ) { int n = j%2 ? cols-1 : cols; NodeItem *prev = 0; @@ -707,13 +706,13 @@ void Main::addMesh() } lastRow[n-1]=prev; #ifndef QT_NO_PROGRESSDIALOG - progress.setProgress( j ); - if ( progress.wasCancelled() ) + progress.setValue( j ); + if ( progress.wasCanceled() ) break; #endif } #ifndef QT_NO_PROGRESSDIALOG - progress.setProgress( rows ); + progress.setValue( rows ); #endif // qDebug( "%d nodes, %d edges", nodecount, EdgeItem::count() ); } |