00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "KPrVariableCollection.h"
00022 #include <KoVariable.h>
00023 #include "KPrDocument.h"
00024 #include "KPrCommand.h"
00025 #include <klocale.h>
00026 #include <kdebug.h>
00027 #include <KoXmlNS.h>
00028 #include <KoDom.h>
00029 #include "KPrTextObject.h"
00030 #include "KPrPage.h"
00031
00032 KPrVariableCollection::KPrVariableCollection(KoVariableSettings *_setting, KoVariableFormatCollection* coll)
00033 : KoVariableCollection(_setting, coll)
00034 {
00035 }
00036
00037 KoVariable* KPrVariableCollection::loadOasisField( KoTextDocument* textdoc, const QDomElement& tag, KoOasisContext& context )
00038 {
00039 const QString localName( tag.localName() );
00040 const bool isTextNS = tag.namespaceURI() == KoXmlNS::text;
00041 if ( isTextNS )
00042 {
00043 if ( localName == "object-count" ||
00044 localName == "picture-count" ||
00045 localName == "paragraph-count" ||
00046 localName == "word-count" ||
00047 localName == "character-count" ||
00048 localName == "sentence-count" ||
00049 localName == "line-count" ||
00050 localName == "frame-count" ||
00051 localName == "non-whitespace-character-count" ||
00052 localName == "syllable-count" )
00053 {
00054 QString key = "NUMBER";
00055 int type = VT_STATISTIC;
00056 return loadOasisFieldCreateVariable( textdoc, tag, context, key, type );
00057 }
00058 else
00059 return KoVariableCollection::loadOasisField( textdoc, tag, context );
00060 }
00061 else
00062 return KoVariableCollection::loadOasisField( textdoc, tag, context );
00063 }
00064
00065 KoVariable *KPrVariableCollection::createVariable( int type, short int subtype, KoVariableFormatCollection * coll,
00066 KoVariableFormat *varFormat,KoTextDocument *textdoc,
00067 KoDocument * doc, int _correct, bool _forceDefaultFormat, bool )
00068 {
00069 KPrDocument*m_doc=static_cast<KPrDocument*>(doc);
00070 KoVariable * var = 0L;
00071 switch(type) {
00072 case VT_PGNUM:
00073 {
00074 kdDebug(33001)<<" subtype == KoPageVariable::VST_CURRENT_SECTION :"<<(subtype == KPrPgNumVariable::VST_CURRENT_SECTION)<<endl;
00075 kdDebug(33001)<<" varFormat :"<<varFormat<<endl;
00076 if ( !varFormat )
00077 varFormat = (subtype == KPrPgNumVariable::VST_CURRENT_SECTION) ? coll->format("STRING") : coll->format("NUMBER");
00078 var = new KPrPgNumVariable( textdoc,subtype, varFormat,this,m_doc );
00079 break;
00080 }
00081 case VT_STATISTIC:
00082 if ( !varFormat )
00083 varFormat = coll->format("NUMBER");
00084 var = new KPrStatisticVariable( textdoc, subtype, varFormat, this, m_doc );
00085 break;
00086 default:
00087 return KoVariableCollection::createVariable( type, subtype, coll, varFormat, textdoc,
00088 doc, _correct, _forceDefaultFormat);
00089 }
00090 return var;
00091 }
00092
00093
00094 KPrPgNumVariable::KPrPgNumVariable( KoTextDocument *textdoc, short int subtype, KoVariableFormat *varFormat,
00095 KoVariableCollection *_varColl, KPrDocument *doc )
00096 : KoPageVariable( textdoc, subtype, varFormat ,_varColl ),m_doc(doc)
00097 {
00098 }
00099
00100 void KPrPgNumVariable::recalc()
00101 {
00102 if ( m_subtype == VST_PGNUM_TOTAL )
00103 {
00104 m_varValue = QVariant( (int)(m_doc->getPageNums()+m_varColl->variableSetting()->startingPageNumber()-1));
00105 resize();
00106 }
00107
00108 if ( width == -1 )
00109 width = 0;
00110 }
00111
00112 KPrStatisticVariable::KPrStatisticVariable( KoTextDocument *textdoc, int subtype, KoVariableFormat *varFormat,KoVariableCollection *_varColl, KPrDocument *doc )
00113 : KoStatisticVariable( textdoc, subtype, varFormat, _varColl ),
00114 m_doc(doc)
00115 {
00116 }
00117
00118 void KPrStatisticVariable::recalc()
00119 {
00120 int nb = 0;
00121 ulong charsWithSpace = 0L;
00122 ulong charsWithoutSpace = 0L;
00123 ulong words = 0L;
00124 ulong sentences = 0L;
00125 ulong lines = 0L;
00126 ulong syllables = 0L;
00127 bool frameInfo = ( m_subtype == VST_STATISTIC_NB_WORD ||
00128 m_subtype == VST_STATISTIC_NB_SENTENCE ||
00129 m_subtype == VST_STATISTIC_NB_LINES ||
00130 m_subtype == VST_STATISTIC_NB_CHARACTERE);
00131 KPrPage *page = m_doc->activePage();
00132 if( !page)
00133 return;
00134 QPtrListIterator<KPrObject> objIt( page->objectList() );
00135
00136 for ( objIt.toFirst(); objIt.current(); ++objIt )
00137 {
00138 KPrObject *obj = objIt.current();
00139 if ( m_subtype == VST_STATISTIC_NB_FRAME )
00140 ++nb;
00141 else if( m_subtype == VST_STATISTIC_NB_PICTURE && obj->getType() == OT_PICTURE)
00142 {
00143 ++nb;
00144 }
00145 else if( m_subtype == VST_STATISTIC_NB_EMBEDDED && obj->getType() == OT_PART )
00146 {
00147 ++nb;
00148 }
00149 if ( frameInfo )
00150 {
00151 KPrTextObject *textObj = dynamic_cast<KPrTextObject *>( obj );
00152 if ( textObj )
00153 textObj->textObject()->statistics( 0L, charsWithSpace, charsWithoutSpace, words, sentences, syllables, lines, false );
00154 }
00155 }
00156 if ( frameInfo )
00157 {
00158 if( m_subtype == VST_STATISTIC_NB_WORD )
00159 {
00160 nb = words;
00161 }
00162 else if( m_subtype == VST_STATISTIC_NB_SENTENCE )
00163 {
00164 nb = sentences;
00165 }
00166 else if( m_subtype == VST_STATISTIC_NB_LINES )
00167 {
00168 nb = lines;
00169 }
00170 else if ( m_subtype == VST_STATISTIC_NB_CHARACTERE )
00171 {
00172 nb = charsWithSpace;
00173 }
00174 else if ( m_subtype ==VST_STATISTIC_NB_NON_WHITESPACE_CHARACTERE )
00175 {
00176 nb = charsWithoutSpace;
00177 }
00178 else if ( m_subtype ==VST_STATISTIC_NB_SYLLABLE )
00179 {
00180 nb = syllables;
00181 }
00182 else
00183 nb = 0;
00184 }
00185
00186 m_varValue = QVariant(nb);
00187 resize();
00188 if ( width == -1 )
00189 width = 0;
00190 }
00191
00192 QString KPrStatisticVariable::text(bool realValue)
00193 {
00194 if (m_varColl->variableSetting()->displayFieldCode()&& !realValue)
00195 return fieldCode();
00196 else
00197 return m_varFormat->convert( m_varValue );
00198 }
00199
00200