/* This file is part of the KDE project
   Copyright (C) 2002 Ulrich Kuettler <ulrich.kuettler@mailbox.tu-dresden.de>

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

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#include <tqcheckbox.h>
#include <tqimage.h>
#include <tqlabel.h>
#include <tqlayout.h>
#include <tqlineedit.h>
#include <tqpaintdevice.h>
#include <tqrect.h>
#include <tqvbuttongroup.h>
#include <tqwidget.h>

#include <tdeapplication.h>
#include <kdebug.h>
#include <tdelocale.h>
#include <tdemessagebox.h>
#include <knuminput.h>

#include "exportsizedia.h"


ExportSizeDia::ExportSizeDia( int width, int height, 
			    TQWidget *parent, const char *name )
    : KDialogBase( parent, name, true,
		   i18n("Export Filter Parameters" ), Ok|Cancel )
{
    kapp->restoreOverrideCursor();

    setupGUI();

    m_realWidth  = width;
    m_realHeight = height;
    m_widthEdit ->setValue( m_realWidth );
    m_heightEdit->setValue( m_realHeight  );
    m_percWidthEdit->setValue( 100 );
    m_percHeightEdit->setValue( 100 );

    connectAll();
    connect( m_proportional, TQ_SIGNAL( clicked() ),
             this,         TQ_SLOT( proportionalClicked() ) );
}


ExportSizeDia::~ExportSizeDia()
{
}


void ExportSizeDia::setupGUI()
{
    //resize( size() );
    TQWidget *page = new TQWidget( this );
    setMainWidget(page);

#if 0
    TQBoxLayout* mainLayout = new TQVBoxLayout( page, 
					      KDialog::marginHint(), 
					      KDialog::spacingHint() );
#else
    TQGridLayout *mainLayout = new TQGridLayout( page, 5, 2,
					       KDialog::marginHint(), 
					       KDialog::spacingHint() );
#endif
    m_proportional = new TQCheckBox( page, "proportional" );
    m_proportional->setText( i18n( "Keep ratio" ) );
    m_proportional->setChecked( true );
    mainLayout->addWidget( m_proportional, 0, 0 );

    TQLabel* width = new TQLabel( page, "width" );
    width->setText( i18n( "Width:" ) );
    m_widthEdit = new KIntNumInput( page, "widthEdit" );
    TQLabel* height = new TQLabel( page, "height" );
    height->setText( i18n( "Height:" ) );
    m_heightEdit = new KIntNumInput( page, "heightEdit" );

    mainLayout->addWidget( width,      1, 0 );
    mainLayout->addWidget( m_widthEdit,  1, 1 );
    mainLayout->addWidget( height,     2, 0 );
    mainLayout->addWidget( m_heightEdit, 2, 1 );

    TQLabel* percentWidth = new TQLabel( page, "PercentWidth" );
    percentWidth->setText( i18n( "Width (%):" ) );
    m_percWidthEdit = new KDoubleNumInput( page, "percWidthEdit" );
    TQLabel* percentHeight = new TQLabel( page, "PercentHeight" );
    percentHeight->setText( i18n( "Height (%):" ) );
    m_percHeightEdit = new KDoubleNumInput( page, "percHeightEdit" );

    mainLayout->addWidget( percentWidth,   3, 0 );
    mainLayout->addWidget( m_percHeightEdit, 3, 1 );
    mainLayout->addWidget( percentHeight,  4, 0 );
    mainLayout->addWidget( m_percWidthEdit,  4, 1 );

    /* Display the main layout */
    //mainLayout->addStretch( 5 );
    mainLayout->activate();
}


// ----------------------------------------------------------------
//                          public methods

int ExportSizeDia::width() const
{
    return m_widthEdit->value();
}


int ExportSizeDia::height() const
{
    return m_heightEdit->value();
}


// ----------------------------------------------------------------
//                            slots


void ExportSizeDia::widthChanged( int width )
{
    disconnectAll();
    width = TQMIN( width, m_realWidth * 10 );
    width = TQMAX( width, m_realWidth / 10 );
    double percent = (100.0 * static_cast<double>( width ) 
		      / static_cast<double>( m_realWidth ));
    m_percWidthEdit->setValue(  percent  );
    if ( m_proportional->isChecked() ) {
        m_percHeightEdit->setValue( percent );
        int height = static_cast<int>( m_realHeight * percent / 100.0 );
        m_heightEdit->setValue(  height );
    }
    connectAll();
}


