kpresenter

KPrRectObject.cpp
1/* This file is part of the KDE project
2 Copyright (C) 1998, 1999 Reginald Stadlbauer <reggie@kde.org>
3 Copyright (C) 2005 Thorsten Zachmann <zachmann@kde.org>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19*/
20
21#include "KPrRectObject.h"
22#include "KPrGradient.h"
23#include "KPrRectObjectIface.h"
24
25#include <KoTextZoomHandler.h>
26#include <KoUnit.h>
27#include <KoOasisContext.h>
28#include <KoXmlNS.h>
29
30#include <kdebug.h>
31#include <tqregion.h>
32#include <tqbitmap.h>
33#include <tqdom.h>
34#include <tqpainter.h>
35
36KPrRectObject::KPrRectObject()
37 : KPr2DObject()
38{
39 xRnd = 0;
40 yRnd = 0;
41}
42
43DCOPObject* KPrRectObject::dcopObject()
44{
45 if ( !dcop )
46 dcop = new KPrRectObjectIface( this );
47 return dcop;
48}
49
50KPrRectObject::KPrRectObject( const KoPen &_pen, const TQBrush &_brush, FillType _fillType,
51 const TQColor &_gColor1, const TQColor &_gColor2,
52 BCType _gType, int _xRnd, int _yRnd,
53 bool _unbalanced, int _xfactor, int _yfactor)
54 : KPr2DObject( _pen, _brush, _fillType, _gColor1, _gColor2, _gType,
55 _unbalanced, _xfactor, _yfactor )
56{
57 xRnd = _xRnd;
58 yRnd = _yRnd;
59}
60
61KPrRectObject &KPrRectObject::operator=( const KPrRectObject & )
62{
63 return *this;
64}
65
66TQDomDocumentFragment KPrRectObject::save( TQDomDocument& doc, double offset )
67{
68 TQDomDocumentFragment fragment=KPr2DObject::save(doc, offset);
69 if (xRnd!=0 || yRnd!=0) {
70 TQDomElement elem=doc.createElement("RNDS");
71 elem.setAttribute("x", xRnd);
72 elem.setAttribute("y", yRnd);
73 fragment.appendChild(elem);
74 }
75 return fragment;
76}
77
78bool KPrRectObject::saveOasisObjectAttributes( KPOasisSaveContext &sc ) const
79{
80 if ( xRnd > 0 && yRnd > 0 )
81 {
82 double cornerRadiusX = ext.width() / 200.0 * xRnd;
83 double cornerRadiusY = ext.height() / 200.0 * yRnd;
84 double cornerRadius = TQMIN( cornerRadiusX, cornerRadiusY );
85 sc.xmlWriter.addAttributePt( "draw:corner-radius", cornerRadius );
86 if ( cornerRadiusX != cornerRadiusY )
87 {
88 sc.xmlWriter.addAttributePt( "koffice:corner-radius-x", cornerRadiusX );
89 sc.xmlWriter.addAttributePt( "koffice:corner-radius-y", cornerRadiusY );
90 }
91 }
92 return true;
93}
94
95
96const char * KPrRectObject::getOasisElementName() const
97{
98 return "draw:rect";
99}
100
101
102void KPrRectObject::loadOasis(const TQDomElement &element, KoOasisContext&context, KPrLoadingInfo *info)
103{
104 KPr2DObject::loadOasis(element, context, info);
105 if ( element.hasAttributeNS( KoXmlNS::koffice, "corner-radius-x" ) &&
106 element.hasAttributeNS( KoXmlNS::koffice, "corner-radius-y" ) )
107 {
108 xRnd = int( KoUnit::parseValue(
109 element.attributeNS( KoXmlNS::koffice, "corner-radius-x", TQString() )
110 ) * 200.0 / ext.width() );
111 yRnd = int( KoUnit::parseValue(
112 element.attributeNS( KoXmlNS::koffice, "corner-radius-y", TQString() )
113 ) * 200.0 / ext.height() );
114 }
115 else if ( element.hasAttributeNS( KoXmlNS::draw, "corner-radius" ) )
116 {
117 xRnd = int( KoUnit::parseValue(
118 element.attributeNS( KoXmlNS::draw, "corner-radius", TQString() )
119 ) * 200.0 / ext.width() );
120 yRnd = xRnd;
121 }
122 kdDebug(33001) << " KPrRectObject : radius xRnd :" << xRnd << " yRnd :" << yRnd <<endl;
123}
124
125double KPrRectObject::load(const TQDomElement &element)
126{
127 double offset=KPr2DObject::load(element);
128 TQDomElement e=element.namedItem("RNDS").toElement();
129 if(!e.isNull()) {
130 int tmp=0;
131 if(e.hasAttribute("x"))
132 tmp=e.attribute("x").toInt();
133 xRnd=tmp;
134 tmp=0;
135 if(e.hasAttribute("y"))
136 tmp=e.attribute("y").toInt();
137 yRnd=tmp;
138 }
139 return offset;
140}
141
142TQPointArray KPrRectObject::boundingRegion( int x, int y, int w, int h, int _xRnd, int _yRnd) const
143{
144 w--;
145 h--;
146 int rxx = (_xRnd==0)?1: (w*_xRnd/200);
147 int ryy = (_yRnd==0)?1: (h*_yRnd/200);
148 // were there overflows?
149 if ( rxx < 0 )
150 rxx = w/200*_xRnd;
151 if ( ryy < 0 )
152 ryy = h/200*_yRnd;
153 int rxx2 = 2*rxx;
154 int ryy2 = 2*ryy;
155 TQPointArray a[4];
156 a[0].makeArc( x, y, rxx2, ryy2, 1440, 1440); // ..., 1*16*90, 16*90);
157 a[1].makeArc( x, y+h-ryy2, rxx2, ryy2, 2880, 1440); // ..., 2*16*90, 16*90);
158 a[2].makeArc( x+w-rxx2, y+h-ryy2, rxx2, ryy2, 4320, 1440); // ..., 3*16*90, 16*90);
159 a[3].makeArc( x+w-rxx2, y, rxx2, ryy2, 0, 1440); // ..., 0*16*90, 16*90);
160 TQPointArray aa;
161 aa.resize( a[0].size() + a[1].size() + a[2].size() + a[3].size() );
162 uint j = 0;
163 for ( int k=0; k<4; k++ ) {
164 for ( uint i=0; i<a[k].size(); i++ ) {
165 aa.setPoint( j, a[k].point(i) );
166 j++;
167 }
168 }
169 return aa;
170}
171
172void KPrRectObject::paint( TQPainter* _painter, KoTextZoomHandler*_zoomHandler,
173 int /* pageNum */, bool drawingShadow, bool drawContour )
174{
175 int ow = _zoomHandler->zoomItX( ext.width() );
176 int oh = _zoomHandler->zoomItY( ext.height() );
177
178 if ( drawContour ) {
179 TQPen pen3( TQt::black, 1, TQt::DotLine );
180 _painter->setPen( pen3 );
181 _painter->setRasterOp( TQt::NotXorROP );
182
183 _painter->drawRoundRect( 0, 0, ow, oh, xRnd, yRnd );
184 return;
185 }
186
187 TQPen pen2 = pen.zoomedPen( _zoomHandler );
188 int pw = ( pen2.style() == TQt::NoPen ) ? 1 : pen2.width();
189 _painter->setPen( pen2 );
190
191 if ( drawingShadow || getFillType() == FT_BRUSH || !gradient ) { //plain fill
192 _painter->setPen( pen2 );
193 _painter->setBrush( getBrush() );
194 }
195 else { //gradient
196 TQSize size( _zoomHandler->zoomSize( ext ) );
197 if ( m_redrawGradientPix || gradient->size() != size ) {
198 m_redrawGradientPix = false;
199 gradient->setSize( size );
200 TQPointArray arr = boundingRegion( 0, 0, ow - pw + 1, oh - pw + 1, xRnd, yRnd );
201 TQRegion clipregion(arr);
202 m_gradientPix.resize ( ow, oh );
203 m_gradientPix.fill( TQt::white );
204 TQPainter p;
205 p.begin( &m_gradientPix );
206 p.setClipRegion( clipregion );
207 p.drawPixmap( 0, 0, gradient->pixmap() );
208 p.end();
209
210 m_gradientPix.setMask( m_gradientPix.createHeuristicMask() );
211 }
212 _painter->drawPixmap( pw / 2, pw / 2, m_gradientPix, 0, 0, ow - pw + 1, oh - pw + 1 );
213
214 _painter->setBrush( TQt::NoBrush );
215 }
216
217 _painter->drawRoundRect( pw / 2, pw / 2, ow - pw + 1, oh - pw + 1, xRnd, yRnd );
218}
Temporary information used only during loading.