//-----------------------------------------------------------------------------
// File: filesdelegate.cpp
//-----------------------------------------------------------------------------
// Project: Kactus2
// Author: Antti Kamppi
// Date: 6.6.2012
//
// Description:
// Delegate for creating editors for files.
//-----------------------------------------------------------------------------

#include "filesdelegate.h"
#include "filetypeeditordelegate.h"
#include "FileColumns.h"

#include <common/widgets/listManager/listeditor.h>

#include <QComboBox>
#include <QLineEdit>
#include <QTextEdit>

//-----------------------------------------------------------------------------
// Function: FilesDelegate::FilesDelegate()
//-----------------------------------------------------------------------------
FilesDelegate::FilesDelegate(Document::Revision docRevision, QObject *parent):
MultilineDescriptionDelegate(parent),
revision_(docRevision)
{

}


//-----------------------------------------------------------------------------
// Function: FilesDelegate::~FilesDelegate()
//-----------------------------------------------------------------------------
FilesDelegate::~FilesDelegate()
{

}

//-----------------------------------------------------------------------------
// Function: FilesDelegate::createEditor()
//-----------------------------------------------------------------------------
QWidget* FilesDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, QModelIndex const& index) const
{
	if (index.column() == FileColumns::NAME_COLUMN)// || index.column() == FileColumns::PATH_COLUMN)
	{
        return 0;
	}
    else if (index.column() == FileColumns::TYPES_COLUMN)
    {
        // create the editor
        ListEditor* listEditor = new ListEditor(parent);
        listEditor->setMinimumHeight(FilesDelegate::LIST_EDITOR_MIN_HEIGHT);

        // create the delegate for the editor
        FileTypeEditorDelegate* delegate = new FileTypeEditorDelegate(revision_, parent);
        listEditor->setItemDelegate(delegate);

        return listEditor;
    }
    else 
    {
        return MultilineDescriptionDelegate::createEditor(parent, option, index);
    }
}

//-----------------------------------------------------------------------------
// Function: FilesDelegate::setEditorData()
//-----------------------------------------------------------------------------
void FilesDelegate::setEditorData(QWidget* editor, QModelIndex const& index ) const
{
    if (index.column() == FileColumns::NAME_COLUMN)// || index.column() == FileColumns::PATH_COLUMN)
    {
        return;
    }
    if (index.column() == FileColumns::TYPES_COLUMN)
    {
        ListEditor* listEditor = qobject_cast<ListEditor*>(editor);
        Q_ASSERT(listEditor);
        QStringList groups = index.data(Qt::DisplayRole).toString().split("; ");
        listEditor->setItems(groups);
    }
    else
    {
        MultilineDescriptionDelegate::setEditorData(editor, index);
    }
}

//-----------------------------------------------------------------------------
// Function: FilesDelegate::setModelData()
//-----------------------------------------------------------------------------
void FilesDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, QModelIndex const& index) const
{
	if (index.column() == FileColumns::NAME_COLUMN)// || index.column() == FileColumns::PATH_COLUMN)
    {
        return;
    }
    else if (index.column() == FileColumns::TYPES_COLUMN)
    {
        ListEditor* listEditor = qobject_cast<ListEditor*>(editor);
        Q_ASSERT(listEditor);

        QStringList fileTypes = listEditor->items();
        model->setData(index, fileTypes.join("; "), Qt::EditRole);
    }
    else
    {
        MultilineDescriptionDelegate::setModelData(editor, model, index);
    }
}

//-----------------------------------------------------------------------------
// Function: FilesDelegate::descriptionColumn()
//-----------------------------------------------------------------------------
int FilesDelegate::descriptionColumn() const
{
    return FileColumns::DESCRIPTION;
}

//-----------------------------------------------------------------------------
// Function: FilesDelegate::commitAndCloseEditor()
//-----------------------------------------------------------------------------
void FilesDelegate::commitAndCloseEditor()
{
	QWidget* edit = qobject_cast<QWidget*>(sender());
	Q_ASSERT(edit);

	emit commitData(edit);
	emit closeEditor(edit);
}
