19#include "KWStatisticsDialog.h"
20#include "KWDocument.h"
21#include "KWFrameSet.h"
23#include <tqtabwidget.h>
27#include <tqprogressdialog.h>
28#include <tqcheckbox.h>
30KWStatisticsDialog::KWStatisticsDialog( TQWidget *parent, KWDocument *document )
31 : KDialogBase(parent,
"statistics", true, i18n(
"Statistics"),KDialogBase::Ok, KDialogBase::Ok, false )
33 TQWidget *page =
new TQWidget(
this );
35 TQVBoxLayout *topLayout =
new TQVBoxLayout( page, 0, KDialog::spacingHint() );
37 TQTabWidget *tab =
new TQTabWidget( page );
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;
45 m_resultGeneralLabel[i]=0;
53 pageGeneral =
new TQFrame(
this );
54 tab->addTab( pageGeneral, i18n(
"General" ) );
56 addBoxGeneral( pageGeneral, m_resultGeneralLabel );
57 calcGeneral( m_resultGeneralLabel );
60 pageAll =
new TQFrame(
this );
61 tab->addTab( pageAll, i18n(
"Text" ) );
63 addBox( pageAll, m_resultLabelAll,
true );
66 pageSelected =
new TQFrame(
this );
67 tab->addTab( pageSelected, i18n(
"Selected Text" ) );
69 bool b = docHasSelection();
70 tab->setTabEnabled(pageSelected, b);
72 addBox( pageSelected, m_resultLabelSelected,
false);
74 if ( !calcStats( m_resultLabelSelected,
true,
true ) )
76 if ( !calcStats( m_resultLabelAll,
false,
false ) )
81 if ( !calcStats( m_resultLabelAll,
false,
false ) )
85 topLayout->addWidget( tab );
90void KWStatisticsDialog::slotRefreshValue(
bool state)
94 bool b = docHasSelection();
97 if ( !calcStats( m_resultLabelSelected,
true,
true ) )
99 if ( !calcStats( m_resultLabelAll,
false, state ) )
105 if ( !calcStats( m_resultLabelAll,
false, state ) )
111void KWStatisticsDialog::calcGeneral( TQLabel **resultLabel )
113 TDELocale *locale = TDEGlobal::locale();
115 resultLabel[0]->setText( locale->formatNumber( m_doc->pageCount(), 0) );
121 TQPtrListIterator<KWFrameSet> framesetIt( m_doc->framesetsIterator() );
122 for ( framesetIt.toFirst(); framesetIt.current(); ++framesetIt ) {
123 KWFrameSet *frameSet = framesetIt.current();
126 if ( frameSet->
type() == FT_TABLE)
128 else if ( frameSet->
type() == FT_PICTURE)
130 else if ( frameSet->
type() == FT_PART )
132 else if ( frameSet->
type() == FT_FORMULA )
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 ) );
145bool KWStatisticsDialog::calcStats( TQLabel **resultLabel,
bool selection,
bool useFootEndNote )
147 ulong charsWithSpace = 0L;
148 ulong charsWithoutSpace = 0L;
150 ulong sentences = 0L;
152 ulong syllables = 0L;
155 for (
int i=0; i < 7; ++i) {
156 if ( !resultLabel[i] ) {
157 kdDebug() <<
"Warning: KWStatisticsDiaolog::calcStats result table not initialized." << endl;
163 ulong paragraphs = 0L;
164 TQPtrListIterator<KWFrameSet> framesetIt( m_doc->framesetsIterator() );
165 for ( framesetIt.toFirst(); framesetIt.current(); ++framesetIt ) {
166 KWFrameSet *frameSet = framesetIt.current();
169 if ( (useFootEndNote && frameSet->
frameSetInfo() == KWFrameSet::FI_FOOTNOTE) ||
171 paragraphs += frameSet->paragraphs();
175 TQProgressDialog progress( i18n(
"Counting..." ), i18n(
"Cancel" ), paragraphs,
this,
"count",
true );
176 progress.setMinimumDuration( 1000 );
177 progress.setProgress( 0 );
180 for ( framesetIt.toFirst(); framesetIt.current(); ++framesetIt ) {
181 KWFrameSet *frameSet = framesetIt.current();
184 if ( (useFootEndNote && frameSet->
frameSetInfo() == KWFrameSet::FI_FOOTNOTE) || frameSet->
frameSetInfo() == KWFrameSet::FI_BODY )
187 if( ! frameSet->statistics( &progress, charsWithSpace, charsWithoutSpace,
188 words, sentences, syllables, lines, selection ) ) {
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 ) );
205 double f = calcFlesch( sentences, words, syllables );
206 TQString flesch = locale->formatNumber( f , 1 );
209 flesch = i18n(
"approximately %1").arg( flesch );
211 resultLabel[6]->setText( flesch );
215double KWStatisticsDialog::calcFlesch( ulong sentences, ulong words, ulong syllables )
218 float flesch_score = 0;
219 if( words > 0 && sentences > 0 )
220 flesch_score = 206.835 - (1.015 * (words / sentences)) - (84.6 * syllables / words);
224void KWStatisticsDialog::addBoxGeneral( TQFrame *page, TQLabel **resultLabel )
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);
232 int fHeight = box->fontMetrics().height();
233 grid->setMargin( fHeight );
234 grid->addColSpacing( 1, fHeight );
235 grid->addRowSpacing( 0, fHeight );
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 );
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 );
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 );
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 );
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 );
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 );
269 topLayout->addWidget( box );
272void KWStatisticsDialog::addBox( TQFrame *page, TQLabel **resultLabel,
bool calcWithFootNoteCheckbox )
275 TQVBoxLayout *topLayout =
new TQVBoxLayout( page, 0, 7 );
276 if ( calcWithFootNoteCheckbox )
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)));
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);
292 int fHeight = box->fontMetrics().height();
293 grid->setMargin( fHeight );
294 grid->addColSpacing( 1, fHeight );
295 grid->addRowSpacing( 0, fHeight );
298 TQString init = i18n(
"approximately %1").arg(
"00000000" );
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 );
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 );
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 );
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 );
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 );
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 );
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 );
337 topLayout->addWidget( box );
340bool KWStatisticsDialog::docHasSelection()
const
342 TQPtrListIterator<KWFrameSet> fsIt( m_doc->framesetsIterator() );
344 for ( ; fsIt.current(); ++fsIt ) {
345 KWFrameSet *fs = fsIt.current();
346 if ( fs->paragraphsSelected() ) {
353#include "KWStatisticsDialog.moc"
virtual FrameSetType type() const
The type of frameset.
Info frameSetInfo() const
Returns the type of TextFrameSet this is.
bool isVisible(KWViewMode *viewMode=0L) const
Returns true if the frameset is visible.