From 79bf16a352cd4f95f07f243c563b8a9709053cdd Mon Sep 17 00:00:00 2001 From: Denes Matetelki Date: Sat, 18 Jun 2011 18:07:35 +0200 Subject: [PATCH] edge color with k --- edge.cpp | 10 ++++++++-- edge.h | 3 +++ graphwidget.cpp | 47 ++++++++++++++++++++++++++++++++++++++++++++--- graphwidget.h | 1 + node.cpp | 10 ++++++++++ node.h | 1 + 6 files changed, 67 insertions(+), 5 deletions(-) diff --git a/edge.cpp b/edge.cpp index 5a3650b..576c486 100644 --- a/edge.cpp +++ b/edge.cpp @@ -43,6 +43,12 @@ double Edge::getAngle() const return m_angle; } +void Edge::setColor(const QColor &color) +{ + m_color = color; + update(); +} + void Edge::adjust() { if (!m_sourceNode || !m_destNode) @@ -95,7 +101,7 @@ void Edge::paint(QPainter *painter, return; // Draw the line itself - painter->setPen(QPen(Qt::black, + painter->setPen(QPen(m_color, 1, Qt::SolidLine, Qt::RoundCap, @@ -113,7 +119,7 @@ void Edge::paint(QPainter *painter, QPointF(sin(m_angle - Edge::m_pi + Edge::m_pi / 3) * m_arrowSize, cos(m_angle - Edge::m_pi + Edge::m_pi / 3) * m_arrowSize); - painter->setBrush(Qt::black); + painter->setBrush(m_color); painter->drawPolygon(QPolygonF() << line.p2() << destArrowP1 << destArrowP2); diff --git a/edge.h b/edge.h index 9dfd831..47c7c0d 100644 --- a/edge.h +++ b/edge.h @@ -15,6 +15,8 @@ public: Node *sourceNode() const; Node *destNode() const; double getAngle() const; + void setColor(const QColor &color); + QColor color() const { return m_color; } void adjust(); @@ -33,6 +35,7 @@ private: QPointF m_sourcePoint; QPointF m_destPoint; double m_angle; + QColor m_color; static const qreal m_arrowSize; static const double m_pi; diff --git a/graphwidget.cpp b/graphwidget.cpp index a66e34b..0216785 100644 --- a/graphwidget.cpp +++ b/graphwidget.cpp @@ -25,6 +25,7 @@ GraphWidget::GraphWidget(MainWindow *parent) : m_editingNode(false), m_edgeAdding(false), m_edgeDeleting(false), + m_edgeColoring(false), m_contentChanged(false) { m_scene = new QGraphicsScene(this); @@ -104,9 +105,15 @@ void GraphWidget::readContentFromXmlFile(const QString &fileName) QDomElement e = edges.item(i).toElement(); if(!e.isNull()) { - m_scene->addItem(new Edge( - m_nodeList[e.attribute("source").toInt()], - m_nodeList[e.attribute("destination").toInt()])); + Edge *edge = new Edge( + m_nodeList[e.attribute("source").toInt()], + m_nodeList[e.attribute("destination").toInt()]); + edge->setColor(QColor(e.attribute("red").toFloat(), + e.attribute("green").toFloat(), + e.attribute("blue").toFloat(), + e.attribute("alpha").toFloat())); + + m_scene->addItem(edge); } } @@ -158,6 +165,11 @@ void GraphWidget::writeContentToXmlFile(const QString &fileName) QString::number(m_nodeList.indexOf(edge->sourceNode()))); cn.setAttribute( "destination", QString::number(m_nodeList.indexOf(edge->destNode()))); + cn.setAttribute( "red", QString::number(edge->color().red())); + cn.setAttribute( "green", QString::number(edge->color().green())); + cn.setAttribute( "blue", QString::number(edge->color().blue())); + cn.setAttribute( "alpha", QString::number(edge->color().alpha())); + edges_root.appendChild(cn); } @@ -220,6 +232,7 @@ void GraphWidget::keyPressEvent(QKeyEvent *event) event->key() == Qt::Key_D || // remove edge event->key() == Qt::Key_C || // node color event->key() == Qt::Key_T || // node text color + event->key() == Qt::Key_K || // edge text color ( event->modifiers() & Qt::ControlModifier && // moving node ( event->key() == Qt::Key_Up || event->key() == Qt::Key_Down || @@ -247,6 +260,11 @@ void GraphWidget::keyPressEvent(QKeyEvent *event) m_edgeDeleting = false; m_parent->statusBarMsg(tr("Edge deleting cancelled.")); } + else if (m_edgeColoring) + { + m_edgeColoring = false; + m_parent->statusBarMsg(tr("Edge coloring cancelled.")); + } else if(m_showingNodeNumbers) { m_hintNumber.clear(); @@ -428,6 +446,14 @@ void GraphWidget::keyPressEvent(QKeyEvent *event) break; } + + case Qt::Key_K: + { + m_parent->statusBarMsg(tr("Edge color: select other end-node.")); + m_edgeColoring = true; + break; + } + default: QGraphicsView::keyPressEvent(event); } @@ -559,6 +585,21 @@ void GraphWidget::nodeSelected(Node *node) removeEdge(m_activeNode, node); m_edgeDeleting = false; } + else if (m_edgeColoring) + { + QColorDialog dialog(this); + dialog.setWindowTitle(tr("Select edge color")); + dialog.setOption(QColorDialog::ShowAlphaChannel); + if (dialog.exec()) + { + QColor color = dialog.selectedColor(); + + Edge *edge = m_activeNode->edgeTo(node); + if (edge) + edge->setColor(color); + } + m_edgeColoring = false; + } else { setActiveNode(node); diff --git a/graphwidget.h b/graphwidget.h index 8824460..2ead792 100644 --- a/graphwidget.h +++ b/graphwidget.h @@ -61,6 +61,7 @@ private: bool m_editingNode; bool m_edgeAdding; bool m_edgeDeleting; + bool m_edgeColoring; bool m_contentChanged; QString m_fileName; diff --git a/node.cpp b/node.cpp index 788ebed..23363ce 100644 --- a/node.cpp +++ b/node.cpp @@ -277,6 +277,16 @@ QList Node::edgesFrom() const return list; } +Edge * Node::edgeTo(const Node *node) const +{ + foreach(EdgeElement element, m_edgeList) + if (element.edge->sourceNode() == node || + element.edge->destNode() == node) + return element.edge; + + return 0; +} + void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *w) diff --git a/node.h b/node.h index 17181c3..db3433c 100644 --- a/node.h +++ b/node.h @@ -40,6 +40,7 @@ public: QPointF intersect(const QLineF &line, const bool &reverse = false) const; QList edgesFrom() const; + Edge * edgeTo(const Node* node) const; protected: