#include "dpConfigurationDialog.h"
#include "ui_dpConfigurationDialog.h"
#include "dataPanel.h"
#include "global.h"
#include <QStandardItemModel>
#include <QRegularExpressionValidator>
#include <QTimer>

dpConfigurationDialog::dpConfigurationDialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::dpConfigurationDialog)
    , m_curActiveTab(nullptr)
    , m_pDataPanel(nullptr)
    , m_pModel_typeSource(nullptr)
    , m_pModel_typeSelected(nullptr)
    , m_pModel_dataSource(nullptr)
    , m_pModel_dataSelected(nullptr)
{
    ui->setupUi(this);
    setWindowFlags(Qt::FramelessWindowHint | Qt::WindowDoesNotAcceptFocus);
    setAttribute(Qt::WA_TranslucentBackground);

    initialize();
}

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

void dpConfigurationDialog::showEvent(QShowEvent* event)
{
    //ui->stackedWidget->setCurrentIndex(0);
    ui->tabDataType->click();
    if(m_pModel_typeSelected && m_pModel_typeSelected->rowCount() == 0)
    {
        ui->errorTip->setVisible(true);
        ui->errorTip->setToolTip(QString::fromStdWString(L"要至少选择一个数据类型"));
        ui->btnConfirm->setEnabled(false);
    }
    else
    {
        ui->errorTip->setVisible(false);
        //ui->errorTip->setToolTip(QString::fromStdWString(L"要至少选择一个数据类型"));
        ui->btnConfirm->setEnabled(true);
    }
}

void dpConfigurationDialog::initialize()
{
    m_curAxisComboBoxIndex = -1;

    ui->tabDataType->setProperty("index", 0);
    connect(ui->tabDataType, SIGNAL(clicked()), this, SLOT(onBtnClicked_tabBtn()));
    ui->tabDataSource->setProperty("index", 1);
    connect(ui->tabDataSource, SIGNAL(clicked()), this, SLOT(onBtnClicked_tabBtn()));
    ui->tabDisplaySetting->setProperty("index", 2);
    connect(ui->tabDisplaySetting, SIGNAL(clicked()), this, SLOT(onBtnClicked_tabBtn()));
    m_curActiveTab = ui->tabDataType;

    ui->stackedWidget->setCurrentIndex(0);

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

    connect(ui->btnReomve_type, SIGNAL(clicked()), this, SLOT(onBtnClicked_remove_type()));
    connect(ui->btnReomve_source, SIGNAL(clicked()), this, SLOT(onBtnClicked_remove_source()));

    //connect(ui->radioBtn_showLegend, &QRadioButton::toggled, this, &dpConfigurationDialog::onRadioBtnToggled);

    connect(ui->typeSourceList, &QListView::clicked, this, &dpConfigurationDialog::onItemClicked_typeSource);
    connect(ui->dataSourceList, &QListView::clicked, this, &dpConfigurationDialog::onItemClicked_dataSource);

    ///数据类型
    //typeSourceList
    m_pModel_typeSource = new QStandardItemModel(this);
    ui->typeSourceList->setModel(m_pModel_typeSource);
    /*QStringList typeSourceTextList;
    typeSourceTextList << QString::fromStdWString(L"电压") << QString::fromStdWString(L"电流") << QString::fromStdWString(L"功率");
    for(QString& text: typeSourceTextList)
    {
        QStandardItem* item = new QStandardItem(text);
        item->setEditable(false); //不可编辑
        m_pModel_typeSource->appendRow(item);
    }*/
    QStandardItem* item = new QStandardItem("电压");
    item->setData(RealTimeDataType::voltage, Qt::UserRole + itemRole_dataType);
    item->setEditable(false); //不可编辑
    m_pModel_typeSource->appendRow(item);
    item = new QStandardItem("电流");
    item->setData(RealTimeDataType::current, Qt::UserRole + itemRole_dataType);
    item->setEditable(false); //不可编辑
    m_pModel_typeSource->appendRow(item);
    item = new QStandardItem("功率");
    item->setData(RealTimeDataType::power, Qt::UserRole + itemRole_dataType);
    item->setEditable(false); //不可编辑
    m_pModel_typeSource->appendRow(item);
    item = new QStandardItem("温度");
    item->setData(RealTimeDataType::temperature, Qt::UserRole + itemRole_dataType);
    item->setEditable(false); //不可编辑
    m_pModel_typeSource->appendRow(item);
    //typeSelectedList
    ui->typeSelectedList->setSelectionBehavior(QAbstractItemView::SelectRows);
    m_pModel_typeSelected = new QStandardItemModel(this);
    ui->typeSelectedList->setModel(m_pModel_typeSelected);

    ///数据源
    //dataSourceList
    m_pModel_dataSource = new QStandardItemModel(this);
    ui->dataSourceList->setModel(m_pModel_dataSource);
    createDataSourceList();
    //sourceSelectedList
    ui->dataSelectedList->setSelectionBehavior(QAbstractItemView::SelectRows);
    m_pModel_dataSelected = new QStandardItemModel(this);
    ui->dataSelectedList->setModel(m_pModel_dataSelected);

    ///数据配置
    QRegularExpression regExp_ip("^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}"
                                 "(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$");
    QRegularExpressionValidator* validator_ip = new QRegularExpressionValidator(regExp_ip, this);
    ui->serviceIP->setValidator(validator_ip);
    QRegularExpression regExp_port("^(?:[0-9]{1,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$");
    QRegularExpressionValidator* validator_port = new QRegularExpressionValidator(regExp_port, this);
    ui->servicePort->setValidator(validator_port);
}

