kpresenter

KPrRectObject.cpp

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) 1998, 1999 Reginald Stadlbauer <reggie@kde.org>
00004    Copyright (C) 2005 Thorsten Zachmann <zachmann@kde.org>
00005 
00006    This library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Library General Public
00008    License as published by the Free Software Foundation; either
00009    version 2 of the License, or (at your option) any later version.
00010 
00011    This library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00014    Library General Public License for more details.
00015 
00016    You should have received a copy of the GNU Library General Public License
00017    along with this library; see the file COPYING.LIB.  If not, write to
00018    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019  * Boston, MA 02110-1301, USA.
00020 */
00021 
00022 #include "KPrRectObject.h"
00023 #include "KPrGradient.h"
00024 #include "KPrRectObjectIface.h"
00025 
00026 #include <KoTextZoomHandler.h>
00027 #include <KoUnit.h>
00028 #include <KoOasisContext.h>
00029 #include <KoXmlNS.h>
00030 
00031 #include <kdebug.h>
00032 #include <qregion.h>
00033 #include <qbitmap.h>
00034 #include <qdom.h>
00035 #include <qpainter.h>
00036 
00037 KPrRectObject::KPrRectObject()
00038     : KPr2DObject()
00039 {
00040     xRnd = 0;
00041     yRnd = 0;
00042 }
00043 
00044 DCOPObject* KPrRectObject::dcopObject()
00045 {
00046     if ( !dcop )
00047         dcop = new KPrRectObjectIface( this );
00048     return dcop;
00049 }
00050 
00051 KPrRectObject::KPrRectObject( const KoPen &_pen, const QBrush &_brush, FillType _fillType,
00052                             const QColor &_gColor1, const QColor &_gColor2,
00053                             BCType _gType, int _xRnd, int _yRnd,
00054                             bool _unbalanced, int _xfactor, int _yfactor)
00055     : KPr2DObject( _pen, _brush, _fillType, _gColor1, _gColor2, _gType,
00056                   _unbalanced, _xfactor, _yfactor )
00057 {
00058     xRnd = _xRnd;
00059     yRnd = _yRnd;
00060 }
00061 
00062 KPrRectObject &KPrRectObject::operator=( const KPrRectObject & )
00063 {
00064     return *this;
00065 }
00066 
00067 QDomDocumentFragment KPrRectObject::save( QDomDocument& doc, double offset )
00068 {
00069     QDomDocumentFragment fragment=KPr2DObject::save(doc, offset);
00070     if (xRnd!=0 || yRnd!=0) {
00071         QDomElement elem=doc.createElement("RNDS");
00072         elem.setAttribute("x", xRnd);
00073         elem.setAttribute("y", yRnd);
00074         fragment.appendChild(elem);
00075     }
00076     return fragment;
00077 }
00078 
00079 bool KPrRectObject::saveOasisObjectAttributes( KPOasisSaveContext &sc ) const
00080 {
00081     if ( xRnd > 0 && yRnd > 0 )
00082     {
00083         double cornerRadiusX = ext.width() / 200.0 * xRnd; 
00084         double cornerRadiusY = ext.height() / 200.0 * yRnd; 
00085         double cornerRadius = QMIN( cornerRadiusX, cornerRadiusY );
00086         sc.xmlWriter.addAttributePt( "draw:corner-radius", cornerRadius );
00087         if ( cornerRadiusX != cornerRadiusY )
00088         {
00089             sc.xmlWriter.addAttributePt( "koffice:corner-radius-x", cornerRadiusX );
00090             sc.xmlWriter.addAttributePt( "koffice:corner-radius-y", cornerRadiusY );
00091         }
00092     }
00093     return true;
00094 }
00095 
00096 
00097 const char * KPrRectObject::getOasisElementName() const
00098 {
00099     return "draw:rect";
00100 }
00101 
00102 
00103 void KPrRectObject::loadOasis(const QDomElement &element, KoOasisContext&context, KPrLoadingInfo *info)
00104 {
00105     KPr2DObject::loadOasis(element, context, info);
00106     if ( element.hasAttributeNS( KoXmlNS::koffice, "corner-radius-x" ) && 
00107          element.hasAttributeNS( KoXmlNS::koffice, "corner-radius-y" ) )
00108     {
00109         xRnd = int( KoUnit::parseValue( 
00110                       element.attributeNS( KoXmlNS::koffice, "corner-radius-x", QString::null )
00111                     ) * 200.0 / ext.width() );
00112         yRnd = int( KoUnit::parseValue( 
00113                       element.attributeNS( KoXmlNS::koffice, "corner-radius-y", QString::null )
00114                     ) * 200.0 / ext.height() );
00115     }
00116     else if ( element.hasAttributeNS( KoXmlNS::draw, "corner-radius" ) )
00117     {
00118         xRnd = int( KoUnit::parseValue( 
00119                       element.attributeNS( KoXmlNS::draw, "corner-radius", QString::null )
00120                     ) * 200.0 / ext.width() );
00121         yRnd = xRnd;
00122     }
00123     kdDebug(33001) << " KPrRectObject : radius xRnd :" << xRnd << " yRnd :" << yRnd <<endl;
00124 }
00125 
00126 double KPrRectObject::load(const QDomElement &element)
00127 {
00128     double offset=KPr2DObject::load(element);
00129     QDomElement e=element.namedItem("RNDS").toElement();
00130     if(!e.isNull()) {
00131         int tmp=0;
00132         if(e.hasAttribute("x"))
00133             tmp=e.attribute("x").toInt();
00134         xRnd=tmp;
00135         tmp=0;
00136         if(e.hasAttribute("y"))
00137             tmp=e.attribute("y").toInt();
00138         yRnd=tmp;
00139     }
00140     return offset;
00141 }
00142 
00143 QPointArray KPrRectObject::boundingRegion( int x, int y, int w, int h, int _xRnd, int _yRnd) const
00144 {
00145     w--;
00146     h--;
00147     int rxx = (_xRnd==0)?1: (w*_xRnd/200);
00148     int ryy = (_yRnd==0)?1: (h*_yRnd/200);
00149     // were there overflows?
00150     if ( rxx < 0 )
00151         rxx = w/200*_xRnd;
00152     if ( ryy < 0 )
00153         ryy = h/200*_yRnd;
00154     int rxx2 = 2*rxx;
00155     int ryy2 = 2*ryy;
00156     QPointArray a[4];
00157     a[0].makeArc( x, y, rxx2, ryy2, 1440, 1440); // ..., 1*16*90, 16*90);
00158     a[1].makeArc( x, y+h-ryy2, rxx2, ryy2, 2880, 1440); // ..., 2*16*90, 16*90);
00159     a[2].makeArc( x+w-rxx2, y+h-ryy2, rxx2, ryy2, 4320, 1440); // ..., 3*16*90, 16*90);
00160     a[3].makeArc( x+w-rxx2, y, rxx2, ryy2, 0, 1440); // ..., 0*16*90, 16*90);
00161     QPointArray aa;
00162     aa.resize( a[0].size() + a[1].size() + a[2].size() + a[3].size() );
00163     uint j = 0;
00164     for ( int k=0; k<4; k++ ) {
00165         for ( uint i=0; i<a[k].size(); i++ ) {
00166             aa.setPoint( j, a[k].point(i) );
00167             j++;
00168         }
00169     }
00170     return aa;
00171 }
00172 
00173 void KPrRectObject::paint( QPainter* _painter, KoTextZoomHandler*_zoomHandler,
00174                           int /* pageNum */, bool drawingShadow, bool drawContour )
00175 {
00176     int ow = _zoomHandler->zoomItX( ext.width() );
00177     int oh = _zoomHandler->zoomItY( ext.height() );
00178 
00179     if ( drawContour ) {
00180         QPen pen3( Qt::black, 1, Qt::DotLine );
00181         _painter->setPen( pen3 );
00182         _painter->setRasterOp( Qt::NotXorROP );
00183 
00184         _painter->drawRoundRect( 0, 0, ow, oh, xRnd, yRnd );
00185         return;
00186     }
00187 
00188     QPen pen2 = pen.zoomedPen( _zoomHandler );
00189     int pw = ( pen2.style() == Qt::NoPen ) ? 1 : pen2.width();
00190     _painter->setPen( pen2 );
00191 
00192     if ( drawingShadow || getFillType() == FT_BRUSH || !gradient ) { //plain fill
00193         _painter->setPen( pen2 );
00194         _painter->setBrush( getBrush() );
00195     }
00196     else { //gradient
00197         QSize size( _zoomHandler->zoomSize( ext ) );
00198         if ( m_redrawGradientPix || gradient->size() != size ) {
00199             m_redrawGradientPix = false;
00200             gradient->setSize( size );
00201             QPointArray arr = boundingRegion( 0, 0, ow - pw + 1, oh - pw + 1, xRnd, yRnd );
00202             QRegion clipregion(arr);
00203             m_gradientPix.resize ( ow, oh );
00204             m_gradientPix.fill( Qt::white );
00205             QPainter p;
00206             p.begin( &m_gradientPix );
00207             p.setClipRegion( clipregion );
00208             p.drawPixmap( 0, 0, gradient->pixmap() );
00209             p.end();
00210 
00211             m_gradientPix.setMask( m_gradientPix.createHeuristicMask() );
00212         }
00213         _painter->drawPixmap( pw / 2, pw / 2, m_gradientPix, 0, 0, ow - pw + 1, oh - pw + 1 );
00214 
00215         _painter->setBrush( Qt::NoBrush );
00216     }
00217 
00218     _painter->drawRoundRect( pw / 2, pw / 2, ow - pw + 1, oh - pw + 1, xRnd, yRnd );
00219 }
KDE Home | KDE Accessibility Home | Description of Access Keys