#include "panelSelectionDialog.h"
#include "ui_panelSelectionDialog.h"

#define panelTye 1

PanelSelectionDialog::PanelSelectionDialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::panelSelectionDialog)
{
    ui->setupUi(this);
    setWindowFlags(Qt::FramelessWindowHint | Qt::Dialog);
    setAttribute(Qt::WA_TranslucentBackground);

    initList();

    connect(ui->btnConfirm, SIGNAL(clicked()), this, SLOT(onBtnClicked_confirm()));
    connect(ui->btnCancle, SIGNAL(clicked()), this, SLOT(onBtnClicked_cancle()));
}

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

void PanelSelectionDialog::initList()
{
    ui->listWidget->clear();

    QListWidgetItem* item = new QListWidgetItem(QString::fromWCharArray(L"曲线图"));
    item->setData(Qt::UserRole + panelTye, lineChart);
    ui->listWidget->addItem(item);
    item = new QListWidgetItem(QString::fromWCharArray(L"直方图"));
    item->setData(Qt::UserRole + panelTye, barChart);
    ui->listWidget->addItem(item);
    item = new QListWidgetItem(QString::fromWCharArray(L"点状图"));
    item->setData(Qt::UserRole + panelTye, dotChart);
    item->setFlags(item->flags() & ~Qt::ItemIsEnabled);
    ui->listWidget->addItem(item);
    item = new QListWidgetItem(QString::fromWCharArray(L"饼状图"));
    item->setData(Qt::UserRole + panelTye, pieChart);
    item->setFlags(item->flags() & ~Qt::ItemIsEnabled);
    ui->listWidget->addItem(item);
    item = new QListWidgetItem(QString::fromWCharArray(L"热力图"));
    item->setData(Qt::UserRole + panelTye, heatMap);
    item->setFlags(item->flags() & ~Qt::ItemIsEnabled);
    ui->listWidget->addItem(item);
    item = new QListWidgetItem(QString::fromWCharArray(L"仪表盘"));
    item->setData(Qt::UserRole + panelTye, dial);
    item->setFlags(item->flags() & ~Qt::ItemIsEnabled);
    ui->listWidget->addItem(item);
    item = new QListWidgetItem(QString::fromWCharArray(L"数据表"));
    item->setData(Qt::UserRole + panelTye, table);
    item->setFlags(item->flags() & ~Qt::ItemIsEnabled);
    ui->listWidget->addItem(item);
    item = new QListWidgetItem(QString::fromWCharArray(L"地图"));
    item->setData(Qt::UserRole + panelTye, map);
    item->setFlags(item->flags() & ~Qt::ItemIsEnabled);
    ui->listWidget->addItem(item);

    ui->listWidget->setCurrentRow(0);
}

// void PanelSelectionDialog::showEvent(QShowEvent* event)
// {
//     Q_UNUSED(event);
//     ui->listWidget->clearSelection();
//     QDialog::showEvent(event);
// }

void PanelSelectionDialog::onBtnClicked_confirm()
{
    QListWidgetItem* item = ui->listWidget->currentItem();
    if(item)
    {
        DataPanelType type = (DataPanelType)item->data(Qt::UserRole + panelTye).toInt();
        ui->listWidget->setCurrentRow(0);
        hide();
        emit panelType(type);
    }
}

void PanelSelectionDialog::onBtnClicked_cancle()
{
    hide();
    emit sgl_hide();
    //ui->listWidget->clearSelection();
}
