/*
    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 "NameSetEditor.h"
#include "BankEditorDialog.h"
#include <kcompletion.h>
#include <tdeglobalsettings.h>
#include <klineedit.h>
#include <tdelocale.h>
#include <tqframe.h>
#include <tqgroupbox.h>
#include <tqhbox.h>
#include <tqlabel.h>
#include <tqlayout.h>
#include <tqpushbutton.h>
#include <tqstring.h>
#include <tqtabwidget.h>
#include <tqtooltip.h>
#include <tqvbox.h>
#include <tqvgroupbox.h>
#include <tqwidget.h>
#include <iostream>

namespace Rosegarden
{

NameSetEditor::NameSetEditor(BankEditorDialog* bankEditor,
                             TQString title,
                             TQWidget* parent,
                             const char* name,
                             TQString headingPrefix,
                             bool showEntryButtons)
        : TQVGroupBox(title, parent, name),
        m_bankEditor(bankEditor),
        m_mainFrame(new TQFrame(this))
{
    m_mainLayout = new TQGridLayout(m_mainFrame,
                                   4,   // rows
                                   6,   // cols
                                   2); // margin

    // Librarian
    //
    TQGroupBox *groupBox = new TQGroupBox(2,
                                        TQt::Horizontal,
                                        i18n("Librarian"),
                                        m_mainFrame);
    m_mainLayout->addMultiCellWidget(groupBox, 0, 2, 3, 5);

    new TQLabel(i18n("Name"), groupBox);
    m_librarian = new TQLabel(groupBox);

    new TQLabel(i18n("Email"), groupBox);
    m_librarianEmail = new TQLabel(groupBox);

    TQToolTip::add
        (groupBox,
                i18n("The librarian maintains the Rosegarden device data for this device.\nIf you've made modifications to suit your own device, it might be worth\nliaising with the librarian in order to publish your information for the benefit\nof others."));

    TQTabWidget* tabw = new TQTabWidget(this);

    tabw->setMargin(10);

    TQHBox *h;
    TQVBox *v;
    TQHBox *numBox;

    unsigned int tabs = 4;
    unsigned int cols = 2;
    unsigned int labelId = 0;

    for (unsigned int tab = 0; tab < tabs; ++tab) {
        h = new TQHBox(tabw);

        for (unsigned int col = 0; col < cols; ++col) {
            v = new TQVBox(h);

            for (unsigned int row = 0; row < 128 / (tabs*cols); ++row) {
                numBox = new TQHBox(v);
                TQString numberText = TQString("%1").arg(labelId + 1);

                if (tab == 0 && col == 0 && row == 0) {
                    // Initial label; button to adjust whether labels start at 0 or 1
                    m_initialLabel = new TQPushButton(numberText, numBox);
                    connect(m_initialLabel,
                            TQ_SIGNAL(clicked()),
                            this,
                            TQ_SLOT(slotToggleInitialLabel()));
                } else {
                    TQLabel *label = new TQLabel(numberText, numBox);
                    label->setFixedWidth(40);
                    label->setAlignment(AlignCenter);
                    m_labels.push_back(label);
                }



                if (showEntryButtons) {
                    TQPushButton *button = new TQPushButton("", numBox, numberText.ascii());
                    button->setMaximumWidth(40);
                    button->setMaximumHeight(20);
                    button->setFlat(true);
                    connect(button, TQ_SIGNAL(clicked()),
                            this, TQ_SLOT(slotEntryButtonPressed()));
                    m_entryButtons.push_back(button);
                }

                KLineEdit* lineEdit = new KLineEdit(numBox, numberText.ascii());
                lineEdit->setMinimumWidth(110);
                lineEdit->setCompletionMode(TDEGlobalSettings::CompletionAuto);
                lineEdit->setCompletionObject(&m_completion);
                m_names.push_back(lineEdit);

                connect(m_names[labelId],
                        TQ_SIGNAL(textChanged(const TQString&)),
                        this,
                        TQ_SLOT(slotNameChanged(const TQString&)));

                ++labelId;
            }
        }

        tabw->addTab(h,
                     (tab == 0 ? headingPrefix + TQString(" %1 - %2") :
                      TQString("%1 - %2")).
                     arg(tab * (128 / tabs) + 1).
                     arg((tab + 1) * (128 / tabs)));
    }

    m_initialLabel->setMaximumSize(m_labels.front()->size());
}

void
NameSetEditor::slotToggleInitialLabel()
{
    TQString initial = m_initialLabel->text();

    // strip some unrequested nice-ification.. urg!
    if (initial.startsWith("&")) {
        initial = initial.right(initial.length() - 1);
    }

    bool ok;
    unsigned index = initial.toUInt(&ok);

    if (!ok) {
        std::cerr << "conversion of '"
        << initial.ascii()
        << "' to number failed"
        << std::endl;
        return ;
    }

    if (index == 0)
        index = 1;
    else
        index = 0;

    m_initialLabel->setText(TQString("%1").arg(index++));
    for (std::vector<TQLabel*>::iterator it( m_labels.begin() );
            it != m_labels.end();
            ++it) {
        (*it)->setText(TQString("%1").arg(index++));
    }
}

}
#include "NameSetEditor.moc"