void dpConfigurationDialog::copyModelData(QStandardItemModel* sourceModel, QStandardItemModel* destModel)
{
    if(!sourceModel || !destModel)
        return;

    for(int row = 0; row < destModel->rowCount(); row++)
    {
        for(int col = 0; col < destModel->columnCount(); col++)
        {
            QStandardItem* item = destModel->item(row, col);
            if(item)
                delete item;
        }
    }
    destModel->clear(); //只做clear不会释放其中的item

    for(int row = 0; row < sourceModel->rowCount(); row++)
    {
        QList<QStandardItem*> itemList;
        for(int col = 0; col < sourceModel->columnCount(); col++)
        {
            QStandardItem* item = sourceModel->item(row, col);
            if(item)
            {
                QStandardItem* newItem = new QStandardItem(item->text());
                QString strTag = item->data(Qt::UserRole + itemRole_tag).toString();
                newItem->setData(strTag, Qt::UserRole + itemRole_tag);
                RealTimeDataType dataType = (RealTimeDataType)item->data(Qt::UserRole + itemRole_dataType).toInt();
                newItem->setData(dataType, Qt::UserRole + itemRole_dataType);
                if(strTag == "point")
                {
                    newItem->setData(item->data(Qt::DecorationRole), Qt::DecorationRole);
                    newItem->setData(item->data(Qt::UserRole + itemRole_stationID), Qt::UserRole + itemRole_stationID);
                    newItem->setData(item->data(Qt::UserRole + itemRole_componentID), Qt::UserRole + itemRole_componentID);
                    newItem->setData(item->data(Qt::UserRole + itemRole_pointID), Qt::UserRole + itemRole_pointID);
                }
                itemList.push_back(newItem);
            }
        }
        destModel->appendRow(itemList);
    }
}

