From eb97252d33ce275175e3cb964eb6b6c6551048af Mon Sep 17 00:00:00 2001 From: Denes Matetelki Date: Mon, 20 Jun 2011 11:14:01 +0200 Subject: [PATCH] Edge has color and width. Edge width changes as parent Node is scaled. Edge has no arrow any more and source/dest point is the source/dest Node's sceneBoundingRect.center(). No more alpha in colors (it was useless). Edge coloring is disabled, edge has the color of the parent Node. --- edge.cpp | 35 ++++++++++++++------ edge.h | 4 +++ graphwidget.cpp | 87 +++++++++++++++++++++++++++---------------------- graphwidget.h | 2 +- node.cpp | 19 ++++++++--- node.h | 2 +- 6 files changed, 94 insertions(+), 55 deletions(-) diff --git a/edge.cpp b/edge.cpp index 576c486..970c711 100644 --- a/edge.cpp +++ b/edge.cpp @@ -13,8 +13,12 @@ const qreal Edge::m_arrowSize = 7; Edge::Edge(Node *sourceNode, Node *destNode) : m_angle(-1) + , m_color(0,0,0) + , m_width(1) { setAcceptedMouseButtons(0); + setZValue(1); + m_sourceNode = sourceNode; m_destNode = destNode; m_sourceNode->addEdge(this,true); @@ -49,6 +53,15 @@ void Edge::setColor(const QColor &color) update(); } +void Edge::setWidth(const qreal &width) +{ + if (width < 1 || width > 100) + return; + + m_width = width; + update(); +} + void Edge::adjust() { if (!m_sourceNode || !m_destNode) @@ -61,8 +74,10 @@ void Edge::adjust() if (line.length() > qreal(20.)) { - m_sourcePoint = m_sourceNode->intersect(line); - m_destPoint = m_destNode->intersect(line,true); +// m_sourcePoint = m_sourceNode->intersect(line); +// m_destPoint = m_destNode->intersect(line,true); + m_sourcePoint = m_sourceNode->sceneBoundingRect().center(); + m_destPoint = m_destNode->sceneBoundingRect().center(); } else { m_sourcePoint = m_destPoint = line.p1(); @@ -102,7 +117,7 @@ void Edge::paint(QPainter *painter, // Draw the line itself painter->setPen(QPen(m_color, - 1, + m_width, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)); @@ -111,13 +126,13 @@ void Edge::paint(QPainter *painter, if (line.length() < m_arrowSize) return; - // Draw the arrows - QPointF destArrowP1 = m_destPoint + - QPointF(sin(m_angle - Edge::m_pi / 3) * m_arrowSize, - cos(m_angle - Edge::m_pi / 3) * m_arrowSize); - QPointF destArrowP2 = m_destPoint + - 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); + // Draw the arrow +// QPointF destArrowP1 = m_destPoint + +// QPointF(sin(m_angle - Edge::m_pi / 3) * m_arrowSize, +// cos(m_angle - Edge::m_pi / 3) * m_arrowSize); +// QPointF destArrowP2 = m_destPoint + +// 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(m_color); painter->drawPolygon(QPolygonF() << line.p2() diff --git a/edge.h b/edge.h index 47c7c0d..6ca4b25 100644 --- a/edge.h +++ b/edge.h @@ -15,8 +15,11 @@ public: Node *sourceNode() const; Node *destNode() const; double getAngle() const; + void setColor(const QColor &color); QColor color() const { return m_color; } + void setWidth(const qreal &width); + qreal width() const { return m_width; } void adjust(); @@ -36,6 +39,7 @@ private: QPointF m_destPoint; double m_angle; QColor m_color; + qreal m_width; static const qreal m_arrowSize; static const double m_pi; diff --git a/graphwidget.cpp b/graphwidget.cpp index 0216785..bc99529 100644 --- a/graphwidget.cpp +++ b/graphwidget.cpp @@ -25,7 +25,7 @@ GraphWidget::GraphWidget(MainWindow *parent) : m_editingNode(false), m_edgeAdding(false), m_edgeDeleting(false), - m_edgeColoring(false), +// m_edgeColoring(false), m_contentChanged(false) { m_scene = new QGraphicsScene(this); @@ -89,12 +89,12 @@ void GraphWidget::readContentFromXmlFile(const QString &fileName) node->setScale(e.attribute("scale").toFloat()); node->setColor(QColor(e.attribute("bg_red").toFloat(), e.attribute("bg_green").toFloat(), - e.attribute("bg_blue").toFloat(), - e.attribute("bg_alpha").toFloat())); + e.attribute("bg_blue").toFloat()/*, + e.attribute("bg_alpha").toFloat()*/)); node->setTextColor(QColor(e.attribute("text_red").toFloat(), e.attribute("text_green").toFloat(), - e.attribute("text_blue").toFloat(), - e.attribute("text_alpha").toFloat())); + e.attribute("text_blue").toFloat()/*, + e.attribute("text_alpha").toFloat()*/)); m_nodeList.append(node); } } @@ -110,8 +110,9 @@ void GraphWidget::readContentFromXmlFile(const QString &fileName) 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())); + e.attribute("blue").toFloat()/*, + e.attribute("alpha").toFloat()*/)); + edge->setWidth(e.attribute("width").toFloat()); m_scene->addItem(edge); } @@ -147,11 +148,11 @@ void GraphWidget::writeContentToXmlFile(const QString &fileName) cn.setAttribute( "bg_red", QString::number(node->color().red())); cn.setAttribute( "bg_green", QString::number(node->color().green())); cn.setAttribute( "bg_blue", QString::number(node->color().blue())); - cn.setAttribute( "bg_alpha", QString::number(node->color().alpha())); +// cn.setAttribute( "bg_alpha", QString::number(node->color().alpha())); cn.setAttribute( "text_red", QString::number(node->textColor().red())); cn.setAttribute( "text_green", QString::number(node->textColor().green())); cn.setAttribute( "text_blue", QString::number(node->textColor().blue())); - cn.setAttribute( "text_alpha", QString::number(node->textColor().alpha())); +// cn.setAttribute( "text_alpha", QString::number(node->textColor().alpha())); nodes_root.appendChild(cn); } @@ -168,7 +169,8 @@ void GraphWidget::writeContentToXmlFile(const QString &fileName) 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())); +// cn.setAttribute( "alpha", QString::number(edge->color().alpha())); + cn.setAttribute( "width", QString::number(edge->width())); edges_root.appendChild(cn); } @@ -232,7 +234,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->key() == Qt::Key_K || // edge text color ( event->modifiers() & Qt::ControlModifier && // moving node ( event->key() == Qt::Key_Up || event->key() == Qt::Key_Down || @@ -260,11 +262,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_edgeColoring) +// { +// m_edgeColoring = false; +// m_parent->statusBarMsg(tr("Edge coloring cancelled.")); +// } else if(m_showingNodeNumbers) { m_hintNumber.clear(); @@ -423,11 +425,13 @@ void GraphWidget::keyPressEvent(QKeyEvent *event) { QColorDialog dialog(this); dialog.setWindowTitle(tr("Select node color")); - dialog.setOption(QColorDialog::ShowAlphaChannel); +// dialog.setOption(QColorDialog::ShowAlphaChannel); + dialog.setCurrentColor(m_activeNode->color()); if (dialog.exec()) { QColor color = dialog.selectedColor(); m_activeNode->setColor(color); + m_activeNode->edgeTo()->setColor(color); } break; @@ -437,7 +441,8 @@ void GraphWidget::keyPressEvent(QKeyEvent *event) { QColorDialog dialog(this); dialog.setWindowTitle(tr("Select text color")); - dialog.setOption(QColorDialog::ShowAlphaChannel); +// dialog.setOption(QColorDialog::ShowAlphaChannel); + dialog.setCurrentColor(m_activeNode->textColor()); if (dialog.exec()) { QColor color = dialog.selectedColor(); @@ -447,12 +452,12 @@ void GraphWidget::keyPressEvent(QKeyEvent *event) break; } - case Qt::Key_K: - { - m_parent->statusBarMsg(tr("Edge color: select other end-node.")); - m_edgeColoring = true; - break; - } +// case Qt::Key_K: +// { +// m_parent->statusBarMsg(tr("Edge color: select other end-node.")); +// m_edgeColoring = true; +// break; +// } default: QGraphicsView::keyPressEvent(event); @@ -502,6 +507,8 @@ void GraphWidget::insertNode() QPointF pos(length * cos(angle), length * sin(angle)); Node *node = new Node(this); + node->setColor(m_activeNode->color()); + node->setTextColor(m_activeNode->textColor()); node->setHtml(QString("")); m_scene->addItem(node); node->setPos(m_activeNode->sceneBoundingRect().center() + @@ -585,21 +592,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(); +// 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; - } +// Edge *edge = m_activeNode->edgeTo(node); +// if (edge) +// edge->setColor(color); +// } +// m_edgeColoring = false; +// } else { setActiveNode(node); @@ -615,7 +622,9 @@ void GraphWidget::addEdge(Node *source, Node *destination) } else { - m_scene->addItem(new Edge(source, destination)); + Edge *edge = new Edge(source, destination); + edge->setColor(destination->color()); + m_scene->addItem(edge); contentChanged(); } } diff --git a/graphwidget.h b/graphwidget.h index 2ead792..909b6f6 100644 --- a/graphwidget.h +++ b/graphwidget.h @@ -61,7 +61,7 @@ private: bool m_editingNode; bool m_edgeAdding; bool m_edgeDeleting; - bool m_edgeColoring; +// bool m_edgeColoring; bool m_contentChanged; QString m_fileName; diff --git a/node.cpp b/node.cpp index 23363ce..1d27ae3 100644 --- a/node.cpp +++ b/node.cpp @@ -26,6 +26,7 @@ Node::Node(GraphWidget *parent) : setFlag(ItemSendsGeometryChanges); setCacheMode(DeviceCoordinateCache); setDefaultTextColor(QColor(0,0,0)); + setZValue(2); } Node::~Node() @@ -112,16 +113,19 @@ void Node::setTextColor(const QColor &color) void Node::scale(const qreal &factor) { - qDebug() << factor * QGraphicsTextItem::scale(); - - if (factor * QGraphicsTextItem::scale() < 0.3 || - factor * QGraphicsTextItem::scale() > 5 ) + if (factor * QGraphicsTextItem::scale() < 0.2 || + factor * QGraphicsTextItem::scale() > 10 ) return; // it would make stuff difficult, like limiting the pos. inside scene // setTransformOriginPoint(boundingRect().center()); QGraphicsTextItem::setScale(factor * QGraphicsTextItem::scale()); + foreach(EdgeElement element, m_edgeList) + if (!element.startsFromThisNode) + element.edge->setWidth(element.edge->width() + + (factor>1 ? 2 : -2) ); + adjustEdges(); } @@ -279,6 +283,13 @@ QList Node::edgesFrom() const Edge * Node::edgeTo(const Node *node) const { + if (!node) + { + foreach(EdgeElement element, m_edgeList) + if (!element.startsFromThisNode) + return element.edge; + } + foreach(EdgeElement element, m_edgeList) if (element.edge->sourceNode() == node || element.edge->destNode() == node) diff --git a/node.h b/node.h index db3433c..0121f0e 100644 --- a/node.h +++ b/node.h @@ -40,7 +40,7 @@ public: QPointF intersect(const QLineF &line, const bool &reverse = false) const; QList edgesFrom() const; - Edge * edgeTo(const Node* node) const; + Edge * edgeTo(const Node* node = 0) const; protected: