karbon

vqpainter.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 2001, The Karbon Developers
00003    Copyright (C) 2002, The Karbon Developers
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
00019 */
00020 
00021 // qpainter wrapper
00022 
00023 #include "vqpainter.h"
00024 #include "vstroke.h"
00025 #include "vcolor.h"
00026 #include "vfill.h"
00027 
00028 #include <qpainter.h>
00029 #include <qpaintdevice.h>
00030 #include <qpen.h>
00031 
00032 #include <KoPoint.h>
00033 #include <kdebug.h>
00034 
00035 VQPainter::VQPainter( QPaintDevice *target, unsigned int w, unsigned int h ) : VPainter( target, w, h ), m_painter( 0L ), m_target( target ), m_width( w ), m_height( h )
00036 {
00037     m_zoomFactor = 1;
00038     m_index = 0;
00039     m_painter = new QPainter( target );
00040 }
00041 
00042 VQPainter::~VQPainter()
00043 {
00044     delete m_painter;
00045 }
00046 
00047 void
00048 VQPainter::resize( unsigned int w, unsigned int h )
00049 {
00050     m_width = w;
00051     m_height = h;
00052 }
00053 
00054 void
00055 VQPainter::blit( const KoRect & )
00056 {
00057     end();
00058 }
00059 
00060 void
00061 VQPainter::clear( const QColor &c )
00062 {
00063     m_painter->setBackgroundColor( c );
00064     m_painter->eraseRect( 0, 0, m_width, m_height );
00065 }
00066 
00067 void
00068 VQPainter::begin()
00069 {
00070     if( !m_painter->isActive() )
00071     {
00072         m_painter->begin( m_target );
00073         m_painter->eraseRect( 0, 0, m_width, m_height );
00074     }
00075 }
00076 
00077 void
00078 VQPainter::end()
00079 {
00080     m_painter->end();
00081 }
00082 
00083 const QWMatrix
00084 VQPainter::worldMatrix()
00085 {
00086     return m_painter->worldMatrix();
00087 }
00088 
00089 void
00090 VQPainter::setWorldMatrix( const QWMatrix& mat )
00091 {
00092     m_painter->setWorldMatrix( mat );
00093 }
00094 
00095 void
00096 VQPainter::setZoomFactor( double zoomFactor )
00097 {
00098     m_zoomFactor = zoomFactor;
00099     /*QWMatrix mat;
00100     mat.scale( zoomFactor, zoomFactor );
00101     m_painter->setWorldMatrix( mat );*/
00102 }
00103 
00104 void 
00105 VQPainter::moveTo( const KoPoint &p )
00106 {
00107     //m_index = 0;
00108     if( m_pa.size() <= m_index )
00109         m_pa.resize( m_index + 10 );
00110 
00111     m_pa.setPoint( m_index, static_cast<int>(p.x() * m_zoomFactor), static_cast<int>(p.y() * m_zoomFactor) );
00112 
00113     m_index++;
00114 }
00115 
00116 void 
00117 VQPainter::lineTo( const KoPoint &p )
00118 {
00119     if( m_pa.size() <= m_index )
00120         m_pa.resize( m_index + 10 );
00121 
00122     m_pa.setPoint( m_index, static_cast<int>(p.x() * m_zoomFactor), static_cast<int>(p.y() * m_zoomFactor) );
00123 
00124     m_index++;
00125 }
00126 
00127 void
00128 VQPainter::curveTo( const KoPoint &p1, const KoPoint &p2, const KoPoint &p3 )
00129 {
00130     // calculate cubic bezier using a temp QPointArray
00131     QPointArray pa( 4 );
00132     pa.setPoint( 0, m_pa.point( m_index - 1 ).x(), m_pa.point( m_index - 1 ).y() );
00133     pa.setPoint( 1, static_cast<int>(p1.x() * m_zoomFactor), static_cast<int>(p1.y() * m_zoomFactor) );
00134     pa.setPoint( 2, static_cast<int>(p2.x() * m_zoomFactor), static_cast<int>(p2.y() * m_zoomFactor) );
00135     pa.setPoint( 3, static_cast<int>(p3.x() * m_zoomFactor), static_cast<int>(p3.y() * m_zoomFactor) );
00136 
00137     QPointArray pa2( pa.cubicBezier() );
00138 
00139     m_pa.resize( m_index + pa2.size() );
00140     m_pa.putPoints( m_index, pa2.size(), pa2 );
00141 
00142     m_index += pa2.size();
00143 }
00144 
00145 void
00146 VQPainter::newPath()
00147 {
00148     m_index = 0;
00149 }
00150 
00151 void
00152 VQPainter::fillPath()
00153 {
00154     // we probably dont need filling for qpainter
00155     //m_index = 0;
00156     m_painter->drawPolygon( m_pa, FALSE, 0, m_index );
00157 }
00158 
00159 void
00160 VQPainter::strokePath()
00161 {
00162     m_painter->drawPolyline( m_pa, 0, m_index );
00163     m_index = 0;
00164 }
00165 
00166 void
00167 VQPainter::setPen( const VStroke &stroke )
00168 {
00169     QPen pen;
00170 
00171     // color + linewidth
00172     pen.setColor( stroke.color() );
00173     pen.setWidth( static_cast<int>(stroke.lineWidth()) );
00174 
00175     // caps
00176     if( stroke.lineCap() == VStroke::capButt )
00177         pen.setCapStyle( Qt::FlatCap );
00178     else if( stroke.lineCap() == VStroke::capRound )
00179         pen.setCapStyle( Qt::RoundCap );
00180     else if( stroke.lineCap() == VStroke::capSquare )
00181         pen.setCapStyle( Qt::SquareCap );
00182 
00183     m_painter->setPen( pen );
00184 }
00185 
00186 void
00187 VQPainter::setBrush( const VFill &fill )
00188 {
00189     switch( fill.type() )
00190     {
00191         case VFill::none:
00192             m_painter->setBrush( Qt::NoBrush );
00193         break;
00194         case VFill::solid:
00195             m_painter->setBrush( QBrush( fill.color(), Qt::SolidPattern ) );
00196         break;
00197         case VFill::grad:
00198             // gradients are nor supported by qpainter
00199             m_painter->setBrush( Qt::NoBrush );
00200         break;
00201         case VFill::patt:
00202             // pixmap brushes not supported for printing
00203             m_painter->setBrush( QBrush( fill.color(), fill.pattern().pixmap() ) );
00204         break;
00205         default:
00206         break;
00207     }
00208 }
00209 
00210 void
00211 VQPainter::setPen( const QColor &c )
00212 {
00213     m_painter->setPen( c );
00214 }
00215 
00216 void
00217 VQPainter::setPen( Qt::PenStyle style )
00218 {
00219     m_painter->setPen( style );
00220 }
00221 
00222 void
00223 VQPainter::setBrush( const QColor &c )
00224 {
00225     m_painter->setBrush( c );
00226 }
00227 
00228 void
00229 VQPainter::setBrush( Qt::BrushStyle style )
00230 {
00231     m_painter->setBrush( style );
00232 }
00233 
00234 void
00235 VQPainter::save()
00236 {
00237     m_painter->save();
00238 }
00239 
00240 void
00241 VQPainter::restore()
00242 {
00243     m_painter->restore();
00244 }
00245 
00246 void
00247 VQPainter::setRasterOp( Qt::RasterOp r )
00248 {
00249     m_painter->setRasterOp( r );
00250 }
00251 
00252 void
00253 VQPainter::drawNode( const KoPoint &p, int width )
00254 {
00255     m_painter->drawRect( QRect( int( p.x() * m_zoomFactor ) - width, int( p.y() * m_zoomFactor ) - width,
00256                                 2 * width + 1,  2 * width + 1 ) );
00257 }
00258 
00259 void
00260 VQPainter::drawRect( const KoRect &rect )
00261 {
00262     m_painter->drawRect( QRect( int( rect.x() ), int( rect.y() ), int( rect.width() ),  int( rect.height() ) ) );
00263 }
00264 
00265 void
00266 VQPainter::drawImage( const QImage &image, const QWMatrix &affine )
00267 {
00268     QWMatrix matrix = m_painter->worldMatrix();
00269     
00270     double m11 = affine.m11() * matrix.m11() * m_zoomFactor + affine.m12() * matrix.m21();
00271     double m12 = (affine.m11() * matrix.m12() + affine.m12() * matrix.m22() ) * m_zoomFactor;
00272     double m21 = (affine.m21() * matrix.m11() + affine.m22() * matrix.m21() ) * m_zoomFactor;
00273     double m22 = affine.m22() * matrix.m22() * m_zoomFactor + affine.m21() * matrix.m12();
00274     double dx = matrix.dx() + affine.dx() * m_zoomFactor;
00275     double dy = matrix.dy() - affine.dy() * m_zoomFactor;
00276     
00277     QWMatrix world( m11, m12, m21, m22, dx, dy );
00278     
00279     m_painter->setWorldMatrix( world );
00280     
00281     m_painter->drawImage( QPoint( 0, 0 ), image );
00282     // restore old world matrix
00283     m_painter->setWorldMatrix( matrix );
00284 }
KDE Home | KDE Accessibility Home | Description of Access Keys