/***************************************************************************
                      definitiondialog.cpp  -  description
                             -------------------
    begin                : Don Apr 18 12:34:56 CEST 2002
    copyright            : (C) 2002 by Dominik Seichter
    email                : domseichter@web.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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 "definitiondialog.h"
#include "printersettings.h"
#include "labelutils.h"
#include "definition.h"

#include <kcombobox.h>
#include <kpushbutton.h>
#include <krestrictedline.h>
#include <tqlabel.h>
#include <tqlayout.h>
#include <tqpainter.h>

// KDE includes
#include <tdelocale.h>

LabelPreview::LabelPreview( TQWidget* parent, const char* name )
    : TQWidget( parent, name)
{
    // For old DIN A4 preview
    PrinterSettings* ps = PrinterSettings::getInstance();
    setFixedSize( TQSize(  ((int)ps->pageWidth() + 20)/2, ((int)ps->pageHeight() + 20)/2 ) );
    m_prv_enabled = true;
}

LabelPreview::~LabelPreview()
{ }

void LabelPreview::paintEvent( TQPaintEvent* )
{
    TQPainter p( this );
    p.fillRect( 0, 0, width(), height(), TQBrush( TQt::white ) );
    p.setPen( TQPen( TQt::black ) );
    p.translate( 10, 10 );

    p.fillRect( 0, 0, 210/2, 297/2, TQBrush( TQt::gray ) );
    p.drawRect( 0, 0, 210/2, 297/2 );

    if(!m_prv_enabled)
        return;

//    int numv = measure.num_v > 0 ? measure.num_v : 1;
    for( int v = 0; v < measure.numV(); v++ ) {
        for( int h = 0; h < measure.numH(); h++ ) {
            if( !v && !h )
                p.setPen( TQPen( TQt::red ) );
            else
                p.setPen( TQPen( TQt::black ) );

            p.fillRect( int(measure.gapLeftMM() + (measure.gapHMM() * h)) / 2,
                        int(measure.gapTopMM() + (measure.gapVMM() * v)) / 2 ,
                        (int)measure.widthMM() / 2 , (int)measure.heightMM() / 2, TQBrush( TQt::white ) );

            p.drawRect( int(measure.gapLeftMM() + (measure.gapHMM() * h)) / 2,
                        int(measure.gapTopMM() + (measure.gapVMM() * v)) / 2,
                        (int)measure.widthMM() / 2, (int)measure.heightMM() / 2 );
        }
    }
}

/*****************************************/
DefinitionDialog::DefinitionDialog( TQWidget* parent,  const char* name, bool modal, WFlags fl )
    : TQDialog( parent, name, modal, fl )
{
    resize( 465, 345 );
    setCaption( i18n( "Add Label Definition" ) );
    TQHBoxLayout* layout = new TQHBoxLayout( this, 6, 6 );
    DefinitionDialogLayout = new TQVBoxLayout( 0, 11, 6, "DefinitionDialogLayout");

    Layout17 = new TQHBoxLayout( 0, 0, 6, "Layout17");

    Layout13 = new TQVBoxLayout( 0, 0, 6, "Layout13");

    TextLabel1 = new TQLabel( this, "TextLabel1" );
    TextLabel1->setText( i18n( "Producer:" ) );
    Layout13->addWidget( TextLabel1 );

    TextLabel2 = new TQLabel( this, "TextLabel2" );
    TextLabel2->setText( i18n( "Type:" ) );
    Layout13->addWidget( TextLabel2 );

    TextLabel3 = new TQLabel( this, "TextLabel3" );
    TextLabel3->setText( TQString( i18n( "Width (in %1):" ) ).arg( Measurements::system() ) );
    Layout13->addWidget( TextLabel3 );

    TextLabel4 = new TQLabel( this, "TextLabel4" );
    TextLabel4->setText( TQString( i18n( "Height (in %1):" ) ).arg( Measurements::system() ) );
    Layout13->addWidget( TextLabel4 );

    TextLabel5 = new TQLabel( this, "TextLabel5" );
    TextLabel5->setText( i18n( "Horizontal Gap:" ) );
    Layout13->addWidget( TextLabel5 );

    TextLabel6 = new TQLabel( this, "TextLabel6" );
    TextLabel6->setText( i18n( "Vertical Gap:" ) );
    Layout13->addWidget( TextLabel6 );

    TextLabel7 = new TQLabel( this, "TextLabel7" );
    TextLabel7->setText( i18n( "Top Gap:" ) );
    Layout13->addWidget( TextLabel7 );

    TextLabel8 = new TQLabel( this, "TextLabel8" );
    TextLabel8->setText( i18n( "Left Gap:" ) );
    Layout13->addWidget( TextLabel8 );

    TextLabel9 = new TQLabel( this, "TextLabel9" );
    TextLabel9->setText( i18n("Number Horizontal:") );
    Layout13->addWidget( TextLabel9 );

    TextLabel10 = new TQLabel( this, "TextLabel10" );
    TextLabel10->setText( i18n("Number Vertical:") );
    Layout13->addWidget( TextLabel10 );

    Layout17->addLayout( Layout13 );
    Layout14 = new TQVBoxLayout( 0, 0, 6, "Layout14");

    comboProducer = new KComboBox( false, this, "comboProducer" );
    comboProducer->setEditable( true );
    Layout14->addWidget( comboProducer );

    comboType = new KComboBox( false, this, "type" );
    comboType->setEditable( true );
    Layout14->addWidget( comboType );

    editWidth = new KRestrictedLine( this, "editWidth" );
    editWidth->setText( i18n( "0" ) );
    editWidth->setValidChars( i18n( "0123456789." ) );
    Layout14->addWidget( editWidth );

    editHeight = new KRestrictedLine( this, "editHeight" );
    editHeight->setText( i18n( "0" ) );
    editHeight->setValidChars( i18n( "0123456789." ) );
    Layout14->addWidget( editHeight );

    editHGap = new KRestrictedLine( this, "editHGap" );
    editHGap->setText( i18n( "0" ) );
    editHGap->setValidChars( i18n( "0123456789." ) );
    Layout14->addWidget( editHGap );

    editVGap = new KRestrictedLine( this, "editVGap" );
    editVGap->setText( i18n( "0" ) );
    editVGap->setValidChars( i18n( "0123456789." ) );
    Layout14->addWidget( editVGap );

    editTGap = new KRestrictedLine( this, "editTGap" );
    editTGap->setText( i18n( "0" ) );
    editTGap->setValidChars( i18n( "0123456789." ) );
    Layout14->addWidget( editTGap );

    editLGap = new KRestrictedLine( this, "editLGap" );
    editLGap->setText( i18n( "0" ) );
    editLGap->setValidChars( i18n( "0123456789." ) );
    Layout14->addWidget( editLGap );

    editNumH = new KRestrictedLine( this, "editNumH" );
    editNumH->setText( i18n( "0" ) );
    editNumH->setValidChars( "0123456789" );
    Layout14->addWidget( editNumH );

    editNumV = new KRestrictedLine( this, "editNumV" );
    editNumV->setText( i18n( "0" ) );
    editNumV->setValidChars( "0123456789" );
    Layout14->addWidget( editNumV );

    Layout17->addLayout( Layout14 );
    TQSpacerItem* spacer = new TQSpacerItem( 0, 0, TQSizePolicy::Expanding, TQSizePolicy::Minimum );
    Layout17->addItem( spacer );
    DefinitionDialogLayout->addLayout( Layout17 );
    TQSpacerItem* spacer_2 = new TQSpacerItem( 0, 0, TQSizePolicy::Minimum, TQSizePolicy::Expanding );
    DefinitionDialogLayout->addItem( spacer_2 );

    Layout16 = new TQHBoxLayout( 0, 0, 6, "Layout16");
    buttonInfo = new KPushButton( i18n("More &Information"), this );
    Layout16->addWidget( buttonInfo );

    TQSpacerItem* spacer_3 = new TQSpacerItem( 0, 0, TQSizePolicy::Expanding, TQSizePolicy::Minimum );
    Layout16->addItem( spacer_3 );

    buttonAdd = new KPushButton( this, "buttonAdd" );
    buttonAdd->setText( i18n( "&Add" ) );
    Layout16->addWidget( buttonAdd );

    buttonCancel = new KPushButton( this, "buttonCancel" );
    buttonCancel->setText( i18n( "&Cancel" ) );
    Layout16->addWidget( buttonCancel );
    DefinitionDialogLayout->addLayout( Layout16 );

    preview = new LabelPreview( this );
    layout->addLayout( DefinitionDialogLayout );
    layout->addWidget( preview );

    l = new TQLabel( this );
    setExtension( l );
    setOrientation(TQt::Vertical );

    connect( buttonCancel, TQ_SIGNAL( clicked() ), this, TQ_SLOT( reject() ) );
    connect( buttonAdd, TQ_SIGNAL( clicked() ), this, TQ_SLOT( add() ) );

    connect( editWidth, TQ_SIGNAL( textChanged(const TQString &) ), this, TQ_SLOT( updatePreview() ) );
    connect( editHeight, TQ_SIGNAL( textChanged(const TQString &) ), this, TQ_SLOT( updatePreview() ) );
    connect( editHGap, TQ_SIGNAL( textChanged(const TQString &) ), this, TQ_SLOT( updatePreview() ) );
    connect( editVGap, TQ_SIGNAL( textChanged(const TQString &) ), this, TQ_SLOT( updatePreview() ) );
    connect( editTGap, TQ_SIGNAL( textChanged(const TQString &) ), this, TQ_SLOT( updatePreview() ) );
    connect( editLGap, TQ_SIGNAL( textChanged(const TQString &) ), this, TQ_SLOT( updatePreview() ) );
    connect( editNumH, TQ_SIGNAL( textChanged(const TQString &) ), this, TQ_SLOT( updatePreview() ) );
    connect( editNumV, TQ_SIGNAL( textChanged(const TQString &) ), this, TQ_SLOT( updatePreview() ) );

    comboProducer->setInsertionPolicy( TQComboBox::NoInsertion );
    comboType->setInsertionPolicy( TQComboBox::NoInsertion );

    comboProducer->clear();
    TQStringList list = Definition::getProducers();
    comboProducer->insertStringList( list );

    types = new TQStringList[list.count()];
    for( unsigned int i = 0; i < list.count(); i++ )
        types[i] = Definition::getTypes( list[i] );

    connect( comboProducer, TQ_SIGNAL( textChanged(const TQString &) ), this, TQ_SLOT( updateType() ) );
    connect( comboType, TQ_SIGNAL( textChanged(const TQString &) ), this, TQ_SLOT( updateText() ) );
    connect( buttonInfo, TQ_SIGNAL( clicked() ), this, TQ_SLOT( toggleExtension() ) );

    drawGraphic();
    updateType();

    showExtension( false );
}

