kspread

kspread_brush.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2005-2006 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
20#include "kspread_brush.h"
21
22#include <KoGenStyles.h>
23#include <KoOasisStyles.h>
24#include <KoOasisContext.h>
25#include <KoXmlNS.h>
26
27KSpreadBrush::KSpreadBrush()
28: m_gColor1( TQt::red )
29, m_gColor2( TQt::green )
30, m_gType( BCT_GHORZ )
31, m_fillType( FT_BRUSH )
32, m_unbalanced( false )
33, m_xfactor( 100 )
34, m_yfactor( 100 )
35{
36}
37
38
39KSpreadBrush::KSpreadBrush( const TQBrush &brush, const TQColor &gColor1, const TQColor &gColor2,
40 BCType gType, FillType fillType, bool unbalanced,
41 int xfactor, int yfactor )
42: m_brush( brush )
43, m_gColor1( gColor1 )
44, m_gColor2( gColor2 )
45, m_gType( gType )
46, m_fillType( fillType )
47, m_unbalanced( unbalanced )
48, m_xfactor( xfactor )
49, m_yfactor( yfactor )
50{
51}
52
53
54KSpreadBrush & KSpreadBrush::operator=( const KSpreadBrush &brush )
55{
56 m_brush = brush.m_brush;
57 m_gColor1 = brush.m_gColor1;
58 m_gColor2 = brush.m_gColor2;
59 m_gType = brush.m_gType;
60 m_fillType = brush.m_fillType;
61 m_unbalanced = brush.m_unbalanced;
62 m_xfactor = brush.m_xfactor;
63 m_yfactor = brush.m_yfactor;
64 return *this;
65}
66
67
68void KSpreadBrush::saveOasisFillStyle( KoGenStyle &styleObjectAuto, KoGenStyles& mainStyles ) const
69{
70 switch ( m_fillType )
71 {
72 case FT_BRUSH:
73 {
74 if( m_brush.style() != TQt::NoBrush )
75 {
76 KoOasisStyles::saveOasisFillStyle( styleObjectAuto, mainStyles, m_brush );
77 }
78 else
79 {
80 styleObjectAuto.addProperty( "draw:fill","none" );
81 }
82 break;
83 }
84 case FT_GRADIENT:
85 styleObjectAuto.addProperty( "draw:fill","gradient" );
86#if 0
87 styleObjectAuto.addProperty( "draw:fill-gradient-name", saveOasisGradientStyle( mainStyles ) );
88#endif
89 break;
90 }
91}
92
93
94/*TQString KSpreadBrush::saveOasisGradientStyle( KoGenStyles& mainStyles ) const
95{
96 KoGenStyle gradientStyle( KPrDocument::STYLE_GRADIENT no family name);
97 gradientStyle.addAttribute( "draw:start-color", m_gColor1.name() );
98 gradientStyle.addAttribute( "draw:end-color", m_gColor2.name() );
99
100 TQString unbalancedx( "50%" );
101 TQString unbalancedy( "50%" );
102
103 if ( m_unbalanced )
104 {
105 unbalancedx = TQString( "%1%" ).arg( m_xfactor / 4 + 50 );
106 unbalancedy = TQString( "%1%" ).arg( m_yfactor / 4 + 50 );
107 }
108 gradientStyle.addAttribute( "draw:cx", unbalancedx );
109 gradientStyle.addAttribute( "draw:cy", unbalancedy );
110
111 switch( m_gType )
112 {
113 case BCT_PLAIN:
114 gradientStyle.addAttribute( "draw:angle", 0 );
115 gradientStyle.addAttribute( "draw:style", "linear" );
116 break;
117 case BCT_GHORZ:
118 gradientStyle.addAttribute( "draw:angle", 0 );
119 gradientStyle.addAttribute( "draw:style", "linear" );
120 break;
121 case BCT_GVERT:
122 gradientStyle.addAttribute( "draw:angle", 900 );
123 gradientStyle.addAttribute( "draw:style", "linear" );
124 break;
125 case BCT_GDIAGONAL1:
126 gradientStyle.addAttribute( "draw:angle", 450 );
127 gradientStyle.addAttribute( "draw:style", "linear" );
128 break;
129 case BCT_GDIAGONAL2:
130 gradientStyle.addAttribute( "draw:angle", 135 );
131 gradientStyle.addAttribute( "draw:style", "linear" );
132 break;
133 case BCT_GCIRCLE:
134 gradientStyle.addAttribute( "draw:angle", 0 );
135 gradientStyle.addAttribute( "draw:style", "radial" );
136 break;
137 case BCT_GRECT:
138 gradientStyle.addAttribute( "draw:angle", 0 );
139 gradientStyle.addAttribute( "draw:style", "square" );
140 break;
141 case BCT_GPIPECROSS:
142 gradientStyle.addAttribute( "draw:angle", 0 );
143 gradientStyle.addAttribute( "draw:style", "axial" );
144 break;
145 case BCT_GPYRAMID: //todo fixme ! it doesn't work !
146 gradientStyle.addAttribute( "draw:angle", 0 );
147 gradientStyle.addAttribute( "draw:style", 0 );
148 break;
149 }
150
151 return mainStyles.lookup( gradientStyle, "gradient" );
152}*/
153
154
155void KSpreadBrush::loadOasisFillStyle( KoOasisContext &context, const char * propertyType )
156{
157 KoStyleStack &styleStack = context.styleStack();
158 styleStack.setTypeProperties( propertyType );
159
160 if ( styleStack.hasAttributeNS( KoXmlNS::draw, "fill" ) )
161 {
162 const TQString fill = styleStack.attributeNS( KoXmlNS::draw, "fill" );
163 kdDebug(33001) << " load object gradient fill type :" << fill << endl;
164
165 if ( fill == "solid" || fill == "hatch" )
166 {
167 setBrush( KoOasisStyles::loadOasisFillStyle( styleStack, fill, context.oasisStyles() ) );
168 }
169 else if ( fill == "gradient" )
170 {
171 TQString style = styleStack.attributeNS( KoXmlNS::draw, "fill-gradient-name" );
172 TQDomElement* draw = context.oasisStyles().drawStyles()[style];
173
174 if ( draw )
175 {
176 setGColor1( draw->attributeNS( KoXmlNS::draw, "start-color", TQString() ) );
177 setGColor2( draw->attributeNS( KoXmlNS::draw, "end-color", TQString() ) );
178
179 TQString type = draw->attributeNS( KoXmlNS::draw, "style", TQString() );
180 kdDebug()<<" type :"<<type<<endl;
181 if ( type == "linear" )
182 {
183 int angle = draw->attributeNS( KoXmlNS::draw, "angle", TQString() ).toInt() / 10;
184
185 // make sure the angle is between 0 and 359
186 angle = abs( angle );
187 angle -= ( (int) ( angle / 360 ) ) * 360;
188
189 // What we are trying to do here is to find out if the given
190 // angle belongs to a horizontal, vertical or diagonal gradient.
191 int lower, upper, nearAngle = 0;
192 for ( lower = 0, upper = 45; upper < 360; lower += 45, upper += 45 )
193 {
194 if ( upper >= angle )
195 {
196 int distanceToUpper = abs( angle - upper );
197 int distanceToLower = abs( angle - lower );
198 nearAngle = distanceToUpper > distanceToLower ? lower : upper;
199 break;
200 }
201 }
202 // nearAngle should now be one of: 0, 45, 90, 135, 180...
203 if ( nearAngle == 0 || nearAngle == 180 )
204 setGType( BCT_GHORZ ); // horizontal
205 else if ( nearAngle == 90 || nearAngle == 270 )
206 setGType( BCT_GVERT ); // vertical
207 else if ( nearAngle == 45 || nearAngle == 225 )
208 setGType( BCT_GDIAGONAL1 ); // diagonal 1
209 else if ( nearAngle == 135 || nearAngle == 315 )
210 setGType( BCT_GDIAGONAL2 ); // diagonal 2
211 }
212 else if ( type == "radial" || type == "ellipsoid" )
213 setGType( BCT_GCIRCLE ); // circle
214 else if ( type == "square" || type == "rectangular" )
215 setGType( BCT_GRECT ); // rectangle
216 else if ( type == "axial" )
217 setGType( BCT_GPIPECROSS ); // pipecross
218 else //safe
219 setGType( BCT_PLAIN ); // plain
220
221 // Hard to map between x- and y-center settings of ooimpress
222 // and (un-)balanced settings of kpresenter. Let's try it.
223 int x, y;
224 if ( draw->hasAttributeNS( KoXmlNS::draw, "cx" ) )
225 x = draw->attributeNS( KoXmlNS::draw, "cx", TQString() ).remove( '%' ).toInt();
226 else
227 x = 50;
228
229 if ( draw->hasAttributeNS( KoXmlNS::draw, "cy" ) )
230 y = draw->attributeNS( KoXmlNS::draw, "cy", TQString() ).remove( '%' ).toInt();
231 else
232 y = 50;
233
234 if ( x == 50 && y == 50 )
235 {
236 setGUnbalanced( false );
237 setGXFactor( 100 );
238 setGYFactor( 100 );
239 }
240 else
241 {
242 setGUnbalanced( true );
243 // map 0 - 100% to -200 - 200
244 setGXFactor( 4 * x - 200 );
245 setGYFactor( 4 * y - 200 );
246 }
247 }
248
249 // We have to set a brush with brushstyle != no background fill
250 // otherwise the properties dialog for the object won't
251 // display the preview for the gradient.
252 TQBrush tmpBrush;
253 tmpBrush.setStyle( static_cast<TQt::BrushStyle>( 1 ) );
254 setBrush( tmpBrush );
255 setFillType( FT_GRADIENT );
256 }
257 else if ( fill == "none" )
258 {
259 //nothing
260 }
261 else if ( fill == "bitmap" )
262 {
263 //todo
264 //not implementer in kpresenter...
265 //the drawing object is filled with the bitmap specified by the draw:fill-image-name attribute.
266 //TQBrush implement setPixmap
267 //easy just add pixmap and store it.
268 }
269 }
270}