kpresenter

KPrBrushProperty.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2005 Thorsten Zachmann <zachmann@kde.org>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018 */
00019 #include "KPrBrushProperty.h"
00020 
00021 #include "brushpropertyui.h"
00022 #include "gradientpropertyui.h"
00023 #include "KPrPBPreview.h"
00024 
00025 #include <qcheckbox.h>
00026 #include <qlabel.h>
00027 #include <qlayout.h>
00028 #include <qslider.h>
00029 #include <qwhatsthis.h>
00030 #include <qwidgetstack.h>
00031 
00032 #include <klocale.h>
00033 #include <kcolorbutton.h>
00034 #include <kcombobox.h>
00035 
00036 KPrBrushProperty::KPrBrushProperty( QWidget *parent, const char *name, const KPrBrushCmd::Brush &brush )
00037     : QWidget( parent, name )
00038     , m_brush( brush )
00039 {
00040     QGridLayout *layout = new QGridLayout( this, 1, 1, KDialog::marginHint(), KDialog::spacingHint() );
00041 
00042     m_typeCombo = new KComboBox( this );
00043     layout->addWidget( m_typeCombo, 0, 1 );
00044     m_typeCombo->insertItem( i18n( "Single Color" ) );
00045     m_typeCombo->insertItem( i18n( "Gradient" ) );
00046     m_typeCombo->insertItem( i18n( "Transparent" ) );
00047     QWhatsThis::add(m_typeCombo, i18n( "You can choose between Single Color, Gradient or Transparent as the type." ) );
00048 
00049     QLabel *typeLabel = new QLabel( i18n( "&Type:" ), this );
00050     layout->addWidget( typeLabel, 0, 0 );
00051     typeLabel->setBuddy( m_typeCombo );
00052 
00053     m_stack = new QWidgetStack( this );
00054     layout->addMultiCellWidget( m_stack, 1, 1, 0, 1 );
00055     connect( m_typeCombo, SIGNAL( activated( int ) ),
00056              this, SLOT( slotTypeChanged( int ) ) );
00057 
00058     m_brushUI = new BrushPropertyUI( m_stack );
00059 
00060     m_brushUI->styleCombo->insertItem( i18n( "%1% Fill Pattern" ).arg( 100 ) );
00061     m_brushUI->styleCombo->insertItem( i18n( "%1% Fill Pattern" ).arg( 94 ) );
00062     m_brushUI->styleCombo->insertItem( i18n( "%1% Fill Pattern" ).arg( 88 ) );
00063     m_brushUI->styleCombo->insertItem( i18n( "%1% Fill Pattern" ).arg( 63 ) );
00064     m_brushUI->styleCombo->insertItem( i18n( "%1% Fill Pattern" ).arg( 50 ) );
00065     m_brushUI->styleCombo->insertItem( i18n( "%1% Fill Pattern" ).arg( 37 ) );
00066     m_brushUI->styleCombo->insertItem( i18n( "%1% Fill Pattern" ).arg( 12 ) );
00067     m_brushUI->styleCombo->insertItem( i18n( "%1% Fill Pattern" ).arg( 6 ) );
00068     m_brushUI->styleCombo->insertItem( i18n( "Horizontal Lines" ) );
00069     m_brushUI->styleCombo->insertItem( i18n( "Vertical Lines" ) );
00070     m_brushUI->styleCombo->insertItem( i18n( "Crossing Lines" ) );
00071     m_brushUI->styleCombo->insertItem( i18n( "Diagonal Lines ( / )" ) );
00072     m_brushUI->styleCombo->insertItem( i18n( "Diagonal Lines ( \\ )" ) );
00073     m_brushUI->styleCombo->insertItem( i18n( "Diagonal Crossing Lines" ) );
00074 
00075     m_preview_color = new KPrPBPreview( m_brushUI->previewPanel, 0, KPrPBPreview::Brush );
00076     QHBoxLayout *hbox = new QHBoxLayout( m_brushUI->previewPanel );
00077     QWhatsThis::add(m_preview_color, i18n( "This displays a preview of your choices." ) );
00078     hbox->addWidget(m_preview_color);
00079 
00080     connect( m_brushUI->styleCombo, SIGNAL( activated( int ) ),
00081              this, SLOT( slotBrushChanged() ) );
00082     connect( m_brushUI->colorChooser, SIGNAL( changed( const QColor& ) ),
00083              this, SLOT( slotBrushChanged() ) );
00084 
00085     m_stack->addWidget( m_brushUI, 0 );
00086 
00087     m_gradientUI = new GradientPropertyUI( m_stack );
00088     m_gradientUI->styleCombo->insertItem( i18n( "Horizontal" ) );
00089     m_gradientUI->styleCombo->insertItem( i18n( "Vertical" ) );
00090     m_gradientUI->styleCombo->insertItem( i18n( "Diagonal 1" ) );
00091     m_gradientUI->styleCombo->insertItem( i18n( "Diagonal 2" ) );
00092     m_gradientUI->styleCombo->insertItem( i18n( "Circle" ) );
00093     m_gradientUI->styleCombo->insertItem( i18n( "Rectangle" ) );
00094     m_gradientUI->styleCombo->insertItem( i18n( "PipeCross" ) );
00095     m_gradientUI->styleCombo->insertItem( i18n( "Pyramid" ) );
00096 
00097     m_preview_gradient = new KPrPBPreview( m_gradientUI->previewPanel, 0, KPrPBPreview::Gradient );
00098     hbox = new QHBoxLayout( m_gradientUI->previewPanel );
00099     hbox->addWidget(m_preview_gradient);
00100 
00101     connect( m_gradientUI->styleCombo, SIGNAL( activated( int ) ),
00102              this, SLOT( slotBackColorTypeChanged() ) );
00103     connect( m_gradientUI->color1Chooser, SIGNAL( changed( const QColor& ) ),
00104              this, SLOT( slotColor1Changed() ) );
00105     connect( m_gradientUI->color2Chooser, SIGNAL( changed( const QColor& ) ),
00106              this, SLOT( slotColor2Changed() ) );
00107     connect( m_gradientUI->unbalancedCheckBox, SIGNAL( clicked() ),
00108              this, SLOT( slotUnbalancedChanged() ) );
00109     connect( m_gradientUI->xSlider, SIGNAL( valueChanged( int ) ),
00110              this, SLOT( slotXFactorChanged() ) );
00111     connect( m_gradientUI->ySlider, SIGNAL( valueChanged( int ) ),
00112              this, SLOT( slotYFactorChanged() ) );
00113 
00114     m_stack->addWidget( m_gradientUI, 1 );
00115     m_stack->addWidget( new QFrame(), 2 ); // the transparent case
00116 
00117     slotReset();
00118 }
00119 
00120 
00121 KPrBrushProperty::~KPrBrushProperty()
00122 {
00123 }
00124 
00125 
00126 FillType KPrBrushProperty::getFillType() const
00127 {
00128     int selected = m_typeCombo->currentItem();
00129     if(selected == 2)
00130         selected = 0;
00131     return (FillType) selected;
00132 }
00133 
00134 
00135 QBrush KPrBrushProperty::getQBrush() const
00136 {
00137     QBrush brush;
00138 
00139     switch ( m_brushUI->styleCombo->currentItem() )
00140     {
00141         case 0:
00142             brush.setStyle( SolidPattern );
00143             break;
00144         case 1:
00145             brush.setStyle( Dense1Pattern );
00146             break;
00147         case 2:
00148             brush.setStyle( Dense2Pattern );
00149             break;
00150         case 3:
00151             brush.setStyle( Dense3Pattern );
00152             break;
00153         case 4:
00154             brush.setStyle( Dense4Pattern );
00155             break;
00156         case 5:
00157             brush.setStyle( Dense5Pattern );
00158             break;
00159         case 6:
00160             brush.setStyle( Dense6Pattern );
00161             break;
00162         case 7:
00163             brush.setStyle( Dense7Pattern );
00164             break;
00165         case 8:
00166             brush.setStyle( HorPattern );
00167             break;
00168         case 9:
00169             brush.setStyle( VerPattern );
00170             break;
00171         case 10:
00172             brush.setStyle( CrossPattern );
00173             break;
00174         case 11:
00175             brush.setStyle( BDiagPattern );
00176             break;
00177         case 12:
00178             brush.setStyle( FDiagPattern );
00179             break;
00180         case 13:
00181             brush.setStyle( DiagCrossPattern );
00182             break;
00183     }
00184     if( m_typeCombo->currentItem() == 2)
00185         brush.setStyle( QBrush::NoBrush );
00186 
00187     brush.setColor( m_brushUI->colorChooser->color() );
00188 
00189     return brush;
00190 }
00191 
00192 
00193 QColor KPrBrushProperty::getGColor1()const
00194 {
00195     return m_gradientUI->color1Chooser->color();
00196 }
00197 
00198 
00199 QColor KPrBrushProperty::getGColor2()const
00200 {
00201     return m_gradientUI->color2Chooser->color();
00202 }
00203 
00204 
00205 BCType KPrBrushProperty::getGType()const
00206 {
00207     return (BCType)( m_gradientUI->styleCombo->currentItem() +1 );
00208 }
00209 
00210 
00211 bool KPrBrushProperty::getGUnbalanced()const
00212 {
00213     return m_gradientUI->unbalancedCheckBox->isChecked();
00214 }
00215 
00216 
00217 int KPrBrushProperty::getGXFactor() const
00218 {
00219     return m_gradientUI->xSlider->value();
00220 }
00221 
00222 
00223 int KPrBrushProperty::getGYFactor() const
00224 {
00225     return m_gradientUI->ySlider->value();
00226 }
00227 
00228 
00229 int KPrBrushProperty::getBrushPropertyChange() const
00230 {
00231     int flags = 0;
00232     bool fillTypeChanged = getFillType() != m_brush.fillType;
00233 
00234     if ( getFillType() == FT_BRUSH )
00235     {
00236         QBrush brush = getQBrush();
00237         if ( fillTypeChanged || brush.color() != m_brush.brush.color() )
00238         {
00239             flags |= KPrBrushCmd::BrushColor;
00240         }
00241         if ( fillTypeChanged || brush.style() != m_brush.brush.style() )
00242         {
00243             flags |= KPrBrushCmd::BrushStyle;
00244         }
00245         if ( fillTypeChanged )
00246         {
00247             flags |= KPrBrushCmd::BrushGradientSelect;
00248         }
00249     }
00250     else
00251     {
00252         if ( fillTypeChanged || getGColor1() != m_brush.gColor1 )
00253         {
00254             flags |= KPrBrushCmd::GradientColor1;
00255         }
00256         if ( fillTypeChanged || getGColor2() != m_brush.gColor2 )
00257         {
00258             flags |= KPrBrushCmd::GradientColor2;
00259         }
00260         if ( fillTypeChanged || getGType() != m_brush.gType )
00261         {
00262             flags |= KPrBrushCmd::GradientType;
00263         }
00264         if ( fillTypeChanged || getGUnbalanced() != m_brush.unbalanced )
00265         {
00266             flags |= KPrBrushCmd::GradientBalanced;
00267         }
00268         if ( fillTypeChanged || getGXFactor() != m_brush.xfactor )
00269         {
00270             flags |= KPrBrushCmd::GradientXFactor;
00271         }
00272         if ( fillTypeChanged || getGYFactor() != m_brush.yfactor )
00273         {
00274             flags |= KPrBrushCmd::GradientYFactor;
00275         }
00276         if ( fillTypeChanged )
00277         {
00278             flags |= KPrBrushCmd::BrushGradientSelect;
00279         }
00280     }
00281     return flags;
00282 }
00283 
00284 
00285 KPrBrushCmd::Brush KPrBrushProperty::getBrush() const
00286 {
00287     KPrBrushCmd::Brush brush( getQBrush(),
00288                            getGColor1(),
00289                            getGColor2(),
00290                            getGType(),
00291                            getFillType(),
00292                            getGUnbalanced(),
00293                            getGXFactor(),
00294                            getGYFactor() );
00295     return brush;
00296 }
00297 
00298 
00299 void KPrBrushProperty::setBrush( KPrBrushCmd::Brush &brush )
00300 {
00301     m_brush = brush;
00302     slotReset();
00303 }
00304 
00305 
00306 void KPrBrushProperty::apply()
00307 {
00308     int flags = getBrushPropertyChange();
00309 
00310     if ( flags & KPrBrushCmd::BrushGradientSelect )
00311         m_brush.fillType = getFillType();
00312 
00313     if ( flags & KPrBrushCmd::BrushColor )
00314         m_brush.brush.setColor( getQBrush().color() );
00315 
00316     if ( flags & KPrBrushCmd::BrushStyle )
00317         m_brush.brush.setStyle( getQBrush().style() );
00318 
00319     if ( flags & KPrBrushCmd::GradientColor1 )
00320         m_brush.gColor1 = getGColor1();
00321 
00322     if ( flags & KPrBrushCmd::GradientColor2 )
00323         m_brush.gColor2 = getGColor2();
00324 
00325     if ( flags & KPrBrushCmd::GradientType )
00326         m_brush.gType = getGType() ;
00327 
00328     if ( flags & KPrBrushCmd::GradientBalanced )
00329         m_brush.unbalanced = getGUnbalanced() ;
00330 
00331     if ( flags & KPrBrushCmd::GradientXFactor )
00332         m_brush.xfactor = getGXFactor() ;
00333 
00334     if ( flags & KPrBrushCmd::GradientYFactor )
00335         m_brush.yfactor = getGYFactor() ;
00336 }
00337 
00338 
00339 void KPrBrushProperty::setQBrush( const QBrush &brush )
00340 {
00341     switch ( brush.style() )
00342     {
00343         case NoBrush:
00344             // TODO
00345             break;
00346         case SolidPattern:
00347             m_brushUI->styleCombo->setCurrentItem( 0 );
00348             break;
00349         case Dense1Pattern:
00350             m_brushUI->styleCombo->setCurrentItem( 1 );
00351             break;
00352         case Dense2Pattern:
00353             m_brushUI->styleCombo->setCurrentItem( 2 );
00354             break;
00355         case Dense3Pattern:
00356             m_brushUI->styleCombo->setCurrentItem( 3 );
00357             break;
00358         case Dense4Pattern:
00359             m_brushUI->styleCombo->setCurrentItem( 4 );
00360             break;
00361         case Dense5Pattern:
00362             m_brushUI->styleCombo->setCurrentItem( 5 );
00363             break;
00364         case Dense6Pattern:
00365             m_brushUI->styleCombo->setCurrentItem( 6 );
00366             break;
00367         case Dense7Pattern:
00368             m_brushUI->styleCombo->setCurrentItem( 7 );
00369             break;
00370         case HorPattern:
00371             m_brushUI->styleCombo->setCurrentItem( 8 );
00372             break;
00373         case VerPattern:
00374             m_brushUI->styleCombo->setCurrentItem( 9 );
00375             break;
00376         case CrossPattern:
00377             m_brushUI->styleCombo->setCurrentItem( 10 );
00378             break;
00379         case BDiagPattern:
00380             m_brushUI->styleCombo->setCurrentItem( 11 );
00381             break;
00382         case FDiagPattern:
00383             m_brushUI->styleCombo->setCurrentItem( 12 );
00384             break;
00385         case DiagCrossPattern:
00386             m_brushUI->styleCombo->setCurrentItem( 13 );
00387             break;
00388         case CustomPattern:
00389             break;
00390     }
00391 
00392     m_brushUI->colorChooser->setColor( brush.color() );
00393     m_preview_color->setBrush( brush );
00394 }
00395 
00396 
00397 void KPrBrushProperty::setGradient( const QColor &_c1, const QColor &_c2, BCType _t,
00398                                     bool _unbalanced, int _xfactor, int _yfactor )
00399 {
00400     m_gradientUI->styleCombo->setCurrentItem( (int) _t - 1 );
00401     m_gradientUI->color1Chooser->setColor( _c1 );
00402     m_gradientUI->color2Chooser->setColor( _c2 );
00403     m_gradientUI->unbalancedCheckBox->setChecked( _unbalanced );
00404     setUnbalancedEnabled( _unbalanced );
00405     m_gradientUI->xSlider->setValue( _xfactor );
00406     m_gradientUI->ySlider->setValue( _yfactor );
00407     m_preview_gradient->setGradient( _c1, _c2, _t, _unbalanced, _xfactor, _yfactor );
00408 }
00409 
00410 
00411 void KPrBrushProperty::setUnbalancedEnabled( bool state )
00412 {
00413     m_gradientUI->xFactorLabel->setEnabled( state );
00414     m_gradientUI->xSlider->setEnabled( state );
00415     m_gradientUI->yFactorLabel->setEnabled( state );
00416     m_gradientUI->ySlider->setEnabled( state );
00417 }
00418 
00419 
00420 void KPrBrushProperty::slotReset()
00421 {
00422     if ( m_brush.gType == BCT_PLAIN )
00423         m_brush.gType = BCT_GHORZ;
00424     setQBrush( m_brush.brush );
00425     setGradient( m_brush.gColor1,
00426                  m_brush.gColor2,
00427                  m_brush.gType,
00428                  m_brush.unbalanced,
00429                  m_brush.xfactor,
00430                  m_brush.yfactor );
00431 
00432     int panelIndex;
00433     if( m_brush.fillType == FT_BRUSH && m_brush.brush.style() == QBrush::NoBrush )
00434         panelIndex = 2;
00435     else
00436         panelIndex = (int)m_brush.fillType;
00437 
00438     m_typeCombo->setCurrentItem( panelIndex );
00439     slotTypeChanged( panelIndex );
00440 }
00441 
00442 
00443 void KPrBrushProperty::slotTypeChanged( int pos )
00444 {
00445     m_stack->raiseWidget( pos );
00446     slotBrushChanged();
00447 }
00448 
00449 
00450 void KPrBrushProperty::slotBrushChanged()
00451 {
00452     m_preview_color->setBrush( getQBrush() );
00453 }
00454 
00455 
00456 void KPrBrushProperty::slotColor1Changed()
00457 {
00458     m_preview_gradient->setColor1( getGColor1() );
00459 }
00460 
00461 
00462 void KPrBrushProperty::slotColor2Changed()
00463 {
00464     m_preview_gradient->setColor2( getGColor2() );
00465 }
00466 
00467 
00468 void KPrBrushProperty::slotBackColorTypeChanged()
00469 {
00470     BCType type = getGType();
00471     m_preview_gradient->setBackColorType( type );
00472     m_gradientUI->xSlider->setEnabled( type != 1 );
00473     m_gradientUI->ySlider->setEnabled( type != 2 );
00474 }
00475 
00476 
00477 void KPrBrushProperty::slotUnbalancedChanged()
00478 {
00479     bool state = getGUnbalanced();
00480     setUnbalancedEnabled( state );
00481     m_preview_gradient->setUnbalanced( state );
00482 
00483     slotBackColorTypeChanged(); // make sure the sliders enabled-ness is up-to-date
00484 }
00485 
00486 
00487 void KPrBrushProperty::slotXFactorChanged()
00488 {
00489     m_preview_gradient->setXFactor( getGXFactor() );
00490 }
00491 
00492 
00493 void KPrBrushProperty::slotYFactorChanged()
00494 {
00495     m_preview_gradient->setYFactor( getGYFactor() );
00496 }
00497 
00498 #include "KPrBrushProperty.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys