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