/*
    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 "LilyPondOptionsDialog.h"
#include "document/io/LilyPondExporter.h"
#include "gui/configuration/HeadersConfigurationPage.h"

#include <tqlayout.h>
#include <tdeapplication.h>

#include "document/ConfigGroups.h"
#include "document/RosegardenGUIDoc.h"
#include "misc/Strings.h"
#include <kcombobox.h>
#include <klineedit.h>
#include <tdeconfig.h>
#include <kdialogbase.h>
#include <tdeglobal.h>
#include <tdelocale.h>
#include <tqcheckbox.h>
#include <tqcombobox.h>
#include <tqframe.h>
#include <tqgroupbox.h>
#include <tqlabel.h>
#include <tqstring.h>
#include <tqtabwidget.h>
#include <tqtooltip.h>
#include <tqvbox.h>
#include <tqwidget.h>
#include <iostream>

namespace Rosegarden
{

LilyPondOptionsDialog::LilyPondOptionsDialog(TQWidget *parent,
	RosegardenGUIDoc *doc,
        TQString windowCaption,
        TQString heading) :
        KDialogBase(parent, 0, true,
                    (windowCaption = "" ? i18n("LilyPond Export/Preview") : windowCaption),
                    Apply | Ok | Cancel),
	m_doc(doc)
{
    setHelp("file-printing");

    TDEConfig *config = kapp->config();
    config->setGroup(NotationViewConfigGroup);

    TQVBox * mainbox = makeVBoxMainWidget();

    //
    // Arrange options in "General" and "Advanced" tabs.
    //

    TQTabWidget * tabWidget = new TQTabWidget(mainbox);

    TQFrame *generalFrame;
    TQFrame *advancedFrame;
    TQGridLayout *generalGrid;
    TQGridLayout *advancedGrid;

    generalFrame = new TQFrame();
    tabWidget->addTab(generalFrame, i18n("General options"));

    generalGrid = new TQGridLayout(generalFrame, 1, 1, 5, 5);

    advancedFrame = new TQFrame();
    tabWidget->addTab(advancedFrame, i18n("Advanced options"));

    advancedGrid = new TQGridLayout(advancedFrame, 1, 1, 5, 5);

    m_headersPage = new HeadersConfigurationPage(this, m_doc);
    tabWidget->addTab(m_headersPage, i18n("Headers"));

    m_headersPage->setSpacing(5);
    m_headersPage->setMargin(5);

    //
    // LilyPond export: Basic options
    //

    TQGroupBox *basicOptionsBox = new TQGroupBox
                           (1, Qt::Horizontal,
                            i18n("Basic options"), generalFrame);
    generalGrid->addWidget(basicOptionsBox, 0, 0);

    TQFrame *frameBasic = new TQFrame(basicOptionsBox);
    TQGridLayout *layoutBasic = new TQGridLayout(frameBasic, 3, 2, 10, 5);

    layoutBasic->addWidget(new TQLabel(
                          i18n("Compatibility level"), frameBasic), 0, 0);

    m_lilyLanguage = new KComboBox(frameBasic);
    // See also setDefaultLilyPondVersion below
    m_lilyLanguage->insertItem(i18n("LilyPond %1").arg("2.6"));
    m_lilyLanguage->insertItem(i18n("LilyPond %1").arg("2.8"));
    m_lilyLanguage->insertItem(i18n("LilyPond %1").arg("2.10"));
    m_lilyLanguage->insertItem(i18n("LilyPond %1").arg("2.12"));
    m_lilyLanguage->setCurrentItem(config->readUnsignedNumEntry("lilylanguage", 0));
    layoutBasic->addWidget(m_lilyLanguage, 0, 1);

    layoutBasic->addWidget(new TQLabel(
                          i18n("Paper size"), frameBasic), 1, 0);

    TQHBoxLayout *hboxPaper = new TQHBoxLayout( frameBasic );
    m_lilyPaperSize = new KComboBox(frameBasic);
    m_lilyPaperSize->insertItem(i18n("A3"));
    m_lilyPaperSize->insertItem(i18n("A4"));
    m_lilyPaperSize->insertItem(i18n("A5"));
    m_lilyPaperSize->insertItem(i18n("A6"));
    m_lilyPaperSize->insertItem(i18n("Legal"));
    m_lilyPaperSize->insertItem(i18n("US Letter"));
    m_lilyPaperSize->insertItem(i18n("Tabloid"));
    m_lilyPaperSize->insertItem(i18n("do not specify"));
    int defaultPaperSize = 1; // A4
    if (TDEGlobal::locale()->country() == "us" || 
        TDEGlobal::locale()->country() == "US") defaultPaperSize = 5; // Letter
    m_lilyPaperSize->setCurrentItem(config->readUnsignedNumEntry
                                    ("lilypapersize", defaultPaperSize));

    m_lilyPaperLandscape = new TQCheckBox(i18n("Landscape"), frameBasic);
    m_lilyPaperLandscape->setChecked(config->readBoolEntry("lilypaperlandscape", false));

    hboxPaper->addWidget( m_lilyPaperSize );
    hboxPaper->addWidget( new TQLabel( " ", frameBasic ) ); // fixed-size spacer
    hboxPaper->addWidget( m_lilyPaperLandscape );
    layoutBasic->addLayout(hboxPaper, 1, 1);

    layoutBasic->addWidget(new TQLabel(
                          i18n("Font size"), frameBasic), 2, 0);

    m_lilyFontSize = new KComboBox(frameBasic);
    int sizes[] = { 11, 13, 16, 19, 20, 23, 26 };
    for (int i = 0; i < sizeof(sizes)/sizeof(sizes[0]); ++i) {
        m_lilyFontSize->insertItem(i18n("%1 pt").arg(sizes[i]));
    }
    m_lilyFontSize->setCurrentItem(config->readUnsignedNumEntry
                                   ("lilyfontsize", 4));
    layoutBasic->addWidget(m_lilyFontSize, 2, 1);

    //
    // LilyPond export: Staff level options
    //

    TQGroupBox *staffOptionsBox = new TQGroupBox
                           (1, Qt::Horizontal,
                            i18n("Staff level options"), generalFrame);
    generalGrid->addWidget(staffOptionsBox, 1, 0);

    TQFrame *frameStaff = new TQFrame(staffOptionsBox);
    TQGridLayout *layoutStaff = new TQGridLayout(frameStaff, 2, 2, 10, 5);

    layoutStaff->addWidget(new TQLabel(
                          i18n("Export content"), frameStaff), 0, 0);

    m_lilyExportSelection = new KComboBox(frameStaff);
    m_lilyExportSelection->insertItem(i18n("All tracks"));
    m_lilyExportSelection->insertItem(i18n("Non-muted tracks"));
    m_lilyExportSelection->insertItem(i18n("Selected track"));
    m_lilyExportSelection->insertItem(i18n("Selected segments"));
    m_lilyExportSelection->setCurrentItem(config->readUnsignedNumEntry("lilyexportselection", 1));

    layoutStaff->addWidget(m_lilyExportSelection, 0, 1);

    m_lilyExportStaffMerge = new TQCheckBox(
                                 i18n("Merge tracks that have the same name"), frameStaff);
    m_lilyExportStaffMerge->setChecked(config->readBoolEntry("lilyexportstaffmerge", false));
    layoutStaff->addMultiCellWidget(m_lilyExportStaffMerge, 1, 1, 0, 1);

    //
    // LilyPond export: Notation options
    //

    TQGroupBox *notationOptionsBox = new TQGroupBox
                           (1, Qt::Horizontal,
                            i18n("Notation options"), generalFrame);
    generalGrid->addWidget(notationOptionsBox, 2, 0);

    TQFrame *frameNotation = new TQFrame(notationOptionsBox);
    TQGridLayout *layoutNotation = new TQGridLayout(frameNotation, 4, 2, 10, 5);

    m_lilyTempoMarks = new KComboBox( frameNotation );
    m_lilyTempoMarks->insertItem(i18n("None"));
    m_lilyTempoMarks->insertItem(i18n("First"));
    m_lilyTempoMarks->insertItem(i18n("All"));
    m_lilyTempoMarks->setCurrentItem(config->readUnsignedNumEntry("lilyexporttempomarks", 0));

    layoutNotation->addWidget( new TQLabel( 
			 i18n("Export tempo marks "), frameNotation), 0, 0 );
    layoutNotation->addWidget(m_lilyTempoMarks, 0, 1);
 
    m_lilyExportLyrics = new TQCheckBox(
                             i18n("Export lyrics"), frameNotation);
    // default to lyric export == false because if you export the default
    // empty "- - -" lyrics, crap results ensue, and people will know if they
    // do need to export the lyrics - DMM
    // fixed, no "- - -" lyrics are generated for an empty lyrics
    // default again into lyrics - HJJ
    m_lilyExportLyrics->setChecked(config->readBoolEntry("lilyexportlyrics", true));
    layoutNotation->addMultiCellWidget(m_lilyExportLyrics, 1, 1, 0, 1);

    m_lilyExportBeams = new TQCheckBox(
                            i18n("Export beamings"), frameNotation);
    m_lilyExportBeams->setChecked(config->readBoolEntry("lilyexportbeamings", false));
    layoutNotation->addMultiCellWidget(m_lilyExportBeams, 2, 2, 0, 1);

    // recycle this for a new option to ignore the track brackets (so it is less
    // obnoxious to print single parts where brackets are in place)
    m_lilyExportStaffGroup = new TQCheckBox(
                                 i18n("Export track staff brackets"), frameNotation);
    m_lilyExportStaffGroup->setChecked(config->readBoolEntry("lilyexportstaffbrackets", true));
    layoutNotation->addMultiCellWidget(m_lilyExportStaffGroup, 3, 3, 0, 1); 

    generalGrid->setRowStretch(4, 10);

    //
    // LilyPond export: Advanced options
    //

    TQGroupBox *advancedLayoutOptionsBox = new TQGroupBox
                           (1, Qt::Horizontal,
                            i18n("Layout options"), advancedFrame);
    advancedGrid->addWidget(advancedLayoutOptionsBox, 0, 0);

    TQFrame *frameAdvancedLayout = new TQFrame(advancedLayoutOptionsBox);
    TQGridLayout *layoutAdvancedLayout = new TQGridLayout(frameAdvancedLayout, 2, 2, 10, 5);

    m_lilyLyricsHAlignment = new KComboBox( frameAdvancedLayout );
    m_lilyLyricsHAlignment->insertItem(i18n("Left"));
    m_lilyLyricsHAlignment->insertItem(i18n("Center"));
    m_lilyLyricsHAlignment->insertItem(i18n("Right"));
    m_lilyLyricsHAlignment->setCurrentItem(config->readUnsignedNumEntry("lilylyricshalignment", 0));

    layoutAdvancedLayout->addWidget(new TQLabel(
                          i18n("Lyrics alignment"), frameAdvancedLayout), 0, 0);
    layoutAdvancedLayout->addWidget(m_lilyLyricsHAlignment, 0, 1);

    m_lilyRaggedBottom = new TQCheckBox(
                           i18n("Ragged bottom (systems will not be spread vertically across the page)"), frameAdvancedLayout);
    m_lilyRaggedBottom->setChecked(config->readBoolEntry("lilyraggedbottom", false));
    layoutAdvancedLayout->addMultiCellWidget(m_lilyRaggedBottom, 1, 2, 0, 1);

    TQGroupBox *miscOptionsBox = new TQGroupBox
                           (1, Qt::Horizontal,
                            i18n("Miscellaneous options"), advancedFrame);
    advancedGrid->addWidget(miscOptionsBox, 1, 0);

    TQFrame *frameMisc = new TQFrame(miscOptionsBox);
    TQGridLayout *layoutMisc = new TQGridLayout(frameMisc, 2, 2, 10, 5);

    m_lilyExportPointAndClick = new TQCheckBox(
                                    i18n("Enable \"point and click\" debugging"), frameMisc);
    m_lilyExportPointAndClick->setChecked(config->readBoolEntry("lilyexportpointandclick", false));
    layoutMisc->addMultiCellWidget(m_lilyExportPointAndClick, 0, 0, 0, 1);

    m_lilyExportMidi = new TQCheckBox(
                           i18n("Export \\midi block"), frameMisc);
    m_lilyExportMidi->setChecked(config->readBoolEntry("lilyexportmidi", false));
    layoutMisc->addMultiCellWidget(m_lilyExportMidi, 1, 1, 0, 1);

    m_lilyMarkerMode = new KComboBox(frameMisc);
    m_lilyMarkerMode->insertItem(i18n("No markers"));
    m_lilyMarkerMode->insertItem(i18n("Rehearsal marks"));
    m_lilyMarkerMode->insertItem(i18n("Marker text"));
    m_lilyMarkerMode->setCurrentItem(config->readUnsignedNumEntry("lilyexportmarkermode", 0));

    layoutMisc->addWidget( new TQLabel( 
                             i18n("Export markers"), frameMisc),2, 0 );
    layoutMisc->addWidget(m_lilyMarkerMode, 2, 1);
    
    advancedGrid->setRowStretch(2, 10);

    resize(minimumSize());
}

void
LilyPondOptionsDialog::slotApply()
{
    TDEConfig *config = kapp->config();
    config->setGroup(NotationViewConfigGroup);

    config->writeEntry("lilylanguage", m_lilyLanguage->currentItem());
    config->writeEntry("lilypapersize", m_lilyPaperSize->currentItem());
    config->writeEntry("lilypaperlandscape", m_lilyPaperLandscape->isChecked());
    config->writeEntry("lilyfontsize", m_lilyFontSize->currentItem());
    config->writeEntry("lilyraggedbottom", m_lilyRaggedBottom->isChecked());
    config->writeEntry("lilyexportlyrics", m_lilyExportLyrics->isChecked());
    config->writeEntry("lilyexportmidi", m_lilyExportMidi->isChecked());
    config->writeEntry("lilyexporttempomarks", m_lilyTempoMarks->currentItem());
    config->writeEntry("lilyexportselection", m_lilyExportSelection->currentItem());
    config->writeEntry("lilyexportpointandclick", m_lilyExportPointAndClick->isChecked());
    config->writeEntry("lilyexportbeamings", m_lilyExportBeams->isChecked());
    config->writeEntry("lilyexportstaffmerge", m_lilyExportStaffMerge->isChecked());
    config->writeEntry("lilyexportstaffbrackets", m_lilyExportStaffGroup->isChecked());
    config->writeEntry("lilylyricshalignment", m_lilyLyricsHAlignment->currentItem());
    config->writeEntry("lilyexportmarkermode", m_lilyMarkerMode->currentItem());
    m_headersPage->apply();
}
 
void
LilyPondOptionsDialog::slotOk()
{
    slotApply();
    accept();
}

void
LilyPondOptionsDialog::setDefaultLilyPondVersion(TQString version)
{
    TDEConfig *config = kapp->config();
    config->setGroup(NotationViewConfigGroup);
    int index = -1;
    bool unstable = false;
    if (version == "2.6" || version.startsWith("2.6.")) {
        index = 0;
    } else if (version == "2.7" || version.startsWith("2.7.")) {
        unstable = true;
        index = 1;
    } else if (version == "2.8" || version.startsWith("2.8.")) {
        index = 1;
    } else if (version == "2.9" || version.startsWith("2.9.")) {
        unstable = true;
        index = 2;
    } else if (version == "2.10" || version.startsWith("2.10.")) {
        index = 2;
    } else if (version == "2.11" || version.startsWith("2.11.")) {
        unstable = true;
        index = 3;
    } else if (version == "2.12" || version.startsWith("2.12.")) {
        index = 3;
    }
    if (unstable) {
        std::cerr << "\nWARNING: Unstable LilyPond version detected, selecting next language version up\n" << std::endl;
    }
    if (index >= 0) {
        config->writeEntry("lilylanguage", index);
    }
}

}
#include "LilyPondOptionsDialog.moc"
