// -*- c++ -*-
/***************************************************************************
                           channelpropertiesdialogimpl.cpp
                           -------------------------------
    begin                : Sat Apr 10 2004
    copyright            : (C) 2004 by Dirk Ziegelmeier
    email                : dziegel@gmx.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 Steet, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

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

#include <qcheckbox.h>
#include <qstring.h>
#include <qcombobox.h>
#include <qlcdnumber.h>
#include <qlineedit.h>
#include <qpushbutton.h>

#include "channel.h"
#include "channelpropertiesdialogimpl.h"
#include "kdetv.h"
#include "sourcemanager.h"


ChannelPropertiesDialogImpl::ChannelPropertiesDialogImpl( Channel* channel,
                                                          SourceManager* srcm,
                                                          QWidget* parent, 
                                                          const char* name,
                                                          bool modal,
                                                          Qt::WFlags fl )
    : QDialog(parent),
      _c(channel),
      _srcm(srcm)
{
    setupUi(this);
    setObjectName(name);
    setModal(modal);
    setWindowFlags(fl);
    _enabled->setChecked(_c->enabled());
    _number->display(_c->number());
    _name->setText(_c->name());
    _freq->setValue(((double)_c->getChannelProperty("frequency").toULongLong())/1000);
    _url->setText(_c->url());
    _description->setText(_c->description());
    _globalControls->setChecked(!_c->hasControls(_srcm->device()));

    // add sources to listbox
    _source->insertItems(0, _srcm->sourcesFor(_srcm->device()));
    // select current source
    for (int i = 0; i < _source->count(); i++) {
        if (_c->getChannelProperty("source").toString() == _source->itemText(i)) {
            _source->setCurrentIndex(i);
            break;
        }
    }

    // add norms to listbox
    _encoding->insertItems(0, _srcm->encodingsFor(_srcm->device()));
    // select current norm
    for (int i = 0; i < _encoding->count(); i++) {
        if (_c->getChannelProperty("encoding").toString() == _encoding->itemText(i)) {
            _encoding->setCurrentIndex(i);
            break;
        }
    }

    connect(_freq, SIGNAL( valueChanged(double) ),
            this,  SLOT( freqChanged(double) ));
    connect(_source, SIGNAL( activated(const QString&) ),
            _srcm,   SLOT( setSource(const QString&) ));
    connect(_encoding, SIGNAL( activated(const QString&) ),
            _srcm,     SLOT( setEncoding(const QString&) ));
    connect(_freqUp, SIGNAL( clicked() ),
            this,    SLOT( freqUp() ));
    connect(_freqDown, SIGNAL( clicked() ),
            this,      SLOT( freqDown() ));
    connect(_guess, SIGNAL( clicked() ),
            this,   SLOT( guessClicked() ));
}

ChannelPropertiesDialogImpl::~ChannelPropertiesDialogImpl()
{
}

void ChannelPropertiesDialogImpl::freqChanged(double f)
{
    _srcm->setFrequency((long)(f*1000));
}

void ChannelPropertiesDialogImpl::freqUp()
{
    _freq->setValue( qMin(_freq->value()+0.25, 1000.0) );
}

void ChannelPropertiesDialogImpl::freqDown()
{
    _freq->setValue( qMax(_freq->value()-0.25, 0.0) );
}

void ChannelPropertiesDialogImpl::guessClicked()
{
    _url->setText("http://www." + _name->text().remove(" ") + ".com");
}

void ChannelPropertiesDialogImpl::accept()
{
    _c->updateValues( _name->text(), _c->number(), _enabled->isChecked() );
    _c->setChannelProperty("frequency", (qulonglong)(_freq->value()*1000));
    _c->setChannelProperty("source", _source->currentText());
    _c->setChannelProperty("encoding", _encoding->currentText());
    _c->setDescription(_description->text());
    _c->setURL(_url->text());
    _c->setHasControls(_srcm->device(), !_globalControls->isChecked());
    QDialog::accept();
    emit accepted();
}

void ChannelPropertiesDialogImpl::reject()
{
    QDialog::reject();
    emit rejected();
}

#include "channelpropertiesdialogimpl.moc"
