00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "KWPictureFrameSet.h"
00022 #include "KWDocument.h"
00023 #include "KWordPictureFrameSetIface.h"
00024 #include <KoStoreDevice.h>
00025 #include <KoOasisContext.h>
00026 #include <KoPictureCollection.h>
00027 #include <KoDom.h>
00028 #include <KoXmlNS.h>
00029 #include <KoXmlWriter.h>
00030 #include <klocale.h>
00031 #include <kdebug.h>
00032
00033
00034
00035 KWPictureFrameSet::KWPictureFrameSet( KWDocument *_doc, const QString & name )
00036 : KWFrameSet( _doc ), m_keepAspectRatio( true ), m_finalSize( false )
00037 {
00038 if ( name.isEmpty() )
00039 m_name = _doc->generateFramesetName( i18n( "Picture %1" ) );
00040 else
00041 m_name = name;
00042 }
00043
00044 KWPictureFrameSet::KWPictureFrameSet( KWDocument* doc, const QDomElement& frame, const QDomElement& imageTag, KoOasisContext& context )
00045 : KWFrameSet( doc ), m_keepAspectRatio( true ), m_finalSize( false )
00046 {
00047 m_name = frame.attributeNS( KoXmlNS::draw, "name", QString::null );
00048 if ( doc->frameSetByName( m_name ) )
00049 m_name = doc->generateFramesetName( m_name + " %1" );
00050 loadOasis( frame, imageTag, context );
00051 }
00052
00053 KWPictureFrameSet::~KWPictureFrameSet() {
00054 }
00055
00056 KWordFrameSetIface* KWPictureFrameSet::dcopObject()
00057 {
00058 if ( !m_dcop )
00059 m_dcop = new KWordPictureFrameSetIface( this );
00060
00061 return m_dcop;
00062 }
00063
00064 void KWPictureFrameSet::loadPicture( const QString & fileName )
00065 {
00066 KoPictureCollection *collection = m_doc->pictureCollection();
00067
00068 m_picture = collection->loadPicture( fileName );
00069 }
00070
00071 void KWPictureFrameSet::insertPicture( const KoPicture& picture )
00072 {
00073 KoPictureCollection *collection = m_doc->pictureCollection();
00074
00075 m_picture = collection->insertPicture( picture.getKey(), picture );
00076 }
00077
00078 void KWPictureFrameSet::reloadPicture( const KoPictureKey& key )
00079 {
00080 KoPictureCollection *collection = m_doc->pictureCollection();
00081
00082 m_picture = collection->insertPicture( key, KoPicture() );
00083 }
00084
00085 QDomElement KWPictureFrameSet::save( QDomElement & parentElem, bool saveFrames )
00086 {
00087 if ( m_frames.isEmpty() )
00088 return QDomElement();
00089 QDomElement framesetElem = parentElem.ownerDocument().createElement( "FRAMESET" );
00090 parentElem.appendChild( framesetElem );
00091
00092 KWFrameSet::saveCommon( framesetElem, saveFrames );
00093
00094 QDomElement imageElem = parentElem.ownerDocument().createElement( "PICTURE" );
00095 framesetElem.appendChild( imageElem );
00096 imageElem.setAttribute( "keepAspectRatio", m_keepAspectRatio ? "true" : "false" );
00097
00098 QDomElement elem = parentElem.ownerDocument().createElement( "KEY" );
00099 imageElem.appendChild( elem );
00100 m_picture.getKey().saveAttributes( elem );
00101 return framesetElem;
00102 }
00103
00104 void KWPictureFrameSet::load( QDomElement &attributes, bool loadFrames )
00105 {
00106 KWFrameSet::load( attributes, loadFrames );
00107
00108 QString defaultRatio="true";
00109
00110 QDomNode node=attributes.namedItem( "PICTURE" );
00111 if ( node.isNull() )
00112 {
00113 node=attributes.namedItem( "IMAGE" );
00114 if ( node.isNull() )
00115 {
00116 node=attributes.namedItem( "CLIPART" );
00117 defaultRatio="false";
00118 }
00119 }
00120
00121 QDomElement image = node.toElement();
00122 if ( !image.isNull() ) {
00123 m_keepAspectRatio = image.attribute( "keepAspectRatio", defaultRatio ) == "true";
00124
00125 QDomElement keyElement = image.namedItem( "KEY" ).toElement();
00126 if ( !keyElement.isNull() )
00127 {
00128 KoPictureKey key;
00129 key.loadAttributes( keyElement );
00130 m_picture.clear();
00131 m_picture.setKey( key );
00132 m_doc->addPictureRequest( this );
00133 }
00134 else
00135 {
00136
00137 QDomElement filenameElement = image.namedItem( "FILENAME" ).toElement();
00138 if ( !filenameElement.isNull() )
00139 {
00140 QString filename = filenameElement.attribute( "value" );
00141 m_picture.clear();
00142 m_picture.setKey( KoPictureKey( filename ) );
00143 m_doc->addPictureRequest( this );
00144 }
00145 else
00146 {
00147 kdError(32001) << "Missing KEY tag in IMAGE" << endl;
00148 }
00149 }
00150 } else {
00151 kdError(32001) << "Missing PICTURE/IMAGE/CLIPART tag in FRAMESET" << endl;
00152 }
00153 }
00154
00155 void KWPictureFrameSet::saveOasis( KoXmlWriter& writer, KoSavingContext& context, bool ) const
00156 {
00157 if( m_frames.isEmpty() )
00158 return;
00159 KWFrame* frame = m_frames.getFirst();
00160 frame->startOasisFrame( writer, context.mainStyles(), name() );
00161 writer.startElement( "draw:image" );
00162 writer.addAttribute( "xlink:type", "simple" );
00163 writer.addAttribute( "xlink:show", "embed" );
00164 writer.addAttribute( "xlink:actuate", "onLoad" );
00165 if ( context.savingMode() == KoSavingContext::Store )
00166 writer.addAttribute( "xlink:href", m_doc->pictureCollection()->getOasisFileName(m_picture) );
00167 else {
00168 writer.startElement( "office:binary-data" );
00169 m_picture.saveAsBase64( writer );
00170 writer.endElement();
00171 }
00172 writer.endElement();
00173
00174 writer.endElement();
00175
00176 }
00177
00178 void KWPictureFrameSet::loadOasis( const QDomElement& frame, const QDomElement& tag, KoOasisContext& context )
00179 {
00180 kdDebug() << k_funcinfo << endl;
00181 KoPictureKey key;
00182 QDomNode binaryData = KoDom::namedItemNS( tag, KoXmlNS::office, "binary-data" );
00183 if ( !binaryData.isNull() )
00184 {
00185 QCString data = binaryData.toElement().text().latin1();
00186 m_picture.loadFromBase64( data );
00187 key = KoPictureKey("nofile", QDateTime::currentDateTime(Qt::UTC));
00188 m_picture.setKey(key);
00189 }
00190 else
00191 {
00192 const QString href( tag.attributeNS( KoXmlNS::xlink, "href", QString::null) );
00193 if ( !href.isEmpty() )
00194 {
00195 QString strExtension;
00196 const int result=href.findRev(".");
00197 if (result>=0)
00198 {
00199 strExtension=href.mid(result+1);
00200 }
00201 QString filename(href);
00202 key = KoPictureKey(filename, QDateTime::currentDateTime(Qt::UTC));
00203 m_picture.setKey(key);
00204
00205 KoStore* store = context.store();
00206 Q_ASSERT( store );
00207 if ( store->open( filename ) )
00208 {
00209 KoStoreDevice dev(store);
00210 if ( !m_picture.load( &dev, strExtension ) )
00211 kdWarning(32001) << "Cannot load picture: " << filename << " " << href << endl;
00212 store->close();
00213 }
00214 }
00215 }
00216
00217 m_doc->pictureCollection()->insertPicture( key, m_picture );
00218 context.styleStack().save();
00219 context.fillStyleStack( frame, KoXmlNS::draw, "style-name", "graphic" );
00220 loadOasisFrame( frame, context );
00221 context.styleStack().restore();
00222 }
00223
00224 void KWPictureFrameSet::drawFrameContents( KWFrame *frame, QPainter *painter, const QRect &crect,
00225 const QColorGroup &, bool, bool, KWFrameSetEdit *, KWViewMode * )
00226 {
00227 #ifdef DEBUG_DRAW
00228 kdDebug(32001) << "KWPictureFrameSet::drawFrameContents crect=" << crect << " size=" << kWordDocument()->zoomItX( frame->innerWidth() ) << "x" << kWordDocument()->zoomItY( frame->innerHeight() ) << endl;
00229 #endif
00230 m_picture.draw( *painter, 0, 0, kWordDocument()->zoomItX( frame->innerWidth() ), kWordDocument()->zoomItY( frame->innerHeight() ),
00231 crect.x(), crect.y(), crect.width(), crect.height(), !m_finalSize);
00232 }
00233
00234 FrameSetType KWPictureFrameSet::type() const
00235 {
00236 return FT_PICTURE;
00237 }
00238
00239 bool KWPictureFrameSet::keepAspectRatio() const
00240 {
00241 return m_keepAspectRatio;
00242 }
00243
00244 void KWPictureFrameSet::setKeepAspectRatio( bool b )
00245 {
00246 m_keepAspectRatio = b;
00247 }
00248
00249 #ifndef NDEBUG
00250 void KWPictureFrameSet::printDebug( KWFrame *frame )
00251 {
00252 KWFrameSet::printDebug( frame );
00253 if ( !isDeleted() )
00254 {
00255 kdDebug(32001) << "Image: key=" << m_picture.getKey().toString() << endl;
00256 }
00257 }
00258 #endif