#include "treewidget.h"

#include <QTreeWidgetItem>

TreeWidget::TreeWidget(QTreeWidget *parent)
    : QTreeWidget(parent)
{
    initUI();
    createConnections();
}

TreeWidget::~TreeWidget()
{

}

void TreeWidget::slotItemDoubleClicked(QTreeWidgetItem *item, int column)
{
    QString txt = item->text(0);
    emit signalClicked(txt);
}

void TreeWidget::initUI()
{
    this->setHeaderHidden(true);
    // 设置交替颜色
    // treeWidget->setAlternatingRowColors(true);
    this->setColumnCount(1);
    QList<QTreeWidgetItem *> items;
    for (int i = 0; i < 4; ++i)
    {
        QString title = "";
        switch(i)
        {
        case 0:
            title = "A盘";
            break;
        case 1:
            title = "B盘";
            break;
        case 2:
            title = "C盘";
            break;
        case 3:
            title = "主体";
            break;
        default:
            title = "主体";
            break;
        }
        items.append(new QTreeWidgetItem((QTreeWidget*)0, QStringList(title)));
    }

    this->insertTopLevelItems(0, items);

    for(int i = 0; i < items.size(); i++)
    {
        switch(i)
        {
        case 0:
        {
            for(int j = 0; j < 6; j++)
            {
                QTreeWidgetItem* item = new QTreeWidgetItem;
                item->setText(0, QString("相机%1").arg(j + 1));
                items.at(i)->addChild(item);
            }
        }

            break;
        case 1:
        {
            for(int j = 5; j < 11; j++)
            {
                QTreeWidgetItem* item = new QTreeWidgetItem;
                item->setText(0, QString("相机%1").arg(j + 1));
                items.at(i)->addChild(item);
            }
        }
            break;
        case 2:
        {
            for(int j = 10; j < 16; j++)
            {
                QTreeWidgetItem* item = new QTreeWidgetItem;
                item->setText(0, QString("相机%1").arg(j + 1));
                items.at(i)->addChild(item);
            }
        }
            break;
        case 3:
        {
            for(int j = 0; j < 2; j++)
            {
                QString title = "";
                if(j == 0)
                    title = "相机";
                else
                    title = "图表";
                QTreeWidgetItem* item = new QTreeWidgetItem;
                item->setText(0, title);
                items.at(i)->addChild(item);
            }
        }
            break;
        default:

            break;
        }
    }
}

void TreeWidget::createConnections()
{
    connect(this, &QTreeWidget::itemClicked, this, &TreeWidget::slotItemDoubleClicked);
}
