<commit_before>#include "laneheaderscene.h"
#include "laneheaderitem.h"
#include "laneheaderview.h"
#include "taskmanager.h"
#include "rolepanel.h"
#include "roleeditor.h"
#include "common.h"
#include "misc.h"
#include <QGraphicsRectItem>
#include <QGraphicsView>
#include <QGraphicsSceneMouseEvent>
#include <QFocusEvent>
#include <QAction>
#include <QMenu>

LaneHeaderScene::LaneHeaderScene(qreal w, qreal h, QObject *parent)
    : QGraphicsScene(0, 0, w, h, parent)
    , hoverItem_(0)
    , currItem_(0)
    , contextMenuActive_(false)
{
    // add background item
    bgItem_ = new QGraphicsRectItem(sceneRect());
    bgItem_->setBrush(QBrush(QColor("#cccccc")));
    bgItem_->setZValue(-1);
    addItem(bgItem_);

    editAction_ = new QAction("Edit", 0);
    connect(editAction_, SIGNAL(triggered()), SLOT(editCurrentLane()));

    removeAction_ = new QAction("Remove", 0);
    connect(removeAction_, SIGNAL(triggered()), SLOT(removeCurrentLane()));

    moveLeftAction_ = new QAction("Move left", 0);
    connect(moveLeftAction_, SIGNAL(triggered()), SLOT(moveCurrentLaneLeft()));

    moveRightAction_ = new QAction("Move right", 0);
    connect(moveRightAction_, SIGNAL(triggered()), SLOT(moveCurrentLaneRight()));

    currMarker_ = new QGraphicsRectItem;
    currMarker_->setBrush(QBrush(QColor(255, 0, 0, 16)));
    {
        QPen pen(QBrush(QColor(255, 0, 0)), 2);
        pen.setCosmetic(true);
        currMarker_->setPen(pen);
    }
    currMarker_->setZValue(15);
    currMarker_->setVisible(false);
    addItem(currMarker_);
}

void LaneHeaderScene::updateFromTaskMgr()
{
    const QList<qint64> tmRoleIds = TaskManager::instance().roleIds();

    // remove header items for roles that no longer exist in the task manager
    foreach (LaneHeaderItem *hItem, headerItems_) {
        if (!tmRoleIds.contains(hItem->roleId())) {
            removeItem(hItem);
            headerItems_.removeOne(hItem);
        }
    }

    // add header items for unrepresented roles in the task manager
    const QList<qint64> hiRoleIds = headerItemRoleIds();
    foreach (qint64 tmRoleId, tmRoleIds) {
        if (!hiRoleIds.contains(tmRoleId)) {
            LaneHeaderItem *item = new LaneHeaderItem(tmRoleId);
            addItem(item);
            headerItems_.append(item);
        }
    }

    updateGeometryAndContents();
}

void LaneHeaderScene::updateGeometryAndContents()
{
    // update scene rect
    const QRectF srect = sceneRect();
    setSceneRect(srect.x(), srect.y(), headerItems_.size() * laneWidth() + laneHorizontalPadding(), views().first()->height() - 10);

    // update header item rects and texts
    const qreal lhpad = laneHorizontalPadding();
    const qreal lvpad = laneVerticalPadding();
    const qreal lwidth = laneWidth();
    int i = 0;
    foreach (LaneHeaderItem *item, headerItems_) {
        item->updateRect(QRectF(i * lwidth + lhpad, lvpad, lwidth - lhpad, height() - 2 * lvpad));
        item->updateProperties();
        i++;
    }

    // update background item
    bgItem_->setRect(sceneRect());
}

void LaneHeaderScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    // reset any existing highlighting
    if (hoverItem_)
        hoverItem_->highlight(false);

    // decide which lane header is hovered, if any
    foreach (QGraphicsItem *item, items(event->scenePos())) {
        hoverItem_ = dynamic_cast<LaneHeaderItem *>(item);
        if (hoverItem_)
            break;
    }

    // update
    if (hoverItem_) {
        hoverItem_->highlight(true);
        RolePanel::instance().setContents(TaskManager::instance().findRole(hoverItem_->roleId()).data());
    } else {
        RolePanel::instance().clearContents();
    }
}

void LaneHeaderScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        updateCurrLaneHeaderItem(false);

    } else if (event->button() == Qt::RightButton) {
        updateCurrLaneHeaderItem(true);

        if (!currItem_)
            return;

        // open context menu
        QMenu contextMenu;
        contextMenu.addAction(editAction_);
        contextMenu.addAction(removeAction_);

        contextMenu.addAction(moveLeftAction_);
        moveLeftAction_->setEnabled(headerItems_.indexOf(currItem_) > 0);
        contextMenu.addAction(moveRightAction_);
        moveRightAction_->setEnabled(headerItems_.indexOf(currItem_) < (headerItems_.size() - 1));

        contextMenuActive_ = true;
        contextMenu.exec(QCursor::pos());
        contextMenuActive_ = false;
    }
}

void LaneHeaderScene::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
{
    if ((event->button() == Qt::LeftButton) && hoverItem_)
        editCurrentLane();
}

void LaneHeaderScene::keyPressEvent(QKeyEvent *event)
{
    if (event->key() == Qt::Key_Left)
        moveCurrentLaneLeft();
    else if (event->key() == Qt::Key_Right)
        moveCurrentLaneRight();
}

void LaneHeaderScene::focusInEvent(QFocusEvent *)
{
    if (currItem_)
        currMarker_->setVisible(true);
}

void LaneHeaderScene::focusOutEvent(QFocusEvent *)<commit_msg>Allowed deleting lanes with keyboard<commit_after>#include "laneheaderscene.h"
#include "laneheaderitem.h"
#include "laneheaderview.h"
#include "taskmanager.h"
#include "rolepanel.h"
#include "roleeditor.h"
#include "common.h"
#include "misc.h"
#include <QGraphicsRectItem>
#include <QGraphicsView>
#include <QGraphicsSceneMouseEvent>
#include <QFocusEvent>
#include <QAction>
#include <QMenu>

LaneHeaderScene::LaneHeaderScene(qreal w, qreal h, QObject *parent)
    : QGraphicsScene(0, 0, w, h, parent)
    , hoverItem_(0)
    , currItem_(0)
    , contextMenuActive_(false)
{
    // add background item
    bgItem_ = new QGraphicsRectItem(sceneRect());
    bgItem_->setBrush(QBrush(QColor("#cccccc")));
    bgItem_->setZValue(-1);
    addItem(bgItem_);

    editAction_ = new QAction("Edit", 0);
    connect(editAction_, SIGNAL(triggered()), SLOT(editCurrentLane()));

    removeAction_ = new QAction("Remove", 0);
    connect(removeAction_, SIGNAL(triggered()), SLOT(removeCurrentLane()));

    moveLeftAction_ = new QAction("Move left", 0);
    connect(moveLeftAction_, SIGNAL(triggered()), SLOT(moveCurrentLaneLeft()));

    moveRightAction_ = new QAction("Move right", 0);
    connect(moveRightAction_, SIGNAL(triggered()), SLOT(moveCurrentLaneRight()));

    currMarker_ = new QGraphicsRectItem;
    currMarker_->setBrush(QBrush(QColor(255, 0, 0, 16)));
    {
        QPen pen(QBrush(QColor(255, 0, 0)), 2);
        pen.setCosmetic(true);
        currMarker_->setPen(pen);
    }
    currMarker_->setZValue(15);
    currMarker_->setVisible(false);
    addItem(currMarker_);
}