void dpConfigurationDialog::createDataSourceList()
{
    //demo先临时写死，后续改为接口动态创建
    QStandardItem* rootItem = m_pModel_dataSource->invisibleRootItem();
    for(int i = 0; i < 3; i++)
    {
        QString strStation = QString("测试站-") + QString::number(i + 1);
        QStandardItem* stationItem = new QStandardItem(strStation);
        stationItem->setEditable(false);
        stationItem->setData("station", Qt::UserRole + itemRole_tag);
        rootItem->appendRow(stationItem);
        QStandardItem* componentItem = new QStandardItem("异步电机");
        componentItem->setEditable(false);
        componentItem->setData("component", Qt::UserRole + itemRole_tag);
        stationItem->appendRow(componentItem);
        QStandardItem* voltageItem = new QStandardItem("电压");
        voltageItem->setEditable(false);
        voltageItem->setData("point", Qt::UserRole + itemRole_tag);
        voltageItem->setData(i, Qt::UserRole + itemRole_stationID);
        voltageItem->setData(0, Qt::UserRole + itemRole_componentID);
        //voltageItem->setData(0, Qt::UserRole + itemRole_pointID);
        voltageItem->setData("grid1.zone1.station1.ns1.tag1.bay.I11_A_rms", Qt::UserRole + itemRole_pointID);
        voltageItem->setData(RealTimeDataType::voltage, Qt::UserRole + itemRole_dataType);
        componentItem->appendRow(voltageItem);
        QStandardItem* currentItem = new QStandardItem("电流");
        currentItem->setEditable(false);
        currentItem->setData("point", Qt::UserRole + itemRole_tag);
        currentItem->setData(i, Qt::UserRole + itemRole_stationID);
        currentItem->setData(0, Qt::UserRole + itemRole_componentID);
        //currentItem->setData(1, Qt::UserRole + itemRole_pointID);
        currentItem->setData("grid1.zone1.station1.ns1.tag1.bay.I11_B_rms", Qt::UserRole + itemRole_pointID);
        currentItem->setData(RealTimeDataType::current, Qt::UserRole + itemRole_dataType);
        componentItem->appendRow(currentItem);
        QStandardItem* powerItem = new QStandardItem("功率");
        powerItem->setEditable(false);
        powerItem->setData("point", Qt::UserRole + itemRole_tag);
        powerItem->setData(i, Qt::UserRole + itemRole_stationID);
        powerItem->setData(0, Qt::UserRole + itemRole_componentID);
        //powerItem->setData(2, Qt::UserRole + itemRole_pointID);
        powerItem->setData("grid1.zone1.station1.ns1.tag1.bay.I11_C_rms", Qt::UserRole + itemRole_pointID);
        powerItem->setData(RealTimeDataType::power, Qt::UserRole + itemRole_dataType);
        componentItem->appendRow(powerItem);
        QStandardItem* tempItem = new QStandardItem("温度");
        tempItem->setEditable(false);
        tempItem->setData("point", Qt::UserRole + itemRole_tag);
        tempItem->setData(i, Qt::UserRole + itemRole_stationID);
        tempItem->setData(0, Qt::UserRole + itemRole_componentID);
        //tempItem->setData(3, Qt::UserRole + itemRole_pointID);
        tempItem->setData("grid1.zone1.station1.ns1.tag1.bay.I11_D_rms", Qt::UserRole + itemRole_pointID);
        tempItem->setData(RealTimeDataType::temperature, Qt::UserRole + itemRole_dataType);
        componentItem->appendRow(tempItem);
    }

    ui->dataSourceList->expandAll();
}

void dpConfigurationDialog::removeDataSelected(int deleteRow)
{
    QList<QStandardItem*> items = m_pModel_dataSelected->takeRow(deleteRow);
    for(QStandardItem* item: std::as_const(items))
        delete item;

    //更新颜色-从当前删除项往后的所有项
    for(int row = deleteRow; row < m_pModel_dataSelected->rowCount(); row++)
    {
        QStandardItem* item = m_pModel_dataSelected->item(row, 0);
        if(item)
        {
            QColor color = g_globalColor.value(item->row() % g_globalColor.size());
            item->setData(color, Qt::DecorationRole);
        }
    }
}

