/****************************************************************************
**
** Copyright (C) 1999-2006 Frank Hemer <frank@hemer.org>,
**                         Tilo Riemer <riemer@crossvc.com>
**                         Wim Delvaux <wim.delvaux@chello.be>,
**                         Jose Hernandez <joseh@tesco.net>,
**                         Helmut Koll <HelmutKoll@web.de>
**
**
**----------------------------------------------------------------------------
**
**----------------------------------------------------------------------------
**
** CrossVC is available under two different licenses:
**
** If CrossVC is linked against the GPLed version of Qt 
** CrossVC is released under the terms of GPL also.
**
** If CrossVC is linked against a nonGPLed version of Qt 
** CrossVC is released under the terms of the 
** CrossVC License for non-Unix platforms (CLNU)
**
**
** CrossVC License for non-Unix platforms (CLNU):
**
** Redistribution and use in binary form, without modification, 
** are permitted provided that the following conditions are met:
**
** 1. Redistributions in binary form must reproduce the above copyright
**    notice, this list of conditions and the following disclaimer in the
**    documentation and/or other materials provided with the distribution.
** 2. It is not permitted to distribute the binary package under a name
**    different than CrossVC.
** 3. The name of the authors may not be used to endorse or promote
**    products derived from this software without specific prior written
**    permission.
** 4. The source code is the creative property of the authors.
**    Extensions and development under the terms of the Gnu Public License
**    are limited to the Unix platform. Any distribution or compilation of 
**    the source code against libraries licensed other than gpl requires 
**    the written permission of the authors.
**
**
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR 
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
** ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 
** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 
** GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
** INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
** NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**
**
**
** CrossVC License for Unix platforms:
**
** 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, version 2 of the License.
** This program 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 General Public License version 2 for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software Foundation,
** Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
**
*****************************************************************************/

#include "config.h"

#include <qlayout.h>
#include <qpushbutton.h>
#include <qgroupbox.h> 
#include <qbuttongroup.h> 
#include <qfontmetrics.h> 

//----------------------------------------------------------------------------

#include "controls.h"

//----------------------------------------------------------------------------

IntValueLineEdit::IntValueLineEdit(QWidget * parent, const char * name)
: QLineEdit(parent, name)
{
   m_pIntValidator = 0;
}

//------------------------------------------------------------------------------

IntValueLineEdit::IntValueLineEdit(const QString &text, QWidget * parent, 
   const char * name)
: QLineEdit(text, parent, name)
{
   m_pIntValidator = 0;
}

//------------------------------------------------------------------------------

IntValueLineEdit::IntValueLineEdit(const int bottom, const int top, 
   QWidget * parent, const char * name)
: QLineEdit(parent, name)
{
   m_pIntValidator = new QIntValidator(bottom, top, this);
   setValidator(m_pIntValidator);
}

//------------------------------------------------------------------------------

IntValueLineEdit::IntValueLineEdit(const int bottom, const int top, 
   const QString &text, QWidget * parent, const char * name)
: QLineEdit(text, parent, name)
{
   m_pIntValidator = new QIntValidator(bottom, top, this);
   setValidator(m_pIntValidator);
}

//------------------------------------------------------------------------------

void IntValueLineEdit::setRange(const int bottom, const int top)
{
   if(m_pIntValidator)
      m_pIntValidator->setRange(bottom, top);
}

//------------------------------------------------------------------------------

int IntValueLineEdit::bottom() const
{
   if (m_pIntValidator)
      return m_pIntValidator->bottom();
   else 
      return 0;
}

//------------------------------------------------------------------------------

int IntValueLineEdit::top() const
{
   if (m_pIntValidator)
      return m_pIntValidator->top();
   else 
      return 0;
}

//------------------------------------------------------------------------------

void IntValueLineEdit::setText ( const QString & text)
{
   validate();
   QLineEdit::setText(text);
} 

//------------------------------------------------------------------------------

void IntValueLineEdit::validate()
{
   QString value;

   if(m_pIntValidator){
      if(text().toInt() > m_pIntValidator->top()){
         value.setNum(m_pIntValidator->top());
         QLineEdit::setText(value);
      }
      else
      if(text().toInt() < m_pIntValidator->bottom()){
         value.setNum(m_pIntValidator->bottom());
         QLineEdit::setText(value);
      }
   }
}

//------------------------------------------------------------------------------

void IntValueLineEdit::focusOutEvent(QFocusEvent *e)
{
   validate();
   QLineEdit::focusOutEvent(e);
}

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------

TagOperationControl::TagOperationControl(QStringList *tagList, const QString& checkBoxLabel,
   QWidget *parent, bool tagEnabled, bool useSticky, const char *name)
