diff options
author | Timothy Pearson <kb9vqf@pearsoncomputing.net> | 2012-09-17 11:49:39 -0500 |
---|---|---|
committer | Timothy Pearson <kb9vqf@pearsoncomputing.net> | 2012-09-17 11:49:39 -0500 |
commit | b6cb4612ca9c615323c0540a3b05edc24feec642 (patch) | |
tree | 7c14ed41a50285e015d351a8b2029aba9d6e7f4b | |
parent | 52cddfb3039519e7745e770b5198ff43cb9d195f (diff) | |
download | gtk3-tqt-engine-b6cb4612ca9c615323c0540a3b05edc24feec642.tar.gz gtk3-tqt-engine-b6cb4612ca9c615323c0540a3b05edc24feec642.zip |
Add transformation support
-rw-r--r-- | tdegtk/tqtcairopainter.cpp | 126 | ||||
-rw-r--r-- | tdegtk/tqtcairopainter.h | 6 | ||||
-rw-r--r-- | tests/open.png | bin | 0 -> 20782 bytes | |||
-rw-r--r-- | tests/test-painter.cpp | 34 |
4 files changed, 144 insertions, 22 deletions
diff --git a/tdegtk/tqtcairopainter.cpp b/tdegtk/tqtcairopainter.cpp index 05e9a0a..2f16d11 100644 --- a/tdegtk/tqtcairopainter.cpp +++ b/tdegtk/tqtcairopainter.cpp @@ -680,6 +680,23 @@ void TQt3CairoPaintDevice::drawText(TQPainter *p, int x, int y, const TQString & g_object_unref(layout); } +void TQt3CairoPaintDevice::setCairoTransformations() { + cairo_matrix_t combinedMatrix; + cairo_matrix_t tempMatrix; + + cairo_matrix_init_identity(&combinedMatrix); + if (m_worldMatrixEnabled) { + cairo_matrix_multiply(&tempMatrix, &combinedMatrix, &m_worldMatrix); + combinedMatrix = tempMatrix; + } + if (m_viewportMatrixEnabled) { + cairo_matrix_multiply(&tempMatrix, &combinedMatrix, &m_viewportMatrix); + combinedMatrix = tempMatrix; + } + + cairo_set_matrix(m_painter, &combinedMatrix); +} + /*! \class TQt3CairoPaintDevice tdeqt4painter.h \brief The TQt3CairoPaintDevice class is a paint device that translates @@ -927,13 +944,20 @@ bool TQt3CairoPaintDevice::cmd( int c, TQPainter *pt, TQPDevCmdParam *p ) cairo_restore(m_painter); } break; -#if 0 case PdcDrawText: - // NOTE - // drawText baseline = FALSE for this! - m_qt4painter->drawText( qt4point1, qt4string ); + if (m_painter) { + cairo_save(m_painter); + if (p) { + TQString string = *p[1].str; + drawText(pt, p[0].rect->x()+CAIRO_PIXEL_OFFSET, p[0].rect->y()+CAIRO_PIXEL_OFFSET, string, 0, -1, TQPainter::Auto, TRUE); + } + cairo_restore(m_painter); + } break; +#if 0 case PdcDrawTextFormatted: + // NOTE + // Unlike PdcDrawText and PdcDrawText2, PdcDrawTextFormatted and PdcDrawText2Formatted do NOT use the baseline as the Y position coordinate! m_qt4painter->drawText( qt4rect, qt4formattedtextflags, qt4string ); break; #endif @@ -944,14 +968,27 @@ bool TQt3CairoPaintDevice::cmd( int c, TQPainter *pt, TQPDevCmdParam *p ) TQString string = *p[1].str; drawText(pt, p[0].rect->x()+CAIRO_PIXEL_OFFSET, p[0].rect->y()+CAIRO_PIXEL_OFFSET, string, 0, -1, TQPainter::Auto, TRUE); } + cairo_restore(m_painter); } break; #if 0 case PdcDrawText2Formatted: m_qt4painter->drawText( qt4rect, qt4formattedtextflags, qt4string ); break; +#endif case PdcDrawPixmap: - m_qt4painter->drawPixmap( qt4rect, qt4pixmap ); + if (m_painter) { + cairo_save(m_painter); + if (p) { + TQImage sourceImage = p[1].pixmap->convertToImage(); + cairo_surface_t* sourceSurface = TQImageToCairoSurface(sourceImage); + cairo_rectangle(m_painter, p[0].rect->x(), p[0].rect->y(), p[0].rect->width(), p[0].rect->height()); + cairo_set_source_surface(m_painter, sourceSurface, p[0].rect->x(), p[0].rect->y()); + cairo_fill(m_painter); + cairo_surface_destroy(sourceSurface); + } + cairo_restore(m_painter); + } break; #if 0 case PdcDrawImage: { @@ -966,7 +1003,6 @@ bool TQt3CairoPaintDevice::cmd( int c, TQPainter *pt, TQPDevCmdParam *p ) } break; #endif -#endif case PdcBegin: if (!m_painter) { m_bgColor = TQColor(0,0,0); @@ -975,6 +1011,11 @@ bool TQt3CairoPaintDevice::cmd( int c, TQPainter *pt, TQPDevCmdParam *p ) m_pen = TQPen(); m_brush = TQBrush(); m_brushOrigin = TQPoint(0,0); + m_worldMatrixEnabled = false; + m_viewportMatrixEnabled = false; + cairo_matrix_init_identity(&m_worldMatrix); + cairo_matrix_init_identity(&m_viewportMatrix); + setCairoTransformations(); } break; case PdcEnd: @@ -1098,7 +1139,6 @@ bool TQt3CairoPaintDevice::cmd( int c, TQPainter *pt, TQPDevCmdParam *p ) } break; #if 0 -#if 0 case PdcSetTabStops: s >> i_16; painter->setTabStops( i_16 ); @@ -1118,32 +1158,74 @@ bool TQt3CairoPaintDevice::cmd( int c, TQPainter *pt, TQPDevCmdParam *p ) delete [] ta; } break; - case PdcSetVXform: - s >> i_8; -#ifndef QT_NO_TRANSFORMATIONS - painter->setViewXForm( i_8 ); #endif + case PdcSetVXform: + if ((p) && (m_painter)) { + m_viewportMatrixEnabled = p[0].ival; + setCairoTransformations(); + } break; case PdcSetWindow: - s >> r; -#ifndef QT_NO_TRANSFORMATIONS - painter->setWindow( r ); -#endif + if ((p) && (m_painter) && (pt)) { + TQRect viewportRect = pt->viewport(); + int wx = p[0].rect->x(); + int wy = p[0].rect->y(); + int ww = p[0].rect->width(); + int wh = p[0].rect->height(); + int vx = viewportRect.x(); + int vy = viewportRect.y(); + int vw = viewportRect.width(); + int vh = viewportRect.height(); + double scaleW = (double)vw/(double)ww; + double scaleH = (double)vh/(double)wh; + cairo_matrix_init(&m_viewportMatrix, scaleW, 0, 0, scaleH, vx - wx*scaleW, vy - wy*scaleH); + setCairoTransformations(); + } break; case PdcSetViewport: - s >> r; -#ifndef QT_NO_TRANSFORMATIONS - painter->setViewport( r ); -#endif + if ((p) && (m_painter) && (pt)) { + TQRect windowRect = pt->window(); + int wx = windowRect.x(); + int wy = windowRect.y(); + int ww = windowRect.width(); + int wh = windowRect.height(); + int vx = p[0].rect->x(); + int vy = p[0].rect->y(); + int vw = p[0].rect->width(); + int vh = p[0].rect->height(); + double scaleW = (double)vw/(double)ww; + double scaleH = (double)vh/(double)wh; + cairo_matrix_init(&m_viewportMatrix, scaleW, 0, 0, scaleH, vx - wx*scaleW, vy - wy*scaleH); + setCairoTransformations(); + } break; -#endif case PdcSetWXform: - m_qt4painter->setWorldMatrixEnabled( p[0].ival ); + if ((p) && (m_painter)) { + m_worldMatrixEnabled = p[0].ival; + setCairoTransformations(); + } break; case PdcSetWMatrix: - m_qt4painter->setWorldMatrix( qt4matrix, p[1].ival ); + if ((p) && (m_painter)) { + const TQWMatrix* tqt3matrix = p[0].matrix; + if (tqt3matrix) { + if (p[1].ival) { + // Combine + cairo_matrix_t new_matrix; + cairo_matrix_t original_matrix = m_worldMatrix; + cairo_matrix_init(&new_matrix, tqt3matrix->m11(), tqt3matrix->m12(), tqt3matrix->m21(), tqt3matrix->m22(), tqt3matrix->dx(), tqt3matrix->dy()); + cairo_matrix_multiply(&m_worldMatrix, &original_matrix, &new_matrix); + } + else { + // Replace + cairo_matrix_init(&m_worldMatrix, tqt3matrix->m11(), tqt3matrix->m12(), tqt3matrix->m21(), tqt3matrix->m22(), tqt3matrix->dx(), tqt3matrix->dy()); + } + setCairoTransformations(); + } + } break; #if 0 +#if 0 #ifndef QT_NO_TRANSFORMATIONS case PdcSaveWMatrix: painter->saveWorldMatrix(); diff --git a/tdegtk/tqtcairopainter.h b/tdegtk/tqtcairopainter.h index 99a3dc2..9700b63 100644 --- a/tdegtk/tqtcairopainter.h +++ b/tdegtk/tqtcairopainter.h @@ -59,10 +59,16 @@ class Q_EXPORT TQt3CairoPaintDevice : public TQPaintDevice // picture class void pangoSetupTextPath(PangoLayout *layout, const char* text); void drawText(TQPainter *p, int x, int y, const TQString &str, int pos, int len, TQPainter::TextDirection dir, bool baseline=TRUE); + + void setCairoTransformations(); private: cairo_surface_t *m_surface; cairo_t *m_painter; + cairo_matrix_t m_worldMatrix; + cairo_matrix_t m_viewportMatrix; + bool m_worldMatrixEnabled; + bool m_viewportMatrixEnabled; TQColor m_bgColor; TQt::BGMode m_bgColorMode; diff --git a/tests/open.png b/tests/open.png Binary files differnew file mode 100644 index 0000000..79beaf8 --- /dev/null +++ b/tests/open.png diff --git a/tests/test-painter.cpp b/tests/test-painter.cpp index 0fb811c..6979af4 100644 --- a/tests/test-painter.cpp +++ b/tests/test-painter.cpp @@ -8,6 +8,31 @@ // TQt3 test image only #include <tqpixmap.h> +void drawColorWheel(TQPainter *p, double scale) +{ + TQFont f( "times", 18, TQFont::Bold ); + p->setFont( f ); + p->setPen(TQt::black); + p->setWindow( 0, 0, 500*scale, 500*scale ); // defines coordinate system + + for ( int i=0; i<36; i++ ) { // draws 36 rotated rectangles + TQWMatrix matrix; + matrix.translate( 250.0F*scale, 250.0F*scale ); // move to center + matrix.shear( 0.0F*scale, 0.3F*scale ); // twist it + matrix.rotate( (float)i*10 ); // rotate 0,10,20,.. degrees + p->setWorldMatrix( matrix ); // use this world matrix + + TQColor c; + c.setHsv( i*10, 255, 255 ); // rainbow effect + p->setBrush( c ); // solid fill with color c + p->drawRect( 70*scale, -10*scale, 80*scale, 10*scale ); // draw the rectangle + + TQString n; + n.sprintf( "H=%d", i*10 ); + p->drawText( (80+70+5)*scale, 0, n ); // draw the hue number + } +} + void runTests(TQPaintDevice* pd) { TQPainter p(pd); @@ -136,6 +161,12 @@ void runTests(TQPaintDevice* pd) { p.drawCubicBezier(a); } + // Pixmap tests + { + TQPixmap pixmap("open.png"); + p.drawPixmap(200, 10, pixmap, 0, 0, -1, -1); + } + // Font tests { static const char *fonts[] = { "Helvetica", "Courier", "Times", 0 }; @@ -163,6 +194,9 @@ void runTests(TQPaintDevice* pd) { } } + //drawColorWheel(&p, 0.5); + //drawColorWheel(&p, 1.0); + p.end(); } |