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.

master
Denes Matetelki 14 years ago
parent 79bf16a352
commit eb97252d33

@ -13,8 +13,12 @@ const qreal Edge::m_arrowSize = 7;
Edge::Edge(Node *sourceNode, Node *destNode) Edge::Edge(Node *sourceNode, Node *destNode)
: m_angle(-1) : m_angle(-1)
, m_color(0,0,0)
, m_width(1)
{ {
setAcceptedMouseButtons(0); setAcceptedMouseButtons(0);
setZValue(1);
m_sourceNode = sourceNode; m_sourceNode = sourceNode;
m_destNode = destNode; m_destNode = destNode;
m_sourceNode->addEdge(this,true); m_sourceNode->addEdge(this,true);
@ -49,6 +53,15 @@ void Edge::setColor(const QColor &color)
update(); update();
} }
void Edge::setWidth(const qreal &width)
{
if (width < 1 || width > 100)
return;
m_width = width;
update();
}
void Edge::adjust() void Edge::adjust()
{ {
if (!m_sourceNode || !m_destNode) if (!m_sourceNode || !m_destNode)
@ -61,8 +74,10 @@ void Edge::adjust()
if (line.length() > qreal(20.)) if (line.length() > qreal(20.))
{ {
m_sourcePoint = m_sourceNode->intersect(line); // m_sourcePoint = m_sourceNode->intersect(line);
m_destPoint = m_destNode->intersect(line,true); // m_destPoint = m_destNode->intersect(line,true);
m_sourcePoint = m_sourceNode->sceneBoundingRect().center();
m_destPoint = m_destNode->sceneBoundingRect().center();
} else { } else {
m_sourcePoint = m_destPoint = line.p1(); m_sourcePoint = m_destPoint = line.p1();
@ -102,7 +117,7 @@ void Edge::paint(QPainter *painter,
// Draw the line itself // Draw the line itself
painter->setPen(QPen(m_color, painter->setPen(QPen(m_color,
1, m_width,
Qt::SolidLine, Qt::SolidLine,
Qt::RoundCap, Qt::RoundCap,
Qt::RoundJoin)); Qt::RoundJoin));
@ -111,13 +126,13 @@ void Edge::paint(QPainter *painter,
if (line.length() < m_arrowSize) if (line.length() < m_arrowSize)
return; return;
// Draw the arrows // Draw the arrow
QPointF destArrowP1 = m_destPoint + // QPointF destArrowP1 = m_destPoint +
QPointF(sin(m_angle - Edge::m_pi / 3) * m_arrowSize, // QPointF(sin(m_angle - Edge::m_pi / 3) * m_arrowSize,
cos(m_angle - Edge::m_pi / 3) * m_arrowSize); // cos(m_angle - Edge::m_pi / 3) * m_arrowSize);
QPointF destArrowP2 = m_destPoint + // QPointF destArrowP2 = m_destPoint +
QPointF(sin(m_angle - Edge::m_pi + Edge::m_pi / 3) * m_arrowSize, // 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); // cos(m_angle - Edge::m_pi + Edge::m_pi / 3) * m_arrowSize);
painter->setBrush(m_color); painter->setBrush(m_color);
painter->drawPolygon(QPolygonF() << line.p2() painter->drawPolygon(QPolygonF() << line.p2()

@ -15,8 +15,11 @@ public:
Node *sourceNode() const; Node *sourceNode() const;
Node *destNode() const; Node *destNode() const;
double getAngle() const; double getAngle() const;
void setColor(const QColor &color); void setColor(const QColor &color);
QColor color() const { return m_color; } QColor color() const { return m_color; }
void setWidth(const qreal &width);
qreal width() const { return m_width; }
void adjust(); void adjust();
@ -36,6 +39,7 @@ private:
QPointF m_destPoint; QPointF m_destPoint;
double m_angle; double m_angle;
QColor m_color; QColor m_color;
qreal m_width;
static const qreal m_arrowSize; static const qreal m_arrowSize;
static const double m_pi; static const double m_pi;

@ -25,7 +25,7 @@ GraphWidget::GraphWidget(MainWindow *parent) :
m_editingNode(false), m_editingNode(false),
m_edgeAdding(false), m_edgeAdding(false),
m_edgeDeleting(false), m_edgeDeleting(false),
m_edgeColoring(false), // m_edgeColoring(false),
m_contentChanged(false) m_contentChanged(false)
{ {
m_scene = new QGraphicsScene(this); m_scene = new QGraphicsScene(this);
@ -89,12 +89,12 @@ void GraphWidget::readContentFromXmlFile(const QString &fileName)
node->setScale(e.attribute("scale").toFloat()); node->setScale(e.attribute("scale").toFloat());
node->setColor(QColor(e.attribute("bg_red").toFloat(), node->setColor(QColor(e.attribute("bg_red").toFloat(),
e.attribute("bg_green").toFloat(), e.attribute("bg_green").toFloat(),
e.attribute("bg_blue").toFloat(), e.attribute("bg_blue").toFloat()/*,
e.attribute("bg_alpha").toFloat())); e.attribute("bg_alpha").toFloat()*/));
node->setTextColor(QColor(e.attribute("text_red").toFloat(), node->setTextColor(QColor(e.attribute("text_red").toFloat(),
e.attribute("text_green").toFloat(), e.attribute("text_green").toFloat(),
e.attribute("text_blue").toFloat(), e.attribute("text_blue").toFloat()/*,
e.attribute("text_alpha").toFloat())); e.attribute("text_alpha").toFloat()*/));
m_nodeList.append(node); m_nodeList.append(node);
} }
} }
@ -110,8 +110,9 @@ void GraphWidget::readContentFromXmlFile(const QString &fileName)
m_nodeList[e.attribute("destination").toInt()]); m_nodeList[e.attribute("destination").toInt()]);
edge->setColor(QColor(e.attribute("red").toFloat(), edge->setColor(QColor(e.attribute("red").toFloat(),
e.attribute("green").toFloat(), e.attribute("green").toFloat(),
e.attribute("blue").toFloat(), e.attribute("blue").toFloat()/*,
e.attribute("alpha").toFloat())); e.attribute("alpha").toFloat()*/));
edge->setWidth(e.attribute("width").toFloat());
m_scene->addItem(edge); 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_red", QString::number(node->color().red()));
cn.setAttribute( "bg_green", QString::number(node->color().green())); cn.setAttribute( "bg_green", QString::number(node->color().green()));
cn.setAttribute( "bg_blue", QString::number(node->color().blue())); 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_red", QString::number(node->textColor().red()));
cn.setAttribute( "text_green", QString::number(node->textColor().green())); cn.setAttribute( "text_green", QString::number(node->textColor().green()));
cn.setAttribute( "text_blue", QString::number(node->textColor().blue())); 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); nodes_root.appendChild(cn);
} }
@ -168,7 +169,8 @@ void GraphWidget::writeContentToXmlFile(const QString &fileName)
cn.setAttribute( "red", QString::number(edge->color().red())); cn.setAttribute( "red", QString::number(edge->color().red()));
cn.setAttribute( "green", QString::number(edge->color().green())); cn.setAttribute( "green", QString::number(edge->color().green()));
cn.setAttribute( "blue", QString::number(edge->color().blue())); 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); edges_root.appendChild(cn);
} }
@ -232,7 +234,7 @@ void GraphWidget::keyPressEvent(QKeyEvent *event)
event->key() == Qt::Key_D || // remove edge event->key() == Qt::Key_D || // remove edge
event->key() == Qt::Key_C || // node color event->key() == Qt::Key_C || // node color
event->key() == Qt::Key_T || // node text 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->modifiers() & Qt::ControlModifier && // moving node
( event->key() == Qt::Key_Up || ( event->key() == Qt::Key_Up ||
event->key() == Qt::Key_Down || event->key() == Qt::Key_Down ||
@ -260,11 +262,11 @@ void GraphWidget::keyPressEvent(QKeyEvent *event)
m_edgeDeleting = false; m_edgeDeleting = false;
m_parent->statusBarMsg(tr("Edge deleting cancelled.")); m_parent->statusBarMsg(tr("Edge deleting cancelled."));
} }
else if (m_edgeColoring) // else if (m_edgeColoring)
{ // {
m_edgeColoring = false; // m_edgeColoring = false;
m_parent->statusBarMsg(tr("Edge coloring cancelled.")); // m_parent->statusBarMsg(tr("Edge coloring cancelled."));
} // }
else if(m_showingNodeNumbers) else if(m_showingNodeNumbers)
{ {
m_hintNumber.clear(); m_hintNumber.clear();
@ -423,11 +425,13 @@ void GraphWidget::keyPressEvent(QKeyEvent *event)
{ {
QColorDialog dialog(this); QColorDialog dialog(this);
dialog.setWindowTitle(tr("Select node color")); dialog.setWindowTitle(tr("Select node color"));
dialog.setOption(QColorDialog::ShowAlphaChannel); // dialog.setOption(QColorDialog::ShowAlphaChannel);
dialog.setCurrentColor(m_activeNode->color());
if (dialog.exec()) if (dialog.exec())
{ {
QColor color = dialog.selectedColor(); QColor color = dialog.selectedColor();
m_activeNode->setColor(color); m_activeNode->setColor(color);
m_activeNode->edgeTo()->setColor(color);
} }
break; break;
@ -437,7 +441,8 @@ void GraphWidget::keyPressEvent(QKeyEvent *event)
{ {
QColorDialog dialog(this); QColorDialog dialog(this);
dialog.setWindowTitle(tr("Select text color")); dialog.setWindowTitle(tr("Select text color"));
dialog.setOption(QColorDialog::ShowAlphaChannel); // dialog.setOption(QColorDialog::ShowAlphaChannel);
dialog.setCurrentColor(m_activeNode->textColor());
if (dialog.exec()) if (dialog.exec())
{ {
QColor color = dialog.selectedColor(); QColor color = dialog.selectedColor();
@ -447,12 +452,12 @@ void GraphWidget::keyPressEvent(QKeyEvent *event)
break; break;
} }
case Qt::Key_K: // case Qt::Key_K:
{ // {
m_parent->statusBarMsg(tr("Edge color: select other end-node.")); // m_parent->statusBarMsg(tr("Edge color: select other end-node."));
m_edgeColoring = true; // m_edgeColoring = true;
break; // break;
} // }
default: default:
QGraphicsView::keyPressEvent(event); QGraphicsView::keyPressEvent(event);
@ -502,6 +507,8 @@ void GraphWidget::insertNode()
QPointF pos(length * cos(angle), length * sin(angle)); QPointF pos(length * cos(angle), length * sin(angle));
Node *node = new Node(this); Node *node = new Node(this);
node->setColor(m_activeNode->color());
node->setTextColor(m_activeNode->textColor());
node->setHtml(QString("")); node->setHtml(QString(""));
m_scene->addItem(node); m_scene->addItem(node);
node->setPos(m_activeNode->sceneBoundingRect().center() + node->setPos(m_activeNode->sceneBoundingRect().center() +
@ -585,21 +592,21 @@ void GraphWidget::nodeSelected(Node *node)
removeEdge(m_activeNode, node); removeEdge(m_activeNode, node);
m_edgeDeleting = false; m_edgeDeleting = false;
} }
else if (m_edgeColoring) // else if (m_edgeColoring)
{ // {
QColorDialog dialog(this); // QColorDialog dialog(this);
dialog.setWindowTitle(tr("Select edge color")); // dialog.setWindowTitle(tr("Select edge color"));
dialog.setOption(QColorDialog::ShowAlphaChannel); // dialog.setOption(QColorDialog::ShowAlphaChannel);
if (dialog.exec()) // if (dialog.exec())
{ // {
QColor color = dialog.selectedColor(); // QColor color = dialog.selectedColor();
Edge *edge = m_activeNode->edgeTo(node); // Edge *edge = m_activeNode->edgeTo(node);
if (edge) // if (edge)
edge->setColor(color); // edge->setColor(color);
} // }
m_edgeColoring = false; // m_edgeColoring = false;
} // }
else else
{ {
setActiveNode(node); setActiveNode(node);
@ -615,7 +622,9 @@ void GraphWidget::addEdge(Node *source, Node *destination)
} }
else else
{ {
m_scene->addItem(new Edge(source, destination)); Edge *edge = new Edge(source, destination);
edge->setColor(destination->color());
m_scene->addItem(edge);
contentChanged(); contentChanged();
} }
} }

@ -61,7 +61,7 @@ private:
bool m_editingNode; bool m_editingNode;
bool m_edgeAdding; bool m_edgeAdding;
bool m_edgeDeleting; bool m_edgeDeleting;
bool m_edgeColoring; // bool m_edgeColoring;
bool m_contentChanged; bool m_contentChanged;
QString m_fileName; QString m_fileName;

@ -26,6 +26,7 @@ Node::Node(GraphWidget *parent) :
setFlag(ItemSendsGeometryChanges); setFlag(ItemSendsGeometryChanges);
setCacheMode(DeviceCoordinateCache); setCacheMode(DeviceCoordinateCache);
setDefaultTextColor(QColor(0,0,0)); setDefaultTextColor(QColor(0,0,0));
setZValue(2);
} }
Node::~Node() Node::~Node()
@ -112,16 +113,19 @@ void Node::setTextColor(const QColor &color)
void Node::scale(const qreal &factor) void Node::scale(const qreal &factor)
{ {
qDebug() << factor * QGraphicsTextItem::scale(); if (factor * QGraphicsTextItem::scale() < 0.2 ||
factor * QGraphicsTextItem::scale() > 10 )
if (factor * QGraphicsTextItem::scale() < 0.3 ||
factor * QGraphicsTextItem::scale() > 5 )
return; return;
// it would make stuff difficult, like limiting the pos. inside scene // it would make stuff difficult, like limiting the pos. inside scene
// setTransformOriginPoint(boundingRect().center()); // setTransformOriginPoint(boundingRect().center());
QGraphicsTextItem::setScale(factor * QGraphicsTextItem::scale()); QGraphicsTextItem::setScale(factor * QGraphicsTextItem::scale());
foreach(EdgeElement element, m_edgeList)
if (!element.startsFromThisNode)
element.edge->setWidth(element.edge->width() +
(factor>1 ? 2 : -2) );
adjustEdges(); adjustEdges();
} }
@ -279,6 +283,13 @@ QList<Edge *> Node::edgesFrom() const
Edge * Node::edgeTo(const Node *node) 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) foreach(EdgeElement element, m_edgeList)
if (element.edge->sourceNode() == node || if (element.edge->sourceNode() == node ||
element.edge->destNode() == node) element.edge->destNode() == node)

@ -40,7 +40,7 @@ public:
QPointF intersect(const QLineF &line, const bool &reverse = false) const; QPointF intersect(const QLineF &line, const bool &reverse = false) const;
QList<Edge *> edgesFrom() const; QList<Edge *> edgesFrom() const;
Edge * edgeTo(const Node* node) const; Edge * edgeTo(const Node* node = 0) const;
protected: protected:

Loading…
Cancel
Save