DefinitionDialog::~DefinitionDialog()
{
    delete [] types;
}

void DefinitionDialog::add()
{
    Definition::write( getCurrentMeasure(), comboType->currentText(), comboProducer->currentText() );

    accept();
}

void DefinitionDialog::updateType()
{
    comboType->clear();
    if( comboProducer->currentText() == comboProducer->text( comboProducer->currentItem() ) )
        if( comboProducer->currentItem() < comboProducer->count())
            comboType->insertStringList( types[ comboProducer->currentItem() ] );
    comboType->insertItem( i18n( "Custom" ), 0 );
}

const Measurements DefinitionDialog::getCurrentMeasure()
{
    Measurements m;
    m.setGapLeft( editLGap->text().toDouble() );
    m.setGapTop( editTGap->text().toDouble() );
    m.setWidth( editWidth->text().toDouble() );
    m.setHeight( editHeight->text().toDouble() );
    m.setGapV( editVGap->text().toDouble() );
    m.setGapH( editHGap->text().toDouble() );
    m.setNumH( editNumH->text().toInt() );
    m.setNumV( editNumV->text().toInt() );

    return m;
}

void DefinitionDialog::updateText()
{
    Definition   d( comboProducer->currentText(), comboType->currentText() );
    Measurements m = d.getMeasurements();

    editTGap->setText( I2S( m.gapTop() ) );
    editLGap->setText( I2S( m.gapLeft() ) );
    editWidth->setText( I2S( m.width() ) );
    editHeight->setText( I2S( m.height() ) );
    editVGap->setText( I2S( m.gapV() ) );
    editHGap->setText( I2S( m.gapH() ) );
    editNumH->setText( I2S( m.numH() ) );
    editNumV->setText( I2S( m.numV() ) );

    updatePreview();
}

