#include "trendtablemodel.h"

TrendTableModel::TrendTableModel(QObject *parent)
    : QAbstractTableModel(parent)
{
}

QVariant TrendTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    // FIXME: Implement me!
//    return QVariant();
    if(role == Qt::DisplayRole && orientation == Qt::Horizontal)
        return headerDatas[section];
    return QVariant();
//    return QAbstractTableModel::headerData(section,orientation,role);
}

bool TrendTableModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value,
                           int role /*= Qt::EditRole*/)
{
    headerDatas[section] = value.toString();
    return true;
}

int TrendTableModel::rowCount(const QModelIndex &parent) const
{
    if (parent.isValid())
        return 0;

    return m_datas.count();

    // FIXME: Implement me!
}

int TrendTableModel::columnCount(const QModelIndex &parent) const
{
    if (parent.isValid())
        return 0;

    return m_nColumnCount;
    // FIXME: Implement me!
}

QVariant TrendTableModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
        return QVariant();

    int row = index.row();
    int column = index.column();
    switch (role)
    {
        case Qt::DisplayRole:
            return m_datas[row][column];
    }
//    if (role != Qt::DisplayRole)
//        return QVariant();
//    return m_datas[row][column];
    return QVariant();
}

void TrendTableModel::addData(int row,int column,QString data)
{
    if(m_nColumnCount <= column)
        m_nColumnCount = column;
    if(m_datas.count() <= row)
    {
        QList<QString> columns;
        for(int c = 0 ; c < m_nColumnCount ; c++)
        {
            columns.append("");
        }
        columns[column] = data;
        m_datas.append(columns);
    }
    else
    {
        m_datas[row][column] = data;
    }
}
