<commit_before>/*
 * This file is part of the UnTech Editor Suite.
 * Copyright (c) 2016 - 2018, <NAME> <<EMAIL>>.
 * Distributed under The MIT License: https://opensource.org/licenses/MIT
 */

#include "resourcestreemodel.h"
#include "gui-qt/abstractproject.h"
#include "gui-qt/abstractresourceitem.h"
#include "gui-qt/abstractresourcelist.h"

using namespace UnTech::GuiQt;

static_assert(N_RESOURCE_TYPES <= ResourcesTreeModel::ROOT_INTERNAL_ID,
              "ROOT_INTERNAL_ID too low");

ResourcesTreeModel::ResourcesTreeModel(QObject* parent)
    : QAbstractItemModel(parent)
    , _project(nullptr)
    , _uncheckedIcon(":icons/resource-unchecked.svg")
    , _validIcon(":icons/resource-valid.svg")
    , _errorIcon(":icons/resource-error.svg")
{
}

void ResourcesTreeModel::setProject(AbstractProject* project)
{
    if (_project) {
        _project->disconnect(this);

        for (AbstractResourceList* rl : _project->resourceLists()) {
            rl->disconnect(this);
            for (AbstractResourceItem* i : rl->items()) {
                i->disconnect(this);
            }
        }
    }

    beginResetModel();

    _project = project;

    if (_project) {
        connect(_project, &AbstractProject::resourceItemCreated,
                this, &ResourcesTreeModel::connectResourceItemSignals);

        for (AbstractResourceList* rl : _project->resourceLists()) {
            connect(rl, &AbstractResourceList::stateChanged,
                    this, &ResourcesTreeModel::onResourceListStateChanged);
            connect(rl, &AbstractResourceList::listChanged,
                    this, &ResourcesTreeModel::onResourceListChanged);

            for (AbstractResourceItem* item : rl->items()) {
                connectResourceItemSignals(item);
            }
        }
    }

    endResetModel();
}

void ResourcesTreeModel::connectResourceItemSignals(AbstractResourceItem* item)
{
    connect(item, &AbstractResourceItem::nameChanged,
            this, &ResourcesTreeModel::onResourceItemNameChanged);
    connect(item, &AbstractResourceItem::stateChanged,
            this, &ResourcesTreeModel::onResourceItemStateChanged);
}

void ResourcesTreeModel::onResourceListChanged()
{
    Q_ASSERT(_project);

    AbstractResourceList* item = qobject_cast<AbstractResourceList*>(sender());
    if (item) {
        emit layoutChanged();
    }
}

void ResourcesTreeModel::onResourceListStateChanged()
{
    Q_ASSERT(_project);

    AbstractResourceList* list = qobject_cast<AbstractResourceList*>(sender());
    if (list) {
        int index = _project->resourceLists().indexOf(list);

        emit dataChanged(createIndex(index, 0, ROOT_INTERNAL_ID),
                         createIndex(index, N_COLUMNS, ROOT_INTERNAL_ID),
                         { Qt::DecorationRole });
    }
    else {
        emit dataChanged(createIndex(0, 0, ROOT_INTERNAL_ID),
                         createIndex(N_RESOURCE_TYPES, N_COLUMNS, ROOT_INTERNAL_ID),
                         { Qt::DecorationRole });
    }
}

void ResourcesTreeModel::onResourceItemStateChanged()
{
    Q_ASSERT(_project);
    emitResourceDataChanged(qobject_cast<AbstractResourceItem*>(sender()),
                            { Qt::DecorationRole });
}

void ResourcesTreeModel::onResourceItemNameChanged()
{
    Q_ASSERT(_project);
    emitResourceDataChanged(qobject_cast<AbstractResourceItem*>(sender()),
                            { Qt::DisplayRole });
}
<commit_msg>Fix invalid ResorcesTreeModel dataChanged call<commit_after>/*
 * This file is part of the UnTech Editor Suite.
 * Copyright (c) 2016 - 2018, <NAME> <<EMAIL>>.
 * Distributed under The MIT License: https://opensource.org/licenses/MIT
 */

