nodeview: better implementation of larger connector hitboxes

This commit is contained in:
itsmattkc
2022-07-27 12:40:27 -07:00
parent 992f3fd3af
commit 183dc8c48e
4 changed files with 21 additions and 19 deletions
+1 -17
View File
@@ -432,23 +432,7 @@ void NodeView::mousePressEvent(QMouseEvent *event)
if (HandPress(event)) return;
// Get the item that the user clicked on, if any
QGraphicsItem* item = nullptr;
{
// Prioritize connectors. I tried overriding boundingRect() and contains() on the connector
// object, but it ended up not working or causing other issues, so this is my hackier solution
const int radius = fontMetrics().height()/2;
QRect connector_rect(event->pos().x()-radius, event->pos().y()-radius, radius*2, radius*2);
QList<QGraphicsItem*> items = this->items(connector_rect);
for (QGraphicsItem *i : items) {
if (dynamic_cast<NodeViewItemConnector*>(i)) {
item = i;
break;
}
}
}
if (!item) {
item = itemAt(event->pos());
}
QGraphicsItem* item = itemAt(event->pos());
if (event->button() == Qt::LeftButton) {
// Sane defaults
+2 -2
View File
@@ -581,7 +581,7 @@ QPointF NodeViewItem::GetInputPoint() const
QPointF NodeViewItem::GetOutputPoint() const
{
QPointF p = output_connector_->scenePos();
QRectF r = output_connector_->boundingRect();
QRectF r = output_connector_->polygon().boundingRect();
switch (flow_dir_) {
case NodeViewCommon::kLeftToRight:
@@ -628,7 +628,7 @@ void NodeViewItem::UpdateNodePosition()
void NodeViewItem::UpdateInputConnectorPosition()
{
QRectF output_rect = input_connector_->boundingRect();
QRectF output_rect = input_connector_->polygon().boundingRect();
NodeViewCommon::FlowDirection using_flow_dir = flow_dir_;
@@ -81,4 +81,19 @@ void NodeViewItemConnector::SetFlowDirection(NodeViewCommon::FlowDirection dir)
setPolygon(p);
}
QPainterPath NodeViewItemConnector::shape() const
{
// Yes, we skip QGraphicsPolygonItem because it adds the polygon. QGraphicsItem adds the
// boundingRect which we modify below
return QGraphicsItem::shape(); // clazy:exclude=skipped-base-method
}
QRectF NodeViewItemConnector::boundingRect() const
{
QRectF b = this->polygon().boundingRect();
const int radius = QFontMetrics(QFont()).height()/2;
b.adjust(-radius, -radius, radius, radius);
return b;
}
}
@@ -39,6 +39,9 @@ public:
return output_;
}
virtual QPainterPath shape() const override;
virtual QRectF boundingRect() const override;
private:
bool output_;