kword

KWPictureFrameSet.cpp
1/* This file is part of the KDE project
2 Copyright (C) 1998, 1999, 2000 Reginald Stadlbauer <reggie@kde.org>
3 Copyright (C) 2005 Thomas Zander <zander@kde.org>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19*/
20
21#include "KWPictureFrameSet.h"
22#include "KWDocument.h"
23#include "KWordPictureFrameSetIface.h"
24#include <KoStoreDevice.h>
25#include <KoOasisContext.h>
26#include <KoPictureCollection.h>
27#include <KoDom.h>
28#include <KoXmlNS.h>
29#include <KoXmlWriter.h>
30#include <tdelocale.h>
31#include <kdebug.h>
32
33//#define DEBUG_DRAW
34
35KWPictureFrameSet::KWPictureFrameSet( KWDocument *_doc, const TQString & name )
36 : KWFrameSet( _doc ), m_keepAspectRatio( true ), m_finalSize( false )
37{
38 if ( name.isEmpty() )
39 m_name = _doc->generateFramesetName( i18n( "Picture %1" ) );
40 else
41 m_name = name;
42}
43
44KWPictureFrameSet::KWPictureFrameSet( KWDocument* doc, const TQDomElement& frame, const TQDomElement& imageTag, KoOasisContext& context )
45 : KWFrameSet( doc ), m_keepAspectRatio( true ), m_finalSize( false )
46{
47 m_name = frame.attributeNS( KoXmlNS::draw, "name", TQString() );
48 if ( doc->frameSetByName( m_name ) ) // already exists!
49 m_name = doc->generateFramesetName( m_name + " %1" );
50 loadOasis( frame, imageTag, context );
51}
52
53KWPictureFrameSet::~KWPictureFrameSet() {
54}
55
56KWordFrameSetIface* KWPictureFrameSet::dcopObject()
57{
58 if ( !m_dcop )
59 m_dcop = new KWordPictureFrameSetIface( this );
60
61 return m_dcop;
62}
63
64void KWPictureFrameSet::loadPicture( const TQString & fileName )
65{
66 KoPictureCollection *collection = m_doc->pictureCollection();
67
68 m_picture = collection->loadPicture( fileName );
69}
70
71void KWPictureFrameSet::insertPicture( const KoPicture& picture )
72{
73 KoPictureCollection *collection = m_doc->pictureCollection();
74
75 m_picture = collection->insertPicture( picture.getKey(), picture );
76}
77
78void KWPictureFrameSet::reloadPicture( const KoPictureKey& key )
79{
80 KoPictureCollection *collection = m_doc->pictureCollection();
81 // If the picture is not already in the collection, then it gives a blank picture
82 m_picture = collection->insertPicture( key, KoPicture() );
83}
84
85TQDomElement KWPictureFrameSet::save( TQDomElement & parentElem, bool saveFrames )
86{
87 if ( m_frames.isEmpty() ) // Deleted frameset -> don't save
88 return TQDomElement();
89 TQDomElement framesetElem = parentElem.ownerDocument().createElement( "FRAMESET" );
90 parentElem.appendChild( framesetElem );
91
92 KWFrameSet::saveCommon( framesetElem, saveFrames );
93
94 TQDomElement imageElem = parentElem.ownerDocument().createElement( "PICTURE" );
95 framesetElem.appendChild( imageElem );
96 imageElem.setAttribute( "keepAspectRatio", m_keepAspectRatio ? "true" : "false" );
97
98 TQDomElement elem = parentElem.ownerDocument().createElement( "KEY" );
99 imageElem.appendChild( elem );
100 m_picture.getKey().saveAttributes( elem );
101 return framesetElem;
102}
103
104void KWPictureFrameSet::load( TQDomElement &attributes, bool loadFrames )
105{
106 KWFrameSet::load( attributes, loadFrames );
107
108 TQString defaultRatio="true";
109 // <PICTURE>
110 TQDomNode node=attributes.namedItem( "PICTURE" );
111 if ( node.isNull() )
112 {
113 node=attributes.namedItem( "IMAGE" );
114 if ( node.isNull() )
115 {
116 node=attributes.namedItem( "CLIPART" );
117 defaultRatio="false";
118 }
119 }
120
121 TQDomElement image = node.toElement();
122 if ( !image.isNull() ) {
123 m_keepAspectRatio = image.attribute( "keepAspectRatio", defaultRatio ) == "true";
124 // <KEY>
125 TQDomElement keyElement = image.namedItem( "KEY" ).toElement();
126 if ( !keyElement.isNull() )
127 {
128 KoPictureKey key;
129 key.loadAttributes( keyElement );
130 m_picture.clear();
131 m_picture.setKey( key );
132 m_doc->addPictureRequest( this );
133 }
134 else
135 {
136 // <FILENAME> (old format, up to KWord-1.1-beta2)
137 TQDomElement filenameElement = image.namedItem( "FILENAME" ).toElement();
138 if ( !filenameElement.isNull() )
139 {
140 TQString filename = filenameElement.attribute( "value" );
141 m_picture.clear();
142 m_picture.setKey( KoPictureKey( filename ) );
143 m_doc->addPictureRequest( this );
144 }
145 else
146 {
147 kdError(32001) << "Missing KEY tag in IMAGE" << endl;
148 }
149 }
150 } else {
151 kdError(32001) << "Missing PICTURE/IMAGE/CLIPART tag in FRAMESET" << endl;
152 }
153}
154
155void KWPictureFrameSet::saveOasis( KoXmlWriter& writer, KoSavingContext& context, bool /*saveFrames*/ ) const
156{
157 if( m_frames.isEmpty() ) // Deleted frameset -> don't save
158 return;
159 KWFrame* frame = m_frames.getFirst();
160 frame->startOasisFrame( writer, context.mainStyles(), name() ); // draw:frame
161 writer.startElement( "draw:image" );
162 writer.addAttribute( "xlink:type", "simple" );
163 writer.addAttribute( "xlink:show", "embed" );
164 writer.addAttribute( "xlink:actuate", "onLoad" );
165 if ( context.savingMode() == KoSavingContext::Store )
166 writer.addAttribute( "xlink:href", m_doc->pictureCollection()->getOasisFileName(m_picture) );
167 else {
168 writer.startElement( "office:binary-data" );
169 m_picture.saveAsBase64( writer );
170 writer.endElement();
171 }
172 writer.endElement();
173
174 writer.endElement(); // draw:frame
175
176}
177
178void KWPictureFrameSet::loadOasis( const TQDomElement& frame, const TQDomElement& tag, KoOasisContext& context )
179{
180 kdDebug() << k_funcinfo << endl;
181 KoPictureKey key;
182 TQDomNode binaryData = KoDom::namedItemNS( tag, KoXmlNS::office, "binary-data" );
183 if ( !binaryData.isNull() )
184 {
185 TQCString data = binaryData.toElement().text().latin1();
186 m_picture.loadFromBase64( data );
187 key = KoPictureKey("nofile", TQDateTime::currentDateTime(TQt::UTC));
188 m_picture.setKey(key);
189 }
190 else
191 {
192 const TQString href( tag.attributeNS( KoXmlNS::xlink, "href", TQString()) );
193 if ( !href.isEmpty() /*&& href[0] == '#'*/ )
194 {
195 TQString strExtension;
196 const int result=href.findRev(".");
197 if (result>=0)
198 {
199 strExtension=href.mid(result+1); // As we are using KoPicture, the extension should be without the dot.
200 }
201 TQString filename(href/*.mid(1)*/);
202 key = KoPictureKey(filename, TQDateTime::currentDateTime(TQt::UTC));
203 m_picture.setKey(key);
204
205 KoStore* store = context.store();
206 Q_ASSERT( store );
207 if ( store->open( filename ) )
208 {
209 KoStoreDevice dev(store);
210 if ( !m_picture.load( &dev, strExtension ) )
211 kdWarning(32001) << "Cannot load picture: " << filename << " " << href << endl;
212 store->close();
213 }
214 }
215 }
216
217 m_doc->pictureCollection()->insertPicture( key, m_picture );
218 context.styleStack().save();
219 context.fillStyleStack( frame, KoXmlNS::draw, "style-name", "graphic" ); // get the style for the graphics element
220 loadOasisFrame( frame, context );
221 context.styleStack().restore();
222}
223
224void KWPictureFrameSet::drawFrameContents( KWFrame *frame, TQPainter *painter, const TQRect &crect,
225 const TQColorGroup &, bool, bool, KWFrameSetEdit *, KWViewMode * )
226{
227#ifdef DEBUG_DRAW
228 kdDebug(32001) << "KWPictureFrameSet::drawFrameContents crect=" << crect << " size=" << kWordDocument()->zoomItX( frame->innerWidth() ) << "x" << kWordDocument()->zoomItY( frame->innerHeight() ) << endl;
229#endif
230 m_picture.draw( *painter, 0, 0, kWordDocument()->zoomItX( frame->innerWidth() ), kWordDocument()->zoomItY( frame->innerHeight() ),
231 crect.x(), crect.y(), crect.width(), crect.height(), !m_finalSize);
232}
233
234FrameSetType KWPictureFrameSet::type() const
235{
236 return FT_PICTURE;
237}
238
239bool KWPictureFrameSet::keepAspectRatio() const
240{
241 return m_keepAspectRatio;
242}
243
244void KWPictureFrameSet::setKeepAspectRatio( bool b )
245{
246 m_keepAspectRatio = b;
247}
248
249#ifndef NDEBUG
250void KWPictureFrameSet::printDebug( KWFrame *frame )
251{
252 KWFrameSet::printDebug( frame );
253 if ( !isDeleted() )
254 {
255 kdDebug(32001) << "Image: key=" << m_picture.getKey().toString() << endl;
256 }
257}
258#endif
This object is created to edit a particular frameset in a particular view The view's canvas creates i...
Class: KWFrameSet Base type, a frameset holds content as well as frames to show that content.
Definition KWFrameSet.h:62
virtual FrameSetType type() const
The type of frameset.
Definition KWFrameSet.h:76
bool isDeleted() const
True if the frameset was deleted (but not destroyed, since it's in the undo/redo).
Definition KWFrameSet.h:166
virtual void load(TQDomElement &framesetElem, bool loadFrames=true)
load from XML - when loading
void saveCommon(TQDomElement &parentElem, bool saveFrames)
save the common attributes for the frameset
KWFrame * frame(unsigned int _num) const
get a frame by number
TQString name() const
get/set frameset name. For tables in particular, this must be unique
Definition KWFrameSet.h:428
This class represents a single frame.
Definition KWFrame.h:68
Abstract base class for KWCanvas's view modes.
Definition KWViewMode.h:48