Skip to content
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

Merged
merged 12 commits into from
Aug 31, 2019
10 changes: 7 additions & 3 deletions src/gui/basegraphgl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -147,32 +149,34 @@ void BaseGraphGL::slotDeleteSelectedNodes()
updateCache();
}

void BaseGraphGL::createNode(QPointF pos)
void BaseGraphGL::createNode(const QPointF pos)
Copy link
Member

@cardinot cardinot Aug 27, 2019

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)

{
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const QPointF& pos

QPointF p = nodePoint(pos - m_origin);
node.setCoords(p.x(), p.y());
emit(nodesMoved());
updateCache();
}

Expand Down
10 changes: 6 additions & 4 deletions src/gui/basegraphgl.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ class BaseGraphGL : public QOpenGLWidget, public GraphGLInterface

void updateCache(bool force=false);

void createNode(QPointF pos);
void deleteNode(QPointF pos);
void createNode(const QPointF pos);
void deleteNode(const QPointF pos);

inline void paintEvent(QPaintEvent*) override;
void mousePressEvent(QMouseEvent* e) override;
Expand All @@ -128,11 +128,13 @@ class BaseGraphGL : public QOpenGLWidget, public GraphGLInterface
void nodeSelected(const Node&);
void nodeDeselected(const Node&);
void clearedSelected();
void nodesMoved();
void updateWidgets(bool) const;

public slots:
virtual void zoomIn();
virtual void zoomOut();
virtual void slotUpdateSelection() = 0;

void slotFullInspectorVisible(int visible);
void setCurrentStep(int step);
Expand Down Expand Up @@ -167,8 +169,8 @@ private slots:

void attrValueChanged(int attrId) const;

void moveSelectedNodes(Node& node, QPointF pos);
void moveNode(Node& node, QPointF pos);
void moveSelectedNodes(Node& node, const QPointF pos);
void moveNode(Node& node, const QPointF pos);

void setupInspector();

Expand Down
47 changes: 27 additions & 20 deletions src/gui/graphview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this approach isn't very efficient either
because you'll always end up looping through all nodes twice

actually, this code is mostly a copy&paste from createStar(). Could we just call the createStart() function straight away when it's needed? (i.e., instead of emitting the nodesMoved() signals)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we just call the createStart() function straight away when it's needed? (i.e., instead of emitting the nodesMoved() signals)

Hm not sure I quite get that. Do you mean to refactor this without using signals?

Copy link
Member

Choose a reason for hiding this comment

The 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

Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reformatted a bit as to directly call createStar, but I still can't see a more efficient way to call the UpdateSelection. This can't be called directly from the painting methods and neither can it not be called each time a node is moved. What am I missing?


m_selectedStars.at(i) = selectedStar;
}
}

CacheStatus GraphView::refreshCache()
{
if (paintingActive()) {
Expand Down Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions src/gui/graphview.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class GraphView : public BaseGraphGL
public slots:
inline void zoomIn() override;
inline void zoomOut() override;
void slotUpdateSelection() override;
void setEdgeScale(int v);
void setEdgeWidth(int v);

Expand Down
11 changes: 11 additions & 0 deletions src/gui/gridview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,17 @@ CacheStatus GridView::refreshCache()
return CacheStatus::Ready;
}

void GridView::slotUpdateSelection() {
for (auto selectedNode : m_selectedNodes) {
int i = selectedNode.first;
const Node node = selectedNode.second;

QRectF selectedCellRect = cellRect(node, m_nodeRadius);

m_selectedCells.at(i).rect = selectedCellRect;
}
}

void GridView::paintFrame(QPainter& painter) const
{
if (m_nodeAttr < 0 || !m_nodeCMap) {
Expand Down
3 changes: 3 additions & 0 deletions src/gui/gridview.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ class GridView : public BaseGraphGL
public:
explicit GridView(QWidget* parent);

public slots:
void slotUpdateSelection() override;

protected:
void paintFrame(QPainter& painter) const override;
Node selectNode(const QPointF& pos, bool center) override;
Expand Down