kspread

kspread_brush.cc

00001 // -*- Mode: c++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4; -*-
00002 /* This file is part of the KDE project
00003    Copyright (C) 2005-2006 Thorsten Zachmann <zachmann@kde.org>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #include "kspread_brush.h"
00022 
00023 #include <KoGenStyles.h>
00024 #include <KoOasisStyles.h>
00025 #include <KoOasisContext.h>
00026 #include <KoXmlNS.h>
00027 
00028 KSpreadBrush::KSpreadBrush()
00029 : m_gColor1( Qt::red )
00030 , m_gColor2( Qt::green )
00031 , m_gType( BCT_GHORZ )
00032 , m_fillType( FT_BRUSH )
00033 , m_unbalanced( false )
00034 , m_xfactor( 100 )
00035 , m_yfactor( 100 )
00036 {
00037 }
00038 
00039 
00040 KSpreadBrush::KSpreadBrush( const QBrush &brush, const QColor &gColor1, const QColor &gColor2,
00041                     BCType gType, FillType fillType, bool unbalanced,
00042                     int xfactor, int yfactor )
00043 : m_brush( brush )
00044 , m_gColor1( gColor1 )
00045 , m_gColor2( gColor2 )
00046 , m_gType( gType )
00047 , m_fillType( fillType )
00048 , m_unbalanced( unbalanced )
00049 , m_xfactor( xfactor )
00050 , m_yfactor( yfactor )
00051 {
00052 }
00053 
00054 
00055 KSpreadBrush & KSpreadBrush::operator=( const KSpreadBrush &brush )
00056 {
00057     m_brush = brush.m_brush;
00058     m_gColor1 = brush.m_gColor1;
00059     m_gColor2 = brush.m_gColor2;
00060     m_gType = brush.m_gType;
00061     m_fillType = brush.m_fillType;
00062     m_unbalanced = brush.m_unbalanced;
00063     m_xfactor = brush.m_xfactor;
00064     m_yfactor = brush.m_yfactor;
00065     return *this;
00066 }
00067 
00068 
00069 void KSpreadBrush::saveOasisFillStyle( KoGenStyle &styleObjectAuto, KoGenStyles& mainStyles ) const
00070 {
00071     switch ( m_fillType )
00072     {
00073         case FT_BRUSH:
00074         {
00075             if( m_brush.style() != Qt::NoBrush )
00076             {
00077                 KoOasisStyles::saveOasisFillStyle( styleObjectAuto, mainStyles, m_brush );
00078             }
00079             else
00080             {
00081                 styleObjectAuto.addProperty( "draw:fill","none" );
00082             }
00083             break;
00084         }
00085         case FT_GRADIENT:
00086             styleObjectAuto.addProperty( "draw:fill","gradient" );
00087 #if 0
00088             styleObjectAuto.addProperty( "draw:fill-gradient-name", saveOasisGradientStyle( mainStyles ) );
00089 #endif
00090             break;
00091     }
00092 }
00093 
00094 
00095 /*QString KSpreadBrush::saveOasisGradientStyle( KoGenStyles& mainStyles ) const
00096 {
00097     KoGenStyle gradientStyle( KPrDocument::STYLE_GRADIENT no family name);
00098     gradientStyle.addAttribute( "draw:start-color", m_gColor1.name() );
00099     gradientStyle.addAttribute( "draw:end-color", m_gColor2.name() );
00100 
00101     QString unbalancedx( "50%" );
00102     QString unbalancedy( "50%" );
00103 
00104     if ( m_unbalanced )
00105     {
00106         unbalancedx = QString( "%1%" ).arg( m_xfactor / 4 + 50 );
00107         unbalancedy = QString( "%1%" ).arg( m_yfactor / 4 + 50 );
00108     }
00109     gradientStyle.addAttribute( "draw:cx", unbalancedx );
00110     gradientStyle.addAttribute( "draw:cy", unbalancedy );
00111 
00112     switch( m_gType )
00113     {
00114         case BCT_PLAIN:
00115             gradientStyle.addAttribute( "draw:angle", 0 );
00116             gradientStyle.addAttribute( "draw:style", "linear" );
00117             break;
00118         case BCT_GHORZ:
00119             gradientStyle.addAttribute( "draw:angle", 0 );
00120             gradientStyle.addAttribute( "draw:style", "linear" );
00121             break;
00122         case BCT_GVERT:
00123             gradientStyle.addAttribute( "draw:angle", 900 );
00124             gradientStyle.addAttribute( "draw:style", "linear" );
00125             break;
00126         case BCT_GDIAGONAL1:
00127             gradientStyle.addAttribute( "draw:angle", 450 );
00128             gradientStyle.addAttribute( "draw:style", "linear" );
00129             break;
00130         case BCT_GDIAGONAL2:
00131             gradientStyle.addAttribute( "draw:angle", 135 );
00132             gradientStyle.addAttribute( "draw:style", "linear" );
00133             break;
00134         case BCT_GCIRCLE:
00135             gradientStyle.addAttribute( "draw:angle", 0 );
00136             gradientStyle.addAttribute( "draw:style", "radial" );
00137             break;
00138         case BCT_GRECT:
00139             gradientStyle.addAttribute( "draw:angle", 0 );
00140             gradientStyle.addAttribute( "draw:style", "square" );
00141             break;
00142         case BCT_GPIPECROSS:
00143             gradientStyle.addAttribute( "draw:angle", 0 );
00144             gradientStyle.addAttribute( "draw:style", "axial" );
00145             break;
00146         case BCT_GPYRAMID: //todo fixme ! it doesn't work !
00147             gradientStyle.addAttribute( "draw:angle", 0 );
00148             gradientStyle.addAttribute( "draw:style", 0 );
00149             break;
00150     }
00151 
00152     return mainStyles.lookup( gradientStyle, "gradient" );
00153 }*/
00154 
00155 
00156 void KSpreadBrush::loadOasisFillStyle( KoOasisContext &context, const char * propertyType )
00157 {
00158     KoStyleStack &styleStack = context.styleStack();
00159     styleStack.setTypeProperties( propertyType );
00160 
00161     if ( styleStack.hasAttributeNS( KoXmlNS::draw, "fill" ) )
00162     {
00163         const QString fill = styleStack.attributeNS( KoXmlNS::draw, "fill" );
00164         kdDebug(33001) << " load object gradient fill type :" << fill << endl;
00165 
00166         if ( fill == "solid" || fill == "hatch" )
00167         {
00168             setBrush( KoOasisStyles::loadOasisFillStyle( styleStack, fill, context.oasisStyles() ) );
00169         }
00170         else if ( fill == "gradient" )
00171         {
00172             QString style = styleStack.attributeNS( KoXmlNS::draw, "fill-gradient-name" );
00173             QDomElement* draw = context.oasisStyles().drawStyles()[style];
00174 
00175             if ( draw )
00176             {
00177                 setGColor1( draw->attributeNS( KoXmlNS::draw, "start-color", QString::null ) );
00178                 setGColor2( draw->attributeNS( KoXmlNS::draw, "end-color", QString::null ) );
00179 
00180                 QString type = draw->attributeNS( KoXmlNS::draw, "style", QString::null );
00181                 kdDebug()<<" type :"<<type<<endl;
00182                 if ( type == "linear" )
00183                 {
00184                     int angle = draw->attributeNS( KoXmlNS::draw, "angle", QString::null ).toInt() / 10;
00185 
00186                     // make sure the angle is between 0 and 359
00187                     angle = abs( angle );
00188                     angle -= ( (int) ( angle / 360 ) ) * 360;
00189 
00190                     // What we are trying to do here is to find out if the given
00191                     // angle belongs to a horizontal, vertical or diagonal gradient.
00192                     int lower, upper, nearAngle = 0;
00193                     for ( lower = 0, upper = 45; upper < 360; lower += 45, upper += 45 )
00194                     {
00195                         if ( upper >= angle )
00196                         {
00197                             int distanceToUpper = abs( angle - upper );
00198                             int distanceToLower = abs( angle - lower );
00199                             nearAngle = distanceToUpper > distanceToLower ? lower : upper;
00200                             break;
00201                         }
00202                     }
00203                     // nearAngle should now be one of: 0, 45, 90, 135, 180...
00204                     if ( nearAngle == 0 || nearAngle == 180 )
00205                         setGType( BCT_GHORZ ); // horizontal
00206                     else if ( nearAngle == 90 || nearAngle == 270 )
00207                         setGType( BCT_GVERT ); // vertical
00208                     else if ( nearAngle == 45 || nearAngle == 225 )
00209                         setGType( BCT_GDIAGONAL1 ); // diagonal 1
00210                     else if ( nearAngle == 135 || nearAngle == 315 )
00211                         setGType( BCT_GDIAGONAL2 ); // diagonal 2
00212                 }
00213                 else if ( type == "radial" || type == "ellipsoid" )
00214                     setGType( BCT_GCIRCLE ); // circle
00215                 else if ( type == "square" || type == "rectangular" )
00216                     setGType( BCT_GRECT ); // rectangle
00217                 else if ( type == "axial" )
00218                     setGType( BCT_GPIPECROSS ); // pipecross
00219                 else //safe
00220                     setGType( BCT_PLAIN ); // plain
00221 
00222                 // Hard to map between x- and y-center settings of ooimpress
00223                 // and (un-)balanced settings of kpresenter. Let's try it.
00224                 int x, y;
00225                 if ( draw->hasAttributeNS( KoXmlNS::draw, "cx" ) )
00226                     x = draw->attributeNS( KoXmlNS::draw, "cx", QString::null ).remove( '%' ).toInt();
00227                 else
00228                     x = 50;
00229 
00230                 if ( draw->hasAttributeNS( KoXmlNS::draw, "cy" ) )
00231                     y = draw->attributeNS( KoXmlNS::draw, "cy", QString::null ).remove( '%' ).toInt();
00232                 else
00233                     y = 50;
00234 
00235                 if ( x == 50 && y == 50 )
00236                 {
00237                     setGUnbalanced( false );
00238                     setGXFactor( 100 );
00239                     setGYFactor( 100 );
00240                 }
00241                 else
00242                 {
00243                     setGUnbalanced( true );
00244                     // map 0 - 100% to -200 - 200
00245                     setGXFactor( 4 * x - 200 );
00246                     setGYFactor( 4 * y - 200 );
00247                 }
00248             }
00249 
00250             // We have to set a brush with brushstyle != no background fill
00251             // otherwise the properties dialog for the object won't
00252             // display the preview for the gradient.
00253             QBrush tmpBrush;
00254             tmpBrush.setStyle( static_cast<Qt::BrushStyle>( 1 ) );
00255             setBrush( tmpBrush );
00256             setFillType( FT_GRADIENT );
00257         }
00258         else if ( fill == "none" )
00259         {
00260             //nothing
00261         }
00262         else if ( fill == "bitmap" )
00263         {
00264             //todo
00265             //not implementer in kpresenter...
00266             //the drawing object is filled with the bitmap specified by the draw:fill-image-name attribute.
00267             //QBrush implement setPixmap
00268             //easy just add pixmap and store it.
00269         }
00270     }
00271 }
KDE Home | KDE Accessibility Home | Description of Access Keys