kpresenter

KPrBrushProperty.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2005 Thorsten Zachmann <zachmann@kde.org>
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#include "KPrBrushProperty.h"
20
21#include "brushpropertyui.h"
22#include "gradientpropertyui.h"
23#include "KPrPBPreview.h"
24
25#include <tqcheckbox.h>
26#include <tqlabel.h>
27#include <tqlayout.h>
28#include <tqslider.h>
29#include <tqwhatsthis.h>
30#include <tqwidgetstack.h>
31
32#include <tdelocale.h>
33#include <kcolorbutton.h>
34#include <kcombobox.h>
35
36KPrBrushProperty::KPrBrushProperty( TQWidget *parent, const char *name, const KPrBrushCmd::Brush &brush )
37 : TQWidget( parent, name )
38 , m_brush( brush )
39{
40 TQGridLayout *layout = new TQGridLayout( this, 1, 1, KDialog::marginHint(), KDialog::spacingHint() );
41
42 m_typeCombo = new KComboBox( this );
43 layout->addWidget( m_typeCombo, 0, 1 );
44 m_typeCombo->insertItem( i18n( "Single Color" ) );
45 m_typeCombo->insertItem( i18n( "Gradient" ) );
46 m_typeCombo->insertItem( i18n( "Transparent" ) );
47 TQWhatsThis::add(m_typeCombo, i18n( "You can choose between Single Color, Gradient or Transparent as the type." ) );
48
49 TQLabel *typeLabel = new TQLabel( i18n( "&Type:" ), this );
50 layout->addWidget( typeLabel, 0, 0 );
51 typeLabel->setBuddy( m_typeCombo );
52
53 m_stack = new TQWidgetStack( this );
54 layout->addMultiCellWidget( m_stack, 1, 1, 0, 1 );
55 connect( m_typeCombo, TQ_SIGNAL( activated( int ) ),
56 this, TQ_SLOT( slotTypeChanged( int ) ) );
57
58 m_brushUI = new BrushPropertyUI( m_stack );
59
60 m_brushUI->styleCombo->insertItem( i18n( "%1% Fill Pattern" ).arg( 100 ) );
61 m_brushUI->styleCombo->insertItem( i18n( "%1% Fill Pattern" ).arg( 94 ) );
62 m_brushUI->styleCombo->insertItem( i18n( "%1% Fill Pattern" ).arg( 88 ) );
63 m_brushUI->styleCombo->insertItem( i18n( "%1% Fill Pattern" ).arg( 63 ) );
64 m_brushUI->styleCombo->insertItem( i18n( "%1% Fill Pattern" ).arg( 50 ) );
65 m_brushUI->styleCombo->insertItem( i18n( "%1% Fill Pattern" ).arg( 37 ) );
66 m_brushUI->styleCombo->insertItem( i18n( "%1% Fill Pattern" ).arg( 12 ) );
67 m_brushUI->styleCombo->insertItem( i18n( "%1% Fill Pattern" ).arg( 6 ) );
68 m_brushUI->styleCombo->insertItem( i18n( "Horizontal Lines" ) );
69 m_brushUI->styleCombo->insertItem( i18n( "Vertical Lines" ) );
70 m_brushUI->styleCombo->insertItem( i18n( "Crossing Lines" ) );
71 m_brushUI->styleCombo->insertItem( i18n( "Diagonal Lines ( / )" ) );
72 m_brushUI->styleCombo->insertItem( i18n( "Diagonal Lines ( \\ )" ) );
73 m_brushUI->styleCombo->insertItem( i18n( "Diagonal Crossing Lines" ) );
74
75 m_preview_color = new KPrPBPreview( m_brushUI->previewPanel, 0, KPrPBPreview::Brush );
76 TQHBoxLayout *hbox = new TQHBoxLayout( m_brushUI->previewPanel );
77 TQWhatsThis::add(m_preview_color, i18n( "This displays a preview of your choices." ) );
78 hbox->addWidget(m_preview_color);
79
80 connect( m_brushUI->styleCombo, TQ_SIGNAL( activated( int ) ),
81 this, TQ_SLOT( slotBrushChanged() ) );
82 connect( m_brushUI->colorChooser, TQ_SIGNAL( changed( const TQColor& ) ),
83 this, TQ_SLOT( slotBrushChanged() ) );
84
85 m_stack->addWidget( m_brushUI, 0 );
86
87 m_gradientUI = new GradientPropertyUI( m_stack );
88 m_gradientUI->styleCombo->insertItem( i18n( "Horizontal" ) );
89 m_gradientUI->styleCombo->insertItem( i18n( "Vertical" ) );
90 m_gradientUI->styleCombo->insertItem( i18n( "Diagonal 1" ) );
91 m_gradientUI->styleCombo->insertItem( i18n( "Diagonal 2" ) );
92 m_gradientUI->styleCombo->insertItem( i18n( "Circle" ) );
93 m_gradientUI->styleCombo->insertItem( i18n( "Rectangle" ) );
94 m_gradientUI->styleCombo->insertItem( i18n( "PipeCross" ) );
95 m_gradientUI->styleCombo->insertItem( i18n( "Pyramid" ) );
96
97 m_preview_gradient = new KPrPBPreview( m_gradientUI->previewPanel, 0, KPrPBPreview::Gradient );
98 hbox = new TQHBoxLayout( m_gradientUI->previewPanel );
99 hbox->addWidget(m_preview_gradient);
100
101 connect( m_gradientUI->styleCombo, TQ_SIGNAL( activated( int ) ),
102 this, TQ_SLOT( slotBackColorTypeChanged() ) );
103 connect( m_gradientUI->color1Chooser, TQ_SIGNAL( changed( const TQColor& ) ),
104 this, TQ_SLOT( slotColor1Changed() ) );
105 connect( m_gradientUI->color2Chooser, TQ_SIGNAL( changed( const TQColor& ) ),
106 this, TQ_SLOT( slotColor2Changed() ) );
107 connect( m_gradientUI->unbalancedCheckBox, TQ_SIGNAL( clicked() ),
108 this, TQ_SLOT( slotUnbalancedChanged() ) );
109 connect( m_gradientUI->xSlider, TQ_SIGNAL( valueChanged( int ) ),
110 this, TQ_SLOT( slotXFactorChanged() ) );
111 connect( m_gradientUI->ySlider, TQ_SIGNAL( valueChanged( int ) ),
112 this, TQ_SLOT( slotYFactorChanged() ) );
113
114 m_stack->addWidget( m_gradientUI, 1 );
115 m_stack->addWidget( new TQFrame(), 2 ); // the transparent case
116
117 slotReset();
118}
119
120
121KPrBrushProperty::~KPrBrushProperty()
122{
123}
124
125
126FillType KPrBrushProperty::getFillType() const
127{
128 int selected = m_typeCombo->currentItem();
129 if(selected == 2)
130 selected = 0;
131 return (FillType) selected;
132}
133
134
135TQBrush KPrBrushProperty::getTQBrush() const
136{
137 TQBrush brush;
138
139 switch ( m_brushUI->styleCombo->currentItem() )
140 {
141 case 0:
142 brush.setStyle( TQt::SolidPattern );
143 break;
144 case 1:
145 brush.setStyle( TQt::Dense1Pattern );
146 break;
147 case 2:
148 brush.setStyle( TQt::Dense2Pattern );
149 break;
150 case 3:
151 brush.setStyle( TQt::Dense3Pattern );
152 break;
153 case 4:
154 brush.setStyle( TQt::Dense4Pattern );
155 break;
156 case 5:
157 brush.setStyle( TQt::Dense5Pattern );
158 break;
159 case 6:
160 brush.setStyle( TQt::Dense6Pattern );
161 break;
162 case 7:
163 brush.setStyle( TQt::Dense7Pattern );
164 break;
165 case 8:
166 brush.setStyle( TQt::HorPattern );
167 break;
168 case 9:
169 brush.setStyle( TQt::VerPattern );
170 break;
171 case 10:
172 brush.setStyle( TQt::CrossPattern );
173 break;
174 case 11:
175 brush.setStyle( TQt::BDiagPattern );
176 break;
177 case 12:
178 brush.setStyle( TQt::FDiagPattern );
179 break;
180 case 13:
181 brush.setStyle( TQt::DiagCrossPattern );
182 break;
183 }
184 if( m_typeCombo->currentItem() == 2)
185 brush.setStyle( TQt::NoBrush );
186
187 brush.setColor( m_brushUI->colorChooser->color() );
188
189 return brush;
190}
191
192
193TQColor KPrBrushProperty::getGColor1()const
194{
195 return m_gradientUI->color1Chooser->color();
196}
197
198
199TQColor KPrBrushProperty::getGColor2()const
200{
201 return m_gradientUI->color2Chooser->color();
202}
203
204
205BCType KPrBrushProperty::getGType()const
206{
207 return (BCType)( m_gradientUI->styleCombo->currentItem() +1 );
208}
209
210
211bool KPrBrushProperty::getGUnbalanced()const
212{
213 return m_gradientUI->unbalancedCheckBox->isChecked();
214}
215
216
217int KPrBrushProperty::getGXFactor() const
218{
219 return m_gradientUI->xSlider->value();
220}
221
222
223int KPrBrushProperty::getGYFactor() const
224{
225 return m_gradientUI->ySlider->value();
226}
227
228
229int KPrBrushProperty::getBrushPropertyChange() const
230{
231 int flags = 0;
232 bool fillTypeChanged = getFillType() != m_brush.fillType;
233
234 if ( getFillType() == FT_BRUSH )
235 {
236 TQBrush brush = getTQBrush();
237 if ( fillTypeChanged || brush.color() != m_brush.brush.color() )
238 {
239 flags |= KPrBrushCmd::BrushColor;
240 }
241 if ( fillTypeChanged || brush.style() != m_brush.brush.style() )
242 {
243 flags |= KPrBrushCmd::BrushStyle;
244 }
245 if ( fillTypeChanged )
246 {
247 flags |= KPrBrushCmd::BrushGradientSelect;
248 }
249 }
250 else
251 {
252 if ( fillTypeChanged || getGColor1() != m_brush.gColor1 )
253 {
254 flags |= KPrBrushCmd::GradientColor1;
255 }
256 if ( fillTypeChanged || getGColor2() != m_brush.gColor2 )
257 {
258 flags |= KPrBrushCmd::GradientColor2;
259 }
260 if ( fillTypeChanged || getGType() != m_brush.gType )
261 {
262 flags |= KPrBrushCmd::GradientType;
263 }
264 if ( fillTypeChanged || getGUnbalanced() != m_brush.unbalanced )
265 {
266 flags |= KPrBrushCmd::GradientBalanced;
267 }
268 if ( fillTypeChanged || getGXFactor() != m_brush.xfactor )
269 {
270 flags |= KPrBrushCmd::GradientXFactor;
271 }
272 if ( fillTypeChanged || getGYFactor() != m_brush.yfactor )
273 {
274 flags |= KPrBrushCmd::GradientYFactor;
275 }
276 if ( fillTypeChanged )
277 {
278 flags |= KPrBrushCmd::BrushGradientSelect;
279 }
280 }
281 return flags;
282}
283
284
285KPrBrushCmd::Brush KPrBrushProperty::getBrush() const
286{
287 KPrBrushCmd::Brush brush( getTQBrush(),
288 getGColor1(),
289 getGColor2(),
290 getGType(),
291 getFillType(),
292 getGUnbalanced(),
293 getGXFactor(),
294 getGYFactor() );
295 return brush;
296}
297
298
299void KPrBrushProperty::setBrush( KPrBrushCmd::Brush &brush )
300{
301 m_brush = brush;
302 slotReset();
303}
304
305
306void KPrBrushProperty::apply()
307{
308 int flags = getBrushPropertyChange();
309
310 if ( flags & KPrBrushCmd::BrushGradientSelect )
311 m_brush.fillType = getFillType();
312
313 if ( flags & KPrBrushCmd::BrushColor )
314 m_brush.brush.setColor( getTQBrush().color() );
315
316 if ( flags & KPrBrushCmd::BrushStyle )
317 m_brush.brush.setStyle( getTQBrush().style() );
318
319 if ( flags & KPrBrushCmd::GradientColor1 )
320 m_brush.gColor1 = getGColor1();
321
322 if ( flags & KPrBrushCmd::GradientColor2 )
323 m_brush.gColor2 = getGColor2();
324
325 if ( flags & KPrBrushCmd::GradientType )
326 m_brush.gType = getGType() ;
327
328 if ( flags & KPrBrushCmd::GradientBalanced )
329 m_brush.unbalanced = getGUnbalanced() ;
330
331 if ( flags & KPrBrushCmd::GradientXFactor )
332 m_brush.xfactor = getGXFactor() ;
333
334 if ( flags & KPrBrushCmd::GradientYFactor )
335 m_brush.yfactor = getGYFactor() ;
336}
337
338
339void KPrBrushProperty::setTQBrush( const TQBrush &brush )
340{
341 switch ( brush.style() )
342 {
343 case NoBrush:
344 // TODO
345 break;
346 case SolidPattern:
347 m_brushUI->styleCombo->setCurrentItem( 0 );
348 break;
349 case Dense1Pattern:
350 m_brushUI->styleCombo->setCurrentItem( 1 );
351 break;
352 case Dense2Pattern:
353 m_brushUI->styleCombo->setCurrentItem( 2 );
354 break;
355 case Dense3Pattern:
356 m_brushUI->styleCombo->setCurrentItem( 3 );
357 break;
358 case Dense4Pattern:
359 m_brushUI->styleCombo->setCurrentItem( 4 );
360 break;
361 case Dense5Pattern:
362 m_brushUI->styleCombo->setCurrentItem( 5 );
363 break;
364 case Dense6Pattern:
365 m_brushUI->styleCombo->setCurrentItem( 6 );
366 break;
367 case Dense7Pattern:
368 m_brushUI->styleCombo->setCurrentItem( 7 );
369 break;
370 case HorPattern:
371 m_brushUI->styleCombo->setCurrentItem( 8 );
372 break;
373 case VerPattern:
374 m_brushUI->styleCombo->setCurrentItem( 9 );
375 break;
376 case CrossPattern:
377 m_brushUI->styleCombo->setCurrentItem( 10 );
378 break;
379 case BDiagPattern:
380 m_brushUI->styleCombo->setCurrentItem( 11 );
381 break;
382 case FDiagPattern:
383 m_brushUI->styleCombo->setCurrentItem( 12 );
384 break;
385 case DiagCrossPattern:
386 m_brushUI->styleCombo->setCurrentItem( 13 );
387 break;
388 case CustomPattern:
389 break;
390 }
391
392 m_brushUI->colorChooser->setColor( brush.color() );
393 m_preview_color->setBrush( brush );
394}
395
396
397void KPrBrushProperty::setGradient( const TQColor &_c1, const TQColor &_c2, BCType _t,
398 bool _unbalanced, int _xfactor, int _yfactor )
399{
400 m_gradientUI->styleCombo->setCurrentItem( (int) _t - 1 );
401 m_gradientUI->color1Chooser->setColor( _c1 );
402 m_gradientUI->color2Chooser->setColor( _c2 );
403 m_gradientUI->unbalancedCheckBox->setChecked( _unbalanced );
404 setUnbalancedEnabled( _unbalanced );
405 m_gradientUI->xSlider->setValue( _xfactor );
406 m_gradientUI->ySlider->setValue( _yfactor );
407 m_preview_gradient->setGradient( _c1, _c2, _t, _unbalanced, _xfactor, _yfactor );
408}
409
410
411void KPrBrushProperty::setUnbalancedEnabled( bool state )
412{
413 m_gradientUI->xFactorLabel->setEnabled( state );
414 m_gradientUI->xSlider->setEnabled( state );
415 m_gradientUI->yFactorLabel->setEnabled( state );
416 m_gradientUI->ySlider->setEnabled( state );
417}
418
419
420void KPrBrushProperty::slotReset()
421{
422 if ( m_brush.gType == BCT_PLAIN )
423 m_brush.gType = BCT_GHORZ;
424 setTQBrush( m_brush.brush );
425 setGradient( m_brush.gColor1,
426 m_brush.gColor2,
427 m_brush.gType,
428 m_brush.unbalanced,
429 m_brush.xfactor,
430 m_brush.yfactor );
431
432 int panelIndex;
433 if( m_brush.fillType == FT_BRUSH && m_brush.brush.style() == TQBrush::NoBrush )
434 panelIndex = 2;
435 else
436 panelIndex = (int)m_brush.fillType;
437
438 m_typeCombo->setCurrentItem( panelIndex );
439 slotTypeChanged( panelIndex );
440}
441
442
443void KPrBrushProperty::slotTypeChanged( int pos )
444{
445 m_stack->raiseWidget( pos );
446 slotBrushChanged();
447}
448
449
450void KPrBrushProperty::slotBrushChanged()
451{
452 m_preview_color->setBrush( getTQBrush() );
453}
454
455
456void KPrBrushProperty::slotColor1Changed()
457{
458 m_preview_gradient->setColor1( getGColor1() );
459}
460
461
462void KPrBrushProperty::slotColor2Changed()
463{
464 m_preview_gradient->setColor2( getGColor2() );
465}
466
467
468void KPrBrushProperty::slotBackColorTypeChanged()
469{
470 BCType type = getGType();
471 m_preview_gradient->setBackColorType( type );
472 m_gradientUI->xSlider->setEnabled( type != 1 );
473 m_gradientUI->ySlider->setEnabled( type != 2 );
474}
475
476
477void KPrBrushProperty::slotUnbalancedChanged()
478{
479 bool state = getGUnbalanced();
480 setUnbalancedEnabled( state );
481 m_preview_gradient->setUnbalanced( state );
482
483 slotBackColorTypeChanged(); // make sure the sliders enabled-ness is up-to-date
484}
485
486
487void KPrBrushProperty::slotXFactorChanged()
488{
489 m_preview_gradient->setXFactor( getGXFactor() );
490}
491
492
493void KPrBrushProperty::slotYFactorChanged()
494{
495 m_preview_gradient->setYFactor( getGYFactor() );
496}
497
498#include "KPrBrushProperty.moc"