/***************************************************************************
 *   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 "jigsawsettings.h"
#include "kverbossettings.h"


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

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

void MyListWidget::setImages(QStringList imageList)
{
    addItems ( imageList );
}





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

JigsawSettings::~JigsawSettings()
{
}

void JigsawSettings::setupUi(QWidget *JigsawSettings)
{
    if (JigsawSettings->objectName().isEmpty())
        JigsawSettings->setObjectName(QString::fromUtf8("JigsawSettings"));
    QSize size(386, 402);
    size = size.expandedTo(JigsawSettings->minimumSizeHint());
    JigsawSettings->resize(size);
    gridLayout = new QGridLayout(JigsawSettings);
    gridLayout->setObjectName(QString::fromUtf8("gridLayout"));
    kcfg_showJigsaw = new QCheckBox(JigsawSettings);
    kcfg_showJigsaw->setObjectName(QString::fromUtf8("kcfg_showJigsaw"));

    gridLayout->addWidget(kcfg_showJigsaw, 0, 0, 1, 1);

    groupBoxJigsawSettings = new QGroupBox(JigsawSettings);
    groupBoxJigsawSettings->setObjectName(QString::fromUtf8("groupBoxJigsawSettings"));
    gridLayout1 = new QGridLayout(groupBoxJigsawSettings);
    gridLayout1->setObjectName(QString::fromUtf8("gridLayout1"));
    labelHorizontal = new QLabel(groupBoxJigsawSettings);
    labelHorizontal->setObjectName(QString::fromUtf8("labelHorizontal"));

    gridLayout1->addWidget(labelHorizontal, 0, 0, 1, 1);

    kcfg_horizontalParts = new QSpinBox(groupBoxJigsawSettings);
    kcfg_horizontalParts->setObjectName(QString::fromUtf8("kcfg_horizontalParts"));
    kcfg_horizontalParts->setMinimum(4);
    kcfg_horizontalParts->setMaximum(12);
    kcfg_horizontalParts->setValue(9);

    gridLayout1->addWidget(kcfg_horizontalParts, 0, 1, 1, 1);

    spacerItem = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);

    gridLayout1->addItem(spacerItem, 0, 2, 1, 2);

    labelVertical = new QLabel(groupBoxJigsawSettings);
    labelVertical->setObjectName(QString::fromUtf8("labelVertical"));

    gridLayout1->addWidget(labelVertical, 1, 0, 1, 1);

    kcfg_verticalParts = new QSpinBox(groupBoxJigsawSettings);
    kcfg_verticalParts->setObjectName(QString::fromUtf8("kcfg_verticalParts"));
    kcfg_verticalParts->setMinimum(4);
    kcfg_verticalParts->setMaximum(12);
    kcfg_verticalParts->setValue(6);

    gridLayout1->addWidget(kcfg_verticalParts, 1, 1, 1, 1);

    spacerItem1 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);

    gridLayout1->addItem(spacerItem1, 1, 2, 1, 2);

    //kcfg_imageList = new KListWidget(groupBoxJigsawSettings);
    kcfg_imageList = new MyListWidget(groupBoxJigsawSettings);
    kcfg_imageList->setObjectName(QString::fromUtf8("kcfg_imageList"));
    kcfg_imageList->setSelectionMode( QAbstractItemView::MultiSelection );

    gridLayout1->addWidget(kcfg_imageList, 2, 0, 1, 4);

    pushButtonDelete = new QPushButton(groupBoxJigsawSettings);
    pushButtonDelete->setObjectName(QString::fromUtf8("pushButtonDelete"));

    gridLayout1->addWidget(pushButtonDelete, 3, 0, 1, 1);

    spacerItem2 = new QSpacerItem(191, 25, QSizePolicy::Expanding, QSizePolicy::Minimum);

    gridLayout1->addItem(spacerItem2, 3, 1, 1, 2);

    pushButtonAdd = new QPushButton(groupBoxJigsawSettings);
    pushButtonAdd->setObjectName(QString::fromUtf8("pushButtonAdd"));

    gridLayout1->addWidget(pushButtonAdd, 3, 3, 1, 1);

    gridLayout->addWidget(groupBoxJigsawSettings, 1, 0, 1, 1);

    retranslateUi(JigsawSettings);

    QMetaObject::connectSlotsByName(JigsawSettings);
}

void JigsawSettings::retranslateUi(QWidget *JigsawSettings)
{
    JigsawSettings->setWindowTitle(tr2i18n("Form", 0));
    kcfg_showJigsaw->setText(tr2i18n("Show the jigsaw", 0));
    groupBoxJigsawSettings->setTitle(tr2i18n("Jigsaw Settings", 0));
    labelHorizontal->setText(tr2i18n("Tiles in a row", 0));
    labelVertical->setText(tr2i18n("Tiles in a column", 0));
    pushButtonDelete->setText(tr2i18n("Delete", 0));
    pushButtonAdd->setText(tr2i18n("Add", 0));
    Q_UNUSED(JigsawSettings);
}

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

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


#include "jigsawsettings.moc"