void DefinitionDialog::updatePreview()
{
    Measurements m = getCurrentMeasure();

    editHeight->setPaletteForegroundColor( TQt::black );
    editWidth->setPaletteForegroundColor( TQt::black );
    editVGap->setPaletteForegroundColor( TQt::black );
    editHGap->setPaletteForegroundColor( TQt::black );
    editTGap->setPaletteForegroundColor( TQt::black );
    editLGap->setPaletteForegroundColor( TQt::black );
    editNumH->setPaletteForegroundColor( TQt::black );
    editNumV->setPaletteForegroundColor( TQt::black );

    // Mark errors in Red
    if( m.heightMM() > PrinterSettings::getInstance()->pageHeight() )
        editHeight->setPaletteForegroundColor( TQt::red );

    if( m.widthMM() > PrinterSettings::getInstance()->pageWidth() )
        editWidth->setPaletteForegroundColor( TQt::red );

    if( m.gapVMM() < m.heightMM() )
        editVGap->setPaletteForegroundColor( TQt::red );

    if( m.gapHMM() < m.widthMM() )
        editHGap->setPaletteForegroundColor( TQt::red );

    if( m.gapTopMM() >  PrinterSettings::getInstance()->pageHeight() - m.heightMM() )
        editTGap->setPaletteForegroundColor( TQt::red );

    if( m.gapLeftMM() > PrinterSettings::getInstance()->pageWidth() - m.widthMM() )
        editLGap->setPaletteForegroundColor( TQt::red );

    if( m.gapLeftMM() + m.numH() * m.gapHMM() > PrinterSettings::getInstance()->pageWidth() )
        editNumH->setPaletteForegroundColor( TQt::red );

    if( m.gapTopMM() + m.numV() * m.gapVMM() > PrinterSettings::getInstance()->pageHeight() )
        editNumV->setPaletteForegroundColor( TQt::red );

    preview->setRect( TQRect( (int)m.gapLeftMM(), (int)m.gapTopMM(), (int)m.widthMM(), (int)m.heightMM() ) );
    preview->setMeasurements( m );
    preview->repaint();
}

