Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

predicttabwidget.cpp

Go to the documentation of this file.
00001 /*
00002                              This file is part of the Opie Project
00003 
00004                              Copyright (C) Opie Team <opie-devel@handhelds.org>
00005               =.
00006             .=l.
00007            .>+-=
00008  _;:,     .>    :=|.         This program is free software; you can
00009 .> <`_,   >  .   <=          redistribute it and/or  modify it under
00010 :`=1 )Y*s>-.--   :           the terms of the GNU Library General Public
00011 .="- .-=="i,     .._         License as published by the Free Software
00012  - .   .-<_>     .<>         Foundation; either version 2 of the License,
00013      ._= =}       :          or (at your option) any later version.
00014     .%`+i>       _;_.
00015     .i_,=:_.      -<s.       This program is distributed in the hope that
00016      +  .  -:.       =       it will be useful,  but WITHOUT ANY WARRANTY;
00017     : ..    .:,     . . .    without even the implied warranty of
00018     =_        +     =;=|`    MERCHANTABILITY or FITNESS FOR A
00019   _.=:.       :    :=>`:     PARTICULAR PURPOSE. See the GNU
00020 ..}^=.=       =       ;      Library General Public License for more
00021 ++=   -.     .`     .:       details.
00022 :     =  ...= . :.=-
00023  -.   .:....=;==+<;          You should have received a copy of the GNU
00024   -_. . .   )=.  =           Library General Public License along with
00025     --        :-=`           this library; see the file COPYING.LIB.
00026                              If not, write to the Free Software Foundation,
00027                              Inc., 59 Temple Place - Suite 330,
00028                              Boston, MA 02111-1307, USA.
00029 */
00030 
00031 #include "predicttabwidget.h"
00032 
00033 #include <qpe/config.h>
00034 #include <qpe/timeconversion.h>
00035 
00036 #include <qlabel.h>
00037 #include <qlayout.h>
00038 #include <qpushbutton.h>
00039 #include <qscrollview.h>
00040 #include <qtable.h>
00041 
00042 #include <stdlib.h>
00043 
00044 PredictTabWidget::PredictTabWidget( QWidget *parent )
00045         : QWidget( parent, 0x0, 0 )
00046 {
00047 /*
00048         QVBoxLayout *tmpvb = new QVBoxLayout( this );
00049         QScrollView *sv = new QScrollView( this );
00050         tmpvb->addWidget( sv, 0, 0 );
00051         sv->setResizePolicy( QScrollView::AutoOneFit );
00052         sv->setFrameStyle( QFrame::NoFrame );
00053         QWidget *container = new QWidget( sv->viewport() );
00054         sv->addChild( container );
00055 */
00056 
00057         QGridLayout *layout = new QGridLayout( this );
00058         layout->setMargin( 2 );
00059         layout->setSpacing( 4 );
00060 
00061         // Predicted time drift
00062         layout->addWidget( new QLabel( tr( "Predicted time drift" ), this ), 0, 0 );
00063         lblDrift = new QLabel( tr( "n/a" ), this );
00064         layout->addWidget( lblDrift, 0, 1 );
00065 
00066         // Estimated time difference
00067         layout->addWidget( new QLabel( tr( "Estimated shift" ), this ), 1, 0 );
00068         lblDiff = new QLabel( tr( "n/a" ), this );
00069         layout->addWidget( lblDiff, 1, 1 );
00070 
00071         // Predicted time
00072         layout->addWidget( new QLabel( tr( "Predicted time" ), this ), 2, 0 );
00073         lblPredicted = new QLabel( tr( "n/a" ), this );
00074         layout->addWidget( lblPredicted, 2, 1 );
00075 
00076         // Prediction table
00077         tblLookups = new QTable( 2, 3, this );
00078         QFont font(  tblLookups->font() );
00079         font.setPointSize( font.pointSize() - 2 );
00080         tblLookups->setFont( font );
00081         tblLookups->horizontalHeader()->setLabel( 0, tr( "Shift [s/h]" ) );
00082         tblLookups->horizontalHeader()->setLabel( 1, tr( "Last [h]" ) );
00083         tblLookups->horizontalHeader()->setLabel( 2, tr( "Offset [s]" ) );
00084         
00085         tblLookups->setMinimumHeight( 50 );
00086         tblLookups->setSizePolicy( QSizePolicy( QSizePolicy::Preferred, QSizePolicy::Maximum ) );
00087         layout->addMultiCellWidget( tblLookups, 3, 3, 0, 1 );
00088 
00089         // Predict time button
00090         QPushButton *pb = new QPushButton( tr( "Predict time" ), this );
00091         connect( pb, SIGNAL(clicked()), this, SLOT(slotPredictTime()) );
00092         layout->addWidget( pb, 4, 0 );
00093 
00094         // Set predicted time button
00095         pb = new QPushButton( tr( "Set predicted time" ), this );
00096         connect( pb, SIGNAL(clicked()), this, SLOT(slotSetPredictedTime()) );
00097         layout->addWidget( pb, 4, 1 );
00098 
00099         // Initialize values
00100         Config config( "ntp" );
00101         config.setGroup( "lookups" );
00102         int lookupCount = config.readNumEntry( "count", 0 );
00103         float last, shift, shiftPerSec;
00104         tblLookups->setNumRows( lookupCount );
00105         int cw = tblLookups->width() / 4;
00106         cw = 50;
00107         tblLookups->sortColumn( 0, FALSE, TRUE );
00108         _shiftPerSec = 0;
00109         QString grpname;
00110         for ( int i=0; i < lookupCount; i++ )
00111         {
00112                 grpname = "lookup_";
00113                 grpname.append( QString::number( i ) );
00114                 config.setGroup( grpname );
00115                 last = config.readEntry( "secsSinceLast", 0 ).toFloat();
00116                 shift = QString( config.readEntry( "timeShift", 0 ) ).toFloat();
00117                 shiftPerSec =  shift / last;
00118                 _shiftPerSec += shiftPerSec;
00119                 tblLookups->setText( i, 0, QString::number( shiftPerSec * 60 * 60 ) );
00120                 tblLookups->setText( i, 2, QString::number( shift ) );
00121                 tblLookups->setText( i, 1, QString::number( last / ( 60 * 60 ) ) );
00122         }
00123         if(lookupCount)
00124             _shiftPerSec /= lookupCount;
00125         QString drift = QString::number( _shiftPerSec * 60 * 60);
00126         drift.append( tr( " s/h" ) );
00127         lblDrift->setText( drift );
00128 
00129         Config lconfig( "locale" );
00130         lconfig.setGroup( "Location" );
00131         tz = lconfig.readEntry( "Timezone", "America/New_York" );
00132 }
00133 
00134 PredictTabWidget::~PredictTabWidget()
00135 {
00136 }
00137 
00138 void PredictTabWidget::setShiftPerSec( int i )
00139 {
00140         _shiftPerSec += i;
00141 }
00142 
00143 void PredictTabWidget::slotTZChanged( const QString &newtz )
00144 {
00145         tz = newtz;
00146 }
00147 
00148 void PredictTabWidget::slotPredictTime()
00149 {
00150         Config config( "ntp" );
00151         config.setGroup( "lookups" );
00152         int lastTime = config.readNumEntry( "time", 0 );
00153         config.writeEntry( "lastNtp", TRUE );
00154         setenv( "TZ", tz, 1 );
00155         int now = TimeConversion::toUTC( QDateTime::currentDateTime() );
00156         int corr = int( ( now - lastTime ) * _shiftPerSec );
00157         QString diff = QString::number( corr );
00158         diff.append( tr( " seconds" ) );
00159         lblDiff->setText( diff );
00160         predictedTime = QDateTime::currentDateTime().addSecs( corr );
00161         lblPredicted->setText( predictedTime.toString() );
00162 }
00163 
00164 void PredictTabWidget::slotSetPredictedTime()
00165 {
00166         slotPredictTime();
00167         emit setTime( predictedTime );
00168 }

Generated on Sat Nov 5 16:17:48 2005 for OPIE by  doxygen 1.4.2