<commit_before>#include "FlowView.hpp"

#include <QtWidgets/QGraphicsScene>

#include <QtGui/QPen>
#include <QtGui/QBrush>
#include <QtWidgets/QMenu>

#include <QtCore/QRectF>

#include <QtOpenGL>
#include <QtWidgets>

#include <QDebug>
#include <iostream>

#include "FlowScene.hpp"
#include "DataModelRegistry.hpp"
#include "Node.hpp"
#include "NodeGraphicsObject.hpp"
#include "ConnectionGraphicsObject.hpp"
#include "StyleCollection.hpp"

using QtNodes::FlowView;
using QtNodes::FlowScene;

FlowView::
FlowView(FlowScene *scene)
  : QGraphicsView(scene)
  , _scene(scene)
{
  setDragMode(QGraphicsView::ScrollHandDrag);
  setRenderHint(QPainter::Antialiasing);

  auto const &flowViewStyle = StyleCollection::flowViewStyle();

  setBackgroundBrush(flowViewStyle.BackgroundColor);

  //setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
  //setViewportUpdateMode(QGraphicsView::MinimalViewportUpdate);
  setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
  setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);

  setTransformationAnchor(QGraphicsView::AnchorUnderMouse);

  setCacheMode(QGraphicsView::CacheBackground);

  //setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers)));
}


void
FlowView::
contextMenuEvent(QContextMenuEvent *event)
{
  QMenu modelMenu;

  auto filterActionText = QStringLiteral("skip me");

  auto *txtBox = new QLineEdit(&modelMenu);
  txtBox->setPlaceholderText(QStringLiteral("Filter"));
  txtBox->setClearButtonEnabled(true);

  auto *txtBoxAction = new QWidgetAction(&modelMenu);
  txtBoxAction->setDefaultWidget(txtBox);
  txtBoxAction->setText(filterActionText);

  modelMenu.addAction(txtBoxAction);

  connect(txtBox, &QLineEdit::textChanged, [&](const QString &text) 
  {
    for (auto action : modelMenu.actions())
    {
      auto actionText = action->text();
      if (actionText != filterActionText && !actionText.contains(text, Qt::CaseInsensitive))
      {
        action->setVisible(false);
      }
      else
      {
        action->setVisible(true);
      }
    }
  });

  for (auto const &modelRegistry : _scene->registry().registeredModels())
  {
    QString const &modelName = modelRegistry.first;
    modelMenu.addAction(modelName);
  }
  
  if (QAction * action = modelMenu.exec(event->globalPos()))
  {
    QString modelName = action->text();

    auto type = _scene->registry().create(modelName);

    if (type)
    {
      auto& node = _scene->createNode(std::move(type));
<commit_msg>Make the filter bar in the context menu have focus by default. (#55)<commit_after>#include "FlowView.hpp"

#include <QtWidgets/QGraphicsScene>

#include <QtGui/QPen>
#include <QtGui/QBrush>
#include <QtWidgets/QMenu>

#include <QtCore/QRectF>

#include <QtOpenGL>
#include <QtWidgets>

#include <QDebug>
#include <iostream>

#include "FlowScene.hpp"
#include "DataModelRegistry.hpp"
#include "Node.hpp"
#include "NodeGraphicsObject.hpp"
#include "ConnectionGraphicsObject.hpp"
#include "StyleCollection.hpp"

using QtNodes::FlowView;
using QtNodes::FlowScene;

FlowView::
FlowView(FlowScene *scene)
  : QGraphicsView(scene)
  , _scene(scene)
{
  setDragMode(QGraphicsView::ScrollHandDrag);
  setRenderHint(QPainter::Antialiasing);

  auto const &flowViewStyle = StyleCollection::flowViewStyle();

  setBackgroundBrush(flowViewStyle.BackgroundColor);

  //setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
  //setViewportUpdateMode(QGraphicsView::MinimalViewportUpdate);
  setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
  setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);

  setTransformationAnchor(QGraphicsView::AnchorUnderMouse);

  setCacheMode(QGraphicsView::CacheBackground);

  //setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers)));
}


void
FlowView::
contextMenuEvent(QContextMenuEvent *event)
{
  QMenu modelMenu;

  auto filterActionText = QStringLiteral("skip me");

  auto *txtBox = new QLineEdit(&modelMenu);
  txtBox->setPlaceholderText(QStringLiteral("Filter"));
  txtBox->setClearButtonEnabled(true);

  auto *txtBoxAction = new QWidgetAction(&modelMenu);
  txtBoxAction->setDefaultWidget(txtBox);
  txtBoxAction->setText(filterActionText);

  modelMenu.addAction(txtBoxAction);

  connect(txtBox, &QLineEdit::textChanged, [&](const QString &text) 
  {
    for (auto action : modelMenu.actions())
    {
      auto actionText = action->text();
      if (actionText != filterActionText && !actionText.contains(text, Qt::CaseInsensitive))
      {
        action->setVisible(false);
      }
      else
      {
        action->setVisible(true);
      }
    }
  });

  for (auto const &modelRegistry : _scene->registry().registeredModels())
  {
    QString const &modelName = modelRegistry.first;
    modelMenu.addAction(modelName);
  }
  
  // make sure the text box gets focus so the user doesn't have to click on it
  txtBox->setFocus();
  
  if (QAction * action = modelMenu.exec(event->globalPos()))
  {
    QString modelName = action->text();

    auto type = _scene->registry().create(modelName);

    if (type)
    {
      auto& node = _scene->createNode(std::move(type));

{'fraction_non_alphanumeric': 0.12872655478775913, 'fraction_numerical': 0.0003948667324777887, 'mean_word_length': 5.26980198019802, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 46, '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': '6296316', 'n_tokens_mistral': 1627, 'n_tokens_neox': 1582, 'n_words': 277}