/* This file is part of the KDE project
   Copyright (C) 2002-2003 Norbert Andres <nandres@web.de>
             (C) 2002 John Dailey <dailey@vt.edu>
             (C) 2001-2002 Philipp Mueller <philipp.mueller@gmx.de>
             (C) 1999-2005 Laurent Montel <montel@kde.org>
             (C) 1998-1999 Torben Weis <weis@kde.org>
   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 "kspread_dlg_validity.h"
#include "kspread_canvas.h"
#include "kspread_doc.h"
#include "kspread_locale.h"
#include "kspread_sheet.h"
#include "kspread_view.h"

#include <tqlayout.h>
#include <tqbuttongroup.h>
#include <knumvalidator.h>
#include <kdebug.h>
#include <tdemessagebox.h>

#include <tqcombobox.h>
#include <tqlineedit.h>
#include <tqpushbutton.h>
#include <tqlabel.h>
#include <tqtextedit.h>
#include <tqcheckbox.h>

using namespace KSpread;

DlgValidity::DlgValidity(View* parent,const char* name , const TQRect &_marker )
  :KDialogBase(KDialogBase::Tabbed, i18n("Validity"),User2|User1|Cancel, User1, parent, name,true,false,KStdGuiItem::ok(),i18n("Clear &All"))

{
  m_pView=parent;
  marker=_marker;
  TQFrame *page1 = addPage(i18n("&Values"));
  TQVBoxLayout *lay1 = new TQVBoxLayout( page1, KDialogBase::marginHint(), KDialogBase::spacingHint() );

  TQGroupBox* tmpTQButtonGroup;
  tmpTQButtonGroup = new TQGroupBox( 0, TQt::Vertical, i18n("Validity Criteria"), page1, "ButtonGroup_1" );
  tmpTQButtonGroup->layout()->setSpacing(KDialog::spacingHint());
  tmpTQButtonGroup->layout()->setMargin(KDialog::marginHint());
  TQGridLayout *grid1 = new TQGridLayout(tmpTQButtonGroup->layout(),4,2);

  TQLabel *tmpTQLabel = new TQLabel( tmpTQButtonGroup, "Label_1" );
  tmpTQLabel->setText(i18n("Allow:" ));
  grid1->addWidget(tmpTQLabel,0,0);

  chooseType=new TQComboBox(tmpTQButtonGroup);
  grid1->addWidget(chooseType,0,1);
  TQStringList listType;
  listType+=i18n("All");
  listType+=i18n("Number");
  listType+=i18n("Integer");
  listType+=i18n("Text");
  listType+=i18n("Date");
  listType+=i18n("Time");
  listType+=i18n("Text Length");
  listType+=i18n("List");
  chooseType->insertStringList(listType);
  chooseType->setCurrentItem(0);

  allowEmptyCell = new TQCheckBox( i18n( "Allow blanks" ), tmpTQButtonGroup );
  grid1->addWidget(allowEmptyCell,1,0);

  tmpTQLabel = new TQLabel( tmpTQButtonGroup, "Label_2" );
  tmpTQLabel->setText(i18n("Data:" ));
  grid1->addWidget(tmpTQLabel,2,0);

  choose=new TQComboBox(tmpTQButtonGroup);
  grid1->addWidget(choose,2,1);
  TQStringList list;
  list+=i18n("equal to");
  list+=i18n("greater than");
  list+=i18n("less than");
  list+=i18n("equal to or greater than");
  list+=i18n("equal to or less than");
  list+=i18n("between");
  list+=i18n("different from");
  list+=i18n("different to");
  choose->insertStringList(list);
  choose->setCurrentItem(0);

  edit1 = new TQLabel( tmpTQButtonGroup, "Label_3" );
  edit1->setText(i18n("Minimum:" ));
  grid1->addWidget(edit1,3,0);

  val_min=new TQLineEdit(tmpTQButtonGroup);
  grid1->addWidget(val_min,3,1);
  val_min->setValidator( new KFloatValidator( val_min ) );

  edit2 = new TQLabel( tmpTQButtonGroup, "Label_4" );
  edit2->setText(i18n("Maximum:" ));
  grid1->addWidget(edit2,4,0);

  val_max=new TQLineEdit(tmpTQButtonGroup);
  grid1->addWidget(val_max,4,1);
  val_max->setValidator( new KFloatValidator( val_max ) );
  lay1->addWidget(tmpTQButtonGroup);

  //Apply minimum width of column1 to avoid horizontal move when changing option
  //A bit ugly to apply text always, but I couldn't get a label->TQFontMetrix.boundingRect("text").width()
  //to give mew the correct results - Philipp
  edit2->setText( i18n( "Date:" ) );
  grid1->addColSpacing( 0, edit2->width() );
  edit2->setText( i18n( "Date minimum:" ) );
  grid1->addColSpacing( 0, edit2->width() );
  edit2->setText( i18n( "Date maximum:" ) );
  grid1->addColSpacing( 0, edit2->width() );
  edit2->setText( i18n( "Time:" ) );
  grid1->addColSpacing( 0, edit2->width() );
  edit2->setText( i18n( "Time minimum:" ) );
  grid1->addColSpacing( 0, edit2->width() );
  edit2->setText( i18n( "Time maximum:" ) );
  grid1->addColSpacing( 0, edit2->width() );
  edit2->setText( i18n( "Minimum:" ) );
  grid1->addColSpacing( 0, edit2->width() );
  edit2->setText( i18n( "Maximum:" ) );
  grid1->addColSpacing( 0, edit2->width() );
  edit2->setText( i18n( "Number:" ) );
  grid1->addColSpacing( 0, edit2->width() );

  validityList = new TQTextEdit( tmpTQButtonGroup );
  grid1->addMultiCellWidget(validityList,2, 4,1, 1);

  validityLabelList = new TQLabel( tmpTQButtonGroup, "Label_Label_List" );
  validityLabelList->setText(i18n("Entries:" ));
  grid1->addWidget(validityLabelList,2,0);

  TQFrame *page2 = addPage(i18n("&Error Alert"));

  lay1 = new TQVBoxLayout( page2, KDialogBase::marginHint(), KDialogBase::spacingHint() );

  tmpTQButtonGroup = new TQButtonGroup( 0, TQt::Vertical, i18n("Contents"), page2, "ButtonGroup_2" );
  tmpTQButtonGroup->layout()->setSpacing(KDialog::spacingHint());
  tmpTQButtonGroup->layout()->setMargin(KDialog::marginHint());
  TQGridLayout *grid2 = new TQGridLayout(tmpTQButtonGroup->layout(),5,2);

  displayMessage = new TQCheckBox(i18n( "Show error message when invalid values are entered" ),tmpTQButtonGroup );
  displayMessage->setChecked( true );
  grid2->addMultiCellWidget(displayMessage,0, 0,0, 1);

  tmpTQLabel = new TQLabel( tmpTQButtonGroup, "Label_5" );
  tmpTQLabel->setText(i18n("Action:" ));
  grid2->addWidget(tmpTQLabel,1,0);

  chooseAction=new TQComboBox(tmpTQButtonGroup);
  grid2->addWidget(chooseAction,1,1);
  TQStringList list2;
  list2+=i18n("Stop");
  list2+=i18n("Warning");
  list2+=i18n("Information");
  chooseAction->insertStringList(list2);
  chooseAction->setCurrentItem(0);
  tmpTQLabel = new TQLabel( tmpTQButtonGroup, "Label_6" );
  tmpTQLabel->setText(i18n("Title:" ));
  grid2->addWidget(tmpTQLabel,2,0);

  title=new TQLineEdit(  tmpTQButtonGroup);
  grid2->addWidget(title,2,1);

  tmpTQLabel = new TQLabel( tmpTQButtonGroup, "Label_7" );
  tmpTQLabel->setText(i18n("Message:" ));
  grid2->addWidget(tmpTQLabel,3,0);

  message =new TQTextEdit( tmpTQButtonGroup);
  grid2->addMultiCellWidget(message,3, 4,1, 1);
  lay1->addWidget(tmpTQButtonGroup);
  message->setTextFormat( TQt::PlainText );

  TQFrame *page3 = addPage(i18n("Input Help"));
  lay1 = new TQVBoxLayout( page3, KDialogBase::marginHint(), KDialogBase::spacingHint() );

  tmpTQButtonGroup = new TQButtonGroup( 0, TQt::Vertical, i18n("Contents"), page3, "ButtonGroup_2" );
  tmpTQButtonGroup->layout()->setSpacing(KDialog::spacingHint());
  tmpTQButtonGroup->layout()->setMargin(KDialog::marginHint());

  TQGridLayout *grid3 = new TQGridLayout(tmpTQButtonGroup->layout(),5,2);

  displayHelp = new TQCheckBox(i18n( "Show input help when cell is selected" ),tmpTQButtonGroup );
  displayMessage->setChecked( false );
  grid3->addMultiCellWidget(displayHelp,0, 0,0, 1);

  tmpTQLabel = new TQLabel( tmpTQButtonGroup, "Label_6" );
  tmpTQLabel->setText(i18n("Title:" ));
  grid3->addWidget(tmpTQLabel,2,0);

  titleHelp=new TQLineEdit(  tmpTQButtonGroup);
  grid3->addWidget(titleHelp,2,1);

  tmpTQLabel = new TQLabel( tmpTQButtonGroup, "Label_7" );
  tmpTQLabel->setText(i18n("Message:" ));
  grid3->addWidget(tmpTQLabel,3,0);

  messageHelp =new TQTextEdit( tmpTQButtonGroup);
  grid3->addMultiCellWidget(messageHelp,3, 4,1, 1);
  lay1->addWidget(tmpTQButtonGroup);
  messageHelp->setTextFormat( TQt::PlainText );


  connect(choose,TQ_SIGNAL(activated(int )),this,TQ_SLOT(changeIndexCond(int)));
  connect(chooseType,TQ_SIGNAL(activated(int )),this,TQ_SLOT(changeIndexType(int)));
  connect( this, TQ_SIGNAL(user1Clicked()), TQ_SLOT(OkPressed()) );
  connect( this, TQ_SIGNAL(user2Clicked()), TQ_SLOT(clearAllPressed()) );
  init();
}

void DlgValidity::displayOrNotListOfValidity( bool _displayList)
{
    if ( _displayList )
    {
        validityList->show();
        validityLabelList->show();
        edit1->hide();
        val_min->hide();
        edit2->hide();
        val_max->hide();
    }
    else
    {
        validityList->hide();
        validityLabelList->hide();
        edit1->show();
        val_min->show();
        edit2->show();
        val_max->show();
    }
}

void DlgValidity::changeIndexType(int _index)
{
    bool activate = ( _index!=0 );
    message->setEnabled(activate);
    title->setEnabled(activate);
    chooseAction->setEnabled( activate );
    displayHelp->setEnabled(activate);
    messageHelp->setEnabled(activate);
    titleHelp->setEnabled(activate);
    if ( _index == 7 )
        displayOrNotListOfValidity( true );
    else
        displayOrNotListOfValidity( false );

    switch(_index)
    {
    case 0:
        edit1->setText("");
        edit2->setText("");
        val_max->setEnabled(false);
        val_min->setEnabled(false);
        edit1->setEnabled(false);
        edit2->setEnabled(false);
        choose->setEnabled(false);
        break;
    case 1:
        val_min->setEnabled(true);
        edit1->setEnabled(true);
        choose->setEnabled(true);
        val_min->setValidator( new KFloatValidator( val_min ) );
        val_max->setValidator( new KFloatValidator( val_max ) );
        if( choose->currentItem()<=4)
        {
            edit1->setText(i18n("Number:"));
            edit2->setText("");
            edit2->setEnabled(false);
            val_max->setEnabled(false);
        }
        else
        {
            edit1->setText(i18n("Minimum:" ));
            edit2->setText(i18n("Maximum:" ));
            edit2->setEnabled(true);
            val_max->setEnabled(true);
        }
        break;
    case 2:
    case 6:
        val_min->setEnabled(true);
        edit1->setEnabled(true);
        choose->setEnabled(true);
        val_min->setValidator( new KIntValidator( val_min ) );
        val_max->setValidator( new KIntValidator( val_max ) );
        if( choose->currentItem()<=4)
        {
            edit1->setText(i18n("Number:"));
            edit2->setText("");
            edit2->setEnabled(false);
            val_max->setEnabled(false);
        }
        else
        {
            edit1->setText(i18n("Minimum:" ));
            edit2->setText(i18n("Maximum:" ));
            edit2->setEnabled(true);
            val_max->setEnabled(true);
        }
        break;

    case 3:
        edit1->setText("");
        edit2->setText("");
        val_max->setEnabled(false);
        val_min->setEnabled(false);
        choose->setEnabled(false);
        edit1->setEnabled(false);
        edit2->setEnabled(false);
        break;
    case 4:
        edit1->setText(i18n("Date:"));
        edit2->setText("");
        val_min->setEnabled(true);
        edit1->setEnabled(true);
        choose->setEnabled(true);

        val_min->clearValidator();
        val_max->clearValidator();
        if( choose->currentItem()<=4)
        {
            edit1->setText(i18n("Date:"));
            edit2->setText("");
            edit2->setEnabled(false);
            val_max->setEnabled(false);
        }
        else
        {
            edit1->setText(i18n("Date minimum:"));
            edit2->setText(i18n("Date maximum:"));
            edit2->setEnabled(true);
            val_max->setEnabled(true);
        }
        break;
    case 5:
        val_min->setEnabled(true);
        edit1->setEnabled(true);
        choose->setEnabled(true);
        val_min->clearValidator();
        val_max->clearValidator();
        if( choose->currentItem()<=4)
        {
            edit1->setText(i18n("Time:"));
            edit2->setText("");
            edit2->setEnabled(false);
            val_max->setEnabled(false);
        }
        else
        {
            edit1->setText(i18n("Time minimum:"));
            edit2->setText(i18n("Time maximum:"));
            edit2->setEnabled(true);
            val_max->setEnabled(true);
        }
        break;
    }
    if ( width() < sizeHint().width() )
        resize( sizeHint() );
}

void DlgValidity::changeIndexCond(int _index)
{
  switch(_index)
  {
   case 0:
   case 1:
   case 2:
   case 3:
   case 4:
    val_max->setEnabled(false);
    if(chooseType->currentItem()==1 ||chooseType->currentItem()==2
       ||chooseType->currentItem()==6)
      edit1->setText(i18n("Number:"));
    else if( chooseType->currentItem()==3)
      edit1->setText("");
    else if( chooseType->currentItem()==4)
      edit1->setText(i18n("Date:"));
    else if( chooseType->currentItem()==5)
      edit1->setText(i18n("Time:"));
    edit2->setText("");
    edit2->setEnabled(false);
    break;
   case 5:
   case 6:
    val_max->setEnabled(true);
    edit2->setEnabled(true);
    edit1->setEnabled(true);
    if(chooseType->currentItem()==1 || chooseType->currentItem()==2
       || chooseType->currentItem()==6)
    {
      edit1->setText(i18n("Minimum:" ));
      edit2->setText(i18n("Maximum:" ));
    }
    else if(chooseType->currentItem()==3)
    {
      edit1->setText("");
      edit2->setText("");
    }
    else if(chooseType->currentItem()==4)
    {
      edit1->setText(i18n("Date minimum:"));
      edit2->setText(i18n("Date maximum:"));
    }
    else if(chooseType->currentItem()==5)
    {
      edit1->setText(i18n("Time minimum:"));
      edit2->setText(i18n("Time maximum:"));
    }
    break;
  }
}

void DlgValidity::init()
{
  Cell *c = m_pView->activeSheet()->cellAt( marker.left(), marker.top() );
  Validity * tmpValidity=c->getValidity(0);
  if(tmpValidity!=0)
  {
    message->setText(tmpValidity->message);
    title->setText(tmpValidity->title);
    TQString tmp;
    switch( tmpValidity->m_restriction)
    {
     case Restriction::None:
      chooseType->setCurrentItem(0);
      break;
     case Restriction::Number:
      chooseType->setCurrentItem(1);
      if(tmpValidity->m_cond >=5 )
        val_max->setText(tmp.setNum(tmpValidity->valMax));
      val_min->setText(tmp.setNum(tmpValidity->valMin));
      break;
     case Restriction::Integer:
      chooseType->setCurrentItem(2);
      if(tmpValidity->m_cond >=5 )
        val_max->setText(tmp.setNum(tmpValidity->valMax));
      val_min->setText(tmp.setNum(tmpValidity->valMin));
      break;
     case Restriction::TextLength:
      chooseType->setCurrentItem(6);
      if(tmpValidity->m_cond >=5 )
        val_max->setText(tmp.setNum(tmpValidity->valMax));
      val_min->setText(tmp.setNum(tmpValidity->valMin));
      break;
     case Restriction::Text:
      chooseType->setCurrentItem(3);
      break;
     case Restriction::Date:
      chooseType->setCurrentItem(4);
      val_min->setText(m_pView->doc()->locale()->formatDate(tmpValidity->dateMin,true));
      if(tmpValidity->m_cond >=5 )
        val_max->setText(m_pView->doc()->locale()->formatDate(tmpValidity->dateMax,true));
      break;
     case Restriction::Time:
      chooseType->setCurrentItem(5);
      val_min->setText(m_pView->doc()->locale()->formatTime(tmpValidity->timeMin,true));
      if(tmpValidity->m_cond >=5 )
        val_max->setText(m_pView->doc()->locale()->formatTime(tmpValidity->timeMax,true));
      break;
     case Restriction::List:
     {
         chooseType->setCurrentItem(7);
         TQStringList lst =tmpValidity->listValidity;
         TQString tmp;
         for ( TQStringList::Iterator it = lst.begin(); it != lst.end(); ++it ) {
             tmp +=( *it )+"\n";
         }
         validityList->setText( tmp );
     }
      break;
     default :
      chooseType->setCurrentItem(0);
      break;
    }
    switch (tmpValidity->m_action)
    {
      case Action::Stop:
      chooseAction->setCurrentItem(0);
      break;
      case Action::Warning:
      chooseAction->setCurrentItem(1);
      break;
      case Action::Information:
      chooseAction->setCurrentItem(2);
      break;
     default :
      chooseAction->setCurrentItem(0);
      break;
    }
    switch ( tmpValidity->m_cond )
    {
      case Conditional::Equal:
      choose->setCurrentItem(0);
      break;
      case Conditional::Superior:
      choose->setCurrentItem(1);
      break;
      case Conditional::Inferior:
      choose->setCurrentItem(2);
      break;
      case Conditional::SuperiorEqual:
      choose->setCurrentItem(3);
      break;
      case Conditional::InferiorEqual:
      choose->setCurrentItem(4);
      break;
      case Conditional::Between:
      choose->setCurrentItem(5);
      break;
      case Conditional::Different:
      choose->setCurrentItem(6);
      break;
      case Conditional::DifferentTo:
      choose->setCurrentItem(7);
      break;
     default :
      choose->setCurrentItem(0);
      break;
    }
    displayMessage->setChecked( tmpValidity->displayMessage );
    allowEmptyCell->setChecked( tmpValidity->allowEmptyCell );
    titleHelp->setText( tmpValidity->titleInfo );
    messageHelp->setText( tmpValidity->messageInfo );
    displayHelp->setChecked( tmpValidity->displayValidationInformation );
  }
  changeIndexType(chooseType->currentItem()) ;
  changeIndexCond(choose->currentItem()) ;
}

void DlgValidity::clearAllPressed()
{
  val_min->setText("");
  val_max->setText("");
  message->setText("");
  title->setText("");
  displayMessage->setChecked( true );
  allowEmptyCell->setChecked( false );
  choose->setCurrentItem(0);
  chooseType->setCurrentItem(0);
  chooseAction->setCurrentItem(0);
  changeIndexType(0);
  changeIndexCond(0);
  messageHelp->setText("" );
  titleHelp->setText( "" );
  validityList->setText( "" );
  displayHelp->setChecked( false );
}

void DlgValidity::OkPressed()
{
  if( chooseType->currentItem()==1)
  {
    bool ok;
    val_min->text().toDouble(&ok);
    if(! ok)
    {
      KMessageBox::error( this , i18n("This is not a valid value."),i18n("Error"));
      val_min->setText("");
      return;
    }
    val_max->text().toDouble(&ok);
    if(! ok && choose->currentItem() >=5 && choose->currentItem()< 7)
    {
      KMessageBox::error( this , i18n("This is not a valid value."),i18n("Error"));
      val_max->setText("");
      return;
    }
  }
  else if( chooseType->currentItem()==2 || chooseType->currentItem()==6)
  {
    bool ok;
    val_min->text().toInt(&ok);
    if(! ok)
    {
      KMessageBox::error( this , i18n("This is not a valid value."),i18n("Error"));
      val_min->setText("");
      return;
    }
    val_max->text().toInt(&ok);
    if(! ok && choose->currentItem() >=5 && choose->currentItem()< 7)
    {
      KMessageBox::error( this , i18n("This is not a valid value."),i18n("Error"));
      val_max->setText("");
      return;
    }
  }
  else  if(  chooseType->currentItem()==5)
  {
    if(! m_pView->doc()->locale()->readTime(val_min->text()).isValid())
    {
      KMessageBox::error( this , i18n("This is not a valid time."),i18n("Error"));
      val_min->setText("");
      return;
    }
    if(! m_pView->doc()->locale()->readTime(val_max->text()).isValid() && choose->currentItem()  >=5)
    {
      KMessageBox::error( this , i18n("This is not a valid time."),i18n("Error"));
      val_max->setText("");
      return;
    }
  }
  else  if(  chooseType->currentItem()==4)
  {
    if(! m_pView->doc()->locale()->readDate(val_min->text()).isValid())
    {
      KMessageBox::error( this , i18n("This is not a valid date."),i18n("Error"));
      val_min->setText("");
      return;
    }
    if(! m_pView->doc()->locale()->readDate(val_max->text()).isValid() && choose->currentItem()  >=5 )
    {
      KMessageBox::error( this , i18n("This is not a valid date."),i18n("Error"));
      val_max->setText("");
      return;
    }
  }
  else if ( chooseType->currentItem()==7 )
  {
      //Nothing
  }

  if( chooseType->currentItem()==0)
  {//no validity
    result.m_restriction=Restriction::None;
    result.m_action=Action::Stop;
    result.m_cond=Conditional::Equal;
    result.message=message->text();
    result.title=title->text();
    result.valMin=0;
    result.valMax=0;
    result.timeMin=TQTime(0,0,0);
    result.timeMax=TQTime(0,0,0);
    result.dateMin=TQDate(0,0,0);
    result.dateMax=TQDate(0,0,0);
  }
  else
  {
    switch( chooseType->currentItem())
    {
     case 0:
      result.m_restriction=Restriction::None;
      break;
     case 1:
      result.m_restriction=Restriction::Number;
      break;
     case 2:
      result.m_restriction=Restriction::Integer;
      break;
     case 3:
      result.m_restriction=Restriction::Text;
      break;
     case 4:
      result.m_restriction=Restriction::Date;
      break;
     case 5:
      result.m_restriction=Restriction::Time;
      break;
     case 6:
      result.m_restriction=Restriction::TextLength;
      break;
     case 7:
      result.m_restriction=Restriction::List;
      break;

     default :
      break;
    }
    switch (chooseAction->currentItem())
    {
     case 0:
       result.m_action=Action::Stop;
      break;
     case 1:
       result.m_action=Action::Warning;
      break;
     case 2:
       result.m_action=Action::Information;
      break;
     default :
      break;
    }
    switch ( choose->currentItem())
    {
     case 0:
       result.m_cond=Conditional::Equal;
      break;
     case 1:
       result.m_cond=Conditional::Superior;
      break;
     case 2:
       result.m_cond=Conditional::Inferior;
      break;
     case 3:
       result.m_cond=Conditional::SuperiorEqual;
      break;
     case 4:
       result.m_cond=Conditional::InferiorEqual;
      break;
     case 5:
       result.m_cond=Conditional::Between;
      break;
     case 6:
       result.m_cond=Conditional::Different;
      break;
     case 7:
       result.m_cond=Conditional::DifferentTo;
      break;
     default :
      break;
    }
    result.message=message->text();
    result.title=title->text();
    result.valMin=0;
    result.valMax=0;
    result.timeMin=TQTime(0,0,0);
    result.timeMax=TQTime(0,0,0);
    result.dateMin=TQDate(0,0,0);
    result.dateMax=TQDate(0,0,0);

    if( chooseType->currentItem()==1)
    {
      if(choose->currentItem()  <5)
      {
        result.valMin=val_min->text().toDouble();
      }
      else
      {
        result.valMin=TQMIN(val_min->text().toDouble(),val_max->text().toDouble());
        result.valMax=TQMAX(val_max->text().toDouble(),val_min->text().toDouble());
      }
    }
    else if( chooseType->currentItem()==2 || chooseType->currentItem()==6)
    {
      if(choose->currentItem()  <5)
      {
        result.valMin=val_min->text().toInt();
      }
      else
      {
        result.valMin=TQMIN(val_min->text().toInt(),val_max->text().toInt());
        result.valMax=TQMAX(val_max->text().toInt(),val_min->text().toInt());
      }
    }
    else  if(  chooseType->currentItem()==4)
    {
      if(choose->currentItem()  <5)
      {
        result.dateMin=m_pView->doc()->locale()->readDate(val_min->text());
      }
      else
      {
        if(m_pView->doc()->locale()->readDate(val_min->text())<m_pView->doc()->locale()->readDate(val_max->text()))
        {
          result.dateMin=m_pView->doc()->locale()->readDate(val_min->text());
          result.dateMax=m_pView->doc()->locale()->readDate(val_max->text());
        }
        else
        {
          result.dateMin=m_pView->doc()->locale()->readDate(val_max->text());
          result.dateMax=m_pView->doc()->locale()->readDate(val_min->text());
        }
      }
    }
    else  if(  chooseType->currentItem()==5)
    {
      if(choose->currentItem()  <5)
      {
        result.timeMin=m_pView->doc()->locale()->readTime(val_min->text());
      }
      else
      {
        if(m_pView->doc()->locale()->readTime(val_min->text())<m_pView->doc()->locale()->readTime(val_max->text()))
        {
          result.timeMax=m_pView->doc()->locale()->readTime(val_max->text());
          result.timeMin=m_pView->doc()->locale()->readTime(val_min->text());
        }
        else
        {
          result.timeMax=m_pView->doc()->locale()->readTime(val_min->text());
          result.timeMin=m_pView->doc()->locale()->readTime(val_max->text());
        }
      }
    }
    else if ( chooseType->currentItem()==7 )
    {
        result.listValidity=TQStringList::split( "\n", validityList->text() );
    }
  }
  result.displayMessage = displayMessage->isChecked();
  result.allowEmptyCell = allowEmptyCell->isChecked();
  result.displayValidationInformation = displayHelp->isChecked();
  result.messageInfo= messageHelp->text();
  result.titleInfo = titleHelp->text();

  m_pView->doc()->emitBeginOperation( false );
  m_pView->activeSheet()->setValidity( m_pView->selectionInfo(),  result);
  m_pView->slotUpdateView( m_pView->activeSheet() );
  accept();
}

#include "kspread_dlg_validity.moc"