void dpConfigurationDialog::setPanel(DataPanel* pPanel)
{
    if(!pPanel)
        return;

    m_pDataPanel = pPanel;
    copyModelData(pPanel->m_cofigurationResults.m_pModel_dataType, m_pModel_typeSelected);
    copyModelData(pPanel->m_cofigurationResults.m_pModel_dataSource, m_pModel_dataSelected);
    // m_pModel_typeSelected = pPanel->m_cofigurationResults.m_pModel_dataType;
    // if(m_pModel_typeSelected)
    //     ui->typeSelectedList->setModel(m_pModel_typeSelected);
    ui->serviceIP->setText(pPanel->m_cofigurationResults.dataServiceIP);
    ui->servicePort->setText(QString::number(pPanel->m_cofigurationResults.dataServicePort));
    ui->radioBtn_showLegend->setChecked(pPanel->m_cofigurationResults.showLegend);
    ui->radioBtn_groupByType->setChecked(pPanel->m_cofigurationResults.groupByType);

    DataPanelType panelType = pPanel->getType();
    switch (panelType)
    {
    case lineChart:
    {
        ui->specialSettings->setVisible(true);
        ui->specialSettings->setCurrentIndex(0);
        ui->axisArrangement->setCurrentIndex((int)m_pDataPanel->m_cofigurationResults.axisArrangement);
        ui->axisObject->clear();
        m_axisCfgMap.clear();
        ui->axisName->setText("");
        ui->axisUnit->setText("");
        m_axisCfgMap = pPanel->m_cofigurationResults.axisCfgMap;
        disconnect(ui->axisObject, &QComboBox::currentIndexChanged, this, &dpConfigurationDialog::onComboBoxIndexChanged_axis);
        for(int i = 0; i< m_pModel_typeSelected->rowCount(); i++)
        {
            RealTimeDataType dataType = (RealTimeDataType)m_pModel_typeSelected->item(i, 0)->data(Qt::UserRole + itemRole_dataType).toInt();
            QString itemText = m_pModel_typeSelected->item(i, 0)->text();
            ui->axisObject->addItem(itemText, dataType);
        }
        if(ui->axisObject->count() > 0)
        {
            m_curAxisComboBoxIndex = ui->axisObject->currentIndex();
            RealTimeDataType itemDataType = (RealTimeDataType)ui->axisObject->currentData().toInt();
            if(m_axisCfgMap.contains(itemDataType))
            {
                ui->axisName->setText(m_axisCfgMap.value(itemDataType).name);
                ui->axisUnit->setText(m_axisCfgMap.value(itemDataType).unit);
            }
        }
        else
        {
            m_curAxisComboBoxIndex = -1;
        }
        connect(ui->axisObject, &QComboBox::currentIndexChanged, this, &dpConfigurationDialog::onComboBoxIndexChanged_axis);
        break;
    }
    case barChart:
    {
        ui->specialSettings->setVisible(true);
        ui->specialSettings->setCurrentIndex(1);
        ui->barOrientation->setCurrentIndex((int)pPanel->m_cofigurationResults.barOrientataion);
        break;
    }
    default:
        ui->specialSettings->setVisible(false);
        break;
    }
}

void dpConfigurationDialog::onBtnClicked_tabBtn()
{
    QPushButton* pTab = qobject_cast<QPushButton*>(sender());
    if(pTab == m_curActiveTab)
        return;

    if(m_curActiveTab)
    {
        m_curActiveTab->setStyleSheet("QPushButton\n"
                                      "{\n"
                                      "color: rgb(250, 250, 250);\n"
                                      "font: 700 12pt \"黑体\";\n"
                                      "border:0px;\n"
                                      "background-color:transparent;\n"
                                      "}\n"
                                      "QPushButton:hover\n"
                                      "{\n"
                                      "}\n"
                                      "QPushButton:pressed\n"
                                      "{\n"
                                      "}");
    }
    if(pTab)
    {
        pTab->setStyleSheet("QPushButton\n"
                                      "{\n"
                                      "color: rgb(250, 250, 250);\n"
                                      "font: 700 12pt \"黑体\";\n"
                                      "border:1px solid rgb(200,200,200);\n"
                                      "border-bottom:0px;\n"
                                      "background-color:rgba(36,43,50, 250);\n"
                                      "}\n"
                                      "QPushButton:hover\n"
                                      "{\n"
                                      "}\n"
                                      "QPushButton:pressed\n"
                                      "{\n"
                                      "}");

        int nIndex = pTab->property("index").toInt();
        ui->stackedWidget->setCurrentIndex(nIndex);
        if(nIndex == 1 && m_pModel_dataSelected->rowCount() > 0)
            ui->dataSelectedList->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch);
    }
    m_curActiveTab = pTab;

}

