kontact Library API Documentation

summarywidget.cpp

00001 /*
00002     This file is part of Kontact.
00003     Copyright (c) 2003 Tobias Koenig <tokoe@kde.org>
00004 
00005     This program is free software; you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation; either version 2 of the License, or
00008     (at your option) any later version.
00009 
00010     This program is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with this program; if not, write to the Free Software
00017     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00018 
00019     As a special exception, permission is given to link this program
00020     with any edition of Qt, and distribute the resulting executable,
00021     without including the source code for Qt in the source distribution.
00022 */
00023 
00024 #include <qimage.h>
00025 #include <qlabel.h>
00026 #include <qlayout.h>
00027 #include <qtooltip.h>
00028 
00029 #include <dcopclient.h>
00030 #include <dcopref.h>
00031 #include <kapplication.h>
00032 #include <kdebug.h>
00033 #include <kglobal.h>
00034 #include <kglobalsettings.h>
00035 #include <kiconloader.h>
00036 #include <klocale.h>
00037 
00038 #include "summarywidget.h"
00039 
00040 SummaryWidget::SummaryWidget( QWidget *parent, const char *name )
00041   : Kontact::Summary( parent, name ),
00042     DCOPObject( "WeatherSummaryWidget" )
00043 {
00044   mLayout = new QVBoxLayout( this );
00045   mLayout->setAlignment( Qt::AlignTop );
00046 
00047   QPixmap icon = KGlobal::iconLoader()->loadIcon( "kweather", KIcon::Desktop, KIcon::SizeMedium );
00048   QWidget *header = createHeader( this, icon, i18n( "Weather Information" ) );
00049   mLayout->addWidget( header );
00050 
00051   connectDCOPSignal( 0, 0, "fileUpdate(QString)", "refresh(QString)", false );
00052   connectDCOPSignal( 0, 0, "stationRemoved(QString)", "stationRemoved(QString)", false );
00053 
00054   QString error;
00055   QCString appID;
00056 
00057   bool serviceAvailable = true;
00058   if ( !kapp->dcopClient()->isApplicationRegistered( "KWeatherService" ) ) {
00059     if ( KApplication::startServiceByDesktopName( "kweatherservice", QStringList(), &error, &appID ) ) {
00060       kdDebug() << "No service available..." << endl;
00061       serviceAvailable = false;
00062     }
00063   }
00064 
00065   if ( serviceAvailable ) {
00066     DCOPRef dcopCall( "KWeatherService", "WeatherService" );
00067     DCOPReply reply = dcopCall.call( "listStations()", true );
00068     if ( reply.isValid() ) {
00069       mStations = reply;
00070 
00071       connect( &mTimer, SIGNAL( timeout() ), this, SLOT( timeout() ) );
00072       mTimer.start( 0 );
00073     } else {
00074       kdDebug() << "ERROR: dcop reply not valid..." << endl;
00075     }
00076   }
00077 }
00078 
00079 int SummaryWidget::summaryHeight() const
00080 {
00081   if ( mStations.isEmpty() )
00082     return 0;
00083   else
00084     return 1;
00085 }
00086 
00087 void SummaryWidget::updateView()
00088 {
00089   mLayouts.setAutoDelete( true );
00090   mLayouts.clear();
00091   mLayouts.setAutoDelete( false );
00092 
00093   mLabels.setAutoDelete( true );
00094   mLabels.clear();
00095   mLabels.setAutoDelete( false );
00096 
00097   if ( mStations.count() == 0 ) {
00098     kdDebug() << "No weather stations defined..." << endl;
00099     return;
00100   }
00101 
00102   QMap<QString, WeatherData>::Iterator it;
00103   for ( it = mWeatherMap.begin(); it != mWeatherMap.end(); ++it ) {
00104     WeatherData data = it.data();
00105 
00106     QString cover;
00107     for ( uint i = 0; i < data.cover().count(); ++i )
00108       cover += QString( "- %1\n" ).arg( data.cover()[ i ] );
00109 
00110     QImage img;
00111     img = data.icon();
00112 
00113     QGridLayout *layout = new QGridLayout( mLayout, 3, 3, 3 );
00114     mLayout->addStretch( 10 );
00115     mLayouts.append( layout );
00116 
00117     QLabel *label = new QLabel( this );
00118     label->setPixmap( img.smoothScale( 32, 32 ) );
00119     label->setMaximumSize(label->sizeHint());
00120     label->setAlignment(/* AlignRight |*/ AlignTop );
00121     layout->addMultiCellWidget( label, 0, 1, 0, 0 );
00122     mLabels.append( label );
00123 
00124     label = new QLabel( this );
00125     label->setText( QString( "%1 (%2)" ).arg( data.name() ).arg( data.temperature() ) );
00126     QFont font = label->font();
00127     font.setBold( true );
00128     label->setFont( font );
00129     label->setAlignment( AlignLeft );
00130     layout->addMultiCellWidget( label, 0, 0, 1, 2 );
00131     mLabels.append( label );
00132 
00133     QString labelText;
00134     labelText = QString( "<b>%1:</b> %2<br>"
00135                          "<b>%3:</b> %4" )
00136                          .arg( i18n( "Wind Speed" ) )
00137                          .arg( data.windSpeed() )
00138                          .arg( i18n( "Rel. Humidity" ) )
00139                          .arg( data.relativeHumidity() );
00140 
00141     QToolTip::add( label, labelText.replace( " ", "&nbsp;" ) );
00142 
00143     label = new QLabel( cover, this );
00144     label->setAlignment( AlignLeft );
00145     layout->addMultiCellWidget( label, 1, 1, 1, 2 );
00146     mLabels.append( label );
00147   }
00148 
00149   for ( QLabel *label = mLabels.first(); label; label = mLabels.next() )
00150     label->show();
00151 
00152   mLayout->addStretch( 1 );
00153 }
00154 
00155 void SummaryWidget::timeout()
00156 {
00157   mTimer.stop();
00158 
00159   DCOPRef dcopCall( "KWeatherService", "WeatherService" );
00160   dcopCall.send( "updateAll()" );
00161 
00162   mTimer.start( 15 * 60000 );
00163 }
00164 
00165 void SummaryWidget::refresh( QString station )
00166 {
00167   DCOPRef dcopCall( "KWeatherService", "WeatherService" );
00168     
00169   mWeatherMap[ station ].setIcon( dcopCall.call( "currentIcon(QString)", station, true ) );
00170   mWeatherMap[ station ].setName( dcopCall.call( "stationName(QString)", station, true ) );
00171   mWeatherMap[ station ].setCover( dcopCall.call( "cover(QString)", station, true ) );
00172   mWeatherMap[ station ].setTemperature( dcopCall.call( "temperature(QString)", station, true ) );
00173   mWeatherMap[ station ].setWindSpeed( dcopCall.call( "wind(QString)", station, true ) );
00174   mWeatherMap[ station ].setRelativeHumidity( dcopCall.call( "relativeHumidity(QString)", station, true ) );
00175 
00176   updateView();
00177 }
00178 
00179 void SummaryWidget::stationRemoved( QString station )
00180 {
00181   mWeatherMap.remove( station );
00182   updateView();
00183 }
00184 
00185 QStringList SummaryWidget::configModules() const
00186 {
00187   return QStringList( "kcmweatherservice.desktop" );
00188 }
00189 
00190 #include "summarywidget.moc"
KDE Logo
This file is part of the documentation for kontact Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat May 1 11:39:00 2004 by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2003