#include "resourcestreemodel.h"
#include "gui-qt/abstractproject.h"
#include "gui-qt/abstractresourceitem.h"
#include "gui-qt/abstractresourcelist.h"

using namespace UnTech::GuiQt;

static_assert(N_RESOURCE_TYPES <= ResourcesTreeModel::ROOT_INTERNAL_ID,
              "ROOT_INTERNAL_ID too low");

ResourcesTreeModel::ResourcesTreeModel(QObject* parent)
    : QAbstractItemModel(parent)
    , _project(nullptr)
    , _uncheckedIcon(":icons/resource-unchecked.svg")
    , _validIcon(":icons/resource-valid.svg")
    , _errorIcon(":icons/resource-error.svg")
{
}

void ResourcesTreeModel::setProject(AbstractProject* project)
{
    if (_project) {
        _project->disconnect(this);

        for (AbstractResourceList* rl : _project->resourceLists()) {
            rl->disconnect(this);
            for (AbstractResourceItem* i : rl->items()) {
                i->disconnect(this);
            }
        }
    }

    beginResetModel();

    _project = project;

    if (_project) {
        connect(_project, &AbstractProject::resourceItemCreated,
                this, &ResourcesTreeModel::connectResourceItemSignals);

        for (AbstractResourceList* rl : _project->resourceLists()) {
            connect(rl, &AbstractResourceList::stateChanged,
                    this, &ResourcesTreeModel::onResourceListStateChanged);
            connect(rl, &AbstractResourceList::listChanged,
                    this, &ResourcesTreeModel::onResourceListChanged);

            for (AbstractResourceItem* item : rl->items()) {
                connectResourceItemSignals(item);
            }
        }
    }

    endResetModel();
}

void ResourcesTreeModel::connectResourceItemSignals(AbstractResourceItem* item)
{
    connect(item, &AbstractResourceItem::nameChanged,
            this, &ResourcesTreeModel::onResourceItemNameChanged);
    connect(item, &AbstractResourceItem::stateChanged,
            this, &ResourcesTreeModel::onResourceItemStateChanged);
}

void ResourcesTreeModel::onResourceListChanged()
{
    Q_ASSERT(_project);

    AbstractResourceList* item = qobject_cast<AbstractResourceList*>(sender());
    if (item) {
        emit layoutChanged();
    }
}

void ResourcesTreeModel::onResourceListStateChanged()
{
    Q_ASSERT(_project);

    AbstractResourceList* list = qobject_cast<AbstractResourceList*>(sender());
    if (list) {
        int index = _project->resourceLists().indexOf(list);

        emit dataChanged(createIndex(index, 0, ROOT_INTERNAL_ID),
                         createIndex(index, N_COLUMNS, ROOT_INTERNAL_ID),
                         { Qt::DecorationRole });
    }
    else {
        int nResourceLists = _project->resourceLists().size();

        emit dataChanged(createIndex(0, 0, ROOT_INTERNAL_ID),
                         createIndex(nResourceLists, N_COLUMNS, ROOT_INTERNAL_ID),
                         { Qt::DecorationRole });
    }
}

void ResourcesTreeModel::onResourceItemStateChanged()
{
    Q_ASSERT(_project);
    emitResourceDataChanged(qobject_cast<AbstractResourceItem*>(sender()),
                            { Qt::DecorationRole });
}

void ResourcesTreeModel::onResourceItemNameChanged()
{
    Q_ASSERT(_project);
    emitResourceDataChanged(qobject_cast<AbstractResourceItem*>(sender()),
                            { Qt::DisplayRole });
}

{'fraction_non_alphanumeric': 0.11429800659687366, 'fraction_numerical': 0.003155026530904919, 'mean_word_length': 3.1710526315789473, 'pattern_counts': {'":': 6, '<': 19, '<?xml version=': 0, '>': 34, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '502620', 'n_tokens_mistral': 1987, 'n_tokens_neox': 1913, 'n_words': 301}