00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "KWAnchor.h"
00021 #include "KWTextFrameSet.h"
00022 #include "KWDocument.h"
00023 #include "KWViewMode.h"
00024 #include "KWView.h"
00025 #include <KoXmlWriter.h>
00026 #include <kdebug.h>
00027
00028
00029
00030 KWAnchor::KWAnchor( KoTextDocument *textDocument, KWFrameSet * frameset, int frameNum )
00031 : KoTextCustomItem( textDocument),
00032 m_frameset( frameset ),
00033 m_frameNum( frameNum )
00034 {
00035 }
00036
00037 KWAnchor::~KWAnchor()
00038 {
00039 kdDebug(32001) << "KWAnchor::~KWAnchor" << endl;
00040 }
00041
00042 void KWAnchor::setFormat( KoTextFormat* format )
00043 {
00044 m_frameset->setAnchorFormat( format, m_frameNum );
00045 }
00046
00047 void KWAnchor::finalize()
00048 {
00049 if ( m_deleted )
00050 return;
00051
00052 int paragx = paragraph()->rect().x();
00053 int paragy = paragraph()->rect().y();
00054 kdDebug(32001) << this << " KWAnchor::finalize " << x() << "," << y() << " paragx=" << paragx << " paragy=" << paragy << endl;
00055
00056 KWTextFrameSet * fs = static_cast<KWTextDocument *>(textDocument())->textFrameSet();
00057 KoPoint dPoint;
00058 if ( fs->internalToDocument( QPoint( x()+paragx, y()+paragy ), dPoint ) )
00059 {
00060
00061
00062 m_frameset->moveFloatingFrame( m_frameNum, dPoint );
00063 } else
00064 {
00065
00066 kdDebug(32001) << "KWAnchor::move internalToDocument returned 0L for " << x()+paragx << ", " << y()+paragy << endl;
00067 }
00068 }
00069
00070 void KWAnchor::draw( QPainter* p, int x, int y, int cx, int cy, int cw, int ch, const QColorGroup& cg, bool selected )
00071 {
00072
00073
00074
00075 if ( m_deleted )
00076 return;
00077
00078 Q_ASSERT( x == xpos );
00079 Q_ASSERT( y == ypos );
00080 if ( x != xpos || y != ypos )
00081 kdDebug() << "Warning: x=" << x << " y=" << y << " xpos=" << xpos << " ypos=" << ypos << endl;
00082
00083
00084 KWTextFrameSet * fs = static_cast<KWTextDocument *>(textDocument())->textFrameSet();
00085 KoTextZoomHandler* zh = fs->textDocument()->paintingZoomHandler();
00086
00087 int paragx = paragraph()->rect().x();
00088 int paragy = paragraph()->rect().y();
00089 QRect inlineFrameLU( paragx+xpos, paragy+ypos, width, height );
00090 #ifdef DEBUG_DRAWING
00091 kdDebug(32001) << "KWAnchor::draw x:" << x << ", y:" << y << " paragx=" << paragx << " paragy=" << paragy << endl;
00092 kdDebug(32001) << " inline frame in LU coordinates: " << inlineFrameLU << endl;
00093 #endif
00094
00095 QRect crectLU = QRect( (cx > 0 ? cx : 0)+paragx, cy+paragy, cw, ch );
00096 #ifdef DEBUG_DRAWING
00097 kdDebug(32001) << " crect in LU coordinates: " << DEBUGRECT( crectLU ) << endl;
00098 #endif
00099
00100 crectLU = crectLU.intersect ( inlineFrameLU );
00101
00102
00103 #ifdef DEBUG_DRAWING
00104 kdDebug(32001) << " crect&frame in LU coordinates: " << DEBUGRECT( crectLU ) << endl;
00105 #endif
00106
00107
00108 QPoint topLeftLU = crectLU.topLeft();
00109 QPoint bottomRightLU = crectLU.bottomRight();
00110 KWFrame* containingFrame = fs->currentDrawnFrame();
00111 if(containingFrame)
00112 containingFrame = KWFrameSet::settingsFrame(containingFrame);
00113 else {
00114 QPoint paragPos = inlineFrameLU.topLeft();
00115 KoPoint dummy(0, 0);
00116 containingFrame = fs->internalToDocument(paragPos, dummy);
00117 }
00118 KoPoint topLeftPt = fs->internalToDocumentKnowingFrame( topLeftLU, containingFrame );
00119
00120
00121 KoPoint bottomRightPt = fs->internalToDocumentKnowingFrame( bottomRightLU, containingFrame );
00122 KoRect crectPt( topLeftPt, bottomRightPt );
00123
00124
00125 QRect crect = fs->currentViewMode()->normalToView( zh->zoomRect( crectPt ) );
00126
00127 crect.rBottom() += 2;
00128 crect.rRight() += 1;
00129 #ifdef DEBUG_DRAWING
00130 kdDebug() << " crect in view coordinates (pixel) : " << DEBUGRECT( crect ) << endl;
00131 #endif
00132
00133
00134
00135
00136
00137 if ( containingFrame && containingFrame->isCopy() )
00138 {
00139
00140 QPtrListIterator<KWFrame> frameIt( fs->frameIterator() );
00141 frameIt.toLast();
00142 while ( !frameIt.atFirst() && frameIt.current() != containingFrame )
00143 --frameIt;
00144 if ( frameIt.atFirst() && frameIt.current() != containingFrame )
00145 kdWarning() << "KWAnchor::draw: containingFrame not found " << containingFrame << endl;
00146 while ( !frameIt.atFirst() && frameIt.current()->isCopy() )
00147 --frameIt;
00148 containingFrame = frameIt.current();
00149
00150 }
00151
00152
00153 KoPoint topLeftParagPt( 0, 0 );
00154 if ( containingFrame )
00155 topLeftParagPt = containingFrame->innerRect().topLeft();
00156
00157 topLeftParagPt.rx() += zh->layoutUnitPtToPt( zh->pixelYToPt( paragx ) );
00158 topLeftParagPt.ry() += zh->layoutUnitPtToPt( zh->pixelYToPt( paragy ) );
00159 if ( containingFrame )
00160 topLeftParagPt.ry() -= containingFrame->internalY();
00161
00162 QPoint topLeftParag = fs->currentViewMode()->normalToView( zh->zoomPoint( topLeftParagPt ) );
00163
00164
00165
00166
00167 p->save();
00168 p->translate( -topLeftParag.x(), -topLeftParag.y() );
00169 #ifdef DEBUG_DRAWING
00170 kdDebug() << " translating by " << -topLeftParag.x() << "," << -topLeftParag.y() << endl;
00171 #endif
00172
00173 QColorGroup cg2( cg );
00174
00175 KWFrameViewManager *fvm = 0;
00176 if(m_frameset->kWordDocument()) {
00177 QValueList<KWView *> views = m_frameset->kWordDocument()->getAllViews();
00178
00179 if ( !views.isEmpty() )
00180 fvm = views.first()->frameViewManager();
00181 }
00182 m_frameset->drawContents( p, crect, cg2, false, true, 0L, fs->currentViewMode(), fvm);
00183
00184 if( selected && placement() == PlaceInline && p->device()->devType() != QInternal::Printer )
00185 {
00186
00187
00188 QRect frameRect = crect;
00189 #ifdef DEBUG_DRAWING
00190 kdDebug() << "KWAnchor::draw selected frame. frameRect=" << frameRect << endl;
00191 #endif
00192 p->fillRect( frameRect, QBrush( cg.highlight(), QBrush::Dense4Pattern) );
00193 }
00194 p->restore();
00195
00196 #ifdef DEBUG_DRAWING
00197 kdDebug() << "KWAnchor::draw done" << endl;
00198 #endif
00199 }
00200
00201 QSize KWAnchor::size() const
00202 {
00203 KoSize kosz = m_frameset->floatingFrameSize( m_frameNum );
00204
00205 KoTextZoomHandler * zh = textDocument()->formattingZoomHandler();
00206 QSize sz( zh->ptToLayoutUnitPixX( kosz.width() ), zh->ptToLayoutUnitPixY( kosz.height() ) );
00207
00208
00209
00210 if ( sz.isNull() )
00211 sz = QSize( width, height );
00212 return sz;
00213 }
00214
00215 int KWAnchor::ascent() const
00216 {
00217 int baseline = m_frameset->floatingFrameBaseline( m_frameNum );
00218 int ret = ( baseline == -1 ) ? height : baseline;
00219
00220 return ret;
00221 }
00222
00223 void KWAnchor::resize()
00224 {
00225 if ( m_deleted )
00226 return;
00227 QSize s = size();
00228 if ( width != s.width() || height != s.height() )
00229 {
00230 width = s.width();
00231 height = s.height();
00232 kdDebug(32001) << "KWAnchor::resize " << width << "x" << height << endl;
00233 KoTextParag * parag = paragraph();
00234 if ( parag )
00235 {
00236 kdDebug(32001) << "KWAnchor::resize invalidating parag " << parag->paragId() << endl;
00237 parag->invalidate( 0 );
00238 }
00239 }
00240 }
00241
00242 KCommand * KWAnchor::createCommand()
00243 {
00244 kdDebug(32001) << "KWAnchor::addCreateCommand" << endl;
00245 return m_frameset->anchoredObjectCreateCommand( m_frameNum );
00246 }
00247
00248 KCommand * KWAnchor::deleteCommand()
00249 {
00250 kdDebug(32001) << "KWAnchor::addDeleteCommand" << endl;
00251 return m_frameset->anchoredObjectDeleteCommand( m_frameNum );
00252 }
00253
00254 void KWAnchor::setDeleted( bool b )
00255 {
00256
00257 KoTextCustomItem::setDeleted( b );
00258
00259 kdDebug() << "KWAnchor::setDeleted " << b << endl;
00260 if ( b )
00261 m_frameset->setAnchored( 0L );
00262 else
00263 m_frameset->setAnchored( static_cast<KWTextDocument *>(textDocument())->textFrameSet() );
00264 }
00265
00266 void KWAnchor::save( QDomElement &parentElem )
00267 {
00268 QDomElement anchorElem = parentElem.ownerDocument().createElement( "ANCHOR" );
00269 parentElem.appendChild( anchorElem );
00270 anchorElem.setAttribute( "type", "frameset" );
00271
00272
00273
00274 anchorElem.setAttribute( "instance", m_frameset->name() );
00275 }
00276
00277 void KWAnchor::saveOasis( KoXmlWriter& writer, KoSavingContext& context ) const
00278 {
00279 if ( m_frameset->canBeSavedAsInlineCharacter() )
00280 m_frameset->saveOasis( writer, context, true );
00281 else
00282 {
00283 writer.startElement( "draw:frame" );
00284 writer.addAttribute( "draw:name", m_frameset->name() + "-Wrapper" );
00285
00286 writer.addAttribute( "koffice:is-wrapper-frame", "true" );
00287
00288 KoSize kosz = m_frameset->floatingFrameSize( m_frameNum );
00289 writer.addAttributePt( "svg:width", kosz.width() );
00290 writer.addAttributePt( "svg:height", kosz.height() );
00291 writer.startElement( "draw:text-box" );
00292 m_frameset->saveOasis( writer, context, true );
00293 writer.endElement();
00294 writer.endElement();
00295 }
00296 }
00297
00298 bool KWAnchor::ownLine() const
00299 {
00300 if ( m_deleted )
00301 return FALSE;
00302
00303 if ( m_frameset)
00304 return m_frameset->ownLine();
00305 return FALSE;
00306 }