kword

KWStatisticsDialog.cpp
1/* This file is part of the KOffice project
2 * Copyright (C) 2005 Thomas Zander <zander@kde.org>
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; version 2.
7
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Library General Public License for more details.
12 *
13 * You should have received a copy of the GNU Library General Public License
14 * along with this library; see the file COPYING.LIB. If not, write to
15 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 * Boston, MA 02110-1301, USA.
17 */
18
19#include "KWStatisticsDialog.h"
20#include "KWDocument.h"
21#include "KWFrameSet.h"
22#include <tdelocale.h>
23#include <tqtabwidget.h>
24#include <tqvbox.h>
25#include <tqlabel.h>
26#include <tqlayout.h>
27#include <tqprogressdialog.h>
28#include <tqcheckbox.h>
29
30KWStatisticsDialog::KWStatisticsDialog( TQWidget *parent, KWDocument *document )
31 : KDialogBase(parent, "statistics", true, i18n("Statistics"),KDialogBase::Ok, KDialogBase::Ok, false )
32{
33 TQWidget *page = new TQWidget( this );
34 setMainWidget(page);
35 TQVBoxLayout *topLayout = new TQVBoxLayout( page, 0, KDialog::spacingHint() );
36
37 TQTabWidget *tab = new TQTabWidget( page );
38 TQFrame *pageAll = 0;
39 TQFrame *pageGeneral = 0;
40 TQFrame *pageSelected = 0;
41 for (int i=0; i < 7; ++i) {
42 m_resultLabelAll[i] = 0;
43 m_resultLabelSelected[i] = 0;
44 if ( i < 6 )
45 m_resultGeneralLabel[i]=0;
46 }
47 m_doc = document;
48 m_parent = parent;
49 m_canceled = true;
50
51
52 // add Tab "General"
53 pageGeneral = new TQFrame( this );
54 tab->addTab( pageGeneral, i18n( "General" ) );
55
56 addBoxGeneral( pageGeneral, m_resultGeneralLabel );
57 calcGeneral( m_resultGeneralLabel );
58
59 // add Tab "All"
60 pageAll = new TQFrame( this );
61 tab->addTab( pageAll, i18n( "Text" ) );
62
63 addBox( pageAll, m_resultLabelAll, true );
64
65 m_canceled = true;
66 pageSelected = new TQFrame( this );
67 tab->addTab( pageSelected, i18n( "Selected Text" ) );
68 // let's see if there's selected text
69 bool b = docHasSelection();
70 tab->setTabEnabled(pageSelected, b);
71 if ( b ) {
72 addBox( pageSelected, m_resultLabelSelected, false);
73 // assign results to 'selected' tab.
74 if ( !calcStats( m_resultLabelSelected, true,true ) )
75 return;
76 if ( !calcStats( m_resultLabelAll, false,false ) )
77 return;
78 showPage( 2 );
79 } else {
80 // assign results
81 if ( !calcStats( m_resultLabelAll, false, false ) )
82 return;
83 showPage( 1 );
84 }
85 topLayout->addWidget( tab );
86 m_canceled = false;
87
88}
89
90void KWStatisticsDialog::slotRefreshValue(bool state)
91{
92 m_canceled = true;
93 // let's see if there's selected text
94 bool b = docHasSelection();
95 if ( b )
96 {
97 if ( !calcStats( m_resultLabelSelected, true, true ) )
98 return;
99 if ( !calcStats( m_resultLabelAll, false, state ) )
100 return;
101 }
102 else
103 {
104 // assign results
105 if ( !calcStats( m_resultLabelAll, false, state ) )
106 return;
107 }
108 m_canceled = false;
109}
110
111void KWStatisticsDialog::calcGeneral( TQLabel **resultLabel )
112{
113 TDELocale *locale = TDEGlobal::locale();
114
115 resultLabel[0]->setText( locale->formatNumber( m_doc->pageCount(), 0) );
116 int table =0;
117 int picture = 0;
118 int part = 0;
119 int nbFrameset = 0;
120 int nbFormula = 0;
121 TQPtrListIterator<KWFrameSet> framesetIt( m_doc->framesetsIterator() );
122 for ( framesetIt.toFirst(); framesetIt.current(); ++framesetIt ) {
123 KWFrameSet *frameSet = framesetIt.current();
124 if ( frameSet && frameSet->isVisible())
125 {
126 if ( frameSet->type() == FT_TABLE)
127 table++;
128 else if ( frameSet->type() == FT_PICTURE)
129 picture++;
130 else if ( frameSet->type() == FT_PART )
131 part++;
132 else if ( frameSet->type() == FT_FORMULA )
133 nbFormula++;
134 nbFrameset++;
135 }
136 }
137
138 resultLabel[1]->setText( locale->formatNumber( nbFrameset, 0 ) );
139 resultLabel[2]->setText( locale->formatNumber( picture, 0 ) );
140 resultLabel[3]->setText( locale->formatNumber( table, 0 ) );
141 resultLabel[4]->setText( locale->formatNumber( part, 0 ) );
142 resultLabel[5]->setText( locale->formatNumber( nbFormula, 0 ) );
143}
144
145bool KWStatisticsDialog::calcStats( TQLabel **resultLabel, bool selection, bool useFootEndNote )
146{
147 ulong charsWithSpace = 0L;
148 ulong charsWithoutSpace = 0L;
149 ulong words = 0L;
150 ulong sentences = 0L;
151 ulong lines = 0L;
152 ulong syllables = 0L;
153
154 // safety check result labels
155 for (int i=0; i < 7; ++i) {
156 if ( !resultLabel[i] ) {
157 kdDebug() << "Warning: KWStatisticsDiaolog::calcStats result table not initialized." << endl;
158 return false;
159 }
160 }
161
162 // count paragraphs for progress dialog:
163 ulong paragraphs = 0L;
164 TQPtrListIterator<KWFrameSet> framesetIt( m_doc->framesetsIterator() );
165 for ( framesetIt.toFirst(); framesetIt.current(); ++framesetIt ) {
166 KWFrameSet *frameSet = framesetIt.current();
167 if ( (frameSet->frameSetInfo() == KWFrameSet::FI_FOOTNOTE || frameSet->frameSetInfo() == KWFrameSet::FI_BODY) && frameSet->isVisible() )
168 {
169 if ( (useFootEndNote && frameSet->frameSetInfo() == KWFrameSet::FI_FOOTNOTE) ||
170 frameSet->frameSetInfo() == KWFrameSet::FI_BODY ) {
171 paragraphs += frameSet->paragraphs();
172 }
173 }
174 }
175 TQProgressDialog progress( i18n( "Counting..." ), i18n( "Cancel" ), paragraphs, this, "count", true );
176 progress.setMinimumDuration( 1000 );
177 progress.setProgress( 0 );
178
179 // do the actual counting
180 for ( framesetIt.toFirst(); framesetIt.current(); ++framesetIt ) {
181 KWFrameSet *frameSet = framesetIt.current();
182 // Exclude headers and footers
183 if ( (frameSet->frameSetInfo() == KWFrameSet::FI_FOOTNOTE || frameSet->frameSetInfo() == KWFrameSet::FI_BODY) && frameSet->isVisible() ) {
184 if ( (useFootEndNote && frameSet->frameSetInfo() == KWFrameSet::FI_FOOTNOTE) || frameSet->frameSetInfo() == KWFrameSet::FI_BODY )
185 {
186
187 if( ! frameSet->statistics( &progress, charsWithSpace, charsWithoutSpace,
188 words, sentences, syllables, lines, selection ) ) {
189 // someone pressed "Cancel"
190 return false;
191 }
192 }
193 }
194 }
195
196 // assign results
197 TDELocale *locale = TDEGlobal::locale();
198 resultLabel[0]->setText( locale->formatNumber( charsWithSpace, 0) );
199 resultLabel[1]->setText( locale->formatNumber( charsWithoutSpace, 0 ) );
200 resultLabel[2]->setText( locale->formatNumber( syllables, 0 ) );
201 resultLabel[3]->setText( locale->formatNumber( words, 0 ) );
202 resultLabel[4]->setText( locale->formatNumber( sentences, 0 ) );
203 resultLabel[5]->setText( locale->formatNumber( lines, 0 ) );
204 // add flesch
205 double f = calcFlesch( sentences, words, syllables );
206 TQString flesch = locale->formatNumber( f , 1 );
207 if( words < 200 ) {
208 // a kind of warning if too few words:
209 flesch = i18n("approximately %1").arg( flesch );
210 }
211 resultLabel[6]->setText( flesch );
212 return true;
213}
214
215double KWStatisticsDialog::calcFlesch( ulong sentences, ulong words, ulong syllables )
216{
217 // calculate Flesch reading ease score:
218 float flesch_score = 0;
219 if( words > 0 && sentences > 0 )
220 flesch_score = 206.835 - (1.015 * (words / sentences)) - (84.6 * syllables / words);
221 return flesch_score;
222}
223
224void KWStatisticsDialog::addBoxGeneral( TQFrame *page, TQLabel **resultLabel )
225{
226 // Layout Managers
227 TQVBoxLayout *topLayout = new TQVBoxLayout( page, 0, 7 );
228 TQGroupBox *box = new TQGroupBox( i18n( "Statistics" ), page );
229 TQGridLayout *grid = new TQGridLayout( box, 9, 3, KDialog::marginHint(), KDialog::spacingHint() );
230 grid->setRowStretch (9, 1);
231 // margins
232 int fHeight = box->fontMetrics().height();
233 grid->setMargin( fHeight );
234 grid->addColSpacing( 1, fHeight );
235 grid->addRowSpacing( 0, fHeight );
236
237 // insert labels
238 TQLabel *label1 = new TQLabel( i18n( "Number of pages:" ), box );
239 grid->addWidget( label1, 1, 0, 1 );
240 resultLabel[0] = new TQLabel( "", box );
241 grid->addWidget( resultLabel[0], 1, 2, 2 );
242
243 TQLabel *label2 = new TQLabel( i18n( "Number of frames:" ), box );
244 grid->addWidget( label2, 2, 0, 1 );
245 resultLabel[1] = new TQLabel( "", box );
246 grid->addWidget( resultLabel[1], 2, 2, 2 );
247
248 TQLabel *label3 = new TQLabel( i18n( "Number of pictures:" ), box );
249 grid->addWidget( label3, 3, 0, 1 );
250 resultLabel[2] = new TQLabel( "", box );
251 grid->addWidget( resultLabel[2], 3, 2, 2 );
252
253
254 TQLabel *label4 = new TQLabel( i18n( "Number of tables:" ), box );
255 grid->addWidget( label4, 4, 0, 1 );
256 resultLabel[3] = new TQLabel( "", box );
257 grid->addWidget( resultLabel[3], 4, 2, 2 );
258
259 TQLabel *label5 = new TQLabel( i18n( "Number of embedded objects:" ), box );
260 grid->addWidget( label5, 5, 0, 1 );
261 resultLabel[4] = new TQLabel( "", box );
262 grid->addWidget( resultLabel[4], 5, 2, 2 );
263
264 TQLabel *label6 = new TQLabel( i18n( "Number of formula frameset:" ), box );
265 grid->addWidget( label6, 6, 0, 1 );
266 resultLabel[5] = new TQLabel( "", box );
267 grid->addWidget( resultLabel[5], 6, 2, 2 );
268
269 topLayout->addWidget( box );
270}
271
272void KWStatisticsDialog::addBox( TQFrame *page, TQLabel **resultLabel, bool calcWithFootNoteCheckbox )
273{
274 // Layout Managers
275 TQVBoxLayout *topLayout = new TQVBoxLayout( page, 0, 7 );
276 if ( calcWithFootNoteCheckbox )
277 {
278 TQWidget *w = new TQWidget(page);
279 topLayout->addWidget( w );
280 TQVBoxLayout *noteLayout = new TQVBoxLayout( w, KDialog::marginHint(), 0 );
281 TQCheckBox *calcWithFootNote = new TQCheckBox( i18n("&Include text from foot- and endnotes"), w);
282 noteLayout->addWidget( calcWithFootNote );
283 connect( calcWithFootNote, TQ_SIGNAL(toggled ( bool )), this, TQ_SLOT( slotRefreshValue(bool)));
284 }
285
286
287 TQGroupBox *box = new TQGroupBox( i18n( "Statistics" ), page );
288 TQGridLayout *grid = new TQGridLayout( box, 9, 3, KDialog::marginHint(), KDialog::spacingHint() );
289 grid->setRowStretch (9, 1);
290
291 // margins
292 int fHeight = box->fontMetrics().height();
293 grid->setMargin( fHeight );
294 grid->addColSpacing( 1, fHeight );
295 grid->addRowSpacing( 0, fHeight );
296
297 //maximum size for result column (don't know how to do this better..)
298 TQString init = i18n("approximately %1").arg( "00000000" );
299
300 // insert labels
301 TQLabel *label1 = new TQLabel( i18n( "Characters including spaces:" ), box );
302 grid->addWidget( label1, 1, 0, 1 );
303 resultLabel[0] = new TQLabel( "", box );
304 grid->addWidget( resultLabel[0], 1, 2, 2 );
305
306 TQLabel *label2 = new TQLabel( i18n( "Characters without spaces:" ), box );
307 grid->addWidget( label2, 2, 0, 1 );
308 resultLabel[1] = new TQLabel( "", box );
309 grid->addWidget( resultLabel[1], 2, 2, 2 );
310
311 TQLabel *label3 = new TQLabel( i18n( "Syllables:" ), box );
312 grid->addWidget( label3, 3, 0, 1 );
313 resultLabel[2] = new TQLabel( "", box );
314 grid->addWidget( resultLabel[2], 3, 2, 2 );
315
316 TQLabel *label4 = new TQLabel( i18n( "Words:" ), box );
317 grid->addWidget( label4, 4, 0, 1 );
318 resultLabel[3] = new TQLabel( "", box );
319 grid->addWidget( resultLabel[3], 4, 2, 2 );
320
321 TQLabel *label5 = new TQLabel( i18n( "Sentences:" ), box );
322 grid->addWidget( label5, 5, 0, 1 );
323 resultLabel[4] = new TQLabel( "", box );
324 grid->addWidget( resultLabel[4], 5, 2, 2 );
325
326 TQLabel *label6 = new TQLabel( i18n( "Lines:" ), box );
327 grid->addWidget( label6, 6, 0, 1 );
328 resultLabel[5] = new TQLabel( "", box );
329 grid->addWidget( resultLabel[5], 6, 2, 2 );
330
331
332 TQLabel *label7 = new TQLabel( i18n( "Flesch reading ease:" ), box );
333 grid->addWidget( label7, 7, 0, 1 );
334 resultLabel[6] = new TQLabel( init, box );
335 grid->addWidget( resultLabel[6], 7, 2, 2 );
336
337 topLayout->addWidget( box );
338}
339
340bool KWStatisticsDialog::docHasSelection()const
341{
342 TQPtrListIterator<KWFrameSet> fsIt( m_doc->framesetsIterator() );
343
344 for ( ; fsIt.current(); ++fsIt ) {
345 KWFrameSet *fs = fsIt.current();
346 if ( fs->paragraphsSelected() ) {
347 return true;
348 }
349 }
350 return false;
351}
352
353#include "KWStatisticsDialog.moc"
354
virtual FrameSetType type() const
The type of frameset.
Definition KWFrameSet.h:76
Info frameSetInfo() const
Returns the type of TextFrameSet this is.
Definition KWFrameSet.h:95
bool isVisible(KWViewMode *viewMode=0L) const
Returns true if the frameset is visible.