-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add nodeEdit mode #46
Changes from 1 commit
f1e846b
97da493
f8ef335
d91157b
356a111
07139ee
28fe85f
7f46c72
abf001f
8e4adab
c503329
d66e976
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,6 +57,8 @@ BaseGraphGL::BaseGraphGL(QWidget* parent) | |
connect(m_ui->bReset, SIGNAL(pressed()), SLOT(resetView())); | ||
connect(m_ui->deleteEdge, SIGNAL(clicked()), this, SLOT(removeEdgeEvent())); | ||
|
||
connect(this, SIGNAL(nodesMoved()), SLOT(slotUpdateSelection())); | ||
|
||
m_bCenter = new QtMaterialIconButton(QIcon(":/icons/material/center_white_18"), this); | ||
m_bCenter->setToolTip("centralize selection"); | ||
m_bCenter->setCheckable(true); | ||
|
@@ -147,32 +149,34 @@ void BaseGraphGL::slotDeleteSelectedNodes() | |
updateCache(); | ||
} | ||
|
||
void BaseGraphGL::createNode(QPointF pos) | ||
void BaseGraphGL::createNode(const QPointF pos) | ||
{ | ||
QPointF p = nodePoint(pos - m_origin); | ||
m_abstractGraph->addNode(m_attrs, p.x(), p.y()); | ||
updateCache(); | ||
} | ||
|
||
void BaseGraphGL::deleteNode(QPointF pos) | ||
void BaseGraphGL::deleteNode(const QPointF pos) | ||
{ | ||
const Node& node = findNode(pos); | ||
clearSelection(); | ||
m_abstractGraph->removeNode(node); | ||
updateCache(); | ||
} | ||
|
||
void BaseGraphGL::moveSelectedNodes(Node& node, QPointF pos) { | ||
void BaseGraphGL::moveSelectedNodes(Node& node, const QPointF pos) { | ||
QPointF v = nodePoint(pos - m_origin) - QPointF(node.x(), node.y()); | ||
for (Node _node : m_selectedNodes) { | ||
_node.setCoords(_node.x() + v.x(), _node.y() + v.y()); | ||
} | ||
emit(nodesMoved()); | ||
updateCache(); | ||
} | ||
|
||
void BaseGraphGL::moveNode(Node& node, QPointF pos) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
QPointF p = nodePoint(pos - m_origin); | ||
node.setCoords(p.x(), p.y()); | ||
emit(nodesMoved()); | ||
updateCache(); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,6 +73,27 @@ GraphView::Star GraphView::createStar(const Node& node, | |
return star; | ||
} | ||
|
||
void GraphView::slotUpdateSelection() { | ||
for (auto selectedNode : m_selectedNodes) { | ||
int i = selectedNode.first; | ||
const Node node = selectedNode.second; | ||
const QPointF xy = QPointF(node.x() * currEdgeSize(), node.y() * currEdgeSize()); | ||
|
||
Star selectedStar(node, xy, {}); | ||
selectedStar.edges.reserve(node.outEdges().size()); | ||
for (auto const& ep : node.outEdges()) { | ||
QPointF xy2 = nodePoint(ep.second.neighbour(), currEdgeSize()); | ||
QLineF line(xy, xy2); | ||
if (!m_showNodes || line.length() - m_nodeRadius * 2. > 4.0) { | ||
selectedStar.edges.push_back({ ep.second, line }); | ||
} | ||
} | ||
selectedStar.edges.shrink_to_fit(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this approach isn't very efficient either actually, this code is mostly a copy&paste from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Hm not sure I quite get that. Do you mean to refactor this without using signals? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, there's no gain in using the signal&slot in this case There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My main point here is that your "slotUpdateSelection" is just a copy&paste from "createStar"; we don't need to duplicate this code. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I reformatted a bit as to directly call |
||
|
||
m_selectedStars.at(i) = selectedStar; | ||
} | ||
} | ||
|
||
CacheStatus GraphView::refreshCache() | ||
{ | ||
if (paintingActive()) { | ||
|
@@ -322,46 +343,32 @@ void GraphView::drawSelectedStars(QPainter& painter, double nodeRadius) const | |
|
||
painter.setOpacity(1.0); | ||
|
||
for (auto selectedNode : m_selectedNodes) { | ||
for (auto selectedStar : m_selectedStars) { | ||
// draw shadow of the selected node | ||
const Node node = selectedNode.second; | ||
const QPointF xy = QPointF(node.x() * currEdgeSize(), node.y() * currEdgeSize()); | ||
|
||
Star selectedStar(node, xy, {}); | ||
selectedStar.edges.reserve(node.outEdges().size()); | ||
for (auto const& ep : node.outEdges()) { | ||
QPointF xy2 = nodePoint(ep.second.neighbour(), currEdgeSize()); | ||
QLineF line(xy, xy2); | ||
if (!m_showNodes || line.length() - m_nodeRadius * 2. > 4.0) { | ||
selectedStar.edges.push_back({ep.second, line}); | ||
} | ||
} | ||
selectedStar.edges.shrink_to_fit(); | ||
|
||
|
||
painter.save(); | ||
double shadowRadius = nodeRadius*1.5; | ||
QRadialGradient r(selectedStar.xy, shadowRadius, selectedStar.xy); | ||
QRadialGradient r(selectedStar.second.xy, shadowRadius, selectedStar.second.xy); | ||
r.setColorAt(0, Qt::black); | ||
r.setColorAt(1, m_background.color()); | ||
painter.setBrush(r); | ||
painter.setPen(Qt::transparent); | ||
painter.drawEllipse(selectedStar.xy, shadowRadius, shadowRadius); | ||
painter.drawEllipse(selectedStar.second.xy, shadowRadius, shadowRadius); | ||
painter.restore(); | ||
|
||
painter.save(); | ||
// highlight immediate edges | ||
painter.setPen(QPen(Qt::darkGray, m_edgePen.width() + 3)); | ||
for (auto const& ep : selectedStar.edges) { | ||
for (auto const& ep : selectedStar.second.edges) { | ||
painter.drawLine(ep.second); | ||
} | ||
|
||
// draw selected node | ||
painter.setPen(m_nodePen); | ||
drawNode(painter, selectedStar, nodeRadius); | ||
drawNode(painter, selectedStar.second, nodeRadius); | ||
|
||
// draw neighbours | ||
const Edges& oe = selectedStar.node.outEdges(); | ||
const Edges& oe = selectedStar.second.node.outEdges(); | ||
const double esize = currEdgeSize(); | ||
for (auto const& e : oe) { | ||
const Node& n = e.second.neighbour(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pos
should be passed as a const reference, i.e.,const QPointF&
(the same for all other calls)