/***************************************************************************
 *   Copyright (C) 2007 by Arnold Kraschinski                              *
 *   arnold.kra67@gmx.de                                                   *
 *                                                                         *
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 *   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 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.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.          *
 ***************************************************************************/

// Qt header files
#include <QCheckBox>
#include <QRadioButton>
#include <QSpinBox>
#include <QFileDialog>

#include <QWidget>
#include <QButtonGroup>
#include <QCheckBox>
#include <QGridLayout>
#include <QGroupBox>
#include <QLabel>
#include <QPushButton>
#include <QSpacerItem>
#include <QSpinBox>

// KDE header files
#include <kdebug.h>
#include <kconfig.h>
#include <klocale.h>
#include <kstandarddirs.h>
#include <kconfigdialog.h>
#include <kmessagebox.h>
#include <kfiledialog.h>
#include <kurl.h>
#include "klistwidget.h"

// project header files
#include "kvtmlsettings.h"
#include "kverbossettings.h"


MyFileListWidget::MyFileListWidget( QWidget* parent )
:KListWidget(parent)
{
}

QStringList MyFileListWidget::files()
{
    QStringList list;
    for (int i = 0; i < count(); i++) {
        list << item(i)->text();
    }
    return list;
}

void MyFileListWidget::setFiles(QStringList filesList)
{
    addItems ( filesList );
}





KvtmlSettings::KvtmlSettings(QWidget *parent) : QWidget(parent)
{
    setupUi( this );
    m_parent = (KConfigDialog*)parent;
    connect ( pushButtonDelete, SIGNAL( pressed() ), this, SLOT( slotDelete() ) );
    connect ( pushButtonAdd, SIGNAL( pressed() ), this, SLOT( slotAdd() ) );
}

KvtmlSettings::~KvtmlSettings()
{
}

