kpresenter

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