#include "loqtreewidget.h"

LoQTreeWidget::LoQTreeWidget(QWidget *parent) :
    QTreeWidget(parent),
    m_checkState(CheckNone)
{
    qDebug() <<"LoQTreeWidget::LoQTreeWidget -s";
    m_header = new LoQHeaderViewCheckBox(Qt::Horizontal, this);
    setHeader(m_header);
    setProperty("ssType", "topList");
    connect(m_header, SIGNAL(sigCheckAll(bool)), this, SLOT(onCheckAll(bool)));
    connect(this, SIGNAL(itemClicked(QTreeWidgetItem*,int)), this, SLOT(onItemClicked(QTreeWidgetItem*,int)));
    setFocusPolicy(Qt::NoFocus);
    qDebug() <<"LoQTreeWidget::LoQTreeWidget -o";

}
void LoQTreeWidget::HideHeaderCheckBox(bool b)
{
    qDebug() <<"HideHeaderCheckBox -s";
    m_header->HideHeaderCheckBox(b);
    qDebug() <<"HideHeaderCheckBox -o";
}
void LoQTreeWidget::adjustCheckState()
{
    qDebug() <<"adjustCheckState -s";
    bool isAllChecked = true;
    int cnt = topLevelItemCount();
    if(cnt == 0) {
        isAllChecked = false;
        m_checkState = CheckNone;
    } else {
        int k = 0;
        for(int i=0; i<cnt; i++) {
             if(!this->isRowHidden(i,indexFromItem(topLevelItem(i)->parent())))
             {
                 if(topLevelItem(i)->checkState(0) == Qt::Unchecked) {
                     isAllChecked = false;
                 } else {
                     k++;
                 }
             }

        }
        if(k > 1) {
            m_checkState = CheckMulti;
        } else if(k > 0) {
            m_checkState = CheckOne;
        } else {
            m_checkState = CheckNone;
        }
    }
    m_header->onAllChecked(isAllChecked);
    sigCheckStateChanged(m_checkState);
    qDebug() <<"adjustCheckState -o";
}

QTreeWidgetItem* LoQTreeWidget::itemChecked()
{
    qDebug() <<"itemChecked -s";
    QTreeWidgetItem *res = nullptr;
    int cnt = topLevelItemCount();
    for(int i=0; i<cnt; i++) {
        QTreeWidgetItem *item = topLevelItem(i);
        if(Qt::Checked == item->checkState(0)) {
            res = item;
            break;
        }
    }
    qDebug() <<"itemChecked -o";
    return res;
}

void LoQTreeWidget::onCheckAll(bool checked)
{
    qDebug() <<"onCheckAll -s";
    int cnt = topLevelItemCount();
    for(int i=0; i<cnt; i++) {
        if(checked)
        {


            if(this->isRowHidden(i,indexFromItem(topLevelItem(i)->parent())))
            {
                topLevelItem(i)->setCheckState(0,  Qt::Unchecked);
            }
            else {
                topLevelItem(i)->setCheckState(0, Qt::Checked);

            }
        }
        else {

            topLevelItem(i)->setCheckState(0, Qt::Unchecked);

        }

     }
   // topLevelItem(i)->setCheckState(0, checked ? Qt::Checked : Qt::Unchecked);
    adjustCheckState();
    qDebug() <<"onCheckAll -o";
}

void LoQTreeWidget::onItemClicked(QTreeWidgetItem *item, int column)
{
    qDebug() <<"onItemClicked -s";
    if(column > 0) {
        item->setCheckState(0, item->checkState(0) == Qt::Unchecked ? Qt::Checked : Qt::Unchecked);
    }
    adjustCheckState();
    qDebug() <<"onItemClicked -o";
}
QModelIndex LoQTreeWidget::indexFromItem(QTreeWidgetItem *item, int column) const
{
    qDebug() <<"onItemClicked -s";
    return QTreeWidget::indexFromItem(item, column);
    qDebug() <<"onItemClicked -o";
}