void KvtmlSettings::setupUi(QWidget *KvtmlSettings)
{
    if (KvtmlSettings->objectName().isEmpty())
        KvtmlSettings->setObjectName(QString::fromUtf8("KvtmlSettings"));
    QSize size(386, 402);
    size = size.expandedTo(KvtmlSettings->minimumSizeHint());
    KvtmlSettings->resize(size);
    // gridLayout is the layout for the whole widget
    // In gridLayout will be three rows:
    // 1.     kcfg_loadKVTML - a QCheckBox
    // 2.     groupBoxKvtmlSettings - a QGroupBox
    // 3.     groupBoxTranslation - anouther QGroupBox
    gridLayout = new QGridLayout(KvtmlSettings);
    gridLayout->setObjectName(QString::fromUtf8("gridLayout"));
    
    // 1. ...
    kcfg_loadKVTML = new QCheckBox(KvtmlSettings);
    kcfg_loadKVTML->setObjectName(QString::fromUtf8("kcfg_loadKVTML"));
    gridLayout->addWidget(kcfg_loadKVTML, 0, 0, 1, 1);

    // 2. ...
    // The groupBoxKvtmlSettings has a QGridLayout gridLayout1
    // gridLayout1 contains:
    // 2.1.     kcfg_fileList - a MyFileListWidget which uses three columns
    // 2.2.     pushButtonDelete, a spacer, pushButtonAdd - they use 1 column each
    groupBoxKvtmlSettings = new QGroupBox(KvtmlSettings);
    groupBoxKvtmlSettings->setObjectName(QString::fromUtf8("groupBoxKvtmlSettings"));
    gridLayoutFiles = new QGridLayout(groupBoxKvtmlSettings);
    gridLayoutFiles->setObjectName(QString::fromUtf8("gridLayoutFiles"));

    // 2.1. ...
    kcfg_fileList = new MyFileListWidget(groupBoxKvtmlSettings);
    kcfg_fileList->setObjectName(QString::fromUtf8("kcfg_fileList"));
    kcfg_fileList->setSelectionMode( QAbstractItemView::MultiSelection );
    gridLayoutFiles->addWidget(kcfg_fileList, 0, 0, 1, 3);

    // 2.2. ...
    pushButtonDelete = new QPushButton(groupBoxKvtmlSettings);
    pushButtonDelete->setObjectName(QString::fromUtf8("pushButtonDelete"));
    gridLayoutFiles->addWidget(pushButtonDelete, 1, 0, 1, 1);

    spacerItem2 = new QSpacerItem(191, 25, QSizePolicy::Expanding, QSizePolicy::Minimum);
    gridLayoutFiles->addItem(spacerItem2, 1, 1, 1, 1);

    pushButtonAdd = new QPushButton(groupBoxKvtmlSettings);
    pushButtonAdd->setObjectName(QString::fromUtf8("pushButtonAdd"));
    gridLayoutFiles->addWidget(pushButtonAdd, 1, 2, 1, 1);

    gridLayout->addWidget(groupBoxKvtmlSettings, 1, 0, 1, 1);
    
    // 3. ...
    // The groupBoxTranslation has a QGridLayout gridLayoutTranslation
    // In the gridLayoutTranslation there are three rows
    // 3.1.         kcfg_translatedVerbsA - QRadioButton
    // 3.2.         kcfg_translatedVerbsF - QRadioButton
    // 3.3.         kcfg_translatedVerbsS - QRadioButton
    groupBoxTranslation = new QGroupBox(KvtmlSettings);
    groupBoxTranslation->setObjectName(QString::fromUtf8("groupBoxTranslation"));
    gridLayoutTranslation = new QGridLayout(groupBoxTranslation);
    gridLayoutTranslation->setObjectName(QString::fromUtf8("gridLayoutTranslation"));
    gridLayoutTranslation->setHorizontalSpacing(6);
    gridLayoutTranslation->setVerticalSpacing(6);
    gridLayoutTranslation->setContentsMargins(9, 9, 9, 9);
    // 3.1. ...
    kcfg_translatedVerbsA = new QRadioButton(groupBoxTranslation);
    kcfg_translatedVerbsA->setObjectName(QString::fromUtf8("kcfg_translatedVerbsA"));
    gridLayoutTranslation->addWidget(kcfg_translatedVerbsA, 2, 0, 1, 1);
    // 3.2. ...
    kcfg_translatedVerbsF = new QRadioButton(groupBoxTranslation);
    kcfg_translatedVerbsF->setObjectName(QString::fromUtf8("kcfg_translatedVerbsF"));
    gridLayoutTranslation->addWidget(kcfg_translatedVerbsF, 1, 0, 1, 1);
    // 3.3. ...
    kcfg_translatedVerbsS = new QRadioButton(groupBoxTranslation);
    kcfg_translatedVerbsS->setObjectName(QString::fromUtf8("kcfg_translatedVerbsS"));
    gridLayoutTranslation->addWidget(kcfg_translatedVerbsS, 0, 0, 1, 1);


    gridLayout->addWidget(groupBoxTranslation, 2, 0, 1, 1);
    // --

    retranslateUi(KvtmlSettings);

    QMetaObject::connectSlotsByName(KvtmlSettings);
}

void KvtmlSettings::retranslateUi(QWidget *KvtmlSettings)
{
    KvtmlSettings->setWindowTitle(tr2i18n("Form", 0));
    kcfg_loadKVTML->setText(tr2i18n("Load KVTML-files to Search Translations for Verbs", 0));
    groupBoxKvtmlSettings->setTitle(tr2i18n("List of KVTML files to load", 0));
    pushButtonDelete->setText(tr2i18n("Delete", 0));
    pushButtonAdd->setText(tr2i18n("Add", 0));
    
    groupBoxTranslation->setTitle(tr2i18n("Translated Verbs", 0));
    kcfg_translatedVerbsA->setText(tr2i18n("Mix the Spanish and foreign infinitives if possible", 0));
    kcfg_translatedVerbsF->setText(tr2i18n("Show always the foreign infinitive if possible", 0));
    kcfg_translatedVerbsS->setText(tr2i18n("Show always the Spanish infinitive", 0));
    
    Q_UNUSED(KvtmlSettings);
}

void KvtmlSettings::slotDelete()
{
    QList<QListWidgetItem*> items = kcfg_fileList->selectedItems();
    for (QList<QListWidgetItem*>::iterator it = items.begin(); it < items.end(); it++) {
        QListWidgetItem* item = kcfg_fileList->takeItem( kcfg_fileList->row (*it) );
        if (item != 0) {
            delete item;
            item = 0;
        }
    }
    m_parent->enableButtonApply(true);
}

void KvtmlSettings::slotAdd()
{
    QStringList list = KFileDialog::getOpenFileNames();
    
    //QStringList list = QFileDialog::getOpenFileNames ();
    kcfg_fileList->addItems ( list );
    m_parent->enableButtonApply(true);
}


#include "kvtmlsettings.moc"
