00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "KWStatisticsDialog.h"
00020 #include "KWDocument.h"
00021 #include "KWFrameSet.h"
00022 #include <klocale.h>
00023 #include <qtabwidget.h>
00024 #include <qvbox.h>
00025 #include <qlabel.h>
00026 #include <qlayout.h>
00027 #include <qprogressdialog.h>
00028 #include <qcheckbox.h>
00029
00030 KWStatisticsDialog::KWStatisticsDialog( QWidget *parent, KWDocument *document )
00031 : KDialogBase(parent, "statistics", true, i18n("Statistics"),KDialogBase::Ok, KDialogBase::Ok, false )
00032 {
00033 QWidget *page = new QWidget( this );
00034 setMainWidget(page);
00035 QVBoxLayout *topLayout = new QVBoxLayout( page, 0, KDialog::spacingHint() );
00036
00037 QTabWidget *tab = new QTabWidget( page );
00038 QFrame *pageAll = 0;
00039 QFrame *pageGeneral = 0;
00040 QFrame *pageSelected = 0;
00041 for (int i=0; i < 7; ++i) {
00042 m_resultLabelAll[i] = 0;
00043 m_resultLabelSelected[i] = 0;
00044 if ( i < 6 )
00045 m_resultGeneralLabel[i]=0;
00046 }
00047 m_doc = document;
00048 m_parent = parent;
00049 m_canceled = true;
00050
00051
00052
00053 pageGeneral = new QFrame( this );
00054 tab->addTab( pageGeneral, i18n( "General" ) );
00055
00056 addBoxGeneral( pageGeneral, m_resultGeneralLabel );
00057 calcGeneral( m_resultGeneralLabel );
00058
00059
00060 pageAll = new QFrame( this );
00061 tab->addTab( pageAll, i18n( "Text" ) );
00062
00063 addBox( pageAll, m_resultLabelAll, true );
00064
00065 m_canceled = true;
00066 pageSelected = new QFrame( this );
00067 tab->addTab( pageSelected, i18n( "Selected Text" ) );
00068
00069 bool b = docHasSelection();
00070 tab->setTabEnabled(pageSelected, b);
00071 if ( b ) {
00072 addBox( pageSelected, m_resultLabelSelected, false);
00073
00074 if ( !calcStats( m_resultLabelSelected, true,true ) )
00075 return;
00076 if ( !calcStats( m_resultLabelAll, false,false ) )
00077 return;
00078 showPage( 2 );
00079 } else {
00080
00081 if ( !calcStats( m_resultLabelAll, false, false ) )
00082 return;
00083 showPage( 1 );
00084 }
00085 topLayout->addWidget( tab );
00086 m_canceled = false;
00087
00088 }
00089
00090 void KWStatisticsDialog::slotRefreshValue(bool state)
00091 {
00092 m_canceled = true;
00093
00094 bool b = docHasSelection();
00095 if ( b )
00096 {
00097 if ( !calcStats( m_resultLabelSelected, true, true ) )
00098 return;
00099 if ( !calcStats( m_resultLabelAll, false, state ) )
00100 return;
00101 }
00102 else
00103 {
00104
00105 if ( !calcStats( m_resultLabelAll, false, state ) )
00106 return;
00107 }
00108 m_canceled = false;
00109 }
00110
00111 void KWStatisticsDialog::calcGeneral( QLabel **resultLabel )
00112 {
00113 KLocale *locale = KGlobal::locale();
00114
00115 resultLabel[0]->setText( locale->formatNumber( m_doc->pageCount(), 0) );
00116 int table =0;
00117 int picture = 0;
00118 int part = 0;
00119 int nbFrameset = 0;
00120 int nbFormula = 0;
00121 QPtrListIterator<KWFrameSet> framesetIt( m_doc->framesetsIterator() );
00122 for ( framesetIt.toFirst(); framesetIt.current(); ++framesetIt ) {
00123 KWFrameSet *frameSet = framesetIt.current();
00124 if ( frameSet && frameSet->isVisible())
00125 {
00126 if ( frameSet->type() == FT_TABLE)
00127 table++;
00128 else if ( frameSet->type() == FT_PICTURE)
00129 picture++;
00130 else if ( frameSet->type() == FT_PART )
00131 part++;
00132 else if ( frameSet->type() == FT_FORMULA )
00133 nbFormula++;
00134 nbFrameset++;
00135 }
00136 }
00137
00138 resultLabel[1]->setText( locale->formatNumber( nbFrameset, 0 ) );
00139 resultLabel[2]->setText( locale->formatNumber( picture, 0 ) );
00140 resultLabel[3]->setText( locale->formatNumber( table, 0 ) );
00141 resultLabel[4]->setText( locale->formatNumber( part, 0 ) );
00142 resultLabel[5]->setText( locale->formatNumber( nbFormula, 0 ) );
00143 }
00144
00145 bool KWStatisticsDialog::calcStats( QLabel **resultLabel, bool selection, bool useFootEndNote )
00146 {
00147 ulong charsWithSpace = 0L;
00148 ulong charsWithoutSpace = 0L;
00149 ulong words = 0L;
00150 ulong sentences = 0L;
00151 ulong lines = 0L;
00152 ulong syllables = 0L;
00153
00154
00155 for (int i=0; i < 7; ++i) {
00156 if ( !resultLabel[i] ) {
00157 kdDebug() << "Warning: KWStatisticsDiaolog::calcStats result table not initialized." << endl;
00158 return false;
00159 }
00160 }
00161
00162
00163 ulong paragraphs = 0L;
00164 QPtrListIterator<KWFrameSet> framesetIt( m_doc->framesetsIterator() );
00165 for ( framesetIt.toFirst(); framesetIt.current(); ++framesetIt ) {
00166 KWFrameSet *frameSet = framesetIt.current();
00167 if ( (frameSet->frameSetInfo() == KWFrameSet::FI_FOOTNOTE || frameSet->frameSetInfo() == KWFrameSet::FI_BODY) && frameSet->isVisible() )
00168 {
00169 if ( (useFootEndNote && frameSet->frameSetInfo() == KWFrameSet::FI_FOOTNOTE) || frameSet->frameSetInfo() == KWFrameSet::FI_BODY )
00170 {
00171 if ( selection && false )
00172 paragraphs += frameSet->paragraphsSelected();
00173 else
00174 paragraphs += frameSet->paragraphs();
00175 }
00176
00177 }
00178 }
00179 QProgressDialog progress( i18n( "Counting..." ), i18n( "Cancel" ), paragraphs, this, "count", true );
00180 progress.setMinimumDuration( 1000 );
00181 progress.setProgress( 0 );
00182
00183
00184 for ( framesetIt.toFirst(); framesetIt.current(); ++framesetIt ) {
00185 KWFrameSet *frameSet = framesetIt.current();
00186
00187 if ( (frameSet->frameSetInfo() == KWFrameSet::FI_FOOTNOTE || frameSet->frameSetInfo() == KWFrameSet::FI_BODY) && frameSet->isVisible() ) {
00188 if ( (useFootEndNote && frameSet->frameSetInfo() == KWFrameSet::FI_FOOTNOTE) || frameSet->frameSetInfo() == KWFrameSet::FI_BODY )
00189 {
00190
00191 if( ! frameSet->statistics( &progress, charsWithSpace, charsWithoutSpace,
00192 words, sentences, syllables, lines, selection ) ) {
00193
00194 return false;
00195 }
00196 }
00197 }
00198 }
00199
00200
00201 KLocale *locale = KGlobal::locale();
00202 resultLabel[0]->setText( locale->formatNumber( charsWithSpace, 0) );
00203 resultLabel[1]->setText( locale->formatNumber( charsWithoutSpace, 0 ) );
00204 resultLabel[2]->setText( locale->formatNumber( syllables, 0 ) );
00205 resultLabel[3]->setText( locale->formatNumber( words, 0 ) );
00206 resultLabel[4]->setText( locale->formatNumber( sentences, 0 ) );
00207 resultLabel[5]->setText( locale->formatNumber( lines, 0 ) );
00208
00209 double f = calcFlesch( sentences, words, syllables );
00210 QString flesch = locale->formatNumber( f , 1 );
00211 if( words < 200 ) {
00212
00213 flesch = i18n("approximately %1").arg( flesch );
00214 }
00215 resultLabel[6]->setText( flesch );
00216 return true;
00217 }
00218
00219 double KWStatisticsDialog::calcFlesch( ulong sentences, ulong words, ulong syllables )
00220 {
00221
00222 float flesch_score = 0;
00223 if( words > 0 && sentences > 0 )
00224 flesch_score = 206.835 - (1.015 * (words / sentences)) - (84.6 * syllables / words);
00225 return flesch_score;
00226 }
00227
00228 void KWStatisticsDialog::addBoxGeneral( QFrame *page, QLabel **resultLabel )
00229 {
00230
00231 QVBoxLayout *topLayout = new QVBoxLayout( page, 0, 7 );
00232 QGroupBox *box = new QGroupBox( i18n( "Statistics" ), page );
00233 QGridLayout *grid = new QGridLayout( box, 9, 3, KDialog::marginHint(), KDialog::spacingHint() );
00234 grid->setRowStretch (9, 1);
00235
00236 int fHeight = box->fontMetrics().height();
00237 grid->setMargin( fHeight );
00238 grid->addColSpacing( 1, fHeight );
00239 grid->addRowSpacing( 0, fHeight );
00240
00241
00242 QLabel *label1 = new QLabel( i18n( "Number of pages:" ), box );
00243 grid->addWidget( label1, 1, 0, 1 );
00244 resultLabel[0] = new QLabel( "", box );
00245 grid->addWidget( resultLabel[0], 1, 2, 2 );
00246
00247 QLabel *label2 = new QLabel( i18n( "Number of frames:" ), box );
00248 grid->addWidget( label2, 2, 0, 1 );
00249 resultLabel[1] = new QLabel( "", box );
00250 grid->addWidget( resultLabel[1], 2, 2, 2 );
00251
00252 QLabel *label3 = new QLabel( i18n( "Number of pictures:" ), box );
00253 grid->addWidget( label3, 3, 0, 1 );
00254 resultLabel[2] = new QLabel( "", box );
00255 grid->addWidget( resultLabel[2], 3, 2, 2 );
00256
00257
00258 QLabel *label4 = new QLabel( i18n( "Number of tables:" ), box );
00259 grid->addWidget( label4, 4, 0, 1 );
00260 resultLabel[3] = new QLabel( "", box );
00261 grid->addWidget( resultLabel[3], 4, 2, 2 );
00262
00263 QLabel *label5 = new QLabel( i18n( "Number of embedded objects:" ), box );
00264 grid->addWidget( label5, 5, 0, 1 );
00265 resultLabel[4] = new QLabel( "", box );
00266 grid->addWidget( resultLabel[4], 5, 2, 2 );
00267
00268 QLabel *label6 = new QLabel( i18n( "Number of formula frameset:" ), box );
00269 grid->addWidget( label6, 6, 0, 1 );
00270 resultLabel[5] = new QLabel( "", box );
00271 grid->addWidget( resultLabel[5], 6, 2, 2 );
00272
00273 topLayout->addWidget( box );
00274 }
00275
00276 void KWStatisticsDialog::addBox( QFrame *page, QLabel **resultLabel, bool calcWithFootNoteCheckbox )
00277 {
00278
00279 QVBoxLayout *topLayout = new QVBoxLayout( page, 0, 7 );
00280 if ( calcWithFootNoteCheckbox )
00281 {
00282 QWidget *w = new QWidget(page);
00283 topLayout->addWidget( w );
00284 QVBoxLayout *noteLayout = new QVBoxLayout( w, KDialog::marginHint(), 0 );
00285 QCheckBox *calcWithFootNote = new QCheckBox( i18n("&Include text from foot- and endnotes"), w);
00286 noteLayout->addWidget( calcWithFootNote );
00287 connect( calcWithFootNote, SIGNAL(toggled ( bool )), this, SLOT( slotRefreshValue(bool)));
00288 }
00289
00290
00291 QGroupBox *box = new QGroupBox( i18n( "Statistics" ), page );
00292 QGridLayout *grid = new QGridLayout( box, 9, 3, KDialog::marginHint(), KDialog::spacingHint() );
00293 grid->setRowStretch (9, 1);
00294
00295
00296 int fHeight = box->fontMetrics().height();
00297 grid->setMargin( fHeight );
00298 grid->addColSpacing( 1, fHeight );
00299 grid->addRowSpacing( 0, fHeight );
00300
00301
00302 QString init = i18n("approximately %1").arg( "00000000" );
00303
00304
00305 QLabel *label1 = new QLabel( i18n( "Characters including spaces:" ), box );
00306 grid->addWidget( label1, 1, 0, 1 );
00307 resultLabel[0] = new QLabel( "", box );
00308 grid->addWidget( resultLabel[0], 1, 2, 2 );
00309
00310 QLabel *label2 = new QLabel( i18n( "Characters without spaces:" ), box );
00311 grid->addWidget( label2, 2, 0, 1 );
00312 resultLabel[1] = new QLabel( "", box );
00313 grid->addWidget( resultLabel[1], 2, 2, 2 );
00314
00315 QLabel *label3 = new QLabel( i18n( "Syllables:" ), box );
00316 grid->addWidget( label3, 3, 0, 1 );
00317 resultLabel[2] = new QLabel( "", box );
00318 grid->addWidget( resultLabel[2], 3, 2, 2 );
00319
00320 QLabel *label4 = new QLabel( i18n( "Words:" ), box );
00321 grid->addWidget( label4, 4, 0, 1 );
00322 resultLabel[3] = new QLabel( "", box );
00323 grid->addWidget( resultLabel[3], 4, 2, 2 );
00324
00325 QLabel *label5 = new QLabel( i18n( "Sentences:" ), box );
00326 grid->addWidget( label5, 5, 0, 1 );
00327 resultLabel[4] = new QLabel( "", box );
00328 grid->addWidget( resultLabel[4], 5, 2, 2 );
00329
00330 QLabel *label6 = new QLabel( i18n( "Lines:" ), box );
00331 grid->addWidget( label6, 6, 0, 1 );
00332 resultLabel[5] = new QLabel( "", box );
00333 grid->addWidget( resultLabel[5], 6, 2, 2 );
00334
00335
00336 QLabel *label7 = new QLabel( i18n( "Flesch reading ease:" ), box );
00337 grid->addWidget( label7, 7, 0, 1 );
00338 resultLabel[6] = new QLabel( init, box );
00339 grid->addWidget( resultLabel[6], 7, 2, 2 );
00340
00341 topLayout->addWidget( box );
00342 }
00343
00344 bool KWStatisticsDialog::docHasSelection()const
00345 {
00346 QPtrListIterator<KWFrameSet> fsIt( m_doc->framesetsIterator() );
00347
00348 for ( ; fsIt.current(); ++fsIt ) {
00349 KWFrameSet *fs = fsIt.current();
00350 if ( fs->paragraphsSelected() ) {
00351 return true;
00352 }
00353 }
00354 return false;
00355 }
00356
00357 #include "KWStatisticsDialog.moc"
00358