/***************************************************************************
                          dockwidget.cpp  -  description
                             -------------------
    begin                : Thu Jul 6 2000
    copyright            : (C) 2000-2003 by Ian Reinhart Geiser
                         : (C) 2002-2003 Nadeem Hasan <nhasan@kde.org>
    email                : geiseri@msoe.edu
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "dockwidget.h"
#include "weatherbutton.h"
#include "weatherservice_stub.h"

#include <qtooltip.h>
#include <qvbox.h>
#include <qlabel.h>
#include <qtimer.h>

#include <kdebug.h>
#include <kglobalsettings.h>
#include <klocale.h>

dockwidget::dockwidget(const QString &location, QWidget *parent,
        const char *name) : QWidget(parent,name), m_locationCode( location ), m_orientation( Horizontal )
{
    initDock();
    connect(m_button, SIGNAL( clicked() ), SIGNAL( buttonClicked() ));

    m_weatherService = new WeatherService_stub( "KWeatherService", "WeatherService" );
    m_font = KGlobalSettings::generalFont();

    setBackgroundOrigin( AncestorOrigin );
}

dockwidget::~dockwidget(){
    delete m_weatherService;
}

void dockwidget::setLocationCode(const QString &locationCode)
{
	m_locationCode = locationCode;
}

void dockwidget::setViewMode(int _mode)
{
    kdDebug(12004) << "View mode " << _mode << endl;
    m_mode = _mode;
    if (m_mode == ShowIconOnly)
    {
        m_lblTemp->hide();
        m_lblWind->hide();
        m_lblPres->hide();
    }
    else if (m_mode == ShowTempOnly)
    {
        m_lblTemp->show();
        m_lblWind->hide();
        m_lblPres->hide();
    }
    else if (m_mode == ShowAll)
    {
        m_lblTemp->show();
        m_lblWind->show();
        m_lblPres->show();
    }
}

void dockwidget::showWeather()
{
    QString tip;

    QString temp     = "?";
    QString wind     = "?";
    QString pressure = "?";

    if ( m_weatherService->stationNeedsMaintenance( m_locationCode ) )
    {
        tip = i18n("Station reports that it needs maintenance\n"
                   "Please try again later");
    }
    else
    {
        if ( !m_locationCode.isEmpty() )
        {
            temp     = m_weatherService->temperature( m_locationCode );
            wind     = m_weatherService->wind( m_locationCode );
            pressure = m_weatherService->pressure( m_locationCode );
        }

        if (m_mode != ShowAll)
        {
            tip = i18n("Temperature: ") + temp;
            tip += i18n("\nWind: ") + wind;
            tip += i18n("\nAir pressure: ") + pressure;
        }
        else
        {
            tip = i18n("Click here to see\nthe detailed weather report...");
        }
    }

    // On null or empty location code, or if the station needs maintenance, this will return the dunno icon.
    QPixmap icon = m_weatherService->icon( m_locationCode );

    QToolTip::remove(m_button);
    QToolTip::add(m_button, tip);

    kdDebug(12004) << "show weather: " << endl;
    kdDebug(12004) << "location: " << m_locationCode << endl;
    kdDebug(12004) << "temp,wind,pressure: " << temp << " " << wind << " " << pressure << endl;
    kdDebug(12004) << "tip: " << tip << endl;

    m_lblTemp->setText(temp);
    m_lblWind->setText(wind);
    m_lblPres->setText(pressure);

    m_button->setPixmap( icon );
}

void dockwidget::initDock(){
    kdDebug(12004) << "Init dockwidget " << endl;

    m_button= new WeatherButton(this,"m_button");

    m_lblTemp= new QLabel(this,"lblTemp");
    m_lblWind= new QLabel(this,"lblWind");
    m_lblPres= new QLabel(this,"lblPres");

    m_lblTemp->setBackgroundOrigin(AncestorOrigin);
    m_lblWind->setBackgroundOrigin(AncestorOrigin);
    m_lblPres->setBackgroundOrigin(AncestorOrigin);

    m_lblTemp->setMargin(0);
    m_lblWind->setMargin(0);
    m_lblPres->setMargin(0);

    updateFont();

    QTimer::singleShot( 0, this, SLOT( showWeather() ) );
}

/** resize the view **/
void dockwidget::resizeView( const QSize &size )
{
    kdDebug(12004) << "Changing to size " << size << endl;
    resize(size);

    int w = size.width();
    int h = size.height();

    if (m_orientation == Horizontal)
    {
        // in case we previously set this to hcenter previously
        // and now change the layout
        m_lblTemp->setAlignment(AlignVCenter);

        // Kicker in horizontal mode
        if( m_mode == ShowAll)
        {
            m_lblTemp->setGeometry(h+1, 0, w-h, h/3);
            m_lblWind->setGeometry(h+1, h/3, w-h, h/3);
            m_lblPres->setGeometry(h+1, 2*h/3, w-h, h/3);
        }
        else if ( m_mode == ShowTempOnly)
        {
            if (h > 32)
            {
                m_lblTemp->setAlignment(AlignVCenter | AlignHCenter);
                m_lblTemp->setGeometry(1, h - h/5, w - 2, h/5);
                m_button->setGeometry(0, 0, 4*h/5, 4*h/5);
            }
            else
            {
                m_lblTemp->setGeometry(h+1, 0, w-h, h);
                m_button->setGeometry(0, 0, h, h );
            }
        }
        else
        {
            m_button->setGeometry(0, 0, h, h );
        }
    }
    else
    {
        // Kicker in vertical mode
        if( m_mode == ShowAll)
        {
            m_lblTemp->setGeometry(0, w, w, w/3);
            m_lblWind->setGeometry(0, 4*w/3, w, w/3);
            m_lblPres->setGeometry(0, 5*w/3, w, w/3);
        }
        else if ( m_mode == ShowTempOnly)
        {
            m_lblTemp->setGeometry(1, w, w, h-(w+1) );
        }
        m_button->setGeometry(0, 0, w, w );
    }
    updateFont();
}