void ExportSizeDia::heightChanged( int height )
{
    disconnectAll();
    height = TQMIN( height, m_realHeight * 10 );
    height = TQMAX( height, m_realHeight / 10 );
    double percent = (100.0 * static_cast<double>( height )
		      / static_cast<double>( m_realHeight ));
    m_percHeightEdit->setValue( percent  );
    if ( m_proportional->isChecked() ) {
        m_percWidthEdit->setValue(  percent  );
        int width = static_cast<int>( m_realWidth * percent / 100.0 );
        m_widthEdit->setValue( width );
    }
    connectAll();
}


void ExportSizeDia::percentWidthChanged( double percent )
{
    disconnectAll();
    percent = TQMIN( percent, 1000 );
    percent = TQMAX( percent, 10 );
    int width = static_cast<int>( m_realWidth * percent / 100. );
    m_widthEdit->setValue(  width  );
    if ( m_proportional->isChecked() ) {
        int height = static_cast<int>( m_realHeight * percent / 100. );
        m_heightEdit->setValue(  height  );
        m_percHeightEdit->setValue(  percent );
    }
    connectAll();
}


void ExportSizeDia::percentHeightChanged( double percent )
{
    disconnectAll();
    percent = TQMIN( percent, 1000 );
    percent = TQMAX( percent, 10 );
    if ( m_proportional->isChecked() ) {
        int width = static_cast<int>( m_realWidth * percent / 100. );
        m_widthEdit->setValue(  width  );
        m_percWidthEdit->setValue(  percent  );
    }
    int height = static_cast<int>( m_realHeight * percent / 100. );
    m_heightEdit->setValue(  height  );
    connectAll();
}


void ExportSizeDia::proportionalClicked()
{
    if ( m_proportional->isChecked() ) {
        disconnectAll();
        int width = m_widthEdit->value( );
        width = TQMIN( width, m_realWidth * 10 );
        width = TQMAX( width, m_realWidth / 10 );
        double percent = (100.0 * static_cast<double>( width )
			  / static_cast<double>( m_realWidth ));
        m_percHeightEdit->setValue(  percent  );
        int height = static_cast<int>( m_realHeight * percent / 100. );
        m_heightEdit->setValue(  height  );
        connectAll();
    }
}


// ----------------------------------------------------------------
//                          private methods


void ExportSizeDia::connectAll()
{
    connect( m_widthEdit,      TQ_SIGNAL( valueChanged(int) ),
             this,             TQ_SLOT( widthChanged( int ) ) );
    connect( m_heightEdit,     TQ_SIGNAL( valueChanged(int) ),
             this,             TQ_SLOT( heightChanged( int ) ) );
    connect( m_percWidthEdit,  TQ_SIGNAL( valueChanged(double) ),
             this,             TQ_SLOT( percentWidthChanged( double ) ) );
    connect( m_percHeightEdit, TQ_SIGNAL( valueChanged(double) ),
             this,             TQ_SLOT( percentHeightChanged(double ) ) );
}


void ExportSizeDia::disconnectAll()
{
    disconnect( m_widthEdit,      TQ_SIGNAL( valueChanged(int) ),
		this,             TQ_SLOT( widthChanged( int ) ) );
    disconnect( m_heightEdit,     TQ_SIGNAL( valueChanged(int) ),
		this,             TQ_SLOT( heightChanged( int ) ) );
    disconnect( m_percWidthEdit,  TQ_SIGNAL( valueChanged(double) ),
		this,             TQ_SLOT( percentWidthChanged( double ) ) );
    disconnect( m_percHeightEdit, TQ_SIGNAL( valueChanged(double) ),
		this,             TQ_SLOT( percentHeightChanged(double ) ) );
}


#if 0
void ExportSizeDia::slotOk()
{
    hide();
    //doc->setZoomAndResolution( 100, 600, 600 );
    //doc->setZoomAndResolution( 1000, TQPaintDevice::x11AppDpiX(), TQPaintDevice::x11AppDpiY() );
    //doc->newZoomAndResolution( false, false );
    int width = widthEdit->value();
    int height = heightEdit->value();
//     kdDebug( KFormula::DEBUGID ) << k_funcinfo
//                                  << "(" << width << " " << height << ")"
//                                  << endl;
//     width = realWidth;
//     height = realHeight;
    TQImage image = formula->drawImage( width, height );
    if ( !image.save( _fileOut, "PNG" ) ) {
        KMessageBox::error( 0, i18n( "Failed to write file." ), i18n( "PNG Export Error" ) );
    }
    reject();
}
#endif

#include "exportsizedia.moc"

