feat: custom node move implementation
This commit is contained in:
@@ -14,7 +14,8 @@ DisplayFlagButton::DisplayFlagButton(QGraphicsItem *parent)
|
||||
|
||||
disabledBrush_ = QBrush(disabledColor_);
|
||||
enabledBrush_ = QBrush(enabledColor_);
|
||||
hoveredBrush_ = QBrush(hoveredColor_);
|
||||
hoveredEnabledBrush_ = QBrush("#1391ff");
|
||||
hoveredDisabledBrush_ = QBrush(hoveredColor_);
|
||||
|
||||
|
||||
|
||||
@@ -32,17 +33,15 @@ void DisplayFlagButton::paint(QPainter *painter, const QStyleOptionGraphicsItem
|
||||
painter->setPen(Qt::NoPen);
|
||||
// painter->setBrush(QBrush(disabledColor_));
|
||||
QBrush usedBrush;
|
||||
if(hovered_)
|
||||
if(enabled_)
|
||||
{
|
||||
usedBrush = hoveredBrush_;
|
||||
}
|
||||
else if(enabled_)
|
||||
{
|
||||
usedBrush = enabledBrush_;
|
||||
if(hovered_) { usedBrush = hoveredEnabledBrush_; }
|
||||
else { usedBrush = enabledBrush_; }
|
||||
}
|
||||
else
|
||||
{
|
||||
usedBrush = disabledColor_;
|
||||
if(hovered_) { usedBrush = hoveredDisabledBrush_; }
|
||||
else { usedBrush = disabledBrush_; }
|
||||
}
|
||||
painter->setBrush(usedBrush);
|
||||
constexpr float roundRad = 3;
|
||||
|
||||
@@ -16,7 +16,8 @@ private:
|
||||
QColor hoveredColor_=QColor("#666666");
|
||||
QBrush disabledBrush_;
|
||||
QBrush enabledBrush_;
|
||||
QBrush hoveredBrush_;
|
||||
QBrush hoveredDisabledBrush_;
|
||||
QBrush hoveredEnabledBrush_;
|
||||
bool hovered_=false;
|
||||
bool enabled_=false;
|
||||
protected:
|
||||
|
||||
@@ -106,9 +106,15 @@ void Network::leftMousePressed(QMouseEvent *event)
|
||||
// display flag
|
||||
else if(QGraphicsItem* clickedDisplayFlag = itemOfType<DisplayFlagButton>(clickedItems))
|
||||
{
|
||||
std::cout << "HERE\n";
|
||||
static_cast<DisplayFlagButton*>(clickedDisplayFlag)->setEnabled(true);
|
||||
}
|
||||
else if(QGraphicsItem* clickedNode = itemOfType<NodeGraphic>(clickedItems))
|
||||
{
|
||||
nodeMoveDelta_=clickedNode->pos()-view_->mapToScene(event->pos());
|
||||
state_=State::MOVING_NODE;
|
||||
moveNodeBuffer.clear();
|
||||
moveNodeBuffer.push_back(clickedNode);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -179,6 +185,12 @@ void Network::mouseMoved(QMouseEvent *event)
|
||||
|
||||
QList<QGraphicsItem*> hoverItems = view_->items(event->pos());
|
||||
|
||||
if(state_==State::MOVING_NODE)
|
||||
{
|
||||
moveNodes(nodeMoveDelta_+view_->mapToScene(event->pos()));
|
||||
return;
|
||||
}
|
||||
|
||||
if(floatingEdge_)
|
||||
{
|
||||
if(QGraphicsItem* hoverSocket = itemOfType<SocketGraphic>(hoverItems); hoverSocket && hoverSocket!=startSocket_)
|
||||
@@ -193,7 +205,6 @@ void Network::mouseMoved(QMouseEvent *event)
|
||||
return;
|
||||
}
|
||||
|
||||
// QGraphicsItem* hoverItem = view_->itemAt(event->pos());
|
||||
QGraphicsItem* hoverEdge = itemOfType<NodeEdgeGraphic>(hoverItems);
|
||||
|
||||
// set node edge color
|
||||
@@ -221,6 +232,15 @@ void Network::mouseMoved(QMouseEvent *event)
|
||||
|
||||
}
|
||||
|
||||
void Network::moveNodes(QPointF pos)
|
||||
{
|
||||
|
||||
for(auto node : moveNodeBuffer)
|
||||
{
|
||||
node->setPos(pos);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Network::keyPressEvent(QKeyEvent *event)
|
||||
{
|
||||
@@ -311,8 +331,16 @@ void Network::mouseReleaseEvent(QMouseEvent *event)
|
||||
// std::cout << "----\nMOUSE RELEASED\n---\n";
|
||||
QList<QGraphicsItem*> hoverItems = view_->items(event->pos());
|
||||
QGraphicsItem* hoverSocket = itemOfType<SocketGraphic>(hoverItems);
|
||||
if(floatingEdge_ && hoverSocket)
|
||||
if(event->button() == Qt::LeftButton)
|
||||
{
|
||||
socketClicked(static_cast<SocketGraphic*>(hoverSocket), event);
|
||||
if(state_==State::MOVING_NODE)
|
||||
{
|
||||
moveNodeBuffer.clear();
|
||||
state_=State::DEFAULT;
|
||||
}
|
||||
else if(floatingEdge_ && hoverSocket)
|
||||
{
|
||||
socketClicked(static_cast<SocketGraphic*>(hoverSocket), event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,12 @@ public:
|
||||
void mouseMoved(QMouseEvent *event);
|
||||
QSize sizeHint() const override { return QSize(-1, -1); }
|
||||
|
||||
enum class State
|
||||
{
|
||||
DEFAULT,
|
||||
MOVING_NODE
|
||||
};
|
||||
|
||||
private:
|
||||
QLayout* mainLayout_;
|
||||
NetworkGraphicsScene* scene_;
|
||||
@@ -29,9 +35,11 @@ private:
|
||||
SocketGraphic* startSocket_=nullptr;
|
||||
|
||||
QGraphicsItem* prevHoverItem_=nullptr;
|
||||
// QPointer<QGraphicsItem> prevHoverItem_=nullptr;
|
||||
// std::shared_ptr<QGraphicsItem> prevHoverItem_=nullptr;
|
||||
// QList<QGraphicsItem*> prevHoverItems_;
|
||||
// nodes currently being moved
|
||||
std::vector<QGraphicsItem*> moveNodeBuffer;
|
||||
QPointF nodeMoveDelta_;
|
||||
|
||||
State state_=State::DEFAULT;
|
||||
|
||||
void keyPressEvent(QKeyEvent *event) override;
|
||||
void keyReleaseEvent(QKeyEvent *event) override;
|
||||
@@ -41,6 +49,8 @@ private:
|
||||
void highlightEdge(QGraphicsItem* edge, bool state);
|
||||
void leftMousePressed(QMouseEvent* event);
|
||||
|
||||
void moveNodes(QPointF pos);
|
||||
|
||||
template<typename T>
|
||||
bool isType(QGraphicsItem* item)
|
||||
{
|
||||
|
||||
@@ -26,7 +26,7 @@ NodeGraphic::NodeGraphic(enzo::nt::OpId id, QGraphicsItem *parent)
|
||||
bodyRect_ = QRect(-width*0.5f, -height*0.5f, width, height);
|
||||
iconScale_=height*0.55;
|
||||
|
||||
setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
|
||||
setFlag(QGraphicsItem::ItemSendsGeometryChanges);
|
||||
|
||||
initFonts();
|
||||
initIcon();
|
||||
@@ -176,11 +176,11 @@ void NodeGraphic::paint(QPainter *painter, const QStyleOptionGraphicsItem *optio
|
||||
|
||||
}
|
||||
|
||||
void NodeGraphic::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
|
||||
{
|
||||
QGraphicsItem::mouseMoveEvent(event);
|
||||
updatePositions(event->scenePos());
|
||||
}
|
||||
// void NodeGraphic::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
|
||||
// {
|
||||
// QGraphicsItem::mouseMoveEvent(event);
|
||||
// updatePositions(event->scenePos());
|
||||
// }
|
||||
|
||||
// void NodeGraphic::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
|
||||
// {
|
||||
@@ -188,10 +188,8 @@ void NodeGraphic::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
|
||||
// QGraphicsItem::mouseReleaseEvent(event);
|
||||
// }
|
||||
|
||||
void NodeGraphic::updatePositions(QPointF pos)
|
||||
void NodeGraphic::updatePositions()
|
||||
{
|
||||
// NOTE: in the future I might store socket ids as internal member variables,
|
||||
// but for now I'm just going based on order
|
||||
for(int socketIndex=0; socketIndex<inputs_.size(); ++socketIndex)
|
||||
{
|
||||
inputs_[socketIndex]->posChanged(getSocketScenePosition(socketIndex, enzo::nt::SocketIOType::Input));
|
||||
@@ -229,18 +227,13 @@ QRectF NodeGraphic::getBodyRect()
|
||||
|
||||
|
||||
|
||||
// QVariant NodeGraphic::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value)
|
||||
// {
|
||||
// if (change == ItemPositionChange && scene()) {
|
||||
// for(auto socket : inputs_)
|
||||
// {
|
||||
// socket->posChanged(value.toPointF());
|
||||
// }
|
||||
// for(auto socket : outputs_)
|
||||
// {
|
||||
// socket->posChanged(value.toPointF());
|
||||
// }
|
||||
// };
|
||||
QVariant NodeGraphic::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value)
|
||||
{
|
||||
QVariant returnVal = QGraphicsItem::itemChange(change, value);
|
||||
|
||||
// return QGraphicsItem::itemChange(change, value);
|
||||
// }
|
||||
if (change == ItemPositionHasChanged) {
|
||||
updatePositions();
|
||||
};
|
||||
|
||||
return returnVal;
|
||||
}
|
||||
|
||||
@@ -64,10 +64,10 @@ private:
|
||||
|
||||
DisplayFlagButton* displayFlagButton_;
|
||||
|
||||
void updatePositions(QPointF pos);
|
||||
void updatePositions();
|
||||
protected:
|
||||
// QVariant itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value) override;
|
||||
void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override;
|
||||
QVariant itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value) override;
|
||||
// void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override;
|
||||
// void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override;
|
||||
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user