kword

KWTextImage.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2001 David Faure <faure@kde.org>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include <kdebug.h>
00021 
00022 #include <KoPictureCollection.h>
00023 
00024 #include "KWDocument.h"
00025 #include "defs.h"
00026 #include "KWTextFrameSet.h"
00027 
00028 #include "KWTextImage.h"
00029 
00030 KWTextImage::KWTextImage( KWTextDocument *textdoc, const QString & filename )
00031     : KoTextCustomItem( textdoc ), place( PlaceInline )
00032 {
00033     KWDocument * doc = textdoc->textFrameSet()->kWordDocument();
00034     if ( !filename.isEmpty() )
00035     {
00036         m_image = doc->pictureCollection()->loadPicture( filename );
00037         Q_ASSERT( !m_image.isNull() );
00038         resize(); // Zoom if necessary
00039     }
00040 }
00041 
00042 void KWTextImage::setImage( const KoPictureCollection & collection )
00043 {
00044     kdDebug(32001) << "Loading text image " << m_image.getKey().toString() << " (in KWTextImage::setImage)" << endl;
00045     m_image=collection.findPicture( m_image.getKey() );
00046     Q_ASSERT( !m_image.isNull() );
00047     kdDebug(32001) << "size: " << m_image.getOriginalSize().width() << "x" << m_image.getOriginalSize().height() << endl;
00048     resize();
00049 }
00050 
00051 void KWTextImage::resize()
00052 {
00053     if ( m_deleted )
00054         return;
00055     if ( !m_image.isNull() ) {
00056         //KWDocument * doc = static_cast<KWTextDocument *>(parent)->textFrameSet()->kWordDocument();
00057         width = m_image.getOriginalSize().width();
00058         // width is a 100%-zoom pixel size. We want a LU pixel size -> we simply need 'to LU', i.e. ptToLayoutPt
00059         width = KoTextZoomHandler::ptToLayoutUnitPt( width );
00060         height = m_image.getOriginalSize().height();
00061         height = KoTextZoomHandler::ptToLayoutUnitPt( height );
00062         kdDebug() << "KWTextImage::resize: " << width << ", " << height << endl;
00063         // no! m_image.setSize( QSize( width, height ) );
00064     }
00065 }
00066 
00067 void KWTextImage::drawCustomItem( QPainter* p, int x, int y, int wpix, int hpix, int /*ascentpix*/, int cx, int cy, int cw, int ch, const QColorGroup& cg, bool selected, int /*offset*/,  bool drawingShadow)
00068 {
00069     if ( drawingShadow )
00070         return;
00071 
00072     // (x,y) is the position of the inline item (in pixels)
00073     // (wpix,hpix) is the size of the inline item (in pixels)
00074     // (cx,cy,cw,ch) is the rectangle to be painted, in pixels too
00075     if ( m_image.isNull() ) {
00076         kdDebug() << "KWTextImage::draw null image!" << endl;
00077         p->fillRect( x, y, 50, 50, cg.dark() );
00078         return;
00079     }
00080 
00081     QSize imgSize( wpix, hpix );
00082 
00083     QRect rect( QPoint(x, y), imgSize );
00084     if ( !rect.intersects( QRect( cx, cy, cw, ch ) ) )
00085         return;
00086 
00087     QPixmap pixmap=m_image.generatePixmap( imgSize, true );
00088     //if ( placement() == PlaceInline )
00089         p->drawPixmap( x, y, pixmap );
00090     //else
00091     //    p->drawPixmap( cx, cy, pixmap, cx - x, cy - y, cw, ch );
00092 
00093     if ( selected && placement() == PlaceInline && p->device()->devType() != QInternal::Printer ) {
00094         p->fillRect( rect , QBrush( cg.highlight(), QBrush::Dense4Pattern) );
00095     }
00096 }
00097 
00098 void KWTextImage::save( QDomElement & parentElem )
00099 {
00100     // This code is similar to KWPictureFrameSet::save
00101     KWDocument * doc = static_cast<KWTextDocument *>(parent)->textFrameSet()->kWordDocument();
00102 
00103     QDomElement imageElem = parentElem.ownerDocument().createElement( "PICTURE" );
00104     parentElem.appendChild( imageElem );
00105     //imageElem.setAttribute( "keepAspectRatio", "true" );
00106     QDomElement elem = parentElem.ownerDocument().createElement( "KEY" );
00107     imageElem.appendChild( elem );
00108     image().getKey().saveAttributes( elem );
00109     // Now we must take care that a <KEY> element will be written as a child of <PICTURES>
00110     doc->addTextImageRequest( this );
00111 }
00112 
00113 void KWTextImage::load( QDomElement & parentElem )
00114 {
00115     // This code is similar to KWPictureFrameSet::load
00116     KWDocument * doc = static_cast<KWTextDocument *>(parent)->textFrameSet()->kWordDocument();
00117 
00118     // <IMAGE> (KOffice 1.0) or <PICTURE> (KWord 1.2)
00119     QDomNode node=parentElem.namedItem( "PICTURE" );
00120     if ( node.isNull() )
00121     {
00122         node=parentElem.namedItem( "IMAGE" );
00123     }
00124     QDomElement image = node.toElement();
00125     if ( image.isNull() )
00126         image = parentElem; // The data is directly child of <FORMAT>
00127     // <KEY>
00128     QDomElement keyElement = image.namedItem( "KEY" ).toElement();
00129     if ( !keyElement.isNull() )
00130     {
00131         KoPictureKey key;
00132         key.loadAttributes( keyElement );
00133         m_image.setKey(key);
00134         doc->addTextImageRequest( this );
00135     }
00136     else
00137     {
00138         // <FILENAME> (old format, up to KWord-1.1-beta2)
00139         QDomElement filenameElement = image.namedItem( "FILENAME" ).toElement();
00140         if ( !filenameElement.isNull() )
00141         {
00142             QString filename = filenameElement.attribute( "value" );
00143             m_image.setKey( KoPictureKey( filename ) );
00144             doc->addTextImageRequest( this );
00145         }
00146         else
00147         {
00148             kdError(32001) << "Missing KEY or FILENAME tag in IMAGE (KWTextImage::load)" << endl;
00149         }
00150     }
00151 }
00152 
00153 KoPictureKey KWTextImage::getKey( void ) const
00154 {
00155     return m_image.getKey();
00156 }
00157 
00158 void KWTextImage::saveOasis( KoXmlWriter&, KoSavingContext& ) const
00159 {
00160     // Not implemented
00161 }
KDE Home | KDE Accessibility Home | Description of Access Keys