summaryrefslogtreecommitdiffstats
path: root/kpresenter/KPrVariableCollection.cpp
blob: a87647e6e0edca594a91f9494303e9c6d088d8c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/* This file is part of the KDE project
   Copyright (C) 2001 Laurent MONTEL <[email protected]>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#include "KPrVariableCollection.h"
#include <KoVariable.h>
#include "KPrDocument.h"
#include "KPrCommand.h"
#include <tdelocale.h>
#include <kdebug.h>
#include <KoXmlNS.h>
#include <KoDom.h>
#include "KPrTextObject.h"
#include "KPrPage.h"

KPrVariableCollection::KPrVariableCollection(KoVariableSettings *_setting, KoVariableFormatCollection* coll)
    : KoVariableCollection(_setting, coll)
{
}

KoVariable* KPrVariableCollection::loadOasisField( KoTextDocument* textdoc, const TQDomElement& tag, KoOasisContext& context )
{
    const TQString localName( tag.localName() );
    const bool isTextNS = tag.namespaceURI() == KoXmlNS::text;
    if ( isTextNS )
    {
        if ( localName == "object-count"  ||
                  localName == "picture-count"  ||
                  localName == "paragraph-count"  ||
                  localName == "word-count"  ||
                  localName == "character-count" ||
                  localName == "sentence-count" ||
                  localName == "line-count" ||
                  localName == "frame-count"  ||
                  localName == "non-whitespace-character-count" ||
                  localName == "syllable-count" )
        {
            TQString key = "NUMBER";
            int type = VT_STATISTIC;
            return loadOasisFieldCreateVariable( textdoc, tag, context, key, type );
        }
        else
            return KoVariableCollection::loadOasisField( textdoc, tag, context );
    }
    else
        return KoVariableCollection::loadOasisField( textdoc, tag, context );
}

KoVariable *KPrVariableCollection::createVariable( int type, short int subtype, KoVariableFormatCollection * coll,
                                                   KoVariableFormat *varFormat,KoTextDocument *textdoc,
                                                   KoDocument * doc,  int _correct, bool _forceDefaultFormat, bool /*loadFootNote*/ )
{
    KPrDocument*m_doc=static_cast<KPrDocument*>(doc);
    KoVariable * var = 0L;
    switch(type) {
    case VT_PGNUM:
    {
        kdDebug(33001)<<" subtype == KoPageVariable::VST_CURRENT_SECTION :"<<(subtype == KPrPgNumVariable::VST_CURRENT_SECTION)<<endl;
        kdDebug(33001)<<" varFormat :"<<varFormat<<endl;
        if ( !varFormat )
            varFormat = (subtype == KPrPgNumVariable::VST_CURRENT_SECTION) ? coll->format("STRING") : coll->format("NUMBER");
        var = new KPrPgNumVariable( textdoc,subtype, varFormat,this,m_doc  );
        break;
    }
    case VT_STATISTIC:
        if ( !varFormat )
            varFormat = coll->format("NUMBER");
        var = new KPrStatisticVariable( textdoc, subtype, varFormat, this, m_doc );
        break;
    default:
        return KoVariableCollection::createVariable( type, subtype, coll, varFormat, textdoc,
                                                     doc, _correct, _forceDefaultFormat);
    }
    return var;
}


KPrPgNumVariable::KPrPgNumVariable( KoTextDocument *textdoc, short int subtype, KoVariableFormat *varFormat,
                                    KoVariableCollection *_varColl, KPrDocument *doc  )
    : KoPageVariable( textdoc, subtype, varFormat ,_varColl ),m_doc(doc)
{
}

void KPrPgNumVariable::recalc()
{
    if ( m_subtype == VST_PGNUM_TOTAL )
    {
        m_varValue = TQVariant( (int)(m_doc->getPageNums()+m_varColl->variableSetting()->startingPageNumber()-1));
        resize();
    }
    // But we don't want to keep a width of -1 ...
    if ( width == -1 )
        width = 0;
}

KPrStatisticVariable::KPrStatisticVariable( KoTextDocument *textdoc,  int subtype, KoVariableFormat *varFormat,KoVariableCollection *_varColl, KPrDocument *doc )
    : KoStatisticVariable( textdoc, subtype, varFormat, _varColl ),
      m_doc(doc)
{
}

void KPrStatisticVariable::recalc()
{
    int nb = 0;
    ulong charsWithSpace = 0L;
    ulong charsWithoutSpace = 0L;
    ulong words = 0L;
    ulong sentences = 0L;
    ulong lines = 0L;
    ulong syllables = 0L;
    bool frameInfo = ( m_subtype == VST_STATISTIC_NB_WORD ||
                       m_subtype == VST_STATISTIC_NB_SENTENCE ||
                       m_subtype == VST_STATISTIC_NB_LINES ||
                       m_subtype == VST_STATISTIC_NB_CHARACTERE);
    KPrPage *page = m_doc->activePage();
    if( !page) //When we load activePage is null
	    return;
    TQPtrListIterator<KPrObject> objIt( page->objectList() );

    for ( objIt.toFirst(); objIt.current(); ++objIt )
    {
        KPrObject *obj = objIt.current();
        if ( m_subtype == VST_STATISTIC_NB_FRAME )
            ++nb;
        else if( m_subtype == VST_STATISTIC_NB_PICTURE && obj->getType() == OT_PICTURE)
        {
            ++nb;
        }
        else if( m_subtype == VST_STATISTIC_NB_EMBEDDED && obj->getType() == OT_PART )
        {
            ++nb;
        }
        if ( frameInfo )
        {
            KPrTextObject *textObj = dynamic_cast<KPrTextObject *>( obj );
            if ( textObj )
                textObj->textObject()->statistics( 0L, charsWithSpace, charsWithoutSpace, words, sentences, syllables, lines, false );
        }
    }
    if ( frameInfo )
    {
        if( m_subtype == VST_STATISTIC_NB_WORD )
        {
            nb = words;
        }
        else if( m_subtype == VST_STATISTIC_NB_SENTENCE )
        {
            nb = sentences;
        }
        else if( m_subtype == VST_STATISTIC_NB_LINES )
        {
                nb = lines;
        }
        else if ( m_subtype == VST_STATISTIC_NB_CHARACTERE )
        {
            nb = charsWithSpace;
        }
        else if ( m_subtype ==VST_STATISTIC_NB_NON_WHITESPACE_CHARACTERE )
        {
            nb = charsWithoutSpace;
        }
        else if ( m_subtype ==VST_STATISTIC_NB_SYLLABLE )
        {
            nb = syllables;
        }
        else
            nb = 0;
    }

    m_varValue = TQVariant(nb);
    resize();
    if ( width == -1 )
        width = 0;
}

TQString KPrStatisticVariable::text(bool realValue)
{
    if (m_varColl->variableSetting()->displayFieldCode()&& !realValue)
        return fieldCode();
    else
        return m_varFormat->convert( m_varValue );
}