int dockwidget::widthForHeight(int h)
{
    int w=h;
    QFontMetrics fm(m_font);

    if( m_mode == ShowAll)
    {
        m_font.setPixelSize( h/3-2 );
        w = h + QMAX(fm.width(m_lblWind->text()),
        fm.width(m_lblPres->text())) + 1;
    }
    else if ( m_mode == ShowTempOnly)
    {
        QFontInfo fi( KGlobalSettings::generalFont() );
        int pixelSize = QMIN( h, fi.pixelSize() );
        m_font.setPixelSize( pixelSize );

        if (h > 32)
        {
            w = 4*h/5;
        }
        else
        {
            w = h + fm.width(m_lblTemp->text()) + 1;
        }
    }

    updateFont();
    return w;
}

int dockwidget::heightForWidth( int w )
{
    int h;
    // Adjust the height of the font to compensate for width
    int startSize = w;
    int width = w + 1;

    if ( m_mode == ShowAll )
    {
       while( (width > w)  && (startSize > 0))
       {
           m_font.setPixelSize( --startSize );
           QFontMetrics fm(m_font);
           width = QMAX(fm.width(m_lblWind->text()),
                        fm.width(m_lblPres->text()));
       }
       h = ( w * 2 );
    }
    else if ( m_mode == ShowTempOnly )
    {
       while( (width > w)  && (startSize > 0))
       {
           m_font.setPixelSize( --startSize );
           QFontMetrics fm(m_font);
           width = fm.width(m_lblTemp->text());
       }
       h = w + startSize + 2;
    }
    else
        h = w;

    updateFont();
    return h;
}


void dockwidget::updateFont()
{
    kdDebug(12004) << "Update font: " << m_font.pixelSize() << endl;
    m_lblTemp->setFont(m_font);
    m_lblWind->setFont(m_font);
    m_lblPres->setFont(m_font);
}

#include "dockwidget.moc"

// vim:ts=4:sw=4:et
