feat: add display flag button click logic

This commit is contained in:
parker
2025-06-24 00:51:25 +01:00
parent 4471a9a537
commit 1f367b82fd
5 changed files with 72 additions and 5 deletions

View File

@@ -9,13 +9,41 @@ DisplayFlagButton::DisplayFlagButton(QGraphicsItem *parent)
constexpr float width = 8; constexpr float width = 8;
constexpr float height = 14; constexpr float height = 14;
baseRect_ = QRectF(-width/2.0f, -height/2.0f, width, height); baseRect_ = QRectF(-width/2.0f, -height/2.0f, width, height);
setAcceptHoverEvents(true);
disabledBrush_ = QBrush(disabledColor_);
enabledBrush_ = QBrush(enabledColor_);
hoveredBrush_ = QBrush(hoveredColor_);
} }
void DisplayFlagButton::setEnabled(bool enabled)
{
enabled_ = enabled;
update();
}
void DisplayFlagButton::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) void DisplayFlagButton::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{ {
painter->setPen(Qt::NoPen); painter->setPen(Qt::NoPen);
painter->setBrush(QBrush(disabledColor_)); // painter->setBrush(QBrush(disabledColor_));
QBrush usedBrush;
if(hovered_)
{
usedBrush = hoveredBrush_;
}
else if(enabled_)
{
usedBrush = enabledBrush_;
}
else
{
usedBrush = disabledColor_;
}
painter->setBrush(usedBrush);
constexpr float roundRad = 3; constexpr float roundRad = 3;
painter->drawRoundedRect(baseRect_, roundRad, roundRad); painter->drawRoundedRect(baseRect_, roundRad, roundRad);
} }
@@ -29,3 +57,17 @@ float DisplayFlagButton::getWidth()
{ {
return baseRect_.width(); return baseRect_.width();
} }
void DisplayFlagButton::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
hovered_ = true;
update();
}
void DisplayFlagButton::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
hovered_ = false;
update();
}

View File

@@ -1,5 +1,6 @@
#pragma once #pragma once
#include <QGraphicsItem> #include <QGraphicsItem>
#include <QBrush>
class DisplayFlagButton class DisplayFlagButton
: public QGraphicsItem : public QGraphicsItem
@@ -7,11 +8,21 @@ class DisplayFlagButton
public: public:
DisplayFlagButton(QGraphicsItem *parent = nullptr); DisplayFlagButton(QGraphicsItem *parent = nullptr);
float getWidth(); float getWidth();
void setEnabled(bool enabled);
private: private:
QRectF baseRect_; QRectF baseRect_;
QColor disabledColor_=QColor("#373737"); QColor disabledColor_=QColor("#373737");
QColor enabledColor_=QColor("#00BFFF"); QColor enabledColor_=QColor("#00BFFF");
QColor hoveredColor_=QColor("#666666");
QBrush disabledBrush_;
QBrush enabledBrush_;
QBrush hoveredBrush_;
bool hovered_=false;
bool enabled_=false;
protected: protected:
QRectF boundingRect() const override; QRectF boundingRect() const override;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override; void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
void hoverEnterEvent(QGraphicsSceneHoverEvent *event) override;
void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) override;
}; };

View File

@@ -1,4 +1,5 @@
#include "gui/network/Network.h" #include "gui/network/Network.h"
#include "gui/network/DisplayFlagButton.h"
#include "gui/network/NodeEdgeGraphic.h" #include "gui/network/NodeEdgeGraphic.h"
#include "gui/network/NetworkGraphicsView.h" #include "gui/network/NetworkGraphicsView.h"
#include "gui/network/NetworkGraphicsScene.h" #include "gui/network/NetworkGraphicsScene.h"
@@ -115,14 +116,22 @@ void Network::leftMousePressed(QMouseEvent *event)
{ {
deleteEdge(clickedEdge); deleteEdge(clickedEdge);
} }
// socket logic
else if(clickedSocket) else if(clickedSocket)
{ {
socketClicked(static_cast<SocketGraphic*>(clickedSocket), event); socketClicked(static_cast<SocketGraphic*>(clickedSocket), event);
} }
// floating edge
else if(floatingEdge_) else if(floatingEdge_)
{ {
destroyFloatingEdge(); destroyFloatingEdge();
} }
// display flag
else if(QGraphicsItem* clickedDisplayFlag = itemOfType<DisplayFlagButton>(clickedItems))
{
std::cout << "HERE\n";
static_cast<DisplayFlagButton*>(clickedDisplayFlag)->setEnabled(true);
}
} }

View File

@@ -18,9 +18,8 @@ NodeEdgeGraphic::NodeEdgeGraphic(SocketGraphic* socket1, SocketGraphic* socket2,
NodeEdgeGraphic::~NodeEdgeGraphic() NodeEdgeGraphic::~NodeEdgeGraphic()
{ {
std::cout << "edge destructor\n"; std::cout << "edge destructor\n";
scene()->removeItem(this); cleanUp();
socket1_->removeEdge(this); std::cout << "destructor finished\n";
socket2_->removeEdge(this);
} }
void NodeEdgeGraphic::updatePath() void NodeEdgeGraphic::updatePath()
@@ -95,4 +94,9 @@ void NodeEdgeGraphic::paint(QPainter *painter, const QStyleOptionGraphicsItem *o
} }
void NodeEdgeGraphic::cleanUp()
{
scene()->removeItem(this);
socket1_->removeEdge(this);
socket2_->removeEdge(this);
}

View File

@@ -33,5 +33,6 @@ private:
qreal padding_=40; qreal padding_=40;
void updatePath(); void updatePath();
void cleanUp();
}; };