00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "KWCommand.h"
00021 #include "KWDocument.h"
00022 #include "KWTextDocument.h"
00023 #include "KWTextFrameSet.h"
00024 #include "KWTextParag.h"
00025 #include "KWLoadingInfo.h"
00026 #include "KWVariable.h"
00027 #include "KWAnchor.h"
00028 #include "KWOasisLoader.h"
00029 #include "KWTableFrameSet.h"
00030
00031 #include <KoOasisContext.h>
00032 #include <KoXmlNS.h>
00033 #include <KoDom.h>
00034
00035 #include <kdebug.h>
00036 #include <kglobalsettings.h>
00037 #include <klocale.h>
00038
00039 KWTextDocument::KWTextDocument( KWTextFrameSet * textfs, KoTextFormatCollection *fc, KoTextFormatter *formatter )
00040 : KoTextDocument( textfs->kWordDocument(), fc, formatter, false ), m_textfs( textfs )
00041 {
00042 init();
00043 }
00044
00045 KWTextDocument::KWTextDocument( KoTextZoomHandler * zoomHandler )
00046 : KoTextDocument( zoomHandler, new KoTextFormatCollection( KGlobalSettings::generalFont() , QColor(), KGlobal::locale()->language(), false), 0L, false ),
00047 m_textfs( 0 )
00048 {
00049 init();
00050 }
00051
00052 void KWTextDocument::init()
00053 {
00054
00055 clear( true );
00056 }
00057
00058 KWTextDocument::~KWTextDocument()
00059 {
00060 }
00061
00062 KoTextParag * KWTextDocument::createParag( KoTextDocument *d, KoTextParag *pr, KoTextParag *nx, bool updateIds )
00063 {
00064 return new KWTextParag( static_cast<KoTextDocument *>(d), static_cast<KoTextParag *>(pr), static_cast<KoTextParag *>(nx), updateIds );
00065 }
00066
00067 KoTextDocCommand *KWTextDocument::deleteTextCommand( KoTextDocument *textdoc, int id, int index, const QMemArray<KoTextStringChar> & str, const CustomItemsMap & customItemsMap, const QValueList<KoParagLayout> & oldParagLayouts )
00068 {
00069
00070 return new KWTextDeleteCommand( textdoc, id, index, str, customItemsMap, oldParagLayouts );
00071 }
00072
00073 void KWTextDocument::loadOasisTOC( const QDomElement& tag, KoOasisContext& context, KoTextParag* & lastParagraph, KoStyleCollection * styleColl, KoTextParag* nextParagraph )
00074 {
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084 QDomElement tocIndexBody = KoDom::namedItemNS( tag, KoXmlNS::text, "index-body" );
00085 QDomElement t;
00086 forEachElement( t, tocIndexBody )
00087 {
00088 context.styleStack().save();
00089 const QString localName = t.localName();
00090 QDomElement e;
00091 bool isTextNS = tag.namespaceURI() == KoXmlNS::text;
00092 if ( isTextNS && localName == "index-title" ) {
00093 lastParagraph = loadOasisText( t, context, lastParagraph, styleColl, nextParagraph );
00094 lastParagraph->setPartOfTableOfContents( true );
00095 } else if ( isTextNS && localName == "p" ) {
00096 context.fillStyleStack( t, KoXmlNS::text, "style-name", "paragraph" );
00097 lastParagraph = createParag( this, lastParagraph, nextParagraph );
00098 uint pos = 0;
00099 lastParagraph->loadOasis( t, context, styleColl, pos );
00100 lastParagraph->setPartOfTableOfContents( true );
00101 } else
00102 kdWarning() << "OASIS TOC loading: unknown tag " << t.tagName() << " found in index-body" << endl;
00103 context.styleStack().restore();
00104 }
00105
00106 m_textfs->kWordDocument()->setTocPresent( true );
00107 }
00108
00109 bool KWTextDocument::loadOasisBodyTag( const QDomElement& tag, KoOasisContext& context,
00110 KoTextParag* & lastParagraph, KoStyleCollection* styleColl,
00111 KoTextParag* nextParagraph )
00112 {
00113 const QString localName( tag.localName() );
00114
00115 if ( localName == "frame" && tag.namespaceURI() == KoXmlNS::draw )
00116 {
00117 KWDocument* doc = m_textfs->kWordDocument();
00118 KWOasisLoader loader( doc );
00119 KWFrame* frame = loader.loadFrame( tag, context, KoPoint() );
00120 if ( frame )
00121 return true;
00122 }
00123
00124
00125 else if ( localName == "table" && tag.namespaceURI() == KoXmlNS::table )
00126 {
00127 KWDocument* doc = m_textfs->kWordDocument();
00128 KWOasisLoader loader( doc );
00129 KWTableFrameSet* table = loader.loadOasisTable( tag, context );
00130 table->finalize();
00131
00132 KoTextParag *parag = createParag( this, lastParagraph, nextParagraph );
00133 if ( !lastParagraph )
00134 setFirstParag( parag );
00135 lastParagraph = parag;
00136
00137 parag->insert( 0, KoTextObject::customItemChar() );
00138 table->setAnchorFrameset( m_textfs );
00139 parag->setCustomItem( 0, table->createAnchor( m_textfs->textDocument(), 0 ), 0 );
00140 return true;
00141 }
00142 else if ( localName == "table-of-content" && tag.namespaceURI() == KoXmlNS::text )
00143 {
00144 loadOasisTOC( tag, context, lastParagraph, styleColl, nextParagraph );
00145 return true;
00146 }
00147
00148 return false;
00149 }
00150
00151 void KWTextDocument::appendBookmark( KoTextParag* parag, int pos, KoTextParag* endParag, int endPos, const QString& name )
00152 {
00153
00154
00155 m_textfs->kWordDocument()->insertBookmark( name, parag, endParag, pos, endPos );
00156 }
00157
00158 void KWTextDocument::loadOasisFootnote( const QDomElement& tag, KoOasisContext& context,
00159 KoTextCustomItem* & customItem )
00160 {
00161 const QString frameName( tag.attributeNS( KoXmlNS::text, "id", QString::null) );
00162 const QString localName( tag.localName() );
00163 const QDomElement citationElem = tag.namedItem( localName + "-citation" ).toElement();
00164
00165 bool endnote = localName == "endnote" && tag.namespaceURI() == KoXmlNS::text;
00166
00167 QString label = citationElem.attributeNS( KoXmlNS::text, "label", QString::null );
00168 bool autoNumbered = label.isEmpty();
00169
00170 KWFootNoteFrameSet *fs = m_textfs->insertFootNote(
00171 endnote ? EndNote : FootNote,
00172 autoNumbered ? KWFootNoteVariable::Auto : KWFootNoteVariable::Manual,
00173 label );
00174 customItem = fs->footNoteVariable();
00175
00176 fs->createInitialFrame( 0 );
00177
00178
00179 const QDomElement bodyElem = KoDom::namedItemNS( tag, KoXmlNS::text, QCString( localName.latin1() ) + "-body" ).toElement();
00180 fs->loadOasisContent( bodyElem, context );
00181 }
00182
00183 bool KWTextDocument::loadSpanTag( const QDomElement& tag, KoOasisContext& context,
00184 KoTextParag* parag, uint pos,
00185 QString& textData, KoTextCustomItem* & customItem )
00186 {
00187 const QString localName( tag.localName() );
00188 const bool isTextNS = tag.namespaceURI() == KoXmlNS::text;
00189 kdDebug(32500) << "KWTextDocument::loadSpanTag: " << localName << endl;
00190
00191 if ( isTextNS )
00192 {
00193 if ( localName == "a" )
00194 {
00195 QString href( tag.attributeNS( KoXmlNS::xlink, "href", QString::null) );
00196 if ( href.startsWith("#") )
00197 {
00198 context.styleStack().save();
00199
00200
00201 parag->loadOasisSpan( tag, context, pos );
00202 context.styleStack().restore();
00203 }
00204 else
00205 {
00206
00207
00208
00209
00210 QDomElement spanElem = KoDom::namedItemNS( tag, KoXmlNS::text, "span" );
00211 QString text;
00212 if( spanElem.isNull() )
00213 text = tag.text();
00214 else {
00215
00216
00217
00218 context.fillStyleStack( spanElem, KoXmlNS::text, "style-name", "text" );
00219 text = spanElem.text();
00220 }
00221 textData = KoTextObject::customItemChar();
00222
00223 KoVariableCollection& coll = context.variableCollection();
00224 customItem = new KoLinkVariable( this, text, href,
00225 coll.formatCollection()->format( "STRING" ),
00226 &coll );
00227 }
00228 return true;
00229 }
00230 else if ( localName == "bookmark" )
00231 {
00232 appendBookmark( parag, pos, parag, pos, tag.attributeNS( KoXmlNS::text, "name", QString::null ) );
00233 return true;
00234 }
00235 else if ( localName == "bookmark-start" ) {
00236 KWLoadingInfo* loadingInfo = m_textfs->kWordDocument()->loadingInfo();
00237 loadingInfo->m_bookmarkStarts.insert( tag.attributeNS( KoXmlNS::text, "name", QString::null ),
00238 KWLoadingInfo::BookmarkStart( this, parag, pos ) );
00239 return true;
00240 }
00241 else if ( localName == "bookmark-end" ) {
00242 KWLoadingInfo* loadingInfo = m_textfs->kWordDocument()->loadingInfo();
00243 QString bkName = tag.attributeNS( KoXmlNS::text, "name", QString::null );
00244 KWLoadingInfo::BookmarkStartsMap::iterator it = loadingInfo->m_bookmarkStarts.find( bkName );
00245 if ( it == loadingInfo->m_bookmarkStarts.end() ) {
00246
00247 appendBookmark( parag, pos, parag, pos, tag.attributeNS( KoXmlNS::text, "name", QString::null ) );
00248 } else {
00249 if ( (*it).doc != this ) {
00250
00251 kdWarning(32500) << "Cross-frameset bookmark! Not supported." << endl;
00252 } else {
00253 appendBookmark( (*it).parag, (*it).pos, parag, pos, it.key() );
00254 }
00255 loadingInfo->m_bookmarkStarts.remove( it );
00256 }
00257 return true;
00258 }
00259 else if ( localName == "footnote" || localName == "endnote" )
00260 {
00261 textData = KoTextObject::customItemChar();
00262 loadOasisFootnote( tag, context, customItem );
00263 return true;
00264 }
00265 }
00266 else
00267 {
00268 if ( tag.namespaceURI() == KoXmlNS::draw && localName == "frame" )
00269 {
00270 if ( tag.attributeNS( KoXmlNS::koffice, "is-wrapper-frame", QString::null )
00271 == "true" )
00272 {
00273 QDomElement textbox = KoDom::namedItemNS( tag, KoXmlNS::draw, "text-box" );
00274 if ( !textbox.isNull() )
00275 {
00276 int numberOfElements = 0;
00277 QDomElement elem;
00278 QDomElement firstElem;
00279 forEachElement( elem, textbox )
00280 {
00281 ++numberOfElements;
00282 firstElem = elem;
00283 }
00284 if ( numberOfElements == 1 )
00285 {
00286 kdDebug(32001) << "Wrapper frame removed, loading " << firstElem.tagName() << " directly" << endl;
00287
00288 return loadSpanTag( firstElem, context, parag, pos, textData, customItem );
00289 }
00290 }
00291 return true;
00292 }
00293
00294 KWDocument* doc = m_textfs->kWordDocument();
00295 KWOasisLoader loader( doc );
00296 KWFrame* frame = loader.loadFrame( tag, context, KoPoint() );
00297 if ( frame )
00298 {
00299 KWFrameSet* fs = frame->frameSet();
00300
00301
00302
00303
00304 textData = KoTextObject::customItemChar();
00305 fs->setAnchorFrameset( m_textfs );
00306 customItem = fs->createAnchor( m_textfs->textDocument(), 0 );
00307 }
00308 return true;
00309 }
00310
00311
00312 else if ( tag.namespaceURI() == KoXmlNS::table && localName == "table" )
00313 {
00314 KWDocument* doc = m_textfs->kWordDocument();
00315 KWOasisLoader loader( doc );
00316 KWTableFrameSet* table = loader.loadOasisTable( tag, context );
00317 table->finalize();
00318 textData = KoTextObject::customItemChar();
00319 table->setAnchorFrameset( m_textfs );
00320 customItem = table->createAnchor( m_textfs->textDocument(), 0 );
00321 return true;
00322 }
00323 }
00324 return false;
00325 }
00326
00327 #include "KWTextDocument.moc"