/*
    This file is part of the proxy model test suite.

    SPDX-FileCopyrightText: 2009 Stephen Kelly <steveire@gmail.com>

    SPDX-License-Identifier: LGPL-2.1-or-later
*/

#include "recursivefilterpmwidget.h"

#include <QLineEdit>
#include <QSplitter>
#include <QTreeView>
#include <QHBoxLayout>
#include <QLabel>
#include <QPlainTextEdit>

#include "dynamictreemodel.h"
#include "dynamictreewidget.h"

RecursiveFilterProxyWidget::RecursiveFilterProxyWidget(QWidget *parent)
    : QWidget(parent),
      m_lineEdit(new QLineEdit(this)),
      m_label(new QLabel(this))
{
    m_label->setText(QStringLiteral("Matching filter re: "));
    m_lineEdit->setText(QStringLiteral("12|13|37|4"));

    QHBoxLayout *hLayout = new QHBoxLayout();
    QVBoxLayout *vLayout = new QVBoxLayout(this);
    QSplitter *splitter = new QSplitter(this);

    m_rootModel = new DynamicTreeModel(this);
    m_recursive = new KRecursiveFilterProxyModel(this);
    m_recursiveSubclass = new KRecursiveFilterProxyModelSubclass(this);

    DynamicTreeWidget *dynamicTreeWidget = new DynamicTreeWidget(m_rootModel, splitter);

    QTreeView *recursiveView = new QTreeView(splitter);
    recursiveView->setModel(m_recursive);
    QTreeView *recursiveSubclassView = new QTreeView(splitter);
    recursiveSubclassView->setModel(m_recursiveSubclass);

    hLayout->addWidget(m_label);
    hLayout->addWidget(m_lineEdit);

    vLayout->addLayout(hLayout);
    vLayout->addWidget(splitter);

    connect(m_lineEdit, SIGNAL(textChanged(QString)), SLOT(reset()));

    connect(m_lineEdit, SIGNAL(textChanged(QString)), recursiveView, SLOT(expandAll()));
    connect(m_lineEdit, SIGNAL(textChanged(QString)), recursiveSubclassView, SLOT(expandAll()));
    connect(dynamicTreeWidget->textEdit(), SIGNAL(textChanged()), recursiveView, SLOT(expandAll()));
    connect(dynamicTreeWidget->textEdit(), SIGNAL(textChanged()), recursiveSubclassView, SLOT(expandAll()));
    connect(m_recursive, SIGNAL(modelReset()), recursiveView, SLOT(expandAll()), Qt::QueuedConnection);
    connect(m_recursiveSubclass, SIGNAL(modelReset()), recursiveSubclassView, SLOT(expandAll()), Qt::QueuedConnection);

    m_recursive->setSourceModel(m_rootModel);
    m_recursiveSubclass->setSourceModel(m_rootModel);

    reset();
}

void RecursiveFilterProxyWidget::reset()
{
    m_recursive->setFilterRegExp(m_lineEdit->text());
    m_recursiveSubclass->setRegExp(QRegExp(m_lineEdit->text()));
}