: QWidget(parent, name)
{
   m_pTagList = tagList;
   
   QBoxLayout *topLayer = new QVBoxLayout(this, 5);

   m_pCheckBox = new QCheckBox(checkBoxLabel, this);
   m_pCheckBox->setChecked(true);
   connect(m_pCheckBox, SIGNAL(toggled(bool)), this, SLOT(enableControls(bool)));
   topLayer->addWidget(m_pCheckBox);

   QButtonGroup *tagBox = new QButtonGroup(4, Vertical, this);
   tagBox->setExclusive(true);
   topLayer->addWidget(tagBox);

   QWidget *bg = new QWidget(tagBox);
   QGridLayout *gridLayer = new QGridLayout(bg, 4, 2, 5);

   //revision
   m_pLabelRev = new QRadioButton(tr("Revision:"), bg);
   m_pLabelRev->setChecked(true);
   connect(m_pCheckBox, SIGNAL(toggled(bool)), m_pLabelRev, SLOT(setEnabled(bool)));
   tagBox->insert(m_pLabelRev);
   gridLayer->addWidget(m_pLabelRev, 0, 0);

   m_pRevision = new QLineEdit(bg);
   connect(m_pLabelRev, SIGNAL(toggled(bool)), m_pRevision, SLOT(setEnabled(bool)));
   gridLayer->addWidget(m_pRevision, 0, 1);
   

   //tag
   m_pLabelTag = new QRadioButton(tr("Tag:"), bg);
   connect(m_pCheckBox, SIGNAL(toggled(bool)), m_pLabelTag, SLOT(setEnabled(bool)));
   tagBox->insert(m_pLabelTag);
   gridLayer->addWidget(m_pLabelTag, 1, 0);

   m_pTagChoice = new QComboBox( true, bg);
   for(unsigned int i = 0; i < m_pTagList->count(); i++) {
      m_pTagChoice->insertItem((*m_pTagList)[i]);
   }
   connect(m_pLabelTag, SIGNAL(toggled(bool)), m_pTagChoice, SLOT(setEnabled(bool)));
   m_pTagChoice->setEnabled(false);
   gridLayer->addWidget(m_pTagChoice, 1, 1);


   //date
   m_pLabelDate = new QRadioButton(tr("Date:"), bg);
   connect(m_pCheckBox, SIGNAL(toggled(bool)), m_pLabelDate, SLOT(setEnabled(bool)));
   tagBox->insert(m_pLabelDate);
   gridLayer->addWidget(m_pLabelDate, 2, 0);

   QWidget *dateBg = new QWidget(bg);
   gridLayer->addWidget(dateBg, 2, 1);
   
   QBoxLayout *dateLayer = new QHBoxLayout(dateBg, 5);

   m_pDateLabel = new QLabel(tr("YYYY.MM.DD"), dateBg);
   m_pDateLabel->setEnabled(false);
   connect(m_pLabelDate, SIGNAL(toggled(bool)), m_pDateLabel, SLOT(setEnabled(bool)));
   dateLayer->addWidget(m_pDateLabel);

   m_pYear = new IntValueLineEdit(1970, 9999, dateBg);
   m_pYear->setMaxLength(4);
   m_pYear->setMinimumWidth(m_pYear->fontMetrics().width("000000"));
   m_pYear->setText("1999");
   m_pYear->setEnabled(false);
   connect(m_pLabelDate, SIGNAL(toggled(bool)), m_pYear, SLOT(setEnabled(bool)));
   dateLayer->addWidget(m_pYear);

   m_pMonth = new IntValueLineEdit(1, 12, dateBg);
   m_pMonth->setMaxLength(2);
   m_pMonth->setMinimumWidth(m_pMonth->fontMetrics().width("000"));
   m_pMonth->setText("12");
   m_pMonth->setEnabled(false);
   connect(m_pLabelDate, SIGNAL(toggled(bool)), m_pMonth, SLOT(setEnabled(bool)));
   dateLayer->addWidget(m_pMonth);

   m_pDay = new IntValueLineEdit(1, 31, dateBg);
   m_pDay->setMaxLength(2);
   m_pDay->setMinimumWidth(m_pDay->fontMetrics().width("000"));
   m_pDay->setText("2");
   m_pDay->setEnabled(false);
   connect(m_pLabelDate, SIGNAL(toggled(bool)), m_pDay, SLOT(setEnabled(bool)));
   dateLayer->addWidget(m_pDay);

   dateLayer->addSpacing(10);
   
   m_pTimeLabel = new QLabel(tr("HH:MM"), dateBg);
   connect(m_pLabelDate, SIGNAL(toggled(bool)), m_pTimeLabel, SLOT(setEnabled(bool)));
   m_pTimeLabel->setEnabled(false);
   dateLayer->addWidget(m_pTimeLabel);

   m_pHour = new IntValueLineEdit(0, 24, dateBg);
   m_pHour->setMaxLength(2);
   m_pHour->setMinimumWidth(m_pHour->fontMetrics().width("000"));
   m_pHour->setText("00");
   m_pHour->setEnabled(false);
   connect(m_pLabelDate, SIGNAL(toggled(bool)), m_pHour, SLOT(setEnabled(bool)));
   dateLayer->addWidget(m_pHour);

   m_pMinute = new IntValueLineEdit(0, 59, dateBg);
   m_pMinute->setMaxLength(2);
   m_pMinute->setMinimumWidth(m_pMinute->fontMetrics().width("000"));
   m_pMinute->setText("00");
   m_pMinute->setEnabled(false);
   connect(m_pLabelDate, SIGNAL(toggled(bool)), m_pMinute, SLOT(setEnabled(bool)));
   dateLayer->addWidget(m_pMinute);


   //sticky
   m_pLabelSticky = new QRadioButton(tr("Reset Sticky Tag:"), bg);
   connect(m_pCheckBox, SIGNAL(toggled(bool)), m_pLabelSticky, SLOT(setEnabled(bool)));
   tagBox->insert(m_pLabelSticky);
   gridLayer->addWidget(m_pLabelSticky, 3, 0);
   

   if(!useSticky)  m_pLabelSticky->hide();
   if(!tagEnabled) m_pCheckBox->setChecked(false);

}

