#include "dialogopencompany.h"
#include "ui_dialogopencompany.h"
#include "companylistitemdelegate.h"
#include "mapplication.h"

#include <QStandardItemModel>

dialogOpenCompany::dialogOpenCompany(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::dialogOpenCompany)
{
    ui->setupUi(this);
    setAttribute(Qt::WA_DeleteOnClose, true);

    QListView *view = ui->listView;
    model  = new QStandardItemModel();
    CompanyListItemDelegate *listdelegate  = new CompanyListItemDelegate();

    view->setItemDelegate(listdelegate);   //connect the delegate to view
    view->setModel(model);                 //connect the model to view.
}

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

void dialogOpenCompany::setData(QString Name, QString CIF, QString path)
{
    QStandardItem *item = new QStandardItem();
    QIcon icon(":/new/prefix1/about.png");

    item->setData(Name, Qt::DisplayRole);   // Nombre
    item->setData(CIF,  Qt::UserRole);      // CIF
    item->setData(path, Qt::UserRole + 1);  // path
    model->appendRow(item);
}

void dialogOpenCompany::on_buttonOpen_released()
{
    on_listView_doubleClicked(ui->listView->currentIndex());
}

void dialogOpenCompany::on_listView_doubleClicked(const QModelIndex &index)
{
    if(!index.isValid())
        return;

    dApp->openCompany(index.data(Qt::UserRole).toString());
    close();
}
