<commit_before>#include "NodeView.h"

NodeView::NodeView(QWidget* parent) :
    QGraphicsView(parent), currentWidget(nullptr), resizeGrabSize(20), rightClickMenu(new QMenu(this))
{}

NodeView::NodeView(QGraphicsScene *scene, QWidget *parent):
    QGraphicsView(scene, parent), currentWidget(nullptr), resizeGrabSize(20), rightClickMenu(new QMenu(this))
{
    actions.push_back(new QAction("Delete Node", rightClickMenu));
    rightClickMenu->addActions(actions);

    connect(rightClickMenu, SIGNAL(triggered(QAction*)), this, SLOT(on_actionSelect(QAction*)));
}
void NodeView::addWidget(QGraphicsProxyWidget * widget, ObjectId id)
{
    widgetMap[id] = widget;
    // If this widget has a parent, draw a line to it
    drawLine2Parent(widget);
}
QGraphicsProxyWidget* NodeView::getWidget(ObjectId id)
{
    auto itr = widgetMap.find(id);
    if (itr != widgetMap.end())
        return itr->second;
    else return nullptr;
}
void NodeView::on_actionSelect(QAction *action)
{
    if(action == actions[0])
    {
        // Delete the current node
        auto nodeWidget = dynamic_cast<QNodeWidget*>(currentWidget->widget());
        if(nodeWidget)
        {
            emit stopThread();
            auto node = nodeWidget->getNode();

            auto parent = node->getParent();
            if(parent != nullptr)
                parent->removeChild(node);
            emit selectionChanged(nullptr);
            emit widgetDeleted(nodeWidget);
            // Causes segfault sometimes, processing thread has fully exited but segfault still occurs in ~QNodeWidget
            // Looks like it has to do with the QGraphicsProxyWidget trying to handle deletion.  Maybe we need to delete that first?
            // Or maybe we just need to delete the QNodeWidget first.
            scene()->removeItem(currentWidget);
            delete currentWidget;
            currentWidget = nullptr;
            EagleLib::NodeManager::getInstance().removeNode(node->GetObjectId());
            emit startThread();
        }
    }
}

void NodeView::mousePressEvent(QMouseEvent* event)
{
    if (QGraphicsItem* item = itemAt(event->pos().x(), event->pos().y()))
    {
        if (QGraphicsProxyWidget* widget = dynamic_cast<QGraphicsProxyWidget*>(item))
        {
            mousePressPosition = event->pos();
            currentWidget = widget;
            emit selectionChanged(widget);
            if(event->button() == Qt::LeftButton)
            {
                // If within the 5x5 corners of the widget, resize<commit_msg>Imshow was moved to using GPU callbacks for image display.  This caused a bug where the node was deleted, and thus memory deallocated but the callback wasn't aware of this.  The fix was to disable the node in the main thread, then wait for any callbacks to clear.  Afterwhich the node can safely be deleted.<commit_after>#include "NodeView.h"

NodeView::NodeView(QWidget* parent) :
    QGraphicsView(parent), currentWidget(nullptr), resizeGrabSize(20), rightClickMenu(new QMenu(this))
{}

NodeView::NodeView(QGraphicsScene *scene, QWidget *parent):
    QGraphicsView(scene, parent), currentWidget(nullptr), resizeGrabSize(20), rightClickMenu(new QMenu(this))
{
    actions.push_back(new QAction("Delete Node", rightClickMenu));
    rightClickMenu->addActions(actions);

    connect(rightClickMenu, SIGNAL(triggered(QAction*)), this, SLOT(on_actionSelect(QAction*)));
}
void NodeView::addWidget(QGraphicsProxyWidget * widget, ObjectId id)
{
    widgetMap[id] = widget;
    // If this widget has a parent, draw a line to it
    drawLine2Parent(widget);
}
QGraphicsProxyWidget* NodeView::getWidget(ObjectId id)
{
    auto itr = widgetMap.find(id);
    if (itr != widgetMap.end())
        return itr->second;
    else return nullptr;
}
void NodeView::on_actionSelect(QAction *action)
{
    if(action == actions[0])
    {
        // Delete the current node
        auto nodeWidget = dynamic_cast<QNodeWidget*>(currentWidget->widget());
        if(nodeWidget)
        {
            auto node = nodeWidget->getNode();
            node->enabled = false;
            boost::this_thread::sleep_for(boost::chrono::milliseconds(30));
            emit stopThread();
            auto parent = node->getParent();
            if(parent != nullptr)
                parent->removeChild(node);
            emit selectionChanged(nullptr);
            emit widgetDeleted(nodeWidget);
            // Causes segfault sometimes, processing thread has fully exited but segfault still occurs in ~QNodeWidget
            // Looks like it has to do with the QGraphicsProxyWidget trying to handle deletion.  Maybe we need to delete that first?
            // Or maybe we just need to delete the QNodeWidget first.
            scene()->removeItem(currentWidget);
            delete currentWidget;
            currentWidget = nullptr;
            emit startThread();
        }
    }
}

void NodeView::mousePressEvent(QMouseEvent* event)
{
    if (QGraphicsItem* item = itemAt(event->pos().x(), event->pos().y()))
    {
        if (QGraphicsProxyWidget* widget = dynamic_cast<QGraphicsProxyWidget*>(item))
        {
            mousePressPosition = event->pos();
            currentWidget = widget;
            emit selectionChanged(widget);
            if(event->button() == Qt::LeftButton)
            {
                // If within the 5x5 corners of the widget, resize
{'fraction_non_alphanumeric': 0.10862262038073908, 'fraction_numerical': 0.0033594624860022394, 'mean_word_length': 2.984386617100372, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 31, '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': '86427', 'n_tokens_mistral': 1459, 'n_tokens_neox': 1440, 'n_words': 395}