kword

KWVariable.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 1998, 1999 Reginald Stadlbauer <reggie@kde.org>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include "KWVariable.h"
00021 
00022 #include "KWTextFrameSet.h"
00023 #include "KWTextDocument.h"
00024 #include "KWMailMergeDataBase.h"
00025 #include "KWDocument.h"
00026 #include "KWCommand.h"
00027 #include "KWViewMode.h"
00028 #include "KWPageManager.h"
00029 
00030 #include <KoVariable.h>
00031 #include <KoXmlWriter.h>
00032 #include <KoXmlNS.h>
00033 #include <KoDom.h>
00034 
00035 #include <klocale.h>
00036 #include <kdebug.h>
00037 
00038 KWVariableSettings::KWVariableSettings() : KoVariableSettings()
00039 {
00040     m_footNoteCounter.setSuffix( QString::null );
00041     m_endNoteCounter.setSuffix( QString::null );
00042     // By default endnotes are numbered in lowercase roman numbers, in other WPs.
00043     m_endNoteCounter.setStyle( KoParagCounter::STYLE_ROM_NUM_L );
00044 }
00045 
00046 void KWVariableSettings::changeFootNoteCounter( KoParagCounter _c )
00047 {
00048     m_footNoteCounter = _c;
00049 }
00050 
00051 void KWVariableSettings::changeEndNoteCounter( KoParagCounter _c )
00052 {
00053     m_endNoteCounter = _c;
00054 }
00055 
00056 void KWVariableSettings::saveNoteConfiguration( KoXmlWriter& writer ) const
00057 {
00058     writer.startElement( "text:notes-configuration" );
00059     writer.addAttribute( "text:note-class", "footnote" );
00060     // let the counter save: num-prefix num-suffix num-format start-value
00061     m_footNoteCounter.saveOasisListLevel( writer, false );
00062     writer.addAttribute( "text:footnotes-position", "page" ); // tell OO what we do
00063     writer.addAttribute( "text:start-numbering-at", "document" ); // tell OO what we do
00064     writer.endElement();
00065     writer.startElement( "text:notes-configuration" );
00066     writer.addAttribute( "text:note-class", "endnote" );
00067     // let the counter save: num-prefix num-suffix num-format start-value
00068     m_endNoteCounter.saveOasisListLevel( writer, false );
00069     writer.addAttribute( "text:start-numbering-at", "document" ); // tell OO what we do
00070     writer.endElement();
00071 }
00072 
00073 void KWVariableSettings::loadNoteConfiguration( const QDomElement& parent )
00074 {
00075     QDomElement e;
00076     forEachElement( e, parent )
00077     {
00078         if ( e.localName() == "notes-configuration" && e.namespaceURI() == KoXmlNS::text )
00079         {
00080             const QString noteClass = e.attributeNS( KoXmlNS::text, "note-class", QString::null );
00081             if ( noteClass == "footnote" ) {
00082                 m_footNoteCounter.loadOasisListStyle( e, QDomElement(), QDomElement(), -1, true, false, 1, false );
00083                 m_footNoteCounter.setNumbering( KoParagCounter::NUM_FOOTNOTE );
00084                 m_footNoteCounter.setRestartCounter( false );
00085             }
00086             else if ( noteClass == "endnote" ) {
00087                 m_endNoteCounter.loadOasisListStyle( e, QDomElement(), QDomElement(), -1, true, false, 1, false );
00088                 m_endNoteCounter.setNumbering( KoParagCounter::NUM_FOOTNOTE );
00089                 m_endNoteCounter.setRestartCounter( false );
00090             }
00091         }
00092     }
00093 }
00094 
00095 void KWVariableSettings::save( QDomElement &parentElem )
00096 {
00097     KoVariableSettings::save( parentElem );
00098     QDomDocument doc = parentElem.ownerDocument();
00099     QDomElement footNoteSettingElem = doc.createElement( "FOOTNOTESETTING" );
00100     parentElem.appendChild( footNoteSettingElem );
00101     m_footNoteCounter.save( footNoteSettingElem );
00102     QDomElement endNoteSettingElem = doc.createElement( "ENDNOTESETTING" );
00103     parentElem.appendChild( endNoteSettingElem );
00104     m_endNoteCounter.save( endNoteSettingElem );
00105 }
00106 
00107 void KWVariableSettings::load( QDomElement &elem )
00108 {
00109     KoVariableSettings::load( elem );
00110     QDomElement footNoteSettings = elem.namedItem( "FOOTNOTESETTING" ).toElement();
00111     if ( !footNoteSettings.isNull() )
00112         m_footNoteCounter.load( footNoteSettings );
00113     QDomElement endNoteSettings = elem.namedItem( "ENDNOTESETTING" ).toElement();
00114     if ( !endNoteSettings.isNull() )
00115         m_endNoteCounter.load( endNoteSettings );
00116 }
00117 
00118 KWVariableCollection::KWVariableCollection(KWVariableSettings *_setting, KoVariableFormatCollection* coll)
00119     : KoVariableCollection(_setting, coll)
00120 {
00121 }
00122 
00123 KoVariable* KWVariableCollection::loadOasisField( KoTextDocument* textdoc, const QDomElement& tag, KoOasisContext& context )
00124 {
00125     const QString localName( tag.localName() );
00126     const bool isTextNS = tag.namespaceURI() == KoXmlNS::text;
00127     if ( isTextNS )
00128     {
00129         //kdDebug()<<" localName :"<<localName<<endl;
00130         if ( localName ==  "note" )
00131         {
00132             QString key = "STRING";
00133             int type = VT_FOOTNOTE;
00134             return loadOasisFieldCreateVariable( textdoc, tag, context, key, type );
00135         }
00136         else if ( localName == "table-count"  ||
00137                   localName == "object-count"  ||
00138                   localName == "picture-count"  ||
00139                   localName == "paragraph-count"  ||
00140                   localName == "word-count"  ||
00141                   localName == "character-count" ||
00142                   localName == "sentence-count" ||
00143                   localName == "line-count" ||
00144                   localName == "frame-count"  ||
00145                   localName == "non-whitespace-character-count" ||
00146                   localName == "syllable-count")
00147         {
00148             QString key = "NUMBER";
00149             int type = VT_STATISTIC;
00150             return loadOasisFieldCreateVariable( textdoc, tag, context, key, type );
00151         }
00152         else
00153             return KoVariableCollection::loadOasisField( textdoc, tag, context );
00154     }
00155     else
00156         return KoVariableCollection::loadOasisField( textdoc, tag, context );
00157 }
00158 
00159 KoVariable *KWVariableCollection::createVariable( int type, short int subtype, KoVariableFormatCollection * coll, KoVariableFormat *varFormat,KoTextDocument *textdoc, KoDocument * doc, int _correct,bool _forceDefaultFormat, bool loadFootNote )
00160 {
00161     KWDocument *m_doc = static_cast<KWDocument *>(doc);
00162     KoVariable * var = 0L;
00163     switch(type) {
00164     case VT_PGNUM:
00165         if ( !varFormat )
00166             varFormat = (subtype == KoPageVariable::VST_CURRENT_SECTION) ? coll->format("STRING") : coll->format("NUMBER");
00167         var = new KWPgNumVariable( textdoc, subtype, varFormat, this, m_doc );
00168         break;
00169     case VT_MAILMERGE:
00170         var = new KWMailMergeVariable( textdoc, QString::null, coll->format("STRING"), this, m_doc );
00171         break;
00172     case VT_FOOTNOTE:
00173         if ( !loadFootNote )
00174             return 0L;
00175         if ( !varFormat )
00176             varFormat =  coll->format("STRING");
00177         var = new KWFootNoteVariable( textdoc, varFormat, this, m_doc );
00178         break;
00179     case VT_STATISTIC:
00180         if ( !varFormat )
00181             varFormat = coll->format("NUMBER");
00182         var = new KWStatisticVariable( textdoc, subtype, varFormat, this, m_doc );
00183         break;
00184     default:
00185         return KoVariableCollection::createVariable( type, subtype, coll, varFormat, textdoc, doc, _correct, _forceDefaultFormat );
00186     }
00187     return var;
00188 }
00189 
00190 /******************************************************************/
00191 /* Class: KWPgNumVariable                                         */
00192 /******************************************************************/
00193 KWPgNumVariable::KWPgNumVariable( KoTextDocument *textdoc, int subtype, KoVariableFormat *varFormat ,KoVariableCollection *_varColl, KWDocument *doc  )
00194     : KoPageVariable( textdoc, subtype, varFormat ,_varColl ), m_doc(doc)
00195 {
00196 }
00197 
00198 void KWPgNumVariable::recalc()
00199 {
00200     if ( !m_doc->layoutViewMode()->hasPages() ) // ModeText
00201     {
00202         //necessary to resize it in this mode because in this mode
00203         //we don't call KWTextFrameSet::drawFrame()
00204         resize();
00205         return;
00206     }
00207 
00208     if ( m_subtype == VST_PGNUM_TOTAL )
00209     {
00210         m_varValue = QVariant(m_doc->pageCount()+m_varColl->variableSetting()->startingPageNumber()-1);
00211         resize();
00212     }
00213     // The other cases are handled by the more dynamic code in KWTextFrameSet::drawFrame()
00214     // But we don't want to keep a width of -1 ...
00215     if ( width == -1 )
00216         width = 0;
00217 }
00218 
00219 QString KWPgNumVariable::text(bool realValue)
00220 {
00221     if (m_varColl->variableSetting()->displayFieldCode()&& !realValue)
00222         return fieldCode();
00223     // #### ??? What?
00224     else if ( m_subtype !=  VST_CURRENT_SECTION && !m_doc->layoutViewMode()->hasPages() && !realValue)
00225         return fieldCode();
00226     else
00227         return m_varFormat->convert( m_varValue );
00228 }
00229 
00230 /******************************************************************/
00231 /* Class: KWMailMergeVariable                                  */
00232 /******************************************************************/
00233 KWMailMergeVariable::KWMailMergeVariable( KoTextDocument *textdoc, const QString &name, KoVariableFormat *varFormat,KoVariableCollection *_varColl, KWDocument *doc  )
00234     : KoMailMergeVariable( textdoc, name, varFormat,_varColl ), m_doc(doc)
00235 {
00236 }
00237 
00238 QString KWMailMergeVariable::value() const
00239 {
00240     return m_doc->mailMergeDataBase()->getValue( m_varValue.toString() );
00241 }
00242 
00243 QString KWMailMergeVariable::text(bool realValue)
00244 {
00245     if (m_varColl->variableSetting()->displayFieldCode()&& !realValue)
00246         return fieldCode();
00247     // ## should use a format maybe
00248     QString v = value();
00249     if ( m_doc->mailMergeDataBase()->isSampleRecord() )
00250         return "<" + v + ">";
00251     return v;
00252 }
00253 
00254 void KWMailMergeVariable::recalc()
00255 {
00256     resize();
00257 }
00258 
00260 
00261 KWFootNoteVariable::KWFootNoteVariable( KoTextDocument *textdoc, KoVariableFormat *varFormat, KoVariableCollection *varColl, KWDocument *doc )
00262     : KoVariable( textdoc, varFormat, varColl ),
00263       m_doc(doc),
00264       m_frameset( 0L ),
00265       m_numberingType( Auto ),
00266       m_num( -1 ),
00267       m_numDisplay( -1 )
00268 {
00269     m_varValue = QVariant( QString::null );
00270 }
00271 
00272 void KWFootNoteVariable::setNumberingType( Numbering _type )
00273 {
00274     m_numberingType = _type;
00275     //delete m_varFormat;
00276     setVariableFormat(m_doc->variableFormatCollection()->format("STRING"));
00277 }
00278 
00279 void KWFootNoteVariable::loadOasis( const QDomElement& footNoteTag, KoOasisContext& context )
00280 {
00281     /*<text:note text:id="ftn0" text:note-class="footnote"><text:note-citation>1</text:note-citation><text:note-body>
00282       <text:p text:style-name="Footnote"></text:p></text:note-body></text:note> */
00283     const QString id = footNoteTag.attributeNS( KoXmlNS::text, "id", QString::null );
00284 
00285     if ( footNoteTag.hasAttributeNS( KoXmlNS::text, "note-class" ) )
00286     {
00287       const QString str =  footNoteTag.attributeNS( KoXmlNS::text, "note-class", QString::null );
00288       if ( str == "footnote" )
00289         m_noteType = FootNote;
00290       else if ( str == "endnote" )
00291         m_noteType = EndNote;
00292       else {
00293         kdWarning()<<" Unknown footnote type: '" << str << "'" << endl;
00294         m_noteType = FootNote;
00295       }
00296     }
00297 
00298     QDomElement element;
00299     QDomElement bodyElement;
00300     forEachElement( element, footNoteTag )
00301     {
00302       if ( element.namespaceURI() != KoXmlNS::text )
00303         continue;
00304 
00305       const QString localName = element.localName();
00306       if( localName == "note-citation" )
00307       {
00308         if ( element.hasAttributeNS( KoXmlNS::text, "label" ) )
00309             m_numberingType = Manual;
00310         else
00311             m_numberingType = Auto;
00312         if ( m_numberingType == Auto )
00313         {
00314             //kdDebug()<<" automatic \n";
00315             m_numDisplay = element.text().toInt();
00316             formatedNote();
00317         }
00318         else
00319         {
00320            // kdDebug()<<" manual \n";
00321             m_varValue = QVariant( element.text() );
00322         }
00323       } else if ( localName == "note-body" ) {
00324           bodyElement = element;
00325       }
00326     }
00327     Q_ASSERT( !bodyElement.isNull() );
00328 
00329     Q_ASSERT( !m_frameset );
00330     m_frameset = new KWFootNoteFrameSet( m_doc, id );
00331     m_frameset->setFrameSetInfo( KWFrameSet::FI_FOOTNOTE );
00332 
00333     m_frameset->setFootNoteVariable( this );
00334     m_frameset->createInitialFrame( 0 );
00335     m_doc->addFrameSet( m_frameset );
00336 
00337     // Load the body of the footnote
00338     m_frameset->loadOasisContent( bodyElement, context );
00339 }
00340 
00341 void KWFootNoteVariable::saveOasis( KoXmlWriter& writer, KoSavingContext& context ) const
00342 {
00343     //<text:note text:id="ftn0" text:note-class="footnote"><text:note-citation>1</text:note-citation><text:note-body><text:p text:style-name="Footnote"/></text:note-body></text:note>
00344     //<text:note text:id="ftn1" text:note-class="endnote"><text:note-citation>i</text:note-citation><text:note-body><text:p text:style-name="Endnote"/></text:note-body></text:note>
00345     //<text:note text:id="ftn2" text:note-class="footnote"><text:note-citation text:label="vv">vv</text:note-citation><text:note-body><text:p text:style-name="Footnote"/></text:note-body></text:note>
00346     writer.startElement( "text:note" );
00347     writer.addAttribute( "text:id",m_frameset->name() );
00348     writer.addAttribute( "text:note-class", m_noteType == FootNote ? "footnote" : "endnote" );
00349 
00350     writer.startElement( "text:note-citation" );
00351     if ( m_numberingType == Auto )
00352         writer.addTextNode( QString( "%1" ).arg( m_numDisplay ) );
00353     else
00354     {
00355         writer.addAttribute( "text:label", m_varValue.toString() );
00356         writer.addTextNode(m_varValue.toString() );
00357     }
00358     writer.endElement();
00359 
00360     writer.startElement( "text:note-body" );
00361     //save text from end/footnote
00362     m_frameset->saveOasisContent( writer, context );
00363     writer.endElement();
00364 
00365     writer.endElement();
00366 }
00367 
00368 void KWFootNoteVariable::saveVariable( QDomElement &parentElem )
00369 {
00370     QDomElement footnoteElem = parentElem.ownerDocument().createElement( "FOOTNOTE" );
00371     parentElem.appendChild( footnoteElem );
00372     //footnoteElem.setAttribute( "subtype", 0 );
00373     if ( m_numberingType == Auto )
00374         footnoteElem.setAttribute( "value", m_numDisplay );
00375     else
00376         footnoteElem.setAttribute( "value", m_varValue.toString() );
00377     footnoteElem.setAttribute( "notetype", m_noteType == FootNote ? "footnote" : "endnote" );
00378     footnoteElem.setAttribute( "numberingtype", m_numberingType == Auto ? "auto" : "manual" );
00379     Q_ASSERT( m_frameset );
00380     if( m_frameset )
00381       footnoteElem.setAttribute( "frameset", m_frameset->name() );
00382 }
00383 
00384 void KWFootNoteVariable::load( QDomElement &elem )
00385 {
00386     KoVariable::load( elem );
00387     QDomElement footnoteElem = elem.namedItem( "FOOTNOTE" ).toElement();
00388     if (!footnoteElem.isNull())
00389     {
00390         //m_subtype = footnoteElem.attribute("subtype").toInt();
00391         QString str = footnoteElem.attribute("notetype").lower();
00392         if ( str == "footnote" )
00393             m_noteType = FootNote;
00394         else if ( str == "endnote" )
00395             m_noteType = EndNote;
00396         else
00397             kdWarning() << "Unknown footnote type: '" << str << "'" << endl;
00398 
00399         str = footnoteElem.attribute("numberingtype").lower();
00400         if ( str == "auto" )
00401             m_numberingType = Auto;
00402         else if ( str == "manual")
00403             m_numberingType = Manual;
00404         else
00405             kdWarning() << "Unknown footnote numbering: '" << str << "'" << endl;
00406 
00407         if ( m_numberingType == Auto )
00408         {
00409             m_numDisplay = footnoteElem.attribute("value").toInt();
00410             formatedNote();
00411         }
00412         else
00413             m_varValue = QVariant(footnoteElem.attribute("value"));
00414 
00415         str = footnoteElem.attribute("frameset");
00416         m_doc->addFootNoteRequest( str, this );
00417     }
00418 }
00419 
00420 void KWFootNoteVariable::formatedNote()
00421 {
00422     if ( m_numberingType == Auto )
00423     {
00424         m_varValue = QVariant(applyStyle());
00425     }
00426 }
00427 
00428 QString KWFootNoteVariable::applyStyle()
00429 {
00430     KWVariableSettings* settings = static_cast<KWVariableSettings*>(m_varColl->variableSetting());
00431     KoParagCounter tmpCounter = (m_noteType == FootNote) ? settings->footNoteCounter() : settings->endNoteCounter();
00432 
00433     QString tmp;
00434     int val = m_numDisplay + tmpCounter.startNumber()-1;
00435     Q_ASSERT( val >= 0 );
00436     if ( val < 0 ) // let's not go into makeRomanNumber with a negative number :}
00437         return i18n("ERROR");
00438     switch ( tmpCounter.style() )
00439     {
00440     case KoParagCounter::STYLE_NUM:
00441         tmp.setNum( val );
00442         break;
00443     case KoParagCounter::STYLE_ALPHAB_L:
00444         tmp=KoParagCounter::makeAlphaLowerNumber( val );
00445         break;
00446     case KoParagCounter::STYLE_ALPHAB_U:
00447         tmp=KoParagCounter::makeAlphaUpperNumber( val );
00448         break;
00449     case KoParagCounter::STYLE_ROM_NUM_L:
00450         tmp = KoParagCounter::makeRomanNumber( val ).lower();
00451         break;
00452     case KoParagCounter::STYLE_ROM_NUM_U:
00453         tmp = KoParagCounter::makeRomanNumber( val ).upper();
00454         break;
00455     case KoParagCounter::STYLE_CUSTOMBULLET:
00456         //todo change font
00457         tmp = tmpCounter.customBulletCharacter();
00458         break;
00459     default:
00460         tmp.setNum( val );
00461         break;
00462     }
00463 
00464     tmp.prepend( tmpCounter.prefix() );
00465     tmp.append( tmpCounter.suffix() );
00466     return tmp;
00467 }
00468 
00469 QString KWFootNoteVariable::text(bool realValue)
00470 {
00471     if (m_varColl->variableSetting()->displayFieldCode()&& !realValue)
00472         return fieldCode();
00473     return m_varFormat->convert( m_varValue );
00474 }
00475 
00476 void KWFootNoteVariable::setNumDisplay( int val )
00477 {
00478     m_numDisplay = val;
00479     if ( val != -1 ) // -1 is used to 'invalidate so that renumberFootNotes recalcs'
00480         formatedNote();
00481 }
00482 
00483 QString KWFootNoteVariable::fieldCode()
00484 {
00485     return (noteType()==FootNote) ?i18n("Footnote"):i18n("Endnote");
00486 }
00487 
00488 void KWFootNoteVariable::drawCustomItem( QPainter* p, int x, int y, int wpix, int hpix, int ascentpix, int /*cx*/, int /*cy*/, int /*cw*/, int /*ch*/, const QColorGroup& cg, bool selected, int _offset, bool drawingShadow )
00489 {
00490     KoTextFormat * fmt = format();
00491     KoTextZoomHandler * zh = textDocument()->paintingZoomHandler();
00492 
00493     // Force drawing as "superscript" - hmm, the formatting will use too big font metrics though.
00494     QFont font( fmt->screenFont( zh ) );
00495     int pointSize = ( ( font.pointSize() * 2 ) / 3 );
00496     font.setPointSize( pointSize );
00497 
00498     int offset = _offset;
00499     if ( offset == 0 )
00500     {
00501         int h = zh->layoutUnitToPixelY( /*_y HACK,*/ height );
00502         offset = -( h - QFontMetrics(font).height() );
00503     }
00504 
00505     QColor textColor( fmt->color() );
00506     drawCustomItemHelper( p, x, y, wpix, hpix, ascentpix, cg, selected, offset, fmt, font, textColor, drawingShadow );
00507 }
00508 
00509 void KWFootNoteVariable::finalize()
00510 {
00511     Q_ASSERT( m_frameset );
00512     if (!m_frameset )
00513         return;
00514     Q_ASSERT( !m_frameset->isDeleted() );
00515     if ( m_frameset->isDeleted() )
00516         return;
00517 
00518     //kdDebug(32001) << "KWFootNoteVariable::finalize" << endl;
00519 
00520     int pageNum = this->pageNum();
00521     if ( pageNum == -1 )
00522         return;
00523 
00524     KWFrame* footNoteFrame = m_frameset->frame( 0 );
00525     int framePage = footNoteFrame->pageNumber();
00526     if ( framePage != pageNum )
00527     {
00528         //kdDebug(32001) << "Footnote var '" << text() << "' at page " << pageNum << ", footnote frame at page " << framePage << " -> abortFormatting() and recalcFrames()" << endl;
00529         KWTextFrameSet * fs = static_cast<KWTextDocument *>(textDocument())->textFrameSet();
00530         fs->textObject()->abortFormatting();
00531 
00532         // abortFormatting is a bool in kotextobject. So we need to return there before
00533         // starting text layout again.
00534         m_doc->delayedRecalcFrames( QMIN( pageNum, framePage ) );
00535         m_doc->delayedRepaintAllViews();
00536     }
00537 }
00538 
00539 void KWFootNoteVariable::resize()
00540 {
00541     if ( m_deleted )
00542         return;
00543     KoTextFormat *fmt = format();
00544     QFont font( fmt->refFont() ); // LU font
00545     if ( fmt->vAlign() == KoTextFormat::AlignNormal ) // if it's still normal...
00546     {
00547         int pointSize = ( ( font.pointSize() * 2 ) / 3 ); // ...force superscript
00548         font.setPointSize( pointSize );
00549     }
00550     QFontMetrics fm( font );
00551     QString txt = text();
00552     width = 0;
00553     for ( int i = 0 ; i < (int)txt.length() ; ++i )
00554         width += fm.charWidth( txt, i ); // size at 100%
00555     // zoom to LU
00556     width = qRound( KoTextZoomHandler::ptToLayoutUnitPt( width ) );
00557     height = fmt->height();
00558     m_ascent = fmt->ascent();
00559     //kdDebug() << "KWFootNoteVariable::resize text=" << txt << " width=" << width << " height=" << height << endl;
00560 }
00561 
00562 void KWFootNoteVariable::setDeleted( bool del )
00563 {
00564     kdDebug() << "KWFootNoteVariable::setDeleted " << del << endl;
00565     if ( del )
00566     {
00567         Q_ASSERT( m_frameset );
00568         if ( m_frameset ) {
00569             m_frameset->deleteAllFrames(); // Important, because we don't want to save it!
00570             m_frameset->setVisible( false );
00571         }
00572     }
00573     else
00574     {
00575         Q_ASSERT( m_frameset );
00576         if ( m_frameset ) {
00577             kdDebug() << "Making frameset " << m_frameset << " visible" << endl;
00578             m_frameset->setVisible( true );
00579             if ( m_frameset->isDeleted() )
00580                 m_frameset->createInitialFrame( 0 ); // Page number shouldn't matter (see recalcFrames below).
00581             Q_ASSERT( m_frameset->isVisible() );
00582         }
00583     }
00584 
00585     // hmm, maybe compress all the stuff below and do only once
00586     // (e.g. when deleting multiple footnotes)?
00587     // (but we can't really delay it with a sst, the formatMore after undo/redo
00588     // needs this to be done already, I think). Bah.
00589 
00590     // Re-number footnote variables
00591     KWTextFrameSet * textfs = static_cast<KWTextDocument *>(textDocument())->textFrameSet();
00592     textfs->renumberFootNotes();
00593 
00594     m_doc->recalcFrames();
00595 
00596     if (!del)
00597         m_frameset->layout(); // format its text, so that it resizes the frame
00598     KoVariable::setDeleted( del );
00599     // Does this compress? Probably not.
00600     m_doc->delayedRepaintAllViews();
00601 }
00602 
00603 int KWFootNoteVariable::pageNum() const
00604 {
00605     int page = m_doc->pageManager()->pageNumber(varY());
00606     Q_ASSERT( page <= m_doc->lastPage() );
00607     return page;
00608 }
00609 
00610 double KWFootNoteVariable::varY() const
00611 {
00612     // Find out the position of the footnote variable in document coordinates.
00613     int paragy = paragraph()->rect().y();
00614     KWTextFrameSet * fs = static_cast<KWTextDocument *>(textDocument())->textFrameSet();
00615     if ( !fs->hasFramesInPageArray() ) // we need it for internalToDocument
00616     {
00617         kdDebug(32001) << "KWFootNoteVariable::varY too early, no updateFrames yet" << endl;
00618         return 0; // this happens on loading - frame layout is done before text layout
00619     }
00620     // What we need is "has never been formatted". Not "has just been invalidated"...
00621     //if ( !paragraph()->isValid() )
00622     //{
00623     //    kdDebug(32001) << "KWFootNoteVariable::varY called but paragraph " << paragraph()->paragId() << " not valid" << endl;
00624     //    return 0;
00625     //}
00626     KoPoint dPoint;
00627     //kdDebug(32001) << "KWFootNoteVariable::pageNum position of variable (LU): " << QPoint( x(), paragy + y() + height ) << endl;
00628     KWFrame* containingFrame = fs->internalToDocument( QPoint( x(), paragy + y() + height ), dPoint );
00629     if ( containingFrame )
00630     {
00631         // Ok, the (bottom of the) footnote variable is at dPoint.
00632         double varY = dPoint.y();
00633         //kdDebug(32001) << " found containingFrame " << containingFrame << " page:" << containingFrame->pageNumber() << "  varY=" << varY << endl;
00634         //int pageNum = containingFrame->pageNumber(); // and at page pageNum
00635         return varY;
00636     } else
00637     {
00638         // This can happen if the page hasn't been created yet
00639         //kdDebug(32001) << "KWFootNoteVariable::pageNum internalToDocument returned 0L for " << x << ", " << y+paragy << endl;
00640         return 0;
00641     }
00642 }
00643 
00644 
00645 KWStatisticVariable::KWStatisticVariable( KoTextDocument *textdoc,  int subtype, KoVariableFormat *varFormat,KoVariableCollection *_varColl, KWDocument *doc )
00646     : KoStatisticVariable( textdoc, subtype, varFormat, _varColl ),
00647       m_doc(doc)
00648 {
00649 }
00650 
00651 void KWStatisticVariable::recalc()
00652 {
00653     int nb = 0;
00654     ulong charsWithSpace = 0L;
00655     ulong charsWithoutSpace = 0L;
00656     ulong words = 0L;
00657     ulong sentences = 0L;
00658     ulong lines = 0L;
00659     ulong syllables = 0L;
00660     bool frameInfo = ( m_subtype == VST_STATISTIC_NB_WORD ||
00661                         m_subtype == VST_STATISTIC_NB_SENTENCE ||
00662                         m_subtype == VST_STATISTIC_NB_LINES ||
00663                         m_subtype == VST_STATISTIC_NB_CHARACTERE);
00664     QPtrListIterator<KWFrameSet> framesetIt( m_doc->framesetsIterator() );
00665     //TODO change int to ulong
00666     for ( framesetIt.toFirst(); framesetIt.current(); ++framesetIt )
00667     {
00668         KWFrameSet *frameSet = framesetIt.current();
00669         if ( frameSet->isVisible() )
00670         {
00671             if ( m_subtype == VST_STATISTIC_NB_FRAME )
00672             {
00673                 ++nb;
00674             }
00675             else if( m_subtype == VST_STATISTIC_NB_PICTURE && frameSet->type() == FT_PICTURE )
00676             {
00677                 ++nb;
00678             }
00679             else if( m_subtype == VST_STATISTIC_NB_TABLE && frameSet->type() == FT_TABLE )
00680             {
00681                 ++nb;
00682             }
00683             else if( m_subtype == VST_STATISTIC_NB_EMBEDDED && frameSet->type() == FT_PART )
00684             {
00685                 ++nb;
00686             }
00687             if ( frameInfo
00688                  && (frameSet->frameSetInfo() == KWFrameSet::FI_FOOTNOTE || frameSet->frameSetInfo() == KWFrameSet::FI_BODY)
00689                  && frameSet->isVisible() )
00690             {
00691                 frameSet->statistics( 0L, charsWithSpace, charsWithoutSpace,
00692                                       words, sentences, syllables, lines, false );
00693             }
00694         }
00695         if ( frameInfo )
00696         {
00697             if( m_subtype == VST_STATISTIC_NB_WORD )
00698             {
00699                 nb = words;
00700             }
00701             else if( m_subtype == VST_STATISTIC_NB_SENTENCE )
00702             {
00703                 nb = sentences;
00704             }
00705             else if( m_subtype == VST_STATISTIC_NB_LINES )
00706             {
00707                 nb = lines;
00708             }
00709             else if ( m_subtype == VST_STATISTIC_NB_CHARACTERE )
00710             {
00711                 nb = charsWithSpace;
00712             }
00713             else if ( m_subtype == VST_STATISTIC_NB_NON_WHITESPACE_CHARACTERE )
00714             {
00715                 nb = charsWithoutSpace;
00716             }
00717             else if ( m_subtype == VST_STATISTIC_NB_SYLLABLE )
00718             {
00719                 nb = syllables;
00720             }
00721             else
00722                 nb = 0;
00723         }
00724     }
00725     m_varValue = QVariant(nb);
00726     resize();
00727     if ( width == -1 )
00728         width = 0;
00729 }
00730 
00731 QString KWStatisticVariable::text(bool realValue)
00732 {
00733     if ( m_varColl->variableSetting()->displayFieldCode() && !realValue )
00734         return fieldCode();
00735     else
00736         return m_varFormat->convert( m_varValue );
00737 }
KDE Home | KDE Accessibility Home | Description of Access Keys