void LaneHeaderScene::updateFromTaskMgr()
{
    const QList<qint64> tmRoleIds = TaskManager::instance().roleIds();

    // remove header items for roles that no longer exist in the task manager
    foreach (LaneHeaderItem *hItem, headerItems_) {
        if (!tmRoleIds.contains(hItem->roleId())) {
            removeItem(hItem);
            headerItems_.removeOne(hItem);
        }
    }

    // add header items for unrepresented roles in the task manager
    const QList<qint64> hiRoleIds = headerItemRoleIds();
    foreach (qint64 tmRoleId, tmRoleIds) {
        if (!hiRoleIds.contains(tmRoleId)) {
            LaneHeaderItem *item = new LaneHeaderItem(tmRoleId);
            addItem(item);
            headerItems_.append(item);
        }
    }

    updateGeometryAndContents();
}

void LaneHeaderScene::updateGeometryAndContents()
{
    // update scene rect
    const QRectF srect = sceneRect();
    setSceneRect(srect.x(), srect.y(), headerItems_.size() * laneWidth() + laneHorizontalPadding(), views().first()->height() - 10);

    // update header item rects and texts
    const qreal lhpad = laneHorizontalPadding();
    const qreal lvpad = laneVerticalPadding();
    const qreal lwidth = laneWidth();
    int i = 0;
    foreach (LaneHeaderItem *item, headerItems_) {
        item->updateRect(QRectF(i * lwidth + lhpad, lvpad, lwidth - lhpad, height() - 2 * lvpad));
        item->updateProperties();
        i++;
    }

    // update background item
    bgItem_->setRect(sceneRect());
}

void LaneHeaderScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    // reset any existing highlighting
    if (hoverItem_)
        hoverItem_->highlight(false);

    // decide which lane header is hovered, if any
    foreach (QGraphicsItem *item, items(event->scenePos())) {
        hoverItem_ = dynamic_cast<LaneHeaderItem *>(item);
        if (hoverItem_)
            break;
    }

    // update
    if (hoverItem_) {
        hoverItem_->highlight(true);
        RolePanel::instance().setContents(TaskManager::instance().findRole(hoverItem_->roleId()).data());
    } else {
        RolePanel::instance().clearContents();
    }
}

void LaneHeaderScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        updateCurrLaneHeaderItem(false);

    } else if (event->button() == Qt::RightButton) {
        updateCurrLaneHeaderItem(true);

        if (!currItem_)
            return;

        // open context menu
        QMenu contextMenu;
        contextMenu.addAction(editAction_);
        contextMenu.addAction(removeAction_);

        contextMenu.addAction(moveLeftAction_);
        moveLeftAction_->setEnabled(headerItems_.indexOf(currItem_) > 0);
        contextMenu.addAction(moveRightAction_);
        moveRightAction_->setEnabled(headerItems_.indexOf(currItem_) < (headerItems_.size() - 1));

        contextMenuActive_ = true;
        contextMenu.exec(QCursor::pos());
        contextMenuActive_ = false;
    }
}

void LaneHeaderScene::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
{
    if ((event->button() == Qt::LeftButton) && hoverItem_)
        editCurrentLane();
}

void LaneHeaderScene::keyPressEvent(QKeyEvent *event)
{
    if (event->key() == Qt::Key_Left)
        moveCurrentLaneLeft();
    else if (event->key() == Qt::Key_Right)
        moveCurrentLaneRight();
    else if (event->key() == Qt::Key_Delete)
        removeCurrentLane();
}

void LaneHeaderScene::focusInEvent(QFocusEvent *)
{
    if (currItem_)
        currMarker_->setVisible(true);
}

void LaneHeaderScene::focusOutEvent(QFocusEvent *)
{'fraction_non_alphanumeric': 0.13214181072370348, 'fraction_numerical': 0.007031936712569587, 'mean_word_length': 3.6251129177958448, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 70, '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': '3489880', 'n_tokens_mistral': 3192, 'n_tokens_neox': 3088, 'n_words': 617}