void dpConfigurationDialog::onBtnClicked_confirm()
{
    hide();
    emit sgl_hide();

    if(m_pDataPanel)
    {
        //m_pDataPanel->m_cofigurationResults.m_pModel_dataType = m_pModel_typeSelected;
        copyModelData(m_pModel_typeSelected, m_pDataPanel->m_cofigurationResults.m_pModel_dataType);
        //m_pDataPanel->configurationComplete();
        copyModelData(m_pModel_dataSelected, m_pDataPanel->m_cofigurationResults.m_pModel_dataSource);
        m_pDataPanel->m_cofigurationResults.dataServiceIP = ui->serviceIP->text();
        m_pDataPanel->m_cofigurationResults.dataServicePort = ui->servicePort->text().toInt();
        m_pDataPanel->m_cofigurationResults.showLegend = ui->radioBtn_showLegend->isChecked();
        m_pDataPanel->m_cofigurationResults.axisArrangement = (AxisArrangementMode)ui->axisArrangement->currentIndex();

        DataPanelType panelType = m_pDataPanel->getType();
        switch (panelType)
        {
        case lineChart:
        {
            RealTimeDataType itemDataType = (RealTimeDataType)ui->axisObject->currentData().toInt();
            if(m_axisCfgMap.contains(itemDataType))
            {
                if(!ui->axisName->text().isEmpty())
                    m_axisCfgMap[itemDataType].name = ui->axisName->text();
                m_axisCfgMap[itemDataType].unit = ui->axisUnit->text();
            }
            break;
        }
        case barChart:
        {
            m_pDataPanel->m_cofigurationResults.barOrientataion = (BarOrientation)ui->barOrientation->currentIndex();
            m_pDataPanel->m_cofigurationResults.groupByType = ui->radioBtn_groupByType->isChecked();
            break;
        }
        default:
            break;
        }

        m_pDataPanel->m_cofigurationResults.axisCfgMap = m_axisCfgMap;
        m_pDataPanel->configurationComplete();
    }
}

void dpConfigurationDialog::onBtnClicked_cancle()
{
    hide();
    emit sgl_hide();
}

