kpresenter

KPrPartObject.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 "KPrPartObject.h"
00023 #include "KPrDocument.h"
00024 #include "KPrView.h"
00025 #include "KPrGradient.h"
00026 #include <KoDom.h>
00027 #include <KoXmlNS.h>
00028 #include "KoOasisContext.h"
00029 #include <kparts/partmanager.h>
00030 
00031 #include <qpainter.h>
00032 #include <kdebug.h>
00033 using namespace std;
00034 
00035 KPrPartObject::KPrPartObject( KPrChild *_child )
00036     : KPr2DObject()
00037 {
00038     child = _child;
00039     pen = KoPen( Qt::black, 1.0, Qt::NoPen );
00040     _enableDrawing = true;
00041 }
00042 
00043 KPrPartObject &KPrPartObject::operator=( const KPrPartObject & )
00044 {
00045     return *this;
00046 }
00047 
00048 void KPrPartObject::updateChildGeometry()
00049 {
00050     KoTextZoomHandler* zh = child->parent()->zoomHandler();
00051     child->setGeometry( zh->zoomRect( getRect() ), true );
00052     child->setRotationPoint( QPoint( zh->zoomItX( getOrig().x() + getSize().width() / 2 ),
00053                                      zh->zoomItY( getOrig().y() + getSize().height() / 2 ) ) );
00054 }
00055 
00056 void KPrPartObject::rotate( float _angle )
00057 {
00058     KPrObject::rotate( _angle );
00059 
00060     child->setRotation( _angle );
00061     KoTextZoomHandler* zh = child->parent()->zoomHandler();
00062     child->setRotationPoint( QPoint( zh->zoomItX( getOrig().x() + getSize().width() / 2 ),
00063                                      zh->zoomItY( getOrig().y() + getSize().height() / 2 ) ) );
00064 }
00065 
00066 bool KPrPartObject::saveOasisObjectAttributes( KPOasisSaveContext &sc ) const
00067 {
00068     kdDebug() << "KPrPartObject::saveOasisPart " << sc.partIndexObj << endl;
00069 
00070     sc.xmlWriter.startElement( "draw:object" );
00071     const QString name = QString( "Object_%1" ).arg( sc.partIndexObj + 1 );
00072     ++sc.partIndexObj;
00073     child->saveOasisAttributes( sc.xmlWriter, name );
00074 
00075     sc.xmlWriter.endElement();
00076     return true;
00077 }
00078 
00079 const char * KPrPartObject::getOasisElementName() const
00080 {
00081     return "draw:frame";
00082 }
00083 
00084 
00085 void KPrPartObject::loadOasis(const QDomElement &element, KoOasisContext&context, KPrLoadingInfo */*info*/)
00086 {
00087     kdDebug()<<"void KPrPartObject::loadOasis(const QDomElement &element)******************\n";
00088 
00089     QDomElement objectElement = KoDom::namedItemNS( element, KoXmlNS::draw, "object" );
00090     child->loadOasis( element, objectElement );
00091     if(element.hasAttributeNS( KoXmlNS::draw, "name" ))
00092         objectName = element.attributeNS( KoXmlNS::draw, "name", QString::null);
00093     (void)child->loadOasisDocument( context.store(), context.manifestDocument() );
00094 }
00095 
00096 void KPrPartObject::draw( QPainter *_painter, KoTextZoomHandler *_zoomhandler,
00097                          int pageNum, SelectionMode selectionMode, bool drawContour )
00098 {
00099     updateChildGeometry();
00100     double ow = ext.width();
00101     double oh = ext.height();
00102 
00103     QSize size( _zoomhandler->zoomSize( ext ) );
00104     int penw = ( ( pen.style() == Qt::NoPen ) ? 1 : int( pen.pointWidth() ) ) / 2;
00105 
00106     QPen pen2;
00107     if ( drawContour )
00108         pen2 = QPen( Qt::black, 1, Qt::NoPen );
00109     else {
00110         pen2 = pen.zoomedPen( _zoomhandler );
00111     }
00112     _painter->save();
00113     child->transform( *_painter );
00114     _painter->setPen( Qt::NoPen );
00115     _painter->setBrush( getBrush() );
00116 
00117     if ( angle == 0 ) {
00118         if ( getFillType() == FT_BRUSH || !gradient )
00119             _painter->drawRect( penw, penw, _zoomhandler->zoomItX( ext.width() - 2 * penw ),
00120                                 _zoomhandler->zoomItY( ext.height() - 2 * penw ) );
00121         else {
00122             gradient->setSize( size );
00123             _painter->drawPixmap( penw, penw, gradient->pixmap(), 0, 0,
00124                                   _zoomhandler->zoomItX( ow - 2 * penw ),
00125                                   _zoomhandler->zoomItY( oh - 2 * penw ) );
00126         }
00127     }
00128     else
00129     {
00130         if ( getFillType() == FT_BRUSH || !gradient )
00131             _painter->drawRect( _zoomhandler->zoomItX( penw ), _zoomhandler->zoomItY( penw ),
00132                                 _zoomhandler->zoomItX( ext.width() - 2 * penw ),
00133                                 _zoomhandler->zoomItY( ext.height() - 2 * penw ) );
00134         else {
00135             gradient->setSize( size );
00136             _painter->drawPixmap( penw, penw, gradient->pixmap(), 0, 0,
00137                                   _zoomhandler->zoomItX( ow - 2 * penw ),
00138                                   _zoomhandler->zoomItY( oh - 2 * penw ) );
00139         }
00140     }
00141 
00142     _painter->setPen( pen2 );
00143     _painter->setBrush( Qt::NoBrush );
00144     _painter->drawRect( _zoomhandler->zoomItX( penw ), _zoomhandler->zoomItY( penw ),
00145                         _zoomhandler->zoomItX( ow - 2 * penw ), _zoomhandler->zoomItY( oh - 2 * penw ) );
00146     paint( _painter, _zoomhandler, pageNum, selectionMode, drawContour );
00147     _painter->restore();
00148 
00149     KPrObject::draw( _painter, _zoomhandler, pageNum, selectionMode, drawContour );
00150 }
00151 
00152 void KPrPartObject::slot_changed( KoChild *_koChild )
00153 {
00154     KoTextZoomHandler* zh = child->parent()->zoomHandler();
00155     KoRect g = zh->unzoomRect( _koChild->geometry() );
00156     KPrObject::setOrig( g.x(), g.y() );
00157     KPrObject::setSize( g.width(), g.height() );
00158 }
00159 
00160 void KPrPartObject::paint( QPainter *_painter, KoTextZoomHandler *_zoomHandler,
00161                           int /* pageNum */, bool /*drawingShadow*/, bool drawContour )
00162 {
00163     if ( !_enableDrawing ) return;
00164 
00165     if ( drawContour ) {
00166         QPen pen3( Qt::black, 1, Qt::DotLine );
00167         _painter->setPen( pen3 );
00168         _painter->setRasterOp( Qt::NotXorROP );
00169         _painter->drawRect( _zoomHandler->zoomRect( KoRect( KoPoint( 0.0, 0.0 ), getSize() ) ) );
00170         return;
00171     }
00172 
00173     if ( !child || !child->document() )
00174         return;
00175 
00176     int penw = ( pen.style() == Qt::NoPen ) ? 0 : int( pen.pointWidth() );
00177     KoRect r( KoPoint( penw, penw ), KoPoint( getSize().width() - ( penw * 2.0 ),
00178               getSize().height() - ( penw * 2.0 ) ) );
00179     double zoomX = static_cast<double>( _zoomHandler->zoom() ) / 100;
00180     double zoomY = static_cast<double>( _zoomHandler->zoom() ) / 100;
00181     child->document()->paintEverything( *_painter,
00182                                         _zoomHandler->zoomRect( r ),
00183                                         true, // flicker?
00184                                         0 /* View isn't known from here - is that a problem? */,
00185                                         zoomX,
00186                                         zoomY );
00187 }
00188 
00189 void KPrPartObject::activate( QWidget *_widget )
00190 {
00191     KPrView *view = dynamic_cast<KPrView*>( _widget );
00192     KoDocument* part = child->document();
00193     if ( !part )
00194         return;
00195     view->partManager()->addPart( part, false );
00196     view->partManager()->setActivePart( part, view );
00197 }
00198 
00199 void KPrPartObject::deactivate()
00200 {
00201 }
00202 
00203 #include "KPrPartObject.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys