karbon

vsmallpreview.cpp
1/* This file is part of the KDE project
2 Made by Tomislav Lukman (tomislav.lukman@ck.t-com.hr)
3 Copyright (C) 2005, 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/* vsmallpreview.cpp */
22#include <tqcolor.h>
23#include <tqframe.h>
24#include <tqlabel.h>
25#include <tqlayout.h>
26#include <tqpixmap.h>
27
28#include <tdelocale.h>
29#include <KoPoint.h>
30
31#include "vcolor.h"
32#include "vfill.h"
33#include "vgradient.h"
34#include "vkopainter.h"
35#include "vpattern.h"
36#include "vstroke.h"
37
38#include "vsmallpreview.h"
39
40#define FRAMEWIDTH 40
41
42VSmallPreview::VSmallPreview( TQWidget* parent, const char* name )
43 : TQWidget( parent, name )
44{
45 /* Create widget layout */
46 TQHBoxLayout *layout = new TQHBoxLayout( this, 4 );
47 m_strokeLabel = new TQLabel( i18n( "Stroke: None" ), this );
48 layout->addWidget( m_strokeLabel );
49 m_strokeFrame = new TQFrame( this );
50 m_strokeFrame->setFixedWidth ( FRAMEWIDTH );
51 m_strokeFrame->setFrameStyle( TQFrame::GroupBoxPanel | TQFrame::Plain );
52 layout->addWidget( m_strokeFrame );
53 m_fillLabel = new TQLabel( i18n( "Fill: None" ), this );
54 layout->addWidget( m_fillLabel );
55 m_fillFrame = new TQFrame( this );
56 m_fillFrame->setFixedWidth ( FRAMEWIDTH );
57 m_fillFrame->setFrameStyle( TQFrame::GroupBoxPanel | TQFrame::Plain );
58 layout->addWidget( m_fillFrame );
59 layout->activate();
60
61 m_fill = VFill();
62 m_stroke = VStroke();
63}
64
65VSmallPreview::~VSmallPreview()
66{
67}
68
69void
70VSmallPreview::update( const VStroke &s, const VFill &f )
71{
72 if( &f )
73 m_fill = f;
74 else
75 m_fill = VFill();
76 if( &s )
77 m_stroke = s;
78 else
79 m_stroke = VStroke();
80
81 drawStroke( m_stroke );
82 drawFill( m_fill );
83}
84
85void
86VSmallPreview::paintEvent( TQPaintEvent* /*event*/ )
87{
88 drawStroke( m_stroke );
89 drawFill( m_fill );
90}
91
92void
93VSmallPreview::drawFill( const VFill &f )
94{
95 VFill fill;
96 VStroke stroke;
97
98 TQPixmap m_pixmap;
99 m_pixmap.resize( m_fillFrame->width(), m_fillFrame->height() );
100 VKoPainter* m_painter = new VKoPainter( &m_pixmap, m_fillFrame->width(), m_fillFrame->height() );
101
102 m_painter->begin();
103 m_painter->setPen( TQt::NoPen );
104 fill.setColor( TQt::white );
105 m_painter->setBrush( fill );
106 m_painter->drawRect( KoRect( 0, 0, m_fillFrame->width(), m_fillFrame->height() ) );
107
108 switch ( f.type() )
109 {
110 case VFill::solid:
111 {
112 switch ( f.color().colorSpace() )
113 {
114 case VColor::rgb:
115 m_fillLabel->setText( i18n( "Fill: RGB") ); break;
116 case VColor::cmyk:
117 m_fillLabel->setText( i18n( "Fill: CMYK") ); break;
118 case VColor::hsb:
119 m_fillLabel->setText( i18n( "Fill: HSB") ); break;
120 case VColor::gray:
121 m_fillLabel->setText( i18n( "Fill: Grayscale") ); break;
122 default:
123 m_fillLabel->setText( i18n( "Fill: Color") );
124 }
125 fill.setColor( f.color() );
126 break;
127 }
128 case VFill::grad:
129 {
130 fill.gradient() = f.gradient();
131 fill.setType( VFill::grad );
132 m_fillLabel->setText( i18n( "Fill: Gradient") );
133 if( f.gradient().type() == VGradient::linear )
134 {
135 fill.gradient().setOrigin( KoPoint( m_fillFrame->width() * 0.5, 0 ) );
136 fill.gradient().setVector( KoPoint( m_fillFrame->width() * 0.5, m_fillFrame->height() ) );
137 }
138 else if( f.gradient().type() == VGradient::radial ||
139 f.gradient().type() == VGradient::conic )
140 {
141 fill.gradient().setOrigin( KoPoint( m_fillFrame->width() * 0.5, m_fillFrame->height() * 0.5 ) );
142 fill.gradient().setFocalPoint( KoPoint( m_fillFrame->width() * 0.5, m_fillFrame->height() * 0.5 ) );
143 fill.gradient().setVector( KoPoint( m_fillFrame->width() * 0.5, m_fillFrame->height() ) );
144 }
145 break;
146
147 }
148 case VFill::patt:
149 {
150 fill.pattern() = f.pattern();
151 fill.pattern().setOrigin( KoPoint( 0, 0 ) );
152 fill.pattern().setVector( KoPoint( m_fillFrame->width() * 0.5, 0 ) );
153 fill.setType( VFill::patt );
154 m_fillLabel->setText( i18n( "Fill: Pattern") );
155 break;
156 }
157 default: //None or unknown
158 {
159 m_fillLabel->setText( i18n( "Fill: None") );
160 fill.setColor( TQt::white );
161 m_painter->setBrush( fill );
162 m_painter->drawRect( KoRect( 0, 0, m_fillFrame->width(), m_fillFrame->height() ) );
163 stroke.setColor( TQt::red );
164 stroke.setLineWidth( 2.0 );
165 m_painter->setPen( stroke );
166 m_painter->newPath();
167 m_painter->moveTo( KoPoint( 4, m_fillFrame->height() - 4 ) );
168 m_painter->lineTo( KoPoint( m_fillFrame->width() - 4, 4 ) );
169 m_painter->strokePath();
170 }
171 }
172
173 if( f.type() != VFill::none )
174 {
175 m_painter->setPen( stroke );
176 m_painter->setBrush( fill );
177 m_painter->drawRect( KoRect( 0, 0, m_fillFrame->width(), m_fillFrame->height() ) );
178 }
179
180 m_painter->end();
181
182 bitBlt( m_fillFrame, m_fillFrame->frameWidth(), m_fillFrame->frameWidth(), &m_pixmap, m_fillFrame->frameWidth(), m_fillFrame->frameWidth(), m_fillFrame->width() - m_fillFrame->frameWidth(), m_fillFrame->height() - m_fillFrame->frameWidth(), CopyROP );
183
184 delete ( m_painter );
185}
186
187void
188VSmallPreview::drawStroke( const VStroke &s )
189{
190 VFill fill;
191 VStroke stroke;
192
193 TQPixmap m_pixmap;
194 m_pixmap.resize( m_fillFrame->width(), m_fillFrame->height() );
195 VKoPainter* m_painter = new VKoPainter( &m_pixmap, m_fillFrame->width(), m_fillFrame->height() );
196
197 m_painter->begin();
198 m_painter->setPen( TQt::NoPen );
199 fill.setColor( TQt::white );
200 m_painter->setBrush( fill );
201 m_painter->drawRect( KoRect( 0, 0, m_strokeFrame->width(), m_strokeFrame->height() ) );
202
203 switch ( s.type() )
204 {
205 case VStroke::solid:
206 {
207 switch ( s.color().colorSpace() )
208 {
209 case VColor::rgb:
210 m_strokeLabel->setText( i18n( "Stroke: RGB") ); break;
211 case VColor::cmyk:
212 m_strokeLabel->setText( i18n( "Stroke: CMYK") ); break;
213 case VColor::hsb:
214 m_strokeLabel->setText( i18n( "Stroke: HSB") ); break;
215 case VColor::gray:
216 m_strokeLabel->setText( i18n( "Stroke: Grayscale") ); break;
217 default:
218 m_strokeLabel->setText( i18n( "Stroke: Color") );
219 }
220 fill.setColor( s.color() );
221 break;
222 }
223 case VStroke::grad:
224 {
225 fill.gradient() = s.gradient();
226 fill.setType( VFill::grad );
227 m_strokeLabel->setText( i18n( "Stroke: Gradient") );
228 if( s.gradient().type() == VGradient::linear )
229 {
230 fill.gradient().setOrigin( KoPoint( m_strokeFrame->width() * 0.5, 0 ) );
231 fill.gradient().setVector( KoPoint( m_strokeFrame->width() * 0.5, m_strokeFrame->height() ) );
232 }
233 else if( s.gradient().type() == VGradient::radial ||
234 s.gradient().type() == VGradient::conic )
235 {
236 fill.gradient().setOrigin( KoPoint( m_strokeFrame->width() * 0.5, m_strokeFrame->height() * 0.5 ) );
237 fill.gradient().setFocalPoint( KoPoint( m_strokeFrame->width() * 0.5, m_strokeFrame->height() * 0.5 ) );
238 fill.gradient().setVector( KoPoint( m_strokeFrame->width() * 0.5, m_strokeFrame->height() ) );
239 }
240 break;
241 }
242 case VStroke::patt:
243 {
244 fill.pattern() = s.pattern();
245 fill.pattern().setOrigin( KoPoint( 0, 0 ) );
246 fill.pattern().setVector( KoPoint( m_strokeFrame->width() * 0.5, 0 ) );
247 fill.setType( VFill::patt );
248 m_strokeLabel->setText( i18n( "Stroke: Pattern") );
249 break;
250 }
251 default: //None or unknown
252 {
253 m_strokeLabel->setText( i18n( "Stroke: None") );
254 fill.setColor( TQt::white );
255 m_painter->setBrush( fill );
256 m_painter->drawRect( KoRect( 0, 0, m_strokeFrame->width(), m_strokeFrame->height() ) );
257 stroke.setColor( TQt::red );
258 stroke.setLineWidth( 2.0 );
259 m_painter->setPen( stroke );
260 m_painter->newPath();
261 m_painter->moveTo( KoPoint( 4, m_strokeFrame->height() - 4 ) );
262 m_painter->lineTo( KoPoint( m_strokeFrame->width() - 4, 4 ) );
263 m_painter->strokePath();
264 }
265 }
266
267 if( s.type() != VStroke::none )
268 {
269 m_painter->setPen( stroke );
270 m_painter->setBrush( fill );
271 m_painter->drawRect( KoRect( 0, 0, m_strokeFrame->width(), m_strokeFrame->height() ) );
272 }
273
274 m_painter->end();
275
276 bitBlt( m_strokeFrame, m_strokeFrame->frameWidth(), m_strokeFrame->frameWidth(), &m_pixmap, m_strokeFrame->frameWidth(), m_strokeFrame->frameWidth(), m_strokeFrame->width() - m_strokeFrame->frameWidth(), m_strokeFrame->height() - m_strokeFrame->frameWidth(), CopyROP );
277
278 delete ( m_painter );
279}
280
281#include "vsmallpreview.moc"
282
VColorSpace colorSpace() const
Returns the color's colorspace.
Definition vcolor.h:145
@ gray
the Gray colorspace (gray from black to white)
Definition vcolor.h:48
@ cmyk
the CMYK colorspace (cyan, magenta, yellow and black components)
Definition vcolor.h:46
@ rgb
the RGB colorspace (red, green and blue components)
Definition vcolor.h:45
@ hsb
the HSB colorspace (hue, saturation and brightnes components)
Definition vcolor.h:47
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