void dpConfigurationDialog::onItemClicked_typeSource(const QModelIndex& index)
{
    QStandardItem* item = m_pModel_typeSource->itemFromIndex(index);
    if(item)
    {
        bool bIsHad = false;
        for(int i = 0; i< m_pModel_typeSelected->rowCount(); i++)
        {
            QString itemText = m_pModel_typeSelected->item(i, 0)->text();
            if(itemText == item->text())
            {
                bIsHad = true;
                break;
            }
        }

        if(!m_pDataPanel)
        {
            ui->errorTip->setVisible(true);
            ui->errorTip->setToolTip(QString("当前面板对象为nullptr"));
            return;
        }

        if(!bIsHad && m_pModel_typeSelected)
        {
            int nMaximumType = m_pModel_typeSource->rowCount();
            if(m_pDataPanel && (m_pDataPanel->getType() == lineChart || m_pDataPanel->getType() == barChart))
                nMaximumType = 4;

            if(m_pModel_typeSelected->rowCount() >= nMaximumType)
            {
                if(!ui->errorTip->isVisible())
                {
                    ui->errorTip->setVisible(true);
                    ui->errorTip->setToolTip(QString("当前面板最多展示 %1 种类型数据").arg(nMaximumType));
                    //QTimer::singleShot(6000, this, [this]{ui->errorTip->setVisible(false);});
                }
                return;
            }

            QStandardItem* newItem = new QStandardItem(item->text());
            newItem->setEditable(false);
            RealTimeDataType dataType = (RealTimeDataType)item->data(Qt::UserRole + itemRole_dataType).toInt();
            newItem->setData(dataType, Qt::UserRole + itemRole_dataType);
            m_pModel_typeSelected->appendRow(newItem);
            ui->typeSelectedList->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch);

            //其它需要同步配置项
            DataPanelType panelType = m_pDataPanel->getType();
            switch (panelType)
            {
            case lineChart:
            {
                AxisCfgInfo cfg;
                cfg.name = item->text();
                m_axisCfgMap.insert(dataType, cfg);
                disconnect(ui->axisObject, &QComboBox::currentIndexChanged, this, &dpConfigurationDialog::onComboBoxIndexChanged_axis);
                ui->axisObject->addItem(item->text(), dataType);
                m_curAxisComboBoxIndex = ui->axisObject->currentIndex();
                if(ui->axisName->text().isEmpty())
                    ui->axisName->setText(ui->axisObject->currentText());
                connect(ui->axisObject, &QComboBox::currentIndexChanged, this, &dpConfigurationDialog::onComboBoxIndexChanged_axis);
                break;
            }
            case barChart:
            {
                break;
            }
            default:
                break;
            }

            if(ui->errorTip->isVisible())
            {
                ui->errorTip->setVisible(false);
                ui->btnConfirm->setEnabled(true);
            }
        }
    }
}
void dpConfigurationDialog::onBtnClicked_remove_type()
{
    QItemSelectionModel* selectionModel = ui->typeSelectedList->selectionModel();
    if(selectionModel && m_pModel_typeSelected)
    {
        int nCurrentRow = selectionModel->currentIndex().row();
        if(nCurrentRow == -1)
            return;

        //同步删除dataSource中已选择的该类型数据
        RealTimeDataType itemDataType = (RealTimeDataType)m_pModel_typeSelected->data(selectionModel->currentIndex(), Qt::UserRole + itemRole_dataType).toInt();
        for(int i = m_pModel_dataSelected->rowCount() - 1; i >= 0; i--) //倒序查找，因为执行removeDataSelected时，内部会从删除项往后更新颜色，倒序可以减少循环次数
        {
            RealTimeDataType dataType = (RealTimeDataType)m_pModel_dataSelected->item(i, 0)->data(Qt::UserRole + itemRole_dataType).toInt();
            if(itemDataType == dataType)
            {
                removeDataSelected(i);
            }
        }

        QList<QStandardItem*> items = m_pModel_typeSelected->takeRow(nCurrentRow);
        for(QStandardItem* item: items)
            delete item;

        //其它需要同步配置项
        DataPanelType panelType = m_pDataPanel->getType();
        switch (panelType)
        {
        case lineChart:
        {
            m_axisCfgMap.remove(itemDataType);
            int index = ui->axisObject->findData(itemDataType);
            if(index != -1)
            {
                disconnect(ui->axisObject, &QComboBox::currentIndexChanged, this, &dpConfigurationDialog::onComboBoxIndexChanged_axis);
                ui->axisObject->removeItem(index);
                if(ui->axisObject->count() == 0)
                    m_curAxisComboBoxIndex = -1;
                else
                    m_curAxisComboBoxIndex = ui->axisObject->currentIndex();
                connect(ui->axisObject, &QComboBox::currentIndexChanged, this, &dpConfigurationDialog::onComboBoxIndexChanged_axis);
            }
            break;
        }
        case barChart:
        {
            break;
        }
        default:
            break;
        }

        if(m_pModel_typeSelected->rowCount() == 0)
        {
            ui->errorTip->setVisible(true);
            ui->errorTip->setToolTip(QString::fromStdWString(L"要至少选择一个数据类型"));
            ui->btnConfirm->setEnabled(false);
        }
        else if(ui->errorTip->isVisible())
            ui->errorTip->setVisible(false);
    }
}

