kword

KWStatisticsDialog.cpp

00001 /* This file is part of the KOffice project
00002  * Copyright (C) 2005 Thomas Zander <zander@kde.org>
00003  *
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Library General Public
00006  * License as published by the Free Software Foundation; version 2.
00007 
00008  * This library is distributed in the hope that it will be useful,
00009  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011  * Library General Public License for more details.
00012  *
00013  * You should have received a copy of the GNU Library General Public License
00014  * along with this library; see the file COPYING.LIB.  If not, write to
00015  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00016  * Boston, MA 02110-1301, USA.
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     // add Tab "General"
00053     pageGeneral = new QFrame( this );
00054     tab->addTab( pageGeneral,  i18n( "General" ) );
00055 
00056     addBoxGeneral( pageGeneral, m_resultGeneralLabel );
00057     calcGeneral( m_resultGeneralLabel );
00058 
00059     // add Tab "All"
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     // let's see if there's selected text
00069     bool b = docHasSelection();
00070     tab->setTabEnabled(pageSelected, b);
00071     if ( b ) {
00072         addBox( pageSelected, m_resultLabelSelected,  false);
00073         // assign results to 'selected' tab.
00074         if ( !calcStats( m_resultLabelSelected, true,true ) )
00075             return;
00076         if ( !calcStats( m_resultLabelAll, false,false ) )
00077             return;
00078         showPage( 2 );
00079     } else {
00080         // assign results
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     // let's see if there's selected text
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         // assign results
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     // safety check result labels
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     // count paragraphs for progress dialog:
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) ||
00170                     frameSet->frameSetInfo() == KWFrameSet::FI_BODY ) {
00171                 paragraphs += frameSet->paragraphs();
00172             }
00173         }
00174     }
00175     QProgressDialog progress( i18n( "Counting..." ), i18n( "Cancel" ), paragraphs, this, "count", true );
00176     progress.setMinimumDuration( 1000 );
00177     progress.setProgress( 0 );
00178 
00179     // do the actual counting
00180     for ( framesetIt.toFirst(); framesetIt.current(); ++framesetIt ) {
00181         KWFrameSet *frameSet = framesetIt.current();
00182         // Exclude headers and footers
00183         if ( (frameSet->frameSetInfo() == KWFrameSet::FI_FOOTNOTE || frameSet->frameSetInfo() == KWFrameSet::FI_BODY) && frameSet->isVisible() ) {
00184             if ( (useFootEndNote && frameSet->frameSetInfo() == KWFrameSet::FI_FOOTNOTE) || frameSet->frameSetInfo() == KWFrameSet::FI_BODY )
00185             {
00186 
00187                 if( ! frameSet->statistics( &progress, charsWithSpace, charsWithoutSpace,
00188                                             words, sentences, syllables, lines, selection ) ) {
00189                     // someone pressed "Cancel"
00190                     return false;
00191                 }
00192             }
00193         }
00194     }
00195 
00196     // assign results
00197     KLocale *locale = KGlobal::locale();
00198     resultLabel[0]->setText( locale->formatNumber( charsWithSpace, 0) );
00199     resultLabel[1]->setText( locale->formatNumber( charsWithoutSpace, 0 ) );
00200     resultLabel[2]->setText( locale->formatNumber( syllables, 0 ) );
00201     resultLabel[3]->setText( locale->formatNumber( words, 0 ) );
00202     resultLabel[4]->setText( locale->formatNumber( sentences, 0 ) );
00203     resultLabel[5]->setText( locale->formatNumber( lines, 0 ) );
00204     // add flesch
00205     double f = calcFlesch( sentences, words, syllables );
00206     QString flesch = locale->formatNumber( f , 1 );
00207     if( words < 200 ) {
00208         // a kind of warning if too few words:
00209         flesch = i18n("approximately %1").arg( flesch );
00210     }
00211     resultLabel[6]->setText( flesch );
00212     return true;
00213 }
00214 
00215 double KWStatisticsDialog::calcFlesch( ulong sentences, ulong words, ulong syllables )
00216 {
00217     // calculate Flesch reading ease score:
00218     float flesch_score = 0;
00219     if( words > 0 && sentences > 0 )
00220         flesch_score = 206.835 - (1.015 * (words / sentences)) - (84.6 * syllables / words);
00221     return flesch_score;
00222 }
00223 
00224 void KWStatisticsDialog::addBoxGeneral( QFrame *page, QLabel **resultLabel )
00225 {
00226     // Layout Managers
00227     QVBoxLayout *topLayout = new QVBoxLayout( page, 0, 7 );
00228     QGroupBox *box = new QGroupBox( i18n( "Statistics" ), page );
00229     QGridLayout *grid = new QGridLayout( box, 9, 3, KDialog::marginHint(), KDialog::spacingHint() );
00230     grid->setRowStretch (9, 1);
00231     // margins
00232     int fHeight = box->fontMetrics().height();
00233     grid->setMargin( fHeight );
00234     grid->addColSpacing( 1, fHeight );
00235     grid->addRowSpacing( 0, fHeight );
00236 
00237     // insert labels
00238     QLabel *label1 = new QLabel( i18n( "Number of pages:" ), box );
00239     grid->addWidget( label1, 1, 0, 1 );
00240     resultLabel[0] = new QLabel( "", box );
00241     grid->addWidget( resultLabel[0], 1, 2, 2 );
00242 
00243     QLabel *label2 = new QLabel( i18n( "Number of frames:" ), box );
00244     grid->addWidget( label2, 2, 0, 1 );
00245     resultLabel[1] = new QLabel( "", box );
00246     grid->addWidget( resultLabel[1], 2, 2, 2 );
00247 
00248     QLabel *label3 = new QLabel( i18n( "Number of pictures:" ), box );
00249     grid->addWidget( label3, 3, 0, 1 );
00250     resultLabel[2] = new QLabel( "", box );
00251     grid->addWidget( resultLabel[2], 3, 2, 2 );
00252 
00253 
00254     QLabel *label4 = new QLabel( i18n( "Number of tables:" ), box );
00255     grid->addWidget( label4, 4, 0, 1 );
00256     resultLabel[3] = new QLabel( "", box );
00257     grid->addWidget( resultLabel[3], 4, 2, 2 );
00258 
00259     QLabel *label5 = new QLabel( i18n( "Number of embedded objects:" ), box );
00260     grid->addWidget( label5, 5, 0, 1 );
00261     resultLabel[4] = new QLabel( "", box );
00262     grid->addWidget( resultLabel[4], 5, 2, 2 );
00263 
00264     QLabel *label6 = new QLabel( i18n( "Number of formula frameset:" ), box );
00265     grid->addWidget( label6, 6, 0, 1 );
00266     resultLabel[5] = new QLabel( "", box );
00267     grid->addWidget( resultLabel[5], 6, 2, 2 );
00268 
00269     topLayout->addWidget( box );
00270 }
00271 
00272 void KWStatisticsDialog::addBox( QFrame *page, QLabel **resultLabel, bool calcWithFootNoteCheckbox )
00273 {
00274     // Layout Managers
00275     QVBoxLayout *topLayout = new QVBoxLayout( page, 0, 7 );
00276     if ( calcWithFootNoteCheckbox )
00277     {
00278         QWidget *w = new QWidget(page);
00279         topLayout->addWidget( w );
00280         QVBoxLayout *noteLayout = new QVBoxLayout( w, KDialog::marginHint(), 0 );
00281         QCheckBox *calcWithFootNote = new QCheckBox( i18n("&Include text from foot- and endnotes"), w);
00282         noteLayout->addWidget( calcWithFootNote );
00283         connect( calcWithFootNote, SIGNAL(toggled ( bool )), this, SLOT( slotRefreshValue(bool)));
00284     }
00285 
00286 
00287     QGroupBox *box = new QGroupBox( i18n( "Statistics" ), page );
00288     QGridLayout *grid = new QGridLayout( box, 9, 3, KDialog::marginHint(), KDialog::spacingHint() );
00289     grid->setRowStretch (9, 1);
00290 
00291     // margins
00292     int fHeight = box->fontMetrics().height();
00293     grid->setMargin( fHeight );
00294     grid->addColSpacing( 1, fHeight );
00295     grid->addRowSpacing( 0, fHeight );
00296 
00297     //maximum size for result column (don't know how to do this better..)
00298     QString init = i18n("approximately %1").arg( "00000000" );
00299 
00300     // insert labels
00301     QLabel *label1 = new QLabel( i18n( "Characters including spaces:" ), box );
00302     grid->addWidget( label1, 1, 0, 1 );
00303     resultLabel[0] = new QLabel( "", box );
00304     grid->addWidget( resultLabel[0], 1, 2, 2 );
00305 
00306     QLabel *label2 = new QLabel( i18n( "Characters without spaces:" ), box );
00307     grid->addWidget( label2, 2, 0, 1 );
00308     resultLabel[1] = new QLabel( "", box );
00309     grid->addWidget( resultLabel[1], 2, 2, 2 );
00310 
00311     QLabel *label3 = new QLabel( i18n( "Syllables:" ), box );
00312     grid->addWidget( label3, 3, 0, 1 );
00313     resultLabel[2] = new QLabel( "", box );
00314     grid->addWidget( resultLabel[2], 3, 2, 2 );
00315 
00316     QLabel *label4 = new QLabel( i18n( "Words:" ), box );
00317     grid->addWidget( label4, 4, 0, 1 );
00318     resultLabel[3] = new QLabel( "", box );
00319     grid->addWidget( resultLabel[3], 4, 2, 2 );
00320 
00321     QLabel *label5 = new QLabel( i18n( "Sentences:" ), box );
00322     grid->addWidget( label5, 5, 0, 1 );
00323     resultLabel[4] = new QLabel( "", box );
00324     grid->addWidget( resultLabel[4], 5, 2, 2 );
00325 
00326     QLabel *label6 = new QLabel( i18n( "Lines:" ), box );
00327     grid->addWidget( label6, 6, 0, 1 );
00328     resultLabel[5] = new QLabel( "", box );
00329     grid->addWidget( resultLabel[5], 6, 2, 2 );
00330 
00331 
00332     QLabel *label7 = new QLabel( i18n( "Flesch reading ease:" ), box );
00333     grid->addWidget( label7, 7, 0, 1 );
00334     resultLabel[6] = new QLabel( init, box );
00335     grid->addWidget( resultLabel[6], 7, 2, 2 );
00336 
00337     topLayout->addWidget( box );
00338 }
00339 
00340 bool KWStatisticsDialog::docHasSelection()const
00341 {
00342     QPtrListIterator<KWFrameSet> fsIt( m_doc->framesetsIterator() );
00343 
00344     for ( ; fsIt.current(); ++fsIt ) {
00345         KWFrameSet *fs = fsIt.current();
00346         if ( fs->paragraphsSelected() ) {
00347             return true;
00348         }
00349     }
00350     return false;
00351 }
00352 
00353 #include "KWStatisticsDialog.moc"
00354 
KDE Home | KDE Accessibility Home | Description of Access Keys