void DefinitionDialog::drawGraphic()
{
    TQWMatrix wm;
    wm.rotate( 90 );

    TQPixmap pic( 450, 330 );
    pic.fill( TQt::gray );

    TQPainter p( &pic );
    p.setPen( TQt::black );

    p.fillRect( 60, 60, 450, 330, TQColor( 255, 254, 217 ) );
    p.drawRect( 60, 60, 450, 330 );
    p.setBrush( TQt::white );

    p.setPen( TQt::DotLine );
    p.drawRoundRect( 90, 90, 150, 90 );
    p.drawRoundRect( 270, 90, 150, 90 );

    p.drawRoundRect( 90, 210, 150, 90 );
    p.drawRoundRect( 270, 210, 150, 90 );

    // Draw width
    p.setPen( TQPen( TQt::red, 2 ) );
    p.drawLine( 90, 150, 240, 150 );

    // Draw numh
    p.drawLine( 90, 270, 450, 270 );

    // Draw Gap Top
    p.setPen( TQPen( TQt::red, 2, TQt::DotLine ) );
    p.drawLine( 0, 60, 90, 60 );
    p.drawLine( 0, 90, 90, 90 );

    // draw Gap V
    p.drawLine( 0, 210, 90, 210 );

    // Draw height
    p.setPen( TQPen( TQt::green, 2 ) );
    p.drawLine( 150, 90, 150, 180 );


    // Draw numv
    p.drawLine( 330, 90, 330, 330 );

    // Draw Gap Left
    p.setPen( TQPen( TQt::green, 2, TQt::DotLine ) );
    p.drawLine( 60, 0, 60, 90 );
    p.drawLine( 90, 0, 90, 90 );

    // draw Gap H
    p.drawLine( 270, 0, 270, 90 );

    // draw Texts:
    p.setPen( TQt::black );
    p.drawText( 100, 140, i18n("Width") );
    p.drawText( 160, 120, i18n("Height") );
    p.drawText( 100, 260, i18n("Number of horizontal Labels") );

    TQPixmap* pix = LabelUtils::drawString( i18n("Number of vertical Labels") );
    p.drawPixmap( 340, 110, pix->xForm( wm ) );
    delete pix;

    p.drawText( 5, 80, i18n("Gap Top") );

    pix = LabelUtils::drawString( i18n("Gap Left") );

    p.drawPixmap( 60, 5, pix->xForm( wm ) );
    delete pix;

    p.drawText( 5, 150, i18n("Vertical Gap") );
    p.drawText( 150, 50, i18n("Horizontal Gap") );

    p.end();

    l->setPixmap( pic );
}

void DefinitionDialog::toggleExtension()
{
    if( l->isVisible() ) {
        showExtension( false );
    } else
        showExtension( true );
}


#include "definitiondialog.moc"
