//-----------------------------------------------------------------------------
// File: PortTagEditorDelegate.cpp
//-----------------------------------------------------------------------------
// Project: Kactus2
// Author: Mikko Teuho
// Date: 20.07.2015
//
// Description:
// Delegate for selecting and creating tags for ports.
//-----------------------------------------------------------------------------

#include "PortTagEditorDelegate.h"

#include <QComboBox>

//-----------------------------------------------------------------------------
// Function: PortTagEditorDelegate::PortTagEditorDelegate()
//-----------------------------------------------------------------------------
PortTagEditorDelegate::PortTagEditorDelegate(QStringList existingPortTags, QObject* parent):
QStyledItemDelegate(parent),
existingTags_(existingPortTags)
{

}

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

}

//-----------------------------------------------------------------------------
// Function: PortTagEditorDelegate::createEditor()
//-----------------------------------------------------------------------------
QWidget* PortTagEditorDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& /*option*/,
    QModelIndex const& /*index*/) const
{
    QComboBox* tagCombo = new QComboBox(parent);
    tagCombo->setEditable(true);
    tagCombo->addItems(existingTags_);

    return tagCombo;
}

//-----------------------------------------------------------------------------
// Function: PortTagEditorDelegate::setEditorData()
//-----------------------------------------------------------------------------
void PortTagEditorDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const
{
    QString text = index.model()->data(index, Qt::DisplayRole).toString();

    QComboBox* tagCombo = qobject_cast<QComboBox*>(editor);

    if (text != QLatin1String("Double click to add new item."))
    {
        tagCombo->setCurrentText(text);
    }
    else
    {
        tagCombo->setCurrentText(QString());
    }
}

//-----------------------------------------------------------------------------
// Function: PortTagEditorDelegate::setModelData()
//-----------------------------------------------------------------------------
void PortTagEditorDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const
{
    QComboBox* tagCombo = qobject_cast<QComboBox*>(editor);
    QString text = tagCombo->currentText();
    model->setData(index, text, Qt::EditRole);
}
