#include "igridform.h"
#include "ui_gridform.h"

#include <QList>

#include "context.h"
#include "filterui.h"

IGridForm::IGridForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::GridForm)
{
    ui->setupUi(this);
    ui->filterWidget->setVisible(false);
    m_contextMenu = new QMenu(this);
    m_contextMenu->addAction(ui->actionSelectColumns);
    m_form = NULL;

    m_columnDialog = new ColumnDialog(this);
    connect(m_columnDialog, SIGNAL(accepted()), this, SLOT(columnsAccepted()));
}

IGridForm::~IGridForm()
{
    delete ui;
}

void IGridForm::setPluginId(const QString &pluginId)
{
    m_pluginId = pluginId;

    if (m_filterUi != NULL)
    {
        m_filterUi->setPluginId(pluginId);
    }

    if (m_form != NULL)
    {
        m_form->setPluginId(pluginId);
    }
}

QString IGridForm::pluginId()
{
    return m_pluginId;
}

QTableView *IGridForm::tableView()
{
    return ui->tableView;
}

void IGridForm::hideColumns(const QList<int> &cols)
{
    foreach (int col, cols) {
        tableView()->setColumnHidden(col, true);
    }
}

QWidget *IGridForm::filterWidget()
{
    return ui->filterWidget;
}


void IGridForm::on_btnNew_clicked()
{
    handleNewRecord();
}

void IGridForm::on_btnEdit_clicked()
{
    handleEditRecord();
}

void IGridForm::on_btnDelete_clicked()
{
    handleDeleteRecord();
}

void IGridForm::on_tableView_customContextMenuRequested(const QPoint &pos)
{
    m_contextMenu->popup(tableView()->viewport()->mapToGlobal(pos));
}

void IGridForm::on_actionSelectColumns_triggered()
{
    m_columnDialog->setTable(tableView());
    m_columnDialog->show();
}

void IGridForm::columnsAccepted()
{
    for(int i = 0; i < tableView()->model()->columnCount(); i++)
    {
        tableView()->setColumnHidden(i, false);
    }

    QList<int> hide = m_columnDialog->columnsToHide();
    hideColumns(hide);

    QList<QVariant> varList;

    foreach (int i, hide) {
        varList.append(i);
    }

    Context::instance().settings()->setValue("grids/" + pluginId() + "/hide", QVariant::fromValue(varList));
}


void IGridForm::on_btnFilter_toggled(bool checked)
{
    if (checked)
    {
        ui->filterWidget->setVisible(true);
    }
    else
    {
        ui->filterWidget->setVisible(false);
    }
}
