KOffice – TDE office suite
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

272 lines
10.0KB

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