kword

KWTextImage.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2001 David Faure <faure@kde.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#include <kdebug.h>
21
22#include <KoPictureCollection.h>
23
24#include "KWDocument.h"
25#include "defs.h"
26#include "KWTextFrameSet.h"
27
28#include "KWTextImage.h"
29
30KWTextImage::KWTextImage( KWTextDocument *textdoc, const TQString & filename )
31 : KoTextCustomItem( textdoc ), place( PlaceInline )
32{
33 KWDocument * doc = textdoc->textFrameSet()->kWordDocument();
34 if ( !filename.isEmpty() )
35 {
36 m_image = doc->pictureCollection()->loadPicture( filename );
37 Q_ASSERT( !m_image.isNull() );
38 resize(); // Zoom if necessary
39 }
40}
41
42void KWTextImage::setImage( const KoPictureCollection & collection )
43{
44 kdDebug(32001) << "Loading text image " << m_image.getKey().toString() << " (in KWTextImage::setImage)" << endl;
45 m_image=collection.findPicture( m_image.getKey() );
46 Q_ASSERT( !m_image.isNull() );
47 kdDebug(32001) << "size: " << m_image.getOriginalSize().width() << "x" << m_image.getOriginalSize().height() << endl;
48 resize();
49}
50
51void KWTextImage::resize()
52{
53 if ( m_deleted )
54 return;
55 if ( !m_image.isNull() ) {
56 //KWDocument * doc = static_cast<KWTextDocument *>(parent)->textFrameSet()->kWordDocument();
57 width = m_image.getOriginalSize().width();
58 // width is a 100%-zoom pixel size. We want a LU pixel size -> we simply need 'to LU', i.e. ptToLayoutPt
59 width = KoTextZoomHandler::ptToLayoutUnitPt( width );
60 height = m_image.getOriginalSize().height();
61 height = KoTextZoomHandler::ptToLayoutUnitPt( height );
62 kdDebug() << "KWTextImage::resize: " << width << ", " << height << endl;
63 // no! m_image.setSize( TQSize( width, height ) );
64 }
65}
66
67void KWTextImage::drawCustomItem( TQPainter* p, int x, int y, int wpix, int hpix, int /*ascentpix*/, int cx, int cy, int cw, int ch, const TQColorGroup& cg, bool selected, int /*offset*/, bool drawingShadow)
68{
69 if ( drawingShadow )
70 return;
71
72 // (x,y) is the position of the inline item (in pixels)
73 // (wpix,hpix) is the size of the inline item (in pixels)
74 // (cx,cy,cw,ch) is the rectangle to be painted, in pixels too
75 if ( m_image.isNull() ) {
76 kdDebug() << "KWTextImage::draw null image!" << endl;
77 p->fillRect( x, y, 50, 50, cg.dark() );
78 return;
79 }
80
81 TQSize imgSize( wpix, hpix );
82
83 TQRect rect( TQPoint(x, y), imgSize );
84 if ( !rect.intersects( TQRect( cx, cy, cw, ch ) ) )
85 return;
86
87 TQPixmap pixmap=m_image.generatePixmap( imgSize, true );
88 //if ( placement() == PlaceInline )
89 p->drawPixmap( x, y, pixmap );
90 //else
91 // p->drawPixmap( cx, cy, pixmap, cx - x, cy - y, cw, ch );
92
93 if ( selected && placement() == PlaceInline && p->device()->devType() != TQInternal::Printer ) {
94 p->fillRect( rect , TQBrush( cg.highlight(), TQBrush::Dense4Pattern) );
95 }
96}
97
98void KWTextImage::save( TQDomElement & parentElem )
99{
100 // This code is similar to KWPictureFrameSet::save
101 KWDocument * doc = static_cast<KWTextDocument *>(parent)->textFrameSet()->kWordDocument();
102
103 TQDomElement imageElem = parentElem.ownerDocument().createElement( "PICTURE" );
104 parentElem.appendChild( imageElem );
105 //imageElem.setAttribute( "keepAspectRatio", "true" );
106 TQDomElement elem = parentElem.ownerDocument().createElement( "KEY" );
107 imageElem.appendChild( elem );
108 image().getKey().saveAttributes( elem );
109 // Now we must take care that a <KEY> element will be written as a child of <PICTURES>
110 doc->addTextImageRequest( this );
111}
112
113void KWTextImage::load( TQDomElement & parentElem )
114{
115 // This code is similar to KWPictureFrameSet::load
116 KWDocument * doc = static_cast<KWTextDocument *>(parent)->textFrameSet()->kWordDocument();
117
118 // <IMAGE> (KOffice 1.0) or <PICTURE> (KWord 1.2)
119 TQDomNode node=parentElem.namedItem( "PICTURE" );
120 if ( node.isNull() )
121 {
122 node=parentElem.namedItem( "IMAGE" );
123 }
124 TQDomElement image = node.toElement();
125 if ( image.isNull() )
126 image = parentElem; // The data is directly child of <FORMAT>
127 // <KEY>
128 TQDomElement keyElement = image.namedItem( "KEY" ).toElement();
129 if ( !keyElement.isNull() )
130 {
131 KoPictureKey key;
132 key.loadAttributes( keyElement );
133 m_image.setKey(key);
134 doc->addTextImageRequest( this );
135 }
136 else
137 {
138 // <FILENAME> (old format, up to KWord-1.1-beta2)
139 TQDomElement filenameElement = image.namedItem( "FILENAME" ).toElement();
140 if ( !filenameElement.isNull() )
141 {
142 TQString filename = filenameElement.attribute( "value" );
143 m_image.setKey( KoPictureKey( filename ) );
144 doc->addTextImageRequest( this );
145 }
146 else
147 {
148 kdError(32001) << "Missing KEY or FILENAME tag in IMAGE (KWTextImage::load)" << endl;
149 }
150 }
151}
152
153KoPictureKey KWTextImage::getKey( void ) const
154{
155 return m_image.getKey();
156}
157
158void KWTextImage::saveOasis( KoXmlWriter&, KoSavingContext& ) const
159{
160 // Not implemented
161}
This is our KoTextDocument reimplementation, to create KWTextParag instead of KoTextParags,...
KWTextFrameSet * textFrameSet() const
Return the text frameset in which this document is.
KWTextImage(KWTextDocument *textdoc, const TQString &filename)
Set filename to load a real file from the disk Otherwise use setImage() - this is what's done on load...
KoPictureKey getKey(void) const
Get the key of the picture.