summaryrefslogtreecommitdiffstats
path: root/kword/KWPictureFrameSet.cpp
blob: c79c1f5cfe0573a58d6f9e277339a6c911854a0e (plain)
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/* This file is part of the KDE project
   Copyright (C) 1998, 1999, 2000 Reginald Stadlbauer <reggie@kde.org>
   Copyright (C) 2005 Thomas Zander <zander@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 "KWPictureFrameSet.h"
#include "KWDocument.h"
#include "KWordPictureFrameSetIface.h"
#include <KoStoreDevice.h>
#include <KoOasisContext.h>
#include <KoPictureCollection.h>
#include <KoDom.h>
#include <KoXmlNS.h>
#include <KoXmlWriter.h>
#include <tdelocale.h>
#include <kdebug.h>

//#define DEBUG_DRAW

KWPictureFrameSet::KWPictureFrameSet( KWDocument *_doc, const TQString & name )
    : KWFrameSet( _doc ), m_keepAspectRatio( true ), m_finalSize( false )
{
    if ( name.isEmpty() )
        m_name = _doc->generateFramesetName( i18n( "Picture %1" ) );
    else
        m_name = name;
}

KWPictureFrameSet::KWPictureFrameSet( KWDocument* doc, const TQDomElement& frame, const TQDomElement& imageTag, KoOasisContext& context )
    : KWFrameSet( doc ), m_keepAspectRatio( true ), m_finalSize( false )
{
    m_name = frame.attributeNS( KoXmlNS::draw, "name", TQString() );
    if ( doc->frameSetByName( m_name ) ) // already exists!
        m_name = doc->generateFramesetName( m_name + " %1" );
    loadOasis( frame, imageTag, context );
}

KWPictureFrameSet::~KWPictureFrameSet() {
}

KWordFrameSetIface* KWPictureFrameSet::dcopObject()
{
    if ( !m_dcop )
        m_dcop = new KWordPictureFrameSetIface( this );

    return m_dcop;
}

void KWPictureFrameSet::loadPicture( const TQString & fileName )
{
    KoPictureCollection *collection = m_doc->pictureCollection();

    m_picture = collection->loadPicture( fileName );
}

void KWPictureFrameSet::insertPicture( const KoPicture& picture )
{
    KoPictureCollection *collection = m_doc->pictureCollection();

    m_picture = collection->insertPicture( picture.getKey(), picture );
}

void KWPictureFrameSet::reloadPicture( const KoPictureKey& key )
{
    KoPictureCollection *collection = m_doc->pictureCollection();
    // If the picture is not already in the collection, then it gives a blank picture
    m_picture = collection->insertPicture( key, KoPicture() );
}

TQDomElement KWPictureFrameSet::save( TQDomElement & parentElem, bool saveFrames )
{
    if ( m_frames.isEmpty() ) // Deleted frameset -> don't save
        return TQDomElement();
    TQDomElement framesetElem = parentElem.ownerDocument().createElement( "FRAMESET" );
    parentElem.appendChild( framesetElem );

    KWFrameSet::saveCommon( framesetElem, saveFrames );

    TQDomElement imageElem = parentElem.ownerDocument().createElement( "PICTURE" );
    framesetElem.appendChild( imageElem );
    imageElem.setAttribute( "keepAspectRatio", m_keepAspectRatio ? "true" : "false" );

    TQDomElement elem = parentElem.ownerDocument().createElement( "KEY" );
    imageElem.appendChild( elem );
    m_picture.getKey().saveAttributes( elem );
    return framesetElem;
}

void KWPictureFrameSet::load( TQDomElement &attributes, bool loadFrames )
{
    KWFrameSet::load( attributes, loadFrames );

    TQString defaultRatio="true";
    // <PICTURE>
    TQDomNode node=attributes.namedItem( "PICTURE" );
    if ( node.isNull() )
    {
        node=attributes.namedItem( "IMAGE" );
        if ( node.isNull() )
        {
            node=attributes.namedItem( "CLIPART" );
            defaultRatio="false";
        }
    }

    TQDomElement image = node.toElement();
    if ( !image.isNull() ) {
        m_keepAspectRatio = image.attribute( "keepAspectRatio", defaultRatio ) == "true";
        // <KEY>
        TQDomElement keyElement = image.namedItem( "KEY" ).toElement();
        if ( !keyElement.isNull() )
        {
            KoPictureKey key;
            key.loadAttributes( keyElement );
            m_picture.clear();
            m_picture.setKey( key );
            m_doc->addPictureRequest( this );
        }
        else
        {
            // <FILENAME> (old format, up to KWord-1.1-beta2)
            TQDomElement filenameElement = image.namedItem( "FILENAME" ).toElement();
            if ( !filenameElement.isNull() )
            {
                TQString filename = filenameElement.attribute( "value" );
                m_picture.clear();
                m_picture.setKey( KoPictureKey( filename ) );
                m_doc->addPictureRequest( this );
            }
            else
            {
                kdError(32001) << "Missing KEY tag in IMAGE" << endl;
            }
        }
    } else {
        kdError(32001) << "Missing PICTURE/IMAGE/CLIPART tag in FRAMESET" << endl;
    }
}

void KWPictureFrameSet::saveOasis( KoXmlWriter& writer, KoSavingContext& context, bool /*saveFrames*/ ) const
{
    if( m_frames.isEmpty() ) // Deleted frameset -> don't save
        return;
    KWFrame* frame = m_frames.getFirst();
    frame->startOasisFrame( writer, context.mainStyles(), name() ); // draw:frame
    writer.startElement( "draw:image" );
    writer.addAttribute( "xlink:type", "simple" );
    writer.addAttribute( "xlink:show", "embed" );
    writer.addAttribute( "xlink:actuate", "onLoad" );
    if ( context.savingMode() == KoSavingContext::Store )
        writer.addAttribute( "xlink:href", m_doc->pictureCollection()->getOasisFileName(m_picture) );
    else {
        writer.startElement( "office:binary-data" );
        m_picture.saveAsBase64( writer );
        writer.endElement();
    }
    writer.endElement();

    writer.endElement(); // draw:frame

}

void KWPictureFrameSet::loadOasis( const TQDomElement& frame, const TQDomElement& tag, KoOasisContext& context )
{
    kdDebug() << k_funcinfo << endl;
    KoPictureKey key;
    TQDomNode binaryData = KoDom::namedItemNS( tag, KoXmlNS::office, "binary-data" );
    if ( !binaryData.isNull() )
    {
        TQCString data = binaryData.toElement().text().latin1();
        m_picture.loadFromBase64( data );
        key = KoPictureKey("nofile", TQDateTime::currentDateTime(TQt::UTC));
        m_picture.setKey(key);
    }
    else
    {
        const TQString href( tag.attributeNS( KoXmlNS::xlink, "href", TQString()) );
        if ( !href.isEmpty() /*&& href[0] == '#'*/ )
        {
            TQString strExtension;
            const int result=href.findRev(".");
            if (result>=0)
            {
                strExtension=href.mid(result+1); // As we are using KoPicture, the extension should be without the dot.
            }
            TQString filename(href/*.mid(1)*/);
            key = KoPictureKey(filename, TQDateTime::currentDateTime(TQt::UTC));
            m_picture.setKey(key);

            KoStore* store = context.store();
            Q_ASSERT( store );
            if ( store->open( filename ) )
            {
                KoStoreDevice dev(store);
                if ( !m_picture.load( &dev, strExtension ) )
                    kdWarning(32001) << "Cannot load picture: " << filename << " " << href << endl;
                store->close();
            }
        }
    }

    m_doc->pictureCollection()->insertPicture( key, m_picture );
    context.styleStack().save();
    context.fillStyleStack( frame, KoXmlNS::draw, "style-name", "graphic" ); // get the style for the graphics element
    loadOasisFrame( frame, context );
    context.styleStack().restore();
}

void KWPictureFrameSet::drawFrameContents( KWFrame *frame, TQPainter *painter, const TQRect &crect,
                                           const TQColorGroup &, bool, bool, KWFrameSetEdit *, KWViewMode * )
{
#ifdef DEBUG_DRAW
    kdDebug(32001) << "KWPictureFrameSet::drawFrameContents crect=" << crect << " size=" << kWordDocument()->zoomItX( frame->innerWidth() ) << "x" << kWordDocument()->zoomItY( frame->innerHeight() ) << endl;
#endif
    m_picture.draw( *painter, 0, 0, kWordDocument()->zoomItX( frame->innerWidth() ), kWordDocument()->zoomItY( frame->innerHeight() ),
                  crect.x(), crect.y(), crect.width(), crect.height(), !m_finalSize);
}

FrameSetType KWPictureFrameSet::type() const
{
    return FT_PICTURE;
}

bool KWPictureFrameSet::keepAspectRatio() const
{
    return m_keepAspectRatio;
}

void KWPictureFrameSet::setKeepAspectRatio( bool b )
{
    m_keepAspectRatio = b;
}

#ifndef NDEBUG
void KWPictureFrameSet::printDebug( KWFrame *frame )
{
    KWFrameSet::printDebug( frame );
    if ( !isDeleted() )
    {
        kdDebug(32001) << "Image: key=" << m_picture.getKey().toString() << endl;
    }
}
#endif