//------------------------------------------------------------------------------

void TagOperationControl::enableControls(bool on)
{
   if(on) {
      if(m_pLabelRev->isChecked()) m_pRevision->setEnabled(true);
      if(m_pLabelTag->isChecked()) m_pTagChoice->setEnabled(true);
      if(m_pLabelDate->isChecked()) m_pDateLabel->setEnabled(true);
      if(m_pLabelDate->isChecked()) m_pYear->setEnabled(true);
      if(m_pLabelDate->isChecked()) m_pMonth->setEnabled(true);
      if(m_pLabelDate->isChecked()) m_pDay->setEnabled(true);
      if(m_pLabelDate->isChecked()) m_pTimeLabel->setEnabled(true);
      if(m_pLabelDate->isChecked()) m_pHour->setEnabled(true);
      if(m_pLabelDate->isChecked()) m_pMinute->setEnabled(true);
   }
   else {
      m_pRevision->setEnabled(false);
      m_pTagChoice->setEnabled(false);
      m_pDateLabel->setEnabled(false);
      m_pYear->setEnabled(false);
      m_pMonth->setEnabled(false);
      m_pDay->setEnabled(false);
      m_pTimeLabel->setEnabled(false);
      m_pHour->setEnabled(false);
      m_pMinute->setEnabled(false);
   }
}

//------------------------------------------------------------------------------

QString TagOperationControl::cvsParameter()
{
   if(!m_pCheckBox->isChecked()) {
      return QString::null;
   }
   else {
      if(m_pLabelRev->isChecked()) {
         return ("-r " + m_pRevision->text());
      }
      else
      if(m_pLabelTag->isChecked()) {
         return ("-r " + m_pTagChoice->currentText());
      }
      else
      if(m_pLabelDate->isChecked()) {
         m_pYear->validate();
         m_pMonth->validate();
         m_pDay->validate();
         QString month = m_pMonth->text().simplifyWhiteSpace();
         if(month.length() < 2) month.prepend("0");
         QString day = m_pDay->text().simplifyWhiteSpace();
         if(day.length() < 2) day.prepend("0");
      
         QString res;
         res = "-D \"" + m_pYear->text() + "-" + month + "-" + day;
         if(!m_pHour->text().isEmpty()) {
            m_pHour->validate();
            m_pMinute->validate();
            QString hour = m_pHour->text().simplifyWhiteSpace();
            if(hour.length() < 2) hour.prepend("0");
            if(hour.length() < 2) hour.prepend("0");
            QString minute = m_pMinute->text().simplifyWhiteSpace();
            if(minute.length() < 2) minute.prepend("0");
            if(minute.length() < 2) minute.prepend("0");
            res += " " + hour + ":" + minute;
         }
         res += "\"";
         
         return res;
      }
      else
      if(m_pLabelSticky->isChecked()) {
         return "-A ";
      }
   }

   return QString::null;
}

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------

CheckoutPropertiesControl::CheckoutPropertiesControl( QWidget *parent, const QString& checkBoxLabel,
						      bool rwEnabled /*= false*/, const char *name)
  : QWidget(parent, name)
{
   QBoxLayout *topLayer = new QVBoxLayout(this, 5);

   m_pCheckBox = new QCheckBox(checkBoxLabel, this);
   m_pCheckBox->setChecked(rwEnabled);
//    connect(m_pCheckBox, SIGNAL(toggled(bool)), this, SLOT(enableControls(bool)));
   topLayer->addWidget(m_pCheckBox);

}

//------------------------------------------------------------------------------

bool CheckoutPropertiesControl::rwParameter()
{
   if(!m_pCheckBox->isChecked()) {
      return false;
   }
   else {
     return true;
   }
   return false;
}

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------

