karbon

vstrokefillpreview.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2002 - 2005, The Karbon Developers
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#include <tqcolor.h>
21
22#include <kdebug.h>
23#include <KoPoint.h>
24
25#include "karbon_part.h"
26#include "vcolordlg.h"
27#include "vfill.h"
28#include "vfillcmd.h"
29#include "vkopainter.h"
30#include "vselection.h"
31#include "vstroke.h"
32#include "vstrokecmd.h"
33#include "vstrokefillpreview.h"
34
35#define PANEL_SIZEX 50.0
36#define PANEL_SIZEY 50.0
37
38#define FILL_TOPX 15.0
39#define FILL_TOPY 15.0
40#define FILL_BOTTOMX 45.0
41#define FILL_BOTTOMY 45.0
42
43#define STROKE_TOPX 5.0
44#define STROKE_TOPY 5.0
45#define STROKE_BOTTOMX 35.0
46#define STROKE_BOTTOMY 35.0
47
48#define STROKE_TOPX_INNER STROKE_TOPX + 4
49#define STROKE_TOPY_INNER STROKE_TOPY + 4
50#define STROKE_BOTTOMX_INNER STROKE_BOTTOMX - 4
51#define STROKE_BOTTOMY_INNER STROKE_BOTTOMY - 4
52
53
54VStrokeFillPreview::VStrokeFillPreview(
55 KarbonPart *part, TQWidget* parent, const char* name )
56 : TQFrame( parent, name ), m_part( part )
57{
58 m_strokeWidget = false;
59 setFocusPolicy( TQWidget::NoFocus );
60
61 setFrameStyle( TQFrame::GroupBoxPanel | TQFrame::Sunken );
62
63 installEventFilter( this );
64 m_pixmap.resize( int( PANEL_SIZEX ), int( PANEL_SIZEY ) );
65 m_painter = new VKoPainter( &m_pixmap, uint( PANEL_SIZEX ), uint( PANEL_SIZEY ) );
66}
67
68VStrokeFillPreview::~VStrokeFillPreview()
69{
70 delete( m_painter );
71}
72
73void
74VStrokeFillPreview::paintEvent( TQPaintEvent* event )
75{
76 bitBlt( this,
77 (int)( width() - PANEL_SIZEX ) / 2, (int)( height() - PANEL_SIZEY ) / 2,
78 &m_pixmap,
79 0, 0, (int)PANEL_SIZEX, (int)PANEL_SIZEY );
80
81 TQFrame::paintEvent( event );
82}
83
84bool
85VStrokeFillPreview::eventFilter( TQObject *, TQEvent *event )
86{
87 TQMouseEvent* e = static_cast<TQMouseEvent*>( event );
88
89 int ex = e->x() - int( ( width() - PANEL_SIZEX ) / 2 );
90 int ey = e->y() - int( ( height() - PANEL_SIZEY ) / 2 );
91
92 if( event && event->type() == TQEvent::MouseButtonPress )
93 {
94 if ( m_strokeWidget )
95 {
96 if(
97 ex >= STROKE_TOPX && ex <= STROKE_BOTTOMX &&
98 ey >= STROKE_TOPY && ey <= STROKE_BOTTOMY )
99 {
100 m_strokeWidget = true;
101 emit strokeSelected();
102 }
103 else if(
104 ex >= FILL_TOPX && ex <= FILL_BOTTOMX &&
105 ey >= FILL_TOPY && ey <= FILL_BOTTOMY )
106 {
107 m_strokeWidget = false;
108 emit fillSelected();
109 }
110 }
111 else
112 {
113 if(
114 ex >= FILL_TOPX && ex <= FILL_BOTTOMX &&
115 ey >= FILL_TOPY && ey <= FILL_BOTTOMY )
116 {
117 m_strokeWidget = false;
118 emit fillSelected();
119 }
120 else if(
121 ex >= STROKE_TOPX && ex <= STROKE_BOTTOMX &&
122 ey >= STROKE_TOPY && ey <= STROKE_BOTTOMY )
123 {
124 m_strokeWidget = true;
125 emit strokeSelected();
126 }
127 }
128 update( m_stroke, m_fill );
129 }
130
131 if( event && event->type() == TQEvent::MouseButtonDblClick )
132 {
133 if(
134 ex >= FILL_TOPX && ex <= FILL_BOTTOMX &&
135 ey >= FILL_TOPY && ey <= FILL_BOTTOMY )
136 {
137 VColorDlg* dialog = new VColorDlg( m_fill.color(), this );
138 if( dialog->exec() == TQDialog::Accepted )
139 {
140 if( m_part && m_part->document().selection() ) m_part->addCommand( new VFillCmd( &m_part->document(), VFill( dialog->Color() ) ), true );
141 }
142 delete dialog;
143 }
144 else if(
145 ex >= STROKE_TOPX && ex <= STROKE_BOTTOMX
146 && ey >= STROKE_TOPY && ey <= STROKE_BOTTOMY )
147 {
148 VColorDlg* dialog = new VColorDlg( m_stroke.color(), this );
149 if( dialog->exec() == TQDialog::Accepted )
150 {
151 if( m_part && m_part->document().selection() ) m_part->addCommand( new VStrokeCmd( &m_part->document(), dialog->Color() ), true );
152 }
153 delete dialog;
154 }
155 }
156 return false;
157}
158
159void
160VStrokeFillPreview::update( const VStroke &s, const VFill &f )
161{
162 m_painter->begin();
163
164 if( &f )
165 m_fill = f;
166 else
167 m_fill = VFill();
168 if( &s )
169 m_stroke = s;
170 else
171 m_stroke = VStroke();
172
173 // draw checkerboard
174 VFill fill;
175 m_painter->setPen( TQt::NoPen );
176
177 for( unsigned char y = 0; y < PANEL_SIZEY; y += 10 )
178 for( unsigned char x = 0; x < PANEL_SIZEX; x += 10 )
179 {
180 fill.setColor( ( ( ( x + y ) % 20 ) == 0 ) ? TQColor( 180, 180, 180 ) : TQColor( 100, 100, 100 ) );
181 m_painter->setBrush( fill );
182 m_painter->drawRect( x, y, 10, 10 );
183 }
184
185 if ( m_strokeWidget )
186 {
187 drawFill( m_fill );
188 drawStroke( m_stroke );
189 }
190 else
191 {
192 drawStroke( m_stroke );
193 drawFill( m_fill );
194 }
195
196 m_painter->end();
197
198 repaint();
199}
200
201void
202VStrokeFillPreview::drawFill( const VFill &f )
203{
204 VStroke stroke;
205
206 if( f.type() != VFill::none )
207 {
208 if( f.type() != VFill::solid )
209 {
210 VFill fill;
211 fill = f;
212
213 if( f.type() == VFill::grad )
214 {
215 if( f.gradient().type() == VGradient::linear )
216 {
217 fill.gradient().setOrigin( KoPoint( 30, 20 ) );
218 fill.gradient().setVector( KoPoint( 30, 50 ) );
219 }
220 else if( f.gradient().type() == VGradient::radial ||
221 f.gradient().type() == VGradient::conic )
222 {
223 fill.gradient().setOrigin( KoPoint( 30, 35 ) );
224 fill.gradient().setFocalPoint( KoPoint( 30, 35 ) );
225 fill.gradient().setVector( KoPoint( 30, 50 ) );
226 }
227 }
228 if( f.type() == VFill::patt )
229 {
230 fill.pattern() = f.pattern();
231 fill.pattern().setOrigin( KoPoint( 20, 10 ) );
232 fill.pattern().setVector( KoPoint( 30, 10 ) );
233 fill.setType( VFill::patt );
234 }
235
236 m_painter->setBrush( fill );
237 }
238 else
239 m_painter->setBrush( f );
240 m_painter->setPen( TQt::NoPen );
241 m_painter->drawRect( KoRect( FILL_TOPX, FILL_TOPY, FILL_BOTTOMX - FILL_TOPX, FILL_BOTTOMY - FILL_TOPY ) );
242 }
243 else
244 {
245 VFill fill;
246 fill.setColor( TQt::white );
247 m_painter->setBrush( fill );
248 m_painter->setPen( TQt::NoPen );
249
250 m_painter->drawRect( KoRect( FILL_TOPX, FILL_TOPY,
251 FILL_BOTTOMX - FILL_TOPX,
252 FILL_BOTTOMY - FILL_TOPY ) );
253 }
254
255 // show 3D outline of fill part
256 VColor color;
257
258 m_painter->setBrush( TQt::NoBrush );
259 color.set( 1.0, 1.0, 1.0 );
260 stroke.setColor( color );
261 m_painter->setPen( stroke );
262
263 m_painter->newPath();
264 m_painter->moveTo( KoPoint( FILL_BOTTOMX, FILL_TOPY ) );
265 m_painter->lineTo( KoPoint( FILL_TOPX, FILL_TOPY ) );
266 m_painter->lineTo( KoPoint( FILL_TOPX, FILL_BOTTOMY ) );
267 m_painter->strokePath();
268
269 color.set( 0.5, 0.5, 0.5 );
270 stroke.setColor( color );
271 m_painter->setPen( stroke );
272
273 m_painter->newPath();
274 m_painter->moveTo( KoPoint( FILL_BOTTOMX, FILL_TOPY ) );
275 m_painter->lineTo( KoPoint( FILL_BOTTOMX, FILL_BOTTOMY ) );
276 m_painter->lineTo( KoPoint( FILL_TOPX, FILL_BOTTOMY ) );
277 m_painter->strokePath();
278
279 if( f.type() == VFill::none )
280 {
281 stroke.setColor( TQt::red );
282 m_painter->setPen( stroke );
283 m_painter->newPath();
284 m_painter->moveTo( KoPoint( FILL_BOTTOMX, FILL_TOPY ) );
285 m_painter->lineTo( KoPoint( FILL_TOPX, FILL_BOTTOMY ) );
286 m_painter->strokePath();
287 }
288}
289
290void
291VStrokeFillPreview::drawStroke( const VStroke &s )
292{
293 VStroke stroke;
294 stroke.setLineWidth( 2.0 );
295
296 m_painter->setPen( TQt::NoPen );
297
298 if( s.type() != VStroke::none )
299 {
300 VFill fill;
301
302 if( s.type() != VStroke::solid )
303 {
304 if( s.type() == VStroke::grad )
305 {
306 fill.gradient() = s.gradient();
307
308 if( s.gradient().type() == VGradient::linear )
309 {
310 fill.gradient().setOrigin( KoPoint( FILL_TOPX, 10 ) );
311 fill.gradient().setVector( KoPoint( FILL_TOPX, 40 ) );
312 }
313 else if( s.gradient().type() == VGradient::radial ||
314 s.gradient().type() == VGradient::conic )
315 {
316 fill.gradient().setOrigin( KoPoint( FILL_TOPX, 25 ) );
317 fill.gradient().setFocalPoint( KoPoint( FILL_TOPX, 25 ) );
318 fill.gradient().setVector( KoPoint( FILL_TOPX, 40 ) );
319 }
320
321 fill.setType( VFill::grad );
322 }
323 if( s.type() == VStroke::patt )
324 {
325 fill.pattern() = s.pattern();
326 fill.pattern().setOrigin( KoPoint( FILL_TOPX, 10 ) );
327 fill.pattern().setVector( KoPoint( FILL_TOPX, 40 ) );
328 fill.setType( VFill::patt );
329 }
330 }
331 else
332 fill.setColor( s.color() );
333
334 m_painter->setFillRule( evenOdd );
335
336 m_painter->setBrush( fill );
337
338 m_painter->newPath();
339 m_painter->moveTo( KoPoint( STROKE_TOPX, STROKE_TOPY ) );
340 m_painter->lineTo( KoPoint( STROKE_BOTTOMX, STROKE_TOPY ) );
341 m_painter->lineTo( KoPoint( STROKE_BOTTOMX, STROKE_BOTTOMY ) );
342 m_painter->lineTo( KoPoint( STROKE_TOPX, STROKE_BOTTOMY ) );
343 m_painter->lineTo( KoPoint( STROKE_TOPX, STROKE_TOPY ) );
344
345 m_painter->moveTo( KoPoint( STROKE_TOPX_INNER, STROKE_TOPY_INNER ) );
346 m_painter->lineTo( KoPoint( STROKE_BOTTOMX_INNER, STROKE_TOPY_INNER ) );
347 m_painter->lineTo( KoPoint( STROKE_BOTTOMX_INNER, STROKE_BOTTOMY_INNER ) );
348 m_painter->lineTo( KoPoint( STROKE_TOPX_INNER, STROKE_BOTTOMY_INNER ) );
349 m_painter->lineTo( KoPoint( STROKE_TOPX_INNER, STROKE_TOPY_INNER ) );
350 m_painter->fillPath();
351 }
352 else
353 {
354 VFill fill;
355 m_painter->setFillRule( evenOdd );
356 fill.setColor( TQt::white );
357
358 m_painter->setBrush( fill );
359 m_painter->setPen( TQt::NoPen );
360
361 m_painter->newPath();
362 m_painter->moveTo( KoPoint( STROKE_TOPX, STROKE_TOPY ) );
363 m_painter->lineTo( KoPoint( STROKE_BOTTOMX, STROKE_TOPY ) );
364 m_painter->lineTo( KoPoint( STROKE_BOTTOMX, STROKE_BOTTOMY ) );
365 m_painter->lineTo( KoPoint( STROKE_TOPX, STROKE_BOTTOMY ) );
366 m_painter->lineTo( KoPoint( STROKE_TOPX, STROKE_TOPY ) );
367
368 m_painter->moveTo( KoPoint( STROKE_TOPX_INNER, STROKE_TOPY_INNER ) );
369 m_painter->lineTo( KoPoint( STROKE_BOTTOMX_INNER, STROKE_TOPY_INNER ) );
370 m_painter->lineTo( KoPoint( STROKE_BOTTOMX_INNER, STROKE_BOTTOMY_INNER ) );
371 m_painter->lineTo( KoPoint( STROKE_TOPX_INNER, STROKE_BOTTOMY_INNER ) );
372 m_painter->lineTo( KoPoint( STROKE_TOPX_INNER, STROKE_TOPY_INNER ) );
373 m_painter->fillPath();
374 }
375
376 // show 3D outline of stroke part
377 VColor color;
378
379 color.set( 1.0, 1.0, 1.0 );
380 stroke.setColor( color );
381 m_painter->setBrush( TQt::NoBrush );
382 m_painter->setPen( stroke );
383
384 m_painter->newPath();
385 m_painter->moveTo( KoPoint( STROKE_BOTTOMX + 1, STROKE_TOPY - 1 ) );
386 m_painter->lineTo( KoPoint( STROKE_TOPX - 1, STROKE_TOPY - 1 ) );
387 m_painter->lineTo( KoPoint( STROKE_TOPX - 1, STROKE_BOTTOMY + 1 ) );
388 m_painter->strokePath();
389
390 color.set( 0.5, 0.5, 0.5 );
391 stroke.setColor( color );
392 m_painter->setPen( stroke );
393
394 m_painter->newPath();
395 m_painter->moveTo( KoPoint( STROKE_BOTTOMX + 1, STROKE_TOPY - 1 ) );
396 m_painter->lineTo( KoPoint( STROKE_BOTTOMX + 1, STROKE_BOTTOMY + 1 ) );
397 m_painter->lineTo( KoPoint( STROKE_TOPX - 1, STROKE_BOTTOMY + 1 ) );
398 m_painter->strokePath();
399
400 //stroke.setColor( TQt::black.rgb() );
401 //m_painter->setPen( stroke );
402 m_painter->newPath();
403 m_painter->moveTo( KoPoint( STROKE_BOTTOMX_INNER - 1, STROKE_TOPY_INNER + 1 ) );
404 m_painter->lineTo( KoPoint( STROKE_TOPX_INNER + 1, STROKE_TOPY_INNER + 1 ) );
405 m_painter->lineTo( KoPoint( STROKE_TOPX_INNER + 1, STROKE_BOTTOMY_INNER - 1 ) );
406 m_painter->strokePath();
407
408 color.set( 1.0, 1.0, 1.0 );
409 stroke.setColor( color );
410 m_painter->setPen( stroke );
411
412 m_painter->newPath();
413 m_painter->moveTo( KoPoint( STROKE_BOTTOMX_INNER - 1, STROKE_TOPY_INNER + 1 ) );
414 m_painter->lineTo( KoPoint( STROKE_BOTTOMX_INNER - 1, STROKE_BOTTOMY_INNER - 1 ) );
415 m_painter->lineTo( KoPoint( STROKE_TOPX_INNER + 1, STROKE_BOTTOMY_INNER - 1 ) );
416 m_painter->strokePath();
417
418 if( s.type() == VStroke::none )
419 {
420 stroke.setColor( TQt::red );
421 m_painter->setPen( stroke );
422
423 m_painter->newPath();
424 m_painter->moveTo( KoPoint( STROKE_BOTTOMX, STROKE_TOPY ) );
425 m_painter->lineTo( KoPoint( STROKE_TOPX, STROKE_BOTTOMY ) );
426 m_painter->strokePath();
427 }
428}
429
430void
431VStrokeFillPreview::setFillSelected()
432{
433 m_strokeWidget = false;
434 update( m_stroke, m_fill );
435 emit fillSelected();
436}
437
438void
439VStrokeFillPreview::setStrokeSelected()
440{
441 m_strokeWidget = true;
442 update( m_stroke, m_fill );
443 emit strokeSelected();
444}
445
446#include "vstrokefillpreview.moc"
447
Keeps track of visual per document properties.
Definition karbon_part.h:46
void set(float v1)
Sets the first color component.
Definition vcolor.h:91
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
@ patt
gradient as stroke
Definition vstroke.h:56
@ grad
solid stroke
Definition vstroke.h:55
@ solid
no stroke at all
Definition vstroke.h:54