#include "widgetcomboboxpopuptable.h"
#include "ui_widgetcomboboxpopuptable.h"
#include "frameless.h"
#include "mapplication.h"


//#include <QScreen>
#include <QGraphicsDropShadowEffect>
#include <QScreen>
#include <QSqlQueryModel>

widgetComboboxPopupTable::widgetComboboxPopupTable(QWidget *parent) :
    QWidget(parent)//,ui(new Ui::widgetComboboxPopupTable)
{
    //setWindowFlags(Qt::FramelessWindowHint);
    setupUi(this);



    /*
    QGraphicsDropShadowEffect *windowShadow = new QGraphicsDropShadowEffect;
    windowShadow->setBlurRadius(9.0);
    windowShadow->setColor(palette().color(QPalette::Highlight));
    windowShadow->setOffset(0.0);
    setGraphicsEffect(windowShadow);
    */

    mMultiselect = false;

    //mModel = new QSqlQueryModel(this);
    connect(dApp, &QApplication::focusChanged, this, &widgetComboboxPopupTable::on_focusChanged);

    FrameLess(this);
}

widgetComboboxPopupTable::~widgetComboboxPopupTable()
{
    delete mModel;
}

void widgetComboboxPopupTable::setModel(QSqlQueryModel *aModel)
{
    mModel =  aModel;
    tableView->setModel(mModel);
}

void widgetComboboxPopupTable::setQueryEXEC(QString command)
{
    dApp->Enterprise().open();
    mModel->setQuery(command, dApp->Enterprise());
    dApp->Enterprise().close();
}

void widgetComboboxPopupTable::on_tableView_doubleClicked(const QModelIndex &index)
{
    SelectValues();
}

void widgetComboboxPopupTable::on_buttonSelect_released()
{
    SelectValues();
}

void widgetComboboxPopupTable::SelectValues()
{
    QStringList values;
    //QModelIndex index = tableView->currentIndex();

    QModelIndexList indexList = tableView->selectionModel()->selectedRows(2);
    int row;
    foreach (QModelIndex index, indexList)
    {
        row = index.row();
        values << index.data().toString();
    }

    emit onSelect(values);
    hide();
}

void widgetComboboxPopupTable::on_focusChanged(QWidget *old, QWidget *now)
{
    Q_UNUSED(old);
    if (now != this && !this->isAncestorOf(now))
    {
        hide();
    }
}
