karbon

vqpainter.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2001, The Karbon Developers
3 Copyright (C) 2002, The Karbon Developers
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19*/
20
21// qpainter wrapper
22
23#include "vqpainter.h"
24#include "vstroke.h"
25#include "vcolor.h"
26#include "vfill.h"
27
28#include <tqpainter.h>
29#include <tqpaintdevice.h>
30#include <tqpen.h>
31
32#include <KoPoint.h>
33#include <kdebug.h>
34
35VTQPainter::VTQPainter( TQPaintDevice *target, unsigned int w, unsigned int h ) : VPainter( target, w, h ), m_painter( 0L ), m_target( target ), m_width( w ), m_height( h )
36{
37 m_zoomFactor = 1;
38 m_index = 0;
39 m_painter = new TQPainter( target );
40}
41
42VTQPainter::~VTQPainter()
43{
44 delete m_painter;
45}
46
47void
48VTQPainter::resize( unsigned int w, unsigned int h )
49{
50 m_width = w;
51 m_height = h;
52}
53
54void
55VTQPainter::blit( const KoRect & )
56{
57 end();
58}
59
60void
61VTQPainter::clear( const TQColor &c )
62{
63 m_painter->setBackgroundColor( c );
64 m_painter->eraseRect( 0, 0, m_width, m_height );
65}
66
67void
68VTQPainter::begin()
69{
70 if( !m_painter->isActive() )
71 {
72 m_painter->begin( m_target );
73 m_painter->eraseRect( 0, 0, m_width, m_height );
74 }
75}
76
77void
78VTQPainter::end()
79{
80 m_painter->end();
81}
82
83const TQWMatrix
84VTQPainter::worldMatrix()
85{
86 return m_painter->worldMatrix();
87}
88
89void
90VTQPainter::setWorldMatrix( const TQWMatrix& mat )
91{
92 m_painter->setWorldMatrix( mat );
93}
94
95void
96VTQPainter::setZoomFactor( double zoomFactor )
97{
98 m_zoomFactor = zoomFactor;
99 /*TQWMatrix mat;
100 mat.scale( zoomFactor, zoomFactor );
101 m_painter->setWorldMatrix( mat );*/
102}
103
104void
105VTQPainter::moveTo( const KoPoint &p )
106{
107 //m_index = 0;
108 if( m_pa.size() <= m_index )
109 m_pa.resize( m_index + 10 );
110
111 m_pa.setPoint( m_index, static_cast<int>(p.x() * m_zoomFactor), static_cast<int>(p.y() * m_zoomFactor) );
112
113 m_index++;
114}
115
116void
117VTQPainter::lineTo( const KoPoint &p )
118{
119 if( m_pa.size() <= m_index )
120 m_pa.resize( m_index + 10 );
121
122 m_pa.setPoint( m_index, static_cast<int>(p.x() * m_zoomFactor), static_cast<int>(p.y() * m_zoomFactor) );
123
124 m_index++;
125}
126
127void
128VTQPainter::curveTo( const KoPoint &p1, const KoPoint &p2, const KoPoint &p3 )
129{
130 // calculate cubic bezier using a temp TQPointArray
131 TQPointArray pa( 4 );
132 pa.setPoint( 0, m_pa.point( m_index - 1 ).x(), m_pa.point( m_index - 1 ).y() );
133 pa.setPoint( 1, static_cast<int>(p1.x() * m_zoomFactor), static_cast<int>(p1.y() * m_zoomFactor) );
134 pa.setPoint( 2, static_cast<int>(p2.x() * m_zoomFactor), static_cast<int>(p2.y() * m_zoomFactor) );
135 pa.setPoint( 3, static_cast<int>(p3.x() * m_zoomFactor), static_cast<int>(p3.y() * m_zoomFactor) );
136
137 TQPointArray pa2( pa.cubicBezier() );
138
139 m_pa.resize( m_index + pa2.size() );
140 m_pa.putPoints( m_index, pa2.size(), pa2 );
141
142 m_index += pa2.size();
143}
144
145void
146VTQPainter::newPath()
147{
148 m_index = 0;
149}
150
151void
152VTQPainter::fillPath()
153{
154 // we probably dont need filling for qpainter
155 //m_index = 0;
156 m_painter->drawPolygon( m_pa, false, 0, m_index );
157}
158
159void
160VTQPainter::strokePath()
161{
162 m_painter->drawPolyline( m_pa, 0, m_index );
163 m_index = 0;
164}
165
166void
167VTQPainter::setPen( const VStroke &stroke )
168{
169 TQPen pen;
170
171 // color + linewidth
172 pen.setColor( stroke.color() );
173 pen.setWidth( static_cast<int>(stroke.lineWidth()) );
174
175 // caps
176 if( stroke.lineCap() == VStroke::capButt )
177 pen.setCapStyle( TQt::FlatCap );
178 else if( stroke.lineCap() == VStroke::capRound )
179 pen.setCapStyle( TQt::RoundCap );
180 else if( stroke.lineCap() == VStroke::capSquare )
181 pen.setCapStyle( TQt::SquareCap );
182
183 m_painter->setPen( pen );
184}
185
186void
187VTQPainter::setBrush( const VFill &fill )
188{
189 switch( fill.type() )
190 {
191 case VFill::none:
192 m_painter->setBrush( TQt::NoBrush );
193 break;
194 case VFill::solid:
195 m_painter->setBrush( TQBrush( fill.color(), TQt::SolidPattern ) );
196 break;
197 case VFill::grad:
198 // gradients are nor supported by qpainter
199 m_painter->setBrush( TQt::NoBrush );
200 break;
201 case VFill::patt:
202 // pixmap brushes not supported for printing
203 m_painter->setBrush( TQBrush( fill.color(), fill.pattern().pixmap() ) );
204 break;
205 default:
206 break;
207 }
208}
209
210void
211VTQPainter::setPen( const TQColor &c )
212{
213 m_painter->setPen( c );
214}
215
216void
217VTQPainter::setPen( TQt::PenStyle style )
218{
219 m_painter->setPen( style );
220}
221
222void
223VTQPainter::setBrush( const TQColor &c )
224{
225 m_painter->setBrush( c );
226}
227
228void
229VTQPainter::setBrush( TQt::BrushStyle style )
230{
231 m_painter->setBrush( style );
232}
233
234void
235VTQPainter::save()
236{
237 m_painter->save();
238}
239
240void
241VTQPainter::restore()
242{
243 m_painter->restore();
244}
245
246void
247VTQPainter::setRasterOp( TQt::RasterOp r )
248{
249 m_painter->setRasterOp( r );
250}
251
252void
253VTQPainter::drawNode( const KoPoint &p, int width )
254{
255 m_painter->drawRect( TQRect( int( p.x() * m_zoomFactor ) - width, int( p.y() * m_zoomFactor ) - width,
256 2 * width + 1, 2 * width + 1 ) );
257}
258
259void
260VTQPainter::drawRect( const KoRect &rect )
261{
262 m_painter->drawRect( TQRect( int( rect.x() ), int( rect.y() ), int( rect.width() ), int( rect.height() ) ) );
263}
264
265void
266VTQPainter::drawImage( const TQImage &image, const TQWMatrix &affine )
267{
268 TQWMatrix matrix = m_painter->worldMatrix();
269
270 double m11 = affine.m11() * matrix.m11() * m_zoomFactor + affine.m12() * matrix.m21();
271 double m12 = (affine.m11() * matrix.m12() + affine.m12() * matrix.m22() ) * m_zoomFactor;
272 double m21 = (affine.m21() * matrix.m11() + affine.m22() * matrix.m21() ) * m_zoomFactor;
273 double m22 = affine.m22() * matrix.m22() * m_zoomFactor + affine.m21() * matrix.m12();
274 double dx = matrix.dx() + affine.dx() * m_zoomFactor;
275 double dy = matrix.dy() - affine.dy() * m_zoomFactor;
276
277 TQWMatrix world( m11, m12, m21, m22, dx, dy );
278
279 m_painter->setWorldMatrix( world );
280
281 m_painter->drawImage( TQPoint( 0, 0 ), image );
282 // restore old world matrix
283 m_painter->setWorldMatrix( matrix );
284}
Manages the fill of shapes.
Definition vfill.h:45
@ solid
no fill at all
Definition vfill.h:50
@ grad
solid fill
Definition vfill.h:51
@ patt
gradient fill
Definition vfill.h:52
Manages stroke properties.
Definition vstroke.h:49