FinceptTerminal is a modern finance application offering advanced market analytics, investment research, and economic data tools, designed for interactive exploration and data-driven decision-making in a user-friendly environment.
C++ ai-agentsalgorithmic-tradingbloomberg-terminalcppfinancefinancial-marketsfintechgood-first-issueinvestmentinvestment-researchmachine-learningopensourcepythonqtquantitative-financestock-markettrading
#include "screens/node_editor/canvas/PortItem.h"
#include "screens/node_editor/canvas/NodeItem.h"
#include "ui/theme/ThemeManager.h"
#include <QCursor>
#include <QGraphicsSceneMouseEvent>
#include <QPainter>
namespace fincept::workflow {
PortItem::PortItem(const PortDef& def, NodeItem* parent_node)
: QGraphicsObject(parent_node), def_(def), parent_node_(parent_node) {
setAcceptHoverEvents(true);
setCursor(Qt::CrossCursor);
setZValue(2);
setToolTip(def_.label);
}
QPointF PortItem::scene_center() const {
return mapToScene(boundingRect().center());
}
bool PortItem::can_connect_to(const PortItem* other) const {
if (!other || other == this)
return false;
if (other->parent_node() == parent_node_)
return false;
if (def_.direction == other->def_.direction)
return false;
// Connection type compatibility:
// - Same type always connects
// - Main is universal — connects to/from any type
// - Otherwise must match exactly
auto a = def_.connection_type;
auto b = other->def_.connection_type;
if (a != b && a != ConnectionType::Main && b != ConnectionType::Main)
return false;
return true;
}
void PortItem::add_edge(EdgeItem* edge) {
if (!edges_.contains(edge))
edges_.append(edge);
}
void PortItem::remove_edge(EdgeItem* edge) {
edges_.removeAll(edge);
}
QRectF PortItem::boundingRect() const {
return QRectF(-kHitRadius, -kHitRadius, kHitRadius * 2, kHitRadius * 2);
}
void PortItem::set_connect_highlight(int state) {
connect_highlight_ = state;
update();
}
void PortItem::paint(QPainter* painter, const QStyleOptionGraphicsItem*, QWidget*) {
painter->setRenderHint(QPainter::Antialiasing);
const auto& t = ui::ThemeManager::instance().tokens();
const bool connected = !edges_.isEmpty();
QColor fill;
if (connect_highlight_ == 1)
fill = QColor(t.positive);
else if (connect_highlight_ == -1)
fill = QColor(t.negative);
else if (hovered_ || connected)
fill = QColor(t.accent);
else
fill = QColor(t.border_med);
QColor border_color(t.border_dim);
qreal radius = kVisualRadius;
if (hovered_ || connect_highlight_ != 0)
radius = kVisualRadius + 1.0; // slightly larger on hover/highlight
painter->setPen(QPen(border_color, 1.0));
painter->setBrush(fill);
painter->drawEllipse(QPointF(0, 0), radius, radius);
}
void PortItem::hoverEnterEvent(QGraphicsSceneHoverEvent* event) {
hovered_ = true;
update();
QGraphicsObject::hoverEnterEvent(event);
}
void PortItem::hoverLeaveEvent(QGraphicsSceneHoverEvent* event) {
hovered_ = false;
update();
QGraphicsObject::hoverLeaveEvent(event);
}
void PortItem::mousePressEvent(QGraphicsSceneMouseEvent* event) {
if (event->button() == Qt::LeftButton) {
emit connection_started(this);
event->accept();
} else {
QGraphicsObject::mousePressEvent(event);
}
}
} // namespace fincept::workflow