kpresenter

KPrVariableCollection.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2001 Laurent MONTEL <lmontel@mandrakesoft.com>
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 "KPrVariableCollection.h"
21#include <KoVariable.h>
22#include "KPrDocument.h"
23#include "KPrCommand.h"
24#include <tdelocale.h>
25#include <kdebug.h>
26#include <KoXmlNS.h>
27#include <KoDom.h>
28#include "KPrTextObject.h"
29#include "KPrPage.h"
30
31KPrVariableCollection::KPrVariableCollection(KoVariableSettings *_setting, KoVariableFormatCollection* coll)
32 : KoVariableCollection(_setting, coll)
33{
34}
35
36KoVariable* KPrVariableCollection::loadOasisField( KoTextDocument* textdoc, const TQDomElement& tag, KoOasisContext& context )
37{
38 const TQString localName( tag.localName() );
39 const bool isTextNS = tag.namespaceURI() == KoXmlNS::text;
40 if ( isTextNS )
41 {
42 if ( localName == "object-count" ||
43 localName == "picture-count" ||
44 localName == "paragraph-count" ||
45 localName == "word-count" ||
46 localName == "character-count" ||
47 localName == "sentence-count" ||
48 localName == "line-count" ||
49 localName == "frame-count" ||
50 localName == "non-whitespace-character-count" ||
51 localName == "syllable-count" )
52 {
53 TQString key = "NUMBER";
54 int type = VT_STATISTIC;
55 return loadOasisFieldCreateVariable( textdoc, tag, context, key, type );
56 }
57 else
58 return KoVariableCollection::loadOasisField( textdoc, tag, context );
59 }
60 else
61 return KoVariableCollection::loadOasisField( textdoc, tag, context );
62}
63
64KoVariable *KPrVariableCollection::createVariable( int type, short int subtype, KoVariableFormatCollection * coll,
65 KoVariableFormat *varFormat,KoTextDocument *textdoc,
66 KoDocument * doc, int _correct, bool _forceDefaultFormat, bool /*loadFootNote*/ )
67{
68 KPrDocument*m_doc=static_cast<KPrDocument*>(doc);
69 KoVariable * var = 0L;
70 switch(type) {
71 case VT_PGNUM:
72 {
73 kdDebug(33001)<<" subtype == KoPageVariable::VST_CURRENT_SECTION :"<<(subtype == KPrPgNumVariable::VST_CURRENT_SECTION)<<endl;
74 kdDebug(33001)<<" varFormat :"<<varFormat<<endl;
75 if ( !varFormat )
76 varFormat = (subtype == KPrPgNumVariable::VST_CURRENT_SECTION) ? coll->format("STRING") : coll->format("NUMBER");
77 var = new KPrPgNumVariable( textdoc,subtype, varFormat,this,m_doc );
78 break;
79 }
80 case VT_STATISTIC:
81 if ( !varFormat )
82 varFormat = coll->format("NUMBER");
83 var = new KPrStatisticVariable( textdoc, subtype, varFormat, this, m_doc );
84 break;
85 default:
86 return KoVariableCollection::createVariable( type, subtype, coll, varFormat, textdoc,
87 doc, _correct, _forceDefaultFormat);
88 }
89 return var;
90}
91
92
93KPrPgNumVariable::KPrPgNumVariable( KoTextDocument *textdoc, short int subtype, KoVariableFormat *varFormat,
94 KoVariableCollection *_varColl, KPrDocument *doc )
95 : KoPageVariable( textdoc, subtype, varFormat ,_varColl ),m_doc(doc)
96{
97}
98
99void KPrPgNumVariable::recalc()
100{
101 if ( m_subtype == VST_PGNUM_TOTAL )
102 {
103 m_varValue = TQVariant( (int)(m_doc->getPageNums()+m_varColl->variableSetting()->startingPageNumber()-1));
104 resize();
105 }
106 // But we don't want to keep a width of -1 ...
107 if ( width == -1 )
108 width = 0;
109}
110
111KPrStatisticVariable::KPrStatisticVariable( KoTextDocument *textdoc, int subtype, KoVariableFormat *varFormat,KoVariableCollection *_varColl, KPrDocument *doc )
112 : KoStatisticVariable( textdoc, subtype, varFormat, _varColl ),
113 m_doc(doc)
114{
115}
116
117void KPrStatisticVariable::recalc()
118{
119 int nb = 0;
120 ulong charsWithSpace = 0L;
121 ulong charsWithoutSpace = 0L;
122 ulong words = 0L;
123 ulong sentences = 0L;
124 ulong lines = 0L;
125 ulong syllables = 0L;
126 bool frameInfo = ( m_subtype == VST_STATISTIC_NB_WORD ||
127 m_subtype == VST_STATISTIC_NB_SENTENCE ||
128 m_subtype == VST_STATISTIC_NB_LINES ||
129 m_subtype == VST_STATISTIC_NB_CHARACTERE);
130 KPrPage *page = m_doc->activePage();
131 if( !page) //When we load activePage is null
132 return;
133 TQPtrListIterator<KPrObject> objIt( page->objectList() );
134
135 for ( objIt.toFirst(); objIt.current(); ++objIt )
136 {
137 KPrObject *obj = objIt.current();
138 if ( m_subtype == VST_STATISTIC_NB_FRAME )
139 ++nb;
140 else if( m_subtype == VST_STATISTIC_NB_PICTURE && obj->getType() == OT_PICTURE)
141 {
142 ++nb;
143 }
144 else if( m_subtype == VST_STATISTIC_NB_EMBEDDED && obj->getType() == OT_PART )
145 {
146 ++nb;
147 }
148 if ( frameInfo )
149 {
150 KPrTextObject *textObj = dynamic_cast<KPrTextObject *>( obj );
151 if ( textObj )
152 textObj->textObject()->statistics( 0L, charsWithSpace, charsWithoutSpace, words, sentences, syllables, lines, false );
153 }
154 }
155 if ( frameInfo )
156 {
157 if( m_subtype == VST_STATISTIC_NB_WORD )
158 {
159 nb = words;
160 }
161 else if( m_subtype == VST_STATISTIC_NB_SENTENCE )
162 {
163 nb = sentences;
164 }
165 else if( m_subtype == VST_STATISTIC_NB_LINES )
166 {
167 nb = lines;
168 }
169 else if ( m_subtype == VST_STATISTIC_NB_CHARACTERE )
170 {
171 nb = charsWithSpace;
172 }
173 else if ( m_subtype ==VST_STATISTIC_NB_NON_WHITESPACE_CHARACTERE )
174 {
175 nb = charsWithoutSpace;
176 }
177 else if ( m_subtype ==VST_STATISTIC_NB_SYLLABLE )
178 {
179 nb = syllables;
180 }
181 else
182 nb = 0;
183 }
184
185 m_varValue = TQVariant(nb);
186 resize();
187 if ( width == -1 )
188 width = 0;
189}
190
191TQString KPrStatisticVariable::text(bool realValue)
192{
193 if (m_varColl->variableSetting()->displayFieldCode()&& !realValue)
194 return fieldCode();
195 else
196 return m_varFormat->convert( m_varValue );
197}
198
199
"current page number" and "number of pages" variables