#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    setupModels();
}

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

void MainWindow::setupModels()
{
    model.setRowCount(1);
    model.setColumnCount(3);

    model.setItem(0,0, new QStandardItem("root"));
    model.item(0,0)->setColumnCount(model.columnCount());
    model.item(0,0)->setRowCount(5);

    for(int row = 0; row < model.item(0,0)->rowCount(); row++)
    {
        for(int column = 0; column < model.item(0,0)->columnCount(); column++)
            model.item(0,0)->setChild(row, column, new QStandardItem(QString::number(row) + "," + QString::number(column)));
    }

    ui->treeView_Source->setModel(&model);

    proxy.setSourceModel(&model);
    ui->treeView_Proxy->setModel(&proxy);
}

void MainWindow::updateViews()
{
//    ui->treeView_Source->repaint();
//    ui->treeView_Proxy->repaint();
//    ui->treeView_Source->update();
//    ui->treeView_Proxy->update();
}

void MainWindow::on_treeView_Source_clicked(const QModelIndex &index)
{
    modelIndex = index;
}

void MainWindow::on_treeView_Proxy_clicked(const QModelIndex &index)
{
    proxyIndex = index;
}

void MainWindow::on_pushButton_SourceInsertRows_released()
{
    if(modelIndex.isValid())
        model.insertRows(modelIndex.row()+1,1,modelIndex.parent());

    updateViews();
}

void MainWindow::on_pushButton_SourceRemoveRows_released()
{
    if(modelIndex.isValid())
        model.removeRows(modelIndex.row(),1,modelIndex.parent());

    updateViews();
}

void MainWindow::on_pushButton_SourceInsertColumns_released()
{
    if(modelIndex.isValid())
        model.insertColumns(modelIndex.column()+1,1,modelIndex.parent());

    updateViews();
}

void MainWindow::on_pushButton_SourceRemoveColumns_released()
{
    if(modelIndex.isValid())
        model.removeColumns(modelIndex.column(),1,modelIndex.parent());

    updateViews();
}

void MainWindow::on_pushButton_ProxyInsertRows_released()
{
    if(proxyIndex.isValid())
        proxy.insertRows(proxyIndex.row()+1,1,proxyIndex.parent());

    updateViews();
}

void MainWindow::on_pushButton_ProxyRemoveRows_released()
{
    if(proxyIndex.isValid())
        proxy.removeRows(proxyIndex.row(),1,proxyIndex.parent());

    updateViews();
}

void MainWindow::on_pushButton_ProxyInsertColumns_released()
{
    if(proxyIndex.isValid())
        proxy.insertColumns(proxyIndex.column()+1,1,proxyIndex.parent());

    updateViews();
}

void MainWindow::on_pushButton_ProxyRemoveColumns_released()
{
    if(proxyIndex.isValid())
        proxy.removeColumns(proxyIndex.column(),1,proxyIndex.parent());

    updateViews();
}