void dpConfigurationDialog::onItemClicked_dataSource(const QModelIndex& index)
{
    QStandardItem* item = m_pModel_dataSource->itemFromIndex(index);
    if(item)
    {
        QString strTag = item->data(Qt::UserRole + itemRole_tag).toString();
        if(strTag != "point")
            return;

        //只能对已选择的数据类型进行选择
        bool bCanbeSelect = false;
        RealTimeDataType itemDataType = (RealTimeDataType)item->data(Qt::UserRole + itemRole_dataType).toInt();
        //qDebug() << "itemDataType" << itemDataType;
        for(int i = 0; i < m_pModel_typeSelected->rowCount(); i++)
        {
            RealTimeDataType dataType = (RealTimeDataType)m_pModel_typeSelected->item(i, 0)->data(Qt::UserRole + itemRole_dataType).toInt();
            //qDebug() << "dataType" << dataType;
            if(itemDataType == dataType)
            {
                bCanbeSelect = true;
                break;
            }
        }
        if(!bCanbeSelect)
            return;

        QString strText = "";
        QStandardItem* compontItme = item->parent();
        if(compontItme && compontItme->data(Qt::UserRole + itemRole_tag).toString() == "component")
        {
            QStandardItem* stationItme = compontItme->parent();
            if(stationItme && stationItme->data(Qt::UserRole + itemRole_tag).toString() == "station")
                strText = stationItme->text() + "." + compontItme->text();
            else
                strText = compontItme->text();
        }
        if(strText.isEmpty())
            strText = item->text();
        else
            strText = strText + "." + item->text();

        bool bIsHad = false;
        for(int i = 0; i< m_pModel_dataSelected->rowCount(); i++)
        {
            QString itemText = m_pModel_dataSelected->item(i, 0)->text();
            if(itemText == strText)
            {
                bIsHad = true;
                break;
            }
        }

        if(!bIsHad && m_pModel_dataSelected)
        {
            QStandardItem* newItem = new QStandardItem(strText);
            newItem->setEditable(false);
            newItem->setData(item->data(Qt::UserRole + itemRole_tag), Qt::UserRole + itemRole_tag);
            newItem->setData(item->data(Qt::UserRole + itemRole_stationID), Qt::UserRole + itemRole_stationID);
            newItem->setData(item->data(Qt::UserRole + itemRole_componentID), Qt::UserRole + itemRole_componentID);
            newItem->setData(item->data(Qt::UserRole + itemRole_pointID), Qt::UserRole + itemRole_pointID);
            newItem->setData(item->data(Qt::UserRole + itemRole_dataType), Qt::UserRole + itemRole_dataType);
            m_pModel_dataSelected->appendRow(newItem);
            QColor color = g_globalColor.value(newItem->row() % g_globalColor.size());
            newItem->setData(color, Qt::DecorationRole);
            ui->dataSelectedList->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch);
        }
    }
}
void dpConfigurationDialog::onBtnClicked_remove_source()
{
    QItemSelectionModel* selectionModel = ui->dataSelectedList->selectionModel();
    if(selectionModel && m_pModel_dataSelected)
    {
        int nCurrentRow = selectionModel->currentIndex().row();
        removeDataSelected(nCurrentRow);
    }
}

void dpConfigurationDialog::onComboBoxIndexChanged_axis(int index)
{
    if(m_curAxisComboBoxIndex == -1)
        return;

    RealTimeDataType itemDataType = (RealTimeDataType)ui->axisObject->itemData(m_curAxisComboBoxIndex).toInt();
    if(m_axisCfgMap.contains(itemDataType))
    {
        if(!ui->axisName->text().isEmpty())
            m_axisCfgMap[itemDataType].name = ui->axisName->text();
        m_axisCfgMap[itemDataType].unit = ui->axisUnit->text();

        m_curAxisComboBoxIndex = ui->axisObject->currentIndex();
        itemDataType = (RealTimeDataType)ui->axisObject->itemData(m_curAxisComboBoxIndex).toInt();
        if(m_axisCfgMap.contains(itemDataType))
        {
            ui->axisName->setText(m_axisCfgMap.value(itemDataType).name);
            ui->axisUnit->setText(m_axisCfgMap.value(itemDataType).unit);
        }
    }
}

void dpConfigurationDialog::onRadioBtnToggled(bool checked)
{

}
