/*
    Rosegarden
    A MIDI and audio sequencer and musical notation editor.
 
    This program is Copyright 2000-2008
        Guillaume Laurent   <glaurent@telegraph-road.org>,
        Chris Cannam        <cannam@all-day-breakfast.com>,
        Richard Bown        <richard.bown@ferventsoftware.com>
 
    The moral rights of Guillaume Laurent, Chris Cannam, and Richard
    Bown to claim authorship of this work have been asserted.
 
    Other copyrights also apply to some parts of this work.  Please
    see the AUTHORS file and individual file headers for details.
 
    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.  See the file
    COPYING included with this distribution for more information.
*/


#include "LatencyConfigurationPage.h"
#include <tqlayout.h>

#include "document/ConfigGroups.h"
#include "ConfigurationPage.h"
#include "document/RosegardenGUIDoc.h"
#include "TabbedConfigurationPage.h"
#include <tdeconfig.h>
#include <tqframe.h>
#include <tqlabel.h>
#include <tqpushbutton.h>
#include <tqslider.h>
#include <tqstring.h>
#include <tqtabwidget.h>
#include <tqwidget.h>


namespace Rosegarden
{

LatencyConfigurationPage::LatencyConfigurationPage(RosegardenGUIDoc *doc,
        TDEConfig *cfg,
        TQWidget *parent,
        const char *name)
        : TabbedConfigurationPage(doc, cfg, parent, name)
{
    //     Configuration &config = doc->getConfiguration();
    m_cfg->setGroup(LatencyOptionsConfigGroup);

#ifdef NOT_DEFINED
#ifdef HAVE_LIBJACK

    frame = new TQFrame(m_tabWidget, i18n("JACK latency"));
    layout = new TQGridLayout(frame, 6, 5, 10, 10);

    layout->addMultiCellWidget(new TQLabel(i18n("Use the \"Fetch JACK latencies\" button to discover the latency values set at\nthe sequencer.  It's recommended that you use the returned values but it's also\npossible to override them manually using the sliders.  Note that if you change\nyour JACK server parameters you should always fetch the latency values again.\nThe latency values will be stored by Rosegarden for use next time."), frame),
                               0, 0,
                               0, 3);

    layout->addWidget(new TQLabel(i18n("JACK playback latency (in ms)"), frame), 1, 0);
    layout->addWidget(new TQLabel(i18n("JACK record latency (in ms)"), frame), 3, 0);

    m_fetchLatencyValues = new TQPushButton(i18n("Fetch JACK latencies"),
                                           frame);

    layout->addWidget(m_fetchLatencyValues, 1, 3);

    connect(m_fetchLatencyValues, TQ_SIGNAL(released()),
            TQ_SLOT(slotFetchLatencyValues()));

    int jackPlaybackValue = (m_cfg->readLongNumEntry(
                                 "jackplaybacklatencyusec", 0) / 1000) +
                            (m_cfg->readLongNumEntry(
                                 "jackplaybacklatencysec", 0) * 1000);

    m_jackPlayback = new TQSlider(Horizontal, frame);
    m_jackPlayback->setTickmarks(TQSlider::Below);
    layout->addMultiCellWidget(m_jackPlayback, 3, 3, 2, 3);

    TQLabel *jackPlaybackLabel = new TQLabel(TQString("%1").arg(jackPlaybackValue),
                                           frame);
    layout->addWidget(jackPlaybackLabel, 2, 2, TQt::AlignHCenter);
    connect(m_jackPlayback, TQ_SIGNAL(valueChanged(int)),
            jackPlaybackLabel, TQ_SLOT(setNum(int)));

    m_jackPlayback->setMinValue(0);
    layout->addWidget(new TQLabel("0", frame), 3, 1, TQt::AlignRight);

    m_jackPlayback->setMaxValue(500);
    layout->addWidget(new TQLabel("500", frame), 3, 4, TQt::AlignLeft);

    m_jackPlayback->setValue(jackPlaybackValue);

    int jackRecordValue = (m_cfg->readLongNumEntry(
                               "jackrecordlatencyusec", 0) / 1000) +
                          (m_cfg->readLongNumEntry(
                               "jackrecordlatencysec", 0) * 1000);

    m_jackRecord = new TQSlider(Horizontal, frame);
    m_jackRecord->setTickmarks(TQSlider::Below);
    layout->addMultiCellWidget(m_jackRecord, 5, 5, 2, 3);

    TQLabel *jackRecordLabel = new TQLabel(TQString("%1").arg(jackRecordValue),
                                         frame);
    layout->addWidget(jackRecordLabel, 4, 2, TQt::AlignHCenter);
    connect(m_jackRecord, TQ_SIGNAL(valueChanged(int)),
            jackRecordLabel, TQ_SLOT(setNum(int)));

    m_jackRecord->setMinValue(0);
    layout->addWidget(new TQLabel("0", frame), 5, 1, TQt::AlignRight);

    m_jackRecord->setMaxValue(500);
    m_jackRecord->setValue(jackRecordValue);
    layout->addWidget(new TQLabel("500", frame), 5, 4, TQt::AlignLeft);

    addTab(frame, i18n("JACK Latency"));
#endif  // HAVE_LIBJACK
#endif // NOT_DEFINED

}

void LatencyConfigurationPage::apply()
{
    m_cfg->setGroup(LatencyOptionsConfigGroup);

#ifdef HAVE_LIBJACK

    int jackPlayback = getJACKPlaybackValue();
    m_cfg->writeEntry("jackplaybacklatencysec", jackPlayback / 1000);
    m_cfg->writeEntry("jackplaybacklatencyusec", jackPlayback * 1000);

    int jackRecord = getJACKRecordValue();
    m_cfg->writeEntry("jackrecordlatencysec", jackRecord / 1000);
    m_cfg->writeEntry("jackrecordlatencyusec", jackRecord * 1000);

#endif  // HAVE_LIBJACK
}

void LatencyConfigurationPage::slotFetchLatencyValues()
{
    int jackPlaybackValue = m_doc->getAudioPlayLatency().msec() +
                            m_doc->getAudioPlayLatency().sec * 1000;

    m_jackPlayback->setValue(jackPlaybackValue);

    int jackRecordValue = m_doc->getAudioRecordLatency().msec() +
                          m_doc->getAudioRecordLatency().sec * 1000;
    m_jackRecord->setValue(jackRecordValue);
}

}
#include "LatencyConfigurationPage.moc"
