<commit_before>/*Copyright 2014 <NAME>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.*/

#include "bookmodel.h"
#include "gkchess_board.h"
#include "gutil_pluginutils.h"
#include "gutil_strings.h"
USING_NAMESPACE_GKCHESS;
USING_NAMESPACE_GUTIL;
USING_NAMESPACE_GUTIL1(QT);

NAMESPACE_GKCHESS1(UI);


BookModel::BookModel(ObservableBoard &b, QObject *parent)
    :QAbstractItemModel(parent),
      m_board(b),
      i_bookReader(PluginUtils::LoadPlugin<IBookReader>(m_pl, "polyglotReaderPlugin"))
{
    connect(&b, SIGNAL(NotifyPieceMoved(const GKChess::MoveData &)),
            this, SLOT(_board_position_changed()));
    connect(&b, SIGNAL(NotifySquareUpdated(const GKChess::Square &)),
            this, SLOT(_board_position_changed()));
    connect(&b, SIGNAL(NotifyBoardReset()),
            this, SLOT(_board_position_changed()));
}

bool BookModel::SetBookFile(const QString &filename)
{
    // If the filename didn't change, then return
    if(filename == i_bookReader->GetBookFilename())
        return false;

    i_bookReader->CloseBook();
    _board_position_changed();

    if(!filename.isEmpty())
    {
        try{
            i_bookReader->OpenBook(filename.toUtf8().constData());
        } catch(...) {
            return false;
        }
    }
    return true;
}

QString BookModel::GetBookFile() const
{
    return i_bookReader->GetBookFilename();
}

QModelIndexList BookModel::GetAncestry(const QModelIndex &ind) const
{
    QModelIndexList ret;
    _get_ancestry_helper(ret, ind);
    return ret;
}

void BookModel::_get_ancestry_helper(QModelIndexList &l, const QModelIndex &i) const
{
    if(!i.isValid())
        return;

    _get_ancestry_helper(l, i.parent());
    l.append(i);
}

const MoveData *BookModel::ConvertIndexToMoveData(const QModelIndex &i) const
{
    MoveData const *ret = 0;
    if(i.isValid())
        ret = &_get_data_from_index(i)->Data;
    return ret;
}

bool BookModel::hasChildren(const QModelIndex &i) const
{
    bool ret = false;
    if(!i.isValid() || 0 == i.column())
        ret = true;
    return ret;
}

int BookModel::rowCount(const QModelIndex &parent) const
{
    int ret = 0;
    if(!parent.isValid() || 0 == parent.column()){
        ret = _get_children_of_index(parent)->Moves.Length();
    }<commit_msg>Now hasChildren returns true if a node is unloaded, or if it was loaded and has children (before was always true)<commit_after>/*Copyright 2014 <NAME>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.*/

#include "bookmodel.h"
#include "gkchess_board.h"
#include "gutil_pluginutils.h"
#include "gutil_strings.h"
USING_NAMESPACE_GKCHESS;
USING_NAMESPACE_GUTIL;
USING_NAMESPACE_GUTIL1(QT);

NAMESPACE_GKCHESS1(UI);


BookModel::BookModel(ObservableBoard &b, QObject *parent)
    :QAbstractItemModel(parent),
      m_board(b),
      i_bookReader(PluginUtils::LoadPlugin<IBookReader>(m_pl, "polyglotReaderPlugin"))
{
    connect(&b, SIGNAL(NotifyPieceMoved(const GKChess::MoveData &)),
            this, SLOT(_board_position_changed()));
    connect(&b, SIGNAL(NotifySquareUpdated(const GKChess::Square &)),
            this, SLOT(_board_position_changed()));
    connect(&b, SIGNAL(NotifyBoardReset()),
            this, SLOT(_board_position_changed()));
}

bool BookModel::SetBookFile(const QString &filename)
{
    // If the filename didn't change, then return
    if(filename == i_bookReader->GetBookFilename())
        return false;

    i_bookReader->CloseBook();
    _board_position_changed();

    if(!filename.isEmpty())
    {
        try{
            i_bookReader->OpenBook(filename.toUtf8().constData());
        } catch(...) {
            return false;
        }
    }
    return true;
}

QString BookModel::GetBookFile() const
{
    return i_bookReader->GetBookFilename();
}

QModelIndexList BookModel::GetAncestry(const QModelIndex &ind) const
{
    QModelIndexList ret;
    _get_ancestry_helper(ret, ind);
    return ret;
}

void BookModel::_get_ancestry_helper(QModelIndexList &l, const QModelIndex &i) const
{
    if(!i.isValid())
        return;

    _get_ancestry_helper(l, i.parent());
    l.append(i);
}

const MoveData *BookModel::ConvertIndexToMoveData(const QModelIndex &i) const
{
    MoveData const *ret = 0;
    if(i.isValid())
        ret = &_get_data_from_index(i)->Data;
    return ret;
}

bool BookModel::hasChildren(const QModelIndex &i) const
{
    bool ret = false;
    MoveDataContainer const *c = _get_children_of_index(i);
    //if(!i.isValid() || 0 == i.column())
    if(!c->Loaded || 0 < c->Moves.Length())
        ret = true;
    return ret;
}

int BookModel::rowCount(const QModelIndex &parent) const
{
    int ret = 0;
    if(!parent.isValid() || 0 == parent.column()){
        ret = _get_children_of_index(parent)->Moves.Length();
    }
{'fraction_non_alphanumeric': 0.11819459079733052, 'fraction_numerical': 0.005444327362135582, 'mean_word_length': 3.935008665511265, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7375679', 'n_tokens_mistral': 1855, 'n_tokens_neox': 1811, 'n_words': 449}