<commit_before>/*
    <one line to give the library's name and an idea of what it does.>
    Copyright (C) <year>  <name of author>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/


#include "itemselectorproxy.h"

#include <Akonadi/EntityTreeModel>
#include <QModelIndex>
#include <QModelIndexList>
#include <QTimer>
#include <QItemSelectionModel>
#include <QAbstractItemModel>
#include <KUrl>
#include <Akonadi/EntityTreeView>


ItemSelectorProxy::ItemSelectorProxy(QObject* parent)
:   QObject(parent),
    m_selectionModel(0),
    m_sourceModel(0),
    m_view(0)
{
    m_selectionTimer = new QTimer(this);
    m_selectionTimer->setSingleShot(true);
    connect(m_selectionTimer, SIGNAL(timeout()), this, SLOT(timeout()));

}

void ItemSelectorProxy::setView(Akonadi::EntityTreeView* view)
{
    if (m_view) {
        disconnect(m_view, SIGNAL(currentChanged(const Akonadi::Item &)), this, SLOT(selectItem(const Akonadi::Item &)));
    }
    m_sourceModel = view->model();
    m_selectionModel = view->selectionModel();
    connect(view, SIGNAL(currentChanged(const Akonadi::Item &)), this, SLOT(selectItem(const Akonadi::Item &)));
    m_view = view;
}


void ItemSelectorProxy::selectIndex(const QModelIndex &index)
{
    m_selectionModel->setCurrentIndex(index, QItemSelectionModel::Rows|QItemSelectionModel::ClearAndSelect);
    Akonadi::Item item = m_sourceModel->data(index, Akonadi::EntityTreeModel::ItemRole).value<Akonadi::Item>();
    kDebug() << "selecting new item" << index << item.url().url();
    Q_ASSERT(item.isValid());
    emit itemSelected(item);
}

bool ItemSelectorProxy::selectNewIndex()
{
    if (!m_selectionModel || !m_sourceModel) {
        kWarning() << "no selection model set";
        return false;
    }
    QModelIndexList idx = Akonadi::EntityTreeModel::modelIndexesForItem(m_sourceModel, m_itemToSelect);
    if (!idx.isEmpty()) {
        selectIndex(idx.first());
        disconnect(m_sourceModel, 0, this, 0);
        m_selectionTimer->stop();
        return true;
    }
    return false;
}

void ItemSelectorProxy::timeout()
{
    kWarning() << "selection timed out";
    disconnect(m_sourceModel, 0, this, 0);
}

void ItemSelectorProxy::selectItem(const Akonadi::Item &item)
{<commit_msg>disable selecting the index in the listview from the model for now<commit_after>/*
    <one line to give the library's name and an idea of what it does.>
    Copyright (C) <year>  <name of author>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/


#include "itemselectorproxy.h"

#include <Akonadi/EntityTreeModel>
#include <QModelIndex>
#include <QModelIndexList>
#include <QTimer>
#include <QItemSelectionModel>
#include <QAbstractItemModel>
#include <KUrl>
#include <Akonadi/EntityTreeView>


ItemSelectorProxy::ItemSelectorProxy(QObject* parent)
:   QObject(parent),
    m_selectionModel(0),
    m_sourceModel(0),
    m_view(0)
{
    m_selectionTimer = new QTimer(this);
    m_selectionTimer->setSingleShot(true);
    connect(m_selectionTimer, SIGNAL(timeout()), this, SLOT(timeout()));

}

void ItemSelectorProxy::setView(Akonadi::EntityTreeView* view)
{
    if (m_view) {
        disconnect(m_view, SIGNAL(currentChanged(const Akonadi::Item &)), this, SLOT(selectItem(const Akonadi::Item &)));
    }
    m_sourceModel = view->model();
    m_selectionModel = view->selectionModel();
    connect(view, SIGNAL(currentChanged(const Akonadi::Item &)), this, SLOT(selectItem(const Akonadi::Item &)));
    m_view = view;
}


void ItemSelectorProxy::selectIndex(const QModelIndex &index)
{
    //TODO this would be needed to sync breadcrumbs with listview, it doesn't really work though if we select items again which have been selected in the listview. In case the item is twice in the list (different topics) theres a chance that we select the other instance than the one that was clicked.
    //m_selectionModel->setCurrentIndex(index, QItemSelectionModel::Rows|QItemSelectionModel::ClearAndSelect); 
    Akonadi::Item item = m_sourceModel->data(index, Akonadi::EntityTreeModel::ItemRole).value<Akonadi::Item>();
    kDebug() << "selecting new item" << index << item.url().url();
    Q_ASSERT(item.isValid());
    emit itemSelected(item);
}

bool ItemSelectorProxy::selectNewIndex()
{
    if (!m_selectionModel || !m_sourceModel) {
        kWarning() << "no selection model set";
        return false;
    }
    QModelIndexList idx = Akonadi::EntityTreeModel::modelIndexesForItem(m_sourceModel, m_itemToSelect);
    if (!idx.isEmpty()) {
        selectIndex(idx.first());
        disconnect(m_sourceModel, 0, this, 0);
        m_selectionTimer->stop();
        return true;
    }
    return false;
}

void ItemSelectorProxy::timeout()
{
    kWarning() << "selection timed out";
    disconnect(m_sourceModel, 0, this, 0);
}

void ItemSelectorProxy::selectItem(const Akonadi::Item &item)
{
{'fraction_non_alphanumeric': 0.1063624678663239, 'fraction_numerical': 0.006426735218508998, 'mean_word_length': 4.375647668393782, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 39, 'https://': 0, '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': '4316889', 'n_tokens_mistral': 1867, 'n_tokens_neox': 1824, 